Debian Codegraph @

debian/missing-sources/glam/templates/vec_mask.rs.tera

raw
1 {% import "macros.rs.tera" as macros %}
2
3 // Generated from {{template_path}} template. Edit the template, not the generated file.
4
5 {% if scalar_t == "bool" %}
6 {% set self_t = "BVec" ~ dim %}
7 {% set align = 1 %}
8 {% set is_bool = true %}
9 {% else %}
10 {% set self_t = "BVec" ~ dim ~ "A" %}
11 {% if dim == 4 %}
12 {% set vec_t = "Vec4" %}
13 {% elif dim == 3 %}
14 {% set vec_t = "Vec3A" %}
15 {% endif %}
16 {% set align = 16 %}
17 {% set is_u32 = true %}
18 {% if is_sse2 %}
19 {% set simd_t = "__m128" %}
20 {% elif is_wasm32 %}
21 {% set simd_t = "v128" %}
22 {% elif is_neon %}
23 {% set simd_t = "uint32x4_t" %}
24 {% elif is_coresimd %}
25 {% set simd_t = "mask32x4" %}
26 {% endif %}
27 {% endif %}
28
29 {% set components = ["x", "y", "z", "w"] | slice(end = dim) %}
30
31 use core::fmt;
32 use core::ops::*;
33
34 {% if is_sse2 %}
35 #[cfg(target_arch = "x86")]
36 use core::arch::x86::*;
37 #[cfg(target_arch = "x86_64")]
38 use core::arch::x86_64::*;
39 {% elif is_wasm32 %}
40 use core::arch::wasm32::*;
41 {% elif is_neon %}
42 use core::arch::aarch64::*;
43 {% elif is_coresimd %}
44 use core::simd::*;
45 {% endif %}
46
47 {% if is_sse2 or is_neon or is_coresimd %}
48 #[repr(C)]
49 union UnionCast {
50 a: [u32; 4],
51 v: {{ self_t }}
52 }
53 {% endif %}
54
55 /// Creates a {{ dim }}-dimensional `bool` vector mask.
56 #[inline(always)]
57 #[must_use]
58 pub const fn {{ self_t | lower }}(
59 {% for c in components %}
60 {{ c }}: bool,
61 {% endfor %}
62 ) -> {{ self_t }} {
63 {{ self_t }}::new({{ components | join(sep=",") }})
64 }
65
66 {% if is_scalar and is_bool %}
67 /// A {{ dim }}-dimensional `bool` vector mask.
68 {%- elif is_scalar and is_u32 %}
69 /// A {{ dim }}-dimensional `u32` vector mask.
70 {%- else %}
71 /// A {{ dim }}-dimensional SIMD vector mask.
72 ///
73 /// This type is {{ align }} byte aligned.
74 {%- endif %}
75 {%- if is_scalar or is_bool %}
76 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
77 {%- else %}
78 #[derive(Clone, Copy)]
79 {%- endif %}
80 {%- if is_scalar %}
81 #[repr(C, align({{ align }}))]
82 #[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
83 pub struct {{ self_t }}
84 {
85 {% for c in components %}
86 pub {{ c }}: {{ scalar_t }},
87 {%- endfor %}
88 }
89 {%- else %}
90 #[repr(transparent)]
91 pub struct {{ self_t }}(pub(crate) {{ simd_t }});
92 {% endif %}
93
94 const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
95
96 impl {{ self_t }} {
97 /// All false.
98 pub const FALSE: Self = Self::splat(false);
99
100 /// All true.
101 pub const TRUE: Self = Self::splat(true);
102
103 /// Creates a new vector mask.
104 #[inline(always)]
105 #[must_use]
106 pub const fn new(
107 {% for c in components %}
108 {{ c }}: bool,
109 {% endfor %}
110 ) -> Self {
111 {% if is_scalar and is_bool %}
112 Self {
113 {% for c in components %}
114 {{ c }},
115 {%- endfor %}
116 }
117 {% elif is_scalar and is_u32 %}
118 Self {
119 {% for c in components %}
120 {{ c }}: MASK[{{ c }} as usize],
121 {%- endfor %}
122 }
123 {% elif is_sse2 or is_neon or is_coresimd %}
124 unsafe {
125 UnionCast { a: [
126 MASK[x as usize],
127 MASK[y as usize],
128 MASK[z as usize],
129 {% if dim == 3 %}
130 0,
131 {% elif dim == 4 %}
132 MASK[w as usize],
133 {% endif %}
134 ] }.v
135 }
136 {% elif is_wasm32 %}
137 Self(u32x4(
138 MASK[x as usize],
139 MASK[y as usize],
140 MASK[z as usize],
141 {% if dim == 3 %}
142 0,
143 {% elif dim == 4 %}
144 MASK[w as usize],
145 {% endif %}
146 ))
147 {% endif %}
148 }
149
150 /// Creates a vector mask with all elements set to `v`.
151 #[inline]
152 #[must_use]
153 pub const fn splat(v: bool) -> Self {
154 Self::new(
155 {% for c in components %}
156 v,
157 {%- endfor %}
158 )
159 }
160
161 /// Creates a new vector mask from a bool array.
162 #[inline]
163 #[must_use]
164 pub const fn from_array(a: [bool; {{ dim }}]) -> Self {
165 Self::new(
166 {% for c in components %}
167 a[{{ loop.index0 }}],
168 {%- endfor %}
169 )
170 }
171
172 /// Returns a bitmask with the lowest {{ dim }} bits set from the elements of `self`.
173 ///
174 /// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
175 /// into the first lowest bit, element `y` into the second, etc.
176 #[inline]
177 #[must_use]
178 pub fn bitmask(self) -> u32 {
179 {% if is_scalar and is_bool %}
180 {% for c in components %}
181 {% if loop.first %}
182 (self.{{ c }} as u32) |
183 {% else %}
184 ((self.{{ c }} as u32) << {{ loop.index0 }}) {% if not loop.last %} | {% endif %}
185 {% endif %}
186 {% endfor %}
187 {% elif is_scalar and is_u32 %}
188 {% for c in components %}
189 {% if loop.first %}
190 (self.{{ c }} & 0x1) |
191 {% else %}
192 ((self.{{ c }} & 0x1) << {{ loop.index0 }}) {% if not loop.last %} | {% endif %}
193 {% endif %}
194 {% endfor %}
195 {% elif is_sse2 %}
196 {% if dim == 3 %}
197 unsafe { (_mm_movemask_ps(self.0) as u32) & 0x7 }
198 {% elif dim == 4 %}
199 unsafe { _mm_movemask_ps(self.0) as u32 }
200 {% endif %}
201 {% elif is_wasm32 %}
202 {% if dim == 3 %}
203 (u32x4_bitmask(self.0) & 0x7) as u32
204 {% elif dim == 4 %}
205 u32x4_bitmask(self.0) as u32
206 {% endif %}
207 {% elif is_neon %}
208 {% if dim == 3 %}
209 let movemask = unsafe {
210 {% elif dim == 4 %}
211 unsafe {
212 {% endif %}
213 let mma = vandq_u32(self.0, vld1q_u32([1, 2, 4, 8].as_ptr())); // [0 1 2 3]
214 let mmb = vextq_u32(mma, mma, 2); // [2 3 0 1]
215 let mmc = vorrq_u32(mma, mmb); // [0+2 1+3 0+2 1+3]
216 let mmd = vextq_u32(mmc, mmc, 3); // [1+3 0+2 1+3 0+2]
217 let mme = vorrq_u32(mmc, mmd); // [0+1+2+3 ...]
218 vgetq_lane_u32(mme, 0)
219 {% if dim == 3 %}
220 };
221
222 movemask & 0x7
223 {% elif dim == 4 %}
224 }
225 {% endif %}
226 {% elif is_coresimd %}
227 {% if dim == 3 %}
228 (self.0.to_bitmask() & 0x7) as u32
229 {% elif dim == 4 %}
230 self.0.to_bitmask() as u32
231 {% endif %}
232 {% else %}
233 unimplemented!()
234 {% endif %}
235 }
236
237 /// Returns true if any of the elements are true, false otherwise.
238 #[inline]
239 #[must_use]
240 pub fn any(self) -> bool {
241 {% if is_scalar and is_bool %}
242 {% for c in components %}
243 self.{{ c }} {% if not loop.last %} || {% endif %}
244 {%- endfor %}
245 {% elif is_scalar and is_u32 %}
246 ((
247 {% for c in components %}
248 self.{{ c }} {% if not loop.last %} | {% endif %}
249 {%- endfor %}
250 ) & 0x1) != 0
251 {% else %}
252 self.bitmask() != 0
253 {% endif %}
254 }
255
256 /// Returns true if all the elements are true, false otherwise.
257 #[inline]
258 #[must_use]
259 pub fn all(self) -> bool {
260 {% if is_scalar and is_bool %}
261 {% for c in components %}
262 self.{{ c }} {% if not loop.last %} && {% endif %}
263 {%- endfor %}
264 {% elif is_scalar and is_u32 %}
265 ((
266 {% for c in components %}
267 self.{{ c }} {% if not loop.last %} & {% endif %}
268 {%- endfor %}
269 ) & 0x1) != 0
270 {% else %}
271 {% if dim == 3 %}
272 self.bitmask() == 0x7
273 {% elif dim == 4 %}
274 self.bitmask() == 0xf
275 {% endif %}
276 {% endif %}
277 }
278
279 /// Tests the value at `index`.
280 ///
281 /// Panics if `index` is greater than {{ dim - 1 }}.
282 #[inline]
283 #[must_use]
284 pub fn test(&self, index: usize) -> bool {
285 {% if is_coresimd %}
286 assert!(index < {{ dim }}, "index out of bounds");
287 self.0.test(index)
288 {% else %}
289 match index {
290 {% for c in components %}
291 {%- if is_scalar and is_bool %}
292 {{ loop.index0 }} => self.{{ c }},
293 {%- elif is_scalar and is_u32 %}
294 {{ loop.index0 }} => (self.{{ c }} & 0x1) != 0,
295 {%- else %}
296 {{ loop.index0 }} => (self.bitmask() & (1 << {{ loop.index0 }})) != 0,
297 {%- endif %}
298 {%- endfor %}
299 _ => panic!("index out of bounds")
300 }
301 {% endif %}
302 }
303
304 /// Sets the element at `index`.
305 ///
306 /// Panics if `index` is greater than {{ dim - 1 }}.
307 #[inline]
308 pub fn set(&mut self, index: usize, value: bool) {
309 {% if is_scalar %}
310 match index {
311 {% for c in components %}
312 {%- if is_bool %}
313 {{ loop.index0 }} => self.{{ c }} = value,
314 {%- elif is_u32 %}
315 {{ loop.index0 }} => self.{{ c }} = MASK[value as usize],
316 {%- endif %}
317 {%- endfor %}
318 _ => panic!("index out of bounds")
319 }
320 {% elif is_coresimd %}
321 {% if dim < 4 %}
322 assert!(index < {{ dim }}, "index out of bounds");
323 {%- endif %}
324 self.0.set(index, value)
325 {% elif is_neon %}
326 self.0 = match index {
327 {% for c in components %}
328 {{ loop.index0 }} => unsafe {
329 vsetq_lane_u32(MASK[value as usize], self.0, {{ loop.index0 }})
330 },
331 {%- endfor %}
332 _ => panic!("index out of bounds")
333 }
334 {% else %}
335 use crate::{{ vec_t }};
336 let mut v = {{ vec_t }}(self.0);
337 v[index] = f32::from_bits(MASK[value as usize]);
338 self.0 = v.0;
339 {% endif %}
340 }
341
342 #[inline]
343 #[must_use]
344 fn into_bool_array(self) -> [bool; {{ dim }}] {
345 {% if is_scalar and is_bool %}
346 [
347 {% for c in components %}
348 self.{{ c }},
349 {%- endfor %}
350 ]
351 {% elif is_scalar and is_u32 %}
352 [
353 {% for c in components %}
354 (self.{{ c }} & 0x1) != 0,
355 {%- endfor %}
356 ]
357 {% else %}
358 {% set bits = [1, 2, 4, 8] | slice(end = dim) %}
359 let bitmask = self.bitmask();
360 [
361 {% for b in bits %}
362 (bitmask & {{ b }}) != 0,
363 {%- endfor %}
364 ]
365 {% endif %}
366 }
367
368 #[inline]
369 #[must_use]
370 fn into_u32_array(self) -> [u32; {{ dim }}] {
371 {% if is_scalar and is_bool %}
372 [
373 {% for c in components %}
374 MASK[self.{{ c }} as usize],
375 {%- endfor %}
376 ]
377 {% elif is_scalar and is_u32 %}
378 [
379 {% for c in components %}
380 self.{{ c }},
381 {%- endfor %}
382 ]
383 {% else %}
384 let bitmask = self.bitmask();
385 [
386 {% for c in components %}
387 {% if loop.first %}
388 MASK[(bitmask & 1) as usize],
389 {% else %}
390 MASK[((bitmask >> {{ loop.index0 }}) & 1) as usize],
391 {% endif %}
392 {%- endfor %}
393 ]
394 {% endif %}
395 }
396 }
397
398
399 impl Default for {{ self_t }} {
400 #[inline]
401 fn default() -> Self {
402 Self::FALSE
403 }
404 }
405
406 {% if not is_scalar and not is_bool %}
407 impl PartialEq for {{ self_t }} {
408 #[inline]
409 fn eq(&self, rhs: &Self) -> bool {
410 self.bitmask().eq(&rhs.bitmask())
411 }
412 }
413
414 impl Eq for {{ self_t }} {}
415
416 impl core::hash::Hash for {{ self_t }} {
417 #[inline]
418 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
419 self.bitmask().hash(state);
420 }
421 }
422 {%- endif %}
423
424 impl BitAnd for {{ self_t }} {
425 type Output = Self;
426 #[inline]
427 fn bitand(self, rhs: Self) -> Self {
428 {% if is_scalar %}
429 Self {
430 {% for c in components %}
431 {{ c }}: self.{{ c }} & rhs.{{ c }},
432 {%- endfor %}
433 }
434 {% elif is_sse2 %}
435 Self(unsafe { _mm_and_ps(self.0, rhs.0) })
436 {% elif is_wasm32 %}
437 Self(v128_and(self.0, rhs.0))
438 {% elif is_neon %}
439 Self(unsafe { vandq_u32(self.0, rhs.0) })
440 {% elif is_coresimd %}
441 Self(self.0 & rhs.0)
442 {% else %}
443 unimplemented!()
444 {% endif %}
445 }
446 }
447
448 {{ macros::impl_ref_binop(imp="BitAnd", self_t=self_t, rhs_t=self_t, output_t=self_t) }}
449
450 {{ macros::impl_op_assign(imp="BitAnd", self_t=self_t, rhs_t=self_t) }}
451
452 impl BitOr for {{ self_t }} {
453 type Output = Self;
454 #[inline]
455 fn bitor(self, rhs: Self) -> Self {
456 {% if is_scalar %}
457 Self {
458 {% for c in components %}
459 {{ c }}: self.{{ c }} | rhs.{{ c }},
460 {%- endfor %}
461 }
462 {% elif is_sse2 %}
463 Self(unsafe { _mm_or_ps(self.0, rhs.0) })
464 {% elif is_wasm32 %}
465 Self(v128_or(self.0, rhs.0))
466 {% elif is_neon %}
467 Self(unsafe { vorrq_u32(self.0, rhs.0) })
468 {% elif is_coresimd %}
469 Self(self.0 | rhs.0)
470 {% else %}
471 unimplemented!()
472 {% endif %}
473 }
474 }
475
476 {{ macros::impl_ref_binop(imp="BitOr", self_t=self_t, rhs_t=self_t, output_t=self_t) }}
477
478 {{ macros::impl_op_assign(imp="BitOr", self_t=self_t, rhs_t=self_t) }}
479
480 impl BitXor for {{ self_t }} {
481 type Output = Self;
482 #[inline]
483 fn bitxor(self, rhs: Self) -> Self {
484 {% if is_scalar %}
485 Self {
486 {% for c in components %}
487 {{ c }}: self.{{ c }} ^ rhs.{{ c }},
488 {%- endfor %}
489 }
490 {% elif is_sse2 %}
491 Self(unsafe { _mm_xor_ps(self.0, rhs.0) })
492 {% elif is_wasm32 %}
493 Self(v128_xor(self.0, rhs.0))
494 {% elif is_neon %}
495 Self(unsafe { veorq_u32(self.0, rhs.0) })
496 {% elif is_coresimd %}
497 Self(self.0 ^ rhs.0)
498 {% else %}
499 unimplemented!()
500 {% endif %}
501 }
502 }
503
504 {{ macros::impl_ref_binop(imp="BitXor", self_t=self_t, rhs_t=self_t, output_t=self_t) }}
505
506 {{ macros::impl_op_assign(imp="BitXor", self_t=self_t, rhs_t=self_t) }}
507
508 impl Not for {{ self_t }} {
509 type Output = Self;
510 #[inline]
511 fn not(self) -> Self {
512 {% if is_scalar %}
513 Self {
514 {% for c in components %}
515 {{ c }}: !self.{{ c }},
516 {%- endfor %}
517 }
518 {% elif is_sse2 %}
519 Self(unsafe {
520 _mm_andnot_ps(self.0, _mm_set_ps1(f32::from_bits(0xff_ff_ff_ff)))
521 })
522 {% elif is_wasm32 %}
523 Self(v128_not(self.0))
524 {% elif is_neon %}
525 Self(unsafe { vmvnq_u32(self.0) })
526 {% elif is_coresimd %}
527 Self(!self.0)
528 {% else %}
529 unimplemented!()
530 {% endif %}
531 }
532 }
533
534 {{ macros::impl_ref_unop(imp="Not", self_t=self_t, output_t=self_t) }}
535
536 {% if not is_scalar %}
537 impl From<{{ self_t }}> for {{ simd_t }} {
538 #[inline]
539 fn from(t: {{ self_t }}) -> Self {
540 t.0
541 }
542 }
543 {% endif %}
544
545 impl fmt::Debug for {{ self_t }} {
546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
547 let arr = self.into_u32_array();
548 {%- if dim == 2 %}
549 write!(f, "{}({:#x}, {:#x})", stringify!({{ self_t }}), arr[0], arr[1])
550 {% elif dim == 3 %}
551 write!(f, "{}({:#x}, {:#x}, {:#x})", stringify!({{ self_t }}), arr[0], arr[1], arr[2])
552 {% elif dim == 4 %}
553 write!(f, "{}({:#x}, {:#x}, {:#x}, {:#x})", stringify!({{ self_t }}), arr[0], arr[1], arr[2], arr[3])
554 {% endif %}
555 }
556 }
557
558 impl fmt::Display for {{ self_t }} {
559 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
560 let arr = self.into_bool_array();
561 {%- if dim == 2 %}
562 write!(f, "[{}, {}]", arr[0], arr[1])
563 {% elif dim == 3 %}
564 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
565 {% elif dim == 4 %}
566 write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
567 {% endif %}
568 }
569 }
570
571 impl From<[bool; {{ dim }}]> for {{ self_t }} {
572 #[inline]
573 fn from(a: [bool; {{ dim }}]) -> Self {
574 Self::from_array(a)
575 }
576 }
577
578 impl From<{{ self_t }}> for [bool; {{ dim }}] {
579 #[inline]
580 fn from(mask: {{ self_t }}) -> Self {
581 mask.into_bool_array()
582 }
583 }
584
585 impl From<{{ self_t }}> for [u32; {{ dim }}] {
586 #[inline]
587 fn from(mask: {{ self_t }}) -> Self {
588 mask.into_u32_array()
589 }
590 }