Skip to content

fix(printf): route float operands through 80-bit long double (#879) - #900

Closed
arookieofc wants to merge 2 commits into
masterfrom
arookieofc/fix-879-printf-longdouble-hexfloat
Closed

arookieofc wants to merge 2 commits into
masterfrom
arookieofc/fix-879-printf-longdouble-hexfloat

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #879.

Float operands now convert through the 80-bit extended format (printf.def:208-213 strtofltmax, :774-798 getfloatmax + L modifier) instead of f64, and %a prints the glibc %La nibble style.

Verification (all oracle probes via WSL GNU bash 5.3 script files, byte-identical):

  • %f 1e999 full 1000-digit expansion; %e 1e999 -> 1.000000e+999; %.50f 0.1 LD digits; %e 5e-324 -> 5.000000e-324
  • %a battery: 5 -> 0xap-1, 0.5 -> 0x8p-4, 4.2 -> 0x8.666666666666666p-1, %.2a 4.2 -> 0x8.66p-1, 1e10 -> 0x9.502f9p+30, 5e-324, 1e-4940 denormal, exact denormal 0x1p-16400 (no ERANGE)
  • half-even: %.0f 0.5/1.5/2.5 -> 0/2/2, %.1a 0x1.78p0 -> 0xb.cp-3, %.0a 0x1.9p0 -> 0xcp-3, %.0a 0xF.8p4 -> 0x1p+8
  • ERANGE: 1e9999/-1e-9999/1e-5000/0xF...F.8p16320/0x1p16384; suffix beats ERANGE (1e9999x -> invalid number); max LD finite, no diagnostic
  • zero-pad after prefix: %015a, %#010x
  • lib 706 pass (1 pre-existing windows_listing_cache env flake also failing on master); printf lib 52/52; new tests/issue879 (9/9)
  • clippy: no new lints in touched files (555 repo-wide hits are pre-existing, newer-clippy); cargo fmt clean

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.
@github-actions

Copy link
Copy Markdown
Contributor

欢迎!这是你的第一个 PR 🎉 请确认已跑过本地测试套件;CI 通过后维护者会 review。

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.
@caomengxuan666

Copy link
Copy Markdown
Member

独立评审结论:REQUEST-CHANGES

(独立评审代理,自建 worktree @ 7e89824 + a05ab23 复核;oracle = WSL GNU bash 5.3.0(1) /usr/local/bin/bash,脚本文件批量探针 + CREATE_NO_WINDOW 运行器。)

主体语义已达标,但新引擎的 hex 字面量路径存在两处可复现的逐字节分歧(均落在 #879 的浮点族逐字节验收面内),修完即可。

已验证逐字节一致的部分(34 形探针矩阵)

性能抽验

300 次 printf '%f' 1e999 大循环:rubash 0.56s(含进程启动)vs GNU ~8.4s(纯循环)——long-double 模拟无性能回退,反而显著快于 glibc 该路径。

两处 REQUEST 级分歧(src/builtins/printf/longdouble.rs,双侧实测)

1. hex 字面量 >100 位有效数字时窗口截断丢幅度(值错 16^k 倍)。
parse_hex_subject 把 sig 截到 HEX_KEEP_DIGITS=100 时没有像十进制路径那样补偿指数(十进制路径有 exp10 + drop,hex 路径 pexp 未加 4 * drop)。复现(101 位有效 hex = 16^100+1 = 2^400+1):

printf '%f\n' 0x100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001p0
GNU:   2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493376.000000   (2^400)
本 PR: 161390617380431786853494948250188242145606612051826469551916209783790476376052574664352834580008614464743948248296718336.000000   (2^396,差 16 倍)
%a 同病:GNU 0x8p+397 vs 本 PR 0x8p+393

建议修法(一处):窗口分支补 pexp += 4 * (sig.len() - HEX_KEEP_DIGITS)(sticky 语义不变)。

2. hex denormal 下溢 guard 差一档(八度),半点以上被误归零。
4 * digits + pexp <= -16445 会把 (2^-16446, 2^-16445) 区间(glibc 就近入 min denormal)也判成零。复现:

printf '%a\n' 0xFp-16449
GNU:   0x0.000000000000001p-16385  rc 1(ERANGE)
本 PR: 0x0p+0                      rc 1     ← 分歧
printf '%a\n' 0x9p-16449   同样分歧(0x8p-16449 恰半点,双侧同为 0x0p+0 ✅)

阈值应为 <= -16446(低于 min denormal 半点才可归零;恰半点经精确路径 tie-to-even 归零已正确)。

两形均在 PR 自述探针电池之外,但属于 #879「高精度字面量 / 长尾数形」验收族;修复面很小,补上后建议把上述 4 条复现加进 tests/issue879_*。

回归与卫生

  • 全量 --no-fail-fast 322 靶:失败集与 master 逐名一致(fd_redirects c_external 族、coproc、tilde 盘符形、part_025 PWD 形等预存环境性,已在 master 复现;lib 的 windows_listing_cache flake 同样 master 可复现);issue879 新靶 9/9;examples::* 在评审 worktree 的失败为 third_party/bash 子模块未初始化伪影,初始化后 10/10 绿。
  • cargo fmt --check 干净;touched 文件 clippy 无新 lint;越界审计无(printf 引擎 + utf8-hygiene allowlist 同步更新,无外溢);无新依赖;GPL 红线干净(纯 Rust 自研实现,GNU 源仅行号注释引用,vendored 子模块未触碰)。
  • 非阻塞 nit:parse_decimal_subject 溢出 guard 里 (k_lo < 0 && 4 * k_lo >= 16384) 为恒假死支,可删或订正意图。

结论:REQUEST-CHANGES(就上述两处修复后复审),不合入。

@caomengxuan666

Copy link
Copy Markdown
Member

补充评审(a05ab236):十进制长有效数字窗口 DECIMAL_KEEP_DIGITS = 32 的半点偏置分歧

在上一条评审(parse_hex_subject 的 HEX_KEEP_DIGITS 指数补偿与 -16446 下溢阈值)之外,补充一处十进制转换路径的同源分歧(同样属于 #879「高精度字面量 / 长尾数形」验收面):

3. parse_decimal_subject 将有效十进制位截断在 DECIMAL_KEEP_DIGITS = 32(src/builtins/printf/longdouble.rs:66, 491-499),导致 >32 位十进制字面量跨越二进制半点时舍入方向错误

  • 原因:
    十六进制下 $16 = 2^4$,每个 hex nibble 与二进制位严格对齐,超过 66 bit 之后截断并折叠为 sticky bit 是数学等价的。
    但十进制下 $10 = 2 \times 5$ 不是 2 的幂:在 32 位十进制(约 106 bit)处截断 sig 并把 exp10 加 drop,等于从真值中减去了 $\Delta \in (0, 10^{\text{exp10}+\text{drop}})$,而 round_scaled_to_ld 中的 sticky_in 仅在 low3 == 0b100 && rem.is_zero()(截断后的 32 位十进制值恰好等于二进制半点)时才参与判断。
    一旦真实字面量位于二进制半点 $(2M+1)\cdot 2^{p-1}$ 及以上、但前 32 位截断值落在该二进制半点以下(例如 $1.0$ 附近的 80-bit 相邻中点 $1 + 2^{-64}$ 和 $1 + 3\cdot 2^{-64}$ 展开本身就有 65 位有效十进制数字),scaled_divmod 算出的 guard 直接变成 0,sticky_in 被完全忽略,从而向零偏置 1 ULP。
  • 复现(WSL GNU Bash 5.3.0 对拍):
    # 1) 36 位有效数字,略高于 1 + 2^-64 半点
    printf '%a\n'   1.00000000000000000005421010862427523
    printf '%.21f\n' 1.00000000000000000005421010862427523
    
    # 2) 精确半点 1 + 3*2^-64(kept 尾数为奇数 0x8000000000000001,tie-to-even 应进位)
    printf '%a\n'   1.0000000000000000001626303258728256651011179201304912567138671875
    printf '%.21f\n' 1.0000000000000000001626303258728256651011179201304912567138671875
    • WSL GNU Bash 5.3.0:
      0x8.000000000000001p-3
      1.000000000000000000108
      0x8.000000000000002p-3
      1.000000000000000000217
      
    • 本 PR (a05ab236)(DECIMAL_KEEP_DIGITS = 32 截断后均小于半点,guard = 0):
      0x8p-3
      1.000000000000000000000
      0x8.000000000000001p-3
      1.000000000000000000108
      
  • 建议修法:将 DECIMAL_KEEP_DIGITS 放宽(例如 1150 或 2048;短字面量由于 sig.len() <= DECIMAL_KEEP_DIGITS 完全零开销,长字面量在 BigUint 下也只需几十个 u64 limb,耗时微秒级),可与上一条评审中的两处 parse_hex_subject 修复一并合入并加入 tests/issue879_printf_longdouble_regressions.rs。

@caomengxuan666

Copy link
Copy Markdown
Member

复审结论(a05ab236 = 当前 head):REQUEST-CHANGES 维持,不合入

(复审代理独立复核:自建 worktree 检出 PR merge ref a05ab236,release 构建 rubash.exe(src/main.rs 真引擎,非 src/bin/bash.rs shim);oracle = WSL GNU bash 5.3.0(1) /usr/local/bin/bash;单脚本批量探针 + CREATE_NO_WINDOW 蹦床,输出落文件逐字节 diff。说明:当前 head 与前两条评审所测 commit 相同——评审之后未收到新提交,本轮是对既报分歧点的实证复核,并另发现一处新分歧。)

复核矩阵(stderr 归一化后双侧逐字节一致,stdout 46+8 形中 9 处分歧,全部复现)

点 1(hex 窗口截断丢指数补偿)——仍在。 src/builtins/printf/longdouble.rs 窗口分支(HEX_KEEP_DIGITS = 100 截断处)依然只有 sticky、没有 pexp += 4 * drop 补偿:

printf '%a\n' 0x1<99个零>1p0    # 101 位有效 hex = 16^100+1
GNU:   0x8p+397
本 PR: 0x8p+393                ← 差 16 倍
printf '%f\n' 同字面量
GNU:   2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493376.000000  (2^400)
本 PR: 161390617380431786853494948250188242145606612051826469551916209783790476376052574664352834580008614464743948248296718336.000000  (2^396)

点 2(hex denormal 下溢 guard 差一档)——仍在。 guard 仍为 4 * digits + pexp <= -16445(应为 <= -16446):

printf '%a\n' 0xFp-16449
GNU:   0x0.000000000000001p-16385  rc=1
本 PR: 0x0p+0                      rc=1   ← 分歧
printf '%a\n' 0x9p-16449           同病(0x0.000000000000001p-16385 vs 0x0p+0)
printf '%a\n' 0x8p-16449           双侧同为 0x0p+0 rc=1 ✅(恰半点锚未破坏)
printf '%a\n' 0x7p-16449           双侧同为 0x0p+0 ✅

点 3(十进制窗口 DECIMAL_KEEP_DIGITS = 32 半点偏置,补充评审)——仍在。 四形逐字节复现(数值同补充评审所引):

printf '%a\n'    1.00000000000000000005421010862427523
GNU:   0x8.000000000000001p-3      本 PR: 0x8p-3
printf '%.21f\n' 1.00000000000000000005421010862427523
GNU:   1.000000000000000000108   本 PR: 1.000000000000000000000
printf '%a\n'    1.0000000000000000001626303258728256651011179201304912567138671875
GNU:   0x8.000000000000002p-3      本 PR: 0x8.000000000000001p-3
printf '%.21f\n' 同上
GNU:   1.000000000000000000217    本 PR: 1.000000000000000000108

新发现(点 4):'0' 旗标对 inf/nan 未被忽略(a/A/e/f 族)

C 标准与 glibc:参数为 inf/nan 时 '0' 旗标无效(空格垫,符号紧贴 nan/inf)。本 PR 全部零垫、且在符号后插零。8 形全分歧:

形式                GNU                  本 PR
printf '%010a\n' nan    "       nan"      "0000000nan"
printf '%010a\n' inf    "       inf"      "0000000inf"
printf '%010A\n' NAN    "       NAN"      "0000000NAN"
printf '%015a\n' nan    "            nan" "000000000000nan"
printf '%010f\n' inf    "       inf"      "0000000inf"
printf '%010e\n' nan    "       nan"      "0000000nan"
printf '%+010a\n' nan   "      +nan"      "+000000nan"
printf '%010a\n' -nan   "      -nan"      "-000000nan"

修法提示:format_hex_ld/浮点拼装在 inf/nan 分支里忽略 zero 旗标(走空格右垫路径),带号形让符号紧邻字面量。

本轮确认无回归的部分

待办(就绪后一次推齐)

  1. 点 1:hex 窗口分支补 pexp += 4 * drop;
  2. 点 2:guard 阈值 -16445 → -16446;
  3. 点 3:DECIMAL_KEEP_DIGITS 放宽(如 1150/2048);
  4. 点 4:inf/nan 忽略 '0' 旗标;
  5. 建议把上述全部复现形加入 tests/issue879_printf_longdouble_regressions.rs;顺带:parse_decimal_subject 溢出 guard 里 (k_lo < 0 && 4 * k_lo >= 16384) 恒假死支仍在(longdouble.rs:476),可一并清理。

修完 ping 我,逐形复审后放行。

@caomengxuan666

Copy link
Copy Markdown
Member

返工 PR 已开:#935 —— 实现本轮评审全部四点分歧(hex 窗口 pexp += 4*drop、denormal guard -16446、DECIMAL_KEEP_DIGITS→2048、inf/nan 忽略 '0' 旗标),外加 k_lo < 0 && 4*k_lo >= 16384 恒假死支清理;全部复现形已加入 tests/issue879_printf_longdouble_regressions.rs(13/13)。

maintainerCanModify=false,无法直推本分支,故基于 PR head a05ab236 另开 #935 承载返工提交 6cc4d6ac——原作者优先:若 @a-rookie-of-C-language 先将该提交合入本 PR,#935 即可关闭(先到先得)。返工提交仅含评审发现实现,原始两提交署名保留。

caomengxuan666 added a commit that referenced this pull request Oct 20, 2026
…uard, decimal window, '0' on inf/nan (#935)

* fix(printf): route float operands through 80-bit long double (#879)

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.

* fix(printf): UTF-8 hygiene single-point digit widening (#879)

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.

* fix(printf): #879 PR #900 review — hex window pexp, denormal guard, decimal 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>

---------

Co-authored-by: arookieofc <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member

Review findings implemented in #935 (merged to master as f36a3c6).

@caomengxuan666

Copy link
Copy Markdown
Member

The four review findings this PR targets have landed via #935 (squash f36a3c6 — hex window pexp compensation, denormal guard -16446, decimal window widening, '0'-flag inf/nan space-pad; 46+8 form probes byte-identical to GNU). Thank you @a-rookie-of-C-language — closing as superseded; branch remains for reference.

@caomengxuan666
caomengxuan666 deleted the arookieofc/fix-879-printf-longdouble-hexfloat branch October 21, 2026 08:07
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
The niu GAP-3 host lane (niubash#900) consumes Session::bind_view
at 7 production call sites and edited its ledger copy's §2.3 row
accordingly; this is the byte-identical rubash-side copy so the
lockstep holds when unixwin#900 merges.

Refs niubash#863
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.

printf 浮点预存分歧:%f 1e999 long-double 展开与 %a/%A 尾数风格——#869 CR 118 形矩阵 3 例外

2 participants