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