Skip to content

8-bit stretches across blocks, and the high byte of A kept at zero - #90

Merged
skerkewitz-agent[bot] merged 3 commits into
developfrom
eight-bit-stretches
Nov 28, 2026
Merged

skerkewitz-agent[bot] merged 3 commits into
developfrom
eight-bit-stretches

Conversation

@skerkewitz-agent

@skerkewitz-agent skerkewitz-agent Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What

M12 row 3, 8-bit stretches, in two steps. Byte code now stays in 8 bits across branches and loops, and where the high byte of A is known to be 0, a byte loses its AND #$00FF before TAX, a compare or a 16-bit store. The examples do 1.0 % less work, Blubber Blasen 1.0 % less with the CPU partner and 1.1 % solo, and no function's worst case grows.

The row is not done: the rest of the game's masks need either the switches placed where B is known to be 0, or 8-bit index registers (row 5). The roadmap row says what is left and its estimate goes down from 3-5 % to about 0.5 % more.

How

Widths (new LIR pass, after ByteLoops). Until now every block boundary ran with 16 bits, except the loops of ByteLoops. The pass takes out every REP #$20/SEP #$20 of a function and places them again by a minimum s-t cut (Dinic):

  • each instruction that depends on the width keeps its own, except a load, AND/ORA/EOR, ADC/SBC, TXA/TYA, ASL A, INC A, DEC A whose low byte is the same in 8 bits and whose flags nothing reads;
  • a switch between two neighbours of different widths costs its master cycles times the block's weight (Weights.estimated: a branch's ways share the runs; with the loop depth alone Catch Me got 0.5 % slower);
  • a block's end has the width of the starts of the blocks it goes to, a function starts with 16 bits, calls and returns run with 16;
  • an instruction that reads the high byte in 16 bits (or TAX/TAY/XBA, a call, a return of A) keeps every instruction before it, back to the last full load of A, in 16 bits.

All these costs are paid only where two choices differ, so the cut is exact. The code is rewritten only where the cut is cheaper.

The worst-case rule, that a speed rule of dampf never makes a function's worst case larger (ADR-6FXZQXQB), needs the worst case with the calls, as the timing report has it. So MirCodeGen.generate now finishes the functions callees first: each function that Widths changed is finished both ways, and keeps its old switches where the new ones would make its worst case larger. finishedWorst (the comparisons of the register parts and of the biased compares) takes the smaller of the two as well.

LParser now follows the width along the edges instead of the text order. A block that a branch enters with another width than the text above it ends with then reads back as it runs. ca65 is not affected: every 8-bit immediate has its own .a8/.a16. LirTest builds its "written for the wrong width" case from lines now, since parsing would correct it.

HighByteZero (new LIR pass, before KnownCarry). It follows which high bytes of A, X and Y are known to be 0 over the blocks: after a 16-bit AND with a byte constant, a load of a byte constant or of a byte's place (every store keeps an i8/i1 zero-extended in its two bytes, MirCodeGen.zeroExtended), LSR A, a transfer of an index register that holds a byte. 8-bit code keeps B; XBA, a call or a block move does not. A 16-bit stretch between 8-bit code then goes to 8 bits whole, from its start or at its end, where B is 0 at the start of the part and the 16-bit code leaves 0 at its end. Inside the part, loads and logic keep the same low byte and the flags that are read (Z only where the 16-bit result's high byte is 0); LSR A, CMP with a byte, and TAX/TAY are allowed; AND #$00FF is dropped. It never adds a cycle on any path.

Verified

  • ./gradlew build green, with BackendOutputTest, StyleGuideTest and LibCostTest recorded again. New: exec/eight_bit_stretches.c (byte stores on both ways of branches, loops entered from blocks that branch elsewhere, a byte read in 16 bits after the join, runtime multiplications between byte code, signed bytes, a switch on a byte, 16-bit sums beside byte stores), WidthsTest, HighByteZeroTest. The execution tests ran in dampf's emulator and, with .mesen linked in the worktree, in Mesen.
  • Worst cases (ADR-6FXZQXQB), every function in the timing reports of the execution tests, random programs and examples (BackendOutputTest's output, MIR and layout builds), against develop at fa128be (with the peepholes of Three peepholes: no CMP #0 where the flags are set, x * 256 as XBA, constant array elements folded #91): 674 smaller, 2,086 the same, 0 larger. In Blubber Blasen's own timing report: 142 smaller, 186 the same, 0 larger.
  • dev/bench_examples.sh, work per tick. The middle columns were measured before the merge against develop at cd726c6; the last two columns are the merged code against fa128be:
Example develop cd726c6 Widths and HighByteZero develop fa128be merged Ratio
Catch Me 45,831 45,626 45,551 45,794 45,526 0.994
Er-Tüp 129,061 128,126 127,511 128,839 127,288 0.988
Gruft 130,683 127,574 127,504 130,306 127,110 0.975
Hello 80,270 79,999 79,964 80,161 79,854 0.996
Mandelbrot 74,622 74,444 74,389 74,622 74,389 0.997
Mode 7 10,604 10,568 10,429 10,581 10,401 0.983
Modes 53,386 53,004 52,996 53,367 52,977 0.993
Schach 364,621 362,876 361,678 362,882 359,938 0.992
Steinschlag 71,544 71,338 71,052 71,509 71,017 0.993
geometric mean 0.993 0.990 0.990
  • Code over the nine examples, merged against fa128be: 157,864 → 153,609 bytes (−2.7 %).
  • Blubber Blasen (a copy built with each kit, both scripted runs played in dampf's emulator), merged against fa128be: work per tick with the CPU partner 292,600 → 289,705 (−1.0 %), solo 204,454 → 202,201 (−1.1 %). WRAM from $7E:2000 was identical to develop's build at the end of every one of the 6,000 ticks of both runs. (Before the merge, against cd726c6: 292,950 → 290,054 and 204,779 → 202,522.)

What follows

  • Generated code: smaller and faster as above. The kit's IRQ write of a scroll register (sdevkit_reg_write_twice) takes 102 master cycles after the wait instead of 138, since both bytes are stored in 8 bits now. IrqWindowTest has the new number, and its HDMA case declares two channels so that it still fails.
  • C guide: the measurements of four rules in lib/C_STYLE.md changed (fast-const-table, same-enum-object, same-list-of-offsets, slow-word-offsets); every rule holds as written. The costs table in lib/README.md is smaller throughout (sdevkit_shadow_commit's best case grew from 870 to 1,006 while its worst case shrank).
  • The game: nothing to change, no entry in docs/game-findings.md.
  • Documents: docs/optimizations-log.md (two entries), docs/optimizations.md (catalogue B, the examples table), docs/architecture.md, docs/history.md (built after 0.10.1), docs/roadmap.md (row 3 with what is left and a lower estimate, a note in row 5, the register-write item).
  • Book pages that will be stale at the release from this branch: inside/optimizations, inside/how-a-compile-works, inside/roadmap, guide/writing-fast-c, guide/snes-hardware, reference/console-api, guide/introduction, guide/debugging, reference/output-files, reference/limits (several were stale already).

Open

  • Left in row 3: placing the switches where B is known to be 0. The cut does not know B, and counting on it makes the problem non-submodular, because a 16-bit instruction can make B unknown where an 8-bit one would keep it. oam_add keeps most of its masks for this reason. About 4.1 % of the game's CPU cycles are still AND #$00FF, most of them byte tests in loops over arrays of structs that step a 16-bit index, where a pair of switches costs more than the mask. That part belongs to row 5.
  • Decided without asking: the weights are Weights.estimated, not the loop depth; a switch in a block that holds only a jump (the long form of a branch) is charged the jump's cycles as well, because it keeps BranchRelax from making that block a single branch; compile time grows a little, since a function that Widths changes is finished twice.
  • To look at closely: the high-byte rules in Widths.highByteReaders/scan and HighByteZero.narrows/after. These are the places where a mistake would give wrong code, not only slow code.

🤖 Generated with Claude Code

skerkewitz-agent Bot and others added 2 commits November 28, 2026 17:51
M12 row 3, first step. Every block boundary of the code from MIR ran with
16 bits, except the loops of ByteLoops. The new LIR pass Widths takes out
every REP #$20 and SEP #$20 of a function and places them again by a
minimum cut: each instruction that depends on the width keeps its own,
except a load or logic whose low byte is the same in 8 bits and whose
flags nothing reads, which may go to 8 bits where nothing reads the high
byte of A before A is loaded again in 16 bits. A switch costs its cycles
times the block's weight (Weights.estimated), a block's end has the width
of the starts of the blocks it goes to, calls and returns run with 16
bits. So byte stores on both ways of an if share one switch, and the SEP
of a byte loop goes before its entry even where the entry also branches
elsewhere.

Each function is finished both ways, callees first, and keeps its old
switches where its worst case with its calls would grow (ADR-6FXZQXQB).
LParser follows the width along the edges now (the immediates are sized
by .a8/.a16 around each), so a block that a branch enters with another
width than the text above it reads back as it runs.

Against develop: 656 worst cases smaller, 2,028 the same, none larger;
the examples 0.993 (geometric mean, Gruft 0.976), code 1.7 % smaller;
Blubber Blasen's work per tick 0.8 % less with the CPU partner, 0.9 %
solo, WRAM identical in every tick. The kit's IRQ scroll register write
takes 102 master cycles instead of 138 (IrqWindowTest).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
M12 row 3, second step. An instruction in 8 bits leaves B, the high byte
of A, as it is; where B is 0 and the 16-bit code would leave a high byte
of 0 too, A holds the same value in both widths and a byte needs no mask:
LDA b / AND #$00FF / TAX is LDA b / TAX in 8 bits.

The LIR pass HighByteZero follows which high bytes of A, X and Y are
known to be 0 (a 16-bit AND with a byte constant, a load of a byte's
place or of a byte constant, a transfer of an index register that holds
a byte; 8-bit code keeps B). A 16-bit stretch between 8-bit code then goes
to 8 bits whole, from its start or at its end, where B is 0 where the part
starts and the 16-bit code leaves 0 where it ends: loads and logic with
the same low byte and the flags that are read, LSR A, CMP with a byte,
TAX/TAY, and AND #$00FF dropped. No path gets a cycle more.

Against develop, both steps: 670 worst cases smaller, none larger; the
examples 0.990 (geometric mean); Blubber Blasen 1.0 % less work per tick
with the CPU partner, 1.1 % solo, WRAM identical in every tick. Most of
the game's remaining masks are byte tests in loops that step a 16-bit
index, which the roadmap now puts with row 5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
skerkewitz
skerkewitz previously approved these changes Nov 28, 2026
Conflicts in the docs: both sides' entries kept in history.md and
optimizations-log.md, the peepholes first; the examples' table in
optimizations.md measured again on the merged code; the register-write
item of the roadmap without the CMP #0 part, which #91 built.
backend-output.txt, cost-baseline.txt, C_STYLE.md and lib/README.md
recorded again by their tests.

Against develop at fa128be: 674 worst cases smaller, 2,086 the same,
none larger; the examples 0.990; Blubber Blasen 1.0 % less work per tick
with the CPU partner, 1.1 % solo, WRAM identical in every tick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@skerkewitz-agent

Copy link
Copy Markdown
Contributor Author

🤖 Merged develop (the peepholes of #91)

Problem: #91 was merged into develop as fa128be, and this branch conflicted in backend-output.txt, cost-baseline.txt, docs/history.md, docs/optimizations-log.md, docs/optimizations.md and docs/roadmap.md.
Fix: merged origin/develop. In the docs both sides' entries are kept in history.md and optimizations-log.md, with the peepholes first. The examples' table in optimizations.md was measured again on the merged code, and the roadmap's register-write item drops the CMP #0 part, which #91 built. The generated files (backend-output.txt, cost-baseline.txt, the measurements in lib/C_STYLE.md, the costs in lib/README.md) were recorded again by their tests instead of merged by hand. The log entry of the second step got the numbers against the new develop; the description's numbers are updated too.
Commits: d65d297 Merge develop (the peepholes, #91) into eight-bit-stretches
Verified: ./gradlew build green. Measured against develop at fa128be:

  • worst cases: 674 smaller, 2,086 the same, 0 larger;
  • the examples' work 0.990 (geometric mean, Gruft 0.975, Mode 7 0.983, Er-Tüp 0.988);
  • code 157,864 → 153,609 bytes (−2.7 %);
  • Blubber Blasen's work per tick: with the CPU partner 292,600 → 289,705 (−1.0 %), solo 204,454 → 202,201 (−1.1 %); WRAM identical in every tick of both runs; in its timing report 142 worst cases smaller, none larger.

@skerkewitz-agent
skerkewitz-agent Bot merged commit 42c7de8 into develop Nov 28, 2026
1 check passed
@skerkewitz-agent
skerkewitz-agent Bot deleted the eight-bit-stretches branch November 28, 2026 20:01
skerkewitz-agent Bot added a commit that referenced this pull request Nov 30, 2026
HighByteZero (#90) took every byte's place as zero-extended to 16 bits. The
selection masks a byte only where a reader needs it clean
(Selection.maskResult): one that only byte-wise code reads keeps whatever
high byte its code left, such as a 16-bit shift's truncation ($FFFF for
a >> 15 of a negative a). A 16-bit EOR with such a place was then believed
to leave B at 0, and a byte counter's increment after it went to 8 bits and
was stored with that high byte.

The predicate is now per function: truth values, byte parameters, and the
bytes in Selection.clean (masked, or clean by themselves where a reader
needs it), kept per function since the LIR passes run after the selection
of all of them. Found by the random loop programs, seeds 20787012 and
20787028; exec/regress_byte_high_byte.c is the program cut down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant