plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


b0781c1 — Peter John 2026-11-10T22:31:03+05:30
benchmarks/math_bits: check exp and ln as fdlibm ports, not against the old loops
Files changed (1) hide show
  1. benchmarks/math_bits/check.plum +80 -96
benchmarks/math_bits/check.plum CHANGED
@@ -1,16 +1,20 @@
1
- # Bit-identity check for std math's unrolled series and bit-based range reduction: runs the
1
+ # A check of std math's bit-level functions over the same deterministic inputs, counting the
2
- # loop versions std had before (kept here only, as `old*`) and std's current ones over the
3
- # same deterministic inputs and counts results whose 64 bits differ (NaN payloads and the
4
- # sign of zero included). `sin`, `cos`, `tan` and `atan` are no longer compared: they are now
5
- # fdlibm's (under 1 ulp; the old series were up to 10^22 and 165 ulp off) and were checked
6
- # against mpmath instead. `logb` now reads the exponent from the bits; it is checked against an
2
+ # results that are off. `logb` reads the exponent from the bits; it is checked against an
7
- # exact halve-or-double reference instead (the old `floor(ln(|x|) * Log2E)` was wrong for
3
+ # exact halve-or-double reference (the old `floor(ln(|x|) * Log2E)` was wrong for about 40%
8
- # about 40% of the floats just below a power of two).
4
+ # of the floats just below a power of two). `pow` must still be what it is documented to
5
+ # be, bit for bit: square-and-multiply for a whole exponent, NaN for a negative base and a
6
+ # fractional one, else `exp(y * ln(x))`. `exp` and `ln` are fdlibm's (under 1 ulp against
7
+ # mpmath, checked when they were ported, as `sin`, `cos`, `tan` and `atan` were); here they
8
+ # are checked against each other: `ln(exp(x))` is `x` within (1 + |x|) * 2^-51, and
9
+ # `exp(ln(x))` is `x` within (2 + |ln(x)|) * 2^-51 of it relatively, wherever the results are
10
+ # normal floats, and the specials (zeros, the infinities, NaN, the ends of their ranges)
11
+ # are exact. The old loop versions of `exp` and `ln` (up to 8700 ulp and 7e10 ulp off) are
12
+ # no longer compared.
9
13
  #
10
14
  # target/release/plum run benchmarks/math_bits/check.plum (also with --no-fbip, --no-fusion)
11
15
  #
12
- # Prints one line per function (inputs, mismatches) and ends with `mismatches 0` when every
16
+ # Prints one line per check (inputs, mismatches) and ends with `mismatches 0` when every
13
- # result matched. Inputs: fixed specials (zeros, the infinities, NaN, subnormals, Float.Min,
17
+ # result was within its bound. Inputs: fixed specials (zeros, the infinities, NaN, subnormals, Float.Min,
14
18
  # Float.Max, 0.5, 700, powers of two), then per index one of: a random 64-bit pattern, a
15
19
  # random exponent field with a random fraction, uniform in [-10, 10], [-1000, 1000] and
16
20
  # [-1e6, 1e6], within a few ulps of k * Pi / 2, k * Pi or k * Tau, within a few ulps of a
@@ -24,72 +28,13 @@ import std/Io
24
28
  import std/collections/List
25
29
  import std/Slice
26
30
  import std/collections/Map
31
+ import std/Pair
27
32
  import std/math/math
28
33
 
29
- # --- the old loop versions ---
34
+ # --- the references ---
30
35
 
