libm/math/log2.rs
1/* origin: FreeBSD /usr/src/lib/msun/src/e_log2.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunSoft, 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 * Return the base 2 logarithm of x. See log.c for most comments.
14 *
15 * Reduce x to 2^k (1+f) and calculate r = log(1+f) - f + f*f/2
16 * as in log.c, then combine and scale in extra precision:
17 * log2(x) = (f - f*f/2 + r)/log(2) + k
18 */
19
20const IVLN2HI: f64 = 1.44269504072144627571e+00; /* 0x3ff71547, 0x65200000 */
21const IVLN2LO: f64 = 1.67517131648865118353e-10; /* 0x3de705fc, 0x2eefa200 */
22const LG1: f64 = 6.666666666666735130e-01; /* 3FE55555 55555593 */
23const LG2: f64 = 3.999999999940941908e-01; /* 3FD99999 9997FA04 */
24const LG3: f64 = 2.857142874366239149e-01; /* 3FD24924 94229359 */
25const LG4: f64 = 2.222219843214978396e-01; /* 3FCC71C5 1D8E78AF */
26const LG5: f64 = 1.818357216161805012e-01; /* 3FC74664 96CB03DE */
27const LG6: f64 = 1.531383769920937332e-01; /* 3FC39A09 D078C69F */
28const LG7: f64 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
29
30/// The base 2 logarithm of `x` (f64).
31#[cfg_attr(assert_no_panic, no_panic::no_panic)]
32pub fn log2(mut x: f64) -> f64 {
33 let x1p54 = f64::from_bits(0x4350000000000000); // 0x1p54 === 2 ^ 54
34
35 let mut ui: u64 = x.to_bits();
36 let hfsq: f64;
37 let f: f64;
38 let s: f64;
39 let z: f64;
40 let r: f64;
41 let mut w: f64;
42 let t1: f64;
43 let t2: f64;
44 let y: f64;
45 let mut hi: f64;
46 let lo: f64;
47 let mut val_hi: f64;
48 let mut val_lo: f64;
49 let mut hx: u32;
50 let mut k: i32;
51
52 hx = (ui >> 32) as u32;
53 k = 0;
54 if hx < 0x00100000 || (hx >> 31) > 0 {
55 if ui << 1 == 0 {
56 return -1. / (x * x); /* log(+-0)=-inf */
57 }
58 if (hx >> 31) > 0 {
59 return (x - x) / 0.0; /* log(-#) = NaN */
60 }
61 /* subnormal number, scale x up */
62 k -= 54;
63 x *= x1p54;
64 ui = x.to_bits();
65 hx = (ui >> 32) as u32;
66 } else if hx >= 0x7ff00000 {
67 return x;
68 } else if hx == 0x3ff00000 && ui << 32 == 0 {
69 return 0.;
70 }
71
72 /* reduce x into [sqrt(2)/2, sqrt(2)] */
73 hx += 0x3ff00000 - 0x3fe6a09e;
74 k += (hx >> 20) as i32 - 0x3ff;
75 hx = (hx & 0x000fffff) + 0x3fe6a09e;
76 ui = ((hx as u64) << 32) | (ui & 0xffffffff);
77 x = f64::from_bits(ui);
78
79 f = x - 1.0;
80 hfsq = 0.5 * f * f;
81 s = f / (2.0 + f);
82 z = s * s;
83 w = z * z;
84 t1 = w * (LG2 + w * (LG4 + w * LG6));
85 t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
86 r = t2 + t1;
87
88 /* hi+lo = f - hfsq + s*(hfsq+R) ~ log(1+f) */
89 hi = f - hfsq;
90 ui = hi.to_bits();
91 ui &= (-1i64 as u64) << 32;
92 hi = f64::from_bits(ui);
93 lo = f - hi - hfsq + s * (hfsq + r);
94
95 val_hi = hi * IVLN2HI;
96 val_lo = (lo + hi) * IVLN2LO + lo * IVLN2HI;
97
98 /* spadd(val_hi, val_lo, y), except for not using double_t: */
99 y = k.into();
100 w = y + val_hi;
101 val_lo += (y - w) + val_hi;
102 val_hi = w;
103
104 val_lo + val_hi
105}