Skip to content

Commit 75fdacd

Browse files
committed
Add rounding to integers
1 parent ffedbe5 commit 75fdacd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

crates/core_simd/src/round.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
macro_rules! implement {
22
{
33
impl $type:ident {
4+
int_type = $int_type:ident,
45
floor = $floor_intrinsic:literal,
56
ceil = $ceil_intrinsic:literal,
67
round = $round_intrinsic:literal,
78
trunc = $trunc_intrinsic:literal,
9+
round_to_int = $round_to_int_intrinsic:literal,
810
}
911
} => {
1012
mod $type {
@@ -18,6 +20,8 @@ macro_rules! implement {
1820
fn round_intrinsic(x: crate::$type) -> crate::$type;
1921
#[link_name = $trunc_intrinsic]
2022
fn trunc_intrinsic(x: crate::$type) -> crate::$type;
23+
#[link_name = $round_to_int_intrinsic]
24+
fn round_to_int_intrinsic(x: crate::$type) -> crate::$int_type;
2125
}
2226

2327
impl crate::$type {
@@ -55,70 +59,91 @@ macro_rules! implement {
5559
pub fn fract(self) -> Self {
5660
self - self.trunc()
5761
}
62+
63+
/// Returns the nearest integer to each lane. Round half-way cases away from 0.0.
64+
#[must_use = "method returns a new vector and does not mutate the original value"]
65+
#[inline]
66+
pub fn round_to_int(self) -> crate::$int_type {
67+
unsafe { round_to_int_intrinsic(self) }
68+
}
5869
}
5970
}
6071
}
6172
}
6273

6374
implement! {
6475
impl f32x2 {
76+
int_type = i32x2,
6577
floor = "llvm.floor.v2f32",
6678
ceil = "llvm.ceil.v2f32",
6779
round = "llvm.round.v2f32",
6880
trunc = "llvm.trunc.v2f32",
81+
round_to_int = "llvm.lround.i32.v2f32",
6982
}
7083
}
7184

7285
implement! {
7386
impl f32x4 {
87+
int_type = i32x4,
7488
floor = "llvm.floor.v4f32",
7589
ceil = "llvm.ceil.v4f32",
7690
round = "llvm.round.v4f32",
7791
trunc = "llvm.trunc.v4f32",
92+
round_to_int = "llvm.lround.i32.v4f32",
7893
}
7994
}
8095

8196
implement! {
8297
impl f32x8 {
98+
int_type = i32x8,
8399
floor = "llvm.floor.v8f32",
84100
ceil = "llvm.ceil.v8f32",
85101
round = "llvm.round.v8f32",
86102
trunc = "llvm.trunc.v8f32",
103+
round_to_int = "llvm.lround.i32.v8f32",
87104
}
88105
}
89106

90107
implement! {
91108
impl f32x16 {
109+
int_type = i32x16,
92110
floor = "llvm.floor.v16f32",
93111
ceil = "llvm.ceil.v16f32",
94112
round = "llvm.round.v16f32",
95113
trunc = "llvm.trunc.v16f32",
114+
round_to_int = "llvm.lround.i32.v16f32",
96115
}
97116
}
98117

99118
implement! {
100119
impl f64x2 {
120+
int_type = i64x2,
101121
floor = "llvm.floor.v2f64",
102122
ceil = "llvm.ceil.v2f64",
103123
round = "llvm.round.v2f64",
104124
trunc = "llvm.trunc.v2f64",
125+
round_to_int = "llvm.lround.i64.v2f64",
105126
}
106127
}
107128

108129
implement! {
109130
impl f64x4 {
131+
int_type = i64x4,
110132
floor = "llvm.floor.v4f64",
111133
ceil = "llvm.ceil.v4f64",
112134
round = "llvm.round.v4f64",
113135
trunc = "llvm.trunc.v4f64",
136+
round_to_int = "llvm.lround.i64.v4f64",
114137
}
115138
}
116139

117140
implement! {
118141
impl f64x8 {
142+
int_type = i64x8,
119143
floor = "llvm.floor.v8f64",
120144
ceil = "llvm.ceil.v8f64",
121145
round = "llvm.round.v8f64",
122146
trunc = "llvm.trunc.v8f64",
147+
round_to_int = "llvm.lround.i64.v8f64",
123148
}
124149
}

0 commit comments

Comments
 (0)