diff --git a/PROOFS.md b/PROOFS.md index 3e270a1..0ee761a 100644 --- a/PROOFS.md +++ b/PROOFS.md @@ -739,6 +739,88 @@ lanes (bounds on the polynomial, sampled by the hinge test, which asserts it) is lanes' values under it. Informal above: that bound, the binades further off than the next, H15 itself (L10), and the code's text. +### L15. expm1's words in long lanes, as Varka emits them + +Varka's emitted kernel (`VarkaMathPort.expm1`, in vecbricks/varka, not in this repository) +computes `Expm1.expm1Blended`'s operations with the C's `int` words in long lanes, as `Log` holds +its words (L8), since a kernel's high-word tests are L3's compares of `|x|`'s bits and its `k` is +the long `I2L` of the `D2I`. The doubles are the same operations in the same order; this lemma +covers the words. `Expm1` here keeps those words in `IntVector` lanes, so (a) to (c) are about +Varka's kernel only; (d), the exponent add, is also `Expm1`'s own (and `Lanes.addExponent`'s). + +**Statement.** Let `k` be the C's `int k` (from L10's reduction) widened to a long. +- (a) On a lane with `2 <= k < 20`, the long `0x3ff00000 - (0x200000 >>> k)` is the C's + `0x3ff00000 - (0x200000>>k)` widened; on a lane with `20 <= k <= 56`, the long `(0x3ff - k) << + 20` is the C's `(0x3ff-k)<<20` widened. +- (b) For every long `w` whose low 32 bits are the C's word `W`, `w << 32` is the bits of the + double whose high word is `W` and whose low word is 0, the C's `t = one; __HI(t) = W`. +- (c) The tests of `k`, `k <= -2`, `k > 56`, `2 <= k < 20`, `k == 1`, `k == -1` and `k == 0`, are + the C's on the long. +- (d) For every double `y` and every `k` with `|k| < 2^11`, `bits(y) + (k << 52)` (a long add) + has `y`'s low word and the high word `__HI(y) + (k<<20)` modulo 2^32: the C's `__HI(y) += + (k<<20)` (lines 199, 206 and 211), wherever the C's `int` add does not overflow. + +**Hypotheses.** +- (H16) The words are read only on the lanes whose form uses them: the kernel picks `t`'s word + by the masks of `k`, and a word computed on any other lane is never kept, whatever the shift + count there. It is needed: at `k = 40`, a lane of the `20 <= k <= 56` form, `0x200000 >> 40` + (an `int` shift, count modulo 32) and `0x200000 >>> 40` (a long one, modulo 64) differ. +- (H17) `-56 <= k <= 1024` on every lane whose result is kept. L10's domain is the lanes the C + does not return early from: `x <= o_threshold`, and `hx < 0x4043687A` for a negative `x`. The + formula `(int) fl(fl(x*invln2) + h)` is non-decreasing in `x` on each sign (R2, and the + truncation), and it gives -56 at the lowest such `x` and 1024 at `o_threshold`. + +**Proof.** +1. (a) For `2 <= k < 20`, `0x200000 >> k` is a positive int below `2^20`, so the arithmetic + and the logical shift agree (JLS 15.19) and the difference lies in `(0x3fe00000, 0x3ff00000)`, + inside 32 bits. For `20 <= k <= 56`, `0x3ff - k` lies in `[0x3c7, 0x3eb]` and its shift by 20 + in `[0x3c700000, 0x3eb00000]`, below `2^31`, so no int overflows and the long is the int + widened. +2. (b) `w << 32` keeps `w`'s low 32 bits as the high word and shifts the rest out, with zeros + below, so the bits above `W` in `w` are irrelevant. +3. (c) `k` is an `int` widened, so each compare with an int constant has the int compare's answer + (JLS 5.6). +4. (d) `k << 52`, for `|k| < 2^11`, is `(k<<20) * 2^32` modulo 2^64, with `k<<20` the C's int (no + overflow for `|k| < 2^11`): its low 32 bits are zero. Adding it to `bits(y)` leaves the low + word, and no carry passes from the low word into the high one. The high 32 bits get `k<<20` + added modulo 2^32, since a carry out of bit 63 is dropped by the long add. That is the C's + `int` add on the high word wherever that add does not overflow (and its two's-complement wrap + where it does). + +No IEEE property is used but R2, in H17. With H16, (a) to (c) give the C's `t` on each lane that +reads it, and (d) the C's scaled `y`; H17 puts every kept lane's `k` in the range the steps cover. + +**Hinge tests (scalar).** `ProofsTest.l15ThresholdsAreTheCs`: `Expm1.K_FAR` and `K_SPLIT` are the +C's 56 and 20. `ProofsTest.l15LongWordsAreExpm1sInts` (a, b): for every `k` from -1100 to 1100 +whose form reads a word, by the C's literal bounds (55 of them), the long word against the C's int +word and the double it heads against `1 - 2^-k` or `2^-k`; `one`'s word against 1.0, once; and +the two shifts at `k = 40`, where they differ. `ProofsTest.l15ExponentAddIsTheCsHighWordAdd` +(d): for every `k` from -1100 to 1100 and 1024 `y` bit patterns (high words 0, `0x3fe00000`, +`0x3ff00000`, `0x7fefffff`, `0x7fffffff`, `0x80000000`, `0xbff00000` and `0xffffffff`, which +wrap either way, against low words 0, 1, `0x7fffffff`, `0x80000000` and `0xffffffff`, and seeded +draws), the long add against the C's int add on the high word. (c) has no scalar test: comparing +an int with its widening cannot fail; the vector test below runs every form. +**Vector tie, here.** `ProofsTest.l15MasksKeepEachWordOnItsLanes` checks H16 on `Expm1`'s own +masks, which Varka's kernel emits: `Expm1.expm1Blended`, at every width, against +`StrictMath.expm1` by raw bits on 7569 inputs that cover every `k` from -56 to 1024 (each input's +`k` and that it is no early-return lane are asserted, and H17's two end doubles with them), +shuffled so that a vector mixes the forms, `k == -1`, `0` and `1` among them. A mask that kept a +word on a lane whose form does not read it fails there wherever that word differs from the +lane's own (the int mid word is one's on a far lane with `k mod 32 >= 22`, so not there): +blending the mid word over `k == 40` made the 7 inputs at `k = 40` differ, and keeping the high +word on the far lanes 7163 of the 7569 (each at every width, mutations made by hand and +reverted). That Varka's kernel uses +these masks and its long words is not checked here. **Vector tie, in Varka.** Where L15's own +claim, the kernel, is checked: Varka's `VarkaMathPortSuite` (vecbricks/varka PR #1629) runs the +emitted `VarkaMathPort.expm1` against `VFdlibm.expm1` and `Expm1.expm1Blended` by raw bits, on +2^24 sampled inputs and the fixed vectors at each `k` form's edge. + +**Encoding.** None, no machine-checked file: the steps are integer ranges and identities, checked +by the Java loops above over `-1100 <= k <= 1100`, which holds every `k` of H17, `-56 <= k <= +1024`; the bounds themselves rest on L10's domain and the monotonicity of the formula, the end +doubles asserted by `l15MasksKeepEachWordOnItsLanes`. Informal: that Varka's kernel is +`expm1Blended`'s operations with these long words, which only Varka's vector tie checks. + ## log10 (`Log10.java`, from `e_log10.c`) e_log10.c reads `k` from the high word, scaling a subnormal by `2^54` first (lines 76-82); sets diff --git a/src/main/java/io/github/vecbricks/vfdlibm/Expm1.java b/src/main/java/io/github/vecbricks/vfdlibm/Expm1.java index 405a15d..4b3660e 100644 --- a/src/main/java/io/github/vecbricks/vfdlibm/Expm1.java +++ b/src/main/java/io/github/vecbricks/vfdlibm/Expm1.java @@ -56,9 +56,10 @@ * the far form's {@code D2I} and {@code t = k} its {@code I2D}, as in {@code Exp}; the words * {@code 0x3ff00000-(0x200000>>k)} and {@code (0x3ff-k)<<20} are int lanes too, shifted by * {@code k} lane by lane, and the C's {@code __HI(y) += (k<<20)} is the long add of {@code k << - * 52} to the bits ({@code Lanes.addExponent}, written out). Each form is computed in every lane - * and blended by the masks of {@code k}; a form's value on a lane that does not take it is never - * kept, whatever the shift counts there. + * 52} to the bits ({@code Lanes.addExponent}, written out; PROOFS.md, L15). Each form is computed + * in every lane and blended by the masks of {@code k}; a form's value on a lane that does not take + * it is never kept, whatever the shift counts there. Varka's emitted kernel holds these words in + * long lanes instead (PROOFS.md, L15, checked in Varka's {@code VarkaMathPortSuite}). * *

