forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moremath.go
271 lines (251 loc) · 9.84 KB
/
moremath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package moremath
import (
"math"
)
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/syntax/values.html#floating-point
const (
// F32CanonicalNaNBits is the 32-bit float where payload's MSB equals 1 and others are all zero.
F32CanonicalNaNBits = uint32(0x7fc0_0000)
// F32CanonicalNaNBitsMask can be used to judge the value `v` is canonical nan as "v&F32CanonicalNaNBitsMask == F32CanonicalNaNBits"
F32CanonicalNaNBitsMask = uint32(0x7fff_ffff)
// F64CanonicalNaNBits is the 64-bit float where payload's MSB equals 1 and others are all zero.
F64CanonicalNaNBits = uint64(0x7ff8_0000_0000_0000)
// F64CanonicalNaNBitsMask can be used to judge the value `v` is canonical nan as "v&F64CanonicalNaNBitsMask == F64CanonicalNaNBits"
F64CanonicalNaNBitsMask = uint64(0x7fff_ffff_ffff_ffff)
// F32ArithmeticNaNPayloadMSB is used to extract the most significant bit of payload of 32-bit arithmetic NaN values
F32ArithmeticNaNPayloadMSB = uint32(0x0040_0000)
// F32ExponentMask is used to extract the exponent of 32-bit floating point.
F32ExponentMask = uint32(0x7f80_0000)
// F32ArithmeticNaNBits is an example 32-bit arithmetic NaN.
F32ArithmeticNaNBits = F32CanonicalNaNBits | 0b1 // Set first bit to make this different from the canonical NaN.
// F64ArithmeticNaNPayloadMSB is used to extract the most significant bit of payload of 64-bit arithmetic NaN values
F64ArithmeticNaNPayloadMSB = uint64(0x0008_0000_0000_0000)
// F64ExponentMask is used to extract the exponent of 64-bit floating point.
F64ExponentMask = uint64(0x7ff0_0000_0000_0000)
// F64ArithmeticNaNBits is an example 64-bit arithmetic NaN.
F64ArithmeticNaNBits = F64CanonicalNaNBits | 0b1 // Set first bit to make this different from the canonical NaN.
)
// WasmCompatMin64 is the Wasm spec compatible variant of math.Min for 64-bit floating points.
func WasmCompatMin64(x, y float64) float64 {
switch {
case math.IsNaN(x) || math.IsNaN(y):
return returnF64NaNBinOp(x, y)
case math.IsInf(x, -1) || math.IsInf(y, -1):
return math.Inf(-1)
case x == 0 && x == y:
if math.Signbit(x) {
return x
}
return y
}
if x < y {
return x
}
return y
}
// WasmCompatMin32 is the Wasm spec compatible variant of math.Min for 32-bit floating points.
func WasmCompatMin32(x, y float32) float32 {
x64, y64 := float64(x), float64(y)
switch {
case math.IsNaN(x64) || math.IsNaN(y64):
return returnF32NaNBinOp(x, y)
case math.IsInf(x64, -1) || math.IsInf(y64, -1):
return float32(math.Inf(-1))
case x == 0 && x == y:
if math.Signbit(x64) {
return x
}
return y
}
if x < y {
return x
}
return y
}
// WasmCompatMax64 is the Wasm spec compatible variant of math.Max for 64-bit floating points.
func WasmCompatMax64(x, y float64) float64 {
switch {
case math.IsNaN(x) || math.IsNaN(y):
return returnF64NaNBinOp(x, y)
case math.IsInf(x, 1) || math.IsInf(y, 1):
return math.Inf(1)
case x == 0 && x == y:
if math.Signbit(x) {
return y
}
return x
}
if x > y {
return x
}
return y
}
// WasmCompatMax32 is the Wasm spec compatible variant of math.Max for 32-bit floating points.
func WasmCompatMax32(x, y float32) float32 {
x64, y64 := float64(x), float64(y)
switch {
case math.IsNaN(x64) || math.IsNaN(y64):
return returnF32NaNBinOp(x, y)
case math.IsInf(x64, 1) || math.IsInf(y64, 1):
return float32(math.Inf(1))
case x == 0 && x == y:
if math.Signbit(x64) {
return y
}
return x
}
if x > y {
return x
}
return y
}
// WasmCompatNearestF32 is the Wasm spec compatible variant of math.Round, used for Nearest instruction.
// For example, this converts 1.9 to 2.0, and this has the semantics of LLVM's rint intrinsic.
//
// e.g. math.Round(-4.5) results in -5 while this results in -4.
//
// See https://llvm.org/docs/LangRef.html#llvm-rint-intrinsic.
func WasmCompatNearestF32(f float32) float32 {
var res float32
// TODO: look at https://github.com/bytecodealliance/wasmtime/pull/2171 and reconsider this algorithm
if f != 0 {
ceil := float32(math.Ceil(float64(f)))
floor := float32(math.Floor(float64(f)))
distToCeil := math.Abs(float64(f - ceil))
distToFloor := math.Abs(float64(f - floor))
h := ceil / 2.0
if distToCeil < distToFloor {
res = ceil
} else if distToCeil == distToFloor && float32(math.Floor(float64(h))) == h {
res = ceil
} else {
res = floor
}
} else {
res = f
}
return returnF32UniOp(f, res)
}
// WasmCompatNearestF64 is the Wasm spec compatible variant of math.Round, used for Nearest instruction.
// For example, this converts 1.9 to 2.0, and this has the semantics of LLVM's rint intrinsic.
//
// e.g. math.Round(-4.5) results in -5 while this results in -4.
//
// See https://llvm.org/docs/LangRef.html#llvm-rint-intrinsic.
func WasmCompatNearestF64(f float64) float64 {
// TODO: look at https://github.com/bytecodealliance/wasmtime/pull/2171 and reconsider this algorithm
var res float64
if f != 0 {
ceil := math.Ceil(f)
floor := math.Floor(f)
distToCeil := math.Abs(f - ceil)
distToFloor := math.Abs(f - floor)
h := ceil / 2.0
if distToCeil < distToFloor {
res = ceil
} else if distToCeil == distToFloor && math.Floor(h) == h {
res = ceil
} else {
res = floor
}
} else {
res = f
}
return returnF64UniOp(f, res)
}
// WasmCompatCeilF32 is the same as math.Ceil on 32-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatCeilF32(f float32) float32 {
return returnF32UniOp(f, float32(math.Ceil(float64(f))))
}
// WasmCompatCeilF64 is the same as math.Ceil on 64-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatCeilF64(f float64) float64 {
return returnF64UniOp(f, math.Ceil(f))
}
// WasmCompatFloorF32 is the same as math.Floor on 32-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatFloorF32(f float32) float32 {
return returnF32UniOp(f, float32(math.Floor(float64(f))))
}
// WasmCompatFloorF64 is the same as math.Floor on 64-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatFloorF64(f float64) float64 {
return returnF64UniOp(f, math.Floor(f))
}
// WasmCompatTruncF32 is the same as math.Trunc on 32-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatTruncF32(f float32) float32 {
return returnF32UniOp(f, float32(math.Trunc(float64(f))))
}
// WasmCompatTruncF64 is the same as math.Trunc on 64-bit except that
// the returned NaN value follows the Wasm specification on NaN
// propagation.
// https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func WasmCompatTruncF64(f float64) float64 {
return returnF64UniOp(f, math.Trunc(f))
}
func f32IsNaN(v float32) bool {
return v != v // this is how NaN is defined.
}
func f64IsNaN(v float64) bool {
return v != v // this is how NaN is defined.
}
// returnF32UniOp returns the result of 32-bit unary operation. This accepts `original` which is the operand,
// and `result` which is its result. This returns the `result` as-is if the result is not NaN. Otherwise, this follows
// the same logic as in the reference interpreter as well as the amd64 and arm64 floating point handling.
func returnF32UniOp(original, result float32) float32 {
// Following the same logic as in the reference interpreter:
// https://github.com/WebAssembly/spec/blob/d48af683f5e6d00c13f775ab07d29a15daf92203/interpreter/exec/fxx.ml#L115-L122
if !f32IsNaN(result) {
return result
}
if !f32IsNaN(original) {
return math.Float32frombits(F32CanonicalNaNBits)
}
return math.Float32frombits(math.Float32bits(original) | F32CanonicalNaNBits)
}
// returnF32UniOp returns the result of 64-bit unary operation. This accepts `original` which is the operand,
// and `result` which is its result. This returns the `result` as-is if the result is not NaN. Otherwise, this follows
// the same logic as in the reference interpreter as well as the amd64 and arm64 floating point handling.
func returnF64UniOp(original, result float64) float64 {
// Following the same logic as in the reference interpreter (== amd64 and arm64's behavior):
// https://github.com/WebAssembly/spec/blob/d48af683f5e6d00c13f775ab07d29a15daf92203/interpreter/exec/fxx.ml#L115-L122
if !f64IsNaN(result) {
return result
}
if !f64IsNaN(original) {
return math.Float64frombits(F64CanonicalNaNBits)
}
return math.Float64frombits(math.Float64bits(original) | F64CanonicalNaNBits)
}
// returnF64NaNBinOp returns a NaN for 64-bit binary operations. `x` and `y` are original floats
// and at least one of them is NaN. The returned NaN is guaranteed to comply with the NaN propagation
// procedure: https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func returnF64NaNBinOp(x, y float64) float64 {
if f64IsNaN(x) {
return math.Float64frombits(math.Float64bits(x) | F64CanonicalNaNBits)
} else {
return math.Float64frombits(math.Float64bits(y) | F64CanonicalNaNBits)
}
}
// returnF64NaNBinOp returns a NaN for 32-bit binary operations. `x` and `y` are original floats
// and at least one of them is NaN. The returned NaN is guaranteed to comply with the NaN propagation
// procedure: https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/exec/numerics.html#nan-propagation
func returnF32NaNBinOp(x, y float32) float32 {
if f32IsNaN(x) {
return math.Float32frombits(math.Float32bits(x) | F32CanonicalNaNBits)
} else {
return math.Float32frombits(math.Float32bits(y) | F32CanonicalNaNBits)
}
}