-
Notifications
You must be signed in to change notification settings - Fork 1
/
arch_nowasm.go
65 lines (52 loc) · 1.44 KB
/
arch_nowasm.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
//go:build !tinygo.wasm
package tinymath
// Functions that can be optimized for wasm
// Returns the smallest integer greater than or equal to a number.
func Ceil(self float32) float32 {
return -Floor(-self)
}
// Returns the largest integer less than or equal to a number.
func Floor(self float32) float32 {
res := float32(int32(self))
if self < res {
res -= 1.0
}
return float32(res)
}
// Approximates the square root of a number with an average deviation of ~5%.
//
// Returns [`NAN`] if `self` is a negative number.
func Sqrt(self float32) float32 {
if self >= 0.0 {
return FromBits((ToBits(self) + 0x3f80_0000) >> 1)
} else {
return NaN
}
}
// Returns the integer part of a number.
func Trunc(self float32) float32 {
const MANTISSA_MASK = 0b0000_0000_0111_1111_1111_1111_1111_1111
x_bits := ToBits(self)
exponent := extractExponentValue(self)
// exponent is negative, there is no whole number, just return zero
if exponent < 0 {
return CopySign(0, self)
}
exponent_clamped := uint32(Max(exponent, 0))
// find the part of the fraction that would be left over
fractional_part := (x_bits << exponent_clamped) & MANTISSA_MASK
// if there isn't a fraction we can just return the whole thing.
if fractional_part == 0 {
return self
}
fractional_mask := fractional_part >> exponent_clamped
return FromBits(x_bits & ^fractional_mask)
}
func leadingZeros(x uint32) uint32 {
var n uint32 = 32
for x != 0 {
x >>= 1
n -= 1
}
return n
}