2021-09-11 13:00:48 -05:00
|
|
|
//@run-pass
|
2024-02-23 11:36:24 -06:00
|
|
|
#![feature(repr_simd, intrinsics)]
|
2021-09-11 13:00:48 -05:00
|
|
|
|
2024-02-23 11:36:24 -06:00
|
|
|
extern "rust-intrinsic" {
|
2021-09-11 13:00:48 -05:00
|
|
|
fn simd_bitmask<T, U>(v: T) -> U;
|
2021-09-11 21:37:23 -05:00
|
|
|
fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
|
2021-09-11 13:00:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
if cfg!(target_endian = "little") {
|
|
|
|
assert_eq!(i, 0b0101);
|
|
|
|
assert_eq!(a, [0b0101]);
|
|
|
|
} else {
|
|
|
|
assert_eq!(i, 0b1010);
|
|
|
|
assert_eq!(a, [0b1010]);
|
|
|
|
}
|
2021-09-11 13:00:48 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
if cfg!(target_endian = "little") {
|
|
|
|
assert_eq!(i, 0b0101000000001100);
|
|
|
|
assert_eq!(a, [0b00001100, 0b01010000]);
|
|
|
|
} else {
|
|
|
|
assert_eq!(i, 0b0011000000001010);
|
|
|
|
assert_eq!(a, [0b00110000, 0b00001010]);
|
|
|
|
}
|
2021-09-11 13:00:48 -05:00
|
|
|
}
|
2021-09-11 21:37:23 -05:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-09 03:29:11 -05:00
|
|
|
let a = Simd::<i32, 4>([0, 1, 2, 3]);
|
|
|
|
let b = Simd::<i32, 4>([8, 9, 10, 11]);
|
|
|
|
let e = [0, 9, 2, 11];
|
2021-09-11 21:37:23 -05:00
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
let mask = if cfg!(target_endian = "little") { 0b0101u8 } else { 0b1010u8 };
|
|
|
|
let r = simd_select_bitmask(mask, a, b);
|
2021-09-11 21:37:23 -05:00
|
|
|
assert_eq!(r.0, e);
|
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
let mask = if cfg!(target_endian = "little") { [0b0101u8] } else { [0b1010u8] };
|
|
|
|
let r = simd_select_bitmask(mask, a, b);
|
2021-09-11 21:37:23 -05:00
|
|
|
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];
|
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
let mask = if cfg!(target_endian = "little") {
|
|
|
|
0b0101000000001100u16
|
|
|
|
} else {
|
|
|
|
0b0011000000001010u16
|
|
|
|
};
|
|
|
|
let r = simd_select_bitmask(mask, a, b);
|
2021-09-11 21:37:23 -05:00
|
|
|
assert_eq!(r.0, e);
|
|
|
|
|
2024-06-09 03:29:11 -05:00
|
|
|
let mask = if cfg!(target_endian = "little") {
|
|
|
|
[0b00001100u8, 0b01010000u8]
|
|
|
|
} else {
|
|
|
|
[0b00110000u8, 0b00001010u8]
|
|
|
|
};
|
|
|
|
let r = simd_select_bitmask(mask, a, b);
|
2021-09-11 21:37:23 -05:00
|
|
|
assert_eq!(r.0, e);
|
|
|
|
}
|
2021-09-11 13:00:48 -05:00
|
|
|
}
|