1: Sample code that reproduces the problem.
int wide(void) { return 4294967296ULL ? 1 : 0; }
int fractional(void) { return 0.5f ? 1 : 0; }
2: Exact command used to run SDCC on this sample code
./bin/sdcc -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 generates code that returns zero from both functions. Both conditions are nonzero, so both functions should return one.
The constant-folding code converts the condition through ulFromVal and then to int before testing it. This discards the significant high bits of the integer condition and the fractional part of the floating condition. These examples have the same root cause.
The candidate fix tests the scalar value directly. Its regression test covers both failing cases and a zero condition. The file support/regression/tests/conditional-truth.c can be renamed after the bug if desired.
GCC 16.1.0, Clang 23.1.0 and TCC 0.9.27 correctly fold both functions to return one. SDCC 4.6.0 does not.
https://godbolt.org/z/oef8TTEo6
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.
Thanks. Fixed in [r16966] via your patch.
Related
Commit: [r16966]