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

Commit 946f6ec

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 82c7f03 commit 946f6ec

File tree

14 files changed

+121
-28
lines changed

14 files changed

+121
-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/icount.rs

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ main!(
131131
icount_bench_jn_group,
132132
icount_bench_jnf_group,
133133
icount_bench_ldexp_group,
134+
icount_bench_ldexpf128_group,
135+
icount_bench_ldexpf16_group,
134136
icount_bench_ldexpf_group,
135137
icount_bench_lgamma_group,
136138
icount_bench_lgamma_r_group,
@@ -163,6 +165,8 @@ main!(
163165
icount_bench_roundf16_group,
164166
icount_bench_roundf_group,
165167
icount_bench_scalbn_group,
168+
icount_bench_scalbnf128_group,
169+
icount_bench_scalbnf16_group,
166170
icount_bench_scalbnf_group,
167171
icount_bench_sin_group,
168172
icount_bench_sinf_group,

crates/libm-test/benches/random.rs

+4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,14 @@ libm_macros::for_each_function! {
133133
| fminf16
134134
| fmodf128
135135
| fmodf16
136+
| ldexpf128
137+
| ldexpf16
136138
| rintf128
137139
| rintf16
138140
| roundf128
139141
| roundf16
142+
| scalbnf128
143+
| scalbnf16
140144
| sqrtf128
141145
| sqrtf16
142146
| truncf128

crates/libm-test/src/mpfloat.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ libm_macros::for_each_function! {
159159
jnf,
160160
ldexp,
161161
ldexpf,
162+
ldexpf128,
163+
ldexpf16,
162164
lgamma_r,
163165
lgammaf_r,
164166
modf,
@@ -178,6 +180,8 @@ libm_macros::for_each_function! {
178180
roundf16,
179181
scalbn,
180182
scalbnf,
183+
scalbnf128,
184+
scalbnf16,
181185
sincos,sincosf,
182186
trunc,
183187
truncf,
@@ -351,34 +355,6 @@ macro_rules! impl_op_for_ty {
351355
}
352356
}
353357

354-
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
355-
// methods.
356-
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
357-
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
358-
359-
fn new_mp() -> Self::MpTy {
360-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
361-
}
362-
363-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
364-
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
365-
}
366-
}
367-
368-
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
369-
type MpTy = MpFloat;
370-
371-
fn new_mp() -> Self::MpTy {
372-
new_mpfloat::<Self::FTy>()
373-
}
374-
375-
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
376-
this.assign(input.0);
377-
*this <<= input.1;
378-
prep_retval::<Self::FTy>(this, Ordering::Equal)
379-
}
380-
}
381-
382358
impl MpOp for crate::op::[<sincos $suffix>]::Routine {
383359
type MpTy = (MpFloat, MpFloat);
384360

@@ -464,6 +440,35 @@ macro_rules! impl_op_for_ty_all {
464440
this.1.assign(input.1);
465441
let ord = this.0.rem_assign_round(&this.1, Nearest);
466442
prep_retval::<Self::RustRet>(&mut this.0, ord)
443+
444+
}
445+
}
446+
447+
// `ldexp` and `scalbn` are the same for binary floating point, so just forward all
448+
// methods.
449+
impl MpOp for crate::op::[<ldexp $suffix>]::Routine {
450+
type MpTy = <crate::op::[<scalbn $suffix>]::Routine as MpOp>::MpTy;
451+
452+
fn new_mp() -> Self::MpTy {
453+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::new_mp()
454+
}
455+
456+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
457+
<crate::op::[<scalbn $suffix>]::Routine as MpOp>::run(this, input)
458+
}
459+
}
460+
461+
impl MpOp for crate::op::[<scalbn $suffix>]::Routine {
462+
type MpTy = MpFloat;
463+
464+
fn new_mp() -> Self::MpTy {
465+
new_mpfloat::<Self::FTy>()
466+
}
467+
468+
fn run(this: &mut Self::MpTy, input: Self::RustArgs) -> Self::RustRet {
469+
this.assign(input.0);
470+
*this <<= input.1;
471+
prep_retval::<Self::FTy>(this, Ordering::Equal)
467472
}
468473
}
469474
}

crates/libm-test/src/precision.rs

+4
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,12 @@ fn int_float_common<F1: Float, F2: Float>(
551551
DEFAULT
552552
}
553553

