Skip to content

[AUTOMATED] Preserve intermediate rounding in integer-to-float conversions - #1071

Merged
mahaloz merged 6 commits into
Noelo-Lab:mainfrom
thotypous:fix/integer-float-rounding
Oct 14, 2026
Merged

mahaloz merged 6 commits into
Noelo-Lab:mainfrom
thotypous:fix/integer-float-rounding

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

A signed integer converted to float and then widened to double loses its first rounding. For value = 16777217, these SPARC instructions return 16777216.0, but the printed C returns 16777217.0.

p=$(mktemp)
printf '%s' 03000080c500400085a0188281a0192281c3e00801000000 | xxd -r -p > "$p"
kuna decompile "$p" 0x10000 --raw-image --base 0x10000 \
  --target sparc:BE:32:default --mode reliable \
  --assert 'prototype 0x10000 double sub_10000(void)' \
  --assert 'data 0x20000 int value'
rm "$p"

Current output:

return (float8)value;

It should retain return (float8)(float4)value;.

  • Preserve integer conversion rounding during cast folding and subfloat lowering.
  • Fold only when floating widths match or the first conversion is exact for every signed source integer; traced narrowing also requires exactness at the replacement width.
  • Keep precision changes conservative for unsupported formats; no option or new floating-point exception contract is needed.

Tests cover SPARC32 widening, SPARC64 double rounding, later truncation, exact intermediates, and same-width controls. A native x86 roundtrip compares emitted C with both rounding chains using GCC/Clang at O0/O2.

Debian: 48 ELFs, 12 architectures, 10,092 successful function comparisons. Two x87 paths regain spill rounding; three retain extended precision. No new failures or identified regressions; 61 matched timeouts/existing failures excluded.

Paired CPU timing (matched dev opt0 builds): -0.15% unaffected, +6.30% affected (128 functions each).

@thotypous
thotypous force-pushed the fix/integer-float-rounding branch from 1e192e6 to 3785f2e Compare October 14, 2026 03:31
…sions

Keep potentially inexact integer conversion intermediates during float cast folding. Require exactness at both widths before subfloat tracing moves an integer conversion through other consumers.

Add independent SPARC32/SPARC64 stages and a native GCC/Clang emitted-C roundtrip covering widening, double rounding, and later truncation. Re-record only the three new stage assertions; stock 675 and stages 2820 parity, full Rust 8657 tests, spec ownership, and 64 tooling tests pass.
@thotypous
thotypous force-pushed the fix/integer-float-rounding branch from 3785f2e to 496bb0d Compare October 14, 2026 10:11
mahaloz and others added 4 commits October 14, 2026 14:40
Resolve the stage corpus count and re-record docs/baseline-stages.json on
the merged tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
…ehind intfloatround

The 80-bit x87 format has no float-format entry, so the exactness check
declined every x87 conversion: on glibc's i386 libm 116 of 827 functions
moved, nearly all from `1.0 < ABS(a0)` to `(long double)1 < ABS((long double)a0)`
for `fld1`/`fldz` and exact `fild`s. SubfloatFlow now judges only its
replacement precision (never wider than the original, so its exactness
implies the original's) and a constant by its own bits, and the extended
format counts 64 significant bits. That leaves 10 functions: seven regain an
x87 spill's float rounding, one int64 comparison stays in long double where
the narrowed form happened to be exact, and two storage comments.

Because declining still moves some correct output, the check is the named
option `intfloatround` in kuna_intfloatround.rs, default on (0/675 datatest
assertions move; every datatest prints identically); `off` is byte-identical
to main. The SPARC stage gains an off pass, and a new x87 stage pins fld1,
an exact fild compare and a float spill, with an off pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
Re-derive the option counts (301 with castnarrow), recapture the catalog
fixture, regenerate docs/options.md and re-record docs/baseline-stages.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 14, 2026
@mahaloz
mahaloz merged commit 79d0750 into Noelo-Lab:main Oct 14, 2026
2 checks passed
@mahaloz

mahaloz commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Before merging we added one commit on top (0ed2266), keeping your rule as the default path:

  • The check now lives in p3_dataflow/kuna_intfloatround.rs behind option intfloatround (default on; off restores the previous unconditional fold), because declining the fold also changes some output that was already correct.
  • x87 and 68k registers use an 80-bit format that has no float-format entry, so every x87 conversion was declined and glibc's i386 libm printed exact ones in long double (1.0 < ABS(a0) became (long double)1 < ABS((long double)a0)). That format now counts 64 significant bits.
  • SubfloatFlow checks only the precision it narrows to, since that one is never wider than the original, and a constant by its own significant bits, so fld1/fldz still narrow.

An x87 int stored to a float slot and reloaded now keeps its (float) rounding (libm i386 ccoshf, ctanf, cexpf). tests/stages/kuna-integer-float-rounding-x87.xml covers the x87 cases, and the first pass of each stage test runs the option off.

@thotypous
thotypous deleted the fix/integer-float-rounding branch October 14, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants