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

Commit 3222e99

Browse files
committed
Add a generic version of scalbn
1 parent 459dd80 commit 3222e99

File tree

6 files changed

+93
-59
lines changed

6 files changed

+93
-59
lines changed

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,

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"

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;

src/math/generic/scalbn.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// if n == 0 || x == F::ZERO || x.is_nan() || x.is_infinite() {
28+
// return x;
29+
// }
30+
31+
// Bits including the implicit bit
32+
let sig_total_bits = F::SIG_BITS + 1;
33+
34+
// Maximum and minimum values when biased
35+
let exp_max: i32 = F::EXP_BIAS as i32;
36+
let exp_min = -(exp_max - 1);
37+
// let exp_min_with_subnorm = -((F::EXP_BIAS + F::SIG_BITS + 1) as i32);
38+
39+
// // let x_exp = x.exp();
40+
// // let x_sig = x.frac();
41+
42+
// if n > exp_max {
43+
// return F::INFINITY * x.signum();
44+
// }
45+
46+
// if n < exp_min_with_subnorm {
47+
// return F::ZERO * x.signum();
48+
// }
49+
50+
// 2 ^ Emax, where Emax is the maximum biased exponent value (1023 for f64)
51+
let f_exp_max = F::from_bits(F::Int::cast_from(F::EXP_BIAS << 1) << F::SIG_BITS);
52+
// 2 ^ Emin, where Emin is the minimum biased exponent value (-1022 for f64)
53+
let f_exp_min = F::from_bits(IntTy::<F>::ONE << F::SIG_BITS);
54+
// 2 ^ sig_total_bits, representation of what can be accounted for with subnormals
55+
let f_exp_subnorm = F::from_parts(false, sig_total_bits + F::EXP_BIAS, zero);
56+
57+
if n > exp_max {
58+
x *= f_exp_max;
59+
n -= exp_max;
60+
if n > exp_max {
61+
x *= f_exp_max;
62+
n -= exp_max;
63+
if n > exp_max {
64+
n = exp_max;
65+
}
66+
}
67+
} else if n < exp_min {
68+
let mul = f_exp_min * f_exp_subnorm;
69+
let add = (exp_max - 1) - sig_total_bits as i32;
70+
71+
x *= mul;
72+
n += add;
73+
if n < exp_min {
74+
x *= mul;
75+
n += add;
76+
if n < exp_min {
77+
n = exp_min;
78+
}
79+
}
80+
}
81+
82+
x * F::from_bits(F::Int::cast_from(F::EXP_BIAS as i32 + n) << F::SIG_BITS)
83+
}

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
}

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)