Repository navigation
The value of a signed comparison with a constant reads C again, not N - #94
Conversation
Since the signed comparisons by subtraction (#89), orderedFlags decides a comparison of an i16 value with a constant by N after CMP where the value ranges rule out an overflow. The value form in BranchSelection.cmp left comparisons with a constant to the biased form, but called orderedFlags and read C after it: (a - b) <= 232 of two bytes became the unsigned a - b >= 233, and in exec/enums.c, dir - 1 < 0 was always 1. The value form now calls the biased form (carryFlags) directly; branches keep N. Found by the random loop programs (seed 20786007), and exec/regress_signed_compare_value.c covers the four predicates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
🤖 Tests for the gap that let the bug through Problem: the existing tests should have caught this. |
|
🤖 The test commit came after the merge Problem: 48169e3, announced in the comment above, reached the branch after this pull request had been merged, so it is not in |
What
Fixes a miscompile that today's random loop program (seed 20786007) found on
develop. The truth value of a signed 16-bit comparison against a constant, stored as a value, could come out wrong whenever the value ranges rule out an overflow:x < 0andx >= 0gave 1 for every 16-bitx(a comparison with 0 never overflows);((uint16_t)a - (uint8_t)b) <= 232of two bytes was computed as the unsigneda - b >= 233, so every positive difference gave 0.Comparisons used as branches (
if, loop conditions) were right. It came in with #89 (signed comparisons by one subtraction).exec/enums.chad the bug and passed by luck: theredir - 1 < 0always gave 1, and the test makesdir - 1negative.The tests for the gap that let it through (
exec/signed_compare.cwith comparisons against constants as values,exec/enums.cwith both outcomes) were pushed after this pull request was merged, and are in #96.How
Since #89,
BranchSelection.orderedFlagsdecides a signed i16 comparison by N afterCMPwhen the value ranges rule out an overflow, and that includes comparisons against a constant. The value form incmpmeant to leave comparisons with a constant to the biased form, but it went throughorderedFlagsand then read C. Now it calls the biased form, split out ascarryFlags, directly. Branches still useorderedFlagsand N, which was right all along.The pass bisect (
dev/bisect_passes.sh) namedNarrow, because that pass gives the difference the value range that enables the subtraction form. The optimized MIR was right; only the code selected from it was wrong.Verified
./gradlew build: 1,272 tests, 0 failed, 13 skipped. That includes the random programs with today's seed, so 20786007 is among them, andExamplesTestfor every example.exec/regress_signed_compare_value.c(<=,<,>=,>of a byte difference against constants) fails without the fix and passes with it. Thex < 0andx >= 0cases were found with the tests of Comparisons with constants as values in signed_compare.c, both outcomes in enums.c #96.RandomProgramTestwith-PrandomSeed=20786007 -Prandom=1passes.What follows
backend-output.txtonlyexec/enums.cchanged (+30 master cycles on the line that was wrong) and the new test was added. Every example's output is byte-identical, so the benchmark can't move and I didn't rerun it.cost-baseline.txtis unchanged.docs/game-findings.md. A comparison of this form stored as a value (x < 0is the likely one) would have given a wrong value since Signed comparisons by one subtraction, and the absolute difference as one instruction #89. Rebuilding with the fixed dampf is enough, and checking that is the game project's job.StyleGuideTest, documents, book: unchanged.Open
developinto Sound effects take the top voices, 8 - n to 7 #93.x < 0) could be cheaper:ASLputs the sign straight into C. Not part of this fix.🤖 Generated with Claude Code