| 1 |
/*! |
| 2 |
A no-std module for some floating point operations. |
| 3 |
|
| 4 |
These were vendored from the [`libm`] crate. For no-std specifically, I |
| 5 |
wouldn't necessarily mind depending on `libm`, but I don't think there's a |
| 6 |
way to express "only depend on a crate if some on-by-default feature is not |
| 7 |
enabled." |
| 8 |
|
| 9 |
We also very intentionally have very minimal floating point requirements. It's |
| 10 |
used for `Span::total` where it's part of the API and thus necessary. It's also |
| 11 |
used when rounding in some cases, although I would like to remove that use of |
| 12 |
floating point. And now it's also used in parts of `SignedDuration`. |
| 13 |
|
| 14 |
[`libm`]: https://github.com/rust-lang/libm |
| 15 |
*/ |
| 16 |
|
| 17 |
pub(crate) trait Float { |
| 18 |
fn abs(self) -> Self; |
| 19 |
fn ceil(self) -> Self; |
| 20 |
fn floor(self) -> Self; |
| 21 |
fn round(self) -> Self; |
| 22 |
fn signum(self) -> Self; |
| 23 |
fn trunc(self) -> Self; |
| 24 |
fn fract(self) -> Self; |
| 25 |
} |
| 26 |
|
| 27 |
const TOINT64: f64 = 1. / f64::EPSILON; |
| 28 |
|
| 29 |
impl Float for f64 { |
| 30 |
fn abs(self) -> f64 { |
| 31 |
if self.is_sign_negative() { |
| 32 |
-self |
| 33 |
} else { |
| 34 |
self |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
fn ceil(self) -> f64 { |
| 39 |
let x = self; |
| 40 |
let u: u64 = x.to_bits(); |
| 41 |
let e: i64 = (u >> 52 & 0x7ff) as i64; |
| 42 |
let y: f64; |
| 43 |
|
| 44 |
if e >= 0x3ff + 52 || x == 0. { |
| 45 |
return x; |
| 46 |
} |
| 47 |
// y = int(x) - x, where int(x) is an integer neighbor of x |
| 48 |
y = if (u >> 63) != 0 { |
| 49 |
x - TOINT64 + TOINT64 - x |
| 50 |
} else { |
| 51 |
x + TOINT64 - TOINT64 - x |
| 52 |
}; |
| 53 |
// special case because of non-nearest rounding modes |
| 54 |
if e < 0x3ff { |
| 55 |
core::hint::black_box(y); |
| 56 |
return if (u >> 63) != 0 { -0. } else { 1. }; |
| 57 |
} |
| 58 |
if y < 0. { |
| 59 |
x + y + 1. |
| 60 |
} else { |
| 61 |
x + y |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
fn floor(self) -> f64 { |
| 66 |
let x = self; |
| 67 |
let ui = x.to_bits(); |
| 68 |
let e = ((ui >> 52) & 0x7ff) as i32; |
| 69 |
|
| 70 |
if (e >= 0x3ff + 52) || (x == 0.) { |
| 71 |
return x; |
| 72 |
} |
| 73 |
/* y = int(x) - x, where int(x) is an integer neighbor of x */ |
| 74 |
let y = if (ui >> 63) != 0 { |
| 75 |
x - TOINT64 + TOINT64 - x |
| 76 |
} else { |
| 77 |
x + TOINT64 - TOINT64 - x |
| 78 |
}; |
| 79 |
/* special case because of non-nearest rounding modes */ |
| 80 |
if e < 0x3ff { |
| 81 |
core::hint::black_box(y); |
| 82 |
return if (ui >> 63) != 0 { -1. } else { 0. }; |
| 83 |
} |
| 84 |
if y > 0. { |
| 85 |
x + y - 1. |
| 86 |
} else { |
| 87 |
x + y |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fn round(self) -> f64 { |
| 92 |
(self + copysign64(0.5 - 0.25 * f64::EPSILON, self)).trunc() |
| 93 |
} |
| 94 |
|
| 95 |
fn signum(self) -> f64 { |
| 96 |
if self.is_nan() { |
| 97 |
Self::NAN |
| 98 |
} else { |
| 99 |
copysign64(1.0, self) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
fn trunc(self) -> f64 { |
| 104 |
let x = self; |
| 105 |
let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120 |
| 106 |
|
| 107 |
let mut i: u64 = x.to_bits(); |
| 108 |
let mut e: i64 = (i >> 52 & 0x7ff) as i64 - 0x3ff + 12; |
| 109 |
let m: u64; |
| 110 |
|
| 111 |
if e >= 52 + 12 { |
| 112 |
return x; |
| 113 |
} |
| 114 |
if e < 12 { |
| 115 |
e = 1; |
| 116 |
} |
| 117 |
m = -1i64 as u64 >> e; |
| 118 |
if (i & m) == 0 { |
| 119 |
return x; |
| 120 |
} |
| 121 |
core::hint::black_box(x + x1p120); |
| 122 |
i &= !m; |
| 123 |
f64::from_bits(i) |
| 124 |
} |
| 125 |
|
| 126 |
fn fract(self) -> f64 { |
| 127 |
self - self.trunc() |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl Float for f32 { |
| 132 |
fn abs(self) -> f32 { |
| 133 |
if self.is_sign_negative() { |
| 134 |
-self |
| 135 |
} else { |
| 136 |
self |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
fn ceil(self) -> f32 { |
| 141 |
let x = self; |
| 142 |
let mut ui = x.to_bits(); |
| 143 |
let e = (((ui >> 23) & 0xff).wrapping_sub(0x7f)) as i32; |
| 144 |
|
| 145 |
if e >= 23 { |
| 146 |
return x; |
| 147 |
} |
| 148 |
if e >= 0 { |
| 149 |
let m = 0x007fffff >> e; |
| 150 |
if (ui & m) == 0 { |
| 151 |
return x; |
| 152 |
} |
| 153 |
core::hint::black_box(x + f32::from_bits(0x7b800000)); |
| 154 |
if ui >> 31 == 0 { |
| 155 |
ui += m; |
| 156 |
} |
| 157 |
ui &= !m; |
| 158 |
} else { |
| 159 |
core::hint::black_box(x + f32::from_bits(0x7b800000)); |
| 160 |
if ui >> 31 != 0 { |
| 161 |
return -0.0; |
| 162 |
} else if ui << 1 != 0 { |
| 163 |
return 1.0; |
| 164 |
} |
| 165 |
} |
| 166 |
f32::from_bits(ui) |
| 167 |
} |
| 168 |
|
| 169 |
fn floor(self) -> f32 { |
| 170 |
let x = self; |
| 171 |
let mut ui = x.to_bits(); |
| 172 |
let e = (((ui >> 23) as i32) & 0xff) - 0x7f; |
| 173 |
|
| 174 |
if e >= 23 { |
| 175 |
return x; |
| 176 |
} |
| 177 |
if e >= 0 { |
| 178 |
let m: u32 = 0x007fffff >> e; |
| 179 |
if (ui & m) == 0 { |
| 180 |
return x; |
| 181 |
} |
| 182 |
core::hint::black_box(x + f32::from_bits(0x7b800000)); |
| 183 |
if ui >> 31 != 0 { |
| 184 |
ui += m; |
| 185 |
} |
| 186 |
ui &= !m; |
| 187 |
} else { |
| 188 |
core::hint::black_box(x + f32::from_bits(0x7b800000)); |
| 189 |
if ui >> 31 == 0 { |
| 190 |
ui = 0; |
| 191 |
} else if ui << 1 != 0 { |
| 192 |
return -1.0; |
| 193 |
} |
| 194 |
} |
| 195 |
f32::from_bits(ui) |
| 196 |
} |
| 197 |
|
| 198 |
fn round(self) -> f32 { |
| 199 |
(self + copysign32(0.5 - 0.25 * f32::EPSILON, self)).trunc() |
| 200 |
} |
| 201 |
|
| 202 |
fn signum(self) -> f32 { |
| 203 |
if self.is_nan() { |
| 204 |
Self::NAN |
| 205 |
} else { |
| 206 |
copysign32(1.0, self) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
fn trunc(self) -> f32 { |
| 211 |
let x = self; |
| 212 |
let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120 |
| 213 |
|
| 214 |
let mut i: u32 = x.to_bits(); |
| 215 |
let mut e: i32 = (i >> 23 & 0xff) as i32 - 0x7f + 9; |
| 216 |
let m: u32; |
| 217 |
|
| 218 |
if e >= 23 + 9 { |
| 219 |
return x; |
| 220 |
} |
| 221 |
if e < 9 { |
| 222 |
e = 1; |
| 223 |
} |
| 224 |
m = -1i32 as u32 >> e; |
| 225 |
if (i & m) == 0 { |
| 226 |
return x; |
| 227 |
} |
| 228 |
core::hint::black_box(x + x1p120); |
| 229 |
i &= !m; |
| 230 |
f32::from_bits(i) |
| 231 |
} |
| 232 |
|
| 233 |
fn fract(self) -> f32 { |
| 234 |
self - self.trunc() |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
fn copysign64(x: f64, y: f64) -> f64 { |
| 239 |
let mut ux = x.to_bits(); |
| 240 |
let uy = y.to_bits(); |
| 241 |
ux &= (!0) >> 1; |
| 242 |
ux |= uy & (1 << 63); |
| 243 |
f64::from_bits(ux) |
| 244 |
} |
| 245 |
|
| 246 |
fn copysign32(x: f32, y: f32) -> f32 { |
| 247 |
let mut ux = x.to_bits(); |
| 248 |
let uy = y.to_bits(); |
| 249 |
ux &= 0x7fffffff; |
| 250 |
ux |= uy & 0x80000000; |
| 251 |
f32::from_bits(ux) |
| 252 |
} |