Skip to content

fix: keep cast that changes operand arithmetic type in arith instructions#2906

Open
obus-globus wants to merge 1 commit into
skylot:masterfrom
obus-globus:fix/keep-arith-type-change-cast
Open

fix: keep cast that changes operand arithmetic type in arith instructions#2906
obus-globus wants to merge 1 commit into
skylot:masterfrom
obus-globus:fix/keep-arith-type-change-cast

Conversation

@obus-globus

Copy link
Copy Markdown
Contributor

NOTE: This is what one may call "AI slop". Feel free to close this PR if it's not up to your quality standards or even if you just aren't interested in reviewing it. I use these changes downstream on an APK and thought I might as well make a PR offering to upstream them, as your CONTRIBUTING.md doesn't say anything discouraging this.

Description

(float) a / (float) b with int operands decompiled to a / b, an integer division, so the result was truncated: floatDiv(1, 2) returned 0.0f instead of 0.5f. It reproduces on both the dex and java/class paths, since the cast is dropped in SimplifyVisitor, which is shared by all inputs.

SimplifyVisitor.processCast drops a primitive cast feeding an ARITH insn when isArithWideUpCast reports it as unnecessary. That helper only kept casts that grow the operand register count (castToType.getRegCount() > argType.getRegCount()). int and float share a register count, so the int->float cast was dropped and jadx emitted an integer division, then relied on a widening at the result that does not happen (the division is already done in int).

Keep any cast that changes the operand arithmetic type (int/long/float/double), because operand types select the arith instruction in the result bytecode. Types narrower than int are treated as int. The (long) i << 32 case the old check handled stays covered, since it is also a type change.

Includes an integration test.

@obus-globus obus-globus force-pushed the fix/keep-arith-type-change-cast branch from 143e072 to 19f18c7 Compare July 4, 2026 02:59
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