Coverage Report

Created: 2026-07-16 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.30/src/exponents/exp10f.rs
Line
Count
Source
1
/*
2
 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3
 * //
4
 * // Redistribution and use in source and binary forms, with or without modification,
5
 * // are permitted provided that the following conditions are met:
6
 * //
7
 * // 1.  Redistributions of source code must retain the above copyright notice, this
8
 * // list of conditions and the following disclaimer.
9
 * //
10
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11
 * // this list of conditions and the following disclaimer in the documentation
12
 * // and/or other materials provided with the distribution.
13
 * //
14
 * // 3.  Neither the name of the copyright holder nor the names of its
15
 * // contributors may be used to endorse or promote products derived from
16
 * // this software without specific prior written permission.
17
 * //
18
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
use crate::exponents::expf::{ExpfBackend, GenericExpfBackend};
30
31
pub(crate) struct ExpBReduc {
32
    pub(crate) hi: f64,
33
    pub(crate) lo: f64,
34
}
35
36
const MID_BITS: u32 = 5;
37
const MID_MASK: usize = (1 << MID_BITS) - 1;
38
const LOG2_B: f64 = f64::from_bits(0x400a934f0979a371) * (1 << MID_BITS) as f64;
39
const M_LOGB_2_HI: f64 = f64::from_bits(0xbfd34413509f8000) / (1 << MID_BITS) as f64;
40
const M_LOGB_2_LO: f64 = f64::from_bits(0x3d380433b83b532a) / (1 << MID_BITS) as f64;
41
const EXP_2_MID: [u64; 32] = [
42
    0x3ff0000000000000,
43
    0x3ff059b0d3158574,
44
    0x3ff0b5586cf9890f,
45
    0x3ff11301d0125b51,
46
    0x3ff172b83c7d517b,
47
    0x3ff1d4873168b9aa,
48
    0x3ff2387a6e756238,
49
    0x3ff29e9df51fdee1,
50
    0x3ff306fe0a31b715,
51
    0x3ff371a7373aa9cb,
52
    0x3ff3dea64c123422,
53
    0x3ff44e086061892d,
54
    0x3ff4bfdad5362a27,
55
    0x3ff5342b569d4f82,
56
    0x3ff5ab07dd485429,
57
    0x3ff6247eb03a5585,
58
    0x3ff6a09e667f3bcd,
59
    0x3ff71f75e8ec5f74,
60
    0x3ff7a11473eb0187,
61
    0x3ff82589994cce13,
62
    0x3ff8ace5422aa0db,
63
    0x3ff93737b0cdc5e5,
64
    0x3ff9c49182a3f090,
65
    0x3ffa5503b23e255d,
66
    0x3ffae89f995ad3ad,
67
    0x3ffb7f76f2fb5e47,
68
    0x3ffc199bdd85529c,
69
    0x3ffcb720dcef9069,
70
    0x3ffd5818dcfba487,
71
    0x3ffdfc97337b9b5f,
72
    0x3ffea4afa2a490da,
73
    0x3fff50765b6e4540,
74
];
75
76
// Approximating 10^dx with degree-5 minimax polynomial generated by Sollya:
77
// > Q = fpminimax((10^x - 1)/x, 4, [|D...|], [-log10(2)/2^6, log10(2)/2^6]);
78
// Then:
79
//   10^dx ~ P(dx) = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5.
80
pub(crate) const EXP10F_COEFFS: [u64; 5] = [
81
    0x40026bb1bbb55515,
82
    0x40053524c73bd3ea,
83
    0x4000470591dff149,
84
    0x3ff2bd7c0a9fbc4d,
85
    0x3fe1429e74a98f43,
86
];
87
88
/// Range reduction function equivalent to exp_b_range_reduc
89
#[inline(always)]
90
0
pub(crate) fn exp_b_range_reduc<B: ExpfBackend>(x: f32, backend: &B) -> ExpBReduc {
91
0
    let xd = x as f64;
92
93
    // kd = round(log2(b) * x)
94
0
    let kd = backend.round(LOG2_B * xd);
95
0
    let k = unsafe { kd.to_int_unchecked::<i32>() }; // it's already not indeterminate.
96
97
    // hi = floor(kd / 2^MID_BITS)
98
0
    let exp_hi = (k.wrapping_shr(MID_BITS) as u64).wrapping_shl(52); // 52 = fraction bits in f64
99
100
    // mh = 2^hi * 2^mid
101
0
    let mid_index = (k as usize) & MID_MASK;
102
0
    let mh_bits = EXP_2_MID[mid_index].wrapping_add(exp_hi);
103
0
    let mh = f64::from_bits(mh_bits);
104
105
    // dx = x - (hi + mid) * log(2)
106
0
    let z0 = backend.fma(kd, M_LOGB_2_HI, xd);
107
0
    let dx = backend.fma(kd, M_LOGB_2_LO, z0);
108
109
0
    ExpBReduc { lo: dx, hi: mh }
110
0
}
Unexecuted instantiation: pxfm::exponents::exp10f::exp_b_range_reduc::<pxfm::exponents::expf::FmaBackend>
Unexecuted instantiation: pxfm::exponents::exp10f::exp_b_range_reduc::<pxfm::exponents::expf::GenericExpfBackend>
111
112
#[inline(always)]
113
0
fn exp10f_gen<B: ExpfBackend>(x: f32, backend: B) -> f32 {
114
0
    let x_u = x.to_bits();
115
0
    let x_abs = x_u & 0x7fff_ffff;
116
117
    // When |x| >= log10(2^128), or x is nan
118
0
    if x_abs >= 0x421a209bu32 {
119
0
        if x_u.wrapping_shl(1) >= 0xffu32 << 24 {
120
            // |x| == inf, |x| == NaN
121
0
            if x.is_sign_negative() && x.is_infinite() {
122
0
                return 0.0;
123
0
            } else if x.is_infinite() {
124
0
                return f32::INFINITY;
125
0
            }
126
0
            return x + f32::NAN; // x == NaN
127
0
        }
128
        // When x < log10(2^-150) or nan
129
0
        if x_u > 0xc2349e35u32 {
130
            // exp(-Inf) = 0
131
0
            if x.is_infinite() {
132
0
                return 0.0;
133
0
            }
134
            // exp(nan) = nan
135
0
            if x.is_nan() {
136
0
                return x;
137
0
            }
138
0
            return 0.0;
139
0
        }
140
        // x >= log10(2^128) or nan
141
0
        if x > 0. && (x_u >= 0x421a209bu32) {
142
            // x is +inf or nan
143
0
            return x + f32::INFINITY;
144
0
        }
145
0
    }
146
147
0
    if x_abs <= 0x3d000000u32 {
148
        // |x| < 1/32
149
0
        if x_abs <= 0x3b9a209bu32 {
150
0
            if x_u == 0xb25e5bd9u32 {
151
                // x = -1.2943e-08
152
0
                return 1.;
153
0
            }
154
            // |x| < 2^-25
155
            // 10^x ~ 1 + log(10) * x
156
0
            if x_abs <= 0x32800000u32 {
157
0
                return backend.fmaf(x, f32::from_bits(0x40135da2), 1.0);
158
0
            }
159
0
        }
160
161
0
        let xd = x as f64;
162
163
        // Special polynomial for small x.
164
        // Generated by Sollya:
165
        // d = [-1/32, 1/32];
166
        // f_exp10f = (10^y - 1)/y;
167
        // Q = fpminimax(f_exp10f, 6, [|D...|], d, relative, floating);
168
169
        // See ./notes/exp10f_small.sollya
170
0
        let p = backend.polyeval7(
171
0
            xd,
172
0
            f64::from_bits(0x40026bb1bbb55516),
173
0
            f64::from_bits(0x40053524c73cfbf6),
174
0
            f64::from_bits(0x4000470591de0b07),
175
0
            f64::from_bits(0x3ff2bd760599f3a5),
176
0
            f64::from_bits(0x3fe142a001511a6f),
177
0
            f64::from_bits(0x3fca7feffa781d53),
178
0
            f64::from_bits(0x3fb16e53492c0f0e),
179
        );
180
0
        return backend.fma(p, xd, 1.) as f32;
181
0
    }
182
183
    // Range reduction: 10^x = 2^(mid + hi) * 10^lo
184
    //   rr = (2^(mid + hi), lo)
185
0
    let rr = exp_b_range_reduc(x, &backend);
186
187
    // The low part is approximated by a degree-5 minimax polynomial.
188
    // 10^lo ~ 1 + COEFFS[0] * lo + ... + COEFFS[4] * lo^5
189
0
    let lo2 = rr.lo * rr.lo;
190
    // c0 = 1 + COEFFS[0] * lo
191
0
    let c0 = backend.fma(rr.lo, f64::from_bits(EXP10F_COEFFS[0]), 1.0);
192
    // c1 = COEFFS[1] + COEFFS[2] * lo
193
0
    let c1 = backend.fma(
194
0
        rr.lo,
195
0
        f64::from_bits(EXP10F_COEFFS[2]),
196
0
        f64::from_bits(EXP10F_COEFFS[1]),
197
    );
198
    // c2 = COEFFS[3] + COEFFS[4] * lo
199
0
    let c2 = backend.fma(
200
0
        rr.lo,
201
0
        f64::from_bits(EXP10F_COEFFS[4]),
202
0
        f64::from_bits(EXP10F_COEFFS[3]),
203
    );
204
    // p = c1 + c2 * lo^2
205
    //   = COEFFS[1] + COEFFS[2] * lo + COEFFS[3] * lo^2 + COEFFS[4] * lo^3
206
0
    let p = backend.fma(lo2, c2, c1);
207
    // 10^lo ~ c0 + p * lo^2
208
    // 10^x = 2^(mid + hi) * 10^lo
209
    //      ~ mh * (c0 + p * lo^2)
210
    //      = (mh * c0) + p * (mh * lo^2)
211
0
    backend.fma(p, lo2 * rr.hi, c0 * rr.hi) as f32
212
0
}
Unexecuted instantiation: pxfm::exponents::exp10f::exp10f_gen::<pxfm::exponents::expf::FmaBackend>
Unexecuted instantiation: pxfm::exponents::exp10f::exp10f_gen::<pxfm::exponents::expf::GenericExpfBackend>
213
214
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
215
#[target_feature(enable = "avx", enable = "fma")]
216
0
unsafe fn exp10f_fma_impl(x: f32) -> f32 {
217
    use crate::exponents::expf::FmaBackend;
218
0
    exp10f_gen(x, FmaBackend {})
219
0
}
220
221
/// Computes exp10
222
///
223
/// Max found ULP 0.49999508
224
#[inline]
225
0
pub fn f_exp10f(x: f32) -> f32 {
226
    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
227
    {
228
        exp10f_gen(x, GenericExpfBackend {})
229
    }
230
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
231
    {
232
        use std::sync::OnceLock;
233
        static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new();
234
0
        let q = EXECUTOR.get_or_init(|| {
235
0
            if std::arch::is_x86_feature_detected!("avx")
236
0
                && std::arch::is_x86_feature_detected!("fma")
237
            {
238
0
                exp10f_fma_impl
239
            } else {
240
0
                fn def_exp10f(x: f32) -> f32 {
241
0
                    exp10f_gen(x, GenericExpfBackend {})
242
0
                }
243
0
                def_exp10f
244
            }
245
0
        });
246
0
        unsafe { q(x) }
247
    }
248
0
}
249
250
#[cfg(test)]
251
mod tests {
252
    use super::*;
253
254
    #[test]
255
    fn test_exp10f() {
256
        assert!(f_exp10f(f32::from_bits(0x7fc0_0000)).is_nan());
257
        assert_eq!(f_exp10f(-1. / 64.), 0.9646616);
258
        assert_eq!(f_exp10f(1. / 64.), 1.0366329);
259
        assert_eq!(f_exp10f(1.), 10.0);
260
        assert_eq!(f_exp10f(2.), 100.0);
261
        assert_eq!(f_exp10f(3.), 1000.0);
262
        assert_eq!(f_exp10f(f32::INFINITY), f32::INFINITY);
263
        assert_eq!(f_exp10f(f32::NEG_INFINITY), 0.);
264
        assert!(f_exp10f(f32::NAN).is_nan());
265
    }
266
}