| 1 |
/* origin: FreeBSD /usr/src/lib/msun/src/e_log2f.c */ |
| 2 |
/* |
| 3 |
* ==================================================== |
| 4 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 5 |
* |
| 6 |
* Developed at SunPro, a Sun Microsystems, Inc. business. |
| 7 |
* Permission to use, copy, modify, and distribute this |
| 8 |
* software is freely granted, provided that this notice |
| 9 |
* is preserved. |
| 10 |
* ==================================================== |
| 11 |
*/ |
| 12 |
/* |
| 13 |
* See comments in log2.c. |
| 14 |
*/ |
| 15 |
|
| 16 |
const IVLN2HI: f32 = 1.4428710938e+00; /* 0x3fb8b000 */ |
| 17 |
const IVLN2LO: f32 = -1.7605285393e-04; /* 0xb9389ad4 */ |
| 18 |
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */ |
| 19 |
const LG1: f32 = 0.66666662693; /* 0xaaaaaa.0p-24 */ |
| 20 |
const LG2: f32 = 0.40000972152; /* 0xccce13.0p-25 */ |
| 21 |
const LG3: f32 = 0.28498786688; /* 0x91e9ee.0p-25 */ |
| 22 |
const LG4: f32 = 0.24279078841; /* 0xf89e26.0p-26 */ |
| 23 |
|
| 24 |
/// The base 2 logarithm of `x` (f32). |
| 25 |
#[cfg_attr(assert_no_panic, no_panic::no_panic)] |
| 26 |
pub fn log2f(mut x: f32) -> f32 { |
| 27 |
let x1p25f = f32::from_bits(0x4c000000); // 0x1p25f === 2 ^ 25 |
| 28 |
|
| 29 |
let mut ui: u32 = x.to_bits(); |
| 30 |
let hfsq: f32; |
| 31 |
let f: f32; |
| 32 |
let s: f32; |
| 33 |
let z: f32; |
| 34 |
let r: f32; |
| 35 |
let w: f32; |
| 36 |
let t1: f32; |
| 37 |
let t2: f32; |
| 38 |
let mut hi: f32; |
| 39 |
let lo: f32; |
| 40 |
let mut ix: u32; |
| 41 |
let mut k: i32; |
| 42 |
|
| 43 |
ix = ui; |
| 44 |
k = 0; |
| 45 |
if ix < 0x00800000 || (ix >> 31) > 0 { |
| 46 |
/* x < 2**-126 */ |
| 47 |
if ix << 1 == 0 { |
| 48 |
return -1. / (x * x); /* log(+-0)=-inf */ |
| 49 |
} |
| 50 |
if (ix >> 31) > 0 { |
| 51 |
return (x - x) / 0.0; /* log(-#) = NaN */ |
| 52 |
} |
| 53 |
/* subnormal number, scale up x */ |
| 54 |
k -= 25; |
| 55 |
x *= x1p25f; |
| 56 |
ui = x.to_bits(); |
| 57 |
ix = ui; |
| 58 |
} else if ix >= 0x7f800000 { |
| 59 |
return x; |
| 60 |
} else if ix == 0x3f800000 { |
| 61 |
return 0.; |
| 62 |
} |
| 63 |
|
| 64 |
/* reduce x into [sqrt(2)/2, sqrt(2)] */ |
| 65 |
ix += 0x3f800000 - 0x3f3504f3; |
| 66 |
k += (ix >> 23) as i32 - 0x7f; |
| 67 |
ix = (ix & 0x007fffff) + 0x3f3504f3; |
| 68 |
ui = ix; |
| 69 |
x = f32::from_bits(ui); |
| 70 |
|
| 71 |
f = x - 1.0; |
| 72 |
s = f / (2.0 + f); |
| 73 |
z = s * s; |
| 74 |
w = z * z; |
| 75 |
t1 = w * (LG2 + w * LG4); |
| 76 |
t2 = z * (LG1 + w * LG3); |
| 77 |
r = t2 + t1; |
| 78 |
hfsq = 0.5 * f * f; |
| 79 |
|
| 80 |
hi = f - hfsq; |
| 81 |
ui = hi.to_bits(); |
| 82 |
ui &= 0xfffff000; |
| 83 |
hi = f32::from_bits(ui); |
| 84 |
lo = f - hi - hfsq + s * (hfsq + r); |
| 85 |
(lo + hi) * IVLN2LO + lo * IVLN2HI + hi * IVLN2HI + k as f32 |
| 86 |
} |