plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
3cd4692
— Peter John
2026-11-10T09:27:33+05:30
std math: series written out, Float.toBits/fromBits, ln and exp scale from the exponent bits
- benchmarks/math_bits/check.plum +313 -0
- compiler/core/src/native.rs +7 -0
- compiler/core/tests/core/purity_test.rs +6 -3
- compiler/wasm-codegen/src/calls.rs +11 -6
- compiler/wasm-codegen/src/intrinsics.rs +7 -2
- compiler/wasm-codegen/tests/wasm_codegen/native_test.rs +49 -2
- docs/std.md +2 -2
- std/Float.plum +11 -0
- std/math.plum +123 -50
- tests/core/float_bits.plum +129 -0
benchmarks/math_bits/check.plum
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Bit-identity check for std math's unrolled series and bit-based range reduction: runs 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).
|
|
5
|
+
#
|
|
6
|
+
# target/release/plum run benchmarks/math_bits/check.plum (also with --no-fbip, --no-fusion)
|
|
7
|
+
#
|
|
8
|
+
# Prints one line per function (inputs, mismatches) and ends with `mismatches 0` when every
|
|
9
|
+
# result matched. Inputs: fixed specials (zeros, the infinities, NaN, subnormals, Float.Min,
|
|
10
|
+
# Float.Max, 0.5, 700, powers of two), then per index one of: a random 64-bit pattern, a
|
|
11
|
+
# random exponent field with a random fraction, uniform in [-10, 10], [-1000, 1000] and
|
|
12
|
+
# [-1e6, 1e6], within a few ulps of k * Pi / 2, k * Pi or k * Tau, within a few ulps of a
|
|
13
|
+
# power of two (or of 0.5, 700, -700), uniform in [-1, 1] scaled by 2^-s, and subnormals.
|
|
14
|
+
import std/Int
|
|
15
|
+
import std/Float
|
|
16
|
+
import std/Str
|
|
17
|
+
import std/Bool
|
|
18
|
+
import std/Option
|
|
19
|
+
import std/Io
|
|
20
|
+
import std/List
|
|
21
|
+
import std/Slice
|
|
22
|
+
import std/Map
|
|
23
|
+
import std/math
|
|
24
|
+
|
|
25
|
+
# --- the old loop versions ---
|
|
26
|
+
|
|
27
|
+
fun oldExp(x: Float) -> Float =
|
|
28
|
+
if x != x
|
|
29
|
+
x
|
|
30
|
+
else if x > 700.0f
|
|
31
|
+
1.0f / 0.0f
|
|
32
|
+
else if x < -700.0f
|
|
33
|
+
0.0f
|
|
34
|
+
else
|
|
35
|
+
oldExpReduce(x, 0)
|
|
36
|
+
|
|
37
|
+
fun oldExpReduce(v: Float, k: Int) -> Float =
|
|
38
|
+
if v > 0.5f || v < -0.5f
|
|
39
|
+
oldExpReduce(v / 2.0f, k + 1)
|
|
40
|
+
else
|
|
41
|
+
oldSquareTimes(oldExpSeries(v, 1.0f, 1.0f, 1), k)
|
|
42
|
+
|
|
43
|
+
fun oldExpSeries(v: Float, term: Float, sum: Float, n: Int) -> Float =
|
|
44
|
+
if n < 25
|
|
45
|
+
t := term * v / Float(n)
|
|
46
|
+
oldExpSeries(v, t, sum + t, n + 1)
|
|
47
|
+
else
|
|
48
|
+
sum
|
|
49
|
+
|
|
50
|
+
fun oldSquareTimes(result: Float, k: Int) -> Float =
|
|
51
|
+
if k > 0
|
|
52
|
+
oldSquareTimes(result * result, k - 1)
|
|
53
|
+
else
|
|
54
|
+
result
|
|
55
|
+
|
|
56
|
+
fun oldLn(x: Float) -> Float =
|
|
57
|
+
if x != x || x < 0.0f
|
|
58
|
+
0.0f / 0.0f
|
|
59
|
+
else if x == 0.0f
|
|
60
|
+
-1.0f / 0.0f
|
|
61
|
+
else if x > Float.Max
|
|
62
|
+
x
|
|
63
|
+
else
|
|
64
|
+
oldLnDown(x, 0)
|
|
65
|
+
|
|
66
|
+
fun oldLnDown(v: Float, k: Int) -> Float =
|
|
67
|
+
if v >= 2.0f
|
|
68
|
+
oldLnDown(v / 2.0f, k + 1)
|
|
69
|
+
else
|
|
70
|
+
oldLnUp(v, k)
|
|
71
|
+
|
|
72
|
+
fun oldLnUp(v: Float, k: Int) -> Float =
|
|
73
|
+
if v < 1.0f
|
|
74
|
+
oldLnUp(v * 2.0f, k - 1)
|
|
75
|
+
else
|
|
76
|
+
t := {v - 1.0f} / {v + 1.0f}
|
|
77
|
+
t2 := t * t
|
|
78
|
+
sum := oldLnSeries(t2, t, t, 1)
|
|
79
|
+
Float(k) * Float.Ln2 + 2.0f * sum
|
|
80
|
+
|
|
81
|
+
fun oldLnSeries(t2: Float, term: Float, sum: Float, n: Int) -> Float =
|
|
82
|
+
if n < 30
|
|
83
|
+
t := term * t2
|
|
84
|
+
oldLnSeries(t2, t, sum + t / Float(2 * n + 1), n + 1)
|
|
85
|
+
else
|
|
86
|
+
sum
|
|
87
|
+
|
|
88
|
+
fun oldReduceToPi(x: Float) -> Float =
|
|
89
|
+
k := {x / Float.Tau + {if x >= 0.0f then 0.5f else -0.5f}}.trunc()
|
|
90
|
+
x - k * Float.Tau
|
|
91
|
+
|
|
92
|
+
fun oldSin(x: Float) -> Float =
|
|
93
|
+
if x != x || x > Float.Max || x < -Float.Max
|
|
94
|
+
0.0f / 0.0f
|
|
95
|
+
else
|
|
96
|
+
r := oldReduceToPi(x)
|
|
97
|
+
oldSinSeries(r * r, r, r, 1)
|
|
98
|
+
|
|
99
|
+
fun oldSinSeries(r2: Float, term: Float, sum: Float, n: Int) -> Float =
|
|
100
|
+
if n < 10
|
|
101
|
+
t := term * {-r2} / Float({2 * n} * {2 * n + 1})
|
|
102
|
+
oldSinSeries(r2, t, sum + t, n + 1)
|
|
103
|
+
else
|
|
104
|
+
sum
|
|
105
|
+
|
|
106
|
+
fun oldCos(x: Float) -> Float =
|
|
107
|
+
if x != x || x > Float.Max || x < -Float.Max
|
|
108
|
+
0.0f / 0.0f
|
|
109
|
+
else
|
|
110
|
+
oldSin(x + Float.HalfPi)
|
|
111
|
+
|
|
112
|
+
fun oldTan(x: Float) -> Float =
|
|
113
|
+
oldSin(x) / oldCos(x)
|
|
114
|
+
|
|
115
|
+
fun oldAtan(x: Float) -> Float =
|
|
116
|
+
if x != x
|
|
117
|
+
x
|
|
118
|
+
else
|
|
119
|
+
neg := x < 0.0f
|
|
120
|
+
v := if neg then -x else x
|
|
121
|
+
result := oldAtanReduce(v, 0)
|
|
122
|
+
if neg then -result else result
|
|
123
|
+
|
|
124
|
+
fun oldAtanReduce(v: Float, k: Int) -> Float =
|
|
125
|
+
if v > 0.5f && k < 8
|
|
126
|
+
oldAtanReduce(v / {1.0f + math.sqrt(1.0f + v * v)}, k + 1)
|
|
127
|
+
else
|
|
128
|
+
sum := oldAtanSeries(v * v, v, v, 1)
|
|
129
|
+
scale := Float(1 << k)
|
|
130
|
+
sum * scale
|
|
131
|
+
|
|
132
|
+
fun oldAtanSeries(v2: Float, term: Float, sum: Float, n: Int) -> Float =
|
|
133
|
+
if n < 20
|
|
134
|
+
t := term * {-v2}
|
|
135
|
+
oldAtanSeries(v2, t, sum + t / Float(2 * n + 1), n + 1)
|
|
136
|
+
else
|
|
137
|
+
sum
|
|
138
|
+
|
|
139
|
+
fun oldPow(base: Float, y: Float) -> Float =
|
|
140
|
+
if y == 0.0f
|
|
141
|
+
1.0f
|
|
142
|
+
else
|
|
143
|
+
yi := Int(y)
|
|
144
|
+
if Float(yi) == y
|
|
145
|
+
neg := yi < 0
|
|
146
|
+
n := if neg then -yi else yi
|
|
147
|
+
result := math.powLoop(1.0f, base, n)
|
|
148
|
+
if neg then 1.0f / result else result
|
|
149
|
+
else if base < 0.0f
|
|
150
|
+
0.0f / 0.0f
|
|
151
|
+
else
|
|
152
|
+
oldExp(y * oldLn(base))
|
|
153
|
+
|
|
154
|
+
fun oldLogb(x: Float) -> Float =
|
|
155
|
+
if x == 0.0f
|
|
156
|
+
-1.0f / 0.0f
|
|
157
|
+
else
|
|
158
|
+
{oldLn(if x < 0.0f then -x else x) * Float.Log2E}.floor()
|
|
159
|
+
|
|
160
|
+
# --- inputs ---
|
|
161
|
+
|
|
162
|
+
# splitmix64 (wrapping Int arithmetic; `>>` here only needs to mix).
|
|
163
|
+
fun mix(z0: Int) -> Int =
|
|
164
|
+
z1 := {z0 ^ {z0 >> 30}} * -4658895280553007687
|
|
165
|
+
z2 := {z1 ^ {z1 >> 27}} * -7723592293110705685
|
|
166
|
+
z2 ^ {z2 >> 31}
|
|
167
|
+
|
|
168
|
+
fun rnd(i: Int, salt: Int) -> Int =
|
|
169
|
+
mix(i * -7046029254386353131 + salt)
|
|
170
|
+
|
|
171
|
+
# Uniform in [0, 1): the top 53 bits.
|
|
172
|
+
fun unit(i: Int, salt: Int) -> Float =
|
|
173
|
+
Float({rnd(i, salt) >> 11} & 0x001F_FFFF_FFFF_FFFF) / 9007199254740992.0f
|
|
174
|
+
|
|
175
|
+
fun specials() -> List[Float] =
|
|
176
|
+
[0.0f, -0.0f, 1.0f / 0.0f, -1.0f / 0.0f, 0.0f / 0.0f, -{0.0f / 0.0f}, Float.Min, -Float.Min, Float.Max, -Float.Max, 0.5f, -0.5f, 1.0f, -1.0f, 2.0f, -2.0f, 700.0f, -700.0f, Float.fromBits(0x000F_FFFF_FFFF_FFFF), Float.fromBits(0x0010_0000_0000_0000), Float.fromBits(0x7FF0_0000_0000_0001), Float.fromBits(0x7FF8_0000_DEAD_BEEF), Float.fromBits(-1), Float.Pi, Float.HalfPi, Float.Tau, -Float.Pi, 1.0e300, 1.0e-300]
|
|
177
|
+
|
|
178
|
+
# Within `d` ulps of `x` (by bit pattern; across zero is fine).
|
|
179
|
+
fun nudge(x: Float, d: Int) -> Float =
|
|
180
|
+
Float.fromBits(x.toBits() + d)
|
|
181
|
+
|
|
182
|
+
fun input(i: Int) -> Float =
|
|
183
|
+
r := rnd(i, 1)
|
|
184
|
+
d := {rnd(i, 2) % 9}
|
|
185
|
+
sign := if r % 2 == 0 then 1.0f else -1.0f
|
|
186
|
+
c := i % 10
|
|
187
|
+
if c == 0
|
|
188
|
+
Float.fromBits(r)
|
|
189
|
+
else if c == 1
|
|
190
|
+
# every exponent field, sign and fraction random
|
|
191
|
+
Float.fromBits({{rnd(i, 3) % 2048} << 52} | {r & 0x000F_FFFF_FFFF_FFFF} | {if sign < 0.0f then Int.Min else 0})
|
|
192
|
+
else if c == 2
|
|
193
|
+
sign * unit(i, 4) * 10.0f
|
|
194
|
+
else if c == 3
|
|
195
|
+
sign * unit(i, 4) * 1000.0f
|
|
196
|
+
else if c == 4
|
|
197
|
+
sign * unit(i, 4) * 1000000.0f
|
|
198
|
+
else if c == 5
|
|
199
|
+
k := Float(rnd(i, 5) % 2000)
|
|
200
|
+
m := rnd(i, 6) % 3
|
|
201
|
+
base := if m == 0 then k * Float.HalfPi else if m == 1 then k * Float.Pi else k * Float.Tau
|
|
202
|
+
nudge(sign * base, d)
|
|
203
|
+
else if c == 6
|
|
204
|
+
# near a power of two, or 0.5 / 700
|
|
205
|
+
m := rnd(i, 7) % 4
|
|
206
|
+
base := if m == 0 then Float.fromBits({{rnd(i, 3) % 2047} << 52}) else if m == 1 then 0.5f else if m == 2 then 700.0f else Float.fromBits({{1023 + rnd(i, 3) % 12} << 52})
|
|
207
|
+
nudge(sign * base, d)
|
|
208
|
+
else if c == 7
|
|
209
|
+
sign * unit(i, 4) * Float.fromBits({1023 - rnd(i, 8) % 64} << 52)
|
|
210
|
+
else if c == 8
|
|
211
|
+
Float.fromBits({r & 0x000F_FFFF_FFFF_FFFF} | {if sign < 0.0f then Int.Min else 0})
|
|
212
|
+
else
|
|
213
|
+
# whole and half numbers, small and large
|
|
214
|
+
sign * Float(rnd(i, 9) % 100000) / {if d % 2 == 0 then 1.0f else 2.0f}
|
|
215
|
+
|
|
216
|
+
fun inputAt(i: Int) -> Float =
|
|
217
|
+
sp := specials()
|
|
218
|
+
if i < sp.length() then sp.get(i).unwrap() else input(i)
|
|
219
|
+
|
|
220
|
+
# --- the sweep ---
|
|
221
|
+
|
|
222
|
+
fun same(a: Float, b: Float) -> Bool =
|
|
223
|
+
a.toBits() == b.toBits()
|
|
224
|
+
|
|
225
|
+
fun sweep1(io: Io, old: fn(Float) -> Float, new: fn(Float) -> Float, i: Int, n: Int, bad: Int) -> Int =
|
|
226
|
+
sweepGen(io, |i| inputAt(i), old, new, i, n, bad)
|
|
227
|
+
|
|
228
|
+
fun sweepGen(io: Io, gen: fn(Int) -> Float, old: fn(Float) -> Float, new: fn(Float) -> Float, i: Int, n: Int, bad: Int) -> Int =
|
|
229
|
+
if i >= n
|
|
230
|
+
bad
|
|
231
|
+
else
|
|
232
|
+
x := gen(i)
|
|
233
|
+
a := old(x)
|
|
234
|
+
b := new(x)
|
|
235
|
+
if same(a, b)
|
|
236
|
+
sweepGen(io, gen, old, new, i + 1, n, bad)
|
|
237
|
+
else
|
|
238
|
+
if bad < 3
|
|
239
|
+
io.printLn(" mismatch x bits {x.toBits()}: old {a.toBits()} new {b.toBits()}")
|
|
240
|
+
sweepGen(io, gen, old, new, i + 1, n, bad + 1)
|
|
241
|
+
|
|
242
|
+
# Every float within 2000 ulps of +-2^e, e in -2..10: exp's reduction boundaries.
|
|
243
|
+
fun expEdge(i: Int) -> Float =
|
|
244
|
+
d := i % 4001 - 2000
|
|
245
|
+
e := {i / 4001} % 13 - 2
|
|
246
|
+
sign := if i / {4001 * 13} == 0 then 0 else Int.Min
|
|
247
|
+
Float.fromBits({{{1023 + e} << 52} | sign} + d)
|
|
248
|
+
|
|
249
|
+
# Every float within 50 ulps of 2^e for every exponent field 0..2046 (subnormals and the
|
|
250
|
+
# normal/subnormal boundary included): ln's reduction boundaries.
|
|
251
|
+
fun lnEdge(i: Int) -> Float =
|
|
252
|
+
d := i % 101 - 50
|
|
253
|
+
e := i / 101
|
|
254
|
+
Float.fromBits({e << 52} + d)
|
|
255
|
+
|
|
256
|
+
# Counts of the main inputs by kind: zero, subnormal, normal, infinite, NaN.
|
|
257
|
+
fun kinds(i: Int, n: Int, z: Int, s: Int, nm: Int, inf: Int, nan: Int) -> Str =
|
|
258
|
+
if i >= n
|
|
259
|
+
"zero {z} subnormal {s} normal {nm} infinite {inf} NaN {nan}"
|
|
260
|
+
else
|
|
261
|
+
x := inputAt(i)
|
|
262
|
+
e := {x.toBits() >> 52} & 0x7FF
|
|
263
|
+
f := x.toBits() & 0x000F_FFFF_FFFF_FFFF
|
|
264
|
+
if e == 0 && f == 0
|
|
265
|
+
kinds(i + 1, n, z + 1, s, nm, inf, nan)
|
|
266
|
+
else if e == 0
|
|
267
|
+
kinds(i + 1, n, z, s + 1, nm, inf, nan)
|
|
268
|
+
else if e < 2047
|
|
269
|
+
kinds(i + 1, n, z, s, nm + 1, inf, nan)
|
|
270
|
+
else if f == 0
|
|
271
|
+
kinds(i + 1, n, z, s, nm, inf + 1, nan)
|
|
272
|
+
else
|
|
273
|
+
kinds(i + 1, n, z, s, nm, inf, nan + 1)
|
|
274
|
+
|
|
275
|
+
fun sweep2(io: Io, old: fn(Float, Float) -> Float, new: fn(Float, Float) -> Float, i: Int, n: Int, bad: Int) -> Int =
|
|
276
|
+
if i >= n
|
|
277
|
+
bad
|
|
278
|
+
else
|
|
279
|
+
x := inputAt(i)
|
|
280
|
+
y := inputAt(rnd(i, 11) % n)
|
|
281
|
+
# small exponents too, where the fractional-power path runs exp(y * ln(x))
|
|
282
|
+
y2 := if i % 2 == 0 then y else {unit(i, 12) - 0.5f} * 20.0f
|
|
283
|
+
a := old(x, y2)
|
|
284
|
+
b := new(x, y2)
|
|
285
|
+
if same(a, b)
|
|
286
|
+
sweep2(io, old, new, i + 1, n, bad)
|
|
287
|
+
else
|
|
288
|
+
if bad < 3
|
|
289
|
+
io.printLn(" mismatch x bits {x.toBits()} y bits {y2.toBits()}: old {a.toBits()} new {b.toBits()}")
|
|
290
|
+
sweep2(io, old, new, i + 1, n, bad + 1)
|
|
291
|
+
|
|
292
|
+
fun report(io: Io, name: Str, n: Int, bad: Int) -> Int =
|
|
293
|
+
io.printLn("{name}: inputs {n} mismatches {bad}")
|
|
294
|
+
bad
|
|
295
|
+
|
|
296
|
+
fun main(io: Io) =
|
|
297
|
+
n := 2000000
|
|
298
|
+
io.printLn("inputs: {kinds(0, n, 0, 0, 0, 0, 0)}")
|
|
299
|
+
# the check finds a difference when there is one
|
|
300
|
+
# (counting from 3: no mismatch lines)
|
|
301
|
+
sanity := sweep1(io, |x| oldSin(x), |x| math.cos(x), 0, 1000, 3) - 3
|
|
302
|
+
io.printLn("sanity (old sin against cos): {sanity} of 1000 differ")
|
|
303
|
+
b1 := report(io, "sin", n, sweep1(io, |x| oldSin(x), |x| math.sin(x), 0, n, 0))
|
|
304
|
+
b2 := report(io, "cos", n, sweep1(io, |x| oldCos(x), |x| math.cos(x), 0, n, 0))
|
|
305
|
+
b3 := report(io, "tan", n, sweep1(io, |x| oldTan(x), |x| math.tan(x), 0, n, 0))
|
|
306
|
+
b4 := report(io, "exp", n, sweep1(io, |x| oldExp(x), |x| math.exp(x), 0, n, 0))
|
|
307
|
+
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))
|
|
308
|
+
b6 := report(io, "ln", n, sweep1(io, |x| oldLn(x), |x| math.ln(x), 0, n, 0))
|
|
309
|
+
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))
|
|
310
|
+
b8 := report(io, "atan", n, sweep1(io, |x| oldAtan(x), |x| math.atan(x), 0, n, 0))
|
|
311
|
+
b9 := report(io, "logb", n, sweep1(io, |x| oldLogb(x), |x| x.logb(), 0, n, 0))
|
|
312
|
+
b10 := report(io, "pow", n, sweep2(io, |x, y| oldPow(x, y), |x, y| x.pow(y), 0, n, 0))
|
|
313
|
+
io.printLn("mismatches {b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9 + b10}")
|
compiler/core/src/native.rs
CHANGED
|
@@ -21,6 +21,11 @@ pub enum NativeOp {
|
|
|
21
21
|
I64Clz,
|
|
22
22
|
I64Ctz,
|
|
23
23
|
I64Popcnt,
|
|
24
|
+
/// `i64.reinterpret_f64`: a Float's 64 IEEE 754 bits as an Int, NaN payloads and the sign
|
|
25
|
+
/// of zero included.
|
|
26
|
+
I64ReinterpretF64,
|
|
27
|
+
/// `f64.reinterpret_i64`: the Float whose bits are the Int's 64-bit pattern.
|
|
28
|
+
F64ReinterpretI64,
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
/// `(file, receiver type, fn name, op)`: `None` receiver is a module-level fn.
|
|
@@ -34,6 +39,8 @@ const TABLE: &[(&str, Option<&str>, &str, NativeOp)] = &[
|
|
|
34
39
|
("std/Int.plum", Some("Int"), "leadingZeros", NativeOp::I64Clz),
|
|
35
40
|
("std/Int.plum", Some("Int"), "trailingZeros", NativeOp::I64Ctz),
|
|
36
41
|
("std/Int.plum", Some("Int"), "popCount", NativeOp::I64Popcnt),
|
|
42
|
+
("std/Float.plum", Some("Float"), "toBits", NativeOp::I64ReinterpretF64),
|
|
43
|
+
("std/Float.plum", Some("Float"), "fromBits", NativeOp::F64ReinterpretI64),
|
|
37
44
|
];
|
|
38
45
|
|
|
39
46
|
/// The native op `f` compiles to: one of std's fns in `TABLE`, under any module rename, whose
|
compiler/core/tests/core/purity_test.rs
CHANGED
|
@@ -165,12 +165,15 @@ fn std_native_ops_are_pure() {
|
|
|
165
165
|
let src = "import std/Int\nimport std/Float\nimport std/math\n\n\
|
|
166
166
|
fun len(x: Float, y: Float) -> Float = math.sqrt(x * x + y * y)\n\n\
|
|
167
167
|
fun cell(x: Float) -> Int = Int(x.floor()) + Int(x.abs().ceil())\n\n\
|
|
168
|
-
fun ones(n: Int) -> Int = n.popCount() + n.leadingZeros()\n
|
|
168
|
+
fun ones(n: Int) -> Int = n.popCount() + n.leadingZeros()\n\n\
|
|
169
|
+
fun flip(x: Float) -> Float = Float.fromBits(x.toBits() ^ Int.Min)\n";
|
|
169
170
|
let source = plum_test_support::load("native_pure", src).unwrap_or_else(|e| panic!("load: {e}"));
|
|
170
171
|
let pure = plum_core::purity::pureFns(&source);
|
|
171
172
|
let named = |n: &str| pure.iter().any(|k| k == n || plum_core::modules::sourceFnName(k) == n);
|
|
172
|
-
for n in ["len", "cell", "ones", "Float.floor", "Float.abs", "Int.popCount"] {
|
|
173
|
+
for n in ["len", "cell", "ones", "flip", "Float.floor", "Float.abs", "Int.popCount", "Float.toBits", "Float.fromBits"] {
|
|
173
174
|
assert!(named(n), "{n} not pure: {pure:?}");
|
|
174
175
|
}
|
|
176
|
+
for n in ["hypot", "ln", "exp"] {
|
|
175
|
-
|
|
177
|
+
assert!(pure.iter().any(|k| plum_core::modules::sourceFnName(k) == n), "{n} not pure: {pure:?}");
|
|
178
|
+
}
|
|
176
179
|
}
|
compiler/wasm-codegen/src/calls.rs
CHANGED
|
@@ -337,15 +337,20 @@ pub(crate) fn compileMethodCallAs(
|
|
|
337
337
|
// Plum source declares a `self` param (see `fnWasmParamTypes`), so something of
|
|
338
338
|
// the right type must still be pushed — the body simply never reads it.
|
|
339
339
|
let is_static_call = matches!(object, ast::Expr { kind: ast::ExprKind::TypeName(n), .. } if *n == class_name || typeParamOwner(n, ctx));
|
|
340
|
+
// A static native op (`Float.fromBits`) is its instruction on the arguments alone: no
|
|
341
|
+
// self slot.
|
|
342
|
+
let native = withGen(|s| s.native_fns.contains_key(&func_idx));
|
|
340
343
|
if is_static_call {
|
|
344
|
+
if !native {
|
|
341
|
-
|
|
345
|
+
match ctx.func_sigs.get(&key) {
|
|
342
|
-
|
|
346
|
+
Some(s) if !s.params.is_empty() => {
|
|
343
|
-
|
|
347
|
+
let n = leafCount(&PlumType::TNamed(class_name.clone())) as usize;
|
|
344
|
-
|
|
348
|
+
for vt in s.params.iter().take(n) {
|
|
345
|
-
|
|
349
|
+
pushSelfPlaceholder(*vt, body);
|
|
350
|
+
}
|
|
346
351
|
}
|
|
352
|
+
_ => pushSelfPlaceholder(ValType::I32, body),
|
|
347
353
|
}
|
|
348
|
-
_ => pushSelfPlaceholder(ValType::I32, body),
|
|
349
354
|
}
|
|
350
355
|
} else {
|
|
351
356
|
compileExpr(object, body, ctx, state)?; // push self
|
compiler/wasm-codegen/src/intrinsics.rs
CHANGED
|
@@ -94,11 +94,14 @@ pub(crate) fn castToEq(vt: &ValType, body: &mut Vec<u8>) {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
/// The body of a std native op (`plum_core::native`), for a call that is not in line (the fn
|
|
97
|
-
/// passed as a value): its parameters, each one wasm local, then the instruction.
|
|
97
|
+
/// passed as a value): its parameters, each one wasm local, then the instruction. A self-less
|
|
98
|
+
/// one in a type (`Float.fromBits`) still has the type's leading self slot (one local: the
|
|
99
|
+
/// receivers are `Int` and `Float`), which it skips.
|
|
98
100
|
pub(crate) fn compileNativeFnBody(f: &ast::Fn, op: plum_core::native::NativeOp) -> Vec<u8> {
|
|
99
101
|
let mut body = vec![0u8];
|
|
102
|
+
let skip = u32::from(f.type_param.is_some() && !f.has_self);
|
|
100
103
|
let n = f.params.len() as u32 + u32::from(f.has_self);
|
|
101
|
-
for local in
|
|
104
|
+
for local in skip..skip + n {
|
|
102
105
|
Instruction::LocalGet(local).encode(&mut body);
|
|
103
106
|
}
|
|
104
107
|
emitNativeOp(op, &mut body);
|
|
@@ -118,6 +121,8 @@ pub(crate) fn emitNativeOp(op: plum_core::native::NativeOp, body: &mut Vec<u8>)
|
|
|
118
121
|
N::I64Clz => Instruction::I64Clz,
|
|
119
122
|
N::I64Ctz => Instruction::I64Ctz,
|
|
120
123
|
N::I64Popcnt => Instruction::I64Popcnt,
|
|
124
|
+
N::I64ReinterpretF64 => Instruction::I64ReinterpretF64,
|
|
125
|
+
N::F64ReinterpretI64 => Instruction::F64ReinterpretI64,
|
|
121
126
|
}
|
|
122
127
|
.encode(body);
|
|
123
128
|
}
|
compiler/wasm-codegen/tests/wasm_codegen/native_test.rs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#![allow(non_snake_case)]
|
|
2
2
|
//! std's native ops (`plum_core::native`): a direct call of `math.sqrt`, `Float.sqrt`,
|
|
3
|
-
//! `abs`, `floor`, `ceil`, `trunc`
|
|
3
|
+
//! `abs`, `floor`, `ceil`, `trunc`, `Int.leadingZeros`/`trailingZeros`/`popCount` or
|
|
4
|
+
//! `Float.toBits`/`fromBits` is the one wasm instruction in line, not a `call` and not a
|
|
4
|
-
//!
|
|
5
|
+
//! loop. Checked with and without FBIP.
|
|
5
6
|
use std::collections::HashMap;
|
|
6
7
|
|
|
7
8
|
/// Per compiled function (by debug name): its operators' names.
|
|
@@ -115,3 +116,49 @@ fn native_ops_compile_in_line() {
|
|
|
115
116
|
assert_eq!(runInt(&wasm), 9 * 1000000000000 + 3950 * 1000000 + 580302, "fbip {fbip}");
|
|
116
117
|
}
|
|
117
118
|
}
|
|
119
|
+
|
|
120
|
+
const BITS_SRC: &str = "import std/Int
|
|
121
|
+
import std/Float
|
|
122
|
+
import std/math
|
|
123
|
+
|
|
124
|
+
fun bitsOf(x: Float) -> Int =
|
|
125
|
+
x.toBits()
|
|
126
|
+
|
|
127
|
+
fun ofBits(b: Int) -> Float =
|
|
128
|
+
Float.fromBits(b)
|
|
129
|
+
|
|
130
|
+
fun series(x: Float) -> Float =
|
|
131
|
+
math.sin(x) + math.cos(x) + math.exp(x) + math.ln(x) + math.atan(x)
|
|
132
|
+
|
|
133
|
+
fun main() -> Int =
|
|
134
|
+
ofBits(bitsOf(-0.0) + 1).toBits() + Int(series(0.5) * 1000.0)
|
|
135
|
+
";
|
|
136
|
+
|
|
137
|
+
#[test]
|
|
138
|
+
fn float_bits_compile_to_reinterprets_and_series_have_no_loop() {
|
|
139
|
+
for fbip in [true, false] {
|
|
140
|
+
let wasm = compile("float_bits", BITS_SRC, fbip);
|
|
141
|
+
let ops = operators(&wasm);
|
|
142
|
+
let f = |n: &str| ops.iter().find(|(k, _)| k.as_str() == n || k.ends_with(&format!(".{n}"))).map(|e| e.1).unwrap_or_else(|| panic!("no fn {n}: {:?}", ops.keys().collect::<Vec<_>>()));
|
|
143
|
+
let b = f("bitsOf");
|
|
144
|
+
assert_eq!(count(b, "I64ReinterpretF64"), 1, "{b:?}");
|
|
145
|
+
assert_eq!(count(b, "Call") + count(b, "ReturnCall"), 0, "{b:?}");
|
|
146
|
+
// a static native op pushes no self placeholder
|
|
147
|
+
let o = f("ofBits");
|
|
148
|
+
assert_eq!(count(o, "F64ReinterpretI64"), 1, "{o:?}");
|
|
149
|
+
assert_eq!(count(o, "F64Const") + count(o, "Call") + count(o, "ReturnCall"), 0, "{o:?}");
|
|
150
|
+
// ln reads its exponent from the bits; the series are written out (no loop, no call,
|
|
151
|
+
// no int-to-float conversion)
|
|
152
|
+
// (twice: a subnormal is scaled by 2^54 first)
|
|
153
|
+
assert_eq!(count(f("ln"), "I64ReinterpretF64"), 2, "{:?}", f("ln"));
|
|
154
|
+
assert_eq!(count(f("ln"), "Loop"), 0, "{:?}", f("ln"));
|
|
155
|
+
assert_eq!(count(f("lnBits"), "F64ReinterpretI64"), 1, "{:?}", f("lnBits"));
|
|
156
|
+
for s in ["sinSeries", "expSeries", "lnSeries", "atanSeries"] {
|
|
157
|
+
let ops = f(s);
|
|
158
|
+
assert_eq!(count(ops, "Call") + count(ops, "ReturnCall") + count(ops, "Loop") + count(ops, "F64ConvertI64S"), 0, "{s}: {ops:?}");
|
|
159
|
+
}
|
|
160
|
+
// -0.0 bits + 1 is -Float.Min, whose bits come back; plus a Float sum
|
|
161
|
+
let sum = 0.479425538604203 + 0.8775825618903728 + 1.6487212707001282 - 0.6931471805599453 + 0.4636476090008061;
|
|
162
|
+
assert_eq!(runInt(&wasm), i64::MIN + 1 + (sum * 1000.0) as i64, "fbip {fbip}");
|
|
163
|
+
}
|
|
164
|
+
}
|
docs/std.md
CHANGED
|
@@ -35,10 +35,10 @@ fun main() =
|
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
- `Int`: `abs`, `sign`, `min`, `max`, `pow`, `toStr`, `Int.parse`, `Int.Min`, `Int.Max`, and on the 64-bit pattern `leadingZeros`, `trailingZeros`, `popCount` (64 for 0)
|
|
38
|
-
- `Float`: `abs`, `sqrt`, `pow`, `floor`, `ceil`, `round`, `trunc`, `isNaN`, `toStr`, `Float.parse`, `Float.Pi`, `Float.Tau`, `Float.E`
|
|
38
|
+
- `Float`: `abs`, `sqrt`, `pow`, `floor`, `ceil`, `round`, `trunc`, `isNaN`, `toStr`, `Float.parse`, `Float.Pi`, `Float.Tau`, `Float.E`, and `toBits` / `Float.fromBits` between a Float and its 64 IEEE 754 bits as an Int
|
|
39
39
|
- `math`: `math.sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `exp`, `ln`, `hypot`, `cbrt`
|
|
40
40
|
|
|
41
|
-
Where wasm has the operation, std uses its instruction, compiled in line at each call: `math.sqrt` and `Float.sqrt` are `f64.sqrt`, correctly rounded; `abs`, `floor`, `ceil` and `trunc` are `f64.abs`, `f64.floor`, `f64.ceil` and `f64.trunc`; `leadingZeros`, `trailingZeros` and `popCount` are `i64.clz`, `i64.ctz` and `i64.popcnt`. These are exact for every Float: NaN and the infinities pass through, a value past `Int`'s range comes back whole as it was, and a zero keeps its sign (`{-0.5}.floor()` is `-1.0`, `{-0.5}.ceil()` is `-0.0`, `{-0.0}.abs()` is `0.0`). `round` is half away from zero and exact too (`0.49999999999999994.round()` is `0.0`). `math.hypot(a, b)` is `sqrt(a * a + b * b)` unless that sum overflows or underflows, then it scales: `hypot(3e200, 4e200)` is about `5e200`, and an infinite side gives Infinity even next to a NaN. `min` and `max` stay comparisons: `x.min(y)` is `if x < y then x else y`, so with a NaN it returns `y`. `sin`, `cos`, `exp`, `ln` and the rest have no wasm instruction and are Plum code made of exact IEEE operations, so they give the same bits on every host.
|
|
41
|
+
Where wasm has the operation, std uses its instruction, compiled in line at each call: `math.sqrt` and `Float.sqrt` are `f64.sqrt`, correctly rounded; `abs`, `floor`, `ceil` and `trunc` are `f64.abs`, `f64.floor`, `f64.ceil` and `f64.trunc`; `leadingZeros`, `trailingZeros` and `popCount` are `i64.clz`, `i64.ctz` and `i64.popcnt`; `x.toBits()` and `Float.fromBits(b)` are `i64.reinterpret_f64` and `f64.reinterpret_i64`, so a NaN keeps its payload and `{-0.0}.toBits()` is `Int.Min`. These are exact for every Float: NaN and the infinities pass through, a value past `Int`'s range comes back whole as it was, and a zero keeps its sign (`{-0.5}.floor()` is `-1.0`, `{-0.5}.ceil()` is `-0.0`, `{-0.0}.abs()` is `0.0`). `round` is half away from zero and exact too (`0.49999999999999994.round()` is `0.0`). `math.hypot(a, b)` is `sqrt(a * a + b * b)` unless that sum overflows or underflows, then it scales: `hypot(3e200, 4e200)` is about `5e200`, and an infinite side gives Infinity even next to a NaN. `min` and `max` stay comparisons: `x.min(y)` is `if x < y then x else y`, so with a NaN it returns `y`. `sin`, `cos`, `exp`, `ln` and the rest have no wasm instruction and are Plum code made of exact IEEE operations, so they give the same bits on every host. Their series are written out term by term (no loop), and `ln` reads the power of two of its argument from the exponent bits instead of halving or doubling it, as `exp` does for the scaling into [-0.5, 0.5]; `benchmarks/math_bits/check.plum` checks that these give the same bits as the loop versions did.
|
|
42
42
|
- `Bool`: `toStr`, `Bool.parse`
|
|
43
43
|
|
|
44
44
|
## Text
|
std/Float.plum
CHANGED
|
@@ -73,6 +73,17 @@ primitive Float =
|
|
|
73
73
|
value := whole + frac
|
|
74
74
|
Ok(if neg then -value else value)
|
|
75
75
|
|
|
76
|
+
# The 64 IEEE 754 bits of `self` as an Int (wasm's `i64.reinterpret_f64`): the sign bit is
|
|
77
|
+
# bit 63, then 11 exponent bits, then 52 fraction bits. `{-0.0}.toBits()` is `Int.Min`, a
|
|
78
|
+
# NaN keeps its payload.
|
|
79
|
+
fun toBits(self) -> Int =
|
|
80
|
+
todo
|
|
81
|
+
|
|
82
|
+
# The Float whose 64 IEEE 754 bits are `bits` (wasm's `f64.reinterpret_i64`), the inverse of
|
|
83
|
+
# `toBits`: `Float.fromBits(x.toBits())` has the same bits as `x`, NaNs included.
|
|
84
|
+
fun fromBits(bits: Int) -> Float =
|
|
85
|
+
todo
|
|
86
|
+
|
|
76
87
|
# wasm's `f64.abs`: clears the sign bit, so `{-0.0}.abs()` is `0.0`.
|
|
77
88
|
fun abs(self) -> Float =
|
|
78
89
|
todo
|
std/math.plum
CHANGED
|
@@ -32,9 +32,9 @@ fun powLoop(result: Float, b: Float, m: Int) -> Float =
|
|
|
32
32
|
result
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
# Natural exponential, e^x, via range reduction (
|
|
35
|
+
# Natural exponential, e^x, via range reduction (x / 2^k with the least k that puts it in
|
|
36
|
+
# [-0.5, 0.5], an exact scaling read from x's exponent bits), the Taylor series there, where
|
|
36
|
-
#
|
|
37
|
+
# it converges fast, then the result squared back up k times.
|
|
37
|
-
# result back up the same number of halvings).
|
|
38
38
|
fun exp(x: Float) -> Float =
|
|
39
39
|
if x != x
|
|
40
40
|
x
|
|
@@ -43,20 +43,46 @@ fun exp(x: Float) -> Float =
|
|
|
43
43
|
else if x < -700.0f
|
|
44
44
|
0.0f
|
|
45
45
|
else
|
|
46
|
-
expReduce(x
|
|
46
|
+
expReduce(x)
|
|
47
47
|
|
|
48
|
+
# `|v| <= 700`. Halving a v above 0.5 is exact, so v / 2^k is v times the power of two
|
|
49
|
+
# 2^-k: k is e + 1 for |v| = 2^e, else e + 2 for 2^e < |v| < 2^(e+1).
|
|
48
|
-
fun expReduce(v: Float
|
|
50
|
+
fun expReduce(v: Float) -> Float =
|
|
49
51
|
if v > 0.5f || v < -0.5f
|
|
52
|
+
b := v.toBits() & 0x7FFF_FFFF_FFFF_FFFF
|
|
50
|
-
|
|
53
|
+
e := {b >> 52} - 1023
|
|
54
|
+
n := if {b & 0x000F_FFFF_FFFF_FFFF} == 0 then e + 1 else e + 2
|
|
55
|
+
squareTimes(expSeries(v * Float.fromBits({1023 - n} << 52)), n)
|
|
51
56
|
else
|
|
52
|
-
|
|
57
|
+
expSeries(v)
|
|
53
58
|
|
|
59
|
+
# The Taylor series of e^v to the v^24 term, in order: term n is term n-1 times v over n.
|
|
54
|
-
fun expSeries(v: Float
|
|
60
|
+
fun expSeries(v: Float) -> Float =
|
|
55
|
-
|
|
61
|
+
t1 := v
|
|
56
|
-
|
|
62
|
+
t2 := t1 * v / 2.0f
|
|
57
|
-
|
|
63
|
+
t3 := t2 * v / 3.0f
|
|
58
|
-
|
|
64
|
+
t4 := t3 * v / 4.0f
|
|
59
|
-
|
|
65
|
+
t5 := t4 * v / 5.0f
|
|
66
|
+
t6 := t5 * v / 6.0f
|
|
67
|
+
t7 := t6 * v / 7.0f
|
|
68
|
+
t8 := t7 * v / 8.0f
|
|
69
|
+
t9 := t8 * v / 9.0f
|
|
70
|
+
t10 := t9 * v / 10.0f
|
|
71
|
+
t11 := t10 * v / 11.0f
|
|
72
|
+
t12 := t11 * v / 12.0f
|
|
73
|
+
t13 := t12 * v / 13.0f
|
|
74
|
+
t14 := t13 * v / 14.0f
|
|
75
|
+
t15 := t14 * v / 15.0f
|
|
76
|
+
t16 := t15 * v / 16.0f
|
|
77
|
+
t17 := t16 * v / 17.0f
|
|
78
|
+
t18 := t17 * v / 18.0f
|
|
79
|
+
t19 := t18 * v / 19.0f
|
|
80
|
+
t20 := t19 * v / 20.0f
|
|
81
|
+
t21 := t20 * v / 21.0f
|
|
82
|
+
t22 := t21 * v / 22.0f
|
|
83
|
+
t23 := t22 * v / 23.0f
|
|
84
|
+
t24 := t23 * v / 24.0f
|
|
85
|
+
1.0f + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15 + t16 + t17 + t18 + t19 + t20 + t21 + t22 + t23 + t24
|
|
60
86
|
|
|
61
87
|
fun squareTimes(result: Float, k: Int) -> Float =
|
|
62
88
|
if k > 0
|
|
@@ -64,8 +90,8 @@ fun squareTimes(result: Float, k: Int) -> Float =
|
|
|
64
90
|
else
|
|
65
91
|
result
|
|
66
92
|
|
|
67
|
-
# Natural logarithm, via range reduction to v in [1, 2)
|
|
93
|
+
# Natural logarithm, via range reduction to x = v * 2^k with v in [1, 2) (k and v read from
|
|
68
|
-
# fast-converging series ln(v) = 2*atanh((v-1)/(v+1)).
|
|
94
|
+
# x's bits) plus the fast-converging series ln(v) = 2*atanh((v-1)/(v+1)).
|
|
69
95
|
fun ln(x: Float) -> Float =
|
|
70
96
|
if x != x || x < 0.0f
|
|
71
97
|
0.0f / 0.0f
|
|
@@ -74,29 +100,53 @@ fun ln(x: Float) -> Float =
|
|
|
74
100
|
else if x > Float.Max
|
|
75
101
|
x
|
|
76
102
|
else
|
|
77
|
-
|
|
103
|
+
b := x.toBits()
|
|
78
|
-
|
|
79
|
-
fun lnDown(v: Float, k: Int) -> Float =
|
|
80
|
-
|
|
104
|
+
if {b >> 52} == 0
|
|
105
|
+
# a subnormal: times 2^54 (exact) it is a normal float
|
|
81
|
-
|
|
106
|
+
lnBits({x * 18014398509481984.0f}.toBits(), -54)
|
|
82
|
-
|
|
107
|
+
else
|
|
83
|
-
|
|
108
|
+
lnBits(b, 0)
|
|
84
109
|
|
|
110
|
+
# ln(f) + k * ln 2, f the positive normal float with bits `b`: f is v * 2^(e - 1023), e its
|
|
111
|
+
# exponent field and v in [1, 2) its fraction under exponent field 1023.
|
|
85
|
-
fun
|
|
112
|
+
fun lnBits(b: Int, k: Int) -> Float =
|
|
86
|
-
if v < 1.0f
|
|
87
|
-
|
|
113
|
+
v := Float.fromBits({b & 0x000F_FFFF_FFFF_FFFF} | 0x3FF0_0000_0000_0000)
|
|
88
|
-
else
|
|
89
|
-
|
|
114
|
+
t := {v - 1.0f} / {v + 1.0f}
|
|
90
|
-
|
|
115
|
+
t2 := t * t
|
|
91
|
-
sum := lnSeries(t2, t, t, 1)
|
|
92
|
-
|
|
116
|
+
Float(k + {b >> 52} - 1023) * Float.Ln2 + 2.0f * lnSeries(t2, t)
|
|
93
117
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
118
|
+
# t + t^3/3 + t^5/5 + ... + t^59/59, in order: each power is the one before times t2.
|
|
119
|
+
fun lnSeries(t2: Float, t: Float) -> Float =
|
|
120
|
+
p3 := t * t2
|
|
121
|
+
p5 := p3 * t2
|
|
122
|
+
p7 := p5 * t2
|
|
123
|
+
p9 := p7 * t2
|
|
124
|
+
p11 := p9 * t2
|
|
125
|
+
p13 := p11 * t2
|
|
126
|
+
p15 := p13 * t2
|
|
127
|
+
p17 := p15 * t2
|
|
128
|
+
p19 := p17 * t2
|
|
129
|
+
p21 := p19 * t2
|
|
130
|
+
p23 := p21 * t2
|
|
131
|
+
p25 := p23 * t2
|
|
132
|
+
p27 := p25 * t2
|
|
133
|
+
p29 := p27 * t2
|
|
134
|
+
p31 := p29 * t2
|
|
135
|
+
p33 := p31 * t2
|
|
136
|
+
p35 := p33 * t2
|
|
137
|
+
p37 := p35 * t2
|
|
138
|
+
p39 := p37 * t2
|
|
139
|
+
p41 := p39 * t2
|
|
140
|
+
p43 := p41 * t2
|
|
141
|
+
p45 := p43 * t2
|
|
142
|
+
p47 := p45 * t2
|
|
143
|
+
p49 := p47 * t2
|
|
144
|
+
p51 := p49 * t2
|
|
145
|
+
p53 := p51 * t2
|
|
146
|
+
p55 := p53 * t2
|
|
147
|
+
p57 := p55 * t2
|
|
148
|
+
p59 := p57 * t2
|
|
149
|
+
t + p3 / 3.0f + p5 / 5.0f + p7 / 7.0f + p9 / 9.0f + p11 / 11.0f + p13 / 13.0f + p15 / 15.0f + p17 / 17.0f + p19 / 19.0f + p21 / 21.0f + p23 / 23.0f + p25 / 25.0f + p27 / 27.0f + p29 / 29.0f + p31 / 31.0f + p33 / 33.0f + p35 / 35.0f + p37 / 37.0f + p39 / 39.0f + p41 / 41.0f + p43 / 43.0f + p45 / 45.0f + p47 / 47.0f + p49 / 49.0f + p51 / 51.0f + p53 / 53.0f + p55 / 55.0f + p57 / 57.0f + p59 / 59.0f
|
|
100
150
|
|
|
101
151
|
# Square root: wasm's `f64.sqrt`, correctly rounded (IEEE 754). `sqrt(-0.0)` is `-0.0`, a
|
|
102
152
|
# negative `x` or NaN gives NaN, `sqrt(Infinity)` is Infinity.
|
|
@@ -118,14 +168,21 @@ fun sin(x: Float) -> Float =
|
|
|
118
168
|
0.0f / 0.0f
|
|
119
169
|
else
|
|
120
170
|
r := reduceToPi(x)
|
|
121
|
-
sinSeries(r
|
|
171
|
+
sinSeries(r, -{r * r})
|
|
122
172
|
|
|
173
|
+
# The Taylor series of sin(r) to the r^19 term, in order: term n is term n-1 times -r^2 over
|
|
174
|
+
# 2n(2n+1).
|
|
123
|
-
fun sinSeries(
|
|
175
|
+
fun sinSeries(r: Float, m: Float) -> Float =
|
|
124
|
-
|
|
176
|
+
t1 := r * m / 6.0f
|
|
177
|
+
t2 := t1 * m / 20.0f
|
|
178
|
+
t3 := t2 * m / 42.0f
|
|
179
|
+
t4 := t3 * m / 72.0f
|
|
180
|
+
t5 := t4 * m / 110.0f
|
|
181
|
+
t6 := t5 * m / 156.0f
|
|
182
|
+
t7 := t6 * m / 210.0f
|
|
183
|
+
t8 := t7 * m / 272.0f
|
|
184
|
+
t9 := t8 * m / 342.0f
|
|
125
|
-
|
|
185
|
+
r + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9
|
|
126
|
-
sinSeries(r2, t, sum + t, n + 1)
|
|
127
|
-
else
|
|
128
|
-
sum
|
|
129
186
|
|
|
130
187
|
fun cos(x: Float) -> Float =
|
|
131
188
|
if x != x || x > Float.Max || x < -Float.Max
|
|
@@ -152,16 +209,32 @@ fun atanReduce(v: Float, k: Int) -> Float =
|
|
|
152
209
|
if v > 0.5f && k < 8
|
|
153
210
|
atanReduce(v / {1.0f + sqrt(1.0f + v * v)}, k + 1)
|
|
154
211
|
else
|
|
155
|
-
sum := atanSeries(v
|
|
212
|
+
sum := atanSeries(v, -{v * v})
|
|
156
213
|
scale := Float(1 << k)
|
|
157
214
|
sum * scale
|
|
158
215
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
216
|
+
# v - v^3/3 + v^5/5 - ... - v^39/39, in order: each power is the one before times -v^2.
|
|
217
|
+
fun atanSeries(v: Float, m: Float) -> Float =
|
|
218
|
+
p3 := v * m
|
|
219
|
+
p5 := p3 * m
|
|
220
|
+
p7 := p5 * m
|
|
221
|
+
p9 := p7 * m
|
|
222
|
+
p11 := p9 * m
|
|
223
|
+
p13 := p11 * m
|
|
224
|
+
p15 := p13 * m
|
|
225
|
+
p17 := p15 * m
|
|
226
|
+
p19 := p17 * m
|
|
227
|
+
p21 := p19 * m
|
|
228
|
+
p23 := p21 * m
|
|
229
|
+
p25 := p23 * m
|
|
230
|
+
p27 := p25 * m
|
|
231
|
+
p29 := p27 * m
|
|
232
|
+
p31 := p29 * m
|
|
233
|
+
p33 := p31 * m
|
|
234
|
+
p35 := p33 * m
|
|
235
|
+
p37 := p35 * m
|
|
236
|
+
p39 := p37 * m
|
|
237
|
+
v + p3 / 3.0f + p5 / 5.0f + p7 / 7.0f + p9 / 9.0f + p11 / 11.0f + p13 / 13.0f + p15 / 15.0f + p17 / 17.0f + p19 / 19.0f + p21 / 21.0f + p23 / 23.0f + p25 / 25.0f + p27 / 27.0f + p29 / 29.0f + p31 / 31.0f + p33 / 33.0f + p35 / 35.0f + p37 / 37.0f + p39 / 39.0f
|
|
165
238
|
|
|
166
239
|
fun asin(x: Float) -> Float =
|
|
167
240
|
if x != x || x < -1.0f || x > 1.0f
|
tests/core/float_bits.plum
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# `Float.toBits` / `Float.fromBits` (wasm's `i64.reinterpret_f64` / `f64.reinterpret_i64`),
|
|
2
|
+
# and std math's unrolled series (sin, cos, tan, exp, ln, atan) and bit-read range reduction
|
|
3
|
+
# (exp, ln): the expected bits are the ones the loop versions gave (see
|
|
4
|
+
# benchmarks/math_bits/check.plum, which compares the two over millions of inputs).
|
|
5
|
+
import std/Float
|
|
6
|
+
import std/Int
|
|
7
|
+
import std/math
|
|
8
|
+
|
|
9
|
+
Inf = 1.0f / 0.0f
|
|
10
|
+
NaN = 0.0f / 0.0f
|
|
11
|
+
|
|
12
|
+
fun negZero(x: Float) -> Bool =
|
|
13
|
+
x == 0.0f && 1.0f / x < 0.0f
|
|
14
|
+
|
|
15
|
+
fun posZero(x: Float) -> Bool =
|
|
16
|
+
x == 0.0f && 1.0f / x > 0.0f
|
|
17
|
+
|
|
18
|
+
fun roundTrip(b: Int) -> Bool =
|
|
19
|
+
Float.fromBits(b).toBits() == b
|
|
20
|
+
|
|
21
|
+
fun applyB(f: fn(Float) -> Int, x: Float) -> Int =
|
|
22
|
+
f(x)
|
|
23
|
+
|
|
24
|
+
fun applyF(f: fn(Int) -> Float, b: Int) -> Float =
|
|
25
|
+
f(b)
|
|
26
|
+
|
|
27
|
+
test "toBits is the IEEE 754 pattern"
|
|
28
|
+
assert 1.0f.toBits() == 0x3FF0_0000_0000_0000
|
|
29
|
+
assert 2.0f.toBits() == 0x4000_0000_0000_0000
|
|
30
|
+
assert {-2.0f}.toBits() == -0x4000_0000_0000_0000
|
|
31
|
+
assert 0.0f.toBits() == 0
|
|
32
|
+
assert {-0.0f}.toBits() == Int.Min
|
|
33
|
+
assert Float.Min.toBits() == 1
|
|
34
|
+
assert Float.Max.toBits() == 0x7FEF_FFFF_FFFF_FFFF
|
|
35
|
+
assert Inf.toBits() == 0x7FF0_0000_0000_0000
|
|
36
|
+
assert {-Inf}.toBits() == -0x0010_0000_0000_0000
|
|
37
|
+
assert 1.5.toBits() == 0x3FF8_0000_0000_0000
|
|
38
|
+
|
|
39
|
+
test "fromBits is the Float with those bits"
|
|
40
|
+
assert Float.fromBits(0x3FF0_0000_0000_0000) == 1.0f
|
|
41
|
+
assert Float.fromBits(0x4009_21FB_5444_2D18) == Float.Pi
|
|
42
|
+
assert posZero(Float.fromBits(0))
|
|
43
|
+
assert negZero(Float.fromBits(Int.Min))
|
|
44
|
+
assert Float.fromBits(1) == Float.Min
|
|
45
|
+
assert Float.fromBits(0x000F_FFFF_FFFF_FFFF) == Float.fromBits(0x0010_0000_0000_0000) - Float.Min
|
|
46
|
+
assert Float.fromBits(0x7FF0_0000_0000_0000) == Inf
|
|
47
|
+
assert Float.fromBits(-0x0010_0000_0000_0000) == -Inf
|
|
48
|
+
assert Float.fromBits(0x7FF8_0000_0000_0000).isNaN()
|
|
49
|
+
|
|
50
|
+
test "round trips keep NaN payloads and the sign of zero"
|
|
51
|
+
assert roundTrip(0)
|
|
52
|
+
assert roundTrip(Int.Min)
|
|
53
|
+
assert roundTrip(1)
|
|
54
|
+
assert roundTrip(-1)
|
|
55
|
+
assert roundTrip(Int.Max)
|
|
56
|
+
assert roundTrip(0x7FF8_0000_DEAD_BEEF)
|
|
57
|
+
assert roundTrip(0x7FF0_0000_0000_0001)
|
|
58
|
+
assert roundTrip(-0x0008_0000_0000_0001)
|
|
59
|
+
assert roundTrip(0x000F_FFFF_FFFF_FFFF)
|
|
60
|
+
assert Float.fromBits(0x7FF8_0000_DEAD_BEEF).isNaN()
|
|
61
|
+
assert Float.fromBits(-1).isNaN()
|
|
62
|
+
assert Float.fromBits(Float.Pi.toBits()) == Float.Pi
|
|
63
|
+
assert Float.fromBits(NaN.toBits()).toBits() == NaN.toBits()
|
|
64
|
+
assert negZero(Float.fromBits({-0.0f}.toBits()))
|
|
65
|
+
|
|
66
|
+
test "toBits and fromBits in closures"
|
|
67
|
+
assert applyB(|x| x.toBits(), 1.0f) == 0x3FF0_0000_0000_0000
|
|
68
|
+
assert applyB(|x| x.toBits(), -0.0f) == Int.Min
|
|
69
|
+
assert applyF(|b| Float.fromBits(b), 0x4000_0000_0000_0000) == 2.0f
|
|
70
|
+
assert applyF(|b| Float.fromBits(b), 0x7FF8_0000_DEAD_BEEF).toBits() == 0x7FF8_0000_DEAD_BEEF
|
|
71
|
+
|
|
72
|
+
test "sin, cos and tan keep their bits"
|
|
73
|
+
assert posZero(math.sin(0.0f))
|
|
74
|
+
assert posZero(math.sin(-0.0f))
|
|
75
|
+
assert math.sin(1.0f).toBits() == 4605754516372524270
|
|
76
|
+
assert math.sin(-2.5f).toBits() == -4619806259459182587
|
|
77
|
+
assert math.sin(Float.Pi).toBits() == 4468182615269985440
|
|
78
|
+
assert math.sin(1.0e6).toBits() == -4623395494514420638
|
|
79
|
+
assert math.sin(Float.Min) == Float.Min
|
|
80
|
+
assert math.cos(0.0f) == 1.0f
|
|
81
|
+
assert math.cos(1.0f).toBits() == 4603041830071955640
|
|
82
|
+
assert math.cos(-3.0f).toBits() == -4616279757631920688
|
|
83
|
+
assert math.tan(0.5f).toBits() == 4603095874924661028
|
|
84
|
+
assert math.tan(-1.25f).toBits() == -4609412669623881302
|
|
85
|
+
assert math.sin(NaN).isNaN()
|
|
86
|
+
assert math.sin(Inf).isNaN()
|
|
87
|
+
assert math.cos(-Inf).isNaN()
|
|
88
|
+
|
|
89
|
+
test "exp keeps its bits, at the reduction boundaries too"
|
|
90
|
+
assert math.exp(0.0f) == 1.0f
|
|
91
|
+
assert math.exp(-0.0f) == 1.0f
|
|
92
|
+
assert math.exp(Float.Min) == 1.0f
|
|
93
|
+
assert math.exp(1.0f).toBits() == 4613303445314885478
|
|
94
|
+
assert math.exp(0.5f).toBits() == 4610103999673009818
|
|
95
|
+
assert math.exp(-0.5f).toBits() == 4603638362051417609
|
|
96
|
+
assert math.exp(0.75f).toBits() == 4611949479042997497
|
|
97
|
+
assert math.exp(2.0f).toBits() == 4620005356000828837
|
|
98
|
+
assert math.exp(-3.3f).toBits() == 4585476245642657404
|
|
99
|
+
assert math.exp(700.0f).toBits() == 9155136748776911380
|
|
100
|
+
assert math.exp(-700.0f).toBits() == 58915316498510369
|
|
101
|
+
assert math.exp(701.0f) == Inf
|
|
102
|
+
assert posZero(math.exp(-701.0f))
|
|
103
|
+
assert math.exp(NaN).isNaN()
|
|
104
|
+
|
|
105
|
+
test "ln keeps its bits, for subnormals too"
|
|
106
|
+
assert posZero(math.ln(1.0f))
|
|
107
|
+
assert math.ln(2.0f).toBits() == 4604418534313441775
|
|
108
|
+
assert math.ln(Float.E) == 1.0f
|
|
109
|
+
assert math.ln(0.1f).toBits() == -4611004657371359979
|
|
110
|
+
assert math.ln(1.0e300).toBits() == 4649287341619838901
|
|
111
|
+
assert math.ln(Float.Max).toBits() == 4649454530587146735
|
|
112
|
+
assert math.ln(Float.Min).toBits() == -4573612656913714749
|
|
113
|
+
assert math.ln(Float.fromBits(0x000F_FFFF_FFFF_FFFF)).toBits() == -4573929700241785645
|
|
114
|
+
assert math.ln(Float.fromBits(0x0010_0000_0000_0000)).toBits() == -4573929700241785646
|
|
115
|
+
assert math.ln(1.9999999999999998f).toBits() == 4604418534313441772
|
|
116
|
+
assert math.ln(0.0f) == -Inf
|
|
117
|
+
assert math.ln(-0.0f) == -Inf
|
|
118
|
+
assert math.ln(Inf) == Inf
|
|
119
|
+
assert math.ln(-1.0f).isNaN()
|
|
120
|
+
assert math.ln(NaN).isNaN()
|
|
121
|
+
|
|
122
|
+
test "atan and pow keep their bits"
|
|
123
|
+
assert posZero(math.atan(0.0f))
|
|
124
|
+
assert math.atan(0.5f).toBits() == 4602023952714414762
|
|
125
|
+
assert math.atan(1.0f).toBits() == 4605249457297304856
|
|
126
|
+
assert math.atan(-7.0f).toBits() == -4614258027452340741
|
|
127
|
+
assert math.atan(NaN).isNaN()
|
|
128
|
+
assert 2.0.pow(0.5f).toBits() == 4609047870845172686
|
|
129
|
+
assert 10.0.pow(-1.5f).toBits() == 4584718143433226873
|