554+
#[cfg(f16_enabled)]
555+
impl MaybeOverride<(f16, i32)> for SpecialCase {}
554556
impl MaybeOverride<(f32, i32)> for SpecialCase {}
555557
impl MaybeOverride<(f64, i32)> for SpecialCase {}
558+
#[cfg(f128_enabled)]
559+
impl MaybeOverride<(f128, i32)> for SpecialCase {}
556560

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

crates/libm-test/tests/compare_built_musl.rs

+4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ libm_macros::for_each_function! {
9595
fminf16,
9696
fmodf128,
9797
fmodf16,
98+
ldexpf128,
99+
ldexpf16,
98100
rintf128,
99101
rintf16,
100102
roundf128,
101103
roundf16,
104+
scalbnf128,
105+
scalbnf16,
102106
sqrtf128,
103107
sqrtf16,
104108
truncf128,

crates/util/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
102102
| fminf16
103103
| fmodf128
104104
| fmodf16
105+
| ldexpf128
106+
| ldexpf16
105107
| rintf128
106108
| rintf16
107109
| roundf128
108110
| roundf16
111+
| scalbnf128
112+
| scalbnf16
109113
| sqrtf128
110114
| sqrtf16
111115
| truncf128

etc/function-definitions.json

+26
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,18 @@
554554
],
555555
"type": "f32"
556556
},
557+
"ldexpf128": {
558+
"sources": [
559+
"src/math/ldexpf128.rs"
560+
],
561+
"type": "f128"
562+
},
563+
"ldexpf16": {
564+
"sources": [
565+
"src/math/ldexpf16.rs"
566+
],
567+
"type": "f16"
568+
},
557569
"lgamma": {
558570
"sources": [
559571
"src/libm_helper.rs",
@@ -774,6 +786,20 @@
774786
],
775787
"type": "f32"
776788
},
789+
"scalbnf128": {
790+
"sources": [
791+
"src/math/generic/scalbn.rs",
792+
"src/math/scalbnf128.rs"
793+
],
794+
"type": "f128"
795+
},
796+
"scalbnf16": {
797+
"sources": [
798+
"src/math/generic/scalbn.rs",
799+
"src/math/scalbnf16.rs"
800+
],
801+
"type": "f16"
802+
},
777803
"sin": {
778804
"sources": [
779805
"src/libm_helper.rs",

etc/function-list.txt

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ jn
7979
jnf
8080
ldexp
8181
ldexpf
82+
ldexpf128
83+
ldexpf16
8284
lgamma
8385
lgamma_r
8486
lgammaf
@@ -111,6 +113,8 @@ roundf128
111113
roundf16
112114
scalbn
113115
scalbnf
116+
scalbnf128
117+
scalbnf16
114118
sin
115119
sincos
116120
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
@@ -349,8 +349,10 @@ cfg_if! {
349349
mod fmaxf16;
350350
mod fminf16;
351351
mod fmodf16;
352+
mod ldexpf16;
352353
mod rintf16;
353354
mod roundf16;
355+
mod scalbnf16;
354356
mod sqrtf16;
355357
mod truncf16;
356358

@@ -362,8 +364,10 @@ cfg_if! {
362364
pub use self::fmaxf16::fmaxf16;
363365
pub use self::fminf16::fminf16;
364366
pub use self::fmodf16::fmodf16;
367+
pub use self::ldexpf16::ldexpf16;
365368
pub use self::rintf16::rintf16;
366369
pub use self::roundf16::roundf16;
370+
pub use self::scalbnf16::scalbnf16;
367371
pub use self::sqrtf16::sqrtf16;
368372
pub use self::truncf16::truncf16;
369373
}
@@ -379,8 +383,10 @@ cfg_if! {
379383
mod fmaxf128;
380384
mod fminf128;
381385
mod fmodf128;
386+
mod ldexpf128;
382387
mod rintf128;
383388
mod roundf128;
389+
mod scalbnf128;
384390
mod sqrtf128;
385391
mod truncf128;
386392

@@ -392,8 +398,10 @@ cfg_if! {
392398
pub use self::fmaxf128::fmaxf128;
393399
pub use self::fminf128::fminf128;
394400
pub use self::fmodf128::fmodf128;
401+
pub use self::ldexpf128::ldexpf128;
395402
pub use self::rintf128::rintf128;
396403
pub use self::roundf128::roundf128;
404+
pub use self::scalbnf128::scalbnf128;
397405
pub use self::sqrtf128::sqrtf128;
398406
pub use self::truncf128::truncf128;
399407
}

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)