Repository navigation
[AUTOMATED] Print a signed power-of-two division corrected by a conditional move as the division - #1973
Merged
Conversation
…nal add WIP: RuleSignDiv2 now also rewrites MULTIEQUAL(x, x + 2^n-1) s>> n to x s/ 2^n when the join's two edges are decided by a branch on the sign of x. [AUTOMATED] Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
WIP: the hook moves to RuleDivOpt, whose op list (SUBPIECE, INT_RIGHT, INT_SRIGHT, INT_AND) covers each read of the corrected join: the arithmetic shift, a logical shift whose reads keep only agreeing bits, the quotient's byte, and the join rounded down (x / 2^n * 2^n). The dividend may be an offset of the add's root ((x + 512) / 1024). Unit tests added. [AUTOMATED] Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
Stage tests kuna-sdivpow2 (gcc x86-64 -O0/-O2: plain, offset, rounded, byte-read forms and a non-division control) and kuna-sdivpow2-thumb (gcc's it lt/addlt and out-of-line blt forms); the stages baseline is re-recorded with their ten assertions. The storealias and globalvalue assertions that pinned the old `v1 = gi + 0xf; if ...` spelling of `gi / 16` now pin `return gi / 0x10;`. A kuna-cli round trip compiles the printed functions of gcc -O0/-O2 and clang -O2 objects beside their source and checks every value from the most negative up. Spec chapter 03 describes the recognition. [AUTOMATED] Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
With the dividend matched as an offset of the add's root, the branch may test an equal value computed before it while the join's own input is computed in the dividend's arm; that input does not reach the join's reads, so the recognition is declined there. [AUTOMATED] Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A signed division by a power of two that the compiler rounds with a conditional move (gcc at -O0 and -O2, clang at -O2, ARM gcc's
it lt; addltor out-of-lineadd) prints as a branch and a shift instead of the division:With this change:
RuleDivOptrecognizes the join ofxandx + (2^n - 1)when one branch on the sign ofxdecides it and the add is taken exactly whenx < 0. The two values may be offsets of one root, as inscaled.>> n(arithmetic, or logical when only low bits are read), the quotient's byte, and& -2^nasx / 2^n * 2^n. The join, the add and the branch then fall away. The result equals the binary's for every input, so there is no option.nstops at8 * size - 2, where the divisor is still positive.Tests: stage tests
kuna-sdivpow2.xml(gcc x86-64: plain, offset, rounded and byte forms, plus an add of 7 before>> 2that keeps itsif) andkuna-sdivpow2-thumb.xml(both Thumb forms) fail before the change.kuna-cli/tests/sdivpow2_round_trip.rscompiles the printed functions of gcc -O0/-O2 and clang -O2 objects next to their source and compares values fromLONG_MINup. Unit tests cover the edge directions, the boolean flip and the rejected shapes. Fourstorealias/globalvalueassertions that pinned the old spelling of the fixtures'gi / 16now pinreturn gi / 0x10;.On the decbench O2 corpus 13 more functions match the source's control flow exactly (gained 14, lost 1), and 33 more at O0 (gained 33, lost 0).
Model:
anthropic/claude-opus-5-5🤖 Generated with Claude Code
https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU