]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/round.rs
06ccab3ec494c6b0799bc43f29416b143318deab
[rust.git] / library / portable-simd / crates / core_simd / src / round.rs
1 use crate::simd::intrinsics;
2 use crate::simd::{LaneCount, Simd, SupportedLaneCount};
3
4 macro_rules! implement {
5     {
6         $type:ty, $int_type:ty
7     } => {
8         impl<const LANES: usize> Simd<$type, LANES>
9         where
10             LaneCount<LANES>: SupportedLaneCount,
11         {
12             /// Rounds toward zero and converts to the same-width integer type, assuming that
13             /// the value is finite and fits in that type.
14             ///
15             /// # Safety
16             /// The value must:
17             ///
18             /// * Not be NaN
19             /// * Not be infinite
20             /// * Be representable in the return type, after truncating off its fractional part
21             #[inline]
22             pub unsafe fn to_int_unchecked(self) -> Simd<$int_type, LANES> {
23                 unsafe { intrinsics::simd_cast(self) }
24             }
25
26             /// Creates a floating-point vector from an integer vector.  Rounds values that are
27             /// not exactly representable.
28             #[inline]
29             pub fn round_from_int(value: Simd<$int_type, LANES>) -> Self {
30                 unsafe { intrinsics::simd_cast(value) }
31             }
32         }
33     }
34 }
35
36 implement! { f32, i32 }
37 implement! { f64, i64 }