Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit be9ba62

Browse files
committed
Add a generic version of scalbn
This replaces the `f32` and `f64` versions of `scalbn` and `ldexp`.
1 parent fd78ea9 commit be9ba62

File tree

6 files changed

+133
-59
lines changed

6 files changed

+133
-59
lines changed

libm/crates/libm-test/src/mpfloat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ libm_macros::for_each_function! {
158158
ilogbf,
159159
jn,
160160
jnf,
161-
ldexp,ldexpf,
161+
ldexp,
162+
ldexpf,
162163
lgamma_r,
163164
lgammaf_r,
164165
modf,

libm/etc/function-definitions.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,12 +698,14 @@
698698
"scalbn": {
699699
"sources": [
700700
"src/libm_helper.rs",
701+
"src/math/generic/scalbn.rs",
701702
"src/math/scalbn.rs"
702703
],
703704
"type": "f64"
704705
},
705706
"scalbnf": {
706707
"sources": [
708+
"src/math/generic/scalbn.rs",
707709
"src/math/scalbnf.rs"
708710
],
709711
"type": "f32"

libm/src/math/generic/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod fabs;
44
mod fdim;
55
mod floor;
66
mod rint;
7+
mod scalbn;
78
mod sqrt;
89
mod trunc;
910

@@ -13,5 +14,6 @@ pub use fabs::fabs;
1314
pub use fdim::fdim;
1415
pub use floor::floor;
1516
pub use rint::rint;
17+
pub use scalbn::scalbn;
1618
pub use sqrt::sqrt;
1719
pub use trunc::trunc;

libm/src/math/generic/scalbn.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use super::super::{CastFrom, CastInto, Float, IntTy, MinInt};
2+
3+
/// Scale the exponent.
4+
///
5+
/// From N3220:
6+
///
7+
/// > The scalbn and scalbln functions compute `x * b^n`, where `b = FLT_RADIX` if the return type
8+
/// > of the function is a standard floating type, or `b = 10` if the return type of the function
9+
/// > is a decimal floating type. A range error occurs for some finite x, depending on n.
10+
/// >
11+
/// > [...]
12+
/// >
13+
/// > * `scalbn(±0, n)` returns `±0`.
14+
/// > * `scalbn(x, 0)` returns `x`.
15+
/// > * `scalbn(±∞, n)` returns `±∞`.
16+
/// >
17+
/// > If the calculation does not overflow or underflow, the returned value is exact and
18+
/// > independent of the current rounding direction mode.
19+
pub fn scalbn<F: Float>(mut x: F, mut n: i32) -> F
20+
where
21+
u32: CastInto<F::Int>,
22+
F::Int: CastFrom<i32>,
23+
F::Int: CastFrom<u32>,
24+
{
25+
let zero = IntTy::<F>::ZERO;
26+
27+
// Bits including the implicit bit
28+
let sig_total_bits = F::SIG_BITS + 1;
29+
30+
// Maximum and minimum values when biased
31+
let exp_max: i32 = F::EXP_BIAS as i32;
32+
let exp_min = -(exp_max - 1);
33+
34+
// 2 ^ Emax, where Emax is the maximum biased exponent value (1023 for f64)
35+
let f_exp_max = F::from_parts(false, F::EXP_BIAS << 1, zero);
36+
37+
// 2 ^ Emin, where Emin is the minimum biased exponent value (-1022 for f64)
38+
let f_exp_min = F::from_parts(false, 1, zero);
39+
40+
// 2 ^ sig_total_bits, representation of what can be accounted for with subnormals
41+
let f_exp_subnorm = F::from_parts(false, sig_total_bits + F::EXP_BIAS, zero);
42+
43+
if n > exp_max {
44+
x *= f_exp_max;
45+
n -= exp_max;
46+
if n > exp_max {
47+
x *= f_exp_max;
48+
n -= exp_max;
49+
if n > exp_max {
50+
n = exp_max;
51+
}
52+
}
53+
} else if n < exp_min {
54+
let mul = f_exp_min * f_exp_subnorm;
55+
let add = (exp_max - 1) - sig_total_bits as i32;
56+
57+
x *= mul;
58+
n += add;
59+
if n < exp_min {
60+
x *= mul;
61+
n += add;
62+
if n < exp_min {
63+
n = exp_min;
64+
}
65+
}
66+
}
67+
68+
x * F::from_parts(false, (F::EXP_BIAS as i32 + n) as u32, zero)
69+
}
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use super::super::super::Int;
74+
use super::*;
75+
76+
// Tests against N3220
77+
fn spec_test<F: Float>()
78+
where
79+
u32: CastInto<F::Int>,
80+
F::Int: CastFrom<i32>,
81+
F::Int: CastFrom<u32>,
82+
{
83+
// `scalbn(±0, n)` returns `±0`.
84+
assert_biteq!(scalbn(F::NEG_ZERO, 10), F::NEG_ZERO);
85+
assert_biteq!(scalbn(F::NEG_ZERO, 0), F::NEG_ZERO);
86+
assert_biteq!(scalbn(F::NEG_ZERO, -10), F::NEG_ZERO);
87+
assert_biteq!(scalbn(F::ZERO, 10), F::ZERO);
88+
assert_biteq!(scalbn(F::ZERO, 0), F::ZERO);
89+
assert_biteq!(scalbn(F::ZERO, -10), F::ZERO);
90+
91+
// `scalbn(x, 0)` returns `x`.
92+
assert_biteq!(scalbn(F::MIN, 0), F::MIN);
93+
assert_biteq!(scalbn(F::MAX, 0), F::MAX);
94+
assert_biteq!(scalbn(F::INFINITY, 0), F::INFINITY);
95+
assert_biteq!(scalbn(F::NEG_INFINITY, 0), F::NEG_INFINITY);
96+
assert_biteq!(scalbn(F::ZERO, 0), F::ZERO);
97+
assert_biteq!(scalbn(F::NEG_ZERO, 0), F::NEG_ZERO);
98+
99+
// `scalbn(±∞, n)` returns `±∞`.
100+
assert_biteq!(scalbn(F::INFINITY, 10), F::INFINITY);
101+
assert_biteq!(scalbn(F::INFINITY, -10), F::INFINITY);
102+
assert_biteq!(scalbn(F::NEG_INFINITY, 10), F::NEG_INFINITY);
103+
assert_biteq!(scalbn(F::NEG_INFINITY, -10), F::NEG_INFINITY);
104+
105+
// NaN should remain NaNs.
106+
assert!(scalbn(F::NAN, 10).is_nan());
107+
assert!(scalbn(F::NAN, 0).is_nan());
108+
assert!(scalbn(F::NAN, -10).is_nan());
109+
assert!(scalbn(-F::NAN, 10).is_nan());
110+
assert!(scalbn(-F::NAN, 0).is_nan());
111+
assert!(scalbn(-F::NAN, -10).is_nan());
112+
}
113+
114+
#[test]
115+
fn spec_test_f32() {
116+
spec_test::<f32>();
117+
}
118+
119+
#[test]
120+
fn spec_test_f64() {
121+
spec_test::<f64>();
122+
}
123+
}

libm/src/math/scalbn.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,4 @@
11
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2-
pub fn scalbn(x: f64, mut n: i32) -> f64 {
3-
let x1p1023 = f64::from_bits(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023
4-
let x1p53 = f64::from_bits(0x4340000000000000); // 0x1p53 === 2 ^ 53
5-
let x1p_1022 = f64::from_bits(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022)
6-
7-
let mut y = x;
8-
9-
if n > 1023 {
10-
y *= x1p1023;
11-
n -= 1023;
12-
if n > 1023 {
13-
y *= x1p1023;
14-
n -= 1023;
15-
if n > 1023 {
16-
n = 1023;
17-
}
18-
}
19-
} else if n < -1022 {
20-
/* make sure final n < -53 to avoid double
21-
rounding in the subnormal range */
22-
y *= x1p_1022 * x1p53;
23-
n += 1022 - 53;
24-
if n < -1022 {
25-
y *= x1p_1022 * x1p53;
26-
n += 1022 - 53;
27-
if n < -1022 {
28-
n = -1022;
29-
}
30-
}
31-
}
32-
y * f64::from_bits(((0x3ff + n) as u64) << 52)
2+
pub fn scalbn(x: f64, n: i32) -> f64 {
3+
super::generic::scalbn(x, n)
334
}

libm/src/math/scalbnf.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
11
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2-
pub fn scalbnf(mut x: f32, mut n: i32) -> f32 {
3-
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
4-
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
5-
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
6-
7-
if n > 127 {
8-
x *= x1p127;
9-
n -= 127;
10-
if n > 127 {
11-
x *= x1p127;
12-
n -= 127;
13-
if n > 127 {
14-
n = 127;
15-
}
16-
}
17-
} else if n < -126 {
18-
x *= x1p_126 * x1p24;
19-
n += 126 - 24;
20-
if n < -126 {
21-
x *= x1p_126 * x1p24;
22-
n += 126 - 24;
23-
if n < -126 {
24-
n = -126;
25-
}
26-
}
27-
}
28-
x * f32::from_bits(((0x7f + n) as u32) << 23)
2+
pub fn scalbnf(x: f32, n: i32) -> f32 {
3+
super::generic::scalbn(x, n)
294
}

0 commit comments

Comments
 (0)