Skip to main content

wide/
u32x8_.rs

1use super::*;
2
3pick! {
4  if #[cfg(target_feature="avx2")] {
5    /// A SIMD vector with eight elements of type [`u32`].
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(32))]
13    pub struct u32x8 { pub(crate) avx2: m256i }
14  } else {
15    /// A SIMD vector with eight elements of type [`u32`].
16    ///
17    /// See the [crate level documentation] for more information about SIMD
18    /// vectors.
19    ///
20    /// [crate level documentation]: crate
21    #[derive(Default, Clone, Copy, PartialEq, Eq)]
22    #[repr(C, align(32))]
23    pub struct u32x8 { pub(crate) a : u32x4, pub(crate) b : u32x4 }
24  }
25}
26
27impl_simd_uint! {
28  unsafe {
29    T = u32,
30    N = 8,
31    Simd = u32x8,
32    IntSimd = i32x8,
33    T_BITS = 32,
34    T_BITS_MUL_2 = 64,
35    [0, 1, 2, 3, 4, 5, 6, 7],
36    optional_type_x86_inner { X86Inner = __m256i },
37    optional_type_arm_inner {},
38    optional_type_wasm_inner {},
39  }
40
41  #[inline]
42  fn not(self) -> Self {
43    pick! {
44      if #[cfg(target_feature="avx2")] {
45        Self { avx2: self.avx2.not()  }
46      } else {
47        Self {
48          a : self.a.not(),
49          b : self.b.not(),
50        }
51      }
52    }
53  }
54
55  #[inline]
56  fn add(self, rhs: Self) -> Self::Output {
57    pick! {
58      if #[cfg(target_feature="avx2")] {
59        Self { avx2: add_i32_m256i(self.avx2, rhs.avx2) }
60      } else {
61        Self {
62          a : self.a.add(rhs.a),
63          b : self.b.add(rhs.b),
64        }
65      }
66    }
67  }
68
69  #[inline]
70  fn sub(self, rhs: Self) -> Self::Output {
71    pick! {
72      if #[cfg(target_feature="avx2")] {
73        Self { avx2: sub_i32_m256i(self.avx2, rhs.avx2) }
74      } else {
75        Self {
76          a : self.a.sub(rhs.a),
77          b : self.b.sub(rhs.b),
78        }
79      }
80    }
81  }
82
83  #[inline]
84  fn mul(self, rhs: Self) -> Self::Output {
85    pick! {
86      if #[cfg(target_feature="avx2")] {
87        Self { avx2: mul_i32_keep_low_m256i(self.avx2, rhs.avx2) }
88      } else {
89        Self {
90          a : self.a.mul(rhs.a),
91          b : self.b.mul(rhs.b),
92        }
93      }
94    }
95  }
96
97  #[inline]
98  fn bitand(self, rhs: Self) -> Self::Output {
99    pick! {
100      if #[cfg(target_feature="avx2")] {
101        Self { avx2: bitand_m256i(self.avx2, rhs.avx2) }
102      } else {
103        Self {
104          a : self.a.bitand(rhs.a),
105          b : self.b.bitand(rhs.b),
106        }
107      }
108    }
109  }
110
111  #[inline]
112  fn bitor(self, rhs: Self) -> Self::Output {
113    pick! {
114      if #[cfg(target_feature="avx2")] {
115        Self { avx2: bitor_m256i(self.avx2, rhs.avx2) }
116      } else {
117        Self {
118          a : self.a.bitor(rhs.a),
119          b : self.b.bitor(rhs.b),
120        }
121      }
122    }
123  }
124
125  #[inline]
126  fn bitxor(self, rhs: Self) -> Self::Output {
127    pick! {
128      if #[cfg(target_feature="avx2")] {
129        Self { avx2: bitxor_m256i(self.avx2, rhs.avx2) }
130      } else {
131        Self {
132          a : self.a.bitxor(rhs.a),
133          b : self.b.bitxor(rhs.b),
134        }
135      }
136    }
137  }
138
139  #[inline]
140  fn simd_eq(self, rhs: Self) -> Self::Output {
141    pick! {
142      if #[cfg(target_feature="avx2")] {
143        Self { avx2: cmp_eq_mask_i32_m256i(self.avx2, rhs.avx2 ) }
144      } else {
145        Self {
146          a : self.a.simd_eq(rhs.a),
147          b : self.b.simd_eq(rhs.b),
148        }
149      }
150    }
151  }
152
153  #[inline]
154  fn simd_ne(self, rhs: Self) -> Self::Output {
155    pick! {
156      if #[cfg(target_feature="avx2")] {
157        !self.simd_eq(rhs)
158      } else {
159        Self {
160          a : self.a.simd_ne(rhs.a),
161          b : self.b.simd_ne(rhs.b),
162        }
163      }
164    }
165  }
166
167  #[inline]
168  fn simd_lt(self, rhs: Self) -> Self::Output {
169    // lt is just gt the other way around
170    rhs.simd_gt(self)
171  }
172
173  #[inline]
174  fn simd_gt(self, rhs: Self) -> Self::Output {
175    pick! {
176      if #[cfg(target_feature="avx2")] {
177        // no unsigned gt than so inverting the high bit will get the correct result
178        let highbit = u32x8::splat(1 << 31);
179        Self { avx2: cmp_gt_mask_i32_m256i((self ^ highbit).avx2, (rhs ^ highbit).avx2 ) }
180      } else {
181        Self {
182          a : self.a.simd_gt(rhs.a),
183          b : self.b.simd_gt(rhs.b),
184        }
185      }
186    }
187  }
188
189  #[inline]
190  fn simd_le(self, rhs: Self) -> Self::Output {
191    self.simd_eq(rhs) | self.simd_lt(rhs)
192  }
193
194  #[inline]
195  fn simd_ge(self, rhs: Self) -> Self::Output {
196    self.simd_eq(rhs) | self.simd_gt(rhs)
197  }
198
199  #[inline]
200  pub fn reduce_add(self) -> u32 {
201    let array: [u32x4; 2] = cast(self);
202    (array[0] + array[1]).reduce_add()
203  }
204
205  #[inline]
206  pub fn reduce_mul(self) -> u32 {
207    let array: [u32x4; 2] = cast(self);
208    (array[0] * array[1]).reduce_mul()
209  }
210
211  #[inline]
212  pub fn bitselect(self, if_one: Self, if_zero: Self) -> Self {
213    pick! {
214      if #[cfg(target_feature="avx2")] {
215        Self {
216          avx2: bitor_m256i(
217            bitand_m256i(if_one.avx2, self.avx2),
218            bitandnot_m256i(self.avx2, if_zero.avx2),
219          ),
220        }
221      } else {
222        Self {
223          a: self.a.bitselect(if_one.a, if_zero.a),
224          b: self.b.bitselect(if_one.b, if_zero.b),
225        }
226      }
227    }
228  }
229
230  #[inline]
231  fn select(self, if_true: Self, if_false: Self) -> Self {
232    pick! {
233      if #[cfg(target_feature="avx2")] {
234        Self { avx2: blend_varying_i8_m256i(if_false.avx2, if_true.avx2, self.avx2) }
235      } else {
236        Self {
237          a : self.a.select(if_true.a, if_false.a),
238          b : self.b.select(if_true.b, if_false.b),
239        }
240      }
241    }
242  }
243
244  #[inline]
245  pub fn to_bitmask(self) -> u32 {
246    pick! {
247      if #[cfg(target_feature="avx2")] {
248        // use f32 move_mask since it is the same size as i32
249        move_mask_m256(cast(self.avx2)) as u32
250      } else {
251        self.a.to_bitmask() | (self.b.to_bitmask() << 4)
252      }
253    }
254  }
255
256  #[inline]
257  pub fn any(self) -> bool {
258    pick! {
259      if #[cfg(target_feature="avx2")] {
260        move_mask_m256(cast(self.avx2)) != 0
261      } else {
262        (self.a | self.b).any()
263      }
264    }
265  }
266
267  #[inline]
268  pub fn all(self) -> bool {
269    pick! {
270      if #[cfg(target_feature="avx2")] {
271        move_mask_m256(cast(self.avx2)) == 0b11111111
272      } else {
273        (self.a & self.b).all()
274      }
275    }
276  }
277
278  #[inline]
279  pub fn shuffle(self, indices: u32x8) -> Self {
280    pick! {
281      if #[cfg(all(target_feature = "avx512f", target_feature = "avx512vl"))] {
282        #[cfg(target_arch = "x86")]
283        use core::arch::x86::_mm256_permutexvar_epi32;
284        #[cfg(target_arch = "x86_64")]
285        use core::arch::x86_64::_mm256_permutexvar_epi32;
286        // TODO(safe_arch): Add `_mm256_permutexvar_epi32`
287        Self { avx2: unsafe { m256i(_mm256_permutexvar_epi32(indices.avx2.0, self.avx2.0)) } }
288      } else if #[cfg(target_feature = "avx2")] {
289        Self { avx2: shuffle_av_i32_all_m256i(self.avx2, indices.avx2) }
290      } else if #[cfg(any(
291        target_feature = "ssse3",
292        all(target_arch = "aarch64", target_feature = "neon"),
293        target_feature = "simd128",
294      ))] {
295        let self_bytes = cast::<u32x8, u8x32>(self);
296        let byte_indices = indices.to_byte_indices();
297
298        cast::<u8x32, u32x8>(self_bytes.shuffle(byte_indices))
299      } else {
300        let self_array = self.to_array();
301        let indices_array = indices.to_array();
302
303        let mut result = [0; 8];
304        for i in 0..8 {
305          let index = indices_array[i] as usize;
306          if index < 8 {
307            result[i] = self_array[index];
308          }
309        }
310
311        Self::new(result)
312      }
313    }
314  }
315
316  #[inline]
317  pub fn shuffle_zeroing(self, indices: u32x8) -> Self {
318    pick! {
319      if #[cfg(any(
320        target_feature = "ssse3",
321        all(target_arch = "aarch64", target_feature = "neon"),
322        target_feature = "simd128",
323      ))] {
324        // Even if the `u8x32::shuffle` implementation is zeroing, our 32-bit to
325        // 8-bit can trigger an overflow causing incorrect behavior
326        self.shuffle(indices) & indices.simd_lt(8)
327      } else {
328        // The fallback branch of `shuffle` already has the behavior we want
329        self.shuffle(indices)
330      }
331    }
332  }
333
334  #[inline]
335  pub fn shuffle_wrapping(self, indices: u32x8) -> Self {
336    pick! {
337      if #[cfg(all(target_feature = "avx512f", target_feature = "avx512vl"))] {
338        // `avx512` shuffle intrinsics are wrapping
339        self.shuffle(indices)
340      } else {
341        self.shuffle(indices & 7)
342      }
343    }
344  }
345
346  #[inline]
347  fn shuffle(self: [u32x8; 2], indices: u32x8) -> u32x8 {
348    pick! {
349      if #[cfg(all(target_feature = "avx512f", target_feature = "avx512vl"))] {
350        u32x8 { avx2: shuffle_abv_i32_all_m256i(self[0].avx2, indices.avx2, self[1].avx2) }
351      } else {
352        let self_bytes = cast::<[u32x8; 2], [u8x32; 2]>(self);
353        let byte_indices = indices.to_byte_indices();
354
355        cast::<u8x32, u32x8>(self_bytes.shuffle(byte_indices))
356      }
357    }
358  }
359
360  #[inline]
361  fn shuffle_zeroing(self: [u32x8; 2], indices: u32x8) -> u32x8 {
362    // Even if the `u8x32::shuffle` implementation is zeroing, our 32-bit to
363    // 8-bit can trigger an overflow causing incorrect behavior
364    self.shuffle(indices) & indices.simd_lt(16)
365  }
366
367  #[inline]
368  fn shuffle_wrapping(self: [u32x8; 2], indices: u32x8) -> u32x8 {
369    pick! {
370      if #[cfg(all(target_feature = "avx512f", target_feature = "avx512vl"))] {
371        // `avx512` shuffle intrinsics are wrapping
372        self.shuffle(indices)
373      } else {
374        self.shuffle(indices & 15)
375      }
376    }
377  }
378
379  #[inline]
380  fn shuffle(self: [u32x8; 3], indices: u32x8) -> u32x8 {
381    let self_bytes = cast::<[u32x8; 3], [u8x32; 3]>(self);
382    let byte_indices = indices.to_byte_indices();
383
384    cast::<u8x32, u32x8>(self_bytes.shuffle(byte_indices))
385  }
386
387  #[inline]
388  fn shuffle_zeroing(self: [u32x8; 3], indices: u32x8) -> u32x8 {
389    // Even if the `u8x32::shuffle` implementation is zeroing, our 32-bit to
390    // 8-bit can trigger an overflow causing incorrect behavior
391    self.shuffle(indices) & indices.simd_lt(24)
392  }
393
394  #[inline]
395  fn shuffle_wrapping(self: [u32x8; 3], indices: u32x8) -> u32x8 {
396    self.shuffle(indices % 24)
397  }
398
399  #[inline]
400  fn shuffle(self: [u32x8; 4], indices: u32x8) -> u32x8 {
401    let self_bytes = cast::<[u32x8; 4], [u8x32; 4]>(self);
402    let byte_indices = indices.to_byte_indices();
403
404    cast::<u8x32, u32x8>(self_bytes.shuffle(byte_indices))
405  }
406
407  #[inline]
408  fn shuffle_zeroing(self: [u32x8; 4], indices: u32x8) -> u32x8 {
409    // Even if the `u8x32::shuffle` implementation is zeroing, our 32-bit to
410    // 8-bit can trigger an overflow causing incorrect behavior
411    self.shuffle(indices) & indices.simd_lt(32)
412  }
413
414  #[inline]
415  fn shuffle_wrapping(self: [u32x8; 4], indices: u32x8) -> u32x8 {
416    self.shuffle(indices & 31)
417  }
418
419  ///
420  /// Currently this function is only accelerated on `avx2`.
421  #[inline]
422  pub fn transpose(data: [Self; 8]) -> [Self; 8] {
423    pick! {
424      if #[cfg(target_feature="avx2")] {
425        let a0 = unpack_low_i32_m256i(data[0].avx2, data[1].avx2);
426        let a1 = unpack_high_i32_m256i(data[0].avx2, data[1].avx2);
427        let a2 = unpack_low_i32_m256i(data[2].avx2, data[3].avx2);
428        let a3 = unpack_high_i32_m256i(data[2].avx2, data[3].avx2);
429        let a4 = unpack_low_i32_m256i(data[4].avx2, data[5].avx2);
430        let a5 = unpack_high_i32_m256i(data[4].avx2, data[5].avx2);
431        let a6 = unpack_low_i32_m256i(data[6].avx2, data[7].avx2);
432        let a7 = unpack_high_i32_m256i(data[6].avx2, data[7].avx2);
433
434        pub const fn mm_shuffle(z: i32, y: i32, x: i32, w: i32) -> i32 {
435          (z << 6) | (y << 4) | (x << 2) | w
436        }
437
438        const SHUFF_LO : i32 = mm_shuffle(1,0,1,0);
439        const SHUFF_HI : i32 = mm_shuffle(3,2,3,2);
440
441        // possible todo: intel performance manual suggests alternative with blend to avoid port 5 pressure
442        // (since blend runs on a different port than shuffle)
443        let b0 = cast::<m256,m256i>(shuffle_m256::<SHUFF_LO>(cast(a0),cast(a2)));
444        let b1 = cast::<m256,m256i>(shuffle_m256::<SHUFF_HI>(cast(a0),cast(a2)));
445        let b2 = cast::<m256,m256i>(shuffle_m256::<SHUFF_LO>(cast(a1),cast(a3)));
446        let b3 = cast::<m256,m256i>(shuffle_m256::<SHUFF_HI>(cast(a1),cast(a3)));
447        let b4 = cast::<m256,m256i>(shuffle_m256::<SHUFF_LO>(cast(a4),cast(a6)));
448        let b5 = cast::<m256,m256i>(shuffle_m256::<SHUFF_HI>(cast(a4),cast(a6)));
449        let b6 = cast::<m256,m256i>(shuffle_m256::<SHUFF_LO>(cast(a5),cast(a7)));
450        let b7 = cast::<m256,m256i>(shuffle_m256::<SHUFF_HI>(cast(a5),cast(a7)));
451
452        [
453          u32x8 { avx2: permute2z_m256i::<0x20>(b0, b4) },
454          u32x8 { avx2: permute2z_m256i::<0x20>(b1, b5) },
455          u32x8 { avx2: permute2z_m256i::<0x20>(b2, b6) },
456          u32x8 { avx2: permute2z_m256i::<0x20>(b3, b7) },
457          u32x8 { avx2: permute2z_m256i::<0x31>(b0, b4) },
458          u32x8 { avx2: permute2z_m256i::<0x31>(b1, b5) },
459          u32x8 { avx2: permute2z_m256i::<0x31>(b2, b6) },
460          u32x8 { avx2: permute2z_m256i::<0x31>(b3, b7) }
461        ]
462      } else {
463        // possible todo: not sure that 128bit SIMD gives us a a lot of speedup here
464
465        #[inline(always)]
466        fn transpose_column(data: &[u32x8; 8], index: usize) -> u32x8 {
467          u32x8::new([
468            data[0].as_array()[index],
469            data[1].as_array()[index],
470            data[2].as_array()[index],
471            data[3].as_array()[index],
472            data[4].as_array()[index],
473            data[5].as_array()[index],
474            data[6].as_array()[index],
475            data[7].as_array()[index],
476          ])
477        }
478
479        [
480          transpose_column(&data, 0),
481          transpose_column(&data, 1),
482          transpose_column(&data, 2),
483          transpose_column(&data, 3),
484          transpose_column(&data, 4),
485          transpose_column(&data, 5),
486          transpose_column(&data, 6),
487          transpose_column(&data, 7),
488        ]
489      }
490    }
491  }
492
493  #[inline]
494  fn shl(self, rhs: u32x8) -> Self::Output {
495    pick! {
496      if #[cfg(target_feature="avx2")] {
497        // ensure same behavior as scalar wrapping_shl
498        let shift_by = bitand_m256i(rhs.avx2, set_splat_i32_m256i(31));
499        Self { avx2: shl_each_u32_m256i(self.avx2, shift_by) }
500      } else {
501        Self {
502          a : self.a.shl(rhs.a),
503          b : self.b.shl(rhs.b),
504        }
505      }
506    }
507  }
508
509  #[inline]
510  fn shl(self, rhs: u32) -> Self::Output {
511    pick! {
512      if #[cfg(target_feature="avx2")] {
513        // Use `rhs % 32` to perform wrapping shift and not unbounded shift.
514        #[expect(clippy::suspicious_arithmetic_impl)]
515        let shift = cast([rhs as u64 & 31, 0]);
516        Self { avx2: shl_all_u32_m256i(self.avx2, shift) }
517      } else {
518        Self {
519          a : self.a.shl(rhs),
520          b : self.b.shl(rhs),
521        }
522      }
523    }
524  }
525
526  #[inline]
527  fn shr(self, rhs: u32x8) -> Self::Output {
528    pick! {
529      if #[cfg(target_feature="avx2")] {
530        // ensure same behavior as scalar wrapping_shr
531        let shift_by = bitand_m256i(rhs.avx2, set_splat_i32_m256i(31));
532        Self { avx2: shr_each_u32_m256i(self.avx2, shift_by ) }
533      } else {
534        Self {
535          a : self.a.shr(rhs.a),
536          b : self.b.shr(rhs.b),
537        }
538      }
539    }
540  }
541
542  #[inline]
543  fn shr(self, rhs: u32) -> Self::Output {
544    pick! {
545      if #[cfg(target_feature="avx2")] {
546        // Use `rhs % 32` to perform wrapping shift and not unbounded shift.
547        #[expect(clippy::suspicious_arithmetic_impl)]
548        let shift = cast([rhs as u64 & 31, 0]);
549        Self { avx2: shr_all_u32_m256i(self.avx2, shift) }
550      } else {
551        Self {
552          a : self.a.shr(rhs),
553          b : self.b.shr(rhs),
554        }
555      }
556    }
557  }
558
559  #[inline]
560  pub fn max(self, rhs: Self) -> Self {
561    pick! {
562      if #[cfg(target_feature="avx2")] {
563        Self { avx2: max_u32_m256i(self.avx2, rhs.avx2 ) }
564      } else {
565        Self {
566          a : self.a.max(rhs.a),
567          b : self.b.max(rhs.b),
568        }
569      }
570    }
571  }
572
573  #[inline]
574  pub fn min(self, rhs: Self) -> Self {
575    pick! {
576      if #[cfg(target_feature="avx2")] {
577        Self { avx2: min_u32_m256i(self.avx2, rhs.avx2 ) }
578      } else {
579        Self {
580          a : self.a.min(rhs.a),
581          b : self.b.min(rhs.b),
582        }
583      }
584    }
585  }
586
587  #[inline]
588  pub fn reduce_max(self) -> u32 {
589    let array: [u32x4; 2] = cast(self);
590    array[0].max(array[1]).reduce_max()
591  }
592
593  #[inline]
594  pub fn reduce_min(self) -> u32 {
595    let array: [u32x4; 2] = cast(self);
596    array[0].min(array[1]).reduce_min()
597  }
598
599  #[inline]
600  pub fn unbounded_shl(self, rhs: Self) -> Self {
601    pick! {
602      if #[cfg(target_feature="avx2")] {
603        Self { avx2: shl_each_u32_m256i(self.avx2, rhs.avx2) }
604      } else {
605        Self {
606          a: self.a.unbounded_shl(rhs.a),
607          b: self.b.unbounded_shl(rhs.b),
608        }
609      }
610    }
611  }
612
613  #[inline]
614  pub fn unbounded_shl_scalar(self, rhs: u32) -> Self {
615    pick! {
616      if #[cfg(target_feature="avx2")] {
617        Self { avx2: shl_all_u32_m256i(self.avx2, cast([rhs as u64, 0])) }
618      } else {
619        Self {
620          a: self.a.unbounded_shl_scalar(rhs),
621          b: self.b.unbounded_shl_scalar(rhs),
622        }
623      }
624    }
625  }
626
627  #[inline]
628  pub fn unbounded_shr(self, rhs: Self) -> Self {
629    pick! {
630      if #[cfg(target_feature="avx2")] {
631        Self { avx2: shr_each_u32_m256i(self.avx2, rhs.avx2) }
632      } else {
633        Self {
634          a: self.a.unbounded_shr(rhs.a),
635          b: self.b.unbounded_shr(rhs.b),
636        }
637      }
638    }
639  }
640
641  #[inline]
642  pub fn unbounded_shr_scalar(self, rhs: u32) -> Self {
643    pick! {
644      if #[cfg(target_feature="avx2")] {
645        Self { avx2: shr_all_u32_m256i(self.avx2, cast([rhs as u64, 0])) }
646      } else {
647        Self {
648          a: self.a.unbounded_shr_scalar(rhs),
649          b: self.b.unbounded_shr_scalar(rhs),
650        }
651      }
652    }
653  }
654
655  #[inline]
656  pub fn saturating_add(self, rhs: Self) -> Self {
657    pick! {
658      if #[cfg(target_feature="avx2")] {
659        let result = self + rhs;
660        let overflow = result.simd_lt(self);
661        // Return `MAX` (all bits set) if overflow occurs.
662        result | overflow
663      } else {
664        Self {
665          a: self.a.saturating_add(rhs.a),
666          b: self.b.saturating_add(rhs.b),
667        }
668      }
669    }
670  }
671
672  #[inline]
673  pub fn saturating_sub(self, rhs: Self) -> Self {
674    pick! {
675      if #[cfg(target_feature="avx2")] {
676        let result = self - rhs;
677        let no_overflow = result.simd_le(self);
678        // Return `0` (no bits set) if overflow occurs.
679        result & no_overflow
680      } else {
681        Self {
682          a: self.a.saturating_sub(rhs.a),
683          b: self.b.saturating_sub(rhs.b),
684        }
685      }
686    }
687  }
688
689  #[inline]
690  pub fn overflowing_mul(self, rhs: Self) -> (Self, Self) {
691    let (low, high) = self.mul_keep_low_high(rhs);
692    let overflow = high.simd_ne(Self::ZERO);
693    (low, overflow)
694  }
695
696  optional_fn_widening_mul {
697    #[inline]
698    pub fn widening_mul(self, rhs: Self) -> u64x8 {
699      pick! {
700        if #[cfg(all(target_feature="avx512f", target_feature="avx2"))] {
701          const SHUFFLE_INDICES: m512i = i64x8::new([0, 4, 1, 5, 2, 6, 3, 7]).avx512;
702
703          let even_wide_mul = mul_u64_low_bits_m256i(self.avx2, rhs.avx2);
704          let odd_wide_mul = mul_u64_low_bits_m256i(
705            shuffle_ai_i32_half_m256i::<0b_00_11_00_01>(self.avx2),
706            shuffle_ai_i32_half_m256i::<0b_00_11_00_01>(rhs.avx2),
707          );
708          let even_then_odd = cast::<[m256i; 2], m512i>([even_wide_mul, odd_wide_mul]);
709          u64x8 {
710            avx512: permute_i64_m512i(SHUFFLE_INDICES, even_then_odd),
711          }
712        } else {
713          let [self_a, self_b] = cast::<u32x8, [u32x4; 2]>(self);
714          let [rhs_a, rhs_b] = cast::<u32x8, [u32x4; 2]>(rhs);
715
716          cast([self_a.widening_mul(rhs_a), self_b.widening_mul(rhs_b)])
717        }
718      }
719    }
720  }
721
722  #[inline]
723  pub fn mul_keep_low_high(self, rhs: Self) -> (Self, Self) {
724    pick! {
725      if #[cfg(target_feature="avx2")] {
726        let even_wide_mul = mul_u64_low_bits_m256i(self.avx2, rhs.avx2);
727        let odd_wide_mul = mul_u64_low_bits_m256i(
728          shuffle_ai_i32_half_m256i::<0b_00_11_00_01>(self.avx2),
729          shuffle_ai_i32_half_m256i::<0b_00_11_00_01>(rhs.avx2),
730        );
731        let ll_hh_1 = unpack_low_i32_m256i(even_wide_mul, odd_wide_mul);
732        let ll_hh_2 = unpack_high_i32_m256i(even_wide_mul, odd_wide_mul);
733        (
734          Self { avx2: unpack_low_i64_m256i(ll_hh_1, ll_hh_2) },
735          Self { avx2: unpack_high_i64_m256i(ll_hh_1, ll_hh_2) },
736        )
737      } else {
738        let [self_a, self_b] = cast::<u32x8, [u32x4; 2]>(self);
739        let [rhs_a, rhs_b] = cast::<u32x8, [u32x4; 2]>(rhs);
740
741        let result_a = self_a.mul_keep_low_high(rhs_a);
742        let result_b = self_b.mul_keep_low_high(rhs_b);
743        (
744          cast([result_a.0, result_b.0]),
745          cast([result_a.1, result_b.1]),
746        )
747      }
748    }
749  }
750
751  #[inline]
752  pub fn mul_keep_high(self, rhs: u32x8) -> u32x8 {
753    pick! {
754      if #[cfg(target_feature="avx2")] {
755        let a : [u32;8]= cast(self);
756        let b : [u32;8]= cast(rhs);
757
758        // let the compiler shuffle the values around, it does the right thing
759        let r1 : [u32;8] = cast(mul_u64_low_bits_m256i(cast([a[0], 0, a[1], 0, a[2], 0, a[3], 0]), cast([b[0], 0, b[1], 0, b[2], 0, b[3], 0])));
760        let r2 : [u32;8] = cast(mul_u64_low_bits_m256i(cast([a[4], 0, a[5], 0, a[6], 0, a[7], 0]), cast([b[4], 0, b[5], 0, b[6], 0, b[7], 0])));
761
762        cast([r1[1], r1[3], r1[5], r1[7], r2[1], r2[3], r2[5], r2[7]])
763      } else {
764        Self {
765          a : self.a.mul_keep_high(rhs.a),
766          b : self.b.mul_keep_high(rhs.b),
767        }
768      }
769    }
770  }
771
772  optional_fn_deserialize {}
773}
774
775/// The following functionality exists only for [`u32x8`], or only for
776/// particular types inconsistently.
777impl u32x8 {
778  /// A helper for shuffle functions that turns indices of 32-bit lanes into
779  /// byte indices that can be used with 8-bit shuffle intrinsics.
780  ///
781  /// This turns each 32-bit lane `i` into four 8-bit lanes
782  /// `[4*i, 4*i + 1, 4*i + 2, 4*i + 3]`.
783  ///
784  /// This assumes `self` has already been reduced to the table's lane count,
785  /// which may be at most 64 lanes so that `4 * i` still fits in a byte.
786  #[allow(dead_code)]
787  #[inline]
788  fn to_byte_indices(self) -> u8x32 {
789    // The byte offset of the lane, broadcast to every byte of the lane.
790    let base = self.unbounded_shl_scalar(2);
791    let base = base | base.unbounded_shl_scalar(8);
792    let base = base | base.unbounded_shl_scalar(16);
793
794    // Then the offset of each byte within its lane. These bits are free because
795    // every byte of `base` is a multiple of four. `from_ne_bytes` keeps this
796    // correct on big endian, where the bytes of a lane are the other way around.
797    const WITHIN_LANE: u32x8 = u32x8::splat(u32::from_ne_bytes([0, 1, 2, 3]));
798
799    cast::<u32x8, u8x32>(base | WITHIN_LANE)
800  }
801}
802
803impl From<u16x8> for u32x8 {
804  /// widens and zero extends to u32x8
805  #[inline]
806  fn from(v: u16x8) -> Self {
807    pick! {
808      if #[cfg(target_feature="avx2")] {
809        Self { avx2:convert_to_i32_m256i_from_u16_m128i(v.sse) }
810      } else if #[cfg(target_feature="sse2")] {
811        Self {
812          a: u32x4 { sse: shr_imm_u32_m128i::<16>( unpack_low_i16_m128i(v.sse, v.sse)) },
813          b: u32x4 { sse: shr_imm_u32_m128i::<16>( unpack_high_i16_m128i(v.sse, v.sse)) },
814        }
815      } else {
816        u32x8::new([
817          u32::from(v.as_array()[0]),
818          u32::from(v.as_array()[1]),
819          u32::from(v.as_array()[2]),
820          u32::from(v.as_array()[3]),
821          u32::from(v.as_array()[4]),
822          u32::from(v.as_array()[5]),
823          u32::from(v.as_array()[6]),
824          u32::from(v.as_array()[7]),
825        ])
826      }
827    }
828  }
829}
830
831/// The following functionality exists only for [`u32x8`], or only for
832/// particular types inconsistently.
833impl u32x8 {
834  /// Returns `[self[0], b[0], self[1], b[1], self[2], b[2], self[3], b[3]]`,
835  /// interleaving the low half of each vector.
836  #[inline]
837  #[must_use]
838  pub fn unpack_lo(self, b: Self) -> Self {
839    pick! {
840      if #[cfg(target_feature="avx2")] {
841        // `unpack_low_i32_m256i` cannot be used because it acts within each
842        // 128-bit lane, which is a different operation.
843        let [aa, _]: [u32x4; 2] = cast(self);
844        let [ba, _]: [u32x4; 2] = cast(b);
845        cast([aa.unpack_lo(ba), aa.unpack_hi(ba)])
846      } else {
847        Self { a: self.a.unpack_lo(b.a), b: self.a.unpack_hi(b.a) }
848      }
849    }
850  }
851
852  /// Returns `[self[4], b[4], self[5], b[5], self[6], b[6], self[7], b[7]]`,
853  /// interleaving the high half of each vector.
854  #[inline]
855  #[must_use]
856  pub fn unpack_hi(self, b: Self) -> Self {
857    pick! {
858      if #[cfg(target_feature="avx2")] {
859        // `unpack_high_i32_m256i` cannot be used because it acts within each
860        // 128-bit lane, which is a different operation.
861        let [_, ab]: [u32x4; 2] = cast(self);
862        let [_, bb]: [u32x4; 2] = cast(b);
863        cast([ab.unpack_lo(bb), ab.unpack_hi(bb)])
864      } else {
865        Self { a: self.b.unpack_lo(b.b), b: self.b.unpack_hi(b.b) }
866      }
867    }
868  }
869
870  /// `self + ((a * b) mod 2^W)`, reading only the low `W` bits of each lane of
871  /// `a` and `b`. `W` must be in `1..=32`.
872  ///
873  /// There is no IFMA equivalent at this width, and below 17 bits no widening
874  /// multiply is needed either: the whole product fits a lane, so the ordinary
875  /// lane multiply already yields both halves.
876  #[inline]
877  #[must_use]
878  pub fn add_mul_lo<const W: u32>(self, a: Self, b: Self) -> Self {
879    if W <= 16 {
880      let mask = Self::splat(add_mul_operand_mask_u32::<W>());
881      return self + (((a & mask) * (b & mask)) & mask);
882    }
883
884    let acc = self.to_array();
885    let a = a.to_array();
886    let b = b.to_array();
887    Self::new(core::array::from_fn(|i| {
888      add_mul_lo_lane_u32::<W>(acc[i], a[i], b[i])
889    }))
890  }
891
892  /// `self + ((a * b) >> W)`, reading only the low `W` bits of each lane of `a`
893  /// and `b`. `W` must be in `1..=32`.
894  #[inline]
895  #[must_use]
896  pub fn add_mul_hi<const W: u32>(self, a: Self, b: Self) -> Self {
897    // See `add_mul_lo`: the whole product is in the lane, so the high half is a
898    // shift.
899    if W <= 16 {
900      let mask = Self::splat(add_mul_operand_mask_u32::<W>());
901      return self + (((a & mask) * (b & mask)) >> W);
902    }
903
904    let acc = self.to_array();
905    let a = a.to_array();
906    let b = b.to_array();
907    Self::new(core::array::from_fn(|i| {
908      add_mul_hi_lane_u32::<W>(acc[i], a[i], b[i])
909    }))
910  }
911}