A leap year check in three instructions

2 months ago (hueffner.de)

> return ((y * 1073750999) & 3221352463) <= 126976;

> How does this work? The answer is surprisingly complex.

I don't think anyone is surprised in the complexity of any explanation for that algorithm :D

> Note that modern compilers like gcc or clang will produce something like is_leap_year2 from is_leap_year1, so there is not much point in doing this in C source, but it might be useful in other programming languages.

The optimizations that compilers can achieve kind of amaze me.

Indeed, the latest version of cal from util-linux keeps it simple in the C source:

  return ( !(year % 4) && (year % 100) ) || !(year % 400);

https://github.com/util-linux/util-linux/blob/v2.41/misc-uti...

  • But this is wrong and can only represent dates after the specific year when they switched from the Julian to Gregorian calendar!

    For more on this, I recommend reading and implementing a function that calculates the day of the week [1]. Then you can join me in the special insanity hell of people that were trying to deal with human calendars.

    And then you should implement a test case for the dates between Thursday 4 October 1582 and Friday 15 October 1582 :)

    [1] https://en.m.wikipedia.org/wiki/Determination_of_the_day_of_...

    • > the specific year

      The problem is, which "specific" year? The English were using "old-style" dates long after 1582. Better not to try to solve this intractable problem in software, but instead annotate every old date you receive with its correct calendar, which may even be a proleptic Gregorian calendar in some fields of study.

      (How do you determine the correct calendar? Through careful inspection of context! Alas, people writing the dates rarely indicated this, and later readers tend to get the calendars hopelessly mangled up. Not to mention the changes in the start of the year. At least the day of week can act as an indicator, when available.)

    • The full code is

         static int leap_year(const struct cal_control *ctl, int32_t year)
         {
          if (year <= ctl->reform_year)
           return !(year % 4);
      
          return ( !(year % 4) && (year % 100) ) || !(year % 400);
         }
      

      Where reform_year is the year the Gregorian calendar was adopted in the specific context specified (defaults to 1752 which is the year it was adopted by GB and therefore also the US).

      So it does account for Julian dates.

      2 replies →

  • I like how the linux one is also easier to understand because it doesn't perform three sequential checks which actually invert the last two conditions plus a default return. That's the kind of stuff that can make you crazy if you ever have to debug it.

    • I wondered 3 minutes "this is not right" til I realized that

        if ((y % 25) != 0) return true;
      

      was actually checking for different from 0 (which in hindsight makes also sense because the century years by default are not leap unless they divide by 400)

I love these incomprehensible magic number optimizations. Every time I see one I wonder how many optimizations like this we missed back in the old days when we were writing all our inner loops in assembly?

