Skip to content

Commit

Permalink
Genericize to_int_unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Feb 3, 2022
1 parent ebf65de commit 4910274
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/core_simd/src/lib.rs
@@ -1,6 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(
const_fn_trait_bound,
convert_float_to_int,
decl_macro,
platform_intrinsics,
repr_simd,
Expand Down
15 changes: 10 additions & 5 deletions crates/core_simd/src/round.rs
@@ -1,9 +1,10 @@
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
use core::convert::FloatToInt;

macro_rules! implement {
{
$type:ty, $int_type:ty
$type:ty
} => {
impl<const LANES: usize> Simd<$type, LANES>
where
Expand All @@ -19,12 +20,16 @@ macro_rules! implement {
/// * Not be infinite
/// * Be representable in the return type, after truncating off its fractional part
#[inline]
pub unsafe fn to_int_unchecked(self) -> Simd<$int_type, LANES> {
pub unsafe fn to_int_unchecked<I>(self) -> Simd<I, LANES>
where
$type: FloatToInt<I>,
I: SimdElement,
{
unsafe { intrinsics::simd_cast(self) }
}
}
}
}

implement! { f32, i32 }
implement! { f64, i64 }
implement! { f32 }
implement! { f64 }
6 changes: 3 additions & 3 deletions crates/core_simd/tests/round.rs
Expand Up @@ -64,11 +64,11 @@ macro_rules! float_rounding_test {
runner.run(
&test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
|x| {
let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() };
let result_1 = unsafe { Vector::from_array(x).to_int_unchecked::<IntScalar>().to_array() };
let result_2 = {
let mut result = [0; LANES];
let mut result: [IntScalar; LANES] = [0; LANES];
for (i, o) in x.iter().zip(result.iter_mut()) {
*o = unsafe { i.to_int_unchecked() };
*o = unsafe { i.to_int_unchecked::<IntScalar>() };
}
result
};
Expand Down

0 comments on commit 4910274

Please sign in to comment.