3-instruction weekday calculation beats compiler output

A faster way to calculate the day of the week

3-instruction weekday calculation beats compiler output

Converting a day-count to the day of the week sounds trivial, but it's surprisingly complex. This article presents a range of really fast functions for the task, tuned for different use cases. The fastest algorithms achieve latency of just a single multiplication plus two cycles, and one surprising result shows that ISO format (1–7) can be computed with the same instructions as Unix format (0–6) with zero speed penalty. The techniques generalize to other Mersenne divisors like 24 and 60, and new fast modulus methods are introduced.

A surprising result is presented: the weekday can be computed in ISO format ([1‥7] instead of [0‥6]), with the exact same instructions, just with tweaked constants (and zero speed penalty).
  1. walrus01

    On a slightly related topic I was recently thinking of how simple it is to only have to worry about one calendar in your personal and business life (the western gregorian calendar). If you're an even slightly observant Muslim with an educated techincal/professional life in many parts of the historical Persian speaking world, you have three calendars to keep track of:

    1) Western gregorian calendar (doing business with foreigners, international travel, banking, finance, most everything done on the Internet, etc).

    2) Islamic arabic originated lunar calendar (When does Ramadan start, when is an okay time to go for hajj, when is Eid Al-Fitr, when is Eid Al-Adha, etc). This goes "backwards" 10 to 11 days per year compared to the western gregorian calendar so in different decades Ramadan might be in the middle of summer, or middle of winter.

    3) Persian solar calendar for new year observances like Nowruz: https://en.wikipedia.org/wiki/Nowruz , and generally persian-related cultural things: https://en.wikipedia.org/wiki/Solar_Hijri_calendar

    But wait, there's more! Let's say you live in Uzbekistan or Tajikistan or that region and you do business with Russians. You might have to think about when the Russians are on vacation and businesses are closed, because they're taking holidays and religious observances based on the eastern orthodox calendar. Basically all the same problem as trying to get business things done in the UK on a UK standard "Bank Holdiay". Their Christmas is not the sam […]

  2. dfdsvfdvd

    if day = "friday" then output = :)

    else output = :(

  3. rmunn

    Excellent article, and very useful for programming. For figuring out the day of the week in your head, there's a different (and very useful) trick. It uses the month and day from the Gregorian calendar, rather than the day number, because 99% of the time that's what you'll be wanting: "Which day of the week will August 31st, 2026 be?"

    The trick is to notice that in any year, leap year or not, the following dates will be the same day of the week in any year: 4/4, 6/6, 8/8, 10/10, and 12/12. (Whether you use the month/day style favored in America, or the day/month style that the rest of the world uses, either way those dates are the same, which is convenient). Which day of the week those dates are will vary from year to year: this year (2026) those are all Saturdays, last year they were all Fridays, next year they will all be Sundays. But in any given year, those dates are separated by 63 days, a multiple of seven, so they are always going to be the same day of the week.

    And that's not all: look at 5/9, 9/5, 7/11, and 11/7 — those are also that same day of the week! (Again, conveniently, this works whether you're using the American calendar style or the rest of the world). Those odd-numbered dates can easily be remembered with the mnemonic phrase "a 9-to-5 job at 7-11". And voila, you have memorized one "special" day in nine of the twelve months. (What you do for January, February, and March I will cover later).

    Now I'll pause and explain why this is useful. Many of you have al […]

  4. srean

    If only the periods of the annual motion of the sun, the periodicity of the phases of the Moon and the unit of day were not mutually prime.

    If only they were in resonance.

  5. userbinator

    I can't readily think of an application in which this is a performance-critical computation, so I'd be tempted to optimise for size, but in the more general sense of "how to quickly obtain the modulus of dividing by 7", this becomes a much more practical use.

More from this day

2026-08-20