31
- fun oldExp(x: Float) -> Float =
32
- if x != x
33
- x
34
- else if x > 700.0f
35
- 1.0f / 0.0f
36
- else if x < -700.0f
37
- 0.0f
38
- else
39
- oldExpReduce(x, 0)
40
-
41
- fun oldExpReduce(v: Float, k: Int) -> Float =
42
- if v > 0.5f || v < -0.5f
43
- oldExpReduce(v / 2.0f, k + 1)
44
- else
45
- oldSquareTimes(oldExpSeries(v, 1.0f, 1.0f, 1), k)
46
-
47
- fun oldExpSeries(v: Float, term: Float, sum: Float, n: Int) -> Float =
48
- if n < 25
49
- t := term * v / Float(n)
50
- oldExpSeries(v, t, sum + t, n + 1)
51
- else
52
- sum
53
-
54
- fun oldSquareTimes(result: Float, k: Int) -> Float =
55
- if k > 0
56
- oldSquareTimes(result * result, k - 1)
57
- else
58
- result
59
-
60
- fun oldLn(x: Float) -> Float =
61
- if x != x || x < 0.0f
62
- 0.0f / 0.0f
63
- else if x == 0.0f
64
- -1.0f / 0.0f
65
- else if x > Float.Max
66
- x
67
- else
68
- oldLnDown(x, 0)
69
-
70
- fun oldLnDown(v: Float, k: Int) -> Float =
71
- if v >= 2.0f
72
- oldLnDown(v / 2.0f, k + 1)
73
- else
74
- oldLnUp(v, k)
75
-
76
- fun oldLnUp(v: Float, k: Int) -> Float =
77
- if v < 1.0f
78
- oldLnUp(v * 2.0f, k - 1)
79
- else
80
- t := {v - 1.0f} / {v + 1.0f}
81
- t2 := t * t
82
- sum := oldLnSeries(t2, t, t, 1)
83
- Float(k) * Float.Ln2 + 2.0f * sum
84
-
85
- fun oldLnSeries(t2: Float, term: Float, sum: Float, n: Int) -> Float =
86
- if n < 30
87
- t := term * t2
88
- oldLnSeries(t2, t, sum + t / Float(2 * n + 1), n + 1)
36
+ # `pow` as documented, from std's own `exp` and `ln`.
89
- else
90
- sum
91
-
92
- fun oldPow(base: Float, y: Float) -> Float =
37
+ fun refPow(base: Float, y: Float) -> Float =
93
38
  if y == 0.0f
94
39
  1.0f
95
40
  else
@@ -102,7 +47,7 @@ fun oldPow(base: Float, y: Float) -> Float =
102
47
  else if base < 0.0f
103
48
  0.0f / 0.0f
104
49
  else
105
- oldExp(y * oldLn(base))
50
+ math.exp(y * math.ln(base))
106
51
 
107
52
  # An exact logb by repeated halving or doubling into [1, 2) (every step exact): std's
108
53
  # `logb` reads the exponent from the bits and must agree everywhere.
@@ -124,6 +69,32 @@ fun refLogbFrom(v: Float, k: Int) -> Int =
124
69
  else
125
70
  k
126
71
 
72
+ # 2^-51: twice the ulp of 1.
73
+ Tol = 4.440892098500626e-16f
74
+
75
+ # Whether `x` is a normal float (not zero, subnormal, infinite or NaN).
76
+ fun normal(x: Float) -> Bool =
77
+ e := {x.toBits() >> 52} & 0x7FF
78
+ e > 0 && e < 2047
79
+
80
+ # Whether `ln(exp(x))` comes back to `x` (`expf` standing for `exp`), for an `x` whose
81
+ # `exp` is a normal float; true for any other `x`.
82
+ fun expRoundTrip(expf: fn(Float) -> Float, x: Float) -> Bool =
83
+ y := expf(x)
84
+ if !normal(x) && x != 0.0f || !normal(y)
85
+ true
86
+ else
87
+ {math.ln(y) - x}.abs() <= {1.0f + x.abs()} * Tol
88
+
89
+ # Whether `exp(ln(x))` comes back to `x`, for a positive normal `x`; true for any other.
90
+ fun lnRoundTrip(x: Float) -> Bool =
91
+ if !normal(x) || x < 0.0f
92
+ true
93
+ else
94
+ l := math.ln(x)
95
+ y := math.exp(l)
96
+ !normal(y) || {y - x}.abs() <= {2.0f + l.abs()} * Tol * x
97
+
127
98
  # --- inputs ---
