This is fast, READABLE, and accurate:
bool is_leap_year(uint32_t y) { // Works for Gregorian years in range [0, 65535] return ((!(y & 3)) && ((y % 25 != 0) || !(y & 15))); }
bool is_leap_year(uint32_t y) { // Works for Gregorian years in range [0, 65535] return ((!(y & 3)) && ((y % 25 != 0) || !(y & 15))); }
I didn't find any way to get a compiler to generate a branchless version. I tried clang and GCC, both for amd64, with -O0, -O5, -Os, and for clang, -Oz.
Do we know why the compiler doesn't do it? Surely the output is the same and avoiding branches is clearly faster.
Maybe short circuiting requires such an optimization not be made?
Great. It will be useful for the exhaustive tests of the faster version.
int main() {
// this should be properly formatted
return 0;
};