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
/// Returns a very close approximation of `self.clamp(-1.0, 1.0).acos()`.
#[inline]
fn acos_approx_f32(v: f32) -> f32 {
    // Based on https://github.com/microsoft/DirectXMath `XMScalarAcos`
    // Clamp input to [-1,1].
    let nonnegative = v >= 0.0;
    let x = abs(v);
    let mut omx = 1.0 - x;
    if omx < 0.0 {
        omx = 0.0;
    }
    let root = sqrt(omx);

    // 7-degree minimax approximation
    #[allow(clippy::approx_constant)]
    let mut result =
        ((((((-0.001_262_491_1 * x + 0.006_670_09) * x - 0.017_088_126) * x + 0.030_891_88) * x
            - 0.050_174_303)
            * x
            + 0.088_978_99)
            * x
            - 0.214_598_8)
            * x
            + 1.570_796_3;
    result *= root;

    // acos(x) = pi - acos(-x) when x < 0
    if nonnegative {
        result
    } else {
        core::f32::consts::PI - result
    }
}

#[cfg(feature = "libm")]
mod libm_math {
    #[inline(always)]
    pub(crate) fn abs(f: f32) -> f32 {
        libm::fabsf(f)
    }

    #[inline(always)]
    pub(crate) fn acos_approx(f: f32) -> f32 {
        super::acos_approx_f32(f)
    }

    #[inline(always)]
    pub(crate) fn atan2(f: f32, other: f32) -> f32 {
        libm::atan2f(f, other)
    }

    #[allow(unused)]
    #[inline(always)]
    pub(crate) fn sin(f: f32) -> f32 {
        libm::sinf(f)
    }

    #[inline(always)]
    pub(crate) fn sin_cos(f: f32) -> (f32, f32) {
        libm::sincosf(f)
    }

    #[inline(always)]
    pub(crate) fn tan(f: f32) -> f32 {
        libm::tanf(f)
    }

    #[inline(always)]
    pub(crate) fn sqrt(f: f32) -> f32 {
        libm::sqrtf(f)
    }

    #[inline(always)]
    pub(crate) fn copysign(f: f32, sign: f32) -> f32 {
        libm::copysignf(f, sign)
    }

    #[inline(always)]
    pub(crate) fn signum(f: f32) -> f32 {
        if f.is_nan() {
            f32::NAN
        } else {
            copysign(1.0, f)
        }
    }

    #[inline(always)]
    pub(crate) fn round(f: f32) -> f32 {
        libm::roundf(f)
    }

    #[inline(always)]
    pub(crate) fn trunc(f: f32) -> f32 {
        libm::truncf(f)
    }

    #[inline(always)]
    pub(crate) fn ceil(f: f32) -> f32 {
        libm::ceilf(f)
    }

    #[inline(always)]
    pub(crate) fn floor(f: f32) -> f32 {
        libm::floorf(f)
    }

    #[inline(always)]
    pub(crate) fn exp(f: f32) -> f32 {
        libm::expf(f)
    }

    #[inline(always)]
    pub(crate) fn powf(f: f32, n: f32) -> f32 {
        libm::powf(f, n)
    }

    #[inline(always)]
    pub(crate) fn mul_add(a: f32, b: f32, c: f32) -> f32 {
        libm::fmaf(a, b, c)
    }

    #[inline]
    pub fn div_euclid(a: f32, b: f32) -> f32 {
        // Based on https://doc.rust-lang.org/src/std/f32.rs.html#293
        let q = libm::truncf(a / b);
        if a % b < 0.0 {
            return if b > 0.0 { q - 1.0 } else { q + 1.0 };
        }
        q
    }

    #[inline]
    pub fn rem_euclid(a: f32, b: f32) -> f32 {
        let r = a % b;
        if r < 0.0 {
            r + abs(b)
        } else {
            r
        }
    }
}

#[cfg(not(feature = "libm"))]
mod std_math {
    #[inline(always)]
    pub(crate) fn abs(f: f32) -> f32 {
        f32::abs(f)
    }

    #[inline(always)]
    pub(crate) fn acos_approx(f: f32) -> f32 {
        super::acos_approx_f32(f)
    }

    #[inline(always)]
    pub(crate) fn atan2(f: f32, other: f32) -> f32 {
        f32::atan2(f, other)
    }

    #[allow(unused)]
    #[inline(always)]
    pub(crate) fn sin(f: f32) -> f32 {
        f32::sin(f)
    }

    #[inline(always)]
    pub(crate) fn sin_cos(f: f32) -> (f32, f32) {
        f32::sin_cos(f)
    }

    #[inline(always)]
    pub(crate) fn tan(f: f32) -> f32 {
        f32::tan(f)
    }

    #[inline(always)]
    pub(crate) fn sqrt(f: f32) -> f32 {
        f32::sqrt(f)
    }

    #[inline(always)]
    pub(crate) fn copysign(f: f32, sign: f32) -> f32 {
        f32::copysign(f, sign)
    }

    #[inline(always)]
    pub(crate) fn signum(f: f32) -> f32 {
        f32::signum(f)
    }

    #[inline(always)]
    pub(crate) fn round(f: f32) -> f32 {
        f32::round(f)
    }

    #[inline(always)]
    pub(crate) fn trunc(f: f32) -> f32 {
        f32::trunc(f)
    }

    #[inline(always)]
    pub(crate) fn ceil(f: f32) -> f32 {
        f32::ceil(f)
    }

    #[inline(always)]
    pub(crate) fn floor(f: f32) -> f32 {
        f32::floor(f)
    }

    #[inline(always)]
    pub(crate) fn exp(f: f32) -> f32 {
        f32::exp(f)
    }

    #[inline(always)]
    pub(crate) fn powf(f: f32, n: f32) -> f32 {
        f32::powf(f, n)
    }

    #[inline(always)]
    pub(crate) fn mul_add(a: f32, b: f32, c: f32) -> f32 {
        f32::mul_add(a, b, c)
    }

    #[inline]
    pub fn div_euclid(a: f32, b: f32) -> f32 {
        f32::div_euclid(a, b)
    }

    #[inline]
    pub fn rem_euclid(a: f32, b: f32) -> f32 {
        f32::rem_euclid(a, b)
    }
}

#[cfg(feature = "libm")]
pub(crate) use libm_math::*;

#[cfg(not(feature = "libm"))]
pub(crate) use std_math::*;