Skip to content

fix(printf): #879 PR #900 review rework — hex window pexp, denormal guard, decimal window, '0' on inf/nan - #935

Merged
caomengxuan666 merged 3 commits into
masterfrom
cr-fix/900
Oct 20, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
cr-fix/900

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Rework of #900, implementing all four REQUEST-CHANGES findings from its review (comment 6290017395 and follow-ups) in the 80-bit long-double printf emulation (GNU printf.def:208-213 strtofltmax, :774-798 getfloatmax with the L modifier). Branch is built on the PR #900 head (a05ab236) so the original two commits by @a-rookie-of-C-language are preserved — original author has priority; if they pull this commit into #900 first, this PR can close (first-come-first-served).

maintainerCanModify on #900 is false, so the rework lands here and is cross-linked instead of pushed to the PR branch.

Review findings implemented

  1. Hex window lost pexp += 4 * drop — parse_hex_subject truncated sig to HEX_KEEP_DIGITS without the decimal path's exponent compensation. 0x1<99 zeros>1p0 (16^100+1) printed 2^396 instead of 2^400. Dropped nibbles are exact 4-bit groups, so pexp += 4 * drop.
  2. Denormal guard -16445 → -16446 — the guard zeroed (2^-16446, 2^-16445), which glibc rounds UP to the minimum denormal (inexact → ERANGE). 0xFp-16449/0x9p-16449 now print 0x0.000000000000001p-16385; the exact half 0x8p-16449 still ties to even → 0x0p+0.
  3. DECIMAL_KEEP_DIGITS 32 → 2048 — a dropped decimal tail is not bit-aligned (10 = 2×5), so a 32-digit truncation could pull the kept value below a binary tie the true value sits at/above, and sticky is only consulted at low3 == 0b100. The widened window keeps the review's boundary literals (36/76 digits) on the exact path; a residual counterexample now needs a >2048-significant-digit literal, documented in the constant's comment.
  4. '0' flag ignored for inf/nan — format_value now clears zero_pad for nonfinite results, so %010a of nan is nan (spaces, sign adjacent), matching glibc across a/A/e/E/f/F/g/G.

Also removed the always-false k_lo < 0 && 4*k_lo >= 16384 overflow-guard disjunct flagged as a nit in the same review.

Test plan

  • All review reproducers added to tests/issue879_printf_longdouble_regressions.rs — 13/13 pass
  • cargo test --lib printf — 52/52 pass
  • cargo test --test issue510_printf_write_error_status_regressions --test issue326_printf_time_strftime_regressions — 14/14 pass
  • Script-file probes vs reviewer's WSL GNU bash 5.3.0 oracle table: %010a inf/nan, %010A NAN, %015a nan, %010f inf, %010e nan, %+010a nan, %010a -nan, %05g nan, %-10a nan, %010.3f inf, % 010a nan, %010g -inf, 0xFp-16449, 0x8p-16449, 0x7p-16449, 101-hex-digit %a/%f — all byte-identical to the GNU column
  • cargo fmt --check clean; no new clippy lints in touched files

Files changed: src/builtins/printf/longdouble.rs, src/builtins/printf/value.rs, tests/issue879_printf_longdouble_regressions.rs

Refs #879, Refs #900

Generated with Devin

arookieofc and others added 3 commits October 20, 2026 01:16
GNU spec: builtins/printf.def:208-213 floatmax_t/strtofltmax
(strtold), printf.def:774-798 float conversions via getfloatmax
with the 'L' modifier (glibc formats a long double), printf.def:1387-1398
chk_converror (a trailing suffix beats ERANGE).

- New src/builtins/printf/longdouble.rs: pure-Rust x86-64 80-bit
  extended emulation (host long double is double on MSVC). Exact
  decimal/hex literal conversion with one round-half-even step to the
  64-bit mantissa; exact decimal expansion of mant*2^exp for the
  formatters; overflow/underflow decided by magnitude guards plus
  ERANGE from the rounder (overflow, inexact underflow).
- number.rs: parse_f64/convert_float_subject/hex_significand_to_f64
  replaced by parse_long_double (suffix still beats ERANGE, matching
  chk_converror order).
- float.rs: %f/%e/%g/%a engines work from exact digits and round
  half-even (oracle: %.0f of 0.5/1.5/2.5 -> 0/2/2). %a prints the
  64-bit mantissa nibble-style with D = exp+60 (oracle: %a of 5 ->
  0xap-1, %.2a of 4.2 -> 0x8.66p-1); %g re-selects style after
  rounding (%.2g of 99.9 -> 1e+02).
- value.rs: zero padding goes after sign and 0x/0X prefix
  (%015a 5 -> 0x000000000ap-1, %#010x 255 -> 0x000000ff).
- Fixed a carry-to-infinity guard typo in the rounder
  (exp+63 vs the unreached exp-63+64) and two stale f64-locked
  %a unit expectations; added
  tests/issue879_printf_longdouble_regressions.rs (9 tests).

Verified: probe batteries A/B/C/D/F/G byte-identical to WSL GNU
bash 5.3 (/usr/local/bin/bash) via script files; lib 706 pass
(1 pre-existing windows_listing_cache env flake); printf lib
52/52; new integration 9/9.
Fold the float engines' digit/nibble -> char widenings into two
reviewed helpers (decimal_digit_char/hex_digit_char in
printf/longdouble.rs, operands 0-9/0-15 by construction) and
allowlist them in scripts/check-utf8-boundary-hygiene.sh, replacing
the stale float.rs digits[] entry from the retired f64 engine.
…ecimal window, '0' on inf/nan

Implements all four REQUEST-CHANGES findings from the #900
review in the 80-bit long-double emulation (GNU printf.def:208-213
strtofltmax / :774-798 getfloatmax with the 'L' modifier):

1. parse_hex_subject: the >100-significant-digit window truncated `sig`
   without the decimal path's exponent compensation. Dropped hex nibbles
   are exact 4-bit groups, so `pexp += 4 * drop`; 16^100+1 = 2^400+1 had
   printed as 2^396 (16x too small).
2. parse_hex_subject: the underflow guard `4*digits + pexp <= -16445`
   zeroed (2^-16446, 2^-16445), which glibc rounds UP to the minimum
   denormal (inexact -> ERANGE). Threshold is half the minimum denormal:
   `<= -16446`.
3. DECIMAL_KEEP_DIGITS 32 -> 2048: a dropped decimal tail is not
   bit-aligned (10 = 2*5), so truncation could pull the kept value below
   a binary tie the true value sits at/above, and `sticky` is only
   consulted at `low3 == 0b100`. The widened window keeps boundary
   literals on the exact path while still bounding the bignum.
4. format_value: the '0' flag no longer applies to nonfinite results —
   glibc space-pads inf/nan with any sign adjacent (`%010a` of nan is
   `       nan`, not `0000000nan`).

Also removes the always-false `k_lo < 0 && 4*k_lo >= 16384` overflow
guard disjunct flagged as a nit in the same review.

All review reproducers are added to
tests/issue879_printf_longdouble_regressions.rs (13/13); printf lib
tests 52/52; issue326 + issue510 regression files green; cargo fmt
clean; no new clippy lints in touched files.

Original patch series by a-rookie-of-C-language (PR #900); this commit
is the review-finding rework only.

Refs #879, Refs #900

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

2 participants