我正在使用编译器资源管理器,发现这两个函数在gcc和clang中生成不同的程序集.我预计在内联后它们会产生相同的表达树,因此会产生相同和最佳的装配.
constexpr bool is_nonzero_decimal_digit(char const c) noexcept { return c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'; } bool is_decimal_digit_v1(char const c) noexcept { return c == '0' || is_nonzero_decimal_digit(c); } bool is_decimal_digit_v2(char const c) noexcept { return c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'; }
Clang 3.9.1 -std = c ++ 1z -O3结果
is_decimal_digit_v1(char): cmp dil, 48 sete cl add dil, -49 cmp dil, 9 setb al or al, cl ret is_decimal_digit_v2(char): add dil, -48 cmp dil, 10 setb al ret
gcc 6.3 -std = c ++ 1z -O3结果
is_decimal_digit_v1(char): cmp dil, 48 je .L3 sub edi, 49 cmp dil, 8 setbe al ret .L3: mov eax, 1 ret is_decimal_digit_v2(char): sub edi, 48 cmp dil, 9 setbe al ret
那么,这是优化者的怪癖还是禁止优化的语言规则的结果?