1/* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.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 log10.c.
14 */
15
16use core::f32;
17
18const IVLN10HI: f32 = 4.3432617188e-01; /* 0x3ede6000 */
19const IVLN10LO: f32 = -3.1689971365e-05; /* 0xb804ead9 */
20const LOG10_2HI: f32 = 3.0102920532e-01; /* 0x3e9a2080 */
21const LOG10_2LO: f32 = 7.9034151668e-07; /* 0x355427db */
22/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
23const LG1: f32 = 0.66666662693; /* 0xaaaaaa.0p-24 */
24const LG2: f32 = 0.40000972152; /* 0xccce13.0p-25 */
25const LG3: f32 = 0.28498786688; /* 0x91e9ee.0p-25 */
26const LG4: f32 = 0.24279078841; /* 0xf89e26.0p-26 */
27
28#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
29pub fn log10f(mut x: f32) -> f32 {
30 let x1p25f = f32::from_bits(0x4c000000); // 0x1p25f === 2 ^ 25
31
32 let mut ui: u32 = x.to_bits();
33 let hfsq: f32;
34 let f: f32;
35 let s: f32;
36 let z: f32;
37 let r: f32;
38 let w: f32;
39 let t1: f32;
40 let t2: f32;
41 let dk: f32;
42 let mut hi: f32;
43 let lo: f32;
44 let mut ix: u32;
45 let mut k: i32;
46
47 ix = ui;
48 k = 0;
49 if ix < 0x00800000 || (ix >> 31) > 0 {
50 /* x < 2**-126 */
51 if ix << 1 == 0 {
52 return -1. / (x * x); /* log(+-0)=-inf */
53 }
54 if (ix >> 31) > 0 {
55 return (x - x) / 0.0; /* log(-#) = NaN */
56 }
57 /* subnormal number, scale up x */
58 k -= 25;
59 x *= x1p25f;
60 ui = x.to_bits();
61 ix = ui;
62 } else if ix >= 0x7f800000 {
63 return x;
64 } else if ix == 0x3f800000 {
65 return 0.;
66 }
67
68 /* reduce x into [sqrt(2)/2, sqrt(2)] */
69 ix += 0x3f800000 - 0x3f3504f3;
70 k += (ix >> 23) as i32 - 0x7f;
71 ix = (ix & 0x007fffff) + 0x3f3504f3;
72 ui = ix;
73 x = f32::from_bits(ui);
74
75 f = x - 1.0;
76 s = f / (2.0 + f);
77 z = s * s;
78 w = z * z;
79 t1 = w * (LG2 + w * LG4);
80 t2 = z * (LG1 + w * LG3);
81 r = t2 + t1;
82 hfsq = 0.5 * f * f;
83
84 hi = f - hfsq;
85 ui = hi.to_bits();
86 ui &= 0xfffff000;
87 hi = f32::from_bits(ui);
88 lo = f - hi - hfsq + s * (hfsq + r);
89 dk = k as f32;
90 dk * LOG10_2LO + (lo + hi) * IVLN10LO + lo * IVLN10HI + hi * IVLN10HI + dk * LOG10_2HI
91}
92