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
272
273
274
275
276
277
278
279
//! `f32` extension

use crate::float::F32;

/// `f32` extension providing various arithmetic approximations and polyfills
/// for `std` functionality.
pub trait F32Ext: Sized {
    /// Compute absolute value with a constant-time, data-independent
    /// implementation.
    fn abs(self) -> f32;

    /// Approximates `acos(x)` in radians in the range `[0, pi]`
    fn acos(self) -> f32;

    /// Approximates `asin(x)` in radians in the range `[-pi/2, pi/2]`.
    fn asin(self) -> f32;

    /// Approximates `atan(x)` in radians with a maximum error of `0.002`.
    fn atan(self) -> f32;

    /// Approximates `atan(x)` normalized to the `[−1,1]` range with a maximum
    /// error of `0.1620` degrees.
    fn atan_norm(self) -> f32;

    /// Approximates the four quadrant arctangent `atan2(x)` in radians, with
    /// a maximum error of `0.002`.
    fn atan2(self, other: f32) -> f32;

    /// Approximates the four quadrant arctangent.
    /// Normalized to the `[0,4)` range with a maximum error of `0.1620` degrees.
    fn atan2_norm(self, other: f32) -> f32;

    /// Approximates floating point ceiling.
    fn ceil(self) -> f32;

    /// Copies the sign from one number to another and returns it.
    fn copysign(self, sign: f32) -> f32;

    /// Approximates cosine in radians with a maximum error of `0.002`.
    fn cos(self) -> f32;

    /// Calculates Euclidean division, the matching method for `rem_euclid`.
    fn div_euclid(self, other: f32) -> f32;

    /// Approximates `e^x`.
    fn exp(self) -> f32;

    /// Approximates floating point floor.
    fn floor(self) -> f32;

    /// Retrieve the fractional part of floating point with sign.
    fn fract(self) -> f32;

    /// Approximates the length of the hypotenuse of a right-angle triangle given
    /// legs of length `x` and `y`.
    fn hypot(self, other: f32) -> f32;

    /// Approximates `1/x` with an average deviation of ~8%.
    fn inv(self) -> f32;

    /// Approximates inverse square root with an average deviation of ~5%.
    fn invsqrt(self) -> f32;

    /// Approximates `ln(x)`.
    fn ln(self) -> f32;

    /// Approximates `log` with an arbitrary base.
    fn log(self, base: f32) -> f32;

    /// Approximates `log2`.
    fn log2(self) -> f32;

    /// Approximates `log10`.
    fn log10(self) -> f32;

    /// Computes `(self * a) + b`.
    fn mul_add(self, a: f32, b: f32) -> f32;

    /// Approximates `self^n`.
    fn powf(self, n: f32) -> f32;

    /// Approximates `self^n` where n is an `i32`
    fn powi(self, n: i32) -> f32;

    /// Returns the reciprocal (inverse) of a number, `1/x`.
    fn recip(self) -> f32;

    /// Calculates the least nonnegative remainder of `self (mod other)`.
    fn rem_euclid(self, other: f32) -> f32;

    /// Round the number part of floating point with sign.
    fn round(self) -> f32;

    /// Returns a number that represents the sign of `self`.
    fn signum(self) -> f32;

    /// Approximates sine in radians with a maximum error of `0.002`.
    fn sin(self) -> f32;

    /// Simultaneously computes the sine and cosine of the number, `x`.
    /// Returns `(sin(x), cos(x))`.
    fn sin_cos(self) -> (f32, f32);

    /// Approximates square root with an average deviation of ~5%.
    fn sqrt(self) -> f32;

    /// Approximates `tan(x)` in radians with a maximum error of `0.6`.
    fn tan(self) -> f32;

    /// Retrieve whole number part of floating point with sign.
    fn trunc(self) -> f32;
}

impl F32Ext for f32 {
    #[inline]
    fn abs(self) -> f32 {
        F32(self).abs().0
    }

    #[inline]
    fn acos(self) -> f32 {
        F32(self).acos().0
    }

    #[inline]
    fn asin(self) -> f32 {
        F32(self).asin().0
    }

    #[inline]
    fn atan(self) -> f32 {
        F32(self).atan().0
    }

    #[inline]
    fn atan_norm(self) -> f32 {
        F32(self).atan_norm().0
    }

    #[inline]
    fn atan2(self, other: f32) -> f32 {
        F32(self).atan2(F32(other)).0
    }

    #[inline]
    fn atan2_norm(self, other: f32) -> f32 {
        F32(self).atan2_norm(F32(other)).0
    }

    #[inline]
    fn ceil(self) -> f32 {
        F32(self).ceil().0
    }

    #[inline]
    fn copysign(self, sign: f32) -> f32 {
        F32(self).copysign(F32(sign)).0
    }

    #[inline]
    fn cos(self) -> f32 {
        F32(self).cos().0
    }

    #[inline]
    fn div_euclid(self, other: f32) -> f32 {
        F32(self).div_euclid(F32(other)).0
    }

    #[inline]
    fn exp(self) -> f32 {
        F32(self).exp().0
    }

    #[inline]
    fn floor(self) -> f32 {
        F32(self).floor().0
    }

    #[inline]
    fn fract(self) -> f32 {
        F32(self).fract().0
    }

    #[inline]
    fn hypot(self, other: f32) -> f32 {
        F32(self).hypot(other.into()).0
    }

    #[inline]
    fn inv(self) -> f32 {
        F32(self).inv().0
    }

    #[inline]
    fn invsqrt(self) -> f32 {
        F32(self).invsqrt().0
    }

    #[inline]
    fn ln(self) -> f32 {
        F32(self).ln().0
    }

    #[inline]
    fn log(self, base: f32) -> f32 {
        F32(self).log(F32(base)).0
    }

    #[inline]
    fn log2(self) -> f32 {
        F32(self).log2().0
    }

    #[inline]
    fn log10(self) -> f32 {
        F32(self).log10().0
    }

    #[inline]
    fn mul_add(self, a: f32, b: f32) -> f32 {
        F32(self).mul_add(F32(a), F32(b)).0
    }

    #[inline]
    fn powf(self, n: f32) -> f32 {
        F32(self).powf(F32(n)).0
    }

    #[inline]
    fn powi(self, n: i32) -> f32 {
        F32(self).powi(n).0
    }

    #[inline]
    fn recip(self) -> f32 {
        F32(self).recip().0
    }

    #[inline]
    fn rem_euclid(self, other: f32) -> f32 {
        F32(self).rem_euclid(F32(other)).0
    }

    #[inline]
    fn round(self) -> f32 {
        F32(self).round().0
    }

    #[inline]
    fn signum(self) -> f32 {
        F32(self).signum().0
    }

    #[inline]
    fn sin(self) -> f32 {
        F32(self).sin().0
    }

    #[inline]
    fn sin_cos(self) -> (f32, f32) {
        (F32(self).sin().0, F32(self).cos().0)
    }

    #[inline]
    fn sqrt(self) -> f32 {
        F32(self).sqrt().0
    }

    #[inline]
    fn tan(self) -> f32 {
        F32(self).tan().0
    }

    #[inline]
    fn trunc(self) -> f32 {
        F32(self).trunc().0
    }
}