1 | // pathfinder/simd/src/extras.rs |
2 | // |
3 | // Copyright © 2019 The Pathfinder Project Developers. |
4 | // |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
8 | // option. This file may not be copied, modified, or distributed |
9 | // except according to those terms. |
10 | |
11 | use crate::default::{F32x2, F32x4, I32x2, I32x4}; |
12 | use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; |
13 | |
14 | // Two 32-bit floats |
15 | |
16 | impl F32x2 { |
17 | // Constructors |
18 | |
19 | #[inline ] |
20 | pub fn from_slice(slice: &[f32]) -> F32x2 { |
21 | F32x2::new(slice[0], slice[1]) |
22 | } |
23 | |
24 | // Accessors |
25 | |
26 | #[inline ] |
27 | pub fn x(self) -> f32 { |
28 | self[0] |
29 | } |
30 | |
31 | #[inline ] |
32 | pub fn y(self) -> f32 { |
33 | self[1] |
34 | } |
35 | |
36 | // Mutators |
37 | |
38 | #[inline ] |
39 | pub fn set_x(&mut self, x: f32) { |
40 | self[0] = x |
41 | } |
42 | |
43 | #[inline ] |
44 | pub fn set_y(&mut self, y: f32) { |
45 | self[1] = y |
46 | } |
47 | |
48 | // Comparisons |
49 | |
50 | #[inline ] |
51 | pub fn approx_eq(self, other: F32x2, epsilon: f32) -> bool { |
52 | (self - other).abs().packed_gt(F32x2::splat(epsilon)).all_false() |
53 | } |
54 | } |
55 | |
56 | impl AddAssign for F32x2 { |
57 | #[inline ] |
58 | fn add_assign(&mut self, other: F32x2) { |
59 | *self = *self + other |
60 | } |
61 | } |
62 | |
63 | impl SubAssign for F32x2 { |
64 | #[inline ] |
65 | fn sub_assign(&mut self, other: F32x2) { |
66 | *self = *self - other |
67 | } |
68 | } |
69 | |
70 | impl MulAssign for F32x2 { |
71 | #[inline ] |
72 | fn mul_assign(&mut self, other: F32x2) { |
73 | *self = *self * other |
74 | } |
75 | } |
76 | |
77 | impl Neg for F32x2 { |
78 | type Output = F32x2; |
79 | #[inline ] |
80 | fn neg(self) -> F32x2 { |
81 | F32x2::default() - self |
82 | } |
83 | } |
84 | |
85 | // Four 32-bit floats |
86 | |
87 | impl F32x4 { |
88 | // Constructors |
89 | |
90 | #[inline ] |
91 | pub fn from_slice(slice: &[f32]) -> F32x4 { |
92 | F32x4::new(slice[0], slice[1], slice[2], slice[3]) |
93 | } |
94 | |
95 | // Accessors |
96 | |
97 | #[inline ] |
98 | pub fn x(self) -> f32 { |
99 | self[0] |
100 | } |
101 | |
102 | #[inline ] |
103 | pub fn y(self) -> f32 { |
104 | self[1] |
105 | } |
106 | |
107 | #[inline ] |
108 | pub fn z(self) -> f32 { |
109 | self[2] |
110 | } |
111 | |
112 | #[inline ] |
113 | pub fn w(self) -> f32 { |
114 | self[3] |
115 | } |
116 | |
117 | // Mutators |
118 | |
119 | #[inline ] |
120 | pub fn set_x(&mut self, x: f32) { |
121 | self[0] = x |
122 | } |
123 | |
124 | #[inline ] |
125 | pub fn set_y(&mut self, y: f32) { |
126 | self[1] = y |
127 | } |
128 | |
129 | #[inline ] |
130 | pub fn set_z(&mut self, z: f32) { |
131 | self[2] = z |
132 | } |
133 | |
134 | #[inline ] |
135 | pub fn set_w(&mut self, w: f32) { |
136 | self[3] = w |
137 | } |
138 | |
139 | // Comparisons |
140 | |
141 | #[inline ] |
142 | pub fn approx_eq(self, other: F32x4, epsilon: f32) -> bool { |
143 | (self - other).abs().packed_gt(F32x4::splat(epsilon)).all_false() |
144 | } |
145 | } |
146 | |
147 | impl AddAssign for F32x4 { |
148 | #[inline ] |
149 | fn add_assign(&mut self, other: F32x4) { |
150 | *self = *self + other |
151 | } |
152 | } |
153 | |
154 | impl SubAssign for F32x4 { |
155 | #[inline ] |
156 | fn sub_assign(&mut self, other: F32x4) { |
157 | *self = *self - other |
158 | } |
159 | } |
160 | |
161 | impl MulAssign for F32x4 { |
162 | #[inline ] |
163 | fn mul_assign(&mut self, other: F32x4) { |
164 | *self = *self * other |
165 | } |
166 | } |
167 | |
168 | impl Neg for F32x4 { |
169 | type Output = F32x4; |
170 | #[inline ] |
171 | fn neg(self) -> F32x4 { |
172 | F32x4::default() - self |
173 | } |
174 | } |
175 | |
176 | // Two 32-bit integers |
177 | |
178 | impl AddAssign for I32x2 { |
179 | #[inline ] |
180 | fn add_assign(&mut self, other: I32x2) { |
181 | *self = *self + other |
182 | } |
183 | } |
184 | |
185 | impl SubAssign for I32x2 { |
186 | #[inline ] |
187 | fn sub_assign(&mut self, other: I32x2) { |
188 | *self = *self - other |
189 | } |
190 | } |
191 | |
192 | impl MulAssign for I32x2 { |
193 | #[inline ] |
194 | fn mul_assign(&mut self, other: I32x2) { |
195 | *self = *self * other |
196 | } |
197 | } |
198 | |
199 | impl Neg for I32x2 { |
200 | type Output = I32x2; |
201 | #[inline ] |
202 | fn neg(self) -> I32x2 { |
203 | I32x2::default() - self |
204 | } |
205 | } |
206 | |
207 | // Four 32-bit integers |
208 | |
209 | impl I32x4 { |
210 | // Accessors |
211 | |
212 | #[inline ] |
213 | pub fn x(self) -> i32 { |
214 | self[0] |
215 | } |
216 | |
217 | #[inline ] |
218 | pub fn y(self) -> i32 { |
219 | self[1] |
220 | } |
221 | |
222 | #[inline ] |
223 | pub fn z(self) -> i32 { |
224 | self[2] |
225 | } |
226 | |
227 | #[inline ] |
228 | pub fn w(self) -> i32 { |
229 | self[3] |
230 | } |
231 | } |
232 | |
233 | impl AddAssign for I32x4 { |
234 | #[inline ] |
235 | fn add_assign(&mut self, other: I32x4) { |
236 | *self = *self + other |
237 | } |
238 | } |
239 | |
240 | impl SubAssign for I32x4 { |
241 | #[inline ] |
242 | fn sub_assign(&mut self, other: I32x4) { |
243 | *self = *self - other |
244 | } |
245 | } |
246 | |
247 | impl MulAssign for I32x4 { |
248 | #[inline ] |
249 | fn mul_assign(&mut self, other: I32x4) { |
250 | *self = *self * other |
251 | } |
252 | } |
253 | |
254 | impl Neg for I32x4 { |
255 | type Output = I32x4; |
256 | #[inline ] |
257 | fn neg(self) -> I32x4 { |
258 | I32x4::default() - self |
259 | } |
260 | } |
261 | |