From 5ec86dc887d46c326fc55d41dbf460952f3c2c19 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 16 Oct 2026 09:59:43 -0500 Subject: [PATCH] Measure the weekday, and record why Hinnant's stays Ben Joffe's article on weekdays offers a 64-bit multiply-add-shift: the low 64 bits of days * ceil(2^64 / 7), turned round to Thursday, top three bits. It agreed with DayOfWeek.fromDaysSinceStartOfEra on 200 million day numbers and is exact far past the range of DaysType. It ran at 3.3 ns against 4.1 with chained calls and 0.68 against 0.77 with independent ones. Instant.asDateTime and the TimeZone lookups that call it showed no difference, so the doc comment records the comparison and the code is unchanged. zig build bench gains DayOfWeek.fromDays, measured with independent and with chained calls, and its name column widens to fit. The README cites the article. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ub7C4SHwzbWTUR2TA4eGgR --- README.md | 4 ++++ src/bench.zig | 22 ++++++++++++++++++++-- src/dayofweek.zig | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01f59f4..d822c63 100644 --- a/README.md +++ b/README.md @@ -1621,6 +1621,10 @@ The documents this library is written against. 11 December 2025, updated 18 December 2025, . The survey and benchmarks of leap-year tests that `leap.is` was chosen from. +- **[JOFFE-WEEKDAY]** Joffe, B., *A faster way to calculate the day of the + week*, 17 August 2026, . The + 64-bit multiply-add-shift weekday that was measured against + `DayOfWeek.fromDaysSinceStartOfEra` and not adopted. - **[NERI-LEAP]** Neri, C., *libstdc++: Simplify year::is_leap()*, GCC commit 86a0df1a6c7f, 11 November 2023, . diff --git a/src/bench.zig b/src/bench.zig index e811086..62003c1 100644 --- a/src/bench.zig +++ b/src/bench.zig @@ -31,13 +31,13 @@ const table_len = 1024; fn report(name: []const u8, ns: u64) void { const per = @as(f64, @floatFromInt(ns)) / @as(f64, @floatFromInt(N)); const rate = 1e9 / per / 1e6; - std.debug.print("{s:<26} {d:>8.2} ns/op {d:>8.2} Mop/s\n", .{ name, per, rate }); + std.debug.print("{s:<28} {d:>8.2} ns/op {d:>8.2} Mop/s\n", .{ name, per, rate }); } /// Prints a line for a benchmark that could not be run, in place of its /// numbers, so that its absence is visible rather than silent. fn skip(name: []const u8, why: []const u8) void { - std.debug.print("{s:<26} {s}\n", .{ name, why }); + std.debug.print("{s:<28} {s}\n", .{ name, why }); } /// Runs every benchmark in turn and prints the results. @@ -114,6 +114,24 @@ pub fn main(init: std.process.Init) !void { report("leap.is (chained)", ns); } + { + // The same two ways as `leap.is`, over the day numbers of the + // dates above. + var t0: std.Io.Timestamp = .now(io, .awake); + var acc: usize = 0; + for (0..N) |i| acc +%= datetime.DayOfWeek.fromDaysSinceStartOfEra(days[i & (table_len - 1)]).weekdayNumber(); + var ns: u64 = @intCast(t0.durationTo(.now(io, .awake)).nanoseconds); + std.mem.doNotOptimizeAway(acc); + report("DayOfWeek.fromDays", ns); + + t0 = .now(io, .awake); + acc = 0; + for (0..N) |i| acc +%= datetime.DayOfWeek.fromDaysSinceStartOfEra(days[(i +% acc) & (table_len - 1)]).weekdayNumber(); + ns = @intCast(t0.durationTo(.now(io, .awake)).nanoseconds); + std.mem.doNotOptimizeAway(acc); + report("DayOfWeek.fromDays (chained)", ns); + } + { const t0: std.Io.Timestamp = .now(io, .awake); var acc: i64 = 0; diff --git a/src/dayofweek.zig b/src/dayofweek.zig index 88b058a..5c88253 100644 --- a/src/dayofweek.zig +++ b/src/dayofweek.zig @@ -242,6 +242,22 @@ pub const DayOfWeek = enum(u3) { /// division that floors. No table and no lookup: the week has run /// unbroken through the whole proleptic Gregorian calendar, so a /// weekday is nothing but the day count modulo seven. + /// + /// Ben Joffe's article on weekdays + /// () gathers faster ways to + /// take that remainder. Most are for 32-bit day counts, which a + /// `DaysType` is not. The one that fits takes the low 64 bits of + /// `days * ceil(2^64 / 7)`, adds a constant that turns the circle round + /// to Thursday, and keeps the top three bits: since 7 is one less than + /// 8, multiplying by about 8/7 lands each day in its own eighth of the + /// circle, one eighth going unused each week. It was tried here on + /// 2026-10-16. It agreed with this on 200 million day numbers, and is + /// exact for any count within about 2.3 * 10^17 of the epoch. It + /// took 3.3 ns a call to this one's 4.1 when each call waited on the one + /// before, and 0.68 to 0.77 when they did not, but `Instant.asDateTime` + /// and the `TimeZone` lookups, which call this, came out the same + /// within the noise of `zig build bench`. So this stays, and the + /// two magic constants do not come in. pub fn fromDaysSinceStartOfEra(days: Date.DaysType) DayOfWeek { const result = if (days >= -4) @rem(days + 4, 7) -- knotmirror 0.0.1