]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/round.rs
Fix comment
[rust.git] / crates / core_simd / src / round.rs
1 macro_rules! implement {
2     {
3         impl $type:ident {
4             int_type = $int_type:ident,
5             floor = $floor_intrinsic:literal,
6             ceil = $ceil_intrinsic:literal,
7             round = $round_intrinsic:literal,
8             trunc = $trunc_intrinsic:literal,
9         }
10     } => {
11         mod $type {
12             #[allow(improper_ctypes)]
13             extern "C" {
14                 #[link_name = $floor_intrinsic]
15                 fn floor_intrinsic(x: crate::$type) -> crate::$type;
16                 #[link_name = $ceil_intrinsic]
17                 fn ceil_intrinsic(x: crate::$type) -> crate::$type;
18                 #[link_name = $round_intrinsic]
19                 fn round_intrinsic(x: crate::$type) -> crate::$type;
20                 #[link_name = $trunc_intrinsic]
21                 fn trunc_intrinsic(x: crate::$type) -> crate::$type;
22             }
23
24             impl crate::$type {
25                 /// Returns the largest integer less than or equal to each lane.
26                 #[must_use = "method returns a new vector and does not mutate the original value"]
27                 #[inline]
28                 pub fn floor(self) -> Self {
29                     unsafe { floor_intrinsic(self) }
30                 }
31
32                 /// Returns the smallest integer greater than or equal to each lane.
33                 #[must_use = "method returns a new vector and does not mutate the original value"]
34                 #[inline]
35                 pub fn ceil(self) -> Self {
36                     unsafe { ceil_intrinsic(self) }
37                 }
38
39                 /// Returns the nearest integer to each lane. Round half-way cases away from 0.0.
40                 #[must_use = "method returns a new vector and does not mutate the original value"]
41                 #[inline]
42                 pub fn round(self) -> Self {
43                     unsafe { round_intrinsic(self) }
44                 }
45
46                 /// Returns the integer part of each lane.
47                 #[must_use = "method returns a new vector and does not mutate the original value"]
48                 #[inline]
49                 pub fn trunc(self) -> Self {
50                     unsafe { trunc_intrinsic(self) }
51                 }
52
53                 /// Returns the fractional part of each lane.
54                 #[must_use = "method returns a new vector and does not mutate the original value"]
55                 #[inline]
56                 pub fn fract(self) -> Self {
57                     self - self.trunc()
58                 }
59
60                 /// Rounds toward zero and converts to the same-width integer type, assuming that
61                 /// the value is finite and fits in that type.
62                 ///
63                 /// # Safety
64                 /// The value must:
65                 ///
66                 /// * Not be NaN
67                 /// * Not be infinite
68                 /// * Be representable in the return type, after truncating off its fractional part
69                 #[inline]
70                 pub unsafe fn to_int_unchecked(self) -> crate::$int_type {
71                     crate::intrinsics::simd_cast(self)
72                 }
73
74                 /// Creates a floating-point vector from an integer vector.  Rounds values that are
75                 /// not exactly representable.
76                 #[inline]
77                 pub fn round_from_int(value: crate::$int_type) -> Self {
78                     unsafe { crate::intrinsics::simd_cast(value) }
79                 }
80             }
81         }
82     }
83 }
84
85 implement! {
86     impl f32x2 {
87         int_type = i32x2,
88         floor = "llvm.floor.v2f32",
89         ceil = "llvm.ceil.v2f32",
90         round = "llvm.round.v2f32",
91         trunc = "llvm.trunc.v2f32",
92     }
93 }
94
95 implement! {
96     impl f32x4 {
97         int_type = i32x4,
98         floor = "llvm.floor.v4f32",
99         ceil = "llvm.ceil.v4f32",
100         round = "llvm.round.v4f32",
101         trunc = "llvm.trunc.v4f32",
102     }
103 }
104
105 implement! {
106     impl f32x8 {
107         int_type = i32x8,
108         floor = "llvm.floor.v8f32",
109         ceil = "llvm.ceil.v8f32",
110         round = "llvm.round.v8f32",
111         trunc = "llvm.trunc.v8f32",
112     }
113 }
114
115 implement! {
116     impl f32x16 {
117         int_type = i32x16,
118         floor = "llvm.floor.v16f32",
119         ceil = "llvm.ceil.v16f32",
120         round = "llvm.round.v16f32",
121         trunc = "llvm.trunc.v16f32",
122     }
123 }
124
125 implement! {
126     impl f64x2 {
127         int_type = i64x2,
128         floor = "llvm.floor.v2f64",
129         ceil = "llvm.ceil.v2f64",
130         round = "llvm.round.v2f64",
131         trunc = "llvm.trunc.v2f64",
132     }
133 }
134
135 implement! {
136     impl f64x4 {
137         int_type = i64x4,
138         floor = "llvm.floor.v4f64",
139         ceil = "llvm.ceil.v4f64",
140         round = "llvm.round.v4f64",
141         trunc = "llvm.trunc.v4f64",
142     }
143 }
144
145 implement! {
146     impl f64x8 {
147         int_type = i64x8,
148         floor = "llvm.floor.v8f64",
149         ceil = "llvm.ceil.v8f64",
150         round = "llvm.round.v8f64",
151         trunc = "llvm.trunc.v8f64",
152     }
153 }