[HN Gopher] A leap year check in three instructions
       ___________________________________________________________________
        
       A leap year check in three instructions
        
       Author : gnabgib
       Score  : 53 points
       Date   : 2025-05-15 21:57 UTC (1 hours ago)
        
 (HTM) web link (hueffner.de)
 (TXT) w3m dump (hueffner.de)
        
       | qingcharles wrote:
       | 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?
        
         | masfuerte wrote:
         | We didn't miss them. In those days they weren't optimizations.
         | Multiplications were really expensive.
        
           | kurthr wrote:
           | and divides were worse. (1 cycle add, 10 cycle mult, 60 cycle
           | div)
        
             | genewitch wrote:
             | That's fair but mod is division, or no? So realistically
             | the new magic number version would be faster. Assuming
             | there is 32 bit int support. Sorry, this is above my
             | paygrade.
        
         | tylerhou wrote:
         | You should look at supercompilation.
        
         | owl_vision wrote:
         | there is "Hacker's Delight" by Henry S. Warren, Jr.
         | 
         | https://en.wikipedia.org/wiki/Hacker's_Delight
        
       | captaincrunch wrote:
       | 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))); }
        
       | drewg123 wrote:
       | 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_...
        
       | 22c wrote:
       | 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!
        
       ___________________________________________________________________
       (page generated 2025-05-15 23:00 UTC)