File: monomorphize-shuffle-index.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (58 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download | duplicates (2)
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
//@[old]run-pass
//@[generic_with_fn]run-pass
//@ revisions: old generic generic_with_fn
#![feature(repr_simd, intrinsics, adt_const_params, unsized_const_params, generic_const_exprs)]
#![allow(incomplete_features)]

extern "rust-intrinsic" {
    #[cfg(old)]
    fn simd_shuffle<T, I, U>(a: T, b: T, i: I) -> U;
    #[cfg(any(generic, generic_with_fn))]
    fn simd_shuffle_generic<T, U, const I: &'static [u32]>(a: T, b: T) -> U;
}

#[derive(Copy, Clone)]
#[repr(simd)]
struct Simd<T, const N: usize>([T; N]);

trait Shuffle<const N: usize> {
    const I: Simd<u32, N>;
    const J: &'static [u32] = &Self::I.0;

    unsafe fn shuffle<T, const M: usize>(&self, a: Simd<T, M>, b: Simd<T, M>) -> Simd<T, N>
    where
        Thing<{ Self::J }>:,
    {
        #[cfg(old)]
        return simd_shuffle(a, b, Self::I);
        #[cfg(generic)]
        return simd_shuffle_generic::<_, _, { &Self::I.0 }>(a, b);
        //[generic]~^ overly complex generic constant
        #[cfg(generic_with_fn)]
        return simd_shuffle_generic::<_, _, { Self::J }>(a, b);
    }
}

struct Thing<const X: &'static [u32]>;

fn main() {
    struct I1;
    impl Shuffle<4> for I1 {
        const I: Simd<u32, 4> = Simd([0, 2, 4, 6]);
    }

    struct I2;
    impl Shuffle<2> for I2 {
        const I: Simd<u32, 2> = Simd([1, 5]);
    }

    let a = Simd::<u8, 4>([0, 1, 2, 3]);
    let b = Simd::<u8, 4>([4, 5, 6, 7]);
    unsafe {
        let x: Simd<u8, 4> = I1.shuffle(a, b);
        assert_eq!(x.0, [0, 2, 4, 6]);

        let y: Simd<u8, 2> = I2.shuffle(a, b);
        assert_eq!(y.0, [1, 5]);
    }
}