Skip to content

[AUTOMATED] Print a signed power-of-two division corrected by a conditional move as the division - #1973

Merged
mahaloz merged 4 commits into
mainfrom
fix/sdivpow2
Nov 25, 2026
Merged

mahaloz merged 4 commits into
mainfrom
fix/sdivpow2

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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; addlt or out-of-line add) prints as a branch and a shift instead of the division:

$ printf 'int div8(int x) { return x / 8; }\nint scaled(int bytes) { return (bytes + 512) / 1024; }\n' > d.c
$ gcc -O2 -c d.c -o d.o && kuna decompile-all d.o
int div8(int a0)
{
  int v1; // eax | edi

  v1 = a0 + 7;
  if (0 <= a0)
    v1 = a0;
  return v1 >> 3;
}

int scaled(int a0)
{
  int v1; // eax | edi

  v1 = a0 + 0x5ff;
  if (0 <= a0 + 0x200)
    v1 = a0 + 0x200;
  return v1 >> 10;
}

With this change:

int div8(int a0)
{
  return a0 / 8;
}

int scaled(int a0)
{
  return (a0 + 0x200) / 0x400;
}
  • RuleDivOpt recognizes the join of x and x + (2^n - 1) when one branch on the sign of x decides it and the add is taken exactly when x < 0. The two values may be offsets of one root, as in scaled.
  • Each read of the join that is a function of the quotient is rewritten: >> n (arithmetic, or logical when only low bits are read), the quotient's byte, and & -2^n as x / 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. n stops at 8 * size - 2, where the divisor is still positive.
  • Reads of other kinds (an unsigned compare of the join) keep the branch.

Tests: stage tests kuna-sdivpow2.xml (gcc x86-64: plain, offset, rounded and byte forms, plus an add of 7 before >> 2 that keeps its if) and kuna-sdivpow2-thumb.xml (both Thumb forms) fail before the change. kuna-cli/tests/sdivpow2_round_trip.rs compiles the printed functions of gcc -O0/-O2 and clang -O2 objects next to their source and compares values from LONG_MIN up. Unit tests cover the edge directions, the boolean flip and the rejected shapes. Four storealias/globalvalue assertions that pinned the old spelling of the fixtures' gi / 16 now pin return 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

mahaloz and others added 4 commits November 25, 2026 19:46
…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
@mahaloz
mahaloz merged commit 4f84320 into main Nov 25, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/sdivpow2 branch November 25, 2026 20:34
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