]> git.proxmox.com Git - rustc.git/blame - src/stdsimd/coresimd/ppsv/api/arithmetic_ops.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / stdsimd / coresimd / ppsv / api / arithmetic_ops.rs
CommitLineData
0531ce1d 1//! Lane-wise arithmetic operations.
83c7162d 2#![allow(unused)]
0531ce1d
XL
3
4macro_rules! impl_arithmetic_ops {
5 ($id:ident) => {
83c7162d 6 impl ::ops::Add for $id {
0531ce1d
XL
7 type Output = Self;
8 #[inline]
9 fn add(self, other: Self) -> Self {
83c7162d 10 use coresimd::simd_llvm::simd_add;
0531ce1d
XL
11 unsafe { simd_add(self, other) }
12 }
13 }
14
83c7162d 15 impl ::ops::Sub for $id {
0531ce1d
XL
16 type Output = Self;
17 #[inline]
18 fn sub(self, other: Self) -> Self {
83c7162d 19 use coresimd::simd_llvm::simd_sub;
0531ce1d
XL
20 unsafe { simd_sub(self, other) }
21 }
22 }
23
83c7162d 24 impl ::ops::Mul for $id {
0531ce1d
XL
25 type Output = Self;
26 #[inline]
27 fn mul(self, other: Self) -> Self {
83c7162d 28 use coresimd::simd_llvm::simd_mul;
0531ce1d
XL
29 unsafe { simd_mul(self, other) }
30 }
31 }
32
83c7162d 33 impl ::ops::Div for $id {
0531ce1d
XL
34 type Output = Self;
35 #[inline]
36 fn div(self, other: Self) -> Self {
83c7162d 37 use coresimd::simd_llvm::simd_div;
0531ce1d
XL
38 unsafe { simd_div(self, other) }
39 }
40 }
41
83c7162d 42 impl ::ops::Rem for $id {
0531ce1d
XL
43 type Output = Self;
44 #[inline]
45 fn rem(self, other: Self) -> Self {
83c7162d 46 use coresimd::simd_llvm::simd_rem;
0531ce1d
XL
47 unsafe { simd_rem(self, other) }
48 }
49 }
50
83c7162d 51 impl ::ops::AddAssign for $id {
0531ce1d
XL
52 #[inline]
53 fn add_assign(&mut self, other: Self) {
54 *self = *self + other;
55 }
56 }
57
83c7162d 58 impl ::ops::SubAssign for $id {
0531ce1d
XL
59 #[inline]
60 fn sub_assign(&mut self, other: Self) {
61 *self = *self - other;
62 }
63 }
64
83c7162d 65 impl ::ops::MulAssign for $id {
0531ce1d
XL
66 #[inline]
67 fn mul_assign(&mut self, other: Self) {
68 *self = *self * other;
69 }
70 }
71
83c7162d 72 impl ::ops::DivAssign for $id {
0531ce1d
XL
73 #[inline]
74 fn div_assign(&mut self, other: Self) {
75 *self = *self / other;
76 }
77 }
78
83c7162d 79 impl ::ops::RemAssign for $id {
0531ce1d
XL
80 #[inline]
81 fn rem_assign(&mut self, other: Self) {
82 *self = *self % other;
83 }
84 }
83c7162d 85 };
0531ce1d
XL
86}
87
88#[cfg(test)]
0531ce1d
XL
89macro_rules! test_arithmetic_ops {
90 ($id:ident, $elem_ty:ident) => {
91 #[test]
92 fn arithmetic() {
83c7162d 93 use coresimd::simd::$id;
0531ce1d
XL
94 let z = $id::splat(0 as $elem_ty);
95 let o = $id::splat(1 as $elem_ty);
96 let t = $id::splat(2 as $elem_ty);
97 let f = $id::splat(4 as $elem_ty);
98
99 // add
100 assert_eq!(z + z, z);
101 assert_eq!(o + z, o);
102 assert_eq!(t + z, t);
103 assert_eq!(t + t, f);
104 // sub
105 assert_eq!(z - z, z);
106 assert_eq!(o - z, o);
107 assert_eq!(t - z, t);
108 assert_eq!(f - t, t);
109 assert_eq!(f - o - o, t);
110 // mul
111 assert_eq!(z * z, z);
112 assert_eq!(z * o, z);
113 assert_eq!(z * t, z);
114 assert_eq!(o * t, t);
115 assert_eq!(t * t, f);
116 // div
117 assert_eq!(z / o, z);
118 assert_eq!(t / o, t);
119 assert_eq!(f / o, f);
120 assert_eq!(t / t, o);
121 assert_eq!(f / t, t);
122 // rem
123 assert_eq!(o % o, z);
124 assert_eq!(f % t, z);
125
126 {
127 let mut v = z;
128 assert_eq!(v, z);
83c7162d 129 v += o; // add_assign
0531ce1d
XL
130 assert_eq!(v, o);
131 v -= o; // sub_assign
132 assert_eq!(v, z);
133 v = t;
134 v *= o; // mul_assign
135 assert_eq!(v, t);
136 v *= t;
137 assert_eq!(v, f);
138 v /= o; // div_assign
139 assert_eq!(v, f);
140 v /= t;
141 assert_eq!(v, t);
142 v %= t; // rem_assign
143 assert_eq!(v, z);
144 }
145 }
146 };
147}