]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/round.rs
Auto merge of #89167 - workingjubilee:use-simd, r=MarkSimulacrum
[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         #[cfg(feature = "std")]
9         impl<const LANES: usize> Simd<$type, LANES>
10         where
11             LaneCount<LANES>: SupportedLaneCount,
12         {
13             /// Returns the smallest integer greater than or equal to each lane.
14             #[must_use = "method returns a new vector and does not mutate the original value"]
15             #[inline]
16             pub fn ceil(self) -> Self {
17                 unsafe { intrinsics::simd_ceil(self) }
18             }
19
20             /// Returns the largest integer value less than or equal to each lane.
21             #[must_use = "method returns a new vector and does not mutate the original value"]
22             #[inline]
23             pub fn floor(self) -> Self {
24                 unsafe { intrinsics::simd_floor(self) }
25             }
26
27             /// Rounds to the nearest integer value. Ties round toward zero.
28             #[must_use = "method returns a new vector and does not mutate the original value"]
29             #[inline]
30             pub fn round(self) -> Self {
31                 unsafe { intrinsics::simd_round(self) }
32             }
33
34             /// Returns the floating point's integer value, with its fractional part removed.
35             #[must_use = "method returns a new vector and does not mutate the original value"]
36             #[inline]
37             pub fn trunc(self) -> Self {
38                 unsafe { intrinsics::simd_trunc(self) }
39             }
40
41             /// Returns the floating point's fractional value, with its integer part removed.
42             #[must_use = "method returns a new vector and does not mutate the original value"]
43             #[inline]
44             pub fn fract(self) -> Self {
45                 self - self.trunc()
46             }
47         }
48
49         impl<const LANES: usize> Simd<$type, LANES>
50         where
51             LaneCount<LANES>: SupportedLaneCount,
52         {
53             /// Rounds toward zero and converts to the same-width integer type, assuming that
54             /// the value is finite and fits in that type.
55             ///
56             /// # Safety
57             /// The value must:
58             ///
59             /// * Not be NaN
60             /// * Not be infinite
61             /// * Be representable in the return type, after truncating off its fractional part
62             #[inline]
63             pub unsafe fn to_int_unchecked(self) -> Simd<$int_type, LANES> {
64                 unsafe { intrinsics::simd_cast(self) }
65             }
66
67             /// Creates a floating-point vector from an integer vector.  Rounds values that are
68             /// not exactly representable.
69             #[inline]
70             pub fn round_from_int(value: Simd<$int_type, LANES>) -> Self {
71                 unsafe { intrinsics::simd_cast(value) }
72             }
73         }
74     }
75 }
76
77 implement! { f32, i32 }
78 implement! { f64, i64 }