Get the latest tech news

A leap year check in three instructions


How to test for leap years (until year 102499) in the proleptic Gregorian calendar with just three 32-bit instructions, with detailed explanation of the bit-level tricks.

I couldn't immediately find a way to prove it, so I employed the tried-and-true method of getting someone else to do it for me by posting it to the Code Golf StackExchange. The new function is_leap_year_fast is 3.8 times faster than the standard implementation for random data, and about 6% slower for the perfectly predictable input. To fully justify a change, ideally we would have a benchmark with realistic data that employs a leap year check as a subroutine, rather than just microbenchmarking.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of instructions

instructions

Photo of leap year check

leap year check

Related news:

News photo

RISC-V With Linux 6.15 Adds Support For BFloat16 "BF16" Instructions

News photo

Bringing the Instructions to the Data

News photo

Instructions for making the Sun: How the race for nuclear fusion has accelerated | Driven by the latest scientific advances and a huge wave of investment in private projects, the dream of generating energy by replicating the processes that keep the stars alight is no longer science fiction