Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ contract of every version is the same: each lane of each function equals
any species of 128 bits or more. Every lane is bit-identical to `StrictMath.atan`, NaN payloads
and signed zeros included. Lanes of magnitude `2^66` or more, the infinities and NaNs are
finished per lane by `StrictMath.atan`.
- `VFdlibm.acos`, in the two forms `acos(x)` and `acos(x, live)`, over `DoubleVector` lanes of
any species of 128 bits or more. Every lane is bit-identical to `StrictMath.acos`, NaN payloads
and signed zeros included. Lanes of magnitude 1 or more and NaNs are finished per lane by
`StrictMath.acos`.

### Changed

Expand Down
40 changes: 20 additions & 20 deletions PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ and expm1's `k == 0` lines, with two arms for its small range: the default arm i
provisionally, until a benchmark host times them). `asin` is done too (`VFdlibm.asin`, from
`e_asin.c`, with `sqrt` as the Vector API's `SQRT`, one arm, and the lanes of `|x| >= 1` finished
per lane), and so is `atan` (`VFdlibm.atan`, from `s_atan.c`, five ranges through one division on
blended operands, the lanes from `2^66` up finished per lane). `log`, `log10` and
blended operands, the lanes from `2^66` up finished per lane), and so is `acos`
(`VFdlibm.acos`, from `e_acos.c`, built as asin is). `log`, `log10` and
`log1p` are done too (`VFdlibm.log`, from `e_log.c`, `VFdlibm.log10`, from `e_log10.c`, which
computes through log's common path, and `VFdlibm.log1p`, from `s_log1p.c`), and so is `expm1`
(`VFdlibm.expm1`, from `s_expm1.c`, whose reduction is exp's).
Expand All @@ -67,11 +68,11 @@ five are a later, optional set; the large-argument reduction of the trigonometri
- published hard cases: inputs where the exact result lies very close to a rounding boundary, from
Lefevre and Muller's worst-case tables (ARITH 15, 2001) and Lefevre's list of round-to-nearest
hard cases, one resource per function under `src/test/resources/.../hard-cases/` (so far `exp`,
`expm1`, `log`, `log10`, `log1p`, `sinh`, `cosh`, `asin` and `atan`), loaded with
`expm1`, `log`, `log10`, `log1p`, `sinh`, `cosh`, `asin`, `atan` and `acos`), loaded with
`Inputs.hardCases("<function>")`. The inputs of Lefevre's list come out of
`dev/testlibm-extract.py`, which rechecks each line with 400-digit arithmetic against exact
values it derives itself (`sinh`'s, `cosh`'s and `atan`'s files were written by it; it also
derives `asin` and `acos`, whose files come with their ports). A port that keeps fdlibm's
values it derives itself (`sinh`'s, `cosh`'s, `atan`'s, `asin`'s and `acos`'s files were
written by it). A port that keeps fdlibm's
operations, or departs from them only where `PROOFS.md` proves the result equal, matches
`StrictMath` on them by construction; they sit where a dropped correction term or a
reassociated sum, an error far below one ulp, is most likely to change the returned double.
Expand All @@ -88,13 +89,13 @@ five are a later, optional set; the large-argument reduction of the trigonometri
-XX:MaxVectorSize=32`, 128 and 256 bits on every draw), on x86 with `-XX:MaxVectorSize=16`
(all of it at 128 bits) and on Arm (128 bits).
- **Nightly:** `.github/workflows/nightly.yml` sweeps every high word of exp, expm1, log, log10,
log1p, sinh, cosh and atan (asin is in `Nightly`'s table and not yet in the night's list) with
three low words each (every branch fdlibm decides on the high word) at every width but 128 bits
on x86, where a rotating 1/32 slice a night covers it in 32 nights, and at 128 bits on Arm every
night; it fuzzes from a fresh seed at every width, and runs every proof under both solvers; a
failure opens an issue labelled `nightly-failure` with the mismatching inputs, ready for a
`hard-cases` file (`SKILLS.md`), and a dispatch that plants a fault, to test that alert, opens
one labelled `nightly-fault-test`.
log1p, sinh, cosh and atan (asin and acos are in `Nightly`'s table and not yet in the night's
list) with three low words each (every branch fdlibm decides on the high word) at every width but
128 bits on x86, where a rotating 1/32 slice a night covers it in 32 nights, and at 128 bits on
Arm every night; it fuzzes from a fresh seed at every width, and runs every proof under both
solvers; a failure opens an issue labelled `nightly-failure` with the mismatching inputs, ready
for a `hard-cases` file (`SKILLS.md`), and a dispatch that plants a fault, to test that alert,
opens one labelled `nightly-fault-test`.
- **Masked forms:** a masked form is checked under seeded random masks, all-true and all-false;
active lanes must equal `StrictMath` and inactive lanes must keep the input's raw bits (for a
two-argument function, the first argument's).
Expand Down Expand Up @@ -159,16 +160,15 @@ five are a later, optional set; the large-argument reduction of the trigonometri
and 512 bits, the preferred species and `SPECIES_MAX`. `Log1pBenchmark` does the same for `log1p`,
with a third arm that blends the rare paths only when a vector holds a rare lane; `Expm1Benchmark`
has the same three arms for `expm1`, `SinhBenchmark` one for `sinh`, `CoshBenchmark` two for
`cosh`, `AsinBenchmark` one for `asin` and `AtanBenchmark` one for `atan`. Each of the nine has
rows at `SPECIES_MAX` (`*Max`), which the allocation check covers: the finish of `exp`,
`expm1`, `log`, `log10` and `log1p` boxed there on mixed data until its first eight lanes were
written out
(`bench/results/max-finish-laptop-2026-11-20.txt`, then
`cosh`, `AsinBenchmark` one for `asin`, `AtanBenchmark` one for `atan` and `AcosBenchmark` one for
`acos`. Each of the ten has rows at `SPECIES_MAX` (`*Max`), which the allocation check covers: the
finish of `exp`, `expm1`, `log`, `log10` and `log1p` boxed there on mixed data until its first
eight lanes were written out (`bench/results/max-finish-laptop-2026-11-20.txt`, then
`bench/results/max-finish-alloc-free-laptop-2026-11-21.txt`, a laptop reading of allocation only);
no file reads the earlier finish of `sinh` or `cosh`, and none has an `asin` or an `atan` row,
whose finishes were written in the new shape from the start. `ExpKindsBench` runs exp's two arms
on one kind of rare input at a time (a NaN, an overflow, an underflow, a subnormal result), to
find which kind costs an arm its time on mixed data (#41); it is named apart so that a run of
no file reads the earlier finish of `sinh` or `cosh`, and none has an `asin`, `atan` or `acos`
row, whose finishes were written in the new shape from the start. `ExpKindsBench` runs exp's two
arms on one kind of rare input at a time (a NaN, an overflow, an underflow, a subnormal result),
to find which kind costs an arm its time on mixed data (#41); it is named apart so that a run of
`ExpBenchmark` and the allocation check's default regex leave it out, as `CoshKindsBench`, which
has cosh's small-range shares, is. A function's body is larger than C2 inlines by default, so a
call from a loop passes boxed vectors; the benchmarks are also run with
Expand Down
59 changes: 59 additions & 0 deletions PROOFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1181,3 +1181,62 @@ way: moved by one word, the lanes of the word between give the same bits (0 of 2
changed, and the test pins that), so no test of results sees those two move, and they are not
checked. That a move by more than one word is seen was not measured, except for the
`0x3ff30000` one.

## acos (`Acos.java`, from `e_acos.c`)

e_acos.c reads `hx = __HI(x)` and `ix = hx&0x7fffffff` (lines 69-70). For `ix >= 0x3ff00000` it
returns `0.0` for exactly `1`, `pi+2.0*pio2_lo` for exactly `-1` and `(x-x)/(x-x)` above (71-76).
Below that, for `|x| < 0.5` (`ix < 0x3fe00000`, line 78) it returns `pio2_hi+pio2_lo` for `ix <=
0x3c600000` (79) and otherwise `pio2_hi - (x - (pio2_lo-x*r))` with `z = x*x`, `r = p/q` (80-84);
for `hx < 0` (`x <= -0.5`: the C's comment says `x < -0.5`, but `-0.5` takes this branch, line
85) with `z = (one+x)*0.5`, `s = sqrt(z)` and `r = p/q` it returns `pi - 2.0*(s+w)`, `w =
r*s-pio2_lo` (86-92); and otherwise (94-103, `x >= 0.5`: the C's comment says
`x > 0.5`, but `+0.5` takes this branch) with `z =
(one-x)*0.5`, `s = sqrt(z)`, `df` the root with its low word cleared and `c = (z-df*df)/(s+df)`
it returns `2.0*(df+w)`, `w = r*s+c`. The port computes the lines below `|x| = 1` in the C's
order, with no fused multiply-add and no reassociation, and **needs no lemma of its own** beyond
L13's blend argument and (R4)'s `sqrt`; the asin section says the same of its own blends,
and what it says of the low-word clear, of the shared polynomials and of the rare lanes holds
here for the same reasons. Where acos's text is not the C's:

- `__LO(df) = 0` (lines 96-97) is the AND of the lane's 64 bits with `0xFFFFFFFF00000000`, the
C's own operation on the bits, exact, as in asin's `__LO(w) = 0`: nothing to prove, the claim is
that the port clears the word the C clears. `acos.skipLowClear` is the fault that omits it.
- The three ranges below 1 share one pass: `z` is `x*x`, `(one+x)*0.5` or `(one-x)*0.5` by the
C's compares, and `p`, `q` and `r = p/q` are then the same operations in all three (lines
81-83, 87-88 and 90, 99-101), on `z`; `s = sqrt(z)` is `VectorOperators.SQRT`. The second
division, `(z-df*df)/(s+df)`, is computed on every lane and kept by the lanes of `x >= 0.5`.
- `pio2_hi+pio2_lo` (line 79) is the one constant of the port's source, `PIO2_HI + PIO2_LO`: a
Java constant expression of two doubles is evaluated as IEEE 754 addition, the one rounding the
C's run-time sum has (R4).
- The lanes of `|x| >= 1`, the infinities and the NaNs, which the C answers with its first
branch, are finished per lane by `StrictMath.acos`, which is the C: no computation of the port
is in them.

**L13 for acos.** H1 (selection) holds: every mask is the C's compare on the same `ix` or `hx` (`ix
< 0x3ff00000`, `ix < 0x3fe00000`, `ix <= 0x3c600000`, `hx < 0`). The `<=` of the constant return is
the C's text. A scalar script run once, not committed, found the first form's result equal to the
constant's in 2000 random draws of each binade from `2^-80` to `2^-56`, and different from `2^-55`
up (213 of 2000 there); that is all the evidence, and no test of the port tells the compare from `<`
or from a neighbouring word. `acos.tinyMoved` moves the word to `0x3cd00000`, far enough to show. H2
(operands) holds for the blend of `z`, the one operand the three ranges give the polynomials, and
for the sums, which are the C's in its order. H3 (totality) holds: every operation is an IEEE
operation on doubles, a compare, a bit operation or a reinterpretation, with no integer division and
no indexed load, so a discarded lane's `sqrt` of a negative `z` or a `NaN` is a value and nothing
else. H4 (select) holds: each blend is a `blend`. Not covered: that H1's words are the C's is
`AcosTest`'s (`constantsAreTheSourcesBits`, and the thresholds and their neighbours), which catches
`acos.halfMoved` (moved to `0x3fe80000`) and `acos.tinyMoved` (moved to `0x3cd00000`) and has no
fault for a word moved by one (for the constant return's word, see above); the port's code having
L13's shape is `AcosTest`'s tie and not L13's tests, which run a model; and (R4)'s `sqrt` is the
operator's, tested by `SqrtTest`, with the C's own `sqrt` not shown there.

**Hinge tests.** The vector tie is `AcosTest`: `VFdlibm.acos` against `StrictMath.acos` at every
width and `SPECIES_MAX`, unmasked and masked, on the thresholds of lines 71, 78 and 79 and their
neighbours, sampled bit patterns, dense draws of `(-1, 1)`, of `(-0.6, -0.4)`, `(0.4, 0.6)`,
`(0.95, 1)` and `(-1, -0.95)`, the magnitudes from `2^-70` to `1`, the published hard cases (1569
inputs, all positive) and their negations, which reach the branch of `x < -0.5`, and every subset
of a vector's lanes rare (`everySubsetOfLanesRare`); `scalarTranscriptionIsStrictMath` checks that
`cAcos`, e_acos.c in scalar Java, is `StrictMath.acos`. L13's own tests are `ProofsTest.l13*`.

**Encoding.** No file of its own: nothing here is a fold. Informal here: that the port's blends
take each branch on exactly the C's lanes, and that it computes these steps, the code's text.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ java --module-path "target/classes:$(cat cp.txt)" -m example/example.Main
```

Each function takes a vector of any species of 128 bits or more. Released in 0.1.0: `exp`,
`expm1`, `log`, `log10` and `log1p`. On `main`, unreleased: `sinh`, `cosh`, `asin` and `atan`.
The system property `vfdlibm.fault` plants a fault, for this project's tests only, and works in
the shipped jar too: never set it, including through `JAVA_TOOL_OPTIONS`; a JVM that has it set
prints a warning to `System.err` once. The property and the fault names are for this
`expm1`, `log`, `log10` and `log1p`. On `main`, unreleased: `sinh`, `cosh`, `asin`, `atan` and
`acos`. The system property `vfdlibm.fault` plants a fault, for this project's tests only, and
works in the shipped jar too: never set it, including through `JAVA_TOOL_OPTIONS`; a JVM that has
it set prints a warning to `System.err` once. The property and the fault names are for this
project's tests only: they are not part of the public API or of the compatibility promise, and
any release, a patch included, may rename or remove them. With the property unset, the results
are unchanged, and each lane still equals `StrictMath`'s.
Expand Down
32 changes: 29 additions & 3 deletions SKILLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Lessons about benchmarking, the JIT and the Vector API that every port follows. Most were learned in
Varka (github.com/vecbricks/varka, its `sql/varka/skills/`) and are rewritten here for a vector math
library; the last sections are what the `exp`, `log`, `log1p`, `log10`, `expm1`, `sinh`, `cosh`,
`asin` and `atan` ports taught. Each entry says where its numbers come from. A new lesson goes in
the section it belongs to, as a short entry; a negative result belongs here too, since it stops the
next port re-trying a settled question.
`asin`, `atan` and `acos` ports taught. Each entry says where its numbers come from. A new lesson
goes in the section it belongs to, as a short entry; a negative result belongs here too, since it
stops the next port re-trying a settled question.

## Benchmarking with JMH

Expand Down Expand Up @@ -1023,3 +1023,29 @@ next port re-trying a settled question.
- **Hard cases: 1655 inputs**, from `dev/testlibm-extract.py asn asin` (the tag `asn` is asin's;
the extractor's recheck passes on all 1655 lines). All are positive, between 6.5e-8 and 1, and
the one at 1 is a rare lane.

## What the acos port taught

- **The sibling of asin, with its own branches.** acos splits `|x| < 0.5` from `x <= -0.5`
(`hx < 0`) and `x >= 0.5`, so one pass computes `z` for the three, the polynomials and `r = p/q`
once, and the blends keep each lane's form; the `x >= 0.5` form divides a second time, for `c`.
The sign decides two forms above 0.5, so the hard cases (all positive, 1569 of them) reach the
`x <= -0.5` form only by their negations, which `AcosTest` draws. One arm: no tie to break.
- **The C's comments are not its tests.** The comment says `x < -0.5` and `x > 0.5` where the
tests send `-0.5` to the `hx < 0` form and `+0.5` to the last one; the port and `AcosTest.cAcos`
use the tests, and the thresholds include both `+-0.5`.
- **Which of the four planted faults changes what** (sized on 2^22 draws, a throwaway program over
`VFdlibm.acos`, run once per fault; the counts are of 4194304 draws each).
`acos.dropPio2Lo` changed 842961 of `[-1, 1]` and none of `[0.5, 1)`, where the form has no
`pio2_lo`; `acos.halfMoved` (the split moved to `0x3fe80000`) 954010 of `[-1, 1]` and none of
`(-0.5, 0.5)`; `acos.skipLowClear` 140899 of `[-1, 1]` (564449 of `[0.5, 1)`, none of
`[-1, -0.5)`, whose form has no low-word clear); `acos.tinyMoved` (the constant return's word
moved to `0x3cd00000`) none of `[-1, 1]` and 935119 of the magnitudes `2^-60` to `2^-50`, which
is why the fault run's inputs include those magnitudes. A word moved by one is not planted.
- **Hard cases: 1569 inputs**, from `dev/testlibm-extract.py acs acos` (the extractor's recheck
passes on all of them). 1265 of them have fewer than 40 bits after the round bit pointing at the
midpoint, so most are not hard to round; the file keeps every line. All are positive, between
0.0143 and 0.999999999999999.
- **Allocation.** CI runs `bench/alloc-check.py` on `AcosBenchmark`'s rows (`finish256`,
`finishPreferred`, `finishMax`, common and mixed data). No reading of them is committed and no
timing exists, so nothing is claimed of either.
1 change: 1 addition & 0 deletions dev/jar-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ public final class Consumer {
check("cosh", StrictMath::cosh);
check("asin", StrictMath::asin);
check("atan", StrictMath::atan);
check("acos", StrictMath::acos);
Module module = VFdlibm.class.getModule();
String in = module.isNamed() ? module.getName() : "";
System.out.printf("%d lanes at %d bits, %d wrong; the library is in %s%n", lanes,
Expand Down
14 changes: 7 additions & 7 deletions dev/nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# dev/nightly.sh sweep <function> <parts> <seed> [<species>...]
# dev/nightly.sh fuzz <function> <seconds> <seed> [<species>...]
#
# <function> is exp, expm1, log, log10, log1p, sinh, cosh, asin or atan. A sweep checks the high
# words of <parts>: comma-separated parts i/N of the 2^32, or stripe:s/S for the stripe Sweep.java's
# --stripe takes. A fuzz runs <seconds> at each species (Fuzz.java). <seed> is decimal or 0x
# hex. The species default to the host's: 128, 256 and 512 bits up to the preferred species, and
# max (`Nightly species`), less the one NIGHTLY_EXCEPT names (the nightly sweeps x86 at 128 bits,
# and Arm at max, by a separate slice); a width the host lacks gets a line in the output saying
# so. For example, a slice of exp at 256 bits:
# <function> is exp, expm1, log, log10, log1p, sinh, cosh, asin, atan or acos. A sweep checks the
# high words of <parts>: comma-separated parts i/N of the 2^32, or stripe:s/S for the stripe
# Sweep.java's --stripe takes. A fuzz runs <seconds> at each species (Fuzz.java). <seed> is decimal
# or 0x hex. The species default to the host's: 128, 256 and 512 bits up to the preferred species,
# and max (`Nightly species`), less the one NIGHTLY_EXCEPT names (the nightly sweeps x86 at 128
# bits, and Arm at max, by a separate slice); a width the host lacks gets a line in the output
# saying so. For example, a slice of exp at 256 bits:
#
# mvn -B -q test-compile && dev/nightly.sh sweep exp 1023/4096 0x1234 256
#
Expand Down
Loading