Skip to content

Commit 7394ee7

Browse files
committed
Add scalbnf16, scalbnf128, ldexpf16, and ldexpf128
Use the generic `scalbn` to provide `f16` and `f128` versions, which also work for `ldexp`.
1 parent 39d645d commit 7394ee7

File tree

13 files changed

+116
-28
lines changed

13 files changed

+116
-28
lines changed

crates/libm-macros/src/shared.rs

+14
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

+4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ libm_macros::for_each_function! {
127127
| fdimf16
128128
| floorf128
129129
| floorf16
130+
| ldexpf128
131+
| ldexpf16
130132
| rintf128
131133
| rintf16
134+
| scalbnf128
135+
| scalbnf16
132136
| sqrtf128
133137
| sqrtf16
134138
| truncf128

crates/libm-test/src/mpfloat.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ libm_macros::for_each_function! {
157157
jnf,
158158
ldexp,
159159
ldexpf,
160+
ldexpf128,
161+
ldexpf16,
160162
lgamma_r,
161163
lgammaf_r,
162164
modf,
@@ -174,6 +176,8 @@ libm_macros::for_each_function! {
174176
roundf,
175177
scalbn,
176178
scalbnf,
179+
scalbnf128,
180+
scalbnf16,
177181
sincos,sincosf,
178182
trunc,
179183
truncf,
@@ -360,34 +364,6 @@ macro_rules! impl_op_for_ty {
360364
}
361365
}
362366

363-
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
364-
// methods.
365-
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
366-
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
367-
368-
fn new_mp() -> Self::MpTy {
369-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
370-
}
371-
372-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
373-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
374-
}
375-
}
376-
377-
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
378-
type MpTy = MpFloat;
379-
380-
fn new_mp() -> Self::MpTy {
381-
new_mpfloat::<Self::FTy>()
382-
}
383-
384-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
385-
this.assign(input.0);
386-
*this <<= input.1;
387-
prep_retval::<Self::FTy>(this, Ordering::Equal)
388-
}
389-
}
390-
391367
impl MpOp for crate::op::[<sincos $suffix>]::Routine {
392368
type MpTy = (MpFloat, MpFloat);
393369

@@ -460,6 +436,34 @@ macro_rules! impl_op_for_ty_all {
460436
prep_retval::<Self::RustRet>(&mut this.0, Ordering::Equal)
461437
}
462438
}
439+
440+
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
441+
// methods.
442+
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
443+
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
444+
445+
fn new_mp() -> Self::MpTy {
446+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
447+
}
448+
449+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
450+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
451+
}
452+
}
453+
454+
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
455+
type MpTy = MpFloat;
456+
457+
fn new_mp() -> Self::MpTy {
458+
new_mpfloat::<Self::FTy>()
459+
}
460+
461+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
462+
this.assign(input.0);
463+
*this <<= input.1;
464+
prep_retval::<Self::FTy>(this, Ordering::Equal)
465+
}
466+
}
463467
}
464468
};
465469
}

crates/libm-test/src/precision.rs

+4
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,12 @@ fn int_float_common<F1: Float, F2: Float>(
554554
DEFAULT
555555
}
556556

557+
#[cfg(f16_enabled)]
558+
impl MaybeOverride<(f16, i32)> for SpecialCase {}
557559
impl MaybeOverride<(f32, i32)> for SpecialCase {}
558560
impl MaybeOverride<(f64, i32)> for SpecialCase {}
561+
#[cfg(f128_enabled)]
562+
impl MaybeOverride<(f128, i32)> for SpecialCase {}
559563

