Skip to main content

wide/
u16x16_.rs

1use super::*;
2
3pick! {
4  if #[cfg(target_feature="avx2")] {
5    #[derive(Default, Clone, Copy, PartialEq, Eq)]
6    #[repr(C, align(32))]
7    pub struct u16x16 { pub(crate) avx2: m256i }
8  } else {
9    #[derive(Default, Clone, Copy, PartialEq, Eq)]
10    #[repr(C, align(32))]
11    pub struct u16x16 { pub(crate) a : u16x8, pub(crate) b : u16x8 }
12  }
13}
14
15int_uint_consts!(u16, 16, u16x16, 256);
16
17unsafe impl Zeroable for u16x16 {}
18unsafe impl Pod for u16x16 {}
19
20impl AlignTo for u16x16 {
21  type Elem = u16;
22}
23
24impl Add for u16x16 {
25  type Output = Self;
26  #[inline]
27  fn add(self, rhs: Self) -> Self::Output {
28    pick! {
29      if #[cfg(target_feature="avx2")] {
30        Self { avx2: add_i16_m256i(self.avx2, rhs.avx2) }
31      } else {
32        Self {
33          a : self.a.add(rhs.a),
34          b : self.b.add(rhs.b),
35        }
36      }
37    }
38  }
39}
40
41impl Sub for u16x16 {
42  type Output = Self;
43  #[inline]
44  fn sub(self, rhs: Self) -> Self::Output {
45    pick! {
46      if #[cfg(target_feature="avx2")] {
47        Self { avx2: sub_i16_m256i(self.avx2, rhs.avx2) }
48      } else {
49        Self {
50          a : self.a.sub(rhs.a),
51          b : self.b.sub(rhs.b),
52        }
53      }
54    }
55  }
56}
57
58impl Shl for u16x16 {
59  type Output = Self;
60
61  /// Shifts lanes by the corresponding lane.
62  ///
63  /// Bitwise shift-left; yields `self << mask(rhs)`, where mask removes any
64  /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth
65  /// of the type. (same as `wrapping_shl`)
66  #[inline]
67  fn shl(self, rhs: Self) -> Self::Output {
68    pick! {
69      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
70        #[cfg(target_arch = "x86")]
71        use core::arch::x86::_mm256_sllv_epi16;
72        #[cfg(target_arch = "x86_64")]
73        use core::arch::x86_64::_mm256_sllv_epi16;
74
75        // Mask `rhs` to 15 to match `wrapping_shl`.
76        let rhs = bitand_m256i(rhs.avx2, set_splat_i16_m256i(15));
77        // TODO(safe_arch): Add `_mm256_sllv_epi16`.
78        cast(unsafe { _mm256_sllv_epi16(self.avx2.0, rhs.0) })
79      } else {
80        let [self_a, self_b]: [u16x8; 2] = cast(self);
81        let [rhs_a, rhs_b]: [u16x8; 2] = cast(rhs);
82
83        cast([self_a << rhs_a, self_b << rhs_b])
84      }
85    }
86  }
87}
88
89impl Shr for u16x16 {
90  type Output = Self;
91
92  /// Shifts lanes by the corresponding lane.
93  ///
94  /// Bitwise shift-right; yields `self >> mask(rhs)`, where mask removes any
95  /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth
96  /// of the type. (same as `wrapping_shr`)
97  #[inline]
98  fn shr(self, rhs: Self) -> Self::Output {
99    pick! {
100      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
101        #[cfg(target_arch = "x86")]
102        use core::arch::x86::_mm256_srlv_epi16;
103        #[cfg(target_arch = "x86_64")]
104        use core::arch::x86_64::_mm256_srlv_epi16;
105
106        // Mask `rhs` to 15 to match `wrapping_shr`.
107        let rhs = bitand_m256i(rhs.avx2, set_splat_i16_m256i(15));
108        // TODO(safe_arch): Add `_mm256_srlv_epi16`.
109        cast(unsafe { _mm256_srlv_epi16(self.avx2.0, rhs.0) })
110      } else {
111        let [self_a, self_b]: [u16x8; 2] = cast(self);
112        let [rhs_a, rhs_b]: [u16x8; 2] = cast(rhs);
113
114        cast([self_a >> rhs_a, self_b >> rhs_b])
115      }
116    }
117  }
118}
119
120impl Add<u16> for u16x16 {
121  type Output = Self;
122  #[inline]
123  fn add(self, rhs: u16) -> Self::Output {
124    self.add(Self::splat(rhs))
125  }
126}
127
128impl Sub<u16> for u16x16 {
129  type Output = Self;
130  #[inline]
131  fn sub(self, rhs: u16) -> Self::Output {
132    self.sub(Self::splat(rhs))
133  }
134}
135
136impl Mul<u16> for u16x16 {
137  type Output = Self;
138
139  #[inline]
140  fn mul(self, rhs: u16) -> Self::Output {
141    self * Self::splat(rhs)
142  }
143}
144
145impl Add<u16x16> for u16 {
146  type Output = u16x16;
147  #[inline]
148  fn add(self, rhs: u16x16) -> Self::Output {
149    u16x16::splat(self).add(rhs)
150  }
151}
152
153impl Sub<u16x16> for u16 {
154  type Output = u16x16;
155  #[inline]
156  fn sub(self, rhs: u16x16) -> Self::Output {
157    u16x16::splat(self).sub(rhs)
158  }
159}
160
161impl Mul<u16x16> for u16 {
162  type Output = u16x16;
163
164  #[inline]
165  fn mul(self, rhs: u16x16) -> Self::Output {
166    u16x16::splat(self) * rhs
167  }
168}
169
170impl BitAnd for u16x16 {
171  type Output = Self;
172  #[inline]
173  fn bitand(self, rhs: Self) -> Self::Output {
174    pick! {
175      if #[cfg(target_feature="avx2")] {
176        Self { avx2: bitand_m256i(self.avx2, rhs.avx2) }
177      } else {
178        Self {
179          a : self.a.bitand(rhs.a),
180          b : self.b.bitand(rhs.b),
181        }
182      }
183    }
184  }
185}
186
187impl BitOr for u16x16 {
188  type Output = Self;
189  #[inline]
190  fn bitor(self, rhs: Self) -> Self::Output {
191    pick! {
192      if #[cfg(target_feature="avx2")] {
193        Self { avx2: bitor_m256i(self.avx2, rhs.avx2) }
194      } else {
195        Self {
196          a : self.a.bitor(rhs.a),
197          b : self.b.bitor(rhs.b),
198        }
199      }
200    }
201  }
202}
203
204impl BitXor for u16x16 {
205  type Output = Self;
206  #[inline]
207  fn bitxor(self, rhs: Self) -> Self::Output {
208    pick! {
209      if #[cfg(target_feature="avx2")] {
210        Self { avx2: bitxor_m256i(self.avx2, rhs.avx2) }
211      } else {
212        Self {
213          a : self.a.bitxor(rhs.a),
214          b : self.b.bitxor(rhs.b),
215        }
216      }
217    }
218  }
219}
220
221impl Not for u16x16 {
222  type Output = Self;
223  #[inline]
224  fn not(self) -> Self {
225    pick! {
226      if #[cfg(target_feature="avx2")] {
227        Self { avx2: self.avx2.not()  }
228      } else {
229        Self {
230          a : self.a.not(),
231          b : self.b.not(),
232        }
233      }
234    }
235  }
236}
237
238macro_rules! impl_shl_t_for_u16x16 {
239  ($($shift_type:ty),+ $(,)?) => {
240    $(impl Shl<$shift_type> for u16x16 {
241      type Output = Self;
242      /// Shifts all lanes by the value given.
243      #[inline]
244      fn shl(self, rhs: $shift_type) -> Self::Output {
245        pick! {
246          if #[cfg(target_feature="avx2")] {
247            let shift = cast([rhs as u64, 0]);
248            Self { avx2: shl_all_u16_m256i(self.avx2, shift) }
249          } else {
250            Self {
251              a : self.a.shl(rhs),
252              b : self.b.shl(rhs),
253            }
254          }
255       }
256     }
257    })+
258  };
259}
260impl_shl_t_for_u16x16!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
261
262macro_rules! impl_shr_t_for_u16x16 {
263  ($($shift_type:ty),+ $(,)?) => {
264    $(impl Shr<$shift_type> for u16x16 {
265      type Output = Self;
266      /// Shifts all lanes by the value given.
267      #[inline]
268      fn shr(self, rhs: $shift_type) -> Self::Output {
269        pick! {
270          if #[cfg(target_feature="avx2")] {
271            let shift = cast([rhs as u64, 0]);
272            Self { avx2: shr_all_u16_m256i(self.avx2, shift) }
273          } else {
274            Self {
275              a : self.a.shr(rhs),
276              b : self.b.shr(rhs),
277            }
278          }
279        }
280      }
281    })+
282  };
283}
284impl_shr_t_for_u16x16!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
285
286#[expect(deprecated)]
287impl CmpEq for u16x16 {
288  type Output = Self;
289  #[inline]
290  fn simd_eq(self, rhs: Self) -> Self::Output {
291    pick! {
292      if #[cfg(target_feature="avx2")] {
293        Self { avx2: cmp_eq_mask_i16_m256i(self.avx2, rhs.avx2) }
294      } else {
295        Self {
296          a : self.a.simd_eq(rhs.a),
297          b : self.b.simd_eq(rhs.b),
298        }
299      }
300    }
301  }
302}
303
304#[expect(deprecated)]
305impl CmpGt for u16x16 {
306  type Output = Self;
307  #[inline]
308  fn simd_gt(self, rhs: Self) -> Self::Output {
309    pick! {
310      if #[cfg(target_feature = "avx2")] {
311        let bias = m256i::from([0x8000u16; 16]);
312        let a_biased = sub_i16_m256i(self.avx2, bias);
313        let b_biased = sub_i16_m256i(rhs.avx2, bias);
314        let mask = cmp_gt_mask_i16_m256i(a_biased, b_biased);
315
316        Self { avx2: mask }
317      } else {
318        Self {
319          a: self.a.simd_gt(rhs.a),
320          b: self.b.simd_gt(rhs.b),
321        }
322      }
323    }
324  }
325}
326
327#[expect(deprecated)]
328impl CmpLt for u16x16 {
329  type Output = Self;
330  #[inline]
331  fn simd_lt(self, rhs: Self) -> Self::Output {
332    // no gt, so just reverse to get same answer
333    Self::simd_gt(rhs, self)
334  }
335}
336
337#[expect(deprecated)]
338impl CmpNe for u16x16 {
339  type Output = Self;
340  #[inline]
341  fn simd_ne(self, rhs: Self) -> Self::Output {
342    pick! {
343      if #[cfg(target_feature="avx2")] {
344        !self.simd_eq(rhs)
345      } else {
346        Self {
347          a : self.a.simd_ne(rhs.a),
348          b : self.b.simd_ne(rhs.b),
349        }
350      }
351    }
352  }
353}
354
355#[expect(deprecated)]
356impl CmpLe for u16x16 {
357  type Output = Self;
358  #[inline]
359  fn simd_le(self, rhs: Self) -> Self::Output {
360    pick! {
361      if #[cfg(target_feature="avx2")] {
362        !self.simd_gt(rhs)
363      } else {
364        Self {
365          a : self.a.simd_le(rhs.a),
366          b : self.b.simd_le(rhs.b),
367        }
368      }
369    }
370  }
371}
372
373#[expect(deprecated)]
374impl CmpGe for u16x16 {
375  type Output = Self;
376  #[inline]
377  fn simd_ge(self, rhs: Self) -> Self::Output {
378    pick! {
379      if #[cfg(target_feature="avx2")] {
380        !self.simd_lt(rhs)
381      } else {
382        Self {
383          a : self.a.simd_ge(rhs.a),
384          b : self.b.simd_ge(rhs.b),
385        }
386      }
387    }
388  }
389}
390
391impl Mul for u16x16 {
392  type Output = Self;
393  #[inline]
394  fn mul(self, rhs: Self) -> Self::Output {
395    pick! {
396      if #[cfg(target_feature="avx2")] {
397        // non-widening multiplication is the same for unsigned and signed
398        Self { avx2: mul_i16_keep_low_m256i(self.avx2, rhs.avx2) }
399      } else {
400        Self {
401          a : self.a.mul(rhs.a),
402          b : self.b.mul(rhs.b),
403        }
404      }
405    }
406  }
407}
408
409integer_impl_div_rem!(
410  u16,
411  u16x16,
412  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
413);
414
415impl From<u8x16> for u16x16 {
416  /// widens and sign extends to u16x16
417  #[inline]
418  fn from(v: u8x16) -> Self {
419    pick! {
420      if #[cfg(target_feature="avx2")] {
421        u16x16 { avx2:convert_to_i16_m256i_from_u8_m128i(v.sse) }
422      } else if #[cfg(target_feature="sse2")] {
423        u16x16 {
424          a: u16x8 { sse: shr_imm_u16_m128i::<8>( unpack_low_i8_m128i(v.sse, v.sse)) },
425          b: u16x8 { sse: shr_imm_u16_m128i::<8>( unpack_high_i8_m128i(v.sse, v.sse)) },
426        }
427      } else {
428
429        u16x16::new([
430          v.as_array()[0] as u16,
431          v.as_array()[1] as u16,
432          v.as_array()[2] as u16,
433          v.as_array()[3] as u16,
434          v.as_array()[4] as u16,
435          v.as_array()[5] as u16,
436          v.as_array()[6] as u16,
437          v.as_array()[7] as u16,
438          v.as_array()[8] as u16,
439          v.as_array()[9] as u16,
440          v.as_array()[10] as u16,
441          v.as_array()[11] as u16,
442          v.as_array()[12] as u16,
443          v.as_array()[13] as u16,
444          v.as_array()[14] as u16,
445          v.as_array()[15] as u16,
446          ])
447      }
448    }
449  }
450}
451
452impl u16x16 {
453  #[inline]
454  #[must_use]
455  pub const fn new(array: [u16; 16]) -> Self {
456    unsafe { core::mem::transmute(array) }
457  }
458
459  simd_comparison_fns!();
460
461  #[inline]
462  #[must_use]
463  pub fn blend(self, t: Self, f: Self) -> Self {
464    pick! {
465      if #[cfg(target_feature="avx2")] {
466        Self { avx2: blend_varying_i8_m256i(f.avx2, t.avx2, self.avx2) }
467      } else {
468        Self {
469          a : self.a.blend(t.a, f.a),
470          b : self.b.blend(t.b, f.b),
471        }
472      }
473    }
474  }
475
476  #[inline]
477  #[must_use]
478  pub fn reduce_add(self) -> u16 {
479    cast(i16x16::reduce_add(cast(self)))
480  }
481
482  #[inline]
483  #[must_use]
484  pub fn reduce_max(self) -> u16 {
485    let array: [u16x8; 2] = cast(self);
486    array[0].max(array[1]).reduce_max()
487  }
488
489  #[inline]
490  #[must_use]
491  pub fn reduce_min(self) -> u16 {
492    let array: [u16x8; 2] = cast(self);
493    array[0].min(array[1]).reduce_min()
494  }
495
496  #[inline]
497  #[must_use]
498  pub fn max(self, rhs: Self) -> Self {
499    pick! {
500      if #[cfg(target_feature="avx2")] {
501        Self { avx2: max_u16_m256i(self.avx2, rhs.avx2) }
502      } else {
503        Self {
504          a : self.a.max(rhs.a),
505          b : self.b.max(rhs.b),
506        }
507      }
508    }
509  }
510  #[inline]
511  #[must_use]
512  pub fn min(self, rhs: Self) -> Self {
513    pick! {
514      if #[cfg(target_feature="avx2")] {
515        Self { avx2: min_u16_m256i(self.avx2, rhs.avx2) }
516      } else {
517        Self {
518          a : self.a.min(rhs.a),
519          b : self.b.min(rhs.b),
520        }
521      }
522    }
523  }
524
525  integer_fn_clamp!();
526
527  #[inline]
528  #[must_use]
529  pub fn saturating_add(self, rhs: Self) -> Self {
530    pick! {
531      if #[cfg(target_feature="avx2")] {
532        Self { avx2: add_saturating_u16_m256i(self.avx2, rhs.avx2) }
533      } else {
534        Self {
535          a : self.a.saturating_add(rhs.a),
536          b : self.b.saturating_add(rhs.b),
537        }
538      }
539    }
540  }
541  #[inline]
542  #[must_use]
543  pub fn saturating_sub(self, rhs: Self) -> Self {
544    pick! {
545      if #[cfg(target_feature="avx2")] {
546        Self { avx2: sub_saturating_u16_m256i(self.avx2, rhs.avx2) }
547      } else {
548        Self {
549          a : self.a.saturating_sub(rhs.a),
550          b : self.b.saturating_sub(rhs.b),
551        }
552      }
553    }
554  }
555
556  /// Lanewise saturating multiply.
557  #[inline]
558  #[must_use]
559  pub fn saturating_mul(self, rhs: Self) -> Self {
560    let [self_a, self_b]: [u16x8; 2] = cast(self);
561    let [rhs_a, rhs_b]: [u16x8; 2] = cast(rhs);
562    cast([self_a.saturating_mul(rhs_a), self_b.saturating_mul(rhs_b)])
563  }
564
565  integer_fn_saturating_div!([
566    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
567  ]);
568
569  #[inline]
570  #[must_use]
571  #[doc(alias("movemask", "move_mask"))]
572  pub fn to_bitmask(self) -> u32 {
573    i16x16::to_bitmask(cast(self))
574  }
575
576  #[inline]
577  #[must_use]
578  pub fn any(self) -> bool {
579    i16x16::any(cast(self))
580  }
581
582  #[inline]
583  #[must_use]
584  pub fn all(self) -> bool {
585    i16x16::all(cast(self))
586  }
587
588  #[inline]
589  #[must_use]
590  pub fn none(self) -> bool {
591    !self.any()
592  }
593
594  /// Transpose matrix of 16x16 `u16` matrix. Currently not accelerated.
595  #[must_use]
596  #[inline]
597  pub fn transpose(data: [u16x16; 16]) -> [u16x16; 16] {
598    cast(i16x16::transpose(cast(data)))
599  }
600
601  #[inline]
602  pub fn to_array(self) -> [u16; 16] {
603    cast(self)
604  }
605
606  #[inline]
607  pub fn as_array(&self) -> &[u16; 16] {
608    cast_ref(self)
609  }
610
611  #[inline]
612  pub fn as_mut_array(&mut self) -> &mut [u16; 16] {
613    cast_mut(self)
614  }
615}