Skip to main content

wide/
i64x8_.rs

1use super::*;
2
3pick! {
4  if #[cfg(target_feature="avx512f")] {
5    /// A SIMD vector with eight elements of type [`i64`].
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(64))]
13    pub struct i64x8 { pub(crate) avx512: m512i }
14  } else {
15    /// A SIMD vector with eight elements of type [`i64`].
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(64))]
23    pub struct i64x8 { pub(crate) a : i64x4, pub(crate) b : i64x4 }
24  }
25}
26
27impl_simd_int! {
28  unsafe {
29    T = i64,
30    N = 8,
31    Simd = i64x8,
32    UintSimd = u64x8,
33    T_BITS = 64,
34    T_BITS_MUL_2 = 128,
35    BitmaskType = u32,
36    [0, 1, 2, 3, 4, 5, 6, 7],
37    optional_type_x86_inner { X86Inner = __m512i },
38    optional_type_arm_inner {},
39    optional_type_wasm_inner {},
40  }
41
42  #[inline]
43  fn simd_lt(self, rhs: Self) -> Self::Output {
44    pick! {
45      if #[cfg(target_feature="avx512f")] {
46        Self { avx512: cmp_op_mask_i64_m512i::<{cmp_int_op!(Lt)}>(self.avx512, rhs.avx512) }
47      } else {
48        Self {
49          a : rhs.a.simd_gt(self.a),
50          b : rhs.b.simd_gt(self.b),
51        }
52      }
53    }
54  }
55
56  #[inline]
57  fn simd_gt(self, rhs: Self) -> Self::Output {
58    pick! {
59      if #[cfg(target_feature="avx512f")] {
60        Self { avx512: cmp_op_mask_i64_m512i::<{cmp_int_op!(Nle)}>(self.avx512, rhs.avx512) }
61      } else {
62        Self {
63          a : self.a.simd_gt(rhs.a),
64          b : self.b.simd_gt(rhs.b),
65        }
66      }
67    }
68  }
69
70  #[inline]
71  fn simd_le(self, rhs: Self) -> Self::Output {
72    pick! {
73      if #[cfg(target_feature="avx512f")] {
74        Self { avx512: cmp_op_mask_i64_m512i::<{cmp_int_op!(Le)}>(self.avx512, rhs.avx512) }
75      } else {
76        Self {
77          a : self.a.simd_le(rhs.a),
78          b : self.b.simd_le(rhs.b),
79        }
80      }
81    }
82  }
83
84  #[inline]
85  fn simd_ge(self, rhs: Self) -> Self::Output {
86    pick! {
87      if #[cfg(target_feature="avx512f")] {
88        Self { avx512: cmp_op_mask_i64_m512i::<{cmp_int_op!(Nlt)}>(self.avx512, rhs.avx512) }
89      } else {
90        Self {
91          a : self.a.simd_ge(rhs.a),
92          b : self.b.simd_ge(rhs.b),
93        }
94      }
95    }
96  }
97
98  #[inline]
99  fn shr(self, rhs: u64x8) -> Self::Output {
100    pick! {
101      if #[cfg(target_feature="avx512f")] {
102        // TODO(safe_arch): add shr_each_i64_m512i (arithmetic right shift)
103        // Self { avx512: shr_each_i64_m512i(self.avx512, rhs.avx512) }
104        // Fallback for now:
105        let a: [i64; 8] = cast(self);
106        let r: [u64; 8] = cast(rhs);
107        cast([
108          a[0].wrapping_shr(r[0] as u32),
109          a[1].wrapping_shr(r[1] as u32),
110          a[2].wrapping_shr(r[2] as u32),
111          a[3].wrapping_shr(r[3] as u32),
112          a[4].wrapping_shr(r[4] as u32),
113          a[5].wrapping_shr(r[5] as u32),
114          a[6].wrapping_shr(r[6] as u32),
115          a[7].wrapping_shr(r[7] as u32),
116        ])
117      } else {
118        // widen via two halves
119        Self {
120          a: self.a.shr(rhs.a),
121          b: self.b.shr(rhs.b),
122        }
123      }
124    }
125  }
126
127  #[inline]
128  fn shr(self, rhs: u32) -> Self::Output {
129    pick! {
130      if #[cfg(target_feature="avx512f")] {
131        // Use `rhs % 64` to perform wrapping shift and not unbounded shift.
132        #[expect(clippy::suspicious_arithmetic_impl)]
133        let shift = rhs as u64 & 63;
134        Self { avx512: shr_all_i64_m512i(self.avx512, shift) }
135      } else {
136        Self {
137          a : self.a.shr(rhs),
138          b : self.b.shr(rhs),
139        }
140      }
141    }
142  }
143
144  #[inline]
145  pub fn max(self, rhs: Self) -> Self {
146    pick! {
147      if #[cfg(target_feature="avx512f")] {
148        Self { avx512: max_i64_m512i(self.avx512, rhs.avx512) }
149      } else {
150        Self {
151          a: self.a.max(rhs.a),
152          b: self.b.max(rhs.b),
153        }
154      }
155    }
156  }
157
158  #[inline]
159  pub fn min(self, rhs: Self) -> Self {
160    pick! {
161      if #[cfg(target_feature="avx512f")] {
162        Self { avx512: min_i64_m512i(self.avx512, rhs.avx512) }
163      } else {
164        Self {
165          a: self.a.min(rhs.a),
166          b: self.b.min(rhs.b),
167        }
168      }
169    }
170  }
171
172  #[inline]
173  pub fn reduce_max(self) -> i64 {
174    let array: [i64x4; 2] = cast(self);
175    array[0].max(array[1]).reduce_max()
176  }
177
178  #[inline]
179  pub fn reduce_min(self) -> i64 {
180    let array: [i64x4; 2] = cast(self);
181    array[0].min(array[1]).reduce_min()
182  }
183
184  #[inline]
185  pub fn unbounded_shr(self, rhs: u64x8) -> Self {
186    // TODO(safe_arch): add shr_each_i64_m512i (arithmetic right shift)
187    // Self { avx512: shr_each_i64_m512i(self.avx512, rhs.avx512) }
188    // Fallback for now:
189
190    let [self_a, self_b] = cast::<i64x8, [i64x4; 2]>(self);
191    let [rhs_a, rhs_b] = cast::<u64x8, [u64x4; 2]>(rhs);
192
193    cast([self_a.unbounded_shr(rhs_a), self_b.unbounded_shr(rhs_b)])
194  }
195
196  #[inline]
197  pub fn unbounded_shr_scalar(self, rhs: u32) -> Self {
198    pick! {
199      if #[cfg(target_feature="avx512f")] {
200        Self { avx512: shr_all_i64_m512i(self.avx512, rhs as u64) }
201      } else {
202        Self {
203          a: self.a.unbounded_shr_scalar(rhs),
204          b: self.b.unbounded_shr_scalar(rhs),
205        }
206      }
207    }
208  }
209
210  #[inline]
211  pub fn saturating_add(self, rhs: Self) -> Self {
212    pick! {
213      if #[cfg(target_feature="avx512f")] {
214        let result = self + rhs;
215        let overflow = (!(self ^ rhs) & (self ^ result)).is_negative();
216        let negative = self.is_negative();
217
218        // If overflow occurs return `MAX` if positive or `MIN` if negative.
219        overflow.select(Self::MAX ^ negative, result)
220      } else {
221        Self {
222          a: self.a.saturating_add(rhs.a),
223          b: self.b.saturating_add(rhs.b),
224        }
225      }
226    }
227  }
228
229  #[inline]
230  pub fn saturating_sub(self, rhs: Self) -> Self {
231    pick! {
232      if #[cfg(target_feature="avx512f")] {
233        let result = self - rhs;
234        let overflow = ((self ^ rhs) & (self ^ result)).is_negative();
235        let negative = self.is_negative();
236
237        // If overflow occurs return `MAX` if positive or `MIN` if negative.
238        overflow.select(Self::MAX ^ negative, result)
239      } else {
240        Self {
241          a: self.a.saturating_sub(rhs.a),
242          b: self.b.saturating_sub(rhs.b),
243        }
244      }
245    }
246  }
247
248  #[inline]
249  pub fn overflowing_mul(self, rhs: Self) -> (Self, Self) {
250    // TODO(perf): This implementation looks quite bad. Is there a better
251    // one? This intentionally avoids `mul_keep_low_high` because getting the
252    // high bits of 64-bit multiplication could be slow.
253
254    let self_array = self.to_array();
255    let rhs_array = rhs.to_array();
256
257    let result = [
258      self_array[0].overflowing_mul(rhs_array[0]),
259      self_array[1].overflowing_mul(rhs_array[1]),
260      self_array[2].overflowing_mul(rhs_array[2]),
261      self_array[3].overflowing_mul(rhs_array[3]),
262      self_array[4].overflowing_mul(rhs_array[4]),
263      self_array[5].overflowing_mul(rhs_array[5]),
264      self_array[6].overflowing_mul(rhs_array[6]),
265      self_array[7].overflowing_mul(rhs_array[7]),
266    ];
267    (
268      Self::new([
269        result[0].0,
270        result[1].0,
271        result[2].0,
272        result[3].0,
273        result[4].0,
274        result[5].0,
275        result[6].0,
276        result[7].0,
277      ]),
278      Self::new([
279        -(result[0].1 as i64),
280        -(result[1].1 as i64),
281        -(result[2].1 as i64),
282        -(result[3].1 as i64),
283        -(result[4].1 as i64),
284        -(result[5].1 as i64),
285        -(result[6].1 as i64),
286        -(result[7].1 as i64),
287      ]),
288    )
289  }
290
291  optional_fn_widening_mul {
292    // Cannot have `widening_mul` because there is no `i128x8` type.
293  }
294
295  #[inline]
296  pub fn mul_keep_low_high(self, rhs: Self) -> (u64x8, i64x8) {
297    // TODO(perf): This implementation looks quite bad. Is there a better
298    // one?
299
300    let self_array = self.to_array();
301    let rhs_array = rhs.to_array();
302
303    let widening_mul = [
304      (self_array[0] as i128).wrapping_mul(rhs_array[0] as i128),
305      (self_array[1] as i128).wrapping_mul(rhs_array[1] as i128),
306      (self_array[2] as i128).wrapping_mul(rhs_array[2] as i128),
307      (self_array[3] as i128).wrapping_mul(rhs_array[3] as i128),
308      (self_array[4] as i128).wrapping_mul(rhs_array[4] as i128),
309      (self_array[5] as i128).wrapping_mul(rhs_array[5] as i128),
310      (self_array[6] as i128).wrapping_mul(rhs_array[6] as i128),
311      (self_array[7] as i128).wrapping_mul(rhs_array[7] as i128),
312    ];
313
314    (
315      u64x8::new([
316        widening_mul[0] as u64,
317        widening_mul[1] as u64,
318        widening_mul[2] as u64,
319        widening_mul[3] as u64,
320        widening_mul[4] as u64,
321        widening_mul[5] as u64,
322        widening_mul[6] as u64,
323        widening_mul[7] as u64,
324      ]),
325      i64x8::new([
326        (widening_mul[0] >> 64) as i64,
327        (widening_mul[1] >> 64) as i64,
328        (widening_mul[2] >> 64) as i64,
329        (widening_mul[3] >> 64) as i64,
330        (widening_mul[4] >> 64) as i64,
331        (widening_mul[5] >> 64) as i64,
332        (widening_mul[6] >> 64) as i64,
333        (widening_mul[7] >> 64) as i64,
334      ]),
335    )
336  }
337
338  #[inline]
339  pub fn mul_keep_high(self, rhs: Self) -> Self {
340    let self_array = self.to_array();
341    let rhs_array = rhs.to_array();
342
343    Self::new([
344      ((self_array[0] as i128).wrapping_mul(rhs_array[0] as i128) >> 64) as i64,
345      ((self_array[1] as i128).wrapping_mul(rhs_array[1] as i128) >> 64) as i64,
346      ((self_array[2] as i128).wrapping_mul(rhs_array[2] as i128) >> 64) as i64,
347      ((self_array[3] as i128).wrapping_mul(rhs_array[3] as i128) >> 64) as i64,
348      ((self_array[4] as i128).wrapping_mul(rhs_array[4] as i128) >> 64) as i64,
349      ((self_array[5] as i128).wrapping_mul(rhs_array[5] as i128) >> 64) as i64,
350      ((self_array[6] as i128).wrapping_mul(rhs_array[6] as i128) >> 64) as i64,
351      ((self_array[7] as i128).wrapping_mul(rhs_array[7] as i128) >> 64) as i64,
352    ])
353  }
354
355  #[inline]
356  pub fn abs(self) -> Self {
357    pick! {
358      if #[cfg(target_feature="avx512f")] {
359        // AVX512 might have this, unsure for now
360        let arr: [i64; 8] = cast(self);
361        cast(
362          [
363            arr[0].wrapping_abs(),
364            arr[1].wrapping_abs(),
365            arr[2].wrapping_abs(),
366            arr[3].wrapping_abs(),
367            arr[4].wrapping_abs(),
368            arr[5].wrapping_abs(),
369            arr[6].wrapping_abs(),
370            arr[7].wrapping_abs(),
371          ])
372      } else {
373        Self {
374          a : self.a.abs(),
375          b : self.b.abs(),
376        }
377      }
378    }
379  }
380
381  #[inline]
382  pub fn is_positive(self) -> Self {
383    pick! {
384      if #[cfg(all(target_feature="neon", target_arch="aarch64"))] {
385        // `neon` has dedicated greater-than-zero intrinsics.
386        Self {
387          a: self.a.is_positive(),
388          b: self.b.is_positive(),
389        }
390      } else {
391        self.simd_gt(Self::ZERO)
392      }
393    }
394  }
395
396  #[inline]
397  pub fn is_negative(self) -> Self {
398    pick! {
399      if #[cfg(all(target_feature="neon", target_arch="aarch64"))] {
400        // `neon` has dedicated less-than-zero intrinsics.
401        Self {
402          a: self.a.is_negative(),
403          b: self.b.is_negative(),
404        }
405      } else {
406        self.simd_lt(Self::ZERO)
407      }
408    }
409  }
410
411  optional_fn_deserialize {}
412}
413
414/// The following functionality exists only for [`i64x8`], or only for
415/// particular types inconsistently.
416impl i64x8 {
417  /// Converts each element from [`i64`] to [`f64`].
418  #[inline]
419  #[must_use]
420  pub fn round_float(self) -> f64x8 {
421    let arr: [i64; 8] = cast(self);
422    cast([
423      arr[0] as f64,
424      arr[1] as f64,
425      arr[2] as f64,
426      arr[3] as f64,
427      arr[4] as f64,
428      arr[5] as f64,
429      arr[6] as f64,
430      arr[7] as f64,
431    ])
432  }
433}