| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#![cfg(all(not(feature = "std"), any(feature = "parse-floats", feature = "write-floats")))] |
| 8 |
#![cfg_attr(any(), rustfmt::skip)] |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
macro_rules! volatile { |
| 14 |
($e:expr) => { |
| 15 |
|
| 16 |
unsafe { |
| 17 |
core::ptr::read_volatile(&$e); |
| 18 |
} |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
pub(crate) fn floord(x: f64) -> f64 { |
| 26 |
const TOINT: f64 = 1. / f64::EPSILON; |
| 27 |
|
| 28 |
let ui = x.to_bits(); |
| 29 |
let e = ((ui >> 52) & 0x7ff) as i32; |
| 30 |
|
| 31 |
if (e >= 0x3ff + 52) || (x == 0.) { |
| 32 |
return x; |
| 33 |
} |
| 34 |
|
| 35 |
let y = if (ui >> 63) != 0 { |
| 36 |
x - TOINT + TOINT - x |
| 37 |
} else { |
| 38 |
x + TOINT - TOINT - x |
| 39 |
}; |
| 40 |
|
| 41 |
if e < 0x3ff { |
| 42 |
volatile!(y); |
| 43 |
return if (ui >> 63) != 0 { |
| 44 |
-1. |
| 45 |
} else { |
| 46 |
0. |
| 47 |
}; |
| 48 |
} |
| 49 |
if y > 0. { |
| 50 |
x + y - 1. |
| 51 |
} else { |
| 52 |
x + y |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub(crate) fn floorf(x: f32) -> f32 { |
| 60 |
let mut ui = x.to_bits(); |
| 61 |
let e = (((ui >> 23) as i32) & 0xff) - 0x7f; |
| 62 |
|
| 63 |
if e >= 23 { |
| 64 |
return x; |
| 65 |
} |
| 66 |
if e >= 0 { |
| 67 |
let m: u32 = 0x007fffff >> e; |
| 68 |
if (ui & m) == 0 { |
| 69 |
return x; |
| 70 |
} |
| 71 |
volatile!(x + f32::from_bits(0x7b800000)); |
| 72 |
if ui >> 31 != 0 { |
| 73 |
ui += m; |
| 74 |
} |
| 75 |
ui &= !m; |
| 76 |
} else { |
| 77 |
volatile!(x + f32::from_bits(0x7b800000)); |
| 78 |
if ui >> 31 == 0 { |
| 79 |
ui = 0; |
| 80 |
} else if ui << 1 != 0 { |
| 81 |
return -1.0; |
| 82 |
} |
| 83 |
} |
| 84 |
f32::from_bits(ui) |
| 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 |
#[allow(clippy::eq_op, clippy::excessive_precision)] |
| 150 |
pub(crate) fn logd(mut x: f64) -> f64 { |
| 151 |
const LN2_HI: f64 = 6.93147180369123816490e-01; |
| 152 |
const LN2_LO: f64 = 1.90821492927058770002e-10; |
| 153 |
const LG1: f64 = 6.666666666666735130e-01; |
| 154 |
const LG2: f64 = 3.999999999940941908e-01; |
| 155 |
const LG3: f64 = 2.857142874366239149e-01; |
| 156 |
const LG4: f64 = 2.222219843214978396e-01; |
| 157 |
const LG5: f64 = 1.818357216161805012e-01; |
| 158 |
const LG6: f64 = 1.531383769920937332e-01; |
| 159 |
const LG7: f64 = 1.479819860511658591e-01; |
| 160 |
|
| 161 |
let x1p54 = f64::from_bits(0x4350000000000000); |
| 162 |
|
| 163 |
let mut ui = x.to_bits(); |
| 164 |
let mut hx: u32 = (ui >> 32) as u32; |
| 165 |
let mut k: i32 = 0; |
| 166 |
|
| 167 |
if (hx < 0x00100000) || ((hx >> 31) != 0) { |
| 168 |
|
| 169 |
if ui << 1 == 0 { |
| 170 |
return -1. / (x * x); |
| 171 |
} |
| 172 |
if hx >> 31 != 0 { |
| 173 |
return (x - x) / 0.0; |
| 174 |
} |
| 175 |
|
| 176 |
k -= 54; |
| 177 |
x *= x1p54; |
| 178 |
ui = x.to_bits(); |
| 179 |
hx = (ui >> 32) as u32; |
| 180 |
} else if hx >= 0x7ff00000 { |
| 181 |
return x; |
| 182 |
} else if hx == 0x3ff00000 && ui << 32 == 0 { |
| 183 |
return 0.; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
hx += 0x3ff00000 - 0x3fe6a09e; |
| 188 |
k += ((hx >> 20) as i32) - 0x3ff; |
| 189 |
hx = (hx & 0x000fffff) + 0x3fe6a09e; |
| 190 |
ui = ((hx as u64) << 32) | (ui & 0xffffffff); |
| 191 |
x = f64::from_bits(ui); |
| 192 |
|
| 193 |
let f: f64 = x - 1.0; |
| 194 |
let hfsq: f64 = 0.5 * f * f; |
| 195 |
let s: f64 = f / (2.0 + f); |
| 196 |
let z: f64 = s * s; |
| 197 |
let w: f64 = z * z; |
| 198 |
let t1: f64 = w * (LG2 + w * (LG4 + w * LG6)); |
| 199 |
let t2: f64 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7))); |
| 200 |
let r: f64 = t2 + t1; |
| 201 |
let dk: f64 = k as f64; |
| 202 |
s * (hfsq + r) + dk * LN2_LO - hfsq + f + dk * LN2_HI |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[allow(clippy::eq_op, clippy::excessive_precision)] |
| 221 |
pub(crate) fn logf(mut x: f32) -> f32 { |
| 222 |
const LN2_HI: f32 = 6.9313812256e-01; |
| 223 |
const LN2_LO: f32 = 9.0580006145e-06; |
| 224 |
|
| 225 |
const LG1: f32 = 0.66666662693; |
| 226 |
const LG2: f32 = 0.40000972152; |
| 227 |
const LG3: f32 = 0.28498786688; |
| 228 |
const LG4: f32 = 0.24279078841; |
| 229 |
|
| 230 |
let x1p25 = f32::from_bits(0x4c000000); |
| 231 |
|
| 232 |
let mut ix = x.to_bits(); |
| 233 |
let mut k = 0i32; |
| 234 |
|
| 235 |
if (ix < 0x00800000) || ((ix >> 31) != 0) { |
| 236 |
|
| 237 |
if ix << 1 == 0 { |
| 238 |
return -1. / (x * x); |
| 239 |
} |
| 240 |
if (ix >> 31) != 0 { |
| 241 |
return (x - x) / 0.; |
| 242 |
} |
| 243 |
|
| 244 |
k -= 25; |
| 245 |
x *= x1p25; |
| 246 |
ix = x.to_bits(); |
| 247 |
} else if ix >= 0x7f800000 { |
| 248 |
return x; |
| 249 |
} else if ix == 0x3f800000 { |
| 250 |
return 0.; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
ix += 0x3f800000 - 0x3f3504f3; |
| 255 |
k += ((ix >> 23) as i32) - 0x7f; |
| 256 |
ix = (ix & 0x007fffff) + 0x3f3504f3; |
| 257 |
x = f32::from_bits(ix); |
| 258 |
|
| 259 |
let f = x - 1.; |
| 260 |
let s = f / (2. + f); |
| 261 |
let z = s * s; |
| 262 |
let w = z * z; |
| 263 |
let t1 = w * (LG2 + w * LG4); |
| 264 |
let t2 = z * (LG1 + w * LG3); |
| 265 |
let r = t2 + t1; |
| 266 |
let hfsq = 0.5 * f * f; |
| 267 |
let dk = k as f32; |
| 268 |
s * (hfsq + r) + dk * LN2_LO - hfsq + f + dk * LN2_HI |
| 269 |
} |