128
99
 
129
100
  # splitmix64 (wrapping Int arithmetic; `>>` here only needs to mix).
@@ -189,22 +160,18 @@ fun inputAt(i: Int) -> Float =
189
160
  fun same(a: Float, b: Float) -> Bool =
190
161
  a.toBits() == b.toBits()
191
162
 
163
+ # The inputs from `i` to `n` for which `ok` fails, the first three printed.
192
- fun sweep1(io: Io, old: fn(Float) -> Float, new: fn(Float) -> Float, i: Int, n: Int, bad: Int) -> Int =
164
+ fun countBad(io: Io, gen: fn(Int) -> Float, ok: fn(Float) -> Bool, i: Int, n: Int, bad: Int) -> Int =
193
- sweepGen(io, |i| inputAt(i), old, new, i, n, bad)
194
-
195
- fun sweepGen(io: Io, gen: fn(Int) -> Float, old: fn(Float) -> Float, new: fn(Float) -> Float, i: Int, n: Int, bad: Int) -> Int =
196
165
  if i >= n
197
166
  bad
198
167
  else
199
168
  x := gen(i)
200
- a := old(x)
169
+ if ok(x)
201
- b := new(x)
202
- if same(a, b)
203
- sweepGen(io, gen, old, new, i + 1, n, bad)
170
+ countBad(io, gen, ok, i + 1, n, bad)
204
171
  else
205
172
  if bad < 3
206
- io.printLn(" mismatch x bits {x.toBits()}: old {a.toBits()} new {b.toBits()}")
173
+ io.printLn(" mismatch x bits {x.toBits()}")
207
- sweepGen(io, gen, old, new, i + 1, n, bad + 1)
174
+ countBad(io, gen, ok, i + 1, n, bad + 1)
208
175
 
209
176
  # Every float within 2000 ulps of +-2^e, e in -2..10: exp's reduction boundaries.
210
177
  fun expEdge(i: Int) -> Float =
