Skip to content

Port hypot and pow, the last two of the thirteen #34

Open

Description

@MaxGekk

Varka's order, VARKA-356.md 2.6 items 4 and 5 (row 358).

  • hypot (e_hypot.c, 115 lines): two arguments swapped so that a's high word is at least b's (with equal high words either may be the larger, which the code allows for), an early a + b when their ratio exceeds 2^60, scaling by 2^-600, 2^600 or 2^1022 at the extremes, and two sqrt forms over operands split at the high word. Uses Port atan, then atan2, asin and acos; atan2 brings the first two-argument nightly #33's two-argument harness.
  • pow (e_pow.c, 309 lines, the largest of the thirteen): no call to exp or log; its own log2(x) in two pieces, y*log2(x) split as n + y', and its own exp of y'. Before any code:
    • Line 306 calls scalbn for subnormal results, and s_scalbn.c is not in reference/fdlibm-5.3/. Fetch it from netlib, with its hash in SOURCES.md, or finish those lanes per lane; it may not come from OpenJDK or glibc.
    • A layout of its branches in the pull request. The special cases are many and each rare: on y (0, +-1, 2, 0.5, +-inf, |y| > 2^31, and whether y is an odd or even integer when x < 0), on x (+-0, +-1, +-inf), and NaN in either. The likely shape is one common path (x > 0 finite and normal, |y| moderate, result in range) with everything else finished by StrictMath.pow, and a proof that the guard admits only lanes the common path is right for.
    • Its body will be the largest; expect NodeCountInliningCutoff to decide what can be a helper.
    • Hard cases: the resources so far come from tables for one-argument functions (PLAN.md section 4). Find what exists for pow, and for atan2 and hypot, and say in each pull request what its hard-case file holds.

Done when as #32's, per function; then PLAN.md section 3 lists the thirteen as done and the optional set (tan, tanh, cbrt, sin, cos) gets a yes or no.

Amended on 20 November 2026 from the plan below: the swap orders the high words only.