Does anyone have a collection of these things?

  • Here is a short list:

    https://graphics.stanford.edu/~seander/bithacks.html

    It is not on the list, but #define CMP(X, Y) (((X) > (Y)) - ((X) < (Y))) is an efficient way to do generic comparisons for things that want UNIX-style comparators. If you compare the output against 0 to check for some form of greater than, less than or equality, the compiler should automatically simplify it. For example, CMP(X, Y) > 0 is simplified to (X > Y) by a compiler.

    The signum(x) function that is equivalent to CMP(X, 0) can be done in 3 or 4 instructions depending on your architecture without any comparison operations:

    https://www.cs.cornell.edu/courses/cs6120/2022sp/blog/supero...

    It is such a famous example, that compilers probably optimize CMP(X, 0) to that, but I have not checked. Coincidentally, the expansion of CMP(X, 0) is on the bit hacks list.

    There are a few more superoptimized mathematical operations listed here:

    https://www2.cs.arizona.edu/~collberg/Teaching/553/2011/Reso...

    Note that the assembly code appears to be for the Motorola 68000 processor and it makes use of flags that are set in edge cases to work.

    Finally, there is a list of helpful macros for bit operations that originated in OpenSolaris (as far as I know) here:

    https://github.com/freebsd/freebsd-src/blob/master/sys/cddl/...

    There used to be an Open Solaris blog post on them, but Oracle has taken it down.

    Enjoy!

    • For an entire book on this stuff, see Henry S. Warren Jr's Hackers Delight. The "three valued compare function" is in chapter 2, for example.

    • > It is not on the list, but #define CMP(X, Y) (((X) > (Y)) - ((X) < (Y))) is an efficient way to do generic comparisons for things that want UNIX-style comparators. If you compare the output against 0 to check for some form of greater than, less than or equality, the compiler should automatically simplify it. For example, CMP(X, Y) > 0 is simplified to (X > Y) by a compiler.

      I guess this only applies when the compiler knows what version of > you are using?

      Eg it might not work in C++ when < and > are overloaded for eg strings?

      7 replies →

  • We didn't miss them. In those days they weren't optimizations. Multiplications were really expensive.

    • Multiplications of this word length, one should clarify. It's not that multiplication was an inherently more expensive or different operation back then (assuming from context here that the "old days" of coding inner loops in assembly language pre-date even the 32-bit ALU era). Binary multiplication has not changed in millennia. Ancient Egyptians were using the same binary integer multiplication logic 5 millennia ago as ALUs do today.

      It was that generally the fast hardware multiplication operations in ALUs didn't have very many bits in the register word length, so multiplications of wider words had to be done with library functions that did long multiplication in (say) base 256.

      So this code in the headlined article would not be "three instructions" but three calls to internal helper library functions used by the compiler for long-word multiplication, comparison, and bitwise AND; not markedly more optimal than three internal helper function calls for the three original modulo operations, and in fact less optimal than the bit-twiddled modulo-powers-of-2 version found halfway down the headlined article, which would only need check the least significant byte and not call library functions for two of the 32-bit modulo operations.

      Bonus points to anyone who remembers the helper function names in Microsoft BASIC's runtime library straight off the top of xyr head. It is probably a good thing that I finally seem to have forgotten them. (-: They all began with "B$" as I recall.

      4 replies →

    • Related, Computerphile had a video a few months ago where they try to put compute time relative to human time, similar to the way one might visualize an atom by making the proton the size of a golfball. I think it can help put some costs into perspective and really show why branching maters as well as the great engineering done to hide some of the slowdowns. But definitely some things are being marked simply by the sheer speed of the clock (like how the small size of a proton hides how empty an atom is)

        https://youtube.com/watch?v=PpaQrzoDW2I

Part-way through the section on bit-twiddling, I thought to myself "Oh I wonder if we could use a solver here". Lo and behold, I was pleasantly surprised to see the author then take that exact approach. Love the attention to detail in this post!

If you need to know a leap year and it's before the year 6000, I made an interactive calculator and visualization [1].

It's >3 machine instructions (and I admire the mathematical tricks included in the post), but it does do thousands of calculations fairly quickly :)

[1] https://calculang.dev/examples-viewer?id=leap-year

  • Please make stuff fit into viewport width, hard to use on mobile as it stands :)

    • Thanks for the prompt! I made a new gallery that's mobile friendly and I almost forgot why I need to land it - adding it to the list!

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

  • 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.

There are many cute binary/logic tricks, if you like them be sure to read Hackers Delight and https://graphics.stanford.edu/~seander/bithacks.html . Once you've studied enough of them you'll find yourself easily coming up with more.

Warning: This may increase or decrease your popularity with fellow programmers, depending on how lucky you are in encountering problems where they make an important performance difference rather than a readability problem for people who have not deeply internalized bit twiddling.

Multiply and mask for varrious purposes is a thing I commonly use in my own code-- it's much more attractive now that it was decades ago because almost all computers we target these days have extremely fast multipliers.

These full-with logic operations and multipliers give you kind of a very parallel computer packed into a single instruction. The only problem is that it's a little tricky to program. :)

At least this one was easy to explain mechanically. Some bit hacks require p-adic numbers and other elements of number theory to explain.

Taking a look at numbers in binary reveals some interesting patterns. Although seems obvious, it was interesting to me when I realized that all prime numbers except 2 end with 1.

  • Not trying to be a jerk, but why is that interesting? Am I missing something more than all odd numbers end in 1, and primes by their nature cannot be even(except 2, as you mentioned).

This is so cool!

Terrible nitpick, but this is actually 3 operations, not instructions. On x86 you get 4:

  is_leap_year_fast:
        imul    eax, edi, 1073750999
        and     eax, -1073614833
        cmp     eax, 126977
        setb    al
        ret

On ARM you get a bit more due to instruction encoding:

  is_leap_year_fast:
        ldr     r1, .LCPI0_0
        mul     r0, r0, r1
        ldr     r1, .LCPI0_1
        and     r1, r0, r1
        mov     r0, #0
        cmp     r1, #126976
        movwls  r0, #1
        bx      lr
  .LCPI0_0:
        .long   1073750999
  .LCPI0_1:
        .long   3221352463

Compiler explorer reference: https://godbolt.org/z/7ajYqbT9z

  • You could argue that the setb and ret are not part of the leap year check itself. For example if the compiled inlined the call into a caller doing:

        if(is_leap_year_fast()) {...}
    

    Then the ret would obviously go away and the setb wouldn't be necessary as it could generate directly a conditional jmp from the result of the cmp.

Somewhat relevant and related.

>“So, it’s a bug in Lotus 123?”

>“Yeah, but probably an intentional one. Lotus had to fit in 640K. That’s not a lot of memory. If you ignore 1900, you can figure out if a given year is a leap year just by looking to see if the rightmost two bits are zero. That’s really fast and easy. The Lotus guys probably figured it didn’t matter to be wrong for those two months way in the past. It looks like the Basic guys wanted to be anal about those two months, so they moved the epoch one day back.”

https://www.joelonsoftware.com/2006/06/16/my-first-billg-rev...

Interesting. In one place the author argues: 0 is missing, but we already know...

The is no year 0, it goes 1 BC, 1 AD. So testing whether 0 is a leap year is moot.

  • > The is no year 0, it goes 1 BC, 1 AD. So testing whether 0 is a leap year is moot.

    Not true if you use astronomical year numbering: https://en.m.wikipedia.org/wiki/Astronomical_year_numbering

    Which is arguably the right thing to do outside of specific domains (such as history) in which BCE is entrenched

    If your software really has to display years in BCE, I think the cleanest way is store it as astronomical year numbering internally, then convert to CE/BCE on output

    • > Astronomers use the Julian calendar for years before 1582, including the year 0, and the Gregorian calendar for years after 1582

      So what happens when it's 1582? (sorry, currently no time to articulate a good wiki fix)

      2 replies →

  • Go back to the start of the article, and you'll find that using the proleptic Gregorian calendar with astronomical year numbering is a premise for the algorithm.

    Without that design constraint, testing for leap years becomes locale-dependent and very complex indeed.

  • ISO8601 accepts year 0. It is 1 BC in astronomical calendars. All the BC years gain a -1 offset as a result.

    • Interesting, how standards just ignore reality.

      At work we had discussions what date format to use in our product. It's for trained users only (but not IT people), English UI only, but used on several continents. Our regulatory expert propsed ISO8601. I did not agree, because that is not used anywhere in daily life except by 8 millions Swedes. I voted 15-Apr-2025 is much less prone to human error. (None of us "won". Different formats in different places still...)

      3 replies →

  • https://listverse.com/2019/05/19/10-bizarre-calendar-fixes-t...

    Everything before the introduction of the gregorian calendar is moot:

    "In 1582, the pope suggested that the whole of Europe skip ten days to be in sync with the new calendar. Several religious European kingdoms obeyed and jumped from October 4 to October 15."

    So you cannot use any date recorded before that time for calculations.

    And before that it gets even more random:

    "The priests’ observations of the lunar cycles were not accurate. They also deliberately avoided leap years over superstitions. Things got worse when they started receiving bribes to declare a year longer or shorter than necessary. Some years were so long that an extra month called Intercalaris or Mercedonius was added."

    • Before 1582 the rule is just simpler. If it is divisible by 4 it's a leap year. So the difference is relevant for years 300, 500, 600, 700, 900 etc. For ranges spanning those years the Gregorian algorithm would result in results not matching reality.

      When the Julian calendar was really adopted I don't know. Certainly not 0001-01-01. And of course it varies by country like Gregorian.

      2 replies →

This is gold! HN Kino! Taking the hardest problem in the world, date checking, and casually bitflipping the hell out of it. Hats off man :D

Knowing how to use z3 for stuff like this is a superpower that not a lot of people have, but is definitely worth knowing if you work with code that needs to be optimized at this level. I have an mcp script that interfaces with z3, and this comment is a reminder to myself to find some time to expand it in the future for this specific flow.

It’s also worth calling out angr as an interface between capstone and z3, which can take this to another level.

One of these days, someone will get creative and do this sort of thing for the leap year algorithm of the Revised Julian Calendar instead of the Gregorian one.

