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

Commit f8e5fc7

Browse files
committed
Add scalbnf16 and scalbnf128
The `scalbn` functions are similar enough that they can easily be made generic. Do so and `f16` and `f128` versions.
1 parent 6ee9aeb commit f8e5fc7

File tree

17 files changed

+174
-92
lines changed

17 files changed

+174
-92
lines changed

crates/libm-macros/src/shared.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
134134
None,
135135
&["jn", "yn"],
136136
),
137+
(
138+
// `(f16, i32) -> f16`
139+
FloatTy::F16,
140+
Signature { args: &[Ty::F16, Ty::I32], returns: &[Ty::F16] },
141+
None,
142+
&["scalbnf16", "ldexpf16"],
143+
),
137144
(
138145
// `(f32, i32) -> f32`
139146
FloatTy::F32,
@@ -148,6 +155,13 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
148155
None,
149156
&["scalbn", "ldexp"],
150157
),
158+
(
159+
// `(f128, i32) -> f128`
160+
FloatTy::F128,
161+
Signature { args: &[Ty::F128, Ty::I32], returns: &[Ty::F128] },
162+
None,
163+
&["scalbnf128", "ldexpf128"],
164+
),
151165
(
152166
// `(f32, &mut f32) -> f32` as `(f32) -> (f32, f32)`
153167
FloatTy::F32,

crates/libm-test/benches/random.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ libm_macros::for_each_function! {
117117
exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)),
118118

119119
// Musl does not provide `f16` and `f128` functions
120-
copysignf16 | copysignf128 | fabsf16 | fabsf128 => (false, None),
120+
copysignf16 | copysignf128 |
121+
fabsf16 | fabsf128 |
122+
ldexpf16 | ldexpf128 |
123+
scalbnf16 | scalbnf128 => (false, None),
121124

122125
// By default we never skip (false) and always have a musl function available
123126
_ => (false, Some(musl_math_sys::MACRO_FN_NAME))

crates/libm-test/src/mpfloat.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ libm_macros::for_each_function! {
137137
fmod, fmodf, frexp, frexpf, ilogb, ilogbf, jn, jnf, ldexp, ldexpf,
138138
lgamma_r, lgammaf_r, modf, modff, nextafter, nextafterf, pow,powf,
139139
remquo, remquof, scalbn, scalbnf, sincos, sincosf, yn, ynf,
140-
copysignf16, copysignf128, fabsf16, fabsf128,
140+
copysignf16, copysignf128, fabsf16, fabsf128, ldexpf16, ldexpf128,
141+
scalbnf16, scalbnf128,
141142
],
142143
fn_extra: match MACRO_FN_NAME {
143144
// Remap function names that are different between mpfr and libm
@@ -255,36 +256,6 @@ macro_rules! impl_op_for_ty {
255256
}
256257
}
257258

258-
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
259-
// methods.
260-
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
261-
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
262-
263-
fn new_mp() -> Self::MpTy {
264-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
265-
}
266-
267-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
268-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
269-
}
270-
}
271-
272-
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
273-
type MpTy = (MpFloat, MpFloat);
274-
275-
fn new_mp() -> Self::MpTy {
276-
(new_mpfloat::<Self::FTy>(), new_mpfloat::<Self::FTy>())
277-
}
278-
279-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
280-
this.0.assign(input.0);
281-
this.1.assign(2.0);
282-
this.1.pow_assign(input.1);
283-
let ord = this.0.mul_assign_round(&this.1, Nearest);
284-
prep_retval::<Self::FTy>(&mut this.0, ord)
285-
}
286-
}
287-
288259
impl MpOp for crate::op::[<sincos $suffix>]::Routine {
289260
type MpTy = (MpFloat, MpFloat);
290261

@@ -339,6 +310,36 @@ macro_rules! impl_op_for_ty_all {
339310
prep_retval::<Self::RustRet>(&mut this.0, Ordering::Equal)
340311
}
341312
}
313+
314+
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
315+
// methods.
316+
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
317+
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
318+
319+
fn new_mp() -> Self::MpTy {
320+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
321+
}
322+
323+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
324+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
325+
}
326+
}
327+
328+
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
329+
type MpTy = (MpFloat, MpFloat);
330+
331+
fn new_mp() -> Self::MpTy {
332+
(new_mpfloat::<Self::FTy>(), new_mpfloat::<Self::FTy>())
333+
}
334+
335+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
336+
this.0.assign(input.0);
337+
this.1.assign(2.0);
338+
this.1.pow_assign(input.1);
339+
let ord = this.0.mul_assign_round(&this.1, Nearest);
340+
prep_retval::<Self::FTy>(&mut this.0, ord)
341+
}
342+
}
342343
}
343344
};
344345
}

