Skip to content

[VFDLIBM-34] Port hypot from fdlibm - #143

Merged
MaxGekk merged 5 commits into
mainfrom
port-hypot
Nov 23, 2026
Merged

MaxGekk merged 5 commits into
mainfrom
port-hypot

Conversation

@MaxGekk

@MaxGekk MaxGekk commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

[VFDLIBM-34] Port hypot from fdlibm

What changes were proposed in this pull request?

Part of #34. Ports hypot from fdlibm 5.3's e_hypot.c, the second function of two arguments, on the pair harness and the gate of #136.

  • Hypot and VFdlibm.hypot(x, y) / hypot(x, y, live) (a lane that is not live keeps x). Every finite lane is computed in the vector by the C's operations in the C's order: the swap by high words only, line 63's a+b, the 2^-600, 2^600 and 2^1022 scalings with their own nested compares and int k, both forms of the medium path with their radicands blended by w > b under one SQRT, and t1*w blended on k != 0.
  • The words stay in int lanes apart from the values, so after the 2^1022 scaling lines 97, 103 and 106 read the stale ha and hb exactly as the C does; every __HI(v) = word is written out inline (I2L, shift, or), and the only calls come first (Lanes.intSpecies, Lanes.hiInt).
  • One arm, the finish: lanes with an infinity or a NaN in either argument (ha >= 0x7ff00000) are finished per lane by StrictMath.hypot, first eight lanes at constant indices. No second arm: a blended non-finite arm would rest on hardware NaN propagation for one-NaN lanes, no timing exists to justify one, and Run the benchmarks owed on the hosts and decide each function's arm #30 sends a tie to the finish arm.
  • PROOFS.md: a hypot section (no fold; L13's hypotheses checked for each blend) and L16, a counter-example: refreshing the words after the 2^1022 scaling changes bits (four witnesses, 42,876 of 1,048,576 subnormal-b pairs).
  • Four planted faults with CATCHERS and pom.xml executions, each sized on 2^22 draws (SKILLS.md): hypot.dropEarlyReturn, hypot.refreshedWords, hypot.dropT2, hypot.dropY2; a swap by value at line 60 was tried and dropped, since part of what caught it was a NaN lane the fault let past the finish's mask, not the swap (SKILLS.md).
  • hard-cases/hypot.txt: 8,400 pairs computed by dev/hypot-hard-cases.py with exact integer arithmetic (near-midpoint pairs of both medium-path forms, exact triples with nearly equal legs, scaled into every range); no published table exists, and Port hypot and pow, the last two of the thirteen #34's question on self-computed files is still open. HardCasePairsTest rechecks every pair with BigDecimal.
  • Nightly.PairFunction.HYPOT (one arm, MAGNITUDES, the C's words and gaps), so Sweep and Fuzz take it by hand; nightly.yml is not changed, so the night does not run it yet (a later pull request, as [VFDLIBM-35] Add asin, acos and atan2 to the nightly #126 was for atan2).
  • The dev/jar-check.sh consumer calls hypot with the MAGNITUDES rule; HypotBenchmark (finish256, finishPreferred, finishMax, common and mixed, refusing any other value); CHANGELOG, README, PLAN, SKILLS, VFdlibm Javadoc.

Why are the changes needed?

hypot is one of the thirteen functions Spark computes with fdlibm (PLAN.md section 3), and Varka's floating track emits it (vecbricks/varka#1216, row 358).

Does this PR introduce any user-facing change?

Yes: adds VFdlibm.hypot. Every lane equals StrictMath.hypot by raw bits, except a lane where both arguments are NaN with different bits, which is one of the two with the sign cleared and the quiet bit set (MAGNITUDES).

How was this patch tested?

  • HypotTest extended: the gate's inputs through the vector at every width and SPECIES_MAX, unmasked and masked; every subset of lanes rare beside every way out of the C; L16's witnesses at every lane; the hard-case pairs; constants; the four fault catchers.
  • mvn -B verify passes locally, and with -Dvfdlibm.test.jvmArgs=-XX:MaxVectorSize=32 and =16.
  • bench/alloc-check.py passes on the HypotBenchmark rows at this host's flags and with --jvm-args="-XX:UseAVX=2 -XX:MaxVectorSize=32".
  • By hand through the nightly programs: dev/nightly.sh fuzz hypot 10 0x5eed 256 (307 million pairs, every lane matched), a sweep of stripe 5/256 for each argument (clean), and the fuzz with hypot.dropY2 planted (exit 1). dev/jar-check.sh and its --self-test pass.
  • Not run: CI, the Arm preferred rows, the suite under -XX:UseAVX=2.
  • No results file times hypot, so no speed is claimed.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Opus 5.5)

Max Gekk added 5 commits November 23, 2026 00:34
VFdlibm.hypot(x, y) and hypot(x, y, live) from e_hypot.c: every finite
lane in the vector by the C's operations in its order, the words kept in
int lanes apart from the values so the 2^1022 scaling's stale words are
read as the C reads them (PROOFS.md L16, a counter-example), and the
lanes of an infinity or a NaN finished per lane by StrictMath.hypot. One
arm, the finish. Four planted faults, hard-cases/hypot.txt computed by
dev/hypot-hard-cases.py, HypotBenchmark, the jar-check consumer with the
MAGNITUDES rule, Nightly.PairFunction.HYPOT (not in nightly.yml).

Generated-by: Claude Code (Opus 5.5)
…he unread claims

The hard-case check takes each midpoint from the doubles either side, so
the narrower gap below a power of two is not mistaken for a midpoint.
SKILLS.md and VFdlibm's Javadoc no longer state allocation readings that
no results file holds, and PLAN.md says why the night does not list
hypot yet.

Generated-by: Claude Code (Opus 5.5)
… lane shows

A swap by |y| > |x| at line 60 changes no finite result, and was caught
only because a NaN y then skips the swap and the rare mask misses the
lane. hypot.dropY2 drops line 108's t1*y2 instead, sized on 2^22 draws.
The Javadoc puts line 79 before the 2^1022 scaling, as the C has it, and
dev/nightly.sh's usage names hypot.

Generated-by: Claude Code (Opus 5.5)
…ds hypot to the night

Generated-by: Claude Code (Opus 5.5)
@MaxGekk
MaxGekk merged commit 99d53f9 into main Nov 23, 2026
14 checks passed
MaxGekk added a commit that referenced this pull request Nov 23, 2026
…emits (#147)

<!-- title: [VFDLIBM] Prove log10's words in long lanes, the form Varka's kernel emits -->

### What changes were proposed in this pull request?

This PR adds lemma L17 to `PROOFS.md`, in the log10 section, and its tests in `ProofsTest` (`l17LongWordsAreLog10sInts`, `l17RareComparesAreTheCs`, `l17NegativeLanesAreAnsweredEarly`, `l17NegativeLanesAreBlendedOver`).
The library code does not change. `Log10.java` gets one comment pointer to L17.
Varka's emitted `log10` kernel keeps the C's `int` words in long lanes, as `Log` does (L8).
L17 proves that these long words, and the kernel's tests on the raw bits, give the C's values on the lanes whose result is kept.

<details>
<summary>The paired Varka PR, the statement of L17 and the proof</summary>

The paired Varka pull request is vecbricks/varka#1645, "Emit fdlibm's log and log10 as kernel IR nodes, exact to StrictMath", which L17 cites.
Its kernel computes the operations of `Log10.log10Blended` in the same order.
The difference is where the words live: the C's `int` words `hx`, `k` and `i` are long lanes, and `y = (double)(k+i)` is L7's exact add and subtract of `0x1.8p52` instead of `I2D`.
`Log10` here keeps int lanes, so the log10 section's sentence "needs no lemma of its own" still holds for this library. L17 is about Varka's kernel only, as L15's (a) to (c) are.

L17 states three facts.
(a) With the sign bit clear, the long `K = (HX >>> 20) - 1023` (less 54 on a scaled lane), `I = K >>> 63` and the new high word `(HX & 0xfffff) | ((0x3ff - I) << 20)` are the C's `k`, `i` and new `hx`, widened. The new word is below 2^31, so its shift into place is the C's `__HI(x) = hx`.
(b) For `K` in `[-1077, 1024]`, the L7 conversion of `K + I` is the C's `(double)(k+i)`.
(c) For every lane, either sign, the signed long compares `b > 0x7ff00000_00000000 - 1` and `b < 0` are the C's `hx >= 0x7ff00000` and `hx < 0`.

The proof is range arithmetic.
With the sign clear, the high word is in `[0, 2^31)`, so the logical and the arithmetic shift agree, and `K` is in `[-1077, 1024]`.
Bit 63 of a long is its sign, so `K >>> 63` is 1 exactly when `K < 0`.
The compares follow from `b = hx * 2^32 + lo` with `0 <= lo < 2^32` (L9's argument on the raw bits).

The hypothesis H18 is that the sign bit is clear on every kept lane.
A lane with the sign set is `x < 0` or `-0`. The C answers it at line 79 or 78, and the kernel blends those answers over the general result.
H18 is needed: on a negative lane the long `K` is not the C's `k`.

The change is on `main` after #143. `L12` to `L16` and `H17` are taken, so the lemma is L17 and the hypothesis H18.
On the old branch it was L13 with H17.

</details>

### Why are the changes needed?

The Varka kernel writes `log10` with long words and tests its rare lanes on the raw bits.
"Same result, proven" (`AGENTS.md`) asks for a numbered lemma for any form that departs from the C.
Without L17 the kernel's long words rest on an unwritten argument.

### Does this PR introduce _any_ user-facing change?

No.
Only `PROOFS.md`, tests and one code comment change.
No public method, result or requirement moves, so `CHANGELOG.md` has no line.

### How was this patch tested?

`mvn -B verify` on the head (27bf8a4, with `main` at 7acd20d merged in): BUILD SUCCESS, 799 tests, 0 failures, 0 errors, 4 skipped, in 6 min 44 s.
`ProofsTest` runs 50 tests.
The four L17 tests are new.
Each hinge test is built to fail when its hinge is false.
`l17NegativeLanesAreAnsweredEarly` is a witness, not a hinge test: it checks only the oracle, that `StrictMath.log10` gives a NaN, or `-inf` for `-0`, on every sign-set input.
`l17NegativeLanesAreBlendedOver` and Varka's suite are the checks of H18. `l17NegativeLanesAreBlendedOver` checks H18 on `Log10`'s own masks: `Log10.log10Blended` at every width, with sign-set inputs beside their positive twins in one vector.
A dropped rare blend is caught: with `Log10.log10Blended`'s blend for negative lanes commented out, `l17NegativeLanesAreBlendedOver` failed at all four widths (a sign-set input gave a finite result instead of a NaN). The mutation was made by hand and reverted.

Where the real check lives: that Varka's kernel uses these long words is checked only in Varka's `VarkaMathPortSuite`, in vecbricks/varka#1645.
`l17LongWordsAreLog10sInts` is a Java loop over every exponent field and every significand word, the values the proof's bound allows.
`l17RareComparesAreTheCs` checks the compares at their edges and on 2^20 samples, not over every word. A loop over every high word took 44 s, too long for a test.

Not run: `dev/prove.sh`, `dev/jar-check.sh` and any benchmark.
No `.smt2` file changed, so `prove.sh` has nothing new to check.
Only a comment changed in the main sources, so `jar-check.sh` has nothing new to check.
Nothing here is a speed claim, so no benchmark.

`/code-review high` on d78a0d7 found no bug and 5 nits, fixed in 7828ab2 and 27bf8a4. The review of 7828ab2 found no bug and four nits; all four are addressed in 27bf8a4. 27bf8a4 itself has not been reviewed.

<details>
<summary>Test setup, the review items and the log1p lemma</summary>

The last run used `origin/main` at 7acd20d as base, JDK 25.0.5.1, Maven 3.9.16, on this laptop (not a timing).
An earlier run on d78a0d7, before the review fixes, also gave BUILD SUCCESS with 799 tests, 0 failures and 4 skipped, in 15 min 22 s. The reviewer's own Maven run overlapped part of it.

The nits:

- Fixed: H18 now names a NaN with the sign bit set among the lanes the C answers early.
- Fixed: a kept lane's `K` is in `[-1074, 1023]`. Field `0x7ff` gives 1024, but that lane is `+inf` or a NaN and is always blended over.
- Fixed: the significand loop ran over five exponent fields that both words mask away. It now loops over the significand and `I` only.
- Fixed: the H18 witness asserted that `K` differs on some sign-set pattern. That assertion could never fail, so 27bf8a4 drops it and calls the test a witness.
- Fixed in 27bf8a4: the Varka tie cited the branch `varka-345d-log-r`. It now cites vecbricks/varka#1645, as L15 cites a PR number.

The review of 7828ab2 (four nits, all addressed in 27bf8a4): a self-contradicting sentence in L17's hinge-test paragraph, reworded; the witness test's assertion that could not fail, dropped, with `l17NegativeLanesAreBlendedOver` and Varka's suite named as the checks of H18; the Varka branch cite, now vecbricks/varka#1645; and `l17CK`'s Javadoc, now citing `e_log10.c` lines 80 and 84.

The log1p lemma (L14 on the old branch `varka-log1p-long-words`) is not in this PR.
`AGENTS.md` says each function comes in its own PR, paired with the Varka PR that emits it.
The Varka PR for log1p is a later part. That lemma will be renumbered then.

</details>

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)
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