The original function is likely only going to be 3 instructions. xor, test, jne and only 1 of these is dependent on a previous instruction. In the "fast" version from the article there are 4 instructions with each depending on the previous instruction. I'm not surprised it lost in the benchmark.

  • A branch that triggers 3/4 of the time will not perform well.

    Whether that matters comes down to how this function integrates into the rest of the program.

    • I don't think the years tested will be random. I think practically it will see long strings of the same value.

tha page seems to have problems with layout overflow in equation blocks on mobile. It seems that because spans are inline elements they won't overflow. I think you can make them block elements and enable some form of overflow to solve it.

Code is written not just for computer to execute, but for other people to read and understand. If you put such code readability will suffer tremendously. Beside that, what are you doing in your code that you need to optimize leap year check?! This clever tricks it remind me phrase that lead to many accidents- 'Hold my beer and see what I can do!'

This reminds me of once when I was giving an algo/ds interview (in Java) and the interviewer started asking me questions where one of the answers usually was “to be memorised” shit like this and he started pestering me to give him those answers (even though I said I don’t know and definitely don’t recall) and that too in C. As per him “everyone who coded knew C.. at least in college” and started becoming a bit more hostile. I think it was the first interview that I had ended as an interviewee.

And I call this thing “bit gymnastics”.

  • Sounds like someone desperate to prove their superiority - now imagine working with them every day.

    • God, everyone has one of these stupid stories, don't they? For me, it was some moron at a Wordpress sweatshop who pointed out that the way to find the number of parameters of a function was through (a now deprecated and unsupported!) arity property.

      OK, great. 16 years into my career, and I've never needed to count the number of parameters of a JavaScript function. And if you needed to, that wasn't going to be the property you'd read anyway.

    • It is not they way todo it agreed on that, but it can be one of the only way of getting hand wavy persons to get technical. Interviewing can be frustrating when you do not see what you expected. So no need to believe it was done toxicly.

      2 replies →

Never thought a leap year check could be this interesting. Maybe low-level programmers had already discovered tricks like this long ago,they just never got written down? Feels like there’s still so much like this, hidden in old code, waiting to be rediscovered. If anyone has a collection of these kinds of techniques, I’d really love to dig into it.

  • Lotus 1-2-3 famously considers 1900 a leap year (carried over in Excel for compatibility), presumably because it just does the "(y & 3) != 0" check (arguably a reasonable optimisation given the hardware of the time).

    I'd be surprised if someone found this solution before, as it seems both relatively difficult to find and a small optimisation.

  • There are things I had to learn at home in the 80s on the z80 that I have mostly forgotten. Very occasionally I wheel something out to “show the kids” (those in their 20s) and it feels like performing a magic trick.

I tend to be of the opinion that for modern general purpose CPUs in this era, such micro-optimizations are totally unnecessary because modern CPUs are so fast that instructions are almost free.

But do you know what's not free? Memory accesses[1]. So when I'm optimizing things, I focus on making things more cache friendly.

