| 1 | #[cfg(any(feature = "libm", all(feature = "nostd-libm", not(feature = "std"))))] |
| 2 | mod libm_math { |
| 3 | #[inline(always)] |
| 4 | pub(crate) fn abs(f: f64) -> f64 { |
| 5 | libm::fabs(f) |
| 6 | } |
| 7 | |
| 8 | #[inline(always)] |
| 9 | pub(crate) fn acos_approx(f: f64) -> f64 { |
| 10 | libm::acos(f.clamp(-1.0, 1.0)) |
| 11 | } |
| 12 | |
| 13 | #[inline(always)] |
| 14 | pub(crate) fn atan2(f: f64, other: f64) -> f64 { |
| 15 | libm::atan2(f, other) |
| 16 | } |
| 17 | |
| 18 | #[inline(always)] |
| 19 | pub(crate) fn sin(f: f64) -> f64 { |
| 20 | libm::sin(f) |
| 21 | } |
| 22 | |
| 23 | #[inline(always)] |
| 24 | pub(crate) fn sin_cos(f: f64) -> (f64, f64) { |
| 25 | libm::sincos(f) |
| 26 | } |
| 27 | |
| 28 | #[inline(always)] |
| 29 | pub(crate) fn tan(f: f64) -> f64 { |
| 30 | libm::tan(f) |
| 31 | } |
| 32 | |
| 33 | #[inline(always)] |
| 34 | pub(crate) fn sqrt(f: f64) -> f64 { |
| 35 | libm::sqrt(f) |
| 36 | } |
| 37 | |
| 38 | #[inline(always)] |
| 39 | pub(crate) fn copysign(f: f64, sign: f64) -> f64 { |
| 40 | libm::copysign(f, sign) |
| 41 | } |
| 42 | |
| 43 | #[inline(always)] |
| 44 | pub(crate) fn signum(f: f64) -> f64 { |
| 45 | if f.is_nan() { |
| 46 | f64::NAN |
| 47 | } else { |
| 48 | copysign(1.0, f) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[inline(always)] |
| 53 | pub(crate) fn round(f: f64) -> f64 { |
| 54 | libm::round(f) |
| 55 | } |
| 56 | |
| 57 | #[inline(always)] |
| 58 | pub(crate) fn trunc(f: f64) -> f64 { |
| 59 | libm::trunc(f) |
| 60 | } |
| 61 | |
| 62 | #[inline(always)] |
| 63 | pub(crate) fn ceil(f: f64) -> f64 { |
| 64 | libm::ceil(f) |
| 65 | } |
| 66 | |
| 67 | #[inline(always)] |
| 68 | pub(crate) fn floor(f: f64) -> f64 { |
| 69 | libm::floor(f) |
| 70 | } |
| 71 | |
| 72 | #[inline(always)] |
| 73 | pub(crate) fn exp(f: f64) -> f64 { |
| 74 | libm::exp(f) |
| 75 | } |
| 76 | |
| 77 | #[inline(always)] |
| 78 | pub(crate) fn powf(f: f64, n: f64) -> f64 { |
| 79 | libm::pow(f, n) |
| 80 | } |
| 81 | |
| 82 | #[inline(always)] |
| 83 | pub(crate) fn mul_add(a: f64, b: f64, c: f64) -> f64 { |
| 84 | libm::fma(a, b, c) |
| 85 | } |
| 86 | |
| 87 | #[inline] |
| 88 | pub fn div_euclid(a: f64, b: f64) -> f64 { |
| 89 | // Based on https://doc.rust-lang.org/src/std/f64.rs.html#293 |
| 90 | let q = libm::trunc(a / b); |
| 91 | if a % b < 0.0 { |
| 92 | return if b > 0.0 { q - 1.0 } else { q + 1.0 }; |
| 93 | } |
| 94 | q |
| 95 | } |
| 96 | |
| 97 | #[inline] |
| 98 | pub fn rem_euclid(a: f64, b: f64) -> f64 { |
| 99 | let r = a % b; |
| 100 | if r < 0.0 { |
| 101 | r + abs(b) |
| 102 | } else { |
| 103 | r |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg(all(not(feature = "libm"), feature = "std"))] |
| 109 | mod std_math { |
| 110 | #[inline(always)] |
| 111 | pub(crate) fn abs(f: f64) -> f64 { |
| 112 | f64::abs(f) |
| 113 | } |
| 114 | |
| 115 | #[inline(always)] |
| 116 | pub(crate) fn acos_approx(f: f64) -> f64 { |
| 117 | f64::acos(f64::clamp(f, -1.0, 1.0)) |
| 118 | } |
| 119 | |
| 120 | #[inline(always)] |
| 121 | pub(crate) fn atan2(f: f64, other: f64) -> f64 { |
| 122 | f64::atan2(f, other) |
| 123 | } |
| 124 | |
| 125 | #[inline(always)] |
| 126 | pub(crate) fn sin(f: f64) -> f64 { |
| 127 | f64::sin(f) |
| 128 | } |
| 129 | |
| 130 | #[inline(always)] |
| 131 | pub(crate) fn sin_cos(f: f64) -> (f64, f64) { |
| 132 | f64::sin_cos(f) |
| 133 | } |
| 134 | |
| 135 | #[inline(always)] |
| 136 | pub(crate) fn tan(f: f64) -> f64 { |
| 137 | f64::tan(f) |
| 138 | } |
| 139 | |
| 140 | #[inline(always)] |
| 141 | pub(crate) fn sqrt(f: f64) -> f64 { |
| 142 | f64::sqrt(f) |
| 143 | } |
| 144 | |
| 145 | #[inline(always)] |
| 146 | pub(crate) fn copysign(f: f64, sign: f64) -> f64 { |
| 147 | f64::copysign(f, sign) |
| 148 | } |
| 149 | |
| 150 | #[inline(always)] |
| 151 | pub(crate) fn signum(f: f64) -> f64 { |
| 152 | f64::signum(f) |
| 153 | } |
| 154 | |
| 155 | #[inline(always)] |
| 156 | pub(crate) fn round(f: f64) -> f64 { |
| 157 | f64::round(f) |
| 158 | } |
| 159 | |
| 160 | #[inline(always)] |
| 161 | pub(crate) fn trunc(f: f64) -> f64 { |
| 162 | f64::trunc(f) |
| 163 | } |
| 164 | |
| 165 | #[inline(always)] |
| 166 | pub(crate) fn ceil(f: f64) -> f64 { |
| 167 | f64::ceil(f) |
| 168 | } |
| 169 | |
| 170 | #[inline(always)] |
| 171 | pub(crate) fn floor(f: f64) -> f64 { |
| 172 | f64::floor(f) |
| 173 | } |
| 174 | |
| 175 | #[inline(always)] |
| 176 | pub(crate) fn exp(f: f64) -> f64 { |
| 177 | f64::exp(f) |
| 178 | } |
| 179 | |
| 180 | #[inline(always)] |
| 181 | pub(crate) fn powf(f: f64, n: f64) -> f64 { |
| 182 | f64::powf(f, n) |
| 183 | } |
| 184 | |
| 185 | #[inline(always)] |
| 186 | pub(crate) fn mul_add(a: f64, b: f64, c: f64) -> f64 { |
| 187 | f64::mul_add(a, b, c) |
| 188 | } |
| 189 | |
| 190 | #[inline] |
| 191 | pub fn div_euclid(a: f64, b: f64) -> f64 { |
| 192 | f64::div_euclid(a, b) |
| 193 | } |
| 194 | |
| 195 | #[inline] |
| 196 | pub fn rem_euclid(a: f64, b: f64) -> f64 { |
| 197 | f64::rem_euclid(a, b) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #[cfg(any(feature = "libm", all(feature = "nostd-libm", not(feature = "std"))))] |
| 202 | pub(crate) use libm_math::*; |
| 203 | |
| 204 | #[cfg(all(not(feature = "libm"), feature = "std"))] |
| 205 | pub(crate) use std_math::*; |