A leap year check in three instructions
Micro-optimization vs. Practical Impact
- Many commenters enjoy the “magic number” leap-year trick as a fun exercise, but question its real-world value.
- Several argue modern general-purpose CPUs make such micro-optimizations rarely worth developer time; memory access patterns, cache locality, and branch behavior usually dominate.
- Others push back: CPU instructions aren’t “free” (especially for power use), small wins compound across trillions of date operations, and such tricks are ideal to bury inside standard libraries where everyone benefits.
- Embedded / IoT and specialized firmware are highlighted as domains where every cycle really can matter, so bit-level tricks still pay off.
Division, Multiplication, and Hardware Realities
- Historical context: on older and especially 8‑bit CPUs, multiplication and division (and therefore modulo) were extremely expensive or even absent, forcing shift/add/boolean tricks.
- Modern CPUs have fast multipliers but integer division and modulo are still relatively slow, so removing them can be worthwhile.
- Branches used to be cheap; now mispredictions are costly. Branchless expressions via bitwise ops are often preferred.
Compilers, Superoptimization, and Solvers
- Several note that modern compilers already transform naïve leap-year code into sophisticated branchless sequences using multiplication, rotates, and comparisons.
- There’s discussion of superoptimization (using search/SMT solvers like Z3) to discover minimal instruction sequences, exactly as in the article.
- Some skepticism remains: compilers don’t always produce optimal code (examples: binary search, memset-elision, AVR codegen), and hand-tuned assembly still beats them in hot paths.
Readability, Maintainability, and Culture
- Strong concern that such code is “bit gymnastics” and unreadable; it risks becoming a debugging hazard unless very well commented and thoroughly tested.
- Several stress Knuth’s “premature optimization” as “profile first,” not “never optimize.”
- Anecdotes about hostile interviews demanding memorized bit tricks reinforce that these techniques can be misused as gatekeeping rather than applied engineering.
Calendar Semantics and Year 0
- Debate over year 0: civil Gregorian calendar has no year 0, but astronomical year numbering and ISO 8601 do.
- Multiple comments note that real-world date handling must consider Julian vs. Gregorian cutovers, country-specific reform years, and skipped days—far beyond a pure proleptic Gregorian leap-year rule.