[1] http://gec.di.uminho.pt/discip/minf/ac0102/1000gap_proc-mem_...

  • > I tend to be of the opinion that for modern general purpose CPUs in this era, such micro-optimizations are totally unnecessary because modern CPUs are so fast that instructions are almost free.

    What does this mean? Free? Optimisations are totally unnecessary because... instructions are free?

    The implementation in TFA is probably on the order of 5x more efficient than a naive approach. This is time and energy as well. I don't understand what "free" means in this context.

    Calendar operations are performed probably trillions of times every second across all types of computers. If you can make them more time- and energy-efficient, why wouldn't you?

    If there's a problem with modern software it's too much bloat, not too much optimisation.

    • GP made an important point that you seemed to have missed: in modern architectures, it’s much more important to minimize memory access than to minimize instructions. They weren’t saying optimization isn’t important, they were describing how to optimize on modern systems.

    • If this is indeed done trillions of times a second, which I frankly have a hard time believing, then sure, it might be worth it. But on a modern CPU, focusing on an optimization like this is a poor use of developer resources. There are likely several other optimizations related to cache locality that you could find in less time than it would take to do this, and those other optimizations would probably give several orders of magnitude more improvement.

      Not to mention that the final code is basically a giant WTF for anybody reading it. It will be an attractive nuisance that people will be drawn to, like moths to a flame, any time there is a bug around calendar operations.

      2 replies →

  • Just because CPU performance is increasing faster than DRAM speeds doesn't mean that CPU performance is "free" while memory is "expensive". One thing that you're ignoring is the impact of caches and prefetching logic which have significantly improved the performance of memory-bound workloads in the last 5-10 years. DRAM might be slow, but if you avoid going out to DRAM...

    More broadly, it 100% depends on your workload. You'd be surprised at how many workloads are compute-bound, even today: LLM inference might be memory bound (thus how it's possible to get remotely good performance on a CPU), but training, esp. prefill, is very much not. And once you get out of LLMs, I'd say that most applications of ML tend to be compute bound.

  • To temper this slightly, these sorts of optimizations are useful on embedded CPUs for device firmware, IOT, etc. I've worked on smart NIC CPUs where cycles were so precious we'd do all kinds of crazy unreadable things.

    • I suspect most IOT device manufacturers expect/design their device to be landfill before worrying about leap year math. (In my least optimistic moments, I suspect some of them may intentionally implement known broken algorithms that make their eWaste stop working correctly at some point in the near future that's statistically likely to bear beyond the warranty period.)

      2 replies →

    • on the flip side of the topic, trying to do any datetime handling on the edge of embedded compute is going to be wrong 100% of the time anyway

      1 reply →

  • The thing is about these optimisations (assuming they test as higher performance) is that they can get applied in a library and then everyone benefits from the speedup that took some hard graft to work out. Very few people bake their own date API nowadays if they can avoid it since it already exists and techniques like this just speed up every programme whether its on the critical path or not.

    • That's basically compilers these days. It used to be that you could try and optimize your code, inline things here and there, but these days, you're not going to beat the compiler optimization.

      18 replies →

  •   > modern CPUs are so fast that instructions are almost free.
    

    Please don't.

    These things compound. You especially need to consider typical computer usage involves using more than one application at a time. There's a tragedy of the commons issue that's often ignored. It can be if you're optimizing your code (you're minimizing your share!) but it can't be if you're not.

    I guarantee you we'd have a lot of faster things if people invested even a little time (these also compound :). Two great examples might be Llama.cpp and FlashAttention. Both of these have had a huge impact of people (among a number of other works) but don't get nearly the same attention as other stuff. These are popular instances but I promise you that there's a million problems like these waiting to be solved. It's just not flashy, but hey plumbers and garbagemen are pretty critical jobs too

    • You haven't refuted the parent comment at all. They asserted that instructions are insignificant, and other things, such as memory accesses, dominate.

      3 replies →

  • > such micro-optimizations are totally unnecessary because modern CPUs are so fast that instructions are almost free.

    I'm amazed by the fact there is always someone who will say that such optimization are totally unnecessary.

    • I'm amazed by the fact there is always someone who misinterprets Knuth's "premature optimization", reading as "don't optimize" instead of "pull out the profiler"

    • Some people have significant positions on CPU manufacturers, so there will always be at least a few.

  • It's true that this code was optimized from 2.6ns down to 0.9ns, a saving of 1.7ns, while an L2 cache miss might be 80ns. But 1.7ns is still about 2% of the 80ns, and it's about 70% of the 2.6ns. You don't want to start optimizing by reducing things that are 2% of your cost, but 2% isn't insignificant.

    The bigger issue is that probably you don't need to do leap-year checks very often so probably your leap-year check isn't the place to focus unless it's, like, sending a SQL query across a data center or something.

  • They're not free at all. They're unlikely to create noticeable latency; however, CPU clock speeds and thus power consumption haven't been constant for years now. You are paying for that lack of optimization and mobile users would thank you to do it.

    There is no silver bullet.

  • Related

    > The world could run on older hardware if software optimization was a priority

    https://news.ycombinator.com/item?id=43971464

    • I disagree.

      This is the kind of optimization that makes you need a 1.5MHz CPU instead of a 1MHz CPU, but saves devs weeks of effort. It's the kind of thing you give up when you move optimization from priority 1 to 2, or from 2 to 3. It would still run blazingly fast on a 30 year old computer. It's a perfectly good tradeoff.

      The stuff that bogs down modern hardware is optimization being priority 8 or not even on the list of considerations.

  • Integer division (and modulo) is not cheap on most CPUs. Along with memory access and branch prediction, it is something worth optimizing for.

    And since you are talking about memory. Code also goes in memory. Shorter code is more cache friendly.

    I don't see a use case where it matters for this particular application (it doesn't mean there isn't) but well targeted micro-optimizations absolutely matter.

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))); }