@@ -253,24 +220,41 @@ fun sweep2(io: Io, old: fn(Float, Float) -> Float, new: fn(Float, Float) -> Floa
253
220
  sweep2(io, old, new, i + 1, n, bad)
254
221
  else
255
222
  if bad < 3
256
- io.printLn(" mismatch x bits {x.toBits()} y bits {y2.toBits()}: old {a.toBits()} new {b.toBits()}")
223
+ io.printLn(" mismatch x bits {x.toBits()} y bits {y2.toBits()}: reference {a.toBits()} std {b.toBits()}")
257
224
  sweep2(io, old, new, i + 1, n, bad + 1)
258
225
 
259
226
  fun report(io: Io, name: Str, n: Int, bad: Int) -> Int =
260
227
  io.printLn("{name}: inputs {n} mismatches {bad}")
261
228
  bad
262
229
 
230
+ # The specials, each `f(x)` against the exact answer: the mismatches.
231
+ fun specialsBad(io: Io) -> Int =
232
+ inf := 1.0f / 0.0f
233
+ nan := 0.0f / 0.0f
234
+ cases := [
235
+ Pair(first: math.exp(0.0f), second: 1.0f), Pair(first: math.exp(-0.0f), second: 1.0f),
236
+ Pair(first: math.exp(inf), second: inf), Pair(first: math.exp(-inf), second: 0.0f),
237
+ Pair(first: math.exp(710.0f), second: inf), Pair(first: math.exp(-746.0f), second: 0.0f),
238
+ Pair(first: math.ln(1.0f), second: 0.0f), Pair(first: math.ln(0.0f), second: -inf),
239
+ Pair(first: math.ln(-0.0f), second: -inf), Pair(first: math.ln(inf), second: inf)]
240
+ exact := cases.fold(0, |bad, c| if same(c.first, c.second) then bad else bad + 1)
241
+ nans := [math.exp(nan), math.ln(nan), math.ln(-1.0f), math.ln(-inf)].fold(0, |bad, x| if x != x then bad else bad + 1)
242
+ # exp is finite up to 709.78 and subnormal, not zero, down to -745.13
243
+ ends := [math.exp(709.78f) < inf, math.exp(-745.13f) > 0.0f, !normal(math.exp(-745.13f))].fold(0, |bad, ok| if ok then bad else bad + 1)
244
+ exact + nans + ends
245
+
263
246
  fun main(io: Io) =
264
247
  n := 2000000
265
248
  io.printLn("inputs: {kinds(0, n, 0, 0, 0, 0, 0)}")
266
- # the check finds a difference when there is one
249
+ # the check finds a difference when there is one: an exp 2^-49 too big fails the round
267
- # (counting from 3: no mismatch lines)
250
+ # trip (counting from 3: no mismatch lines)
268
- sanity := sweep1(io, |x| oldExp(x), |x| math.ln(x), 0, 1000, 3) - 3
251
+ sanity := countBad(io, |i| inputAt(i), |x| expRoundTrip(|v| math.exp(v) * {1.0f + 8.0f * Tol}, x), 0, 1000, 3) - 3
269
- io.printLn("sanity (old exp against ln): {sanity} of 1000 differ")
252
+ io.printLn("sanity (an exp 2^-49 off): {sanity} of 1000 differ")
253
+ b1 := report(io, "exp and ln specials", 17, specialsBad(io))
270
- b4 := report(io, "exp", n, sweep1(io, |x| oldExp(x), |x| math.exp(x), 0, n, 0))
254
+ b4 := report(io, "ln(exp(x))", n, countBad(io, |i| inputAt(i), |x| expRoundTrip(|v| math.exp(v), x), 0, n, 0))
271
- b5 := report(io, "exp near +-2^-2..2^10", 4001 * 26, sweepGen(io, |i| expEdge(i), |x| oldExp(x), |x| math.exp(x), 0, 4001 * 26, 0))
255
+ b5 := report(io, "ln(exp(x)) near +-2^-2..2^10", 4001 * 26, countBad(io, |i| expEdge(i), |x| expRoundTrip(|v| math.exp(v), x), 0, 4001 * 26, 0))
272
- b6 := report(io, "ln", n, sweep1(io, |x| oldLn(x), |x| math.ln(x), 0, n, 0))
256
+ b6 := report(io, "exp(ln(x))", n, countBad(io, |i| inputAt(i), |x| lnRoundTrip(x), 0, n, 0))
273
- b7 := report(io, "ln near every 2^e", 101 * 2047, sweepGen(io, |i| lnEdge(i), |x| oldLn(x), |x| math.ln(x), 0, 101 * 2047, 0))
257
+ b7 := report(io, "exp(ln(x)) near every 2^e", 101 * 2047, countBad(io, |i| lnEdge(i), |x| lnRoundTrip(x), 0, 101 * 2047, 0))
274
- b9 := report(io, "logb (against an exact reference)", n, sweep1(io, |x| refLogb(x), |x| x.logb(), 0, n, 0))
258
+ b9 := report(io, "logb (against an exact reference)", n, countBad(io, |i| inputAt(i), |x| same(refLogb(x), x.logb()), 0, n, 0))
275
- b10 := report(io, "pow", n, sweep2(io, |x, y| oldPow(x, y), |x, y| x.pow(y), 0, n, 0))
259
+ b10 := report(io, "pow (against its definition)", n, sweep2(io, |x, y| refPow(x, y), |x, y| x.pow(y), 0, n, 0))
276
- io.printLn("mismatches {b4 + b5 + b6 + b7 + b9 + b10}")
260
+ io.printLn("mismatches {b1 + b4 + b5 + b6 + b7 + b9 + b10}")