retrofire_core/math/
float.rs

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
//! Floating-point compatibility API.
//!
//! Most floating-point functions are currently unavailable in `no_std`.
//! This module provides the missing functions using either the `libm` or
//! `micromath` crate, depending on which feature is enabled. As a fallback,
//! it also implements a critical subset of the functions even if none of
//! the features is enabled.

#[cfg(feature = "libm")]
pub mod libm {
    pub use libm::fabsf as abs;
    pub use libm::floorf as floor;

    pub use libm::powf;
    pub use libm::sqrtf as sqrt;

    pub use libm::cosf as cos;
    pub use libm::sinf as sin;
    pub use libm::tanf as tan;

    pub use libm::acosf as acos;
    pub use libm::asinf as asin;
    pub use libm::atan2f as atan2;

    pub use libm::expf as exp;
    pub use libm::log2;

    pub use super::fallback::rem_euclid;

    pub fn recip_sqrt(x: f32) -> f32 {
        powf(x, -0.5)
    }
}

#[cfg(feature = "mm")]
pub mod mm {
    use micromath::F32Ext as mm;

    #[inline]
    pub fn abs(x: f32) -> f32 {
        mm::abs(x)
    }
    #[inline]
    pub fn floor(x: f32) -> f32 {
        mm::floor(x)
    }
    #[inline]
    pub fn rem_euclid(x: f32, m: f32) -> f32 {
        mm::rem_euclid(x, m)
    }
    /// Returns the approximate square root of `x`.
    #[inline]
    pub fn sqrt(x: f32) -> f32 {
        let y = mm::sqrt(x);
        // One round of Newton's method
        0.5 * (y + (x / y))
    }
    /// Returns the approximate reciprocal of the square root of `x`.
    #[inline]
    pub fn recip_sqrt(x: f32) -> f32 {
        let y = mm::invsqrt(x);
        // A round of Newton's method
        y * (1.5 - 0.5 * x * y * y)
    }
    #[inline]
    pub fn powf(x: f32, y: f32) -> f32 {
        mm::powf(x, y)
    }
    #[inline]
    pub fn sin(x: f32) -> f32 {
        mm::sin(x)
    }
    #[inline]
    pub fn cos(x: f32) -> f32 {
        mm::cos(x)
    }
    #[inline]
    pub fn tan(x: f32) -> f32 {
        mm::tan(x)
    }
    #[inline]
    pub fn asin(x: f32) -> f32 {
        mm::asin(x)
    }
    #[inline]
    pub fn acos(x: f32) -> f32 {
        mm::acos(x)
    }
    #[inline]
    pub fn atan2(y: f32, x: f32) -> f32 {
        #[cfg(debug_assertions)]
        if y == 0.0 && x == 0.0 {
            // Micromath yields a NaN but others return zero
            return 0.0;
        }
        mm::atan2(y, x)
    }
}

pub mod fallback {
    /// Returns the absolute value of `x`.
    #[inline]
    pub fn abs(x: f32) -> f32 {
        f32::from_bits(x.to_bits() & !0x8000_0000)
    }
    /// Returns the largest integer less than or equal to `x`.
    #[inline]
    pub fn floor(x: f32) -> f32 {
        (x as i64 - x.is_sign_negative() as i64) as f32
    }
    // Returns the least non-negative remainder of `x` (mod `m`).
    #[inline]
    pub fn rem_euclid(x: f32, m: f32) -> f32 {
        x % m + (x.is_sign_negative() as u32 as f32) * m
    }
    /// Returns the approximate reciprocal of the square root of `x`.
    #[inline]
    pub fn recip_sqrt(x: f32) -> f32 {
        // https://en.wikipedia.org/wiki/Fast_inverse_square_root
        let y = f32::from_bits(0x5f37_5a86 - (x.to_bits() >> 1));
        // A round of Newton's method
        y * (1.5 - 0.5 * x * y * y)
    }
}

#[cfg(feature = "std")]
#[allow(non_camel_case_types)]
pub type f32 = core::primitive::f32;

#[allow(unused)]
pub(crate) trait RecipSqrt {
    fn recip_sqrt(x: Self) -> Self;
}

#[cfg(feature = "std")]
impl RecipSqrt for f32 {
    fn recip_sqrt(x: f32) -> f32 {
        x.powf(-0.5)
    }
}

#[cfg(all(feature = "libm", not(feature = "std")))]
pub use libm as f32;

#[cfg(all(feature = "mm", not(feature = "std"), not(feature = "libm")))]
pub use mm as f32;

#[cfg(not(feature = "fp"))]
pub use fallback as f32;

#[cfg(test)]
#[allow(unused_imports)]
mod tests {

    use crate::assert_approx_eq;

    #[cfg(feature = "libm")]
    #[test]
    fn libm_functions() {
        use super::libm;
        use core::f32::consts::PI;
        assert_eq!(libm::cos(PI), -1.0);
        assert_eq!(libm::sqrt(9.0), 3.0);
    }

    #[cfg(feature = "mm")]
    #[test]
    fn mm_functions() {
        use core::f32::consts::*;

        use super::f32;

        assert_approx_eq!(f32::sin(FRAC_PI_6), 0.5);
        assert_eq!(f32::cos(PI), -1.0);
        assert_eq!(f32::sqrt(16.0), 4.0);
        assert_approx_eq!(f32::sqrt(9.0), 3.0, eps = 1e-3);
    }

    #[cfg(feature = "std")]
    #[test]
    fn std_functions() {
        use super::f32;
        use core::f32::consts::PI;
        assert_eq!(f32::cos(PI), -1.0);
        assert_eq!(f32::sqrt(9.0), 3.0);
    }

    #[cfg(not(feature = "fp"))]
    #[test]
    fn fallback_functions() {
        use super::{f32, RecipSqrt};

        assert_eq!(f32::floor(1.23), 1.0);
        assert_eq!(f32::floor(0.0), 0.0);
        assert_eq!(f32::floor(-1.23), -2.0);

        assert_eq!(f32::abs(1.23), 1.23);
        assert_eq!(f32::abs(0.0), 0.0);
        assert_eq!(f32::abs(-1.23), 1.23);

        assert_approx_eq!(f32::rem_euclid(1.23, 4.0), 1.23);
        assert_approx_eq!(f32::rem_euclid(4.0, 4.0), 0.0);
        assert_approx_eq!(f32::rem_euclid(5.67, 4.0), 1.67);
        assert_approx_eq!(f32::rem_euclid(-1.23, 4.0), 2.77);
    }

    #[test]
    fn recip_sqrt() {
        use super::{f32, RecipSqrt};
        assert_approx_eq!(f32::recip_sqrt(2.0), f32::sqrt(0.5), eps = 1e-2);
        assert_approx_eq!(f32::recip_sqrt(9.0), 1.0 / 3.0, eps = 1e-3);
    }
}