Activity

  1. MaxGekk commented on Nov 20, 2026

    @MaxGekk
    ContributorAuthor

    Implementation plan

    File and line references are to main at a32c806; C lines are reference/fdlibm-5.3/'s.

    Outcome. VFdlibm.hypot and VFdlibm.pow, each with its masked form and each in a pull request of its own, every lane bit-identical to StrictMath. Then PLAN.md section 3 lists the thirteen as done and the optional five have a yes or a no.

    Decisions first.

    1. scalbn. e_pow.c:306 calls it for a subnormal result, and fdlibm.h:161 only declares it. Recommended: finish those lanes per lane in pow's first version, which needs no new C file. Fetch s_scalbn.c from netlib, with its row and hash in reference/fdlibm-5.3/SOURCES.md (:15-48), only when step 4 wants those lanes in the vector.
    2. Which of pow's special cases are in the vector from the start. Recommended: y of 0, 1, -1, 2 and 0.5, whose results are one, x, one/x, x*x and sqrt(x) (e_pow.c:119, :156-163), blended behind one anyTrue. A constant exponent of 2 or 0.5 makes every lane special, so finishing them per lane would run StrictMath.pow on every lane. Every other special case is finished per lane at first.
    3. What happens if pow's common path does not stay unboxed (step 2). Recommended: say so and ship pow as the special-y blends over a per-lane StrictMath.pow, which PLAN.md:92-103 allows, before cutting the body in ways that need proofs.
    4. The optional five (tan, tanh, cbrt, sin, cos; PLAN.md:43-46). Decide once pow is in. Not part of this issue's code.

    Steps. Each a pull request unless said otherwise.

    1. What the sources settle (no pull request).
      • hypot (e_hypot.c): the swap is by high words (:60), so a's high word is at least b's; a >= b itself is not guaranteed when the words are equal, and :94-95 handles either order. Writing the masked word back (:61-62) is abs. A ratio above 2^60 returns a+b (:63). Above 2^500 it handles the non-finite (:66-71) or scales down (:73-75); below 2^-500 it returns a for a zero b (:79), multiplies by 2^1022 (:80-84) or scales up (:85-91). The medium path is one of two radicands and a sqrt (:94-109), then a scale back when k != 0 (:110-114).
      • One quirk to port as written or leave to the finish: the subnormal branch scales a and b but not ha and hb (:80-84), and the splits below read ha and hb (:97, :103, :106).
      • pow (e_pow.c): the method is its own log2(x) in two pieces, y*log2(x) as n + y', and its own exp (:19-25). Early returns: y == 0 (:119), NaN (:122-124), special y (:147-164), special x (:168-180), a negative x with a non-integer y (:185), and |y| > 2^31 (:191-207), which either returns or takes a short log for x near 1. The main log is :208-256, with a scaling for a subnormal x (:212-213), a three-way interval by the significand (:218-220), and a reciprocal and a product that are the C's own (:225-226). Then y*log2(x) (:259-263), the overflow and underflow returns (:264-278), n and the reduced part (:282-293), the exp tail with the second division (:294-303), and the scale back (:304-307) times the sign s (:187-188, :308).
      • pow calls neither exp nor log, so it nests no other port; PLAN.md:38 still says it builds on exp.
    2. Port hypot from fdlibm's e_hypot.c. Needs Port atan, then atan2, asin and acos; atan2 brings the first two-argument nightly #33's harness.
      • Hypot.java. Both high words in int lanes; the swap by hb > ha; a and b by blend and abs.
      • In the vector: the medium path, with both radicands computed and blended by w > b and one sqrt; and a+b where (ha-hb) > 0x3c00000, since hypot(x, 0) for an ordinary x takes that return. The cleared low words (:96-97, :102-103) are an AND of the bits, and :105-106 is the same word plus one exponent step.
      • Rare lanes, finished per lane: ha > 0x5f300000 or hb < 0x20b00000 where the ratio test did not return, and every non-finite lane at first, until the oracle's bits for a+b on NaN inputs are checked (SKILLS.md:338).
      • Arms: finish first. A guarded or blended arm is possible here, since every rare branch is a few vector operations; it must reproduce StrictMath at subnormal b, where the quirk above decides the bits.
      • HypotTest on Oracle.assertBinary and assertBinaryMasked: Inputs.cross of the specials, a near 2b on both sides of w > b, equal high words with either low word larger, ratios across 2^60, the 2^500 and 2^-500 words, zero and subnormal b, every subset of lanes rare, and cHypot. Wiring, benchmark and results file as Port sinh and cosh from e_sinh.c and e_cosh.c, over exp and expm1 #32's plan lists them.
    3. Prototype pow's common path before any test (no pull request). PowBenchmark rows for the body of step 3; bench/alloc-check.py at the host's flags and as AVX2; -XX:+PrintInlining and PrintIntrinsics. The body has two divisions (:225, :302), two polynomials, six low-word clears, int-lane shifts by a per-lane count (:286-290) and an I2D (:252). Expm1 already shifts int lanes by k and passes the check under -XX:UseAVX=2 (Expm1.java:263-266, SKILLS.md:434); nothing of pow's size has been tried. If it boxes, decision 3 applies.
    4. Port pow from fdlibm's e_pow.c.
      • Pow.java, one body. Common lanes: x positive, finite and normal, not exactly 1; y finite, nonzero, |y| <= 2^31, none of decision 2's values; and the result neither out of range nor subnormal.
      • The rare mask has an early part, from the arguments (:119-185, :191, :212), and a late part, from z's words: :266 and :272 taken as a whole, and (j>>20) <= 0 at :306. Marking more lanes rare than the C returns early is safe: they are finished by StrictMath.pow.
      • On a common lane x is not negative, so n of :182 is 1 and s is one; s*z at :308 is then z by (R3) (PROOFS.md:38), or is kept as a multiply.
      • Decision 2's blends apply only where the C reaches their lines: y == 0 for any x, since :119 comes first; y of 1, -1 and 2 only for an x that is not NaN, since :122-124 comes before them; and 0.5 only for x >= +0 (:161). A NaN lane is rare and is rewritten by the finish, which runs last.
      • Per-lane choices inside the path, each by a mask and a blend: bp[k], dp_h[k], dp_l[k] and the n += 1 of :218-220; the |z| > 0.5 block of :285-293, whose shifts take garbage counts on the lanes that skip it and are not kept there; the sign at :291.
      • The guard's proof, a numbered lemma in PROOFS.md: for every lane outside the rare mask, (i) none of the returns of :119-198 is taken, (ii) :208's branch is taken and :212 is not, (iii) neither :266-278 nor :306 applies, and (iv) s is one. Its test is every subset of lanes rare, at each boundary of the mask, with a fault that drops one test.
      • PowTest: Inputs.cross of the specials, as HarnessSelfTest already does for its stand-in (HarnessSelfTest.java:108-115); integer and half-integer y; both sides of the 0x3988E and 0xBB67A splits; y*log2(x) near 1024, -1075 and the subnormal boundary; |z| at 0.5; a vector where every y is 2; masked; cPow.
      • PowBenchmark: common, mixed with each rare kind, and rows with a constant exponent of 2 and of 0.5. The allocation check asks for inputs=common,mixed itself (bench/alloc-check.py:59-60), so a third value would not disturb it, but every ordinary run would take its rows; separate methods are simpler.
    5. Widen pow by measurement. One pull request each, in the order the benchmark and Varka's data ask for: a negative x with an integer y (yisint, :131-144, and s); a subnormal x (:212-213); the overflow and underflow constants blended (:193-198, :268-276); subnormal results in the vector, after decision 1's s_scalbn.c.
    6. Close the list. PLAN.md section 3 (:28-46) says all thirteen are ported, README.md:16 lists them, and decision 4 is recorded.

    Planted faults, sized before they are wired (SKILLS.md:391, :426). Candidates, none measured:

    • hypot has no floating constants. Candidates: t1*y2 dropped from :108; the low-word clear one bit short; w > b as w >= b, which can show only at a == 2b. Not the 0x3c00000 word: on both sides of it the result appears to be a (inferred, not run).
    • pow: t2 dropped from :261; cp_h or lg2_h one ulp off (:94, :90); the 0x3988E word moved, where both sides compute a valid log; and a guard that drops the :306 test, so a subnormal result keeps the exponent add.

    Order and dependencies.

    Risks and unknowns.

    • pow's size. It is the largest body by far, and Varka's plan calls it "the one most likely to meet the method budget" (VARKA-356.md 2.6). Step 2 settles C2's side here; Varka's side is Pair the five ports with Varka: no port has its emitted kernel or cross-check yet #31's.
    • A constant exponent. If every lane is special, the main path is computed and thrown away. Measure the constant-exponent rows; an allTrue test that skips the main path is the fix if they show it.
    • NaN bits. x+y (:124), y - y (:150), (z-z)/(z-z) (:174) and (x-x)/(x-x) (:185) are all on rare lanes in the first version. Check the oracle's bits before step 4 blends any.
    • Hard cases. The existing files' sources are tables for one-argument functions (PLAN.md:57-64). Nothing for pow, hypot or atan2 is known here; each pull request says what its file holds, the nightly's finds included.
    • hypot's quirk. Whether StrictMath.hypot follows :80-84 as written is the oracle's to say; the finish arm does not depend on it.

    Done when. hypot and pow each meet #32's "done when" (steps 1 and 3); pow's guard has its lemma and test (step 3); PLAN.md section 3 lists the thirteen as done and the optional set has its answer (step 5).

  2. MaxGekk commented on Nov 20, 2026

    @MaxGekk
    ContributorAuthor

    Scheduling note, 20 November 2026: Varka's milestone 8 now schedules the eight ports vfdlibm does not hold yet (pow, asin, acos, atan, atan2, sinh, cosh, hypot) in its floating track (section 13 of its m8/PLAN.md, in review as vecbricks/varka#1585). The owner asked for it on that day. Each port is written here and as Varka's IR expansion, and a port arm opens its vfdlibm pull request in the same sitting. The order of the rest is set by what a port builds on and by its measurement; a port is not ranked by census pairs, since it moves none.

  3. MaxGekk commented on Nov 20, 2026

    @MaxGekk
    ContributorAuthor

    Varka side: vecbricks/varka#1216 (row 358, the exponential, logarithmic, power and root functions: pow, hypot) and vecbricks/varka#1210 (row 345). The port arms of those functions pair with this issue.

  4. MaxGekk commented on Nov 21, 2026

    @MaxGekk
    ContributorAuthor

    Design notes for hypot and pow (21 November 2026; read only, no code)

    These add to the plan of 20 November ("the plan") and point to it where it already says the thing. C lines are reference/fdlibm-5.3/e_hypot.c and e_pow.c. The only thing run was a scratch transcription of e_hypot.c (not committed). On #33: its newest design is still the plan of 20 November (checked 21 November), so "as on #33" below means that plan's step 2 and decisions 2 and 4.

    1. hypot. Every finite lane is vector work, each lane doing the C's own operations; the only per-lane finish is the non-finite one.

    • Swap :60: by high words only, so a, b are blends of x, y where hb > ha, and :61-62 is ABS. With equal high words nothing swaps and b may be the larger; keep hb > ha, not a >= b.
    • Early a + b :63, for ha - hb > 0x3c00000 (int subtraction, never negative after the swap): a blend. It is not optional: without it, 767,778 of 1.86 million draws with b below a * 2^-70 differ from StrictMath.
    • The three scalings are exclusive (a scaled-down b is at least 2^-160): ha > 0x5f300000 (k = 600), hb in [0x00100000, 0x20b00000) (k = -600), hb <= 0x000fffff (times 2^1022, k = -1022; b == 0 returns a). In the vector: one int add of 0 or +-0x25800000 to both words, one multiplied copy blended in, and 2^k built from bits ((0x3ff + k) << 20) << 32, multiplied always (k = 0 is 1.0 * w, exact by (R3)).
    • The stale words: after the 2^1022 scaling :80-84 leaves ha and hb as they were, and the splits :97, :103, :106 read them. Measured: as written the transcription equals StrictMath on 2,000,000 subnormal-b draws; with ha and hb refreshed it differs on 141,232. The port keeps the stale words. This is a "counter-example, not a fold" entry for PROOFS.md (as L4 and L12), whose test shows the refreshed variant failing.
    • sqrt: allowed (README.md:162, PLAN.md:38-39), and no port uses it yet, so whichever of hypot and asin lands first checks PrintIntrinsics at 128, 256, 512 and SPECIES_MAX and adds sqrt to (R4). The C has two calls, one in each side of w > b; the port computes both radicands, blends by w > b and takes one SQRT, so each lane takes the root of its own radicand.
    • Per lane, by StrictMath.hypot: lanes with ha >= 0x7ff00000, and lanes that return a + b early with a non-finite operand, until NaN bits are compared with the oracle (SKILLS.md:338). :66-71 makes hypot(inf, NaN) an infinity. Body about 70 to 90 lines (Expm1's is 92), no nesting, so no cutoff worry; arms finish, and a blended one only if measured.

    2. pow. Branch layout: y == 0 :119; NaN :122; yisint, for x < 0 only, :132-144; with ly == 0: y = +-inf :148-154, +-1 :156, 2 :159, 0.5 for x >= +0 only :160-162; x in {+-0, +-inf, +-1} with lx == 0 :168-180; x < 0 and y not an integer, NaN :185; s :188; |y| > 2^31 :191-207; the log :208-256 (subnormal x :212, three intervals :218-220); y * log2(x) :259-263; overflow and underflow :266-278; the exp :282-303; the scale back :304-308.

    • Common path: x > 0, finite, normal, not 1; y finite, not 0, |y| <= 2^31, not one of the special values; z strictly inside (-1075, 1024); a normal result. Then s is one. The special y are blends on the full bits of y (ly == 0 is part of the test), as in the plan's decision 2. Everything else is finished by StrictMath.pow at first; the plan's step 4 widens.
    • scalbn: take the plan's decision 1. Subnormal results (:306) are rare lanes and are finished, so s_scalbn.c is not needed until a widening wants them in the vector.
    • Size. The main computation is about 100 C lines (:208-308); one body with the blends and the rare mask is 190 to 240 lines. The largest private bodies now are Log 116, Log1p 112, Exp 94, Expm1 92, Cosh 86 (signature to end, comments included). Log10, which is log's body plus about 25 lines, hit the cutoff at a late helper (SKILLS.md, "What the log10 port taught"). The cutoff refuses non-ForceInline callees only, so one body of Vector API calls and written-out int operations can pass, but there is no useful split into two methods: the cut after t1, t2 (:253-255) passes two vectors into a late call, which boxes (SKILLS.md, "Two port bodies in one loop do not fit"). So one body, prototyped first as the plan's step 2 says, and the plan's decision 3 if it fails. C2's overall node limit is a second risk, not measured.
    • Lemmas. Exp's and Log's do not apply to pow's tail: its reduction is by exponent-field arithmetic (:282-293), not invln2 * x, and its tail form differs. By shape, L3 covers the word compares (ix, iy, j), L7 the (double)n (:252), L8 the word arithmetic (:231, :286-290) if done in long lanes. Pow's own: (P1) the guard (outside the rare mask no return of :119-198, none of :266-278, :208's branch, s = one); (P2) the blend order equals the C's return order (y == 0 before NaN before 1, 2, 0.5); (P3) s * z is z for s = one (R3) and (j >> 20) <= 0 is one compare of bits. bp[k], dp_h[k], dp_l[k] are blends of the C's own operands, so no lemma; adding dp_h[0] = +0.0 is not skipped, since -0 + +0 is +0.

    3. Binary functions in the nightly. As on #33 (harness pull request first: Arm2, pair mismatch lines, Sweep --argument 1|2, pair fuzz draws). hypot and pow add generators of related pairs. hypot: ha - hb either side of 0x3c00000, a near 2^500 and b near 2^-500, a near 2b, equal high words with either low word larger, subnormal b. pow: y at +-1, 2, 0.5, and by 2^31 and 2^64; integer and half-integer y with x < 0; x at the 0x3988E and 0xBB67A splits; y = target / log2(x) for a target z near 1024, -1075 and 0.5. hypot is symmetric in its arguments apart from the tie at equal high words, so sweeping both arguments covers both roles. pow's cost per input is above any unary function's: measure it before #35's budget is set.

    4. Hard cases. Lefevre's lines are <mode> <tag> <x> <result> <direction> with one x (dev/testlibm-extract.py:15), so a pair function has no place in them, and the script names three tags (sh, ch, em1) and says only that others exist. I could not check offline whether any tag is for hypot or pow. Proposed: hard-cases/hypot.txt and pow.txt as pair files, lines 0x<x> 0x<y> (#33's Inputs.hardCasePairs), computed here with exact arithmetic so there is no licence question: (a) exact results, integer ** integer up to 2^53 (fdlibm promises them, e_pow.c:49-52) and Pythagorean triples scaled by powers of 4 across the 2^+-600 and 2^1022 scalings; (b) near-midpoint cases found by search, easy for hypot (integer arithmetic) and for pow with small integer and half-integer y; (c) the nightly's mismatches. The header says which section is which.

    5. Faults, proofs, benchmark. Candidates besides the plan's, each sized before it is wired: hypot refreshes ha and hb after the 2^1022 scaling (7% of subnormal-b draws, so well sized); the early-return word moved by one (to measure). pow: a guard that lets a NaN lane keep the y == 2 blend; a guard that lets x < 0 through (wrong s). Proofs: P1 to P3 and the stale-word entry above. Benchmarks: common as Varka's baseline draws (section 7), constant-y rows for 2 and 0.5, mixed with each rare kind; bench/alloc-check.py at host flags and as AVX2.

    6. Order and size (compared with Cosh.java, 269 lines, and Expm1Test.java, 491). (1) #33's harness pull request. (2) hypot: Hypot.java about 200, test about 250, benchmark about 100, PROOFS.md about 60, data files: 700 to 900 lines. (3) pow prototype, no pull request. (4) pow: Pow.java about 500, test about 500, PROOFS.md about 150, data: 1200 to 1500. (5) widenings, 150 to 300 each, yisint first (section 7). (6) PLAN.md section 3.

    7. Varka's and Spark's use (vecbricks-varka, master, 21 November; ranges are stated in the files, not measured data).

    • Varka's baseline, VarkaMathBaselineBenchmark.java:109-113, 160, 163: pow(p, y) with p in [1e-6, 1e6) and y in [-10, 10), results in [1e-60, 1e60]; hypot(x, y) with x, y in [-10, 10). All lanes of both are on the common paths; no y is 2 or 0.5 by chance, and no x is negative for pow.
    • Spark tests: MathExpressionsSuite.scala:592-594 pow over (-5..5, same), so negative x with integer y, zeros, and NaN for (-1, 0.9), (-2.2, 1.7), (-2.2, -1.7); :711-712 hypot over -2 to 2 step 0.1 and its negation; MathFunctionsSuite.scala:515-524, 552-553 pow(a, 2.0) and pow(a, -0.5) and the same for hypot. A constant exponent 2.0 makes every lane special (x * x); -0.5 is not special in fdlibm.
    • sql-tests/inputs/postgreSQL/float8.sql:129-137: power(144, 0.5) (the sqrt case), NaN on either side, power(1, NaN) (fdlibm gives NaN, :122; SPARK-28135) and power(NaN, 0) (1, :119). Docs: pow(2, 3), hypot(3, 4).
    • Not found: which columns users give pow or hypot; whether a constant exponent is common outside tests (no sql-tests input other than float8.sql uses pow, power or hypot); Connect's plan test (PlanGenerationTestSuite.scala:1593) runs nothing.
    • It implies: the special y 2 and 0.5 stay vector blends; negative x with integer y is wanted by Spark's tests but not by the baseline, so yisint comes first among the widenings; NaN, infinity and zero are test inputs, so finishing them per lane costs nothing in the baseline.

    Questions for the owner

    1. Does Varka emit pow with a constant exponent, so that 2 and 0.5 could be chosen when the kernel is built instead of blended at run time?
    2. Is it right to finish the non-finite hypot lanes per lane first, and may the first sqrt port add sqrt to (R4)?
    3. Are self-computed exact and searched cases acceptable as the hard-case files for hypot and pow, with no published source known?
    4. If pow's prototype does not stay unboxed, ship decision 3's fallback (special-y blends over per-lane StrictMath.pow), or hold pow?
    5. For pow's pair sweep, every night (as Port atan, then atan2, asin and acos; atan2 brings the first two-argument nightly #33's recommendation) or alternating nights if its cost is high?
  5. MaxGekk commented on Nov 23, 2026

    @MaxGekk
    ContributorAuthor

    Question 3 answered for hypot (23 November 2026)

    The owner accepts self-computed hard cases for hypot: hard-cases/hypot.txt, written by dev/hypot-hard-cases.py with exact integer arithmetic (near-midpoint pairs for both forms of the final step and exact Pythagorean triples, scaled into every range), as #143 adds them.
    HardCasePairsTest rechecks every pair's property with exact BigDecimal arithmetic, independently of the script, and the script reproduces the file byte for byte.
    No published double-precision table of hypot's hard cases was found (Zimmermann's correct-rounding cases folded into LLVM libc are binary32); a follow-up issue checks LLVM libc's newer hypot worst cases.
    This answers the question for hypot only; pow gets its own answer when its port comes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions