pxfm/exponents/
auxiliary.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/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 */
29use crate::dyadic_float::{DyadicSign, f64_from_parts};
30
31#[inline]
32pub(crate) fn ldexp(d: f64, i: i32) -> f64 {
33    let mut n = i;
34    let exp_max = 1023;
35    let exp_min = -1022;
36
37    const EXP_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
38    // 2 ^ Emax, maximum positive with null significand (0x1p1023 for f64)
39    let f_exp_max = f64_from_parts(DyadicSign::Pos, EXP_BIAS << 1, 0);
40
41    // 2 ^ Emin, minimum positive normal with null significand (0x1p-1022 for f64)
42    let f_exp_min = f64_from_parts(DyadicSign::Pos, 1, 0);
43
44    let mut x = d;
45
46    if n < exp_min {
47        // 2 ^ sig_total_bits, moltiplier to normalize subnormals (0x1p53 for f64)
48        let f_pow_subnorm = f64_from_parts(DyadicSign::Pos, 52 + EXP_BIAS, 0);
49
50        let mul = f_exp_min * f_pow_subnorm;
51        let add = -exp_min - 52i32;
52
53        // Worse case negative `n`: `x`  is the maximum positive value, the result is `F::MIN`.
54        // This must be reachable by three scaling multiplications (two here and one final).
55        debug_assert!(-exp_min + 52i32 + exp_max <= add * 2 + -exp_min);
56
57        x *= mul;
58        n += add;
59
60        if n < exp_min {
61            x *= mul;
62            n += add;
63
64            if n < exp_min {
65                n = exp_min;
66            }
67        }
68    } else if n > exp_max {
69        x *= f_exp_max;
70        n -= exp_max;
71        if n > exp_max {
72            x *= f_exp_max;
73            n -= exp_max;
74            if n > exp_max {
75                n = exp_max;
76            }
77        }
78    }
79
80    let scale = f64_from_parts(DyadicSign::Pos, (EXP_BIAS as i32 + n) as u64, 0);
81    x * scale
82}
83
84#[inline]
85pub(crate) fn fast_ldexp(d: f64, i: i32) -> f64 {
86    let mut u = d.to_bits();
87    u = u.wrapping_add((i as u64).wrapping_shl(52));
88    f64::from_bits(u)
89}