560564
impl MaybeOverride<(f32, f32, f32)> for SpecialCase {
561565
fn check_float<F: Float>(

crates/libm-test/tests/compare_built_musl.rs

+4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ libm_macros::for_each_function! {
8989
fdimf16,
9090
floorf128,
9191
floorf16,
92+
ldexpf128,
93+
ldexpf16,
9294
rintf128,
9395
rintf16,
96+
scalbnf128,
97+
scalbnf16,
9498
sqrtf128,
9599
sqrtf16,
96100
truncf128,

crates/util/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
9696
| fdimf16
9797
| floorf128
9898
| floorf16
99+
| ldexpf128
100+
| ldexpf16
99101
| rintf128
100102
| rintf16
103+
| scalbnf128
104+
| scalbnf16
101105
| sqrtf128
102106
| sqrtf16
103107
| truncf128

etc/function-definitions.json

+26
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,18 @@
506506
],
507507
"type": "f32"
508508
},
509+
"ldexpf128": {
510+
"sources": [
511+
"src/math/ldexpf128.rs"
512+
],
513+
"type": "f128"
514+
},
515+
"ldexpf16": {
516+
"sources": [
517+
"src/math/ldexpf16.rs"
518+
],
519+
"type": "f16"
520+
},
509521
"lgamma": {
510522
"sources": [
511523
"src/libm_helper.rs",
@@ -710,6 +722,20 @@
710722
],
711723
"type": "f32"
712724
},
725+
"scalbnf128": {
726+
"sources": [
727+
"src/math/generic/scalbn.rs",
728+
"src/math/scalbnf128.rs"
729+
],
730+
"type": "f128"
731+
},
732+
"scalbnf16": {
733+
"sources": [
734+
"src/math/generic/scalbn.rs",
735+
"src/math/scalbnf16.rs"
736+
],
737+
"type": "f16"
738+
},
713739
"sin": {
714740
"sources": [
715741
"src/libm_helper.rs",

etc/function-list.txt

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jn
7373
jnf
7474
ldexp
7575
ldexpf
76+
ldexpf128
77+
ldexpf16
7678
lgamma
7779
lgamma_r
7880
lgammaf
@@ -103,6 +105,8 @@ round
103105
roundf
104106
scalbn
105107
scalbnf
108+
scalbnf128
109+
scalbnf16
106110
sin
107111
sincos
108112
sincosf

src/math/ldexpf128.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn ldexpf128(x: f128, n: i32) -> f128 {
3+
super::scalbnf128(x, n)
4+
}

src/math/ldexpf16.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn ldexpf16(x: f16, n: i32) -> f16 {
3+
super::scalbnf16(x, n)
4+
}

src/math/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ cfg_if! {
346346
mod fabsf16;
347347
mod fdimf16;
348348
mod floorf16;
349+
mod ldexpf16;
349350
mod rintf16;
351+
mod scalbnf16;
350352
mod sqrtf16;
351353
mod truncf16;
352354

@@ -355,7 +357,9 @@ cfg_if! {
355357
pub use self::fabsf16::fabsf16;
356358
pub use self::fdimf16::fdimf16;
357359
pub use self::floorf16::floorf16;
360+
pub use self::ldexpf16::ldexpf16;
358361
pub use self::rintf16::rintf16;
362+
pub use self::scalbnf16::scalbnf16;
359363
pub use self::sqrtf16::sqrtf16;
360364
pub use self::truncf16::truncf16;
361365
}
@@ -368,7 +372,9 @@ cfg_if! {
368372
mod fabsf128;
369373
mod fdimf128;
370374
mod floorf128;
375+
mod ldexpf128;
371376
mod rintf128;
377+
mod scalbnf128;
372378
mod sqrtf128;
373379
mod truncf128;
374380

@@ -377,7 +383,9 @@ cfg_if! {
377383
pub use self::fabsf128::fabsf128;
378384
pub use self::fdimf128::fdimf128;
379385
pub use self::floorf128::floorf128;
386+
pub use self::ldexpf128::ldexpf128;
380387
pub use self::rintf128::rintf128;
388+
pub use self::scalbnf128::scalbnf128;
381389
pub use self::sqrtf128::sqrtf128;
382390
pub use self::truncf128::truncf128;
383391
}

src/math/scalbnf128.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn scalbnf128(x: f128, n: i32) -> f128 {
3+
super::generic::scalbn(x, n)
4+
}

src/math/scalbnf16.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn scalbnf16(x: f16, n: i32) -> f16 {
3+
super::generic::scalbn(x, n)
4+
}

0 commit comments

Comments
 (0)