crates/libm-test/src/precision.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,5 +454,9 @@ fn bessel_prec_dropoff<F: Float>(
454454

455455
impl MaybeOverride<(f32, f32, f32)> for SpecialCase {}
456456
impl MaybeOverride<(f64, f64, f64)> for SpecialCase {}
457+
#[cfg(f16_enabled)]
458+
impl MaybeOverride<(f16, i32)> for SpecialCase {}
457459
impl MaybeOverride<(f32, i32)> for SpecialCase {}
458460
impl MaybeOverride<(f64, i32)> for SpecialCase {}
461+
#[cfg(f128_enabled)]
462+
impl MaybeOverride<(f128, i32)> for SpecialCase {}

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ where
4747
libm_macros::for_each_function! {
4848
callback: musl_rand_tests,
4949
// Musl does not support `f16` and `f128` on all platforms.
50-
skip: [copysignf16, copysignf128, fabsf16, fabsf128],
50+
skip: [
51+
copysignf16, copysignf128, fabsf16, fabsf128,
52+
ldexpf16, ldexpf128, scalbnf16, scalbnf128,
53+
],
5154
attributes: [
5255
#[cfg_attr(x86_no_sse, ignore)] // FIXME(correctness): wrong result on i586
5356
[exp10, exp10f, exp2, exp2f, rint]

crates/libm-test/tests/multiprecision.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ libm_macros::for_each_function! {
144144
jnf,
145145
ldexp,
146146
ldexpf,
147+
ldexpf16,
148+
ldexpf128,
147149
nextafter,
148150
nextafterf,
149151
pow,
@@ -154,6 +156,8 @@ libm_macros::for_each_function! {
154156
remquof,
155157
scalbn,
156158
scalbnf,
159+
scalbnf16,
160+
scalbnf128,
157161
yn,
158162
ynf,
159163

etc/function-definitions.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,18 @@
458458
],
459459
"type": "f32"
460460
},
461+
"ldexpf128": {
462+
"sources": [
463+
"src/math/ldexpf128.rs"
464+
],
465+
"type": "f128"
466+
},
467+
"ldexpf16": {
468+
"sources": [
469+
"src/math/ldexpf16.rs"
470+
],
471+
"type": "f16"
472+
},
461473
"lgamma": {
462474
"sources": [
463475
"src/libm_helper.rs",
@@ -630,16 +642,32 @@
630642
"scalbn": {
631643
"sources": [
632644
"src/libm_helper.rs",
645+
"src/math/generic/scalbn.rs",
633646
"src/math/scalbn.rs"
634647
],
635648
"type": "f64"
636649
},
637650
"scalbnf": {
638651
"sources": [
652+
"src/math/generic/scalbn.rs",
639653
"src/math/scalbnf.rs"
640654
],
641655
"type": "f32"
642656
},
657+
"scalbnf128": {
658+
"sources": [
659+
"src/math/generic/scalbn.rs",
660+
"src/math/scalbnf128.rs"
661+
],
662+
"type": "f128"
663+
},
664+
"scalbnf16": {
665+
"sources": [
666+
"src/math/generic/scalbn.rs",
667+
"src/math/scalbnf16.rs"
668+
],
669+
"type": "f16"
670+
},
643671
"sin": {
644672
"sources": [
645673
"src/libm_helper.rs",

etc/function-list.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ jn
6767
jnf
6868
ldexp
6969
ldexpf
70+
ldexpf128
71+
ldexpf16
7072
lgamma
7173
lgamma_r
7274
lgammaf
@@ -95,6 +97,8 @@ round
9597
roundf
9698
scalbn
9799
scalbnf
100+
scalbnf128
101+
scalbnf16
98102
sin
99103
sincos
100104
sincosf

src/math/generic/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod copysign;
22
mod fabs;
3+
mod scalbn;
34

45
pub use copysign::copysign;
56
pub use fabs::fabs;
7+
pub use scalbn::scalbn;

src/math/generic/scalbn.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use super::super::{CastFrom, CastInto, Float, IntTy, MinInt};
2+
3+
pub fn scalbn<F: Float>(mut x: F, mut n: i32) -> F
4+
where
5+
u32: CastInto<F::Int>,
6+
F::Int: CastFrom<i32>,
7+
F::Int: CastFrom<u32>,
8+
{
9+
// Bits including the implicit bit
10+
let sig_total_bits = F::SIG_BITS + 1;
11+
12+
// Maximum and minimum values when biased
13+
let exp_max: i32 = F::EXP_BIAS as i32;
14+
let exp_min = -(exp_max - 1);
15+
16+
// 2 ^ Emax, where Emax is the maximum biased exponent value (1023 for f64)
17+
let f_exp_max = F::from_bits(F::Int::cast_from(F::EXP_BIAS << 1) << F::SIG_BITS);
18+
// 2 ^ Emin, where Emin is the minimum biased exponent value (-1022 for f64)
19+
let f_exp_min = F::from_bits(IntTy::<F>::ONE << F::SIG_BITS);
20+
// 2 ^ sig_total_bits, representation of what can be accounted for with subnormals
21+
let f_exp_subnorm = F::from_bits((F::EXP_BIAS + sig_total_bits).cast() << F::SIG_BITS);
22+
23+
if n > exp_max {
24+
x *= f_exp_max;
25+
n -= exp_max;
26+
if n > exp_max {
27+
x *= f_exp_max;
28+
n -= exp_max;
29+
if n > exp_max {
30+
n = exp_max;
31+
}
32+
}
33+
} else if n < exp_min {
34+
let mul = f_exp_min * f_exp_subnorm;
35+
let add = (exp_max - 1) - sig_total_bits as i32;
36+
37+
x *= mul;
38+
n += add;
39+
if n < exp_min {
40+
x *= mul;
41+
n += add;
42+
if n < exp_min {
43+
n = exp_min;
44+
}
45+
}
46+
}
47+
48+
x * F::from_bits(F::Int::cast_from(F::EXP_BIAS as i32 + n) << F::SIG_BITS)
49+
}

0 commit comments

Comments
 (0)