Skip to content

Fix UInt8/UInt16 comparison after wrapping arithmetic (#694)#713

Closed
elefthei wants to merge 1 commit intomasterfrom
lef/fix-694-eurydice
Closed

Fix UInt8/UInt16 comparison after wrapping arithmetic (#694)#713
elefthei wants to merge 1 commit intomasterfrom
lef/fix-694-eurydice

Conversation

@elefthei
Copy link
Copy Markdown

Comparisons (Eq, Neq, Lt, Lte, Gt, Gte) on UInt8/UInt16 after wrapping arithmetic (add_mod, sub_mod, mul_mod, shift_left, xor, or, etc.) produced wrong C code. The uint32 intermediate from mk_arith was compared directly without masking back to the original width.

For example, (255uy +%^ 1uy) = 0uy generated:
(uint32_t)255 + (uint32_t)1 == 0 => 256 == 0 => false (WRONG)

Fix: handle comparisons in mk_expr (not mk_arith) with a dedicated pattern that only widens+masks operands containing arithmetic subexpressions. Atomic operands (variables, field accesses, etc.) are compared at their native width without unnecessary casts.

Now generates:
simple: a == b (no casts)
arith: ((uint32_t)a + (uint32_t)b & 0xFFU) == c (only arith side)

Fixes #694. Also fixes #689, #691, #692, #693 (same root cause).

@elefthei elefthei force-pushed the lef/fix-694-eurydice branch from 3caacde to 22f64f2 Compare March 31, 2026 06:45
Comparisons (Eq, Neq, Lt, Lte, Gt, Gte) on UInt8/UInt16 after wrapping
arithmetic (add_mod, sub_mod, mul_mod, shift_left, xor, or, etc.) produced
wrong C code. The uint32 intermediate from mk_arith was compared directly
without masking back to the original width.

For example, (255uy +%^ 1uy) = 0uy generated:
  (uint32_t)255 + (uint32_t)1 == 0  =>  256 == 0  =>  false (WRONG)

Fix: handle comparisons in mk_expr (not mk_arith) with a dedicated pattern
that only widens+masks operands containing arithmetic subexpressions.
Atomic operands (variables, field accesses, etc.) are compared at their
native width without unnecessary casts.

Now generates:
  simple:  a == b                              (no casts)
  arith:   ((uint32_t)a + (uint32_t)b & 0xFFU) == c   (only arith side)

Fixes #694. Also fixes #689, #691, #692, #693 (same root cause).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@elefthei elefthei force-pushed the lef/fix-694-eurydice branch from 22f64f2 to a2e354b Compare March 31, 2026 06:49
@elefthei elefthei closed this Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant