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