Most active commenters
  • kragen(4)

←back to thread

420 points gnabgib | 12 comments | | HN request time: 3.596s | source | bottom
1. captaincrunch ◴[] No.44000123[source]
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))); }

replies(3): >>44000304 #>>44000522 #>>44003153 #
2. andrepd ◴[] No.44000304[source]
This impl is mentioned in TFA.. It's much slower and includes branches.
replies(1): >>44000495 #
3. hoten ◴[] No.44000495[source]
I'd expect even without optimizations on, there wouldn't be branches in the output for that code.
replies(1): >>44000544 #
4. kragen ◴[] No.44000522[source]
You commented out your entire function body and the closing }. Also, on 32-bit platforms, it doesn't stop working at 65535.
replies(1): >>44001008 #
5. kragen ◴[] No.44000544{3}[source]
There are, even with optimizations on. You could have checked: https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename...

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.

replies(1): >>44000615 #
6. mmozeiko ◴[] No.44000615{4}[source]
If you change logic and/or to bitwise and/or then it'll be branchless.
replies(1): >>44014827 #
7. captaincrunch ◴[] No.44001008[source]
just a formatting issue on my side, there were \n.
replies(2): >>44003548 #>>44007925 #
8. windward ◴[] No.44003153[source]
>READABLE

Great. It will be useful for the exhaustive tests of the faster version.

9. archargelod ◴[] No.44003548{3}[source]
This website eats newlines, unless you double them (one of the annoying features of markdown). You can use codeblocks by putting 4 spaces before each line:

    int main() {
        // this should be properly formatted
        return 0;
    };
10. kragen ◴[] No.44007925{3}[source]
If you fix it, other people can test your code without having to fix the syntax themselves first.
11. kragen ◴[] No.44014827{5}[source]
True: https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename... but I understood hoten to be saying that compilers would generally produce that version from the short-circuiting version, and they don't.
replies(1): >>44017765 #
12. hoten ◴[] No.44017765{6}[source]
Yeah I was wrong.

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?