←back to thread

420 points gnabgib | 2 comments | | HN request time: 0.448s | source
1. dahart ◴[] No.44005784[source]
Looks like gcc & clang use some of the bit-twiddling tricks when you compile the original function with -O3: https://godbolt.org/z/eshd9axod

    is_leap_year(unsigned int):
            xor     eax, eax
            test    dil, 3
            jne     .L1
            imul    edi, edi, -1030792151
            mov     eax, 1
            mov     edx, edi
            ror     edx, 2
            cmp     edx, 42949672
            ja      .L1
            ror     edi, 4
            cmp     edi, 10737418
            setbe   al
    .L1:
            ret
replies(1): >>44009070 #
2. ryao ◴[] No.44009070[source]
They are sometimes very good at using mathematical identities to do simplifications. The following commit was actually inspired by the output of GCC:

https://github.com/openzfs/spl/commit/8fc851b7b5315c9cae9255...

Jason had noticed that GCC’s assembly output did not match the original macro when looking for a solution to the unsigned integer overflow warning that a PaX GCC plugin had output (erroneously in my opinion). He had conjectured we could safely adopt GCC’s version as a workaround. I gave him the proof of correctness for the commit message and it was accepted into ZFS. As you can see from the proof, deriving that from the original required 4 steps. I assume that GCC had gone through a similar process to derive its output.