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
89 changes: 87 additions & 2 deletions PROOFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ same double for every input (`PLAN.md` section 6). Each lemma names its hypothes
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.
log1p) has a section saying so; log10's names the lemmas it rests on through `log` and holds
L17, on Varka's form of it, and 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
Expand Down Expand Up @@ -885,6 +886,90 @@ of the new high word, `I2L` then a shift left by 32, is the C's `__HI(x) =` beca
never negative (so sign extension and zero extension agree, as in L8's proof), the
commutativity of the product, and that the port computes these steps, the code's text.

### L17. log10's words in long lanes, as Varka emits them

Varka's emitted kernel (`VarkaMathPort.log10`, in vecbricks/varka, not in this repository)
computes `Log10.log10Blended`'s doubles in the same order, with the C's `int` words in long
lanes, as `Log` holds its own (L8), so the kernel needs no `I2L` or `L2I`; its `log` of the
rescaled lane is `Log.logCore`'s nodes. `Log10` here keeps those words in `IntVector` lanes and
takes `y` by an exact `I2D`, so this lemma is about Varka's kernel only, and the section above,
that `Log10` needs no lemma of its own, still holds. The subnormal mask, `0 < x < MIN_NORMAL`,
is the same IEEE compare in both.

**Statement.** Let `b` be a lane's bits after the subnormal scaling (`xs` of line 80) and `s` 1
on a scaled lane, 0 otherwise. Write `hx = (int) (b >> 32)` for the C's signed high word, `k =
(hx >> 20) - 1023 - 54s` and `i = ((unsigned)k&0x80000000)>>31` for the C's, and `HX = b >>> 32`
for the long one.
- (a) If `b`'s sign bit is clear, the long `K = (HX >>> 20) - 1023 - 54s` is `k` widened, the
long `I = K >>> 63` is `i` widened, and the long `(HX & 0xfffff) | ((0x3ff - I) << 20)` is the
C's new `hx` of line 86 widened, a value in `[0, 2^31)`, so the long shift by 32 that writes
it back is the C's `__HI(x) = hx`.
- (b) For every `K` with `-1077 <= K <= 1024` and `I = K >>> 63`, `fromBits(K + I +
0x43380000_00000000) - 0x1.8p52` is the double `K + I`, the C's `(double)(k+i)` (L7).
- (c) For every `b`, either sign, the signed long compares `b > 0x7ff00000_00000000 - 1` and `b <
0` are the C's `hx >= 0x7ff00000` and `hx < 0` (lines 83 and 79).

**Hypotheses.**
- (H18) The sign bit of `b` is clear on every lane whose result is kept. A lane with it set is
`x < 0`, `-0` or a NaN with the sign bit set, which the C answers at line 79 with
`(x-x)/zero` (a NaN) or at line 78 with `-two54/zero` (`-inf`), and the kernel blends those
two answers over every earlier result.
It is needed: for a negative `b`, `HX >>> 20` holds the sign bit and `K` is not the C's `k`.

**Proof.**
1. (a) With the sign clear, `b >> 32 = b >>> 32 = HX` lies in `[0, 2^31)`, so `HX` is `hx`
widened and `HX >>> 20` is the C's `hx >> 20` (JLS 15.19: on a non-negative value the two
shifts agree). `HX >>> 20` lies in `[0, 0x7ff]`, so `K` lies in `[-1077, 1024]` and is `k`
widened. A long's bit 63 is its sign, so `K >>> 63` is 1 exactly when `K < 0`, and so is the
C's `i` for the int `k`. `HX & 0xfffff` keeps the bits that `hx & 0x000fffff` keeps, and
`0x3ff - I` is `0x3ff` or `0x3fe`, whose shift by 20 is below `2^30`: the OR lies in `[0,
2^31)`, the int has no overflow, and the long is the int widened. A non-negative word below
`2^31` shifted left by 32 is the C's high word with zero low bits, which the kernel ORs over
`b`'s low word, as L8's proof does.
2. (b) `K + I` lies in `[-1076, 1024]`, inside L7's `[-2^51, 2^51)`, so the add of the bits of
`0x1.8p52` and the subtract of `0x1.8p52` are exact (L7), and give the same double as the
exact `I2D` of the int `k + i`.
3. (c) `b = hx * 2^32 + lo` with `0 <= lo < 2^32` as signed longs, so for any int `h`, `hx >= h`
iff `b >= h * 2^32`, which for `h = 0x7ff00000` is `b > 0x7ff00000_00000000 - 1`; and `hx < 0`
iff `b < 0`. This is L9's argument on the raw bits.

No IEEE property is used but L7's exact conversion. With H18, (a) and (b) give the C's words and
`y` on every kept lane; (c) holds on every lane, so the rare lanes are the C's.

**Hinge tests (scalar).** `ProofsTest.l17LongWordsAreLog10sInts` (a, b): a loop over every
exponent field from 0 to `0x7ff`, scaled and not, which by step 1 is every value `K` takes (the
words depend on `b` only through `HX >>> 20` and `HX & 0xfffff`), checks `K`, `I` and `y`
against the C's int `k`, `i` and `I2D`, and `K` in `[-1077, 1024]`; it also checks, from the
smallest and the largest subnormal, that a scaled lane's field is 3 to 54, so a kept lane's `K`
is in `[-1074, 1023]` (field `0x7ff`, `K = 1024`, is `+inf` or a NaN, always blended over). A
second loop runs every significand word from 0 to `0xfffff`, for both `I`, the long new high word
against the C's and below `2^31`; the exponent field is masked away in both, so it needs no
loop. `ProofsTest.l17RareComparesAreTheCs` (c): the two long compares against
the C's on every high word within `2^16` of 0, `0x7ff00000`, `0x7fffffff` and `0x80000000` (where
an answer changes or the int wraps), with five low words each, and on 2^20 sampled patterns.
`ProofsTest.l17NegativeLanesAreAnsweredEarly` is a witness for H18 and the oracle's own check,
not a hinge test: on every sign-set pattern of a set of specials, sampled patterns and every
exponent field, `StrictMath.log10` is a NaN, or `-inf` for `-0`, as the C's early returns give.
On every one of those patterns the long `K` is not the C's `k`, which shows H18 is needed: the
long field holds the sign bit, so `K` is at least 1025, while the C's `k` is at most -1024; this
holds for every input, so no test checks it. The checks of H18 are
`l17NegativeLanesAreBlendedOver` below and Varka's suite. **Vector tie, here.**
`ProofsTest.l17NegativeLanesAreBlendedOver` checks H18 on `Log10`'s own masks, whose blends
Varka's kernel emits in the same order: `Log10.log10Blended`, at every width, against
`StrictMath.log10` by raw bits on those sign-set patterns, each beside its positive twin so that
a vector mixes rare and kept lanes; a rare blend dropped or placed before the general form fails
there. **Vector tie, in Varka.** Where L17's own claim, the kernel, is checked: Varka's
`VarkaMathPortSuite` (vecbricks/varka#1645) runs the emitted `VarkaMathPort.log10` against
`VFdlibm.log10` and `Log10.log10Blended` by raw bits, on 2^24 sampled inputs and the fixed
vectors of e_log.c's and e_log10.c's words. That Varka's kernel uses these long words is not
checked here.

**Encoding.** None, no machine-checked file: (a) is an integer identity checked by the Java
loops above over every exponent field and every significand word; (c) is step 3's argument,
checked at its boundaries and on samples, not over every word; and (b) is L7's file,
`proofs/log_exact_dk.smt2`, over a range that holds `[-1076, 1024]`. Informal: that Varka's
kernel is `log10Blended`'s operations with these long words, which only Varka's vector tie checks.

## log1p (`Log1p.java`, from `s_log1p.c`)

s_log1p.c returns early for `x <= -1`, `|x| < 2^-29` and a non-finite `x` (lines 113-124 and
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/github/vecbricks/vfdlibm/Log10.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
* <p>The C's {@code int} words stay in int lanes, as in {@code Log}: {@code hx}, {@code k} and
* {@code i} are an {@link IntVector} with the double vector's lane count at half its bits, every
* test on them is the C's signed {@code int} compare, and {@code y = (double)(k+i)} is an exact
* {@code I2D}, which every AVX2 host lowers.
* {@code I2D}, which every AVX2 host lowers. Varka's emitted kernel holds these words in long
* lanes instead (PROOFS.md, L17, checked in Varka's {@code VarkaMathPortSuite}).
*
* <p>C2 stops inlining once the compiled graph passes {@code NodeCountInliningCutoff}, and a
* refused call passes its vectors boxed ({@code SKILLS.md}, "What the log10 port taught"). So
Expand Down
135 changes: 135 additions & 0 deletions src/test/java/io/github/vecbricks/vfdlibm/ProofsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,4 +1035,139 @@ void l15MasksKeepEachWordOnItsLanes(VectorWidth width) {
Oracle.assertUnary("l15 expm1Blended", width.species(), x -> Expm1.expm1Blended(x, null),
StrictMath::expm1, l15EveryK());
}

// L17: log10's words in long lanes, as Varka emits them.

/** The C's k of e_log10.c's lines 80 and 84, hx the signed high word (line 81's if scaled). */
private static int l17CK(int hx, int scaled) {
return (hx >> 20) - 1023 - 54 * scaled;
}

/**
* L17 (a), (b): a loop over every exponent field, 0 to 0x7ff, scaled and not, which is every
* value K takes: the long K, I and y against the C's int k, i and I2D, K in [-1077, 1024]; a
* scaled subnormal's field is 3 to 54; and a loop over every significand word for both I:
* the long new high word against the C's, below 2^31.
*/
@Test
void l17LongWordsAreLog10sInts() {
for (int field = 0; field <= 0x7ff; field++) {
for (int scaled = 0; scaled < 2; scaled++) {
int chx = field << 20;
long hx = Integer.toUnsignedLong(chx);
long k = (hx >>> 20) - 1023 - 54 * scaled;
int ck = l17CK(chx, scaled);
String at = "field " + field + ", scaled " + scaled;
assertTrue(k >= -1077 && k <= 1024, at);
assertEquals((long) ck, k, at);
long i = k >>> 63;
int ci = (int) (((long) ck & 0x80000000L) >>> 31);
assertEquals((long) ci, i, at);
double y = Double.longBitsToDouble(k + i + Log.DK_MAGIC_BITS) - Log.DK_MAGIC;
assertEquals(bits((double) (ck + ci)), bits(y), at);
}
}
// A scaled lane is a subnormal times 2^54: its field is 3 (2^-1020) to 54 (below 2^-968).
assertEquals(3, (int) (bits(Double.MIN_VALUE * Log10.TWO54) >>> 52));
assertEquals(54, (int) (bits(Math.nextDown(Double.MIN_NORMAL) * Log10.TWO54) >>> 52));
// The exponent field is masked away in both words, so the significand and I are all that
// the new high word reads.
for (int m = 0; m <= 0xfffff; m++) {
for (int ci = 0; ci < 2; ci++) {
long i = ci;
long word = (m & 0x000fffffL) | ((0x3ffL - i) << 20);
int cword = (m & 0x000fffff) | ((0x3ff - ci) << 20);
if (word != (long) cword || word < 0 || word >= 1L << 31) {
assertEquals((long) cword, word, "m " + m + ", i " + i);
assertTrue(word >= 0 && word < 1L << 31, "word " + Long.toHexString(word));
}
}
}
}

/**
* L17 (c): the signed long compares on the raw bits are the C's {@code hx >= 0x7ff00000} and
* {@code hx < 0}, on every high word within 2^16 of the words where an answer changes or the
* int wraps (0, 0x7ff00000, 0x7fffffff and 0x80000000), with five low words each, and on
* 2^20 sampled patterns.
*/
@Test
void l17RareComparesAreTheCs() {
long nonFinite = Lanes.fromHiBits(Log10.HX_NON_FINITE) - 1;
long[] los = {0L, 1L, 0x7fff_ffffL, 0x8000_0000L, 0xffff_ffffL};
SplittableRandom random = new SplittableRandom(1702L);
int checked = 0;
for (int edge : new int[] {0, Log10.HX_NON_FINITE, Integer.MAX_VALUE, Integer.MIN_VALUE}) {
for (int d = -(1 << 16); d <= 1 << 16; d++) {
int h = edge + d;
for (long lo : los) {
long b = ((long) h << 32) | lo;
assertEquals(h >= Log10.HX_NON_FINITE, b > nonFinite, Long.toHexString(b));
assertEquals(h < 0, b < 0, Long.toHexString(b));
checked++;
}
}
}
for (int n = 0; n < 1 << 20; n++) {
long b = random.nextLong();
int h = (int) (b >> 32);
assertEquals(h >= Log10.HX_NON_FINITE, b > nonFinite, Long.toHexString(b));
assertEquals(h < 0, b < 0, Long.toHexString(b));
}
assertEquals(4 * ((1 << 17) + 1) * los.length, checked);
}

/**
* L17's H18, a witness and the oracle's own check, not a hinge test: StrictMath.log10 answers
* every lane with the sign bit set with a NaN, or -inf for -0, as the C's early returns do. On
* every one of those lanes the long K is not the C's k (the long field holds the sign bit, so
* K is at least 1025, while the C's k is at most -1024), which shows H18 is needed; that holds
* for every input, so it is not asserted. H18 is checked by l17NegativeLanesAreBlendedOver and
* Varka's suite.
*/
@Test
void l17NegativeLanesAreAnsweredEarly() {
for (double x : l17SignSet()) {
double got = StrictMath.log10(x);
assertTrue(x == 0.0 ? bits(got) == bits(Double.NEGATIVE_INFINITY) : Double.isNaN(got),
Oracle.hex(x));
}
}

/**
* Patterns with the sign bit set: one at every exponent field, the specials, 2^16 sampled
* patterns and 2^12 subnormals, each with its sign set.
*/
private static double[] l17SignSet() {
double[] xs = Inputs.concat(Inputs.specials(), Inputs.sampled(1700L, 1 << 16),
Inputs.uniform(1701L, 1 << 12, 0.0, Double.MIN_NORMAL));
double[] out = new double[0x800 + xs.length];
for (int field = 0; field <= 0x7ff; field++) {
long significand = field * 0x9e3779b97f4aL & 0xf_ffff_ffff_ffffL;
out[field] = Double.longBitsToDouble(Long.MIN_VALUE | ((long) field << 52) | significand);
}
for (int j = 0; j < xs.length; j++) {
out[0x800 + j] = Double.longBitsToDouble(bits(xs[j]) | Long.MIN_VALUE);
}
return out;
}

/**
* L17's H18 on Log10's own masks: Log10.log10Blended, whose blends Varka's kernel emits in the
* same order, against StrictMath by raw bits on the sign-set patterns, each beside its
* positive twin so that a vector mixes rare and kept lanes. A kernel that kept a sign-set
* lane's general form, its rare blend dropped or placed before the general one, fails here.
*/
@ParameterizedTest
@EnumSource(VectorWidth.class)
void l17NegativeLanesAreBlendedOver(VectorWidth width) {
double[] signSet = l17SignSet();
double[] xs = new double[2 * signSet.length];
for (int j = 0; j < signSet.length; j++) {
xs[2 * j] = signSet[j];
xs[2 * j + 1] = Math.abs(signSet[j]);
}
Oracle.assertUnary("l17 log10Blended", width.species(), x -> Log10.log10Blended(x, null),
StrictMath::log10, xs);
}
}