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

Commit e3c91f1

Browse files
committed
Add truncf16 and truncf128
Use the generic algorithms to provide implementations for these routines.
1 parent 1be4dc4 commit e3c91f1

File tree

9 files changed

+54
-3
lines changed

9 files changed

+54
-3
lines changed

crates/libm-macros/src/shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
99
FloatTy::F16,
1010
Signature { args: &[Ty::F16], returns: &[Ty::F16] },
1111
None,
12-
&["fabsf16"],
12+
&["fabsf16", "truncf16"],
1313
),
1414
(
1515
// `fn(f32) -> f32`
@@ -40,7 +40,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
4040
FloatTy::F128,
4141
Signature { args: &[Ty::F128], returns: &[Ty::F128] },
4242
None,
43-
&["fabsf128"],
43+
&["fabsf128", "truncf128"],
4444
),
4545
(
4646
// `(f16, f16) -> f16`

crates/libm-test/src/domain.rs

+10
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,13 @@ impl HasDomain<f16> for crate::op::fabsf16::Routine {
199199
impl HasDomain<f128> for crate::op::fabsf128::Routine {
200200
const DOMAIN: Domain<f128> = Domain::<f128>::UNBOUNDED;
201201
}
202+
203+
#[cfg(f16_enabled)]
204+
impl HasDomain<f16> for crate::op::truncf16::Routine {
205+
const DOMAIN: Domain<f16> = Domain::<f16>::UNBOUNDED;
206+
}
207+
208+
#[cfg(f128_enabled)]
209+
impl HasDomain<f128> for crate::op::truncf128::Routine {
210+
const DOMAIN: Domain<f128> = Domain::<f128>::UNBOUNDED;
211+
}

crates/libm-test/src/mpfloat.rs

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ libm_macros::for_each_function! {
141141
lgamma_r, lgammaf_r, modf, modff, nextafter, nextafterf, pow,powf,
142142
remquo, remquof, scalbn, scalbnf, sincos, sincosf, yn, ynf,
143143
copysignf16, copysignf128, fabsf16, fabsf128,
144+
truncf16, truncf128,
144145
],
145146
fn_extra: match MACRO_FN_NAME {
146147
// Remap function names that are different between mpfr and libm
@@ -202,11 +203,13 @@ impl_no_round! {
202203
#[cfg(f16_enabled)]
203204
impl_no_round! {
204205
fabsf16 => abs_mut;
206+
truncf16 => trunc_mut;
205207
}
206208

207209
#[cfg(f128_enabled)]
208210
impl_no_round! {
209211
fabsf128 => abs_mut;
212+
truncf128 => trunc_mut;
210213
}
211214

212215
/// Some functions are difficult to do in a generic way. Implement them here.

crates/libm-test/tests/compare_built_musl.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
libm_macros::for_each_function! {
4949
callback: musl_rand_tests,
5050
// Musl does not support `f16` and `f128` on all platforms.
51-
skip: [copysignf16, copysignf128, fabsf16, fabsf128],
51+
skip: [copysignf16, copysignf128, fabsf16, fabsf128, truncf16, truncf128],
5252
attributes: [
5353
#[cfg_attr(x86_no_sse, ignore)] // FIXME(correctness): wrong result on i586
5454
[exp10, exp10f, exp2, exp2f, rint]
@@ -146,5 +146,7 @@ libm_macros::for_each_function! {
146146
// Not provided by musl
147147
fabsf16,
148148
fabsf128,
149+
truncf16,
150+
truncf128,
149151
],
150152
}

etc/function-definitions.json

+16
Original file line numberDiff line numberDiff line change
@@ -739,17 +739,33 @@
739739
"sources": [
740740
"src/libm_helper.rs",
741741
"src/math/arch/wasm32.rs",
742+
"src/math/generic/trunc.rs",
742743
"src/math/trunc.rs"
743744
],
744745
"type": "f64"
745746
},
746747
"truncf": {
747748
"sources": [
748749
"src/math/arch/wasm32.rs",
750+
"src/math/generic/trunc.rs",
749751
"src/math/truncf.rs"
750752
],
751753
"type": "f32"
752754
},
755+
"truncf128": {
756+
"sources": [
757+
"src/math/generic/trunc.rs",
758+
"src/math/truncf128.rs"
759+
],
760+
"type": "f128"
761+
},
762+
"truncf16": {
763+
"sources": [
764+
"src/math/generic/trunc.rs",
765+
"src/math/truncf16.rs"
766+
],
767+
"type": "f16"
768+
},
753769
"y0": {
754770
"sources": [
755771
"src/libm_helper.rs",

etc/function-list.txt

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ tgamma
111111
tgammaf
112112
trunc
113113
truncf
114+
truncf128
115+
truncf16
114116
y0
115117
y0f
116118
y1

src/math/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,23 @@ cfg_if! {
343343
if #[cfg(f16_enabled)] {
344344
mod copysignf16;
345345
mod fabsf16;
346+
mod truncf16;
346347

347348
pub use self::copysignf16::copysignf16;
348349
pub use self::fabsf16::fabsf16;
350+
pub use self::truncf16::truncf16;
349351
}
350352
}
351353

352354
cfg_if! {
353355
if #[cfg(f128_enabled)] {
354356
mod copysignf128;
355357
mod fabsf128;
358+
mod truncf128;
356359

357360
pub use self::copysignf128::copysignf128;
358361
pub use self::fabsf128::fabsf128;
362+
pub use self::truncf128::truncf128;
359363
}
360364
}
361365

src/math/truncf128.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// Rounds the number toward 0 to the closest integral value (f128).
2+
///
3+
/// This effectively removes the decimal part of the number, leaving the integral part.
4+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
5+
pub fn truncf128(x: f128) -> f128 {
6+
super::generic::trunc(x)
7+
}

src/math/truncf16.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// Rounds the number toward 0 to the closest integral value (f16).
2+
///
3+
/// This effectively removes the decimal part of the number, leaving the integral part.
4+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
5+
pub fn truncf16(x: f16) -> f16 {
6+
super::generic::trunc(x)
7+
}

0 commit comments

Comments
 (0)