Three arms differ in the C's rare paths, the early returns: the filter ({@code +-inf}, * NaN, {@code x > o_threshold} and {@code x < -56*ln2}) and {@code |x| < 2^-54}. A positive @@ -346,9 +347,9 @@ private static DoubleVector expm1(DoubleVector x, VectorMask live, int m VectorMask highLanes = high.cast(species); DoubleVector y = tv.sub(em.sub(r)).blend(r.sub(em.add(tv)).add(one), highLanes); // Lines 199, 206 and 211: __HI(y) += (k<<20), as the long add of k << 52 to y's bits - // (Lanes.addExponent, written out). The addend's low word is zero, so y's low word is kept - // and nothing carries out of it: the high word becomes __HI(y) + (k<<20) modulo 2^32, the - // C's int add. Line 200: the far lanes return y-one. + // (Lanes.addExponent, written out; PROOFS.md, L15). The addend's low word is zero, so y's + // low word is kept and nothing carries out of it: the high word becomes __HI(y) + (k<<20) + // modulo 2^32, the C's int add. Line 200: the far lanes return y-one. LongVector yBits = (LongVector) y.viewAsIntegralLanes(); DoubleVector scaled = (DoubleVector) yBits.add(kLong.lanewise(VectorOperators.LSHL, 52)) .viewAsFloatingLanes(); diff --git a/src/main/java/io/github/vecbricks/vfdlibm/Lanes.java b/src/main/java/io/github/vecbricks/vfdlibm/Lanes.java index cf19c97..7d93d0d 100644 --- a/src/main/java/io/github/vecbricks/vfdlibm/Lanes.java +++ b/src/main/java/io/github/vecbricks/vfdlibm/Lanes.java @@ -178,7 +178,7 @@ static VectorSpecies intSpecies(VectorSpecies species) { /** * {@code __HI(y) += (k << 20)}: adds {@code k} to each lane's binary exponent by adding {@code * k << 52} to its bits. The low word of {@code k << 52} is zero, so the sum's high word is the - * C's {@code int} sum modulo 2^32 and its low word is unchanged. + * C's {@code int} sum modulo 2^32 and its low word is unchanged (PROOFS.md, L15). */ static DoubleVector addExponent(DoubleVector y, LongVector k) { return fromBits(bits(y).add(k.lanewise(VectorOperators.LSHL, 52))); diff --git a/src/test/java/io/github/vecbricks/vfdlibm/ProofsTest.java b/src/test/java/io/github/vecbricks/vfdlibm/ProofsTest.java index 3584bee..494d34c 100644 --- a/src/test/java/io/github/vecbricks/vfdlibm/ProofsTest.java +++ b/src/test/java/io/github/vecbricks/vfdlibm/ProofsTest.java @@ -885,4 +885,154 @@ void l13FalseHingesAreCaught(VectorWidth width) { assertThrows(ArithmeticException.class, () -> IntVector.broadcast(ints, 1).div(IntVector.zero(ints))); } + + // L15: expm1's words in long lanes, as Varka emits them. + + /** L15: Expm1's thresholds of k are the C's literals, 56 (line 197) and 20 (line 203). */ + @Test + void l15ThresholdsAreTheCs() { + assertEquals(56, Expm1.K_FAR); + assertEquals(20, Expm1.K_SPLIT); + } + + /** + * L15 (a), (b): for every k from -1100 to 1100 whose form reads a word, the long word is + * the C's int word widened, and the double it heads is the one the C's form means, 1-2^-k for + * {@code 2 <= k < 20} and 2^-k for {@code 20 <= k <= 56}, the C's literals; and at a k where + * the int and the long shifts differ, k = 40, the lane's form reads the other word (H16). + */ + @Test + void l15LongWordsAreExpm1sInts() { + // The far, k == 1, k == 0 and k == -1 lanes read t = one, whose word heads 1.0, for every k. + assertEquals(bits(1.0), Lanes.fromHiBits(0x3ff00000)); + assertEquals(bits(1.0), 0x3ff00000L << 32); + int read = 0; + for (int k = -1100; k <= 1100; k++) { + long kl = k; + String at = "k = " + k; + if (k >= 2 && k < 20) { + int word = 0x3ff00000 - (0x200000 >> k); + long longWord = 0x3ff00000L - (0x200000L >>> kl); + assertEquals((long) word, longWord, at); + assertEquals(bits(1.0 - Math.scalb(1.0, -k)), longWord << 32, at); + assertEquals(Lanes.fromHiBits(word), longWord << 32, at); + read++; + } else if (k >= 20 && k <= 56) { + int word = (0x3ff - k) << 20; + long longWord = (0x3ffL - kl) << 20; + assertEquals((long) word, longWord, at); + assertEquals(bits(Math.scalb(1.0, -k)), longWord << 32, at); + assertEquals(Lanes.fromHiBits(word), longWord << 32, at); + read++; + } + } + assertEquals(55, read); + // k = 40, a lane of the high form, which does not read the mid word: an int shift takes the + // count modulo 32 and a long one modulo 64, so the words differ there, and H16 is needed. + assertNotEquals((long) (0x200000 >> 40), 0x200000L >>> 40L); + } + + /** + * L15 (d): the C's {@code __HI(y) += (k<<20)} is {@code yBits + ((long) k << 52)}: the low word + * kept, no carry into the high word, and the high word the int sum modulo 2^32, the carry out + * of bit 63 dropped. Over every k from -1100 to 1100 and high words that wrap either way. + */ + @Test + void l15ExponentAddIsTheCsHighWordAdd() { + SplittableRandom random = new SplittableRandom(1500L); + long[] ys = new long[1 << 10]; + int[] his = {0, 0x3fe00000, 0x3ff00000, 0x7fefffff, 0x7fffffff, 0x80000000, 0xbff00000, + 0xffffffff}; + int[] los = {0, 1, 0x7fffffff, 0x80000000, 0xffffffff}; + int n = 0; + for (int hi : his) { + for (int lo : los) { + ys[n++] = ((long) hi << 32) | (lo & 0xffffffffL); + } + } + while (n < ys.length) { + ys[n++] = random.nextLong(); + } + for (int k = -1100; k <= 1100; k++) { + for (long y : ys) { + int hi = (int) (y >>> 32); + int lo = (int) y; + long want = ((long) (hi + (k << 20)) << 32) | (lo & 0xffffffffL); + assertEquals(want, y + ((long) k << 52), () -> "y = " + Long.toHexString(y)); + } + } + } + + /** The C's k for x on the lanes it reduces (lines 162 and 170), and 0 on the others (L10). */ + private static int l15K(double x) { + int hx = (int) (bits(x) >>> 32) & 0x7fffffff; + return hx > Expm1.HX_HALF_LN2 ? (int) (Expm1.INVLN2 * x + (x < 0 ? -0.5 : 0.5)) : 0; + } + + /** A lane the C returns early from (lines 146-158 and 177), whose result Expm1 replaces. */ + private static boolean l15Rare(double x) { + int hx = (int) (bits(x) >>> 32) & 0x7fffffff; + return hx >= Expm1.HX_FILTER && !(x <= Expm1.O_THRESHOLD) + || x < 0 && hx >= Expm1.HX_56_LN2 || hx < Expm1.HX_TINY; + } + + /** + * Inputs at every k the lanes kept can take, -56 to 1024 (L10's domain, its two ends checked + * here), several per k, shuffled so that a vector mixes the forms of k. + */ + private static double[] l15EveryK() { + double lowest = -Math.nextDown(Lanes.fromHi(Expm1.HX_56_LN2)); + assertFalse(l15Rare(lowest)); + assertTrue(l15Rare(Math.nextDown(lowest))); + assertEquals(-56, l15K(lowest)); + assertFalse(l15Rare(Expm1.O_THRESHOLD)); + assertTrue(l15Rare(Math.nextUp(Expm1.O_THRESHOLD))); + assertEquals(1024, l15K(Expm1.O_THRESHOLD)); + // ln2 rounded; any value near it puts (k + d) * ln2 at k for these offsets d. At k = 0 the + // offset 0 would give x = 0, a tiny lane, so that one input is 0.2 instead. + double ln2 = 0x1.62e42fefa39efp-1; + double[] offsets = {-0.49, -0.3, -0.1, 0.0, 0.1, 0.3, 0.49}; + double[] xs = new double[1081 * offsets.length + 2]; + int n = 0; + xs[n++] = lowest; + xs[n++] = Expm1.O_THRESHOLD; + for (int k = -56; k <= 1024; k++) { + for (double d : offsets) { + double x = k == 0 && d == 0.0 ? 0.2 : (k + d) * ln2; + xs[n++] = Math.min(Math.max(x, lowest), Expm1.O_THRESHOLD); + } + } + boolean[] seen = new boolean[1081]; + for (double x : xs) { + int k = l15K(x); + assertFalse(l15Rare(x), Oracle.hex(x)); + assertTrue(k >= -56 && k <= 1024, Oracle.hex(x)); + seen[k + 56] = true; + } + for (int k = -56; k <= 1024; k++) { + assertTrue(seen[k + 56], "no input at k = " + k); + } + SplittableRandom random = new SplittableRandom(1501L); + for (int i = xs.length - 1; i > 0; i--) { + int j = random.nextInt(i + 1); + double swap = xs[i]; + xs[i] = xs[j]; + xs[j] = swap; + } + return xs; + } + + /** + * L15's H16 on Expm1's own masks: Expm1.expm1Blended, the body Varka's kernel emits, against + * StrictMath by raw bits on every k from -56 to 1024, with the forms mixed within a vector. A + * mask that kept a word on a lane whose form does not read it (the mid word at k = 40, the high + * word on a far lane) fails here wherever that word differs from the lane's own. The int mid + * word equals one's on a far lane with k mod 32 >= 22, so a mid word kept there would pass. + */ + @ParameterizedTest + @EnumSource(VectorWidth.class) + void l15MasksKeepEachWordOnItsLanes(VectorWidth width) { + Oracle.assertUnary("l15 expm1Blended", width.species(), x -> Expm1.expm1Blended(x, null), + StrictMath::expm1, l15EveryK()); + } }