]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/round.rs
ccad1aad9c44493a891194c73b3a2cc5577302c3
[rust.git] / crates / core_simd / src / round.rs
1 macro_rules! implement {
2     {
3         $type:ident, $int_type:ident
4     } => {
5         impl<const LANES: usize> crate::$type<LANES>
6         where
7             Self: crate::LanesAtMost32,
8         {
9             /// Returns the largest integer less than or equal to each lane.
10             #[cfg(feature = "std")]
11             #[must_use = "method returns a new vector and does not mutate the original value"]
12             #[inline]
13             pub fn floor(self) -> Self {
14                 unsafe { crate::intrinsics::simd_floor(self) }
15             }
16
17             /// Returns the smallest integer greater than or equal to each lane.
18             #[cfg(feature = "std")]
19             #[must_use = "method returns a new vector and does not mutate the original value"]
20             #[inline]
21             pub fn ceil(self) -> Self {
22                 unsafe { crate::intrinsics::simd_ceil(self) }
23             }
24         }
25
26         impl<const LANES: usize> crate::$type<LANES>
27         where
28             Self: crate::LanesAtMost32,
29             crate::$int_type<LANES>: crate::LanesAtMost32,
30         {
31             /// Rounds toward zero and converts to the same-width integer type, assuming that
32             /// the value is finite and fits in that type.
33             ///
34             /// # Safety
35             /// The value must:
36             ///
37             /// * Not be NaN
38             /// * Not be infinite
39             /// * Be representable in the return type, after truncating off its fractional part
40             #[inline]
41             pub unsafe fn to_int_unchecked(self) -> crate::$int_type<LANES> {
42                 crate::intrinsics::simd_cast(self)
43             }
44
45             /// Creates a floating-point vector from an integer vector.  Rounds values that are
46             /// not exactly representable.
47             #[inline]
48             pub fn round_from_int(value: crate::$int_type<LANES>) -> Self {
49                 unsafe { crate::intrinsics::simd_cast(value) }
50             }
51         }
52     }
53 }
54
55 implement! { SimdF32, SimdI32 }
56 implement! { SimdF64, SimdI64 }