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

Commit b4fda6e

Browse files
Give rounding intrinsics their own modules
1 parent 24ebae8 commit b4fda6e

File tree

4 files changed

+81
-63
lines changed

4 files changed

+81
-63
lines changed

crates/core_simd/src/intrinsics.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ extern "platform-intrinsic" {
4343
/// neg/fneg
4444
pub(crate) fn simd_neg<T>(x: T) -> T;
4545

46-
// floor
47-
#[cfg(feature = "std")]
48-
pub(crate) fn simd_floor<T>(x: T) -> T;
49-
50-
// ceil
51-
#[cfg(feature = "std")]
52-
pub(crate) fn simd_ceil<T>(x: T) -> T;
53-
5446
/// fabs
5547
pub(crate) fn simd_fabs<T>(x: T) -> T;
5648

@@ -85,3 +77,17 @@ extern "platform-intrinsic" {
8577
pub(crate) fn simd_reduce_or<T, U>(x: T) -> U;
8678
pub(crate) fn simd_reduce_xor<T, U>(x: T) -> U;
8779
}
80+
81+
#[cfg(feature = "std")]
82+
mod std {
83+
extern "platform-intrinsic" {
84+
// ceil
85+
pub(crate) fn simd_ceil<T>(x: T) -> T;
86+
87+
// floor
88+
pub(crate) fn simd_floor<T>(x: T) -> T;
89+
}
90+
}
91+
92+
#[cfg(feature = "std")]
93+
pub(crate) use crate::intrinsics::std::*;

crates/core_simd/src/round.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ macro_rules! implement {
22
{
33
$type:ident, $int_type:ident
44
} => {
5+
#[cfg(feature = "std")]
56
impl<const LANES: usize> crate::$type<LANES>
67
where
78
Self: crate::LanesAtMost32,
89
{
910
/// Returns the largest integer less than or equal to each lane.
10-
#[cfg(feature = "std")]
1111
#[must_use = "method returns a new vector and does not mutate the original value"]
1212
#[inline]
1313
pub fn floor(self) -> Self {
1414
unsafe { crate::intrinsics::simd_floor(self) }
1515
}
1616

1717
/// Returns the smallest integer greater than or equal to each lane.
18-
#[cfg(feature = "std")]
1918
#[must_use = "method returns a new vector and does not mutate the original value"]
2019
#[inline]
2120
pub fn ceil(self) -> Self {

crates/core_simd/tests/ops_macros.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ macro_rules! impl_float_tests {
353353
mod $scalar {
354354
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
355355
type Scalar = $scalar;
356-
type IntScalar = $int_scalar;
357356

358357
impl_unary_op_test!(Vector<LANES>, Scalar, Neg::neg);
359358
impl_binary_op_test!(Vector<LANES>, Scalar, Add::add, AddAssign::add_assign);
@@ -362,25 +361,6 @@ macro_rules! impl_float_tests {
362361
impl_binary_op_test!(Vector<LANES>, Scalar, Div::div, DivAssign::div_assign);
363362
impl_binary_op_test!(Vector<LANES>, Scalar, Rem::rem, RemAssign::rem_assign);
364363

365-
#[cfg(feature = "std")]
366-
test_helpers::test_lanes! {
367-
fn ceil<const LANES: usize>() {
368-
test_helpers::test_unary_elementwise(
369-
&Vector::<LANES>::ceil,
370-
&Scalar::ceil,
371-
&|_| true,
372-
)
373-
}
374-
375-
fn floor<const LANES: usize>() {
376-
test_helpers::test_unary_elementwise(
377-
&Vector::<LANES>::floor,
378-
&Scalar::floor,
379-
&|_| true,
380-
)
381-
}
382-
}
383-
384364
test_helpers::test_lanes! {
385365
fn is_sign_positive<const LANES: usize>() {
386366
test_helpers::test_unary_mask_elementwise(
@@ -446,39 +426,6 @@ macro_rules! impl_float_tests {
446426
)
447427
}
448428

449-
fn round_from_int<const LANES: usize>() {
450-
test_helpers::test_unary_elementwise(
451-
&Vector::<LANES>::round_from_int,
452-
&|x| x as Scalar,
453-
&|_| true,
454-
)
455-
}
456-
457-
fn to_int_unchecked<const LANES: usize>() {
458-
// The maximum integer that can be represented by the equivalently sized float has
459-
// all of the mantissa digits set to 1, pushed up to the MSB.
460-
const ALL_MANTISSA_BITS: IntScalar = ((1 << <Scalar>::MANTISSA_DIGITS) - 1);
461-
const MAX_REPRESENTABLE_VALUE: Scalar =
462-
(ALL_MANTISSA_BITS << (core::mem::size_of::<Scalar>() * 8 - <Scalar>::MANTISSA_DIGITS as usize - 1)) as Scalar;
463-
464-
let mut runner = proptest::test_runner::TestRunner::default();
465-
runner.run(
466-
&test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
467-
|x| {
468-
let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() };
469-
let result_2 = {
470-
let mut result = [0; LANES];
471-
for (i, o) in x.iter().zip(result.iter_mut()) {
472-
*o = unsafe { i.to_int_unchecked() };
473-
}
474-
result
475-
};
476-
test_helpers::prop_assert_biteq!(result_1, result_2);
477-
Ok(())
478-
},
479-
).unwrap();
480-
}
481-
482429
fn horizontal_sum<const LANES: usize>() {
483430
test_helpers::test_1(&|x| {
484431
test_helpers::prop_assert_biteq! (

crates/core_simd/tests/round.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
macro_rules! float_rounding_test {
2+
{ $vector:ident, $scalar:tt, $int_scalar:tt } => {
3+
mod $scalar {
4+
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
5+
type Scalar = $scalar;
6+
type IntScalar = $int_scalar;
7+
8+
#[cfg(feature = "std")]
9+
test_helpers::test_lanes! {
10+
fn ceil<const LANES: usize>() {
11+
test_helpers::test_unary_elementwise(
12+
&Vector::<LANES>::ceil,
13+
&Scalar::ceil,
14+
&|_| true,
15+
)
16+
}
17+
18+
fn floor<const LANES: usize>() {
19+
test_helpers::test_unary_elementwise(
20+
&Vector::<LANES>::floor,
21+
&Scalar::floor,
22+
&|_| true,
23+
)
24+
}
25+
}
26+
27+
test_helpers::test_lanes! {
28+
fn from_int<const LANES: usize>() {
29+
test_helpers::test_unary_elementwise(
30+
&Vector::<LANES>::round_from_int,
31+
&|x| x as Scalar,
32+
&|_| true,
33+
)
34+
}
35+
36+
fn to_int_unchecked<const LANES: usize>() {
37+
// The maximum integer that can be represented by the equivalently sized float has
38+
// all of the mantissa digits set to 1, pushed up to the MSB.
39+
const ALL_MANTISSA_BITS: IntScalar = ((1 << <Scalar>::MANTISSA_DIGITS) - 1);
40+
const MAX_REPRESENTABLE_VALUE: Scalar =
41+
(ALL_MANTISSA_BITS << (core::mem::size_of::<Scalar>() * 8 - <Scalar>::MANTISSA_DIGITS as usize - 1)) as Scalar;
42+
43+
let mut runner = proptest::test_runner::TestRunner::default();
44+
runner.run(
45+
&test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
46+
|x| {
47+
let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() };
48+
let result_2 = {
49+
let mut result = [0; LANES];
50+
for (i, o) in x.iter().zip(result.iter_mut()) {
51+
*o = unsafe { i.to_int_unchecked() };
52+
}
53+
result
54+
};
55+
test_helpers::prop_assert_biteq!(result_1, result_2);
56+
Ok(())
57+
},
58+
).unwrap();
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
float_rounding_test! { SimdF32, f32, i32 }
66+
float_rounding_test! { SimdF64, f64, i64 }

0 commit comments

Comments
 (0)