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

Commit 9d39102

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

File tree

11 files changed

+57
-4
lines changed

11 files changed

+57
-4
lines changed

crates/compiler-builtins-smoke-test/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ no_mangle! {
157157
tgammaf(x: f32) -> f32;
158158
trunc(x: f64) -> f64;
159159
truncf(x: f32) -> f32;
160+
truncf128(x: f128) -> f128;
161+
truncf16(x: f16) -> f16;
160162
y0(x: f64) -> f64;
161163
y0f(x: f32) -> f32;
162164
y1(x: f64) -> f64;

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/benches/random.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ 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 | fabsf16 | fabsf128 | truncf16 | truncf128 => (false, None),
121121

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

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
@@ -743,17 +743,33 @@
743743
"sources": [
744744
"src/libm_helper.rs",
745745
"src/math/arch/wasm32.rs",
746+
"src/math/generic/trunc.rs",
746747
"src/math/trunc.rs"
747748
],
748749
"type": "f64"
749750
},
750751
"truncf": {
751752
"sources": [
752753
"src/math/arch/wasm32.rs",
754+
"src/math/generic/trunc.rs",
753755
"src/math/truncf.rs"
754756
],
755757
"type": "f32"
756758
},
759+
"truncf128": {
760+
"sources": [
761+
"src/math/generic/trunc.rs",
762+
"src/math/truncf128.rs"
763+
],
764+
"type": "f128"
765+
},
766+
"truncf16": {
767+
"sources": [
768+
"src/math/generic/trunc.rs",
769+
"src/math/truncf16.rs"
770+
],
771+
"type": "f16"
772+
},
757773
"y0": {
758774
"sources": [
759775
"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)