/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.29/src/exponents/auxiliary.rs
Line | Count | Source |
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 | | */ |
29 | | use crate::dyadic_float::{DyadicSign, f64_from_parts}; |
30 | | |
31 | | #[inline] |
32 | 0 | pub(crate) fn ldexp(d: f64, i: i32) -> f64 { |
33 | 0 | let mut n = i; |
34 | 0 | let exp_max = 1023; |
35 | 0 | 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 | 0 | 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 | 0 | let f_exp_min = f64_from_parts(DyadicSign::Pos, 1, 0); |
43 | | |
44 | 0 | let mut x = d; |
45 | | |
46 | 0 | if n < exp_min { |
47 | | // 2 ^ sig_total_bits, moltiplier to normalize subnormals (0x1p53 for f64) |
48 | 0 | let f_pow_subnorm = f64_from_parts(DyadicSign::Pos, 52 + EXP_BIAS, 0); |
49 | | |
50 | 0 | let mul = f_exp_min * f_pow_subnorm; |
51 | 0 | 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 | 0 | debug_assert!(-exp_min + 52i32 + exp_max <= add * 2 + -exp_min); |
56 | | |
57 | 0 | x *= mul; |
58 | 0 | n += add; |
59 | | |
60 | 0 | if n < exp_min { |
61 | 0 | x *= mul; |
62 | 0 | n += add; |
63 | | |
64 | 0 | if n < exp_min { |
65 | 0 | n = exp_min; |
66 | 0 | } |
67 | 0 | } |
68 | 0 | } else if n > exp_max { |
69 | 0 | x *= f_exp_max; |
70 | 0 | n -= exp_max; |
71 | 0 | if n > exp_max { |
72 | 0 | x *= f_exp_max; |
73 | 0 | n -= exp_max; |
74 | 0 | if n > exp_max { |
75 | 0 | n = exp_max; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | 0 | let scale = f64_from_parts(DyadicSign::Pos, (EXP_BIAS as i32 + n) as u64, 0); |
81 | 0 | x * scale |
82 | 0 | } |
83 | | |
84 | | #[inline] |
85 | 0 | pub(crate) fn fast_ldexp(d: f64, i: i32) -> f64 { |
86 | 0 | let mut u = d.to_bits(); |
87 | 0 | u = u.wrapping_add((i as u64).wrapping_shl(52)); |
88 | 0 | f64::from_bits(u) |
89 | 0 | } |