Skip to main content

wide/
u16x8_.rs

1use super::*;
2
3pick! {
4  if #[cfg(target_feature="sse2")] {
5    /// A SIMD vector with eight elements of type [`u16`].
6    ///
7    /// See the [crate level documentation] for more information about SIMD
8    /// vectors.
9    ///
10    /// [crate level documentation]: crate
11    #[derive(Default, Clone, Copy, PartialEq, Eq)]
12    #[repr(C, align(16))]
13    pub struct u16x8 { pub(crate) sse: m128i }
14  } else if #[cfg(target_feature="simd128")] {
15    use core::arch::wasm32::*;
16
17    /// A SIMD vector with eight elements of type [`u16`].
18    ///
19    /// See the [crate level documentation] for more information about SIMD
20    /// vectors.
21    ///
22    /// [crate level documentation]: crate
23    #[derive(Clone, Copy)]
24    #[repr(transparent)]
25    pub struct u16x8 { pub(crate) simd: v128 }
26
27    impl Default for u16x8 {
28      fn default() -> Self {
29        Self::splat(0)
30      }
31    }
32
33    impl PartialEq for u16x8 {
34      fn eq(&self, other: &Self) -> bool {
35        u16x8_all_true(u16x8_eq(self.simd, other.simd))
36      }
37    }
38
39    impl Eq for u16x8 { }
40  } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
41      use core::arch::aarch64::*;
42
43      /// A SIMD vector with eight elements of type [`u16`].
44      ///
45      /// See the [crate level documentation] for more information about SIMD
46      /// vectors.
47      ///
48      /// [crate level documentation]: crate
49      #[repr(C)]
50      #[derive(Copy, Clone)]
51      pub struct u16x8 { pub(crate) neon : uint16x8_t }
52
53      impl Default for u16x8 {
54        #[inline]
55        fn default() -> Self {
56          Self::splat(0)
57        }
58      }
59
60      impl PartialEq for u16x8 {
61        #[inline]
62        fn eq(&self, other: &Self) -> bool {
63          unsafe { vminvq_u16(vceqq_u16(self.neon, other.neon))==u16::MAX }
64        }
65      }
66
67      impl Eq for u16x8 { }
68  } else {
69    /// A SIMD vector with eight elements of type [`u16`].
70    ///
71    /// See the [crate level documentation] for more information about SIMD
72    /// vectors.
73    ///
74    /// [crate level documentation]: crate
75    #[derive(Default, Clone, Copy, PartialEq, Eq)]
76    #[repr(C, align(16))]
77    pub struct u16x8 { pub(crate) arr: [u16;8] }
78  }
79}
80
81impl_simd_uint! {
82  unsafe {
83    T = u16,
84    N = 8,
85    Simd = u16x8,
86    IntSimd = i16x8,
87    T_BITS = 16,
88    T_BITS_MUL_2 = 32,
89    [0, 1, 2, 3, 4, 5, 6, 7],
90    optional_type_x86_inner { X86Inner = __m128i },
91    optional_type_arm_inner { ArmInner = uint16x8_t },
92    optional_type_wasm_inner { WasmInner = v128 },
93  }
94
95  #[inline]
96  fn not(self) -> Self::Output {
97    self ^ cast::<u128, u16x8>(u128::MAX)
98  }
99
100  #[inline]
101  fn add(self, rhs: Self) -> Self::Output {
102    pick! {
103      if #[cfg(target_feature="sse2")] {
104        Self { sse: add_i16_m128i(self.sse, rhs.sse) }
105      } else if #[cfg(target_feature="simd128")] {
106        Self { simd: u16x8_add(self.simd, rhs.simd) }
107      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
108        unsafe { Self { neon: vaddq_u16(self.neon, rhs.neon) } }
109      } else {
110        Self { arr: [
111          self.arr[0].wrapping_add(rhs.arr[0]),
112          self.arr[1].wrapping_add(rhs.arr[1]),
113          self.arr[2].wrapping_add(rhs.arr[2]),
114          self.arr[3].wrapping_add(rhs.arr[3]),
115          self.arr[4].wrapping_add(rhs.arr[4]),
116          self.arr[5].wrapping_add(rhs.arr[5]),
117          self.arr[6].wrapping_add(rhs.arr[6]),
118          self.arr[7].wrapping_add(rhs.arr[7]),
119        ]}
120      }
121    }
122  }
123
124  #[inline]
125  fn sub(self, rhs: Self) -> Self::Output {
126    pick! {
127      if #[cfg(target_feature="sse2")] {
128        Self { sse: sub_i16_m128i(self.sse, rhs.sse) }
129      } else if #[cfg(target_feature="simd128")] {
130        Self { simd: u16x8_sub(self.simd, rhs.simd) }
131      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
132        unsafe {Self { neon: vsubq_u16(self.neon, rhs.neon) }}
133      } else {
134        Self { arr: [
135          self.arr[0].wrapping_sub(rhs.arr[0]),
136          self.arr[1].wrapping_sub(rhs.arr[1]),
137          self.arr[2].wrapping_sub(rhs.arr[2]),
138          self.arr[3].wrapping_sub(rhs.arr[3]),
139          self.arr[4].wrapping_sub(rhs.arr[4]),
140          self.arr[5].wrapping_sub(rhs.arr[5]),
141          self.arr[6].wrapping_sub(rhs.arr[6]),
142          self.arr[7].wrapping_sub(rhs.arr[7]),
143        ]}
144      }
145    }
146  }
147
148  #[inline]
149  fn mul(self, rhs: Self) -> Self::Output {
150    pick! {
151      if #[cfg(target_feature="sse2")] {
152        Self { sse: mul_i16_keep_low_m128i(self.sse, rhs.sse) }
153      } else if #[cfg(target_feature="simd128")] {
154        Self { simd: u16x8_mul(self.simd, rhs.simd) }
155      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
156        unsafe {Self { neon: vmulq_u16(self.neon, rhs.neon) }}
157      } else {
158        Self { arr: [
159          self.arr[0].wrapping_mul(rhs.arr[0]),
160          self.arr[1].wrapping_mul(rhs.arr[1]),
161          self.arr[2].wrapping_mul(rhs.arr[2]),
162          self.arr[3].wrapping_mul(rhs.arr[3]),
163          self.arr[4].wrapping_mul(rhs.arr[4]),
164          self.arr[5].wrapping_mul(rhs.arr[5]),
165          self.arr[6].wrapping_mul(rhs.arr[6]),
166          self.arr[7].wrapping_mul(rhs.arr[7]),
167        ]}
168      }
169    }
170  }
171
172  #[inline]
173  fn bitand(self, rhs: Self) -> Self::Output {
174    pick! {
175      if #[cfg(target_feature="sse2")] {
176        Self { sse: bitand_m128i(self.sse, rhs.sse) }
177      } else if #[cfg(target_feature="simd128")] {
178        Self { simd: v128_and(self.simd, rhs.simd) }
179      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
180        unsafe {Self { neon: vandq_u16(self.neon, rhs.neon) }}
181      } else {
182        Self { arr: [
183          self.arr[0].bitand(rhs.arr[0]),
184          self.arr[1].bitand(rhs.arr[1]),
185          self.arr[2].bitand(rhs.arr[2]),
186          self.arr[3].bitand(rhs.arr[3]),
187          self.arr[4].bitand(rhs.arr[4]),
188          self.arr[5].bitand(rhs.arr[5]),
189          self.arr[6].bitand(rhs.arr[6]),
190          self.arr[7].bitand(rhs.arr[7]),
191        ]}
192      }
193    }
194  }
195
196  #[inline]
197  fn bitor(self, rhs: Self) -> Self::Output {
198    pick! {
199      if #[cfg(target_feature="sse2")] {
200        Self { sse: bitor_m128i(self.sse, rhs.sse) }
201      } else if #[cfg(target_feature="simd128")] {
202        Self { simd: v128_or(self.simd, rhs.simd) }
203      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
204        unsafe {Self { neon: vorrq_u16(self.neon, rhs.neon) }}
205      } else {
206        Self { arr: [
207          self.arr[0].bitor(rhs.arr[0]),
208          self.arr[1].bitor(rhs.arr[1]),
209          self.arr[2].bitor(rhs.arr[2]),
210          self.arr[3].bitor(rhs.arr[3]),
211          self.arr[4].bitor(rhs.arr[4]),
212          self.arr[5].bitor(rhs.arr[5]),
213          self.arr[6].bitor(rhs.arr[6]),
214          self.arr[7].bitor(rhs.arr[7]),
215        ]}
216      }
217    }
218  }
219
220  #[inline]
221  fn bitxor(self, rhs: Self) -> Self::Output {
222    pick! {
223      if #[cfg(target_feature="sse2")] {
224        Self { sse: bitxor_m128i(self.sse, rhs.sse) }
225      } else if #[cfg(target_feature="simd128")] {
226        Self { simd: v128_xor(self.simd, rhs.simd) }
227      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
228        unsafe {Self { neon: veorq_u16(self.neon, rhs.neon) }}
229      } else {
230        Self { arr: [
231          self.arr[0].bitxor(rhs.arr[0]),
232          self.arr[1].bitxor(rhs.arr[1]),
233          self.arr[2].bitxor(rhs.arr[2]),
234          self.arr[3].bitxor(rhs.arr[3]),
235          self.arr[4].bitxor(rhs.arr[4]),
236          self.arr[5].bitxor(rhs.arr[5]),
237          self.arr[6].bitxor(rhs.arr[6]),
238          self.arr[7].bitxor(rhs.arr[7]),
239        ]}
240      }
241    }
242  }
243
244  #[inline]
245  fn simd_eq(self, rhs: Self) -> Self::Output {
246    pick! {
247      if #[cfg(target_feature="sse2")] {
248        Self { sse: cmp_eq_mask_i16_m128i(self.sse, rhs.sse) }
249      } else if #[cfg(target_feature="simd128")] {
250        Self { simd: u16x8_eq(self.simd, rhs.simd) }
251      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
252        unsafe {Self { neon: vceqq_u16(self.neon, rhs.neon) }}
253      } else {
254        Self { arr: [
255          if self.arr[0] == rhs.arr[0] { u16::MAX } else { 0 },
256          if self.arr[1] == rhs.arr[1] { u16::MAX } else { 0 },
257          if self.arr[2] == rhs.arr[2] { u16::MAX } else { 0 },
258          if self.arr[3] == rhs.arr[3] { u16::MAX } else { 0 },
259          if self.arr[4] == rhs.arr[4] { u16::MAX } else { 0 },
260          if self.arr[5] == rhs.arr[5] { u16::MAX } else { 0 },
261          if self.arr[6] == rhs.arr[6] { u16::MAX } else { 0 },
262          if self.arr[7] == rhs.arr[7] { u16::MAX } else { 0 },
263        ]}
264      }
265    }
266  }
267
268  #[inline]
269  fn simd_ne(self, rhs: Self) -> Self::Output {
270    pick! {
271      if #[cfg(target_feature="sse2")] {
272        !self.simd_eq(rhs)
273      } else if #[cfg(target_feature="simd128")] {
274        Self { simd: u16x8_ne(self.simd, rhs.simd) }
275      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
276        !self.simd_eq(rhs)
277      } else {
278        Self { arr: [
279          if self.arr[0] != rhs.arr[0] { u16::MAX } else { 0 },
280          if self.arr[1] != rhs.arr[1] { u16::MAX } else { 0 },
281          if self.arr[2] != rhs.arr[2] { u16::MAX } else { 0 },
282          if self.arr[3] != rhs.arr[3] { u16::MAX } else { 0 },
283          if self.arr[4] != rhs.arr[4] { u16::MAX } else { 0 },
284          if self.arr[5] != rhs.arr[5] { u16::MAX } else { 0 },
285          if self.arr[6] != rhs.arr[6] { u16::MAX } else { 0 },
286          if self.arr[7] != rhs.arr[7] { u16::MAX } else { 0 },
287        ]}
288      }
289    }
290  }
291
292  #[inline]
293  fn simd_lt(self, rhs: Self) -> Self::Output {
294    // no lt, so reverse gt
295    Self::simd_gt(rhs, self)
296  }
297
298  #[inline]
299  fn simd_gt(self, rhs: Self) -> Self::Output {
300    pick! {
301      if #[cfg(target_feature = "sse2")] {
302        use safe_arch::*;
303
304        let bias = m128i::from([0x8000u16; 8]);
305
306        let a_biased = sub_i16_m128i(self.sse, bias);
307        let b_biased = sub_i16_m128i(rhs.sse, bias);
308        let mask = cmp_gt_mask_i16_m128i(a_biased, b_biased);
309
310        Self { sse: mask }
311      } else if #[cfg(target_feature="simd128")] {
312        Self { simd: u16x8_gt(self.simd, rhs.simd) }
313      } else if #[cfg(all(target_feature = "neon", target_arch = "aarch64"))] {
314        unsafe {
315          use core::arch::aarch64::*;
316          Self {
317            neon: vcgtq_u16(self.neon, rhs.neon),
318          }
319        }
320      } else {
321        Self {
322          arr: [
323            if self.arr[0] > rhs.arr[0] { u16::MAX } else { 0 },
324            if self.arr[1] > rhs.arr[1] { u16::MAX } else { 0 },
325            if self.arr[2] > rhs.arr[2] { u16::MAX } else { 0 },
326            if self.arr[3] > rhs.arr[3] { u16::MAX } else { 0 },
327            if self.arr[4] > rhs.arr[4] { u16::MAX } else { 0 },
328            if self.arr[5] > rhs.arr[5] { u16::MAX } else { 0 },
329            if self.arr[6] > rhs.arr[6] { u16::MAX } else { 0 },
330            if self.arr[7] > rhs.arr[7] { u16::MAX } else { 0 },
331          ]
332        }
333      }
334    }
335  }
336
337  #[inline]
338  fn simd_le(self, rhs: Self) -> Self::Output {
339    pick! {
340      if #[cfg(target_feature="sse2")] {
341        !self.simd_gt(rhs)
342      } else if #[cfg(target_feature="simd128")] {
343        Self { simd: u16x8_le(self.simd, rhs.simd) }
344      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
345        !self.simd_gt(rhs)
346      } else {
347        Self { arr: [
348          if self.arr[0] <= rhs.arr[0] { u16::MAX } else { 0 },
349          if self.arr[1] <= rhs.arr[1] { u16::MAX } else { 0 },
350          if self.arr[2] <= rhs.arr[2] { u16::MAX } else { 0 },
351          if self.arr[3] <= rhs.arr[3] { u16::MAX } else { 0 },
352          if self.arr[4] <= rhs.arr[4] { u16::MAX } else { 0 },
353          if self.arr[5] <= rhs.arr[5] { u16::MAX } else { 0 },
354          if self.arr[6] <= rhs.arr[6] { u16::MAX } else { 0 },
355          if self.arr[7] <= rhs.arr[7] { u16::MAX } else { 0 },
356        ]}
357      }
358    }
359  }
360
361  #[inline]
362  fn simd_ge(self, rhs: Self) -> Self::Output {
363    pick! {
364      if #[cfg(target_feature="sse2")] {
365        !self.simd_lt(rhs)
366      } else if #[cfg(target_feature="simd128")] {
367        Self { simd: u16x8_ge(self.simd, rhs.simd) }
368      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
369        !self.simd_lt(rhs)
370      } else {
371        Self { arr: [
372          if self.arr[0] >= rhs.arr[0] { u16::MAX } else { 0 },
373          if self.arr[1] >= rhs.arr[1] { u16::MAX } else { 0 },
374          if self.arr[2] >= rhs.arr[2] { u16::MAX } else { 0 },
375          if self.arr[3] >= rhs.arr[3] { u16::MAX } else { 0 },
376          if self.arr[4] >= rhs.arr[4] { u16::MAX } else { 0 },
377          if self.arr[5] >= rhs.arr[5] { u16::MAX } else { 0 },
378          if self.arr[6] >= rhs.arr[6] { u16::MAX } else { 0 },
379          if self.arr[7] >= rhs.arr[7] { u16::MAX } else { 0 },
380        ]}
381      }
382    }
383  }
384
385  #[inline]
386  pub fn reduce_add(self) -> u16 {
387    pick! {
388      if #[cfg(target_feature="sse2")] {
389        // there is a horizontal add instruction on ssse3, but apparently it is very slow on some AMD CPUs
390        let hi64 = shuffle_ai_f32_all_m128i::<0b01_00_11_10>(self.sse);
391        let sum64 = add_i16_m128i(self.sse, hi64);
392        let hi32 = shuffle_ai_f32_all_m128i::<0b11_10_00_01>(sum64);
393        let sum32 = add_i16_m128i(sum64, hi32);
394        let lo16 = shr_imm_u32_m128i::<16>(sum32);
395        let sum16 = add_i16_m128i(sum32, lo16);
396        extract_i16_as_i32_m128i::<0>(sum16) as u16
397      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
398        unsafe { vaddvq_u16(self.neon) }
399      } else {
400        let arr: [u16; 8] = cast(self);
401
402        // most boring implementation possible so optimizer doesn't overthink this
403        let mut r = arr[0];
404        r = r.wrapping_add(arr[1]);
405        r = r.wrapping_add(arr[2]);
406        r = r.wrapping_add(arr[3]);
407        r = r.wrapping_add(arr[4]);
408        r = r.wrapping_add(arr[5]);
409        r = r.wrapping_add(arr[6]);
410        r.wrapping_add(arr[7])
411      }
412    }
413  }
414
415  #[inline]
416  pub fn reduce_mul(self) -> u16 {
417    pick! {
418      if #[cfg(target_feature="sse2")] {
419        let high_64 = shuffle_ai_f32_all_m128i::<0b01_00_11_10>(self.sse);
420        let reduce_64 = mul_i16_keep_low_m128i(self.sse, high_64);
421        let high_32 = shuffle_ai_f32_all_m128i::<0b11_10_00_01>(reduce_64);
422        let reduce_32 = mul_i16_keep_low_m128i(reduce_64, high_32);
423        let high_16 = shr_imm_u32_m128i::<16>(reduce_32);
424        let reduce_16 = mul_i16_keep_low_m128i(reduce_32, high_16);
425        extract_i16_as_i32_m128i::<0>(reduce_16) as u16
426      } else if #[cfg(target_feature="simd128")] {
427        let high_64 = u64x2_shuffle::<1, 0>(self.simd, self.simd);
428        let reduce_64 = u16x8_mul(self.simd, high_64);
429        let high_32 = u32x4_shuffle::<1, 0, 0, 0>(reduce_64, reduce_64);
430        let reduce_32 = u16x8_mul(reduce_64, high_32);
431        let high_16 = u16x8_shuffle::<1, 0, 0, 0, 0, 0, 0, 0>(reduce_32, reduce_32);
432        let reduce_16 = u16x8_mul(reduce_32, high_16);
433        u16x8_extract_lane::<0>(reduce_16)
434      } else if #[cfg(all(target_feature="neon", target_arch="aarch64"))] {
435        unsafe {
436          let high_64 = vextq_u16::<4>(self.neon, self.neon);
437          let reduce_64 = vmulq_u16(self.neon, high_64);
438          let high_32 = vrev64q_u16(reduce_64);
439          let reduce_32 = vmulq_u16(reduce_64, high_32);
440          let high_16 = vrev32q_u16(reduce_32);
441          let reduce_16 = vmulq_u16(reduce_32, high_16);
442          vgetq_lane_u16::<0>(reduce_16)
443        }
444      } else {
445        let array = self.to_array();
446
447        // most boring implementation possible so optimizer doesn't overthink this
448        let mut result = array[0];
449        result = result.wrapping_mul(array[1]);
450        result = result.wrapping_mul(array[2]);
451        result = result.wrapping_mul(array[3]);
452        result = result.wrapping_mul(array[4]);
453        result = result.wrapping_mul(array[5]);
454        result = result.wrapping_mul(array[6]);
455        result.wrapping_mul(array[7])
456      }
457    }
458  }
459
460  #[inline]
461  pub fn bitselect(self, if_one: Self, if_zero: Self) -> Self {
462    pick! {
463      if #[cfg(target_feature="sse2")] {
464        Self {
465          sse: bitor_m128i(
466            bitand_m128i(if_one.sse, self.sse),
467            bitandnot_m128i(self.sse, if_zero.sse),
468          ),
469        }
470      } else if #[cfg(target_feature="simd128")] {
471        Self { simd: v128_bitselect(if_one.simd, if_zero.simd, self.simd) }
472      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
473        unsafe {Self { neon: vbslq_u16(self.neon, if_one.neon, if_zero.neon) }}
474      } else {
475        generic_bit_blend(self, if_one, if_zero)
476      }
477    }
478  }
479
480  #[inline]
481  fn select(self, if_true: Self, if_false: Self) -> Self {
482    pick! {
483      if #[cfg(target_feature="sse4.1")] {
484        Self { sse: blend_varying_i8_m128i(if_false.sse, if_true.sse, self.sse) }
485      } else if #[cfg(target_feature="simd128")] {
486        Self { simd: v128_bitselect(if_true.simd, if_false.simd, self.simd) }
487      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
488        unsafe {Self { neon: vbslq_u16(self.neon, if_true.neon, if_false.neon) }}
489      } else {
490        generic_bit_blend(self, if_true, if_false)
491      }
492    }
493  }
494
495  #[inline]
496  pub fn to_bitmask(self) -> u32 {
497    pick! {
498      if #[cfg(target_feature="sse2")] {
499        (move_mask_i8_m128i( pack_i16_to_i8_m128i(self.sse,self.sse)) as u32) & 0xff
500      } else if #[cfg(target_feature="simd128")] {
501        u16x8_bitmask(self.simd) as u32
502      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
503        unsafe
504        {
505          // set all to 1 if top bit is set, else 0
506          let masked = vcltzq_s16(self.cast_signed().neon);
507
508          // select the right bit out of each lane
509          let selectbit : uint16x8_t = core::mem::transmute([1u16, 2, 4, 8, 16, 32, 64, 128]);
510          let r = vandq_u16(masked, selectbit);
511
512          // horizontally add the 16-bit lanes
513          vaddvq_u16(r) as u32
514         }
515       } else {
516        ((self.arr[0].cast_signed() < 0) as u32) |
517        ((self.arr[1].cast_signed() < 0) as u32) << 1 |
518        ((self.arr[2].cast_signed() < 0) as u32) << 2 |
519        ((self.arr[3].cast_signed() < 0) as u32) << 3 |
520        ((self.arr[4].cast_signed() < 0) as u32) << 4 |
521        ((self.arr[5].cast_signed() < 0) as u32) << 5 |
522        ((self.arr[6].cast_signed() < 0) as u32) << 6 |
523        ((self.arr[7].cast_signed() < 0) as u32) << 7
524      }
525    }
526  }
527
528  #[inline]
529  pub fn any(self) -> bool {
530    pick! {
531      if #[cfg(target_feature="sse2")] {
532        (move_mask_i8_m128i(self.sse) & 0b1010101010101010) != 0
533      } else if #[cfg(target_feature="simd128")] {
534        u16x8_bitmask(self.simd) != 0
535      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
536        unsafe {
537          vminvq_s16(self.cast_signed().neon) < 0
538        }
539      } else {
540        let v : [u64;2] = cast(self);
541        ((v[0] | v[1]) & 0x8000800080008000) != 0
542      }
543    }
544  }
545
546  #[inline]
547  pub fn all(self) -> bool {
548    pick! {
549      if #[cfg(target_feature="sse2")] {
550        (move_mask_i8_m128i(self.sse) & 0b1010101010101010) == 0b1010101010101010
551      } else if #[cfg(target_feature="simd128")] {
552        u16x8_bitmask(self.simd) == 0b11111111
553      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
554        unsafe {
555          vmaxvq_s16(self.cast_signed().neon) < 0
556        }
557      } else {
558        let v : [u64;2] = cast(self);
559        (v[0] & v[1] & 0x8000800080008000) == 0x8000800080008000
560      }
561    }
562  }
563
564  #[inline]
565  pub fn shuffle(self, indices: u16x8) -> Self {
566    pick! {
567      if #[cfg(all(target_feature = "avx512bw", target_feature = "avx512vl"))] {
568        #[cfg(target_arch = "x86")]
569        use core::arch::x86::_mm_permutexvar_epi16;
570        #[cfg(target_arch = "x86_64")]
571        use core::arch::x86_64::_mm_permutexvar_epi16;
572        // TODO(safe_arch): Add `_mm_permutexvar_epi16`
573        Self { sse: unsafe { m128i(_mm_permutexvar_epi16(indices.sse.0, self.sse.0)) } }
574      } else if #[cfg(any(
575        target_feature = "ssse3",
576        all(target_arch = "aarch64", target_feature = "neon"),
577        target_feature = "simd128",
578      ))] {
579        let self_bytes = cast::<u16x8, u8x16>(self);
580        let byte_indices = indices.to_byte_indices();
581
582        cast::<u8x16, u16x8>(self_bytes.shuffle(byte_indices))
583      } else {
584        let self_array = self.to_array();
585        let indices_array = indices.to_array();
586
587        let mut result = [0; 8];
588        for i in 0..8 {
589          let index = indices_array[i] as usize;
590          if index < 8 {
591            result[i] = self_array[index];
592          }
593        }
594
595        Self::new(result)
596      }
597    }
598  }
599
600  #[inline]
601  pub fn shuffle_zeroing(self, indices: u16x8) -> Self {
602    pick! {
603      if #[cfg(any(
604        target_feature = "ssse3",
605        all(target_arch = "aarch64", target_feature = "neon"),
606        target_feature = "simd128",
607      ))] {
608        // Even if the `u8x16::shuffle` implementation is zeroing, our 16-bit to
609        // 8-bit can trigger an overflow causing incorrect behavior
610        self.shuffle(indices) & indices.simd_lt(8)
611      } else {
612        // The fallback branch of `shuffle` already has the behavior we want
613        self.shuffle(indices)
614      }
615    }
616  }
617
618  #[inline]
619  pub fn shuffle_wrapping(self, indices: u16x8) -> Self {
620    pick! {
621      if #[cfg(all(target_feature = "avx512bw", target_feature = "avx512vl"))] {
622        // `avx512` shuffle intrinsics are wrapping
623        self.shuffle(indices)
624      } else {
625        self.shuffle(indices & 7)
626      }
627    }
628  }
629
630  #[inline]
631  fn shuffle(self: [u16x8; 2], indices: u16x8) -> u16x8 {
632    pick! {
633      if #[cfg(all(target_feature = "avx512bw", target_feature = "avx512vl"))] {
634        #[cfg(target_arch = "x86")]
635        use core::arch::x86::_mm_permutex2var_epi16;
636        #[cfg(target_arch = "x86_64")]
637        use core::arch::x86_64::_mm_permutex2var_epi16;
638        // TODO(safe_arch): add `_mm_permutex2var_epi16`.
639        u16x8 {
640          sse: unsafe {
641            m128i(_mm_permutex2var_epi16(self[0].sse.0, indices.sse.0, self[1].sse.0))
642          },
643        }
644      } else {
645        let self_bytes = cast::<[u16x8; 2], [u8x16; 2]>(self);
646        let byte_indices = indices.to_byte_indices();
647
648        cast::<u8x16, u16x8>(self_bytes.shuffle(byte_indices))
649      }
650    }
651  }
652
653  #[inline]
654  fn shuffle_zeroing(self: [u16x8; 2], indices: u16x8) -> u16x8 {
655    // Even if the `u8x16` shuffle is zeroing, our 16-bit to 8-bit index
656    // conversion breaks for out of bounds indices.
657    self.shuffle(indices) & indices.simd_lt(16)
658  }
659
660  #[inline]
661  fn shuffle_wrapping(self: [u16x8; 2], indices: u16x8) -> u16x8 {
662    pick! {
663      if #[cfg(all(target_feature = "avx512bw", target_feature = "avx512vl"))] {
664        // `avx512` shuffle intrinsics are wrapping
665        self.shuffle(indices)
666      } else {
667        self.shuffle(indices & 15)
668      }
669    }
670  }
671
672  #[inline]
673  fn shuffle(self: [u16x8; 3], indices: u16x8) -> u16x8 {
674    let self_bytes = cast::<[u16x8; 3], [u8x16; 3]>(self);
675    let byte_indices = indices.to_byte_indices();
676
677    cast::<u8x16, u16x8>(self_bytes.shuffle(byte_indices))
678  }
679
680  #[inline]
681  fn shuffle_zeroing(self: [u16x8; 3], indices: u16x8) -> u16x8 {
682    // Even if the `u8x16` shuffle is zeroing, our 16-bit to 8-bit index
683    // conversion breaks for out of bounds indices.
684    self.shuffle(indices) & indices.simd_lt(24)
685  }
686
687  #[inline]
688  fn shuffle_wrapping(self: [u16x8; 3], indices: u16x8) -> u16x8 {
689    self.shuffle(indices % 24)
690  }
691
692  #[inline]
693  fn shuffle(self: [u16x8; 4], indices: u16x8) -> u16x8 {
694    let self_bytes = cast::<[u16x8; 4], [u8x16; 4]>(self);
695    let byte_indices = indices.to_byte_indices();
696
697    cast::<u8x16, u16x8>(self_bytes.shuffle(byte_indices))
698  }
699
700  #[inline]
701  fn shuffle_zeroing(self: [u16x8; 4], indices: u16x8) -> u16x8 {
702    // Even if the `u8x16` shuffle is zeroing, our 16-bit to 8-bit index
703    // conversion breaks for out of bounds indices.
704    self.shuffle(indices) & indices.simd_lt(32)
705  }
706
707  #[inline]
708  fn shuffle_wrapping(self: [u16x8; 4], indices: u16x8) -> u16x8 {
709    self.shuffle(indices & 31)
710  }
711
712  ///
713  /// This function is accelerated on multiple target architectures.
714  #[inline]
715  pub fn transpose(data: [Self; 8]) -> [Self; 8] {
716    pick! {
717      if #[cfg(target_feature="sse2")] {
718        let a1 = unpack_low_i16_m128i(data[0].sse, data[1].sse);
719        let a2 = unpack_high_i16_m128i(data[0].sse, data[1].sse);
720        let a3 = unpack_low_i16_m128i(data[2].sse, data[3].sse);
721        let a4 = unpack_high_i16_m128i(data[2].sse, data[3].sse);
722        let a5 = unpack_low_i16_m128i(data[4].sse, data[5].sse);
723        let a6 = unpack_high_i16_m128i(data[4].sse, data[5].sse);
724        let a7 = unpack_low_i16_m128i(data[6].sse, data[7].sse);
725        let a8 = unpack_high_i16_m128i(data[6].sse, data[7].sse);
726
727        let b1 = unpack_low_i32_m128i(a1, a3);
728        let b2 = unpack_high_i32_m128i(a1, a3);
729        let b3 = unpack_low_i32_m128i(a2, a4);
730        let b4 = unpack_high_i32_m128i(a2, a4);
731        let b5 = unpack_low_i32_m128i(a5, a7);
732        let b6 = unpack_high_i32_m128i(a5, a7);
733        let b7 = unpack_low_i32_m128i(a6, a8);
734        let b8 = unpack_high_i32_m128i(a6, a8);
735
736        [
737          Self { sse: unpack_low_i64_m128i(b1, b5) },
738          Self { sse: unpack_high_i64_m128i(b1, b5) },
739          Self { sse: unpack_low_i64_m128i(b2, b6) },
740          Self { sse: unpack_high_i64_m128i(b2, b6) },
741          Self { sse: unpack_low_i64_m128i(b3, b7) },
742          Self { sse: unpack_high_i64_m128i(b3, b7) },
743          Self { sse: unpack_low_i64_m128i(b4, b8) },
744          Self { sse: unpack_high_i64_m128i(b4, b8) } ,
745        ]
746     } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
747
748          #[inline] fn vtrq32(a : uint16x8_t, b : uint16x8_t) -> (uint16x8_t, uint16x8_t)
749          {
750              unsafe {
751                let r = vtrnq_u32(vreinterpretq_u32_u16(a),vreinterpretq_u32_u16(b));
752                (vreinterpretq_u16_u32(r.0), vreinterpretq_u16_u32(r.1))
753              }
754          }
755
756        unsafe {
757          let (q0,q2) = vtrq32(data[0].neon, data[2].neon);
758          let (q1,q3) = vtrq32(data[1].neon, data[3].neon);
759          let (q4,q6) = vtrq32(data[4].neon, data[6].neon);
760          let (q5,q7) = vtrq32(data[5].neon, data[7].neon);
761
762          let b1 = vtrnq_u16(q0, q1);
763          let b2 = vtrnq_u16(q2, q3);
764          let b3 = vtrnq_u16(q4, q5);
765          let b4 = vtrnq_u16(q6, q7);
766
767          // There is no vtrnq_u64 unfortunately, so there's this mess
768          // which does a somewhat reasonable job, but not as good as the
769          // assembly versions which just swap the 64 bit register aliases.
770          [
771            Self { neon: vcombine_u16(vget_low_u16(b1.0), vget_low_u16(b3.0)) },
772            Self { neon: vcombine_u16(vget_low_u16(b1.1), vget_low_u16(b3.1)) },
773            Self { neon: vcombine_u16(vget_low_u16(b2.0), vget_low_u16(b4.0)) },
774            Self { neon: vcombine_u16(vget_low_u16(b2.1), vget_low_u16(b4.1)) },
775            Self { neon: vcombine_u16(vget_high_u16(b1.0), vget_high_u16(b3.0)) },
776            Self { neon: vcombine_u16(vget_high_u16(b1.1), vget_high_u16(b3.1)) },
777            Self { neon: vcombine_u16(vget_high_u16(b2.0), vget_high_u16(b4.0)) },
778            Self { neon: vcombine_u16(vget_high_u16(b2.1), vget_high_u16(b4.1)) },
779          ]
780        }
781      } else if #[cfg(target_feature="simd128")] {
782        #[inline] fn lo_i16(a : v128, b : v128) -> v128 { u16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(a,b) }
783        #[inline] fn hi_i16(a : v128, b : v128) -> v128 { u16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(a,b) }
784        #[inline] fn lo_i32(a : v128, b : v128) -> v128 { u32x4_shuffle::<0, 4, 1, 5>(a,b) }
785        #[inline] fn hi_i32(a : v128, b : v128) -> v128 { u32x4_shuffle::<2, 6, 3, 7>(a,b) }
786        #[inline] fn lo_i64(a : v128, b : v128) -> v128 { u64x2_shuffle::<0, 2>(a,b) }
787        #[inline] fn hi_i64(a : v128, b : v128) -> v128 { u64x2_shuffle::<1, 3>(a,b) }
788
789        let a1 = lo_i16(data[0].simd, data[1].simd);
790        let a2 = hi_i16(data[0].simd, data[1].simd);
791        let a3 = lo_i16(data[2].simd, data[3].simd);
792        let a4 = hi_i16(data[2].simd, data[3].simd);
793        let a5 = lo_i16(data[4].simd, data[5].simd);
794        let a6 = hi_i16(data[4].simd, data[5].simd);
795        let a7 = lo_i16(data[6].simd, data[7].simd);
796        let a8 = hi_i16(data[6].simd, data[7].simd);
797
798        let b1 = lo_i32(a1, a3);
799        let b2 = hi_i32(a1, a3);
800        let b3 = lo_i32(a2, a4);
801        let b4 = hi_i32(a2, a4);
802        let b5 = lo_i32(a5, a7);
803        let b6 = hi_i32(a5, a7);
804        let b7 = lo_i32(a6, a8);
805        let b8 = hi_i32(a6, a8);
806
807        [
808          Self { simd: lo_i64(b1, b5) },
809          Self { simd: hi_i64(b1, b5) },
810          Self { simd: lo_i64(b2, b6) },
811          Self { simd: hi_i64(b2, b6) },
812          Self { simd: lo_i64(b3, b7) },
813          Self { simd: hi_i64(b3, b7) },
814          Self { simd: lo_i64(b4, b8) },
815          Self { simd: hi_i64(b4, b8) } ,
816        ]
817
818      } else {
819        #[inline(always)]
820        fn transpose_column(data: &[u16x8; 8], index: usize) -> u16x8 {
821          u16x8::new([
822            data[0].as_array()[index],
823            data[1].as_array()[index],
824            data[2].as_array()[index],
825            data[3].as_array()[index],
826            data[4].as_array()[index],
827            data[5].as_array()[index],
828            data[6].as_array()[index],
829            data[7].as_array()[index],
830          ])
831        }
832
833        [
834          transpose_column(&data, 0),
835          transpose_column(&data, 1),
836          transpose_column(&data, 2),
837          transpose_column(&data, 3),
838          transpose_column(&data, 4),
839          transpose_column(&data, 5),
840          transpose_column(&data, 6),
841          transpose_column(&data, 7),
842        ]
843      }
844    }
845  }
846
847  #[inline]
848  fn shl(self, rhs: Self) -> Self::Output {
849    pick! {
850      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
851        #[cfg(target_arch = "x86")]
852        use core::arch::x86::_mm_sllv_epi16;
853        #[cfg(target_arch = "x86_64")]
854        use core::arch::x86_64::_mm_sllv_epi16;
855
856        // Mask `rhs` to 15 to match `wrapping_shl`.
857        let rhs = bitand_m128i(rhs.sse, set_splat_i16_m128i(15));
858        // TODO(safe_arch): Add `_mm_sllv_epi16`.
859        cast(unsafe { _mm_sllv_epi16(self.sse.0, rhs.0) })
860      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
861        unsafe {
862          // Mask `rhs` to 15 to match `wrapping_shl`.
863          let rhs = vreinterpretq_s16_u16(vandq_u16(rhs.neon, vmovq_n_u16(15)));
864          Self { neon: vshlq_u16(self.neon, rhs) }
865        }
866      } else {
867        let self_array = self.to_array();
868        let rhs_array = rhs.to_array();
869
870        Self::new([
871          self_array[0].wrapping_shl(rhs_array[0] as u32),
872          self_array[1].wrapping_shl(rhs_array[1] as u32),
873          self_array[2].wrapping_shl(rhs_array[2] as u32),
874          self_array[3].wrapping_shl(rhs_array[3] as u32),
875          self_array[4].wrapping_shl(rhs_array[4] as u32),
876          self_array[5].wrapping_shl(rhs_array[5] as u32),
877          self_array[6].wrapping_shl(rhs_array[6] as u32),
878          self_array[7].wrapping_shl(rhs_array[7] as u32),
879        ])
880      }
881    }
882  }
883
884  #[inline]
885  fn shl(self, rhs: u32) -> Self::Output {
886    pick! {
887      if #[cfg(target_feature="sse2")] {
888        // Use `rhs % 16` to perform wrapping shift and not unbounded shift.
889        #[expect(clippy::suspicious_arithmetic_impl)]
890        let shift = cast([rhs as u64 & 15, 0]);
891        Self { sse: shl_all_u16_m128i(self.sse, shift) }
892      } else if #[cfg(target_feature="simd128")] {
893        Self { simd: u16x8_shl(self.simd, rhs) }
894      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
895        // Use `rhs % 16` to perform wrapping shift and not unbounded shift.
896        #[expect(clippy::suspicious_arithmetic_impl)]
897        unsafe {Self { neon: vshlq_u16(self.neon, vmovq_n_s16(rhs as i16 & 15)) }}
898      } else {
899        Self { arr: [
900          self.arr[0].wrapping_shl(rhs),
901          self.arr[1].wrapping_shl(rhs),
902          self.arr[2].wrapping_shl(rhs),
903          self.arr[3].wrapping_shl(rhs),
904          self.arr[4].wrapping_shl(rhs),
905          self.arr[5].wrapping_shl(rhs),
906          self.arr[6].wrapping_shl(rhs),
907          self.arr[7].wrapping_shl(rhs),
908        ]}
909      }
910    }
911  }
912
913  #[inline]
914  fn shr(self, rhs: Self) -> Self::Output {
915    pick! {
916      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
917        #[cfg(target_arch = "x86")]
918        use core::arch::x86::_mm_srlv_epi16;
919        #[cfg(target_arch = "x86_64")]
920        use core::arch::x86_64::_mm_srlv_epi16;
921
922        // Mask `rhs` to 15 to match `wrapping_shr`.
923        let rhs = bitand_m128i(rhs.sse, set_splat_i16_m128i(15));
924        // TODO(safe_arch): Add `_mm_srlv_epi16`.
925        cast(unsafe { _mm_srlv_epi16(self.sse.0, rhs.0) })
926      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
927        unsafe {
928          // Mask `rhs` to 15 to match `wrapping_shr`, and negate it because
929          // there is no shift-right intrinsic.
930          let neg_rhs = vnegq_s16(vreinterpretq_s16_u16(vandq_u16(rhs.neon, vmovq_n_u16(15))));
931          Self { neon: vshlq_u16(self.neon, neg_rhs) }
932        }
933      } else {
934        let self_array = self.to_array();
935        let rhs_array = rhs.to_array();
936
937        Self::new([
938          self_array[0].wrapping_shr(rhs_array[0] as u32),
939          self_array[1].wrapping_shr(rhs_array[1] as u32),
940          self_array[2].wrapping_shr(rhs_array[2] as u32),
941          self_array[3].wrapping_shr(rhs_array[3] as u32),
942          self_array[4].wrapping_shr(rhs_array[4] as u32),
943          self_array[5].wrapping_shr(rhs_array[5] as u32),
944          self_array[6].wrapping_shr(rhs_array[6] as u32),
945          self_array[7].wrapping_shr(rhs_array[7] as u32),
946        ])
947      }
948    }
949  }
950
951  #[inline]
952  fn shr(self, rhs: u32) -> Self::Output {
953    pick! {
954      if #[cfg(target_feature="sse2")] {
955        // Use `rhs % 16` to perform wrapping shift and not unbounded shift.
956        #[expect(clippy::suspicious_arithmetic_impl)]
957        let shift = cast([rhs as u64 & 15, 0]);
958        Self { sse: shr_all_u16_m128i(self.sse, shift) }
959      } else if #[cfg(target_feature="simd128")] {
960        Self { simd: u16x8_shr(self.simd, rhs) }
961      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
962        // Use `rhs % 16` to perform wrapping shift and not unbounded shift.
963        #[expect(clippy::suspicious_arithmetic_impl)]
964        unsafe {Self { neon: vshlq_u16(self.neon, vmovq_n_s16( -(rhs as i16 & 15))) }}
965      } else {
966        Self { arr: [
967          self.arr[0].wrapping_shr(rhs),
968          self.arr[1].wrapping_shr(rhs),
969          self.arr[2].wrapping_shr(rhs),
970          self.arr[3].wrapping_shr(rhs),
971          self.arr[4].wrapping_shr(rhs),
972          self.arr[5].wrapping_shr(rhs),
973          self.arr[6].wrapping_shr(rhs),
974          self.arr[7].wrapping_shr(rhs),
975        ]}
976      }
977    }
978  }
979
980  #[inline]
981  pub fn max(self, rhs: Self) -> Self {
982    pick! {
983      if #[cfg(target_feature="sse4.1")] {
984        Self { sse: max_u16_m128i(self.sse, rhs.sse) }
985      } else if #[cfg(target_feature="simd128")] {
986        Self { simd: u16x8_max(self.simd, rhs.simd) }
987      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
988        unsafe {Self { neon: vmaxq_u16(self.neon, rhs.neon) }}
989      } else {
990        let arr: [u16; 8] = cast(self);
991        let rhs: [u16; 8] = cast(rhs);
992        cast([
993          arr[0].max(rhs[0]),
994          arr[1].max(rhs[1]),
995          arr[2].max(rhs[2]),
996          arr[3].max(rhs[3]),
997          arr[4].max(rhs[4]),
998          arr[5].max(rhs[5]),
999          arr[6].max(rhs[6]),
1000          arr[7].max(rhs[7]),
1001        ])
1002      }
1003    }
1004  }
1005
1006  #[inline]
1007  pub fn min(self, rhs: Self) -> Self {
1008    pick! {
1009      if #[cfg(target_feature="sse4.1")] {
1010        Self { sse: min_u16_m128i(self.sse, rhs.sse) }
1011      } else if #[cfg(target_feature="simd128")] {
1012        Self { simd: u16x8_min(self.simd, rhs.simd) }
1013      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1014        unsafe {Self { neon: vminq_u16(self.neon, rhs.neon) }}
1015      } else {
1016        let arr: [u16; 8] = cast(self);
1017        let rhs: [u16; 8] = cast(rhs);
1018        cast([
1019          arr[0].min(rhs[0]),
1020          arr[1].min(rhs[1]),
1021          arr[2].min(rhs[2]),
1022          arr[3].min(rhs[3]),
1023          arr[4].min(rhs[4]),
1024          arr[5].min(rhs[5]),
1025          arr[6].min(rhs[6]),
1026          arr[7].min(rhs[7]),
1027        ])
1028      }
1029    }
1030  }
1031
1032  #[inline]
1033  pub fn reduce_max(self) -> u16 {
1034    pick! {
1035      if #[cfg(all(target_feature="ssse3", target_feature="sse4.1"))] {
1036        let hi64 = shuffle_ai_f32_all_m128i::<0b01_00_11_10>(self.sse);
1037        let sum64 = max_u16_m128i(self.sse, hi64);
1038        let hi32 = shuffle_ai_f32_all_m128i::<0b11_10_00_01>(sum64);
1039        let sum32 = max_u16_m128i(sum64, hi32);
1040        let lo16 = shr_imm_u32_m128i::<16>(sum32);
1041        let sum16 = max_u16_m128i(sum32, lo16);
1042        extract_i16_as_i32_m128i::<0>(sum16) as u16
1043      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1044        unsafe { vmaxvq_u16(self.neon) }
1045      } else {
1046        let arr: [u16; 8] = cast(self);
1047
1048        // most boring implementation possible so optimizer doesn't overthink this
1049        let mut r = arr[0];
1050        r = r.max(arr[1]);
1051        r = r.max(arr[2]);
1052        r = r.max(arr[3]);
1053        r = r.max(arr[4]);
1054        r = r.max(arr[5]);
1055        r = r.max(arr[6]);
1056        r.max(arr[7])
1057      }
1058    }
1059  }
1060
1061  #[inline]
1062  pub fn reduce_min(self) -> u16 {
1063    pick! {
1064      if #[cfg(all(target_feature="ssse3", target_feature="sse4.1"))] {
1065        let hi64 = shuffle_ai_f32_all_m128i::<0b01_00_11_10>(self.sse);
1066        let sum64 = min_u16_m128i(self.sse, hi64);
1067        let hi32 = shuffle_ai_f32_all_m128i::<0b11_10_00_01>(sum64);
1068        let sum32 = min_u16_m128i(sum64, hi32);
1069        let lo16 = shr_imm_u32_m128i::<16>(sum32);
1070        let sum16 = min_u16_m128i(sum32, lo16);
1071        extract_i16_as_i32_m128i::<0>(sum16) as u16
1072      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1073        unsafe { vminvq_u16(self.neon) }
1074      } else {
1075        let arr: [u16; 8] = cast(self);
1076
1077        // most boring implementation possible so optimizer doesn't overthink this
1078        let mut r = arr[0];
1079        r = r.min(arr[1]);
1080        r = r.min(arr[2]);
1081        r = r.min(arr[3]);
1082        r = r.min(arr[4]);
1083        r = r.min(arr[5]);
1084        r = r.min(arr[6]);
1085        r.min(arr[7])
1086      }
1087    }
1088  }
1089
1090  #[inline]
1091  pub fn unbounded_shl(self, rhs: Self) -> Self {
1092    pick! {
1093      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
1094        #[cfg(target_arch = "x86")]
1095        use core::arch::x86::_mm_sllv_epi16;
1096        #[cfg(target_arch = "x86_64")]
1097        use core::arch::x86_64::_mm_sllv_epi16;
1098
1099        // TODO(safe_arch): Add `_mm_sllv_epi16`.
1100        cast(unsafe { _mm_sllv_epi16(self.sse.0, rhs.sse.0) })
1101      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
1102        unsafe {
1103          // The intrinsic has different semantics so we need to mask ourselves.
1104          Self { neon: vshlq_u16(self.neon, vreinterpretq_s16_u16(rhs.neon)) } & rhs.simd_lt(16)
1105        }
1106      } else {
1107        let self_array = self.to_array();
1108        let rhs_array = rhs.to_array();
1109
1110        Self::new([
1111          self_array[0].unbounded_shl(rhs_array[0] as u32),
1112          self_array[1].unbounded_shl(rhs_array[1] as u32),
1113          self_array[2].unbounded_shl(rhs_array[2] as u32),
1114          self_array[3].unbounded_shl(rhs_array[3] as u32),
1115          self_array[4].unbounded_shl(rhs_array[4] as u32),
1116          self_array[5].unbounded_shl(rhs_array[5] as u32),
1117          self_array[6].unbounded_shl(rhs_array[6] as u32),
1118          self_array[7].unbounded_shl(rhs_array[7] as u32),
1119        ])
1120      }
1121    }
1122  }
1123
1124  #[inline]
1125  pub fn unbounded_shl_scalar(self, rhs: u32) -> Self {
1126    pick! {
1127      if #[cfg(target_feature="sse2")] {
1128        Self { sse: shl_all_u16_m128i(self.sse, cast([rhs as u64, 0])) }
1129      } else if #[cfg(target_feature="simd128")] {
1130        // The intrinsic performs wrapping shift so we need to mask the result.
1131        if rhs >= 16 { Self::ZERO } else { Self { simd: u16x8_shl(self.simd, rhs) } }
1132      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1133        // The intrinsic has different semantics so we need to saturate `rhs`.
1134        unsafe { Self { neon: vshlq_u16(self.neon, vmovq_n_s16(rhs.min(16) as i16)) } }
1135      } else {
1136        Self { arr: [
1137          self.arr[0].unbounded_shl(rhs),
1138          self.arr[1].unbounded_shl(rhs),
1139          self.arr[2].unbounded_shl(rhs),
1140          self.arr[3].unbounded_shl(rhs),
1141          self.arr[4].unbounded_shl(rhs),
1142          self.arr[5].unbounded_shl(rhs),
1143          self.arr[6].unbounded_shl(rhs),
1144          self.arr[7].unbounded_shl(rhs),
1145        ]}
1146      }
1147    }
1148  }
1149
1150  #[inline]
1151  pub fn unbounded_shr(self, rhs: Self) -> Self {
1152    pick! {
1153      if #[cfg(all(target_feature="avx512bw", target_feature="avx512vl"))] {
1154        #[cfg(target_arch = "x86")]
1155        use core::arch::x86::_mm_srlv_epi16;
1156        #[cfg(target_arch = "x86_64")]
1157        use core::arch::x86_64::_mm_srlv_epi16;
1158
1159        // TODO(safe_arch): Add `_mm_srlv_epi16`.
1160        cast(unsafe { _mm_srlv_epi16(self.sse.0, rhs.sse.0) })
1161      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
1162        unsafe {
1163          // Negate `rhs` because there is no direct shift-right intrinsic, and
1164          // mask to hide `rhs` overflow.
1165          Self { neon: vshlq_u16(self.neon, vnegq_s16(vreinterpretq_s16_u16(rhs.neon))) } & rhs.simd_lt(16)
1166        }
1167      } else {
1168        let self_array = self.to_array();
1169        let rhs_array = rhs.to_array();
1170
1171        Self::new([
1172          self_array[0].unbounded_shr(rhs_array[0] as u32),
1173          self_array[1].unbounded_shr(rhs_array[1] as u32),
1174          self_array[2].unbounded_shr(rhs_array[2] as u32),
1175          self_array[3].unbounded_shr(rhs_array[3] as u32),
1176          self_array[4].unbounded_shr(rhs_array[4] as u32),
1177          self_array[5].unbounded_shr(rhs_array[5] as u32),
1178          self_array[6].unbounded_shr(rhs_array[6] as u32),
1179          self_array[7].unbounded_shr(rhs_array[7] as u32),
1180        ])
1181      }
1182    }
1183  }
1184
1185  #[inline]
1186  pub fn unbounded_shr_scalar(self, rhs: u32) -> Self {
1187    pick! {
1188      if #[cfg(target_feature="sse2")] {
1189        Self { sse: shr_all_u16_m128i(self.sse, cast([rhs as u64, 0])) }
1190      } else if #[cfg(target_feature="simd128")] {
1191        if rhs < 16 { Self { simd: u16x8_shr(self.simd, rhs) } } else { Self::ZERO }
1192      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1193        unsafe {
1194          // Negate `rhs` because there is no direct shift-right intrinsic, and
1195          // restrict it to prevent overflow.
1196          Self { neon: vshlq_u16(self.neon, vmovq_n_s16(-rhs.min(16).cast_signed() as i16)) }
1197        }
1198      } else {
1199        Self {
1200          arr: [
1201            self.arr[0].unbounded_shr(rhs),
1202            self.arr[1].unbounded_shr(rhs),
1203            self.arr[2].unbounded_shr(rhs),
1204            self.arr[3].unbounded_shr(rhs),
1205            self.arr[4].unbounded_shr(rhs),
1206            self.arr[5].unbounded_shr(rhs),
1207            self.arr[6].unbounded_shr(rhs),
1208            self.arr[7].unbounded_shr(rhs),
1209          ],
1210        }
1211      }
1212    }
1213  }
1214
1215  #[inline]
1216  pub fn saturating_add(self, rhs: Self) -> Self {
1217    pick! {
1218      if #[cfg(target_feature="sse2")] {
1219        Self { sse: add_saturating_u16_m128i(self.sse, rhs.sse) }
1220      } else if #[cfg(target_feature="simd128")] {
1221        Self { simd: u16x8_add_sat(self.simd, rhs.simd) }
1222      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1223        unsafe {Self { neon: vqaddq_u16(self.neon, rhs.neon) }}
1224      } else {
1225        Self { arr: [
1226          self.arr[0].saturating_add(rhs.arr[0]),
1227          self.arr[1].saturating_add(rhs.arr[1]),
1228          self.arr[2].saturating_add(rhs.arr[2]),
1229          self.arr[3].saturating_add(rhs.arr[3]),
1230          self.arr[4].saturating_add(rhs.arr[4]),
1231          self.arr[5].saturating_add(rhs.arr[5]),
1232          self.arr[6].saturating_add(rhs.arr[6]),
1233          self.arr[7].saturating_add(rhs.arr[7]),
1234        ]}
1235      }
1236    }
1237  }
1238
1239  #[inline]
1240  pub fn saturating_sub(self, rhs: Self) -> Self {
1241    pick! {
1242      if #[cfg(target_feature="sse2")] {
1243        Self { sse: sub_saturating_u16_m128i(self.sse, rhs.sse) }
1244      } else if #[cfg(target_feature="simd128")] {
1245        Self { simd: u16x8_sub_sat(self.simd, rhs.simd) }
1246      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))]{
1247        unsafe {Self { neon: vqsubq_u16(self.neon, rhs.neon) }}
1248      } else {
1249        Self { arr: [
1250          self.arr[0].saturating_sub(rhs.arr[0]),
1251          self.arr[1].saturating_sub(rhs.arr[1]),
1252          self.arr[2].saturating_sub(rhs.arr[2]),
1253          self.arr[3].saturating_sub(rhs.arr[3]),
1254          self.arr[4].saturating_sub(rhs.arr[4]),
1255          self.arr[5].saturating_sub(rhs.arr[5]),
1256          self.arr[6].saturating_sub(rhs.arr[6]),
1257          self.arr[7].saturating_sub(rhs.arr[7]),
1258        ]}
1259      }
1260    }
1261  }
1262
1263  #[inline]
1264  pub fn overflowing_mul(self, rhs: Self) -> (Self, Self) {
1265    let (low, high) = self.mul_keep_low_high(rhs);
1266    let overflow = high.simd_ne(Self::ZERO);
1267    (low, overflow)
1268  }
1269
1270  optional_fn_widening_mul {
1271    #[inline]
1272    pub fn widening_mul(self, rhs: Self) -> u32x8 {
1273      pick! {
1274        if #[cfg(target_feature="avx2")] {
1275          let a = convert_to_i32_m256i_from_u16_m128i(self.sse);
1276          let b = convert_to_i32_m256i_from_u16_m128i(rhs.sse);
1277          u32x8 { avx2: mul_i32_keep_low_m256i(a,b) }
1278        } else if #[cfg(target_feature="sse2")] {
1279          let low = mul_i16_keep_low_m128i(self.sse, rhs.sse);
1280          let high = mul_u16_keep_high_m128i(self.sse, rhs.sse);
1281          u32x8 {
1282            a: u32x4 { sse:unpack_low_i16_m128i(low, high) },
1283            b: u32x4 { sse:unpack_high_i16_m128i(low, high) }
1284          }
1285        } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
1286          let lhs_low = unsafe { vget_low_u16(self.neon) };
1287          let rhs_low = unsafe { vget_low_u16(rhs.neon) };
1288
1289          let lhs_high = unsafe { vget_high_u16(self.neon) };
1290          let rhs_high = unsafe { vget_high_u16(rhs.neon) };
1291
1292          let low = unsafe { vmull_u16(lhs_low, rhs_low) };
1293          let high = unsafe { vmull_u16(lhs_high, rhs_high) };
1294
1295          u32x8 { a: u32x4 { neon: low }, b: u32x4 {neon: high } }
1296        } else {
1297          let a = self.as_array();
1298          let b = rhs.as_array();
1299          u32x8::new([
1300            u32::from(a[0]) * u32::from(b[0]),
1301            u32::from(a[1]) * u32::from(b[1]),
1302            u32::from(a[2]) * u32::from(b[2]),
1303            u32::from(a[3]) * u32::from(b[3]),
1304            u32::from(a[4]) * u32::from(b[4]),
1305            u32::from(a[5]) * u32::from(b[5]),
1306            u32::from(a[6]) * u32::from(b[6]),
1307            u32::from(a[7]) * u32::from(b[7]),
1308          ])
1309        }
1310      }
1311    }
1312  }
1313
1314  #[inline]
1315  pub fn mul_keep_low_high(self, rhs: Self) -> (Self, Self) {
1316    pick! {
1317      if #[cfg(target_feature="simd128")] {
1318        let low_wide_mul = u32x4_extmul_low_u16x8(self.simd, rhs.simd);
1319        let high_wide_mul = u32x4_extmul_high_u16x8(self.simd, rhs.simd);
1320        (
1321          Self { simd: u16x8_shuffle::<0, 2, 4, 6, 8, 10, 12, 14>(low_wide_mul, high_wide_mul) },
1322          Self { simd: u16x8_shuffle::<1, 3, 5, 7, 9, 11, 13, 15>(low_wide_mul, high_wide_mul) },
1323        )
1324      } else if #[cfg(all(target_feature="neon", target_arch="aarch64"))] {
1325        unsafe {
1326          let low_wide_mul = vreinterpretq_u16_u32(
1327            vmull_u16(vget_low_u16(self.neon), vget_low_u16(rhs.neon)),
1328          );
1329          let high_wide_mul = vreinterpretq_u16_u32(
1330            vmull_u16(vget_high_u16(self.neon), vget_high_u16(rhs.neon)),
1331          );
1332          let low_high = vuzpq_u16(low_wide_mul, high_wide_mul);
1333          (
1334            Self { neon: low_high.0 },
1335            Self { neon: low_high.1 },
1336          )
1337        }
1338      } else {
1339        // TODO(perf): This implementation looks quite bad. Is there a better
1340        // one?
1341
1342        let self_array = self.to_array();
1343        let rhs_array = rhs.to_array();
1344
1345        let widening_mul = [
1346          (self_array[0] as u32).wrapping_mul(rhs_array[0] as u32),
1347          (self_array[1] as u32).wrapping_mul(rhs_array[1] as u32),
1348          (self_array[2] as u32).wrapping_mul(rhs_array[2] as u32),
1349          (self_array[3] as u32).wrapping_mul(rhs_array[3] as u32),
1350          (self_array[4] as u32).wrapping_mul(rhs_array[4] as u32),
1351          (self_array[5] as u32).wrapping_mul(rhs_array[5] as u32),
1352          (self_array[6] as u32).wrapping_mul(rhs_array[6] as u32),
1353          (self_array[7] as u32).wrapping_mul(rhs_array[7] as u32),
1354        ];
1355
1356        (
1357          Self::new([
1358            widening_mul[0] as u16,
1359            widening_mul[1] as u16,
1360            widening_mul[2] as u16,
1361            widening_mul[3] as u16,
1362            widening_mul[4] as u16,
1363            widening_mul[5] as u16,
1364            widening_mul[6] as u16,
1365            widening_mul[7] as u16,
1366          ]),
1367          Self::new([
1368            (widening_mul[0] >> 16) as u16,
1369            (widening_mul[1] >> 16) as u16,
1370            (widening_mul[2] >> 16) as u16,
1371            (widening_mul[3] >> 16) as u16,
1372            (widening_mul[4] >> 16) as u16,
1373            (widening_mul[5] >> 16) as u16,
1374            (widening_mul[6] >> 16) as u16,
1375            (widening_mul[7] >> 16) as u16,
1376          ]),
1377        )
1378      }
1379    }
1380  }
1381
1382  #[inline]
1383  pub fn mul_keep_high(self, rhs: Self) -> Self {
1384    pick! {
1385      if #[cfg(target_feature="sse2")] {
1386        Self { sse: mul_u16_keep_high_m128i(self.sse, rhs.sse) }
1387      } else if #[cfg(all(target_feature="neon",target_arch="aarch64"))] {
1388        let lhs_low = unsafe { vget_low_u16(self.neon) };
1389        let rhs_low = unsafe { vget_low_u16(rhs.neon) };
1390
1391        let lhs_high = unsafe { vget_high_u16(self.neon) };
1392        let rhs_high = unsafe { vget_high_u16(rhs.neon) };
1393
1394        let low = unsafe { vmull_u16(lhs_low, rhs_low) };
1395        let high = unsafe { vmull_u16(lhs_high, rhs_high) };
1396
1397        u16x8 { neon: unsafe { vuzpq_u16(vreinterpretq_u16_u32(low), vreinterpretq_u16_u32(high)).1 } }
1398      } else if #[cfg(target_feature="simd128")] {
1399        let low =  u32x4_extmul_low_u16x8(self.simd, rhs.simd);
1400        let high = u32x4_extmul_high_u16x8(self.simd, rhs.simd);
1401
1402        Self { simd: u16x8_shuffle::<1, 3, 5, 7, 9, 11, 13, 15>(low, high) }
1403      } else {
1404        u16x8::new([
1405          ((u32::from(rhs.as_array()[0]) * u32::from(self.as_array()[0])) >> 16) as u16,
1406          ((u32::from(rhs.as_array()[1]) * u32::from(self.as_array()[1])) >> 16) as u16,
1407          ((u32::from(rhs.as_array()[2]) * u32::from(self.as_array()[2])) >> 16) as u16,
1408          ((u32::from(rhs.as_array()[3]) * u32::from(self.as_array()[3])) >> 16) as u16,
1409          ((u32::from(rhs.as_array()[4]) * u32::from(self.as_array()[4])) >> 16) as u16,
1410          ((u32::from(rhs.as_array()[5]) * u32::from(self.as_array()[5])) >> 16) as u16,
1411          ((u32::from(rhs.as_array()[6]) * u32::from(self.as_array()[6])) >> 16) as u16,
1412          ((u32::from(rhs.as_array()[7]) * u32::from(self.as_array()[7])) >> 16) as u16,
1413        ])
1414      }
1415    }
1416  }
1417
1418  optional_fn_deserialize {}
1419}
1420
1421/// The following functionality exists only for [`u16x8`], or only for
1422/// particular types inconsistently.
1423impl u16x8 {
1424  /// Converts the lower eight elements of `u` from [`u8`] to [`u16`], dropping
1425  /// the higher eight elements.
1426  #[inline]
1427  #[must_use]
1428  pub fn from_u8x16_low(u: u8x16) -> Self {
1429    pick! {
1430      if #[cfg(target_feature="sse2")] {
1431        Self{ sse: unpack_low_i8_m128i(u.sse, m128i::zeroed()) }
1432      } else {
1433        let u_arr: [u8; 16] = cast(u);
1434        cast([
1435          u_arr[0] as u16,
1436          u_arr[1] as u16,
1437          u_arr[2] as u16,
1438          u_arr[3] as u16,
1439          u_arr[4] as u16,
1440          u_arr[5] as u16,
1441          u_arr[6] as u16,
1442          u_arr[7] as u16,
1443        ])
1444      }
1445    }
1446  }
1447
1448  /// Converts the higher eight elements of `u` from [`u8`] to [`u16`], dropping
1449  /// the lower eight elements.
1450  #[inline]
1451  #[must_use]
1452  pub fn from_u8x16_high(u: u8x16) -> Self {
1453    pick! {
1454      if #[cfg(target_feature="sse2")] {
1455        Self{ sse: unpack_high_i8_m128i(u.sse, m128i::zeroed()) }
1456      } else {
1457        let u_arr: [u8; 16] = cast(u);
1458        cast([
1459          u_arr[8] as u16,
1460          u_arr[9] as u16,
1461          u_arr[10] as u16,
1462          u_arr[11] as u16,
1463          u_arr[12] as u16,
1464          u_arr[13] as u16,
1465          u_arr[14] as u16,
1466          u_arr[15] as u16,
1467        ])
1468      }
1469    }
1470  }
1471
1472  /// Widening multiplication. Computes `self * rhs`, widening to a SIMD
1473  /// vector of larger integers.
1474  ///
1475  /// The returned value is always exact and can never overflow.
1476  ///
1477  /// This function has been renamed to [`widening_mul`].
1478  ///
1479  /// [`widening_mul`]: Self::widening_mul
1480  #[inline]
1481  #[must_use]
1482  #[deprecated(since = "1.6.0", note = "renamed to `widening_mul`")]
1483  pub fn mul_widen(self, rhs: Self) -> u32x8 {
1484    self.widening_mul(rhs)
1485  }
1486
1487  /// A helper for shuffle functions that turns indices of 16-bit lanes into
1488  /// byte indices that can be used with 8-bit shuffle intrinsics.
1489  ///
1490  /// This turns each 16-bit lane `i` into two 8-bit lanes `[2*i, 2*i + 1]`.
1491  ///
1492  /// This assumes `self` has already been reduced to the table's lane count,
1493  /// which may be at most 128 lanes so that `2 * i` still fits in a byte.
1494  #[allow(dead_code)]
1495  #[inline]
1496  fn to_byte_indices(self) -> u8x16 {
1497    // The byte offset of the lane, broadcast to every byte of the lane.
1498    let base = self.unbounded_shl_scalar(1);
1499    let base = base | base.unbounded_shl_scalar(8);
1500
1501    // Then the offset of each byte within its lane. These bits are free because
1502    // every byte of `base` is a multiple of two. `from_ne_bytes` keeps this
1503    // correct on big endian, where the bytes of a lane are the other way around.
1504    const WITHIN_LANE: u16x8 = u16x8::splat(u16::from_ne_bytes([0, 1]));
1505
1506    cast::<u16x8, u8x16>(base | WITHIN_LANE)
1507  }
1508}