mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 17:06:04 +00:00
The generic `rotr_wk(dst, n)` does its byte/bit decomposition with two runtime `while` loops — necessary in the abstract, but wasteful in SHA-256 where every rotation amount is one of ten fixed compile-time constants (2, 6, 7, 11, 13, 17, 18, 19, 22, 25). The loop overhead alone is ~80 cycles of bookkeeping per call, on top of the actual rotation work. Replace each `rotr_wk(SIG, 6)` call inside the sigma helpers with a dedicated `rotr_wk_6(SIG)` (and similarly for the other amounts and for `shr_wk_3` / `shr_wk_10`). Each new helper just chains the right number of `byte_rotr_wk` and `rotr1_wk` calls inline — no `rem` variable, no `>= 8` / `> 0` checks. The original `rotr_wk` / `shr_wk` wrappers stay defined for runtime-amount callers; nothing else in the example uses them today, but keeping them documents the general-purpose form. Per SHA-256 block: ~45K cycles saved across 384 sigma rotations. Combined with commit 0b5470b's leaf-function fix, the per-block compression cost drops by ~3.5 frames at NTSC. The hash output is unchanged (still AE9145DB5CABC41FE34B54E34AF8881F462362EA20FD8F861B26532FFBB84E0D for "NES"), no other examples are affected, and the emulator harness stays at 34/34. https://claude.ai/code/session_01FRmSBruVWCufm3LsUVMs8v
24 KiB
24 KiB