Skip to content

fix(base): classify F32.hypot overflow at the rounding boundary - #2202

Draft
sebastian-griego wants to merge 1 commit into
bendlang:mainfrom
sebastian-griego:fix/hypot-overflow-boundary
Draft

sebastian-griego wants to merge 1 commit into
bendlang:mainfrom
sebastian-griego:fix/hypot-overflow-boundary

Conversation

@sebastian-griego

Copy link
Copy Markdown
Contributor

F32.hypot can return infinity for a result that rounds to the largest finite F32. It can also return the largest finite F32 for a result that rounds to infinity. Both occur on 9c586f09 in the default CLI, native C, Bun, and Node. This follows #1515.

Inputs Current bits Correct bits
2.22e38, 2.578916e38 7f800000 7f7fffff
3.4028235e38, 1.0e35 7f7fffff 7f800000

Draft because of a performance regression. The numerical checks pass. A focused loop of ordinary calls is 2.71 times slower on Node. The measurements below make that cost explicit for review.

The scaled squares lose information near the overflow boundary. For inputs whose maximum magnitude reaches 2^127, the fix checks the original significands with existing 48-bit Nat operations. The exact check selects infinity or a finite result from the scaled formula.

For the relevant finite inputs, write hi = A*2^104 and lo = B*2^(104-d). The overflow midpoint is (2^24 - 1/2)*2^104. Set D = (2^24-A)*(2^24+A) - 2^24 and P = 4^d. Overflow occurs when floor((B^2 + floor(3P/4))/P) > D. This also handles an exact midpoint. For d >= 12, overflow is impossible, and clamping d to 12 gives the same decision. All integer intermediates fit below 2^48.

The regressions cover both reported cases, signs, argument order, an exact midpoint and its adjacent inputs, large exponent gaps, zero, infinity, and NaN. The GPU test adds parallel checks at the boundary.

Validation

  • An independent integer oracle checked 45,264 input pairs. The default CLI, native C, Bun, and Node pass all cases. Main fails 1,614. The oracle checks classification exactly and finite accuracy within two ULP.
  • Forced Metal passes all 45,264 classification checks. The two-ULP check retains 21 tiny-value failures. Each exact input/output pair also fails on main.
  • The new regression passes the default CLI, native C, Bun, and Node. The extended GPU regression also passes on forced Metal.
  • 34 related math programs passed 169 of 170 local run checks. The Node NaN-payload test is the sole failure and emits identical JS on main and this branch. Its payload behavior is listed in WONTFIX.txt.
  • The repository gate passes 56/56.

Performance on Apple M5 Max, macOS 26.6.2, Apple clang 21, Bun 1.3.14, and Node 24.15.0. These are median process times from nine alternating pairs after one warmup.

Workload Native, one worker Bun Node
30 million ordinary calls 37.34 → 41.68 ms 880.95 → 890.65 ms 146.02 → 395.45 ms
300,000 boundary calls Startup dominates at about 3 ms 172.09 → 228.28 ms 232.83 → 360.85 ms

The ordinary loop uses runtime-generated coordinates and returns the same checksum. On Node, the new cold branch prevents loop peeling in this benchmark. The generated loop then boxes its accumulator and seed on each iteration. With loop peeling disabled for diagnosis, both versions take about 374–380 ms. Several helper arrangements retained this cost. The existing runtime benchmarks contain no F32.hypot calls.

The full cluster suite and CUDA were not run.

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.

1 participant