| 1 | // num_traits is optional as it adds 70% to compile times. It is needed by no_std builds |
| 2 | #[cfg(feature = "libm")] |
| 3 | pub use num_traits::{Float, Num, Signed}; |
| 4 | |
| 5 | use core::{ |
| 6 | marker::Sized, |
| 7 | ops::{Add, Div, Mul, Sub}, |
| 8 | }; |
| 9 | |
| 10 | // Stub the necessary parts of num traits |
| 11 | #[cfg(not(feature = "libm"))] |
| 12 | pub trait Num: PartialEq {} |
| 13 | |
| 14 | #[cfg(not(feature = "libm"))] |
| 15 | pub trait Signed: Sized + Num + core::ops::Neg<Output = Self> { |
| 16 | fn abs(self) -> Self; |
| 17 | fn signum(self) -> Self; |
| 18 | } |
| 19 | |
| 20 | #[cfg(not(feature = "libm"))] |
| 21 | pub trait Float: Num + Copy + core::ops::Neg<Output = Self> { |
| 22 | fn asin(self) -> Self; |
| 23 | fn acos(self) -> Self; |
| 24 | fn ceil(self) -> Self; |
| 25 | fn exp(self) -> Self; |
| 26 | fn floor(self) -> Self; |
| 27 | fn is_finite(self) -> bool; |
| 28 | fn is_nan(self) -> bool; |
| 29 | fn powf(self, n: Self) -> Self; |
| 30 | fn recip(self) -> Self; |
| 31 | fn round(self) -> Self; |
| 32 | fn sqrt(self) -> Self; |
| 33 | fn sin(self) -> Self; |
| 34 | fn sin_cos(self) -> (Self, Self); |
| 35 | fn tan(self) -> Self; |
| 36 | } |
| 37 | |
| 38 | #[cfg(not(feature = "libm"))] |
| 39 | macro_rules! impl_num_trait { |
| 40 | ($t:ident) => { |
| 41 | impl Num for $t {} |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | #[cfg(not(feature = "libm"))] |
| 46 | macro_rules! impl_signed_trait { |
| 47 | ($t:ident) => { |
| 48 | impl_num_trait!($t); |
| 49 | |
| 50 | impl Signed for $t { |
| 51 | #[inline(always)] |
| 52 | fn abs(self) -> Self { |
| 53 | $t::abs(self) |
| 54 | } |
| 55 | #[inline(always)] |
| 56 | fn signum(self) -> Self { |
| 57 | $t::signum(self) |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | #[cfg(not(feature = "libm"))] |
| 64 | macro_rules! impl_float_trait { |
| 65 | ($t:ident) => { |
| 66 | impl_signed_trait!($t); |
| 67 | |
| 68 | impl Float for $t { |
| 69 | #[inline(always)] |
| 70 | fn asin(self) -> Self { |
| 71 | $t::asin(self) |
| 72 | } |
| 73 | #[inline(always)] |
| 74 | fn acos(self) -> Self { |
| 75 | $t::acos(self) |
| 76 | } |
| 77 | #[inline(always)] |
| 78 | fn ceil(self) -> Self { |
| 79 | $t::ceil(self) |
| 80 | } |
| 81 | #[inline(always)] |
| 82 | fn exp(self) -> Self { |
| 83 | $t::exp(self) |
| 84 | } |
| 85 | #[inline(always)] |
| 86 | fn floor(self) -> Self { |
| 87 | $t::floor(self) |
| 88 | } |
| 89 | #[inline(always)] |
| 90 | fn is_finite(self) -> bool { |
| 91 | $t::is_finite(self) |
| 92 | } |
| 93 | #[inline(always)] |
| 94 | fn is_nan(self) -> bool { |
| 95 | $t::is_nan(self) |
| 96 | } |
| 97 | #[inline(always)] |
| 98 | fn powf(self, n: Self) -> Self { |
| 99 | $t::powf(self, n) |
| 100 | } |
| 101 | #[inline(always)] |
| 102 | fn recip(self) -> Self { |
| 103 | $t::recip(self) |
| 104 | } |
| 105 | #[inline(always)] |
| 106 | fn round(self) -> Self { |
| 107 | $t::round(self) |
| 108 | } |
| 109 | #[inline(always)] |
| 110 | fn sin(self) -> Self { |
| 111 | $t::sin(self) |
| 112 | } |
| 113 | #[inline(always)] |
| 114 | fn sin_cos(self) -> (Self, Self) { |
| 115 | $t::sin_cos(self) |
| 116 | } |
| 117 | #[inline(always)] |
| 118 | fn sqrt(self) -> Self { |
| 119 | $t::sqrt(self) |
| 120 | } |
| 121 | #[inline(always)] |
| 122 | fn tan(self) -> Self { |
| 123 | $t::tan(self) |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | #[cfg(not(feature = "libm"))] |
| 130 | impl_float_trait!(f32); |
| 131 | #[cfg(not(feature = "libm"))] |
| 132 | impl_float_trait!(f64); |
| 133 | #[cfg(not(feature = "libm"))] |
| 134 | impl_signed_trait!(i32); |
| 135 | #[cfg(not(feature = "libm"))] |
| 136 | impl_num_trait!(u32); |
| 137 | |
| 138 | pub trait MaskConst: Sized { |
| 139 | const MASK: [Self; 2]; |
| 140 | } |
| 141 | |
| 142 | pub trait NumConstEx: Sized { |
| 143 | const ZERO: Self; |
| 144 | const ONE: Self; |
| 145 | } |
| 146 | |
| 147 | pub trait FloatConstEx: Sized { |
| 148 | const NEG_ONE: Self; |
| 149 | const TWO: Self; |
| 150 | const HALF: Self; |
| 151 | } |
| 152 | |
| 153 | pub trait NumEx: |
| 154 | Num |
| 155 | + NumConstEx |
| 156 | + Copy |
| 157 | + Clone |
| 158 | + PartialEq |
| 159 | + PartialOrd |
| 160 | + Add<Output = Self> |
| 161 | + Div<Output = Self> |
| 162 | + Mul<Output = Self> |
| 163 | + Sub<Output = Self> |
| 164 | { |
| 165 | fn min(self, other: Self) -> Self; |
| 166 | fn max(self, other: Self) -> Self; |
| 167 | } |
| 168 | |
| 169 | pub trait SignedEx: Signed + NumEx {} |
| 170 | |
| 171 | pub trait FloatEx: Float + FloatConstEx + SignedEx { |
| 172 | /// Returns a very close approximation of `self.clamp(-1.0, 1.0).acos()`. |
| 173 | fn acos_approx(self) -> Self; |
| 174 | fn from_f32(f: f32) -> Self; |
| 175 | fn from_f64(f: f64) -> Self; |
| 176 | } |
| 177 | |
| 178 | impl NumConstEx for f32 { |
| 179 | const ZERO: Self = 0.0; |
| 180 | const ONE: Self = 1.0; |
| 181 | } |
| 182 | |
| 183 | impl FloatConstEx for f32 { |
| 184 | const NEG_ONE: Self = -1.0; |
| 185 | const TWO: Self = 2.0; |
| 186 | const HALF: Self = 0.5; |
| 187 | } |
| 188 | |
| 189 | impl NumEx for f32 { |
| 190 | #[inline(always)] |
| 191 | fn min(self, other: Self) -> Self { |
| 192 | f32::min(self, other) |
| 193 | } |
| 194 | #[inline(always)] |
| 195 | fn max(self, other: Self) -> Self { |
| 196 | f32::max(self, other) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | impl SignedEx for f32 {} |
| 201 | |
| 202 | impl FloatEx for f32 { |
| 203 | #[inline(always)] |
| 204 | fn from_f32(v: f32) -> Self { |
| 205 | v |
| 206 | } |
| 207 | #[inline(always)] |
| 208 | fn from_f64(v: f64) -> Self { |
| 209 | v as Self |
| 210 | } |
| 211 | #[inline(always)] |
| 212 | fn acos_approx(self) -> Self { |
| 213 | // Based on https://github.com/microsoft/DirectXMath `XMScalarAcos` |
| 214 | // Clamp input to [-1,1]. |
| 215 | let nonnegative = self >= 0.0; |
| 216 | let x = self.abs(); |
| 217 | let mut omx = 1.0 - x; |
| 218 | if omx < 0.0 { |
| 219 | omx = 0.0; |
| 220 | } |
| 221 | let root = omx.sqrt(); |
| 222 | |
| 223 | // 7-degree minimax approximation |
| 224 | #[allow(clippy::approx_constant)] |
| 225 | let mut result = ((((((-0.001_262_491_1 * x + 0.006_670_09) * x - 0.017_088_126) * x |
| 226 | + 0.030_891_88) |
| 227 | * x |
| 228 | - 0.050_174_303) |
| 229 | * x |
| 230 | + 0.088_978_99) |
| 231 | * x |
| 232 | - 0.214_598_8) |
| 233 | * x |
| 234 | + 1.570_796_3; |
| 235 | result *= root; |
| 236 | |
| 237 | // acos(x) = pi - acos(-x) when x < 0 |
| 238 | if nonnegative { |
| 239 | result |
| 240 | } else { |
| 241 | core::f32::consts::PI - result |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | impl NumConstEx for f64 { |
| 247 | const ZERO: Self = 0.0; |
| 248 | const ONE: Self = 1.0; |
| 249 | } |
| 250 | |
| 251 | impl FloatConstEx for f64 { |
| 252 | const NEG_ONE: Self = -1.0; |
| 253 | const TWO: Self = 2.0; |
| 254 | const HALF: Self = 0.5; |
| 255 | } |
| 256 | |
| 257 | impl NumEx for f64 { |
| 258 | #[inline(always)] |
| 259 | fn min(self, other: Self) -> Self { |
| 260 | f64::min(self, other) |
| 261 | } |
| 262 | #[inline(always)] |
| 263 | fn max(self, other: Self) -> Self { |
| 264 | f64::max(self, other) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | impl SignedEx for f64 {} |
| 269 | |
| 270 | impl FloatEx for f64 { |
| 271 | #[inline(always)] |
| 272 | fn from_f32(v: f32) -> Self { |
| 273 | v as Self |
| 274 | } |
| 275 | #[inline(always)] |
| 276 | fn from_f64(v: f64) -> Self { |
| 277 | v |
| 278 | } |
| 279 | #[inline(always)] |
| 280 | fn acos_approx(self) -> Self { |
| 281 | f64::acos(self.max(-1.0).min(1.0)) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | impl NumConstEx for i32 { |
| 286 | const ZERO: Self = 0; |
| 287 | const ONE: Self = 1; |
| 288 | } |
| 289 | |
| 290 | impl NumEx for i32 { |
| 291 | #[inline(always)] |
| 292 | fn min(self, other: Self) -> Self { |
| 293 | core::cmp::min(self, other) |
| 294 | } |
| 295 | #[inline(always)] |
| 296 | fn max(self, other: Self) -> Self { |
| 297 | core::cmp::max(self, other) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | impl SignedEx for i32 {} |
| 302 | |
| 303 | impl NumConstEx for u32 { |
| 304 | const ZERO: Self = 0; |
| 305 | const ONE: Self = 1; |
| 306 | } |
| 307 | |
| 308 | impl NumEx for u32 { |
| 309 | #[inline(always)] |
| 310 | fn min(self, other: Self) -> Self { |
| 311 | core::cmp::min(self, other) |
| 312 | } |
| 313 | #[inline(always)] |
| 314 | fn max(self, other: Self) -> Self { |
| 315 | core::cmp::max(self, other) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | #[cfg(test)] |
| 320 | macro_rules! assert_approx_eq { |
| 321 | ($a:expr, $b:expr) => {{ |
| 322 | assert_approx_eq!($a, $b, core::f32::EPSILON); |
| 323 | }}; |
| 324 | ($a:expr, $b:expr, $eps:expr) => {{ |
| 325 | let (a, b) = (&$a, &$b); |
| 326 | let eps = $eps; |
| 327 | assert!( |
| 328 | (a - b).abs() <= eps, |
| 329 | "assertion failed: `(left !== right)` \ |
| 330 | (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)", |
| 331 | *a, |
| 332 | *b, |
| 333 | eps, |
| 334 | (a - b).abs() |
| 335 | ); |
| 336 | }}; |
| 337 | } |
| 338 | |
| 339 | #[cfg(test)] |
| 340 | macro_rules! assert_relative_eq { |
| 341 | ($a:expr, $b:expr) => {{ |
| 342 | assert_relative_eq!($a, $b, core::f32::EPSILON); |
| 343 | }}; |
| 344 | ($a:expr, $b:expr, $eps:expr) => {{ |
| 345 | let (a, b) = (&$a, &$b); |
| 346 | let eps = $eps; |
| 347 | let diff = (a - b).abs(); |
| 348 | let largest = a.abs().max(b.abs()); |
| 349 | assert!( |
| 350 | diff <= largest * eps, |
| 351 | "assertion failed: `(left !== right)` \ |
| 352 | (left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)", |
| 353 | *a, |
| 354 | *b, |
| 355 | largest * eps, |
| 356 | diff |
| 357 | ); |
| 358 | }}; |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn test_scalar_acos() { |
| 363 | fn test_scalar_acos_angle(a: f32) { |
| 364 | // 1e-6 is the lowest epsilon that will pass |
| 365 | assert_relative_eq!(a.acos_approx(), a.acos(), 1e-6); |
| 366 | // assert_approx_eq!(scalar_acos(a), a.acos(), 1e-6); |
| 367 | } |
| 368 | |
| 369 | // test 1024 floats between -1.0 and 1.0 inclusive |
| 370 | const MAX_TESTS: u32 = 1024 / 2; |
| 371 | const SIGN: u32 = 0x80_00_00_00; |
| 372 | const PTVE_ONE: u32 = 0x3f_80_00_00; // 1.0_f32.to_bits(); |
| 373 | const NGVE_ONE: u32 = SIGN | PTVE_ONE; |
| 374 | const STEP_SIZE: usize = (PTVE_ONE / MAX_TESTS) as usize; |
| 375 | for f in (SIGN..=NGVE_ONE).step_by(STEP_SIZE).map(f32::from_bits) { |
| 376 | test_scalar_acos_angle(f); |
| 377 | } |
| 378 | for f in (0..=PTVE_ONE).step_by(STEP_SIZE).map(f32::from_bits) { |
| 379 | test_scalar_acos_angle(f); |
| 380 | } |
| 381 | |
| 382 | // input is clamped to -1.0..1.0 |
| 383 | assert_approx_eq!(2.0_f32.acos_approx(), 0.0); |
| 384 | assert_approx_eq!((-2.0_f32).acos_approx(), core::f32::consts::PI); |
| 385 | |
| 386 | // input is clamped to -1.0..1.0 |
| 387 | assert_eq!(2.0_f64.acos_approx(), 0.0); |
| 388 | assert_eq!((-2.0_f64).acos_approx(), core::f64::consts::PI); |
| 389 | } |