Menu ▾ ▴

#4100 Unsigned _BitInt negation is mishandled in a constant expression

closed-fixed
None
Front-end
5
2026-10-05
2026-10-01
No

1: Sample code that reproduces the problem.

_Static_assert((unsigned long long)(-(unsigned _BitInt(8))1) == 255ULL, "negation");

2: Exact command used to run SDCC on this sample code

./bin/sdcc --std-c2y -S bug.c

3: SDCC version tested

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/huc6280/f8/f8l TD- 4.6.2 #0 (Linux)

Built from upstream revision 16945 (Git commit 5c14f028791cc1756418966e62cfd0a53bafdb33).

4: Copy of the error message or incorrect output, or a clear description of the observed versus expected behavior.

SDCC reports:

bug.c:1: warning 215: static assertion failed: "negation"

The negation of an unsigned 8-bit value of 1 should produce 255. Converting that result to unsigned long long should preserve the value. SDCC instead retains the high bits from the 64-bit subtraction used to evaluate the negation.

The candidate fix masks the result to the _BitInt width. It adds a test for the failing 8-bit case, zero, and the 64-bit boundary. (The file support/valdiag/tests/bitint-negation.c can be renamed after this bug if desired.)

The static assertion passes with GCC 16.2 and Clang 23.1.0
https://godbolt.org/z/8a9d6hEfG

The problem was reproduced with mcs51 (default, large and stack-auto), ds390, z80, z180, r2k, r4k, sm83, tlcs90, hc08, s08, mos6502, huc6280, stm8, f8, pdk13, pdk14 and pdk15.

1 Attachments

Related

Bugs: #4093

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    Thanks. Fixed via your patch in [r16964]. That fixes the issue for unsigned _BitInt. signed _BitInt overflow is UB, so I guess we can keep ignoring that case (but we might as well be nice and "fix" (i.e. give it two's complement semantics) that one too sometime for this one compile-time case.

     

    Related

    Commit: [r16964]


Log in to post a comment.