simply-simd 0.2.0

No-boilerplate portable SIMD library for raw registers with runtime feature detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use std::fmt;
use std::marker::PhantomData;
use std::ops::*;

use num_traits::NumCast;

use crate::SimdElement;
use crate::architectures::interface::*;
use crate::simd_array::SimdToArray;
use crate::mask::Mask;
use crate::register::Simd;
use crate::simd_types::{B32, B64, BitSize, SimdType};

impl<T: SimdElement, A: Arch> Simd<T, A> {
    #[inline(always)]
    pub(crate) fn new(data: A::Vec) -> Self {
        Self {
            data,
            _marker: PhantomData,
        }
    }

    #[inline(always)]
    pub fn zero() -> Self {
        unsafe { Self::new(A::Vec::zero()) }
    }

    #[inline(always)]
    pub fn from_aligned_slice(slice: &[T]) -> Self {
        let ptr = slice.as_ptr();
        assert!(ptr.align_offset(Self::SIMD_WIDTH) == 0);
        assert!(slice.len() >= Self::LANES);
        unsafe { Self::new(A::Vec::load_aligned(ptr)) }
    }

    /// # Safety
    /// Does not check if the slice goes out of bounds.
    #[inline(always)]
    pub unsafe fn from_aligned_slice_unchecked(slice: &[T]) -> Self {
        let ptr = slice.as_ptr();
        debug_assert!(ptr.align_offset(Self::SIMD_WIDTH) == 0);
        debug_assert!(slice.len() >= Self::LANES);
        unsafe { Self::new(A::Vec::load_aligned(ptr)) }
    }

    #[inline(always)]
    pub fn from_slice(slice: &[T]) -> Self {
        if slice.len() >= Self::LANES {
            unsafe { Self::new(A::Vec::load_unaligned(slice.as_ptr())) }
        } else {
            let mut array = Self::zero().to_array();
            for (arr, val) in array.iter_mut().zip(slice.iter()) {
                *arr = *val;
            }
            unsafe { Self::from_slice_unchecked(array.as_slice()) }
        }
    }

    /// # Safety
    /// Requires allocated memory to be behind (left) of the slice.
    /// Bounds are not checked.
    /// Length of the slice must be less than or equal to the number of lanes.
    #[inline(always)]
    pub unsafe fn from_slice_partial(slice: &[T]) -> Self {
        debug_assert!(slice.len() <= Self::LANES);
        unsafe {
            let offset = Self::LANES - slice.len();
            let raw_ptr = slice.as_ptr().sub(offset);
            let simd = Self::new(A::Vec::load_unaligned(raw_ptr));
            simd.left_lane_shift(offset as u32)
        }
    }

    /// # Safety
    /// Does not check if the slice goes out of bounds.
    #[inline(always)]
    pub unsafe fn from_slice_unchecked(slice: &[T]) -> Self {
        debug_assert!(slice.len() >= Self::LANES);
        unsafe { Self::new(A::Vec::load_unaligned(slice.as_ptr())) }
    }

    #[inline(always)]
    pub fn copy_to_aligned_slice(self, slice: &mut [T]) {
        let ptr = slice.as_mut_ptr();
        assert!(ptr.align_offset(Self::SIMD_WIDTH) == 0);
        assert!(slice.len() >= Self::LANES);
        unsafe { self.data.store_aligned(ptr) };
    }

    /// # Safety
    /// Does not check if the slice goes out of bounds.
    #[inline(always)]
    pub unsafe fn copy_to_aligned_slice_unchecked(self, slice: &mut [T]) {
        let ptr = slice.as_mut_ptr();
        debug_assert!(ptr.align_offset(Self::SIMD_WIDTH) == 0);
        debug_assert!(slice.len() >= Self::LANES);
        unsafe { self.data.store_aligned(ptr) };
    }

    #[inline(always)]
    pub fn copy_to_slice(self, slice: &mut [T]) {
        if slice.len() >= Self::LANES {
            let ptr = slice.as_mut_ptr();
            unsafe { self.data.store_unaligned(ptr) };
        } else {
            // Scalar/tail case.
            let array = self.to_array();
            slice
                .iter_mut()
                .zip(array.iter())
                .for_each(|(src, new)| *src = *new);
        }
    }

    /// # Safety
    /// Does not check if the slice goes out of bounds.
    #[inline(always)]
    pub unsafe fn copy_to_slice_unchecked(self, slice: &mut [T]) {
        let ptr = slice.as_mut_ptr();
        debug_assert!(slice.len() >= Self::LANES);
        unsafe { self.data.store_unaligned(ptr) };
    }

