File: simd-bitmask.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 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; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (73 lines) | stat: -rw-r--r-- 2,280 bytes parent folder | download | duplicates (3)
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
//@run-pass
#![feature(repr_simd, intrinsics)]

extern "rust-intrinsic" {
    fn simd_bitmask<T, U>(v: T) -> U;
    fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
}

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

fn main() {
    unsafe {
        let v = Simd::<i8, 4>([-1, 0, -1, 0]);
        let i: u8 = simd_bitmask(v);
        let a: [u8; 1] = simd_bitmask(v);

        if cfg!(target_endian = "little") {
            assert_eq!(i, 0b0101);
            assert_eq!(a, [0b0101]);
        } else {
            assert_eq!(i, 0b1010);
            assert_eq!(a, [0b1010]);
        }

        let v = Simd::<i8, 16>([0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0]);
        let i: u16 = simd_bitmask(v);
        let a: [u8; 2] = simd_bitmask(v);

        if cfg!(target_endian = "little") {
            assert_eq!(i, 0b0101000000001100);
            assert_eq!(a, [0b00001100, 0b01010000]);
        } else {
            assert_eq!(i, 0b0011000000001010);
            assert_eq!(a, [0b00110000, 0b00001010]);
        }
    }

    unsafe {
        let a = Simd::<i32, 4>([0, 1, 2, 3]);
        let b = Simd::<i32, 4>([8, 9, 10, 11]);
        let e = [0, 9, 2, 11];

        let mask = if cfg!(target_endian = "little") { 0b0101u8 } else { 0b1010u8 };
        let r = simd_select_bitmask(mask, a, b);
        assert_eq!(r.0, e);

        let mask = if cfg!(target_endian = "little") { [0b0101u8] } else { [0b1010u8] };
        let r = simd_select_bitmask(mask, a, b);
        assert_eq!(r.0, e);

        let a = Simd::<i32, 16>([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
        let b = Simd::<i32, 16>([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]);
        let e = [16, 17, 2, 3, 20, 21, 22, 23, 24, 25, 26, 27, 12, 29, 14, 31];

        let mask = if cfg!(target_endian = "little") {
            0b0101000000001100u16
        } else {
            0b0011000000001010u16
        };
        let r = simd_select_bitmask(mask, a, b);
        assert_eq!(r.0, e);

        let mask = if cfg!(target_endian = "little") {
            [0b00001100u8, 0b01010000u8]
        } else {
            [0b00110000u8, 0b00001010u8]
        };
        let r = simd_select_bitmask(mask, a, b);
        assert_eq!(r.0, e);
    }
}