Repository navigation
fix(printf): #879 PR #900 review rework — hex window pexp, denormal guard, decimal window, '0' on inf/nan - #935
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-213strtofltmax,:774-798getfloatmax with theLmodifier). 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).maintainerCanModifyon #900 is false, so the rework lands here and is cross-linked instead of pushed to the PR branch.Review findings implemented
pexp += 4 * drop—parse_hex_subjecttruncatedsigtoHEX_KEEP_DIGITSwithout 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, sopexp += 4 * drop.-16445→-16446— the guard zeroed(2^-16446, 2^-16445), which glibc rounds UP to the minimum denormal (inexact → ERANGE).0xFp-16449/0x9p-16449now print0x0.000000000000001p-16385; the exact half0x8p-16449still ties to even →0x0p+0.DECIMAL_KEEP_DIGITS32 → 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, andstickyis only consulted atlow3 == 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.'0'flag ignored for inf/nan —format_valuenow clearszero_padfor nonfinite results, so%010aofnanisnan(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 >= 16384overflow-guard disjunct flagged as a nit in the same review.Test plan
tests/issue879_printf_longdouble_regressions.rs— 13/13 passcargo test --lib printf— 52/52 passcargo test --test issue510_printf_write_error_status_regressions --test issue326_printf_time_strftime_regressions— 14/14 pass%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 columncargo fmt --checkclean; no new clippy lints in touched filesFiles changed:
src/builtins/printf/longdouble.rs,src/builtins/printf/value.rs,tests/issue879_printf_longdouble_regressions.rsRefs #879, Refs #900
Generated with Devin