(no title)
abainbridge | 2 months ago
int foo(char const *s) {
if (s[0] == 'h' && s[1] == 'e' && s[2] == 'l' && s[3] == 'l')
return 1;
return 0;
}
The outputs 4 cmp instructions here, even though I'd have thought 1 was sufficient. https://godbolt.org/z/hqMnbrnKe
ynik|2 months ago
If you use `&` instead of `&&` (so that all array elements are accessed unconditionally), the optimization will happen: https://godbolt.org/z/KjdT16Kfb
(also note you got the endianness wrong in your hand-optimized version)
zrm|2 months ago
But then you're accessing four elements of a string that could have a strlen of less than 3. If the strlen is 1 then the short circuit case saves you because s[1] will be '\0' instead of 'e' and then you don't access elements past the end of the string. The "optimized" version is UB for short strings.
kragen|2 months ago
abainbridge|2 months ago
> (also note you got the endianness wrong in your hand-optimized version) Doh :-)
NooneAtAll3|2 months ago
abbeyj|2 months ago
Interestingly, it is comparing against a different 32-bit value than `bar` does. I think this is because you accidentally got the order backwards in `bar`.
The code in `bar` is probably not a good idea on targets that don't like unaligned loads.
raphlinus|2 months ago
abainbridge|2 months ago