diff --git a/AGENTS.md b/AGENTS.md index 2ca4d28..6e0e73a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,7 +1,8 @@ # Working in vfdlibm, for agents and people alike vfdlibm ports fdlibm's double functions to the Java Vector API, each lane bit-identical to -`java.lang.StrictMath`. `PLAN.md` is the plan and the rules; read it before changing anything. +`java.lang.StrictMath`, but for a two-NaN lane of a function of two arguments ("Releases"). +`PLAN.md` is the plan and the rules; read it before changing anything. ## The rules that matter most @@ -9,22 +10,23 @@ vfdlibm ports fdlibm's double functions to the Java Vector API, each lane bit-id read OpenJDK's `FdLibm.java`, HotSpot's math stubs or glibc in order to write code here; they are GPL or LGPL. `StrictMath` is the oracle, called from tests, never a source to copy. - **Bit identity.** A function is done when every test compares its lanes with `StrictMath` by - `Double.doubleToRawLongBits` and passes, at every vector width the host has. -- **Same result, proven.** Every lane's result stays bit-identical to `StrictMath`. A port - follows fdlibm's C operation by operation, or computes differently where a written proof shows - the result equal for every input. The proof goes in `PROOFS.md` as a numbered lemma: its - hypotheses (the input domain, each hinge condition with the range where it holds and why the - lanes are in it), the IEEE property it rests on, the proof, and the test that checks its - hinges, which must fail if a hinge were false; the code carries a short Javadoc pointer to it. - A proof is informal (step by step, citing each IEEE property used) or formal: machine-checked, - or reduced to a finite check by a theorem, such as monotonicity of correctly rounded operations - plus evaluation at the boundary doubles. Machine-checked proofs follow the conventions of - Varka's `sql/varka/proofs/` (SMT-LIB over integers, a double as `M * 2^E` with a round-to-nearest - relation per binade, `unsat` checks as the proofs and `sat` checks for non-vacuity and for a - constant changed by one being refuted); here they live in `proofs/`, run by `dev/prove.sh` as - `proofs/README.md` describes. Fused multiply-add, reassociation and division by a - reciprocal stay out because they change results in general, so no proof covers them as a - rule; a specific use proven equal for every input would follow the proof rule like any other. + `Double.doubleToRawLongBits` and passes, at every vector width the host has. A function of two + arguments is compared by `PairBits`, which relaxes only a lane of two NaNs with different bits. +- **Same result, proven.** Every lane's result stays bit-identical to `StrictMath` (the two-NaN lane + aside, "Releases"). A port follows fdlibm's C operation by operation, or computes differently + where a written proof shows the result equal for every input. The proof goes in `PROOFS.md` as a + numbered lemma: its hypotheses (the input domain, each hinge condition with the range where it + holds and why the lanes are in it), the IEEE property it rests on, the proof, and the test that + checks its hinges, which must fail if a hinge were false; the code carries a short Javadoc pointer + to it. A proof is informal (step by step, citing each IEEE property used) or formal: + machine-checked, or reduced to a finite check by a theorem, such as monotonicity of correctly + rounded operations plus evaluation at the boundary doubles. Machine-checked proofs follow the + conventions of Varka's `sql/varka/proofs/` (SMT-LIB over integers, a double as `M * 2^E` with a + round-to-nearest relation per binade, `unsat` checks as the proofs and `sat` checks for + non-vacuity and for a constant changed by one being refuted); here they live in `proofs/`, run by + `dev/prove.sh` as `proofs/README.md` describes. Fused multiply-add, reassociation and division by + a reciprocal stay out because they change results in general, so no proof covers them as a rule; a + specific use proven equal for every input would follow the proof rule like any other. ## Build and test @@ -46,10 +48,10 @@ vfdlibm ports fdlibm's double functions to the Java Vector API, each lane bit-id and then one forked surefire execution per fault, running only the tests tagged `fault`. A new fault goes in `PlantedFaultTest.CATCHERS` with the check that must catch it, and gets an execution in `pom.xml`; `PlantedFaultTest` fails if either is missing. -- The harness is in `src/test/java`: `Oracle` compares lanes with `StrictMath` by raw bits, - `Inputs` makes the inputs, `VectorWidth` runs a test at 128, 256 and 512 bits and the preferred - species, and `PlantedFaultTest` requires each fault planted through `Faults` (in the main - sources) to be caught by its check. `HarnessSelfTest` tests them. +- The harness is in `src/test/java`: `Oracle` compares lanes with `StrictMath` by raw bits (by + `PairBits` for two arguments), `Inputs` makes the inputs, `VectorWidth` runs a test at 128, 256 + and 512 bits and the preferred species, and `PlantedFaultTest` requires each fault planted through + `Faults` (in the main sources) to be caught by its check. `HarnessSelfTest` tests them. - A width wider than the host's vectors is skipped. To see an AVX2 host's skips on an AVX-512 one: `mvn -B test -Dvfdlibm.test.jvmArgs=-XX:MaxVectorSize=32`. That keeps AVX-512's instructions at 256 bits, though: to make C2 compile as on an AVX2 host such as the EPYC @@ -84,7 +86,8 @@ vfdlibm ports fdlibm's double functions to the Java Vector API, each lane bit-id otherwise (`ExpKindsBench`), so CI leaves it out; check it by name and on its own inputs before reading its times (`--include` with `--inputs`; the command is in its Javadoc). Its `setUp` must throw on an `inputs` value it does not know, and a test must pin that, or a row - named for one kind could pass on another's data. + named for one kind could pass on another's data. A raw-operator row (`SqrtBenchmark`) may take + the `*Benchmark` name. ## Lessons @@ -173,18 +176,24 @@ vfdlibm is released to Maven Central as `io.github.vecbricks:vfdlibm`, the way v sees (the API, a behaviour, a speed worth quoting) adds one line under `Unreleased`, in Added, Changed or Fixed; a change to tests, docs or the build alone does not. -**The promise, while the version is 0.x.** A minor version may break the public API and a patch -may not. Adding a function is a patch. Changing an arm (how a function reaches its result: the -blend, the per-lane finish, the order of a branch) is a patch too, because every arm gives the -same bits. 0.1.x supports JDK 25 only (`README.md`, "Supported JDKs"). What no version may -change is the contract: **each lane of each function equals -`StrictMath`'s result on that input, by `Double.doubleToRawLongBits`**. A result that differs -from `StrictMath`'s is a bug and a fix, never a breaking change. The property `vfdlibm.fault` -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. A breaking -change has a changelog line that starts with `**Breaking:**` and says what a client must do, and -its pull request says the same. 1.0.0 promises that the surface stays; it is not scheduled. +**The promise, while the version is 0.x.** A minor version may break the public API and a patch may +not. Adding a function is a patch. Changing an arm (how a function reaches its result: the blend, +the per-lane finish, the order of a branch) is a patch too, because every arm gives the same bits +(on a two-NaN lane, one of the declared candidates). 0.1.x supports JDK 25 only (`README.md`, +"Supported JDKs"). What no version may change is the contract: **each lane of each function equals +`StrictMath`'s result on that input, by `Double.doubleToRawLongBits`**, NaN payloads included. One +exception: a lane of a function of two arguments where both arguments are NaN with different bits is +one of two candidates the function declares, the arguments' bits with the quiet bit set (`OPERANDS`: +`atan2`, `pow`) or the same with the sign cleared (`MAGNITUDES`: `hypot`), and which one is not +specified, since `StrictMath`'s own choice varies by JIT tier. Every other lane of such a function, +and every lane of a function of one argument (its NaN results do not vary by tier), keeps the +raw-bit rule. Outside that exception, a result that differs from `StrictMath`'s is a bug and a fix, +never a breaking change. The property `vfdlibm.fault` 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 meets that contract. A breaking change has a changelog line that starts with +`**Breaking:**` and says what a client must do, and its pull request says the same. 1.0.0 promises +that the surface stays; it is not scheduled. **The release is owner-run, from a tag.** `.github/workflows/release.yml` is a dry run only: it publishes nothing and holds no secret; it shows on GitHub's runners that a tag would build, sign diff --git a/CHANGELOG.md b/CHANGELOG.md index 82cb0fb..72e1a1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ break the API and a patch may not (`AGENTS.md`, "Releases"). A pull request that user sees adds its line under `Unreleased`; a change to tests, docs or the build alone does not. A line for a breaking change starts with `**Breaking:**` and says what a client must do. The contract of every version is the same: each lane of each function equals -`java.lang.StrictMath`'s result on that input, by `Double.doubleToRawLongBits`. +`java.lang.StrictMath`'s result on that input, by `Double.doubleToRawLongBits`, except a lane of +two NaNs with different bits in a function of two arguments (`AGENTS.md`, "Releases"). ## [Unreleased] @@ -40,6 +41,10 @@ contract of every version is the same: each lane of each function equals ### Changed +- The contract is stated for functions of two arguments (`atan2`; `pow` and `hypot` later): a lane + where both arguments are NaN with different bits is one of two candidates the function declares, + and no other lane differs from `StrictMath`'s bits. No result of a released function changes, and + a client does nothing. - The per-lane finish of `exp`, `expm1`, `log`, `log10`, `log1p`, `sinh` and `cosh` writes out its first eight lanes with constant indices. Before the change, at `DoubleVector.SPECIES_MAX` (up to eight lanes) the finish of `exp`, `expm1`, `log`, `log10` and `log1p` allocated 30,432 to 36,992 diff --git a/PLAN.md b/PLAN.md index 96523ad..9bafffb 100644 --- a/PLAN.md +++ b/PLAN.md @@ -3,7 +3,8 @@ ## 1. Goal Vector API versions of fdlibm's double functions, each lane bit-identical to -`java.lang.StrictMath`, released as a small library under `io.github.vecbricks`. +`java.lang.StrictMath` (a two-NaN lane of a function of two arguments aside), released as a small +library under `io.github.vecbricks`. ## 2. How it grows: one function at a time, with Varka @@ -15,7 +16,8 @@ function is ported here and in Varka's kernel together, and the two are cross-ch files are committed unchanged under `reference/fdlibm-5.3/`, and a port cites the one it follows. The port may compute differently from the C where a proof in `PROOFS.md` shows every result equal (section 6). -2. **Test it here** against `StrictMath`, lane by lane and bit for bit (section 4). +2. **Test it here** against `StrictMath`, lane by lane and bit for bit (section 4; + a two-NaN lane of a function of two arguments by `PairBits`). 3. **Build Varka's side.** Varka's kernel emits the same sequence of operations as its own IR nodes. A large function body is not inlined by C2, so a call into the library from a kernel would box the vectors, and Varka uses this library as its reference rather than calling it. @@ -136,14 +138,14 @@ five are a later, optional set; the large-argument reduction of the trigonometri copy from or translate OpenJDK's `FdLibm.java`, HotSpot's Intel LIBM stubs or glibc, which are GPL or LGPL. - The result must be the same; the implementation may differ. Every lane stays bit-identical to - `StrictMath`, and a port may compute differently from the C only where a written proof, formal - or informal, shows the result equal for every input. Informal: step by step, citing each IEEE - property used and each hinge condition, with the input range where it holds and why the lanes - are in it. Formal: machine-checked, following Varka's `sql/varka/proofs/` conventions, or - reduced to a finite check by a theorem (for example, monotonicity of correctly rounded - operations plus evaluation at the boundary doubles). Proofs live in `PROOFS.md` as numbered - lemmas (statement, hypotheses, proof, and the test that checks the hinges, which must fail if a - hinge were false), with a short Javadoc pointer at the code. + `StrictMath` (a two-NaN lane of a function of two arguments aside), and a port may compute + differently from the C only where a written proof, formal or informal, shows the result equal for + every input. Informal: step by step, citing each IEEE property used and each hinge condition, with + the input range where it holds and why the lanes are in it. Formal: machine-checked, following + Varka's `sql/varka/proofs/` conventions, or reduced to a finite check by a theorem (for example, + monotonicity of correctly rounded operations plus evaluation at the boundary doubles). Proofs live + in `PROOFS.md` as numbered lemmas (statement, hypotheses, proof, and the test that checks the + hinges, which must fail if a hinge were false), with a short Javadoc pointer at the code. - No fused multiply-add, no reassociation, no division by a reciprocal: they change results in general, so no proof covers them as a rule. A specific use proven equal for every input would follow the proof rule above like any other different computation. No `Math.*` call inside a @@ -186,10 +188,11 @@ released version, and the next function arrives as a patch or a minor. - **What it holds:** the five ports, `exp`, `expm1`, `log`, `log1p` and `log10`, each in the forms `f(x)` and `f(x, live)`, each lane bit-identical to `StrictMath`. -- **The contract:** bit identity with `StrictMath` for every `double` input, compared by raw - bits at every vector width of 128 bits or more. It is the one thing no version changes. How a - function reaches its bits (an arm) may change in a patch, since every arm gives the same - results; a result that differs from `StrictMath`'s is a bug, fixed in a patch. +- **The contract:** bit identity with `StrictMath` for every `double` input (a two-NaN lane of a + function of two arguments aside: `AGENTS.md`, "Releases"), compared by raw bits at every vector + width of 128 bits or more. It is the one thing no version changes. How a function reaches its bits + (an arm) may change in a patch, since every arm gives the same results; a result that differs from + `StrictMath`'s is a bug, fixed in a patch. - **The version promise (0.x):** a minor may break the public API, a patch may not; adding a function is a patch (`AGENTS.md`, "Releases"). - **What must hold on the commit that is tagged:** `mvn -B clean verify` with every test; the diff --git a/PROOFS.md b/PROOFS.md index feca73c..3e270a1 100644 --- a/PROOFS.md +++ b/PROOFS.md @@ -1,13 +1,14 @@ # Proofs: where a port computes differently from fdlibm's C -Every lane of every function here is bit-identical to `StrictMath`. A port follows the C operation -by operation, except where a lemma below proves that a different computation gives the same -double for every input (`PLAN.md` section 6). Each lemma names its hypotheses (the input domain +Every lane of every function here is bit-identical to `StrictMath`, but for a two-NaN lane of a +function of two arguments ("The contract of a function of two arguments"). A port follows the C +operation by operation, except where a lemma below proves that a different computation gives the +same double for every input (`PLAN.md` section 6). Each lemma names its hypotheses (the input domain and each hinge condition, with the range where it holds and why the lanes are in it), the IEEE 754 property it rests on, the proof, and the test that checks its hinges; that test fails if a hinge -were false. The code carries a pointer to the lemma. A port with no departure of its own -(log10, log1p) has a section saying so; log10's names the lemmas it rests on through `log`, -log1p's holds a counter-example, a statement and not a proof of a fold. +were false. The code carries a pointer to the lemma. A port with no departure of its own (log10, +log1p) has a section saying so; log10's names the lemmas it rests on through `log`, log1p's holds a +counter-example, a statement and not a proof of a fold. The tests are of two kinds, and each lemma names both. Hinge tests in `ProofsTest` mostly check a lemma's hypotheses on scalar Java, where each operation is the same single rounding as a vector diff --git a/README.md b/README.md index 562cf62..97edc47 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # vfdlibm fdlibm on the Java Vector API: math functions over `DoubleVector` lanes whose every lane is -bit-identical to `java.lang.StrictMath`. +bit-identical to `java.lang.StrictMath`, but for a two-NaN lane of `atan2` (unreleased). **Status: 0.x.** `exp`, `expm1`, `log`, `log1p` and `log10` are ported and released: `io.github.vecbricks:vfdlibm` 0.1.0 is on Maven Central. Functions are added one at a time, as [Varka](https://github.com/vecbricks/varka) needs them. A minor version may break the API and a patch may not; adding a function is a patch. Bit identity with `StrictMath` is the contract, and -no version changes it (`CHANGELOG.md`). +no version changes it (`CHANGELOG.md`), and no released function has a two-NaN lane to except. ## Use @@ -127,7 +127,7 @@ through `JAVA_TOOL_OPTIONS`; a JVM that has it set prints a warning to `System.e 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. +are unchanged, and each lane still meets the contract below. **Which species to pass.** Every species of 128 bits or more gives the same bits; what the body allocates differs. The allocation check (`bench/alloc-check.py`, run by CI) covers 256 bits, the @@ -164,7 +164,7 @@ fdlibm's own sequence of operations, lane by lane. That is this library. - **Bit identity.** For every `double` input, each lane equals `StrictMath.f` on that input, compared by `Double.doubleToRawLongBits`. NaN payloads and signed zeros are included. For a function of two arguments (`atan2`, `pow`, `hypot`), a lane where both arguments are NaN with - different bits equals one of the two arguments' bits with the quiet bit set (for `hypot`, also + different bits equals one of the two arguments' bits with the quiet bit set (for `hypot`, the same with the sign bit cleared); which one is not specified, since `StrictMath`'s own choice varies. - **fdlibm's operations, in fdlibm's order.** Only IEEE `+ - * /`, `sqrt` and bit operations. There is no fused multiply-add and no reassociation, so the rounding of every step is fdlibm's. @@ -185,10 +185,11 @@ Each function is checked against `StrictMath` on the same JVM: - exhaustively where a range is small enough; - by sampled bit patterns over the whole domain; - with fdlibm's thresholds and the known hard cases as fixed vectors; -- every night (`.github/workflows/nightly.yml`), by a sweep of all 2^32 high words, three low - words each, and by fuzzing from a fresh seed, every arm at every width the runner has; x86's - 128 bits, which run lane by lane there, are swept a 1/32 slice a night, and Arm's 128 bits - whole every night (`SKILLS.md`, "The nightly sweep and fuzz"). +- every night (`.github/workflows/nightly.yml`), by a sweep of all 2^32 high words, three low words + each (of one argument a night for a function of two arguments, the two alternating by default: + `pair-passes`), and by fuzzing from a fresh seed, every arm at every width the runner has; x86's + 128 bits, which run lane by lane there, are swept a 1/32 slice a night, and Arm's 128 bits whole + every night (`SKILLS.md`, "The nightly sweep and fuzz"). ## Licence diff --git a/src/main/java/io/github/vecbricks/vfdlibm/VFdlibm.java b/src/main/java/io/github/vecbricks/vfdlibm/VFdlibm.java index 3247afb..afdb89d 100644 --- a/src/main/java/io/github/vecbricks/vfdlibm/VFdlibm.java +++ b/src/main/java/io/github/vecbricks/vfdlibm/VFdlibm.java @@ -15,7 +15,9 @@ /** * fdlibm's double functions over {@link DoubleVector} lanes, each lane bit-identical to the * {@link StrictMath} method of the same name, NaN payloads and signed zeros included; the one - * exception is a lane of {@code atan2} where both arguments are NaN with different bits. + * exception is a lane of a function of two arguments ({@code atan2}) where both arguments are NaN + * with different bits, which is one of the two arguments' bits with the quiet bit set (which one + * is not specified). * *
Every method works on a vector of any species of 128 bits or more (exp, expm1, log, log10, * log1p, sinh, cosh, asin, atan, acos and atan2 hold fdlibm's {@code int} words in int lanes of