Repository navigation
8-bit stretches across blocks, and the high byte of A kept at zero - #90
Conversation
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>
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>
|
🤖 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.
|
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>
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 #$00FFbeforeTAX, 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, afterByteLoops). Until now every block boundary ran with 16 bits, except the loops ofByteLoops. The pass takes out everyREP #$20/SEP #$20of a function and places them again by a minimum s-t cut (Dinic):AND/ORA/EOR,ADC/SBC,TXA/TYA,ASL A,INC A,DEC Awhose low byte is the same in 8 bits and whose flags nothing reads;Weights.estimated: a branch's ways share the runs; with the loop depth alone Catch Me got 0.5 % slower);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.generatenow finishes the functions callees first: each function thatWidthschanged 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.LParsernow 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.LirTestbuilds its "written for the wrong width" case from lines now, since parsing would correct it.HighByteZero(new LIR pass, beforeKnownCarry). It follows which high bytes of A, X and Y are known to be 0 over the blocks: after a 16-bitANDwith a byte constant, a load of a byte constant or of a byte's place (every store keeps ani8/i1zero-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,CMPwith a byte, andTAX/TAYare allowed;AND #$00FFis dropped. It never adds a cycle on any path.Verified
./gradlew buildgreen, withBackendOutputTest,StyleGuideTestandLibCostTestrecorded 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.mesenlinked in the worktree, in Mesen.BackendOutputTest's output, MIR and layout builds), againstdevelopat 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 againstdevelopat cd726c6; the last two columns are the merged code against fa128be:developcd726c6WidthsHighByteZerodevelopfa128be$7E:2000was 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
sdevkit_reg_write_twice) takes 102 master cycles after the wait instead of 138, since both bytes are stored in 8 bits now.IrqWindowTesthas the new number, and its HDMA case declares two channels so that it still fails.lib/C_STYLE.mdchanged (fast-const-table,same-enum-object,same-list-of-offsets,slow-word-offsets); every rule holds as written. The costs table inlib/README.mdis smaller throughout (sdevkit_shadow_commit's best case grew from 870 to 1,006 while its worst case shrank).docs/game-findings.md.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).Open
oam_addkeeps most of its masks for this reason. About 4.1 % of the game's CPU cycles are stillAND #$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.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 keepsBranchRelaxfrom making that block a single branch; compile time grows a little, since a function thatWidthschanges is finished twice.Widths.highByteReaders/scanandHighByteZero.narrows/after. These are the places where a mistake would give wrong code, not only slow code.🤖 Generated with Claude Code