    /// Converts the Simd register into an array.
    ///
    /// # Example
    ///
    /// TODO
    /// use simply_simd::
    #[inline(always)]
    pub fn to_array(self) -> T::Array<A> {
        let mut array = T::Array::<A>::from_fn(|_| T::from(0).unwrap());
        self.copy_to_slice(array.as_mut_slice());
        array
    }

    #[inline(always)]
    pub fn iota(offset: T) -> Self {
        let iota_array =
            T::Array::<A>::from_fn(|i| <T as NumCast>::from(i).unwrap().safe_add(offset));
        Self::from_slice(iota_array.as_slice())
    }
}

impl<T: SimdElement, A: Arch> fmt::Debug for Simd<T, A> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let buf = self.to_array();
        write!(f, "{:?}", buf)
    }
}

// === Assign operations ===
impl<T: SimdElement, A: Arch> AddAssign for Simd<T, A>
where
    Self: Add<Output = Self> + Copy,
{
    #[inline(always)]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<T: SimdElement, A: Arch> SubAssign for Simd<T, A>
where
    Self: Sub<Output = Self> + Copy,
{
    #[inline(always)]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<T: SimdElement, A: Arch> MulAssign for Simd<T, A>
where
    Self: Mul<Output = Self> + Copy,
{
    #[inline(always)]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<T: SimdElement, A: Arch> DivAssign for Simd<T, A>
where
    Self: Div<Output = Self> + Copy,
{
    #[inline(always)]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<T: SimdElement, A: Arch> RemAssign for Simd<T, A>
where
    Self: Rem<Output = Self> + Copy,
{
    #[inline(always)]
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}

impl<T: SimdElement, A: Arch> BitAndAssign for Simd<T, A>
where
    Self: BitAnd<Output = Self> + Copy,
{
    #[inline(always)]
    fn bitand_assign(&mut self, rhs: Self) {
        *self = *self & rhs;
    }
}

impl<T: SimdElement, A: Arch> BitOrAssign for Simd<T, A>
where
    Self: BitOr<Output = Self> + Copy,
{
    #[inline(always)]
    fn bitor_assign(&mut self, rhs: Self) {
        *self = *self | rhs;
    }
}

impl<T: SimdElement, A: Arch> BitXorAssign for Simd<T, A>
where
    Self: BitXor<Output = Self> + Copy,
{
    #[inline(always)]
    fn bitxor_assign(&mut self, rhs: Self) {
        *self = *self ^ rhs;
    }
}

impl<T: SimdElement, A: Arch> Neg for Simd<T, A> {
    type Output = Self;
    #[inline(always)]
    fn neg(self) -> Self {
        Self::zero() - self
    }
}

impl<T: SimdElement, A: Arch> Simd<T, A> {
    #[inline(always)]
    pub fn raw_cast<S: SimdElement>(self) -> Simd<S, A> {
        Simd::new(self.data)
    }
}

impl<T: SimdElement, A: Arch> Default for Simd<T, A> {
    #[inline(always)]
    fn default() -> Self {
        Self::splat(<T as NumCast>::from(T::default()).unwrap())
    }
}

impl<T: SimdElement, A: Arch> Add for Simd<T, A> {
    type Output = Self;
    #[inline(always)]
    fn add(self, rhs: Self) -> Self {
        unsafe {
            Self::new(match T::TYPE {
                SimdType::F64 => self.data.f64_add(rhs.data),
                SimdType::F32 => self.data.f32_add(rhs.data),
                SimdType::I64 => self.data.i64_add(rhs.data),
                SimdType::I32 => self.data.i32_add(rhs.data),
                SimdType::I16 => self.data.i16_add(rhs.data),
                SimdType::I8 => self.data.i8_add(rhs.data),
                SimdType::U64 => self.data.i64_add(rhs.data),
                SimdType::U32 => self.data.i32_add(rhs.data),
                SimdType::U16 => self.data.i16_add(rhs.data),
                SimdType::U8 => self.data.i8_add(rhs.data),
            })
        }
    }
}

impl<T: SimdElement, A: Arch> Sub for Simd<T, A> {
    type Output = Self;
    #[inline(always)]
    fn sub(self, rhs: Self) -> Self {
        unsafe {
            Self::new(match T::TYPE {
                SimdType::F64 => self.data.f64_sub(rhs.data),
                SimdType::F32 => self.data.f32_sub(rhs.data),
                SimdType::I64 => self.data.i64_sub(rhs.data),
                SimdType::I32 => self.data.i32_sub(rhs.data),
                SimdType::I16 => self.data.i16_sub(rhs.data),
                SimdType::I8 => self.data.i8_sub(rhs.data),
                SimdType::U64 => self.data.i64_sub(rhs.data),
                SimdType::U32 => self.data.i32_sub(rhs.data),
                SimdType::U16 => self.data.i16_sub(rhs.data),
                SimdType::U8 => self.data.i8_sub(rhs.data),
            })
        }
    }
}


impl<T: SimdElement, A: Arch> Simd<T, A> {
    /// Clamps the values in a register between two bounds, inclusive.
    ///
    /// # Parameters:
    /// - `lower_bound`: Minimum value after clamping
    /// - `upper_bound`: Maximum value after clamping
    #[inline(always)]
    pub fn clamp(self, lower_bound: Self, upper_bound: Self) -> Self {
        self.min(upper_bound).max(lower_bound)
    }

    /// Shifts bytes N to the left within 128-bit blocks.
    #[inline(always)]
    pub fn block_left_byte_shift<const N: i32>(self) -> Self {
        unsafe { Self::new(self.data.block_left_byte_shift::<N>()) }
    }

    /// Shifts bytes N to the right within 128-bit blocks.
    #[inline(always)]
    pub fn block_right_byte_shift<const N: i32>(self) -> Self {
        unsafe { Self::new(self.data.block_right_byte_shift::<N>()) }
    }

    /// Blends two registers using an immediate mask.
    #[inline(always)]
    pub fn blend<const N: i32>(self, false_values: Self) -> Self {
        unsafe {
            match T::BIT_SIZE {
                BitSize::Size64 => Self::new(self.data.blend_32::<N>(false_values.data)),
                BitSize::Size32 => Self::new(self.data.blend_32::<N>(false_values.data)),
                _ => unreachable!(), // TODO: Add 16 and 8 for immediate blend.
            }
        }
    }
}

impl<T: SimdElement, A: Arch> Simd<T, A> {
    /// Broadcasts a value across the entire register.
    #[inline(always)]
    pub fn splat(val: T) -> Self {
        unsafe {
            Self::new(match T::BIT_SIZE {
                BitSize::Size64 => A::Vec::splat_64(val),
                BitSize::Size32 => A::Vec::splat_32(val),
                BitSize::Size16 => A::Vec::splat_16(val),
                BitSize::Size8 => A::Vec::splat_8(val),
            })
        }
    }

    /// Loads a register according to a mask.
    #[inline(always)]
    pub fn masked_load(slice: &[T], mask: Mask<T, A>) -> Self {
        unsafe {
            Self::new(match T::BIT_SIZE {
                BitSize::Size64 => A::Vec::masked_load_64(slice.as_ptr(), mask.data),
                BitSize::Size32 => A::Vec::masked_load_32(slice.as_ptr(), mask.data),
                _ => unreachable!(),
            })
        }
    }

    /// Loads only the first `amount` elements into the register.
    pub fn partial_load(slice: &[T], amount: usize) -> Self {
        assert!(slice.len() >= Self::LANES, "Attempted to do a partial load, but index is out of bounds!");
        let amount_vec = Self::splat(<T as NumCast>::from(amount).unwrap());
        let mask = Self::iota(<T as NumCast>::from(0).unwrap()).simd_lt(amount_vec);
        Self::masked_load(slice, mask)
    }

    /// Loads only the first `amount` elements into the register.
    ///
    /// # Safety
    /// - Slice must be greater than or equal to `ArchSimd::<T>::LANES`
    pub unsafe fn partial_load_unchecked(slice: &[T], amount: usize) -> Self {
        debug_assert!(slice.len() >= Self::LANES, "Index is out of bounds in unsafe code!");
        let amount_vec = Self::splat(<T as NumCast>::from(amount).unwrap());
        let mask = Self::iota(<T as NumCast>::from(0).unwrap()).simd_lt(amount_vec);
        Self::masked_load(slice, mask)
    }

    /// Stores the register using a given mask.
    #[inline(always)]
    pub fn masked_store(self, slice: &mut [T], mask: Mask<T, A>) {
        unsafe {
            match T::BIT_SIZE {
                BitSize::Size64 => {
                    A::Vec::masked_store_64(self.data, slice.as_mut_ptr(), mask.data)
                }
                BitSize::Size32 => {
                    A::Vec::masked_store_32(self.data, slice.as_mut_ptr(), mask.data)
                }
                _ => unreachable!(), // TODO: ADD SUPPORT AOR OTHER SIZES!!!
            }
        }
    }

    /// Stores only the first `amount` elements into the register.
    pub fn partial_store(self, slice: &mut [T], amount: usize) {
        match T::BIT_SIZE {
            BitSize::Size64 => {
                let iota = Simd::iota(0u64);
                let n_vec = Simd::splat(amount as u64);
                let mask = n_vec.simd_gt(iota);
                Self::masked_store(self, slice, mask.raw_cast());
            }
            BitSize::Size32 => {
                let iota = Simd::iota(0u32);
                let n_vec = Simd::splat(amount as u32);
                let mask = n_vec.simd_gt(iota);
                Self::masked_store(self, slice, mask.raw_cast());
            }
            _ => unreachable!(),
        }
    }

    #[inline(always)]
    pub fn simd_eq(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_eq(rhs.data),
                SimdType::F32 => self.data.cmp_f32_eq(rhs.data),
                SimdType::U64 => self.data.cmp_i64_eq(rhs.data),
                SimdType::U32 => self.data.cmp_i32_eq(rhs.data),
                SimdType::U16 => self.data.cmp_i16_eq(rhs.data),
                SimdType::U8 => self.data.cmp_i8_eq(rhs.data),
                SimdType::I64 => self.data.cmp_i64_eq(rhs.data),
                SimdType::I32 => self.data.cmp_i32_eq(rhs.data),
                SimdType::I16 => self.data.cmp_i16_eq(rhs.data),
                SimdType::I8 => self.data.cmp_i8_eq(rhs.data),
                // _ => unreachable!() // TODO: Add integer types .
            })
        }
    }

    #[inline(always)]
    pub fn simd_ne(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_neq(rhs.data),
                SimdType::F32 => self.data.cmp_f32_neq(rhs.data),
                _ => self.simd_eq(rhs).data,
            })
        }
    }

    #[inline(always)]
    pub fn simd_gt(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_gt(rhs.data),
                SimdType::F32 => self.data.cmp_f32_gt(rhs.data),
                SimdType::U64 => self.data.cmp_i64_gt(rhs.data),
                SimdType::U32 => self.data.cmp_i32_gt(rhs.data),
                SimdType::U16 => self.data.cmp_i16_gt(rhs.data),
                SimdType::U8 => self.data.cmp_i8_gt(rhs.data),
                SimdType::I64 => self.data.cmp_i64_gt(rhs.data),
                SimdType::I32 => self.data.cmp_i32_gt(rhs.data),
                SimdType::I16 => self.data.cmp_i16_gt(rhs.data),
                SimdType::I8 => self.data.cmp_i8_gt(rhs.data),
                // _ => unreachable!() // TODO: Add integer types .
            })
        }
    }

    // TODO: Aind better way to handle comparisons.
    #[inline(always)]
    pub fn simd_ge(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_ge(rhs.data),
                SimdType::F32 => self.data.cmp_f32_ge(rhs.data),
                // SimdType::U64 => self.data.cmp_i64_gt(rhs.data).or(self.data.cmp_i64_eq(rhs.data)),
                // SimdType::U32 => self.data.cmp_i32_gt(rhs.data).or(self.data.cmp_i32_eq(rhs.data)),
                // SimdType::U16 => self.data.cmp_i16_gt(rhs.data).or(self.data.cmp_i16_eq(rhs.data)),
                // SimdType::U8 => self.data.cmp_i8_gt(rhs.data).or(self.data.cmp_i8_eq(rhs.data)),
                SimdType::I64 => self
                    .data
                    .cmp_i64_gt(rhs.data)
                    .or(self.data.cmp_i64_eq(rhs.data)),
                SimdType::I32 => self
                    .data
                    .cmp_i32_gt(rhs.data)
                    .or(self.data.cmp_i32_eq(rhs.data)),
                SimdType::I16 => self
                    .data
                    .cmp_i16_gt(rhs.data)
                    .or(self.data.cmp_i16_eq(rhs.data)),
                SimdType::I8 => self
                    .data
                    .cmp_i8_gt(rhs.data)
                    .or(self.data.cmp_i8_eq(rhs.data)),
                _ => panic!("Unsigned integer types for less than not implemented!"), // TODO: Add integer types .
            })
        }
    }

    #[inline(always)]
    pub fn simd_lt(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_lt(rhs.data),
                SimdType::F32 => self.data.cmp_f32_lt(rhs.data),
                _ => panic!("Less than for integers not implemented!"), // TODO: Add integer types .
            })
        }
    }

    #[inline(always)]
    pub fn simd_le(self, rhs: Self) -> Mask<T, A> {
        unsafe {
            Mask::new(match T::TYPE {
                SimdType::F64 => self.data.cmp_f64_le(rhs.data),
                SimdType::F32 => self.data.cmp_f32_le(rhs.data),
                _ => panic!("Less than or equal not implemented for integers!"), // TODO: Add integer types .
            })
        }
    }

    // TODO: Handle max for U64/I64.
    pub fn max(self, rhs: Self) -> Self {
        unsafe {
            Self::new(match T::TYPE {
                SimdType::F64 => self.data.max_f64(rhs.data),
                SimdType::F32 => self.data.max_f32(rhs.data),
                SimdType::I32 => self.data.max_i32(rhs.data),
                SimdType::I16 => self.data.max_i16(rhs.data),
                SimdType::I8 => self.data.max_i8(rhs.data),
                SimdType::U32 => self.data.max_u32(rhs.data),
                SimdType::U16 => self.data.max_u16(rhs.data),
                SimdType::U8 => self.data.max_u8(rhs.data),
                _ => panic!("Max for U64/I64 not implemented!"),
            })
        }
    }

    pub fn min(self, rhs: Self) -> Self {
        unsafe {
            Self::new(match T::TYPE {
                SimdType::F64 => self.data.min_f64(rhs.data),
                SimdType::F32 => self.data.min_f32(rhs.data),
                SimdType::I32 => self.data.min_i32(rhs.data),
                SimdType::I16 => self.data.min_i16(rhs.data),
                SimdType::I8 => self.data.min_i8(rhs.data),
                SimdType::U32 => self.data.min_u32(rhs.data),
                SimdType::U16 => self.data.min_u16(rhs.data),
                SimdType::U8 => self.data.min_u8(rhs.data),
                _ => panic!("Min for U64/I64 not implemented!"),
            })
        }
    }
}

impl<T: SimdElement + SimdElement<BitWidthType = B32>, A: Arch> Simd<T, A> {
    #[inline(always)]
    pub fn permute_32(self, indices: Simd<u32, A>) -> Self {
        unsafe { Self::new(self.data.permute_32(indices.data)) }
    }
}

impl<T: SimdElement, A: Arch> Simd<T, A> {
    #[inline(always)]
    pub fn permute_8(self, indices: Simd<u8, A>) -> Self {
        unsafe { Self::new(self.data.permute_8(indices.data)) }
    }

    #[inline(always)]
    pub fn permute_8_pattern_32(self, indices: [u8; 4]) -> Self {
        let pattern = u32::from_ne_bytes(indices);
        let pattern_vec = Simd::<u32, A>::splat(pattern);
        unsafe { Self::new(self.data.permute_8(pattern_vec.data)) }
    }
}

// TODO: Super early version gather.
impl<A: Arch> Simd<u32, A> {
    pub fn gather<S: SimdElement + SimdElement<BitWidthType = B32>, const N: usize>(
        self,
        slice: &[S; N],
    ) -> Simd<S, A> {
        if N <= Self::LANES {
            let data = Simd::<S, A>::from_slice(&slice[..]);
            data.permute_32(self)
        } else {
            unsafe { Simd::new(self.data.gather_32_from_32::<S, 4>(slice.as_ptr())) }
        }
    }
}

impl<A: Arch> Simd<u64, A> {
    pub fn gather<S: SimdElement + SimdElement<BitWidthType = B64>, const N: usize>(
        self,
        slice: &[S; N],
    ) -> Simd<S, A> {
        unsafe { Simd::new(self.data.gather_64_from_64::<S, 8>(slice.as_ptr())) }
    }
}

// TODO: Add other types of lane shifts.
impl<T: SimdElement, A: Arch> Simd<T, A> {
    pub fn left_lane_shift(self, n: u32) -> Self {
        match T::BIT_SIZE {
            BitSize::Size32 => unsafe { Self::new(self.data.left_lane_shift_32(n)) },
            _ => unreachable!(),
        }
    }
    pub fn right_lane_shift(self, n: u32) -> Self {
        match T::BIT_SIZE {
            BitSize::Size32 => unsafe { Self::new(self.data.right_lane_shift_32(n)) },
            _ => unreachable!(),
        }
    }
}