1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const LN2_HI: f32 = 6.9313812256e-01;
const LN2_LO: f32 = 9.0580006145e-06;
const LG1: f32 = 0.66666662693;
const LG2: f32 = 0.40000972152;
const LG3: f32 = 0.28498786688;
const LG4: f32 = 0.24279078841;
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn logf(mut x: f32) -> f32 {
let x1p25 = f32::from_bits(0x4c000000);
let mut ix = x.to_bits();
let mut k = 0i32;
if (ix < 0x00800000) || ((ix >> 31) != 0) {
if ix << 1 == 0 {
return -1. / (x * x);
}
if (ix >> 31) != 0 {
return (x - x) / 0.;
}
k -= 25;
x *= x1p25;
ix = x.to_bits();
} else if ix >= 0x7f800000 {
return x;
} else if ix == 0x3f800000 {
return 0.;
}
ix += 0x3f800000 - 0x3f3504f3;
k += ((ix >> 23) as i32) - 0x7f;
ix = (ix & 0x007fffff) + 0x3f3504f3;
x = f32::from_bits(ix);
let f = x - 1.;
let s = f / (2. + f);
let z = s * s;
let w = z * z;
let t1 = w * (LG2 + w * LG4);
let t2 = z * (LG1 + w * LG3);
let r = t2 + t1;
let hfsq = 0.5 * f * f;
let dk = k as f32;
s * (hfsq + r) + dk * LN2_LO - hfsq + f + dk * LN2_HI
}