2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2019-07-13 10:16:57 -05:00
|
|
|
#![allow(non_camel_case_types, incomplete_features)]
|
2018-08-31 08:02:01 -05:00
|
|
|
|
2018-03-16 13:42:42 -05:00
|
|
|
// ignore-emscripten FIXME(#45351) hits an LLVM assert
|
|
|
|
|
2019-07-13 10:16:57 -05:00
|
|
|
#![feature(repr_simd, platform_intrinsics, const_generics)]
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct i32x4(pub i32, pub i32, pub i32, pub i32);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
2019-07-13 10:16:57 -05:00
|
|
|
struct U32<const N: usize>([u32; N]);
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct f32x4(pub f32, pub f32, pub f32, pub f32);
|
|
|
|
|
|
|
|
macro_rules! all_eq {
|
|
|
|
($a: expr, $b: expr) => {{
|
|
|
|
let a = $a;
|
|
|
|
let b = $b;
|
|
|
|
assert!(a.0 == b.0 && a.1 == b.1 && a.2 == b.2 && a.3 == b.3);
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-07-13 10:16:57 -05:00
|
|
|
macro_rules! all_eq_ {
|
|
|
|
($a: expr, $b: expr) => {{
|
|
|
|
let a = $a;
|
|
|
|
let b = $b;
|
|
|
|
assert!(a.0 == b.0);
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-12 23:12:36 -05:00
|
|
|
extern "platform-intrinsic" {
|
|
|
|
fn simd_add<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_sub<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_mul<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_div<T>(x: T, y: T) -> T;
|
2017-11-06 06:37:26 -06:00
|
|
|
fn simd_rem<T>(x: T, y: T) -> T;
|
2015-08-12 23:12:36 -05:00
|
|
|
fn simd_shl<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_shr<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_and<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_or<T>(x: T, y: T) -> T;
|
|
|
|
fn simd_xor<T>(x: T, y: T) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x1 = i32x4(1, 2, 3, 4);
|
2019-07-13 10:16:57 -05:00
|
|
|
let y1 = U32::<4>([1, 2, 3, 4]);
|
2015-08-12 23:12:36 -05:00
|
|
|
let z1 = f32x4(1.0, 2.0, 3.0, 4.0);
|
|
|
|
let x2 = i32x4(2, 3, 4, 5);
|
2019-07-13 10:16:57 -05:00
|
|
|
let y2 = U32::<4>([2, 3, 4, 5]);
|
2015-08-12 23:12:36 -05:00
|
|
|
let z2 = f32x4(2.0, 3.0, 4.0, 5.0);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
all_eq!(simd_add(x1, x2), i32x4(3, 5, 7, 9));
|
|
|
|
all_eq!(simd_add(x2, x1), i32x4(3, 5, 7, 9));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_add(y1, y2), U32::<4>([3, 5, 7, 9]));
|
|
|
|
all_eq_!(simd_add(y2, y1), U32::<4>([3, 5, 7, 9]));
|
2015-08-12 23:12:36 -05:00
|
|
|
all_eq!(simd_add(z1, z2), f32x4(3.0, 5.0, 7.0, 9.0));
|
|
|
|
all_eq!(simd_add(z2, z1), f32x4(3.0, 5.0, 7.0, 9.0));
|
|
|
|
|
|
|
|
all_eq!(simd_mul(x1, x2), i32x4(2, 6, 12, 20));
|
|
|
|
all_eq!(simd_mul(x2, x1), i32x4(2, 6, 12, 20));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_mul(y1, y2), U32::<4>([2, 6, 12, 20]));
|
|
|
|
all_eq_!(simd_mul(y2, y1), U32::<4>([2, 6, 12, 20]));
|
2015-08-12 23:12:36 -05:00
|
|
|
all_eq!(simd_mul(z1, z2), f32x4(2.0, 6.0, 12.0, 20.0));
|
|
|
|
all_eq!(simd_mul(z2, z1), f32x4(2.0, 6.0, 12.0, 20.0));
|
|
|
|
|
|
|
|
all_eq!(simd_sub(x2, x1), i32x4(1, 1, 1, 1));
|
|
|
|
all_eq!(simd_sub(x1, x2), i32x4(-1, -1, -1, -1));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_sub(y2, y1), U32::<4>([1, 1, 1, 1]));
|
|
|
|
all_eq_!(simd_sub(y1, y2), U32::<4>([!0, !0, !0, !0]));
|
2015-08-12 23:12:36 -05:00
|
|
|
all_eq!(simd_sub(z2, z1), f32x4(1.0, 1.0, 1.0, 1.0));
|
|
|
|
all_eq!(simd_sub(z1, z2), f32x4(-1.0, -1.0, -1.0, -1.0));
|
|
|
|
|
2017-11-06 06:37:26 -06:00
|
|
|
all_eq!(simd_div(x1, x1), i32x4(1, 1, 1, 1));
|
|
|
|
all_eq!(simd_div(i32x4(2, 4, 6, 8), i32x4(2, 2, 2, 2)), x1);
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_div(y1, y1), U32::<4>([1, 1, 1, 1]));
|
|
|
|
all_eq_!(simd_div(U32::<4>([2, 4, 6, 8]), U32::<4>([2, 2, 2, 2])), y1);
|
2017-11-06 06:37:26 -06:00
|
|
|
all_eq!(simd_div(z1, z1), f32x4(1.0, 1.0, 1.0, 1.0));
|
2015-08-12 23:12:36 -05:00
|
|
|
all_eq!(simd_div(z1, z2), f32x4(1.0/2.0, 2.0/3.0, 3.0/4.0, 4.0/5.0));
|
|
|
|
all_eq!(simd_div(z2, z1), f32x4(2.0/1.0, 3.0/2.0, 4.0/3.0, 5.0/4.0));
|
|
|
|
|
2017-11-06 06:37:26 -06:00
|
|
|
all_eq!(simd_rem(x1, x1), i32x4(0, 0, 0, 0));
|
|
|
|
all_eq!(simd_rem(x2, x1), i32x4(0, 1, 1, 1));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_rem(y1, y1), U32::<4>([0, 0, 0, 0]));
|
|
|
|
all_eq_!(simd_rem(y2, y1), U32::<4>([0, 1, 1, 1]));
|
2017-11-06 06:37:26 -06:00
|
|
|
all_eq!(simd_rem(z1, z1), f32x4(0.0, 0.0, 0.0, 0.0));
|
|
|
|
all_eq!(simd_rem(z1, z2), z1);
|
|
|
|
all_eq!(simd_rem(z2, z1), f32x4(0.0, 1.0, 1.0, 1.0));
|
|
|
|
|
2015-08-12 23:12:36 -05:00
|
|
|
all_eq!(simd_shl(x1, x2), i32x4(1 << 2, 2 << 3, 3 << 4, 4 << 5));
|
|
|
|
all_eq!(simd_shl(x2, x1), i32x4(2 << 1, 3 << 2, 4 << 3, 5 << 4));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_shl(y1, y2), U32::<4>([1 << 2, 2 << 3, 3 << 4, 4 << 5]));
|
|
|
|
all_eq_!(simd_shl(y2, y1), U32::<4>([2 << 1, 3 << 2, 4 << 3, 5 << 4]));
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
// test right-shift by assuming left-shift is correct
|
|
|
|
all_eq!(simd_shr(simd_shl(x1, x2), x2), x1);
|
|
|
|
all_eq!(simd_shr(simd_shl(x2, x1), x1), x2);
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_shr(simd_shl(y1, y2), y2), y1);
|
|
|
|
all_eq_!(simd_shr(simd_shl(y2, y1), y1), y2);
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
// ensure we get logical vs. arithmetic shifts correct
|
|
|
|
let (a, b, c, d) = (-12, -123, -1234, -12345);
|
|
|
|
all_eq!(simd_shr(i32x4(a, b, c, d), x1), i32x4(a >> 1, b >> 2, c >> 3, d >> 4));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_shr(U32::<4>([a as u32, b as u32, c as u32, d as u32]), y1),
|
|
|
|
U32::<4>([(a as u32) >> 1, (b as u32) >> 2, (c as u32) >> 3, (d as u32) >> 4]));
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
all_eq!(simd_and(x1, x2), i32x4(0, 2, 0, 4));
|
|
|
|
all_eq!(simd_and(x2, x1), i32x4(0, 2, 0, 4));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_and(y1, y2), U32::<4>([0, 2, 0, 4]));
|
|
|
|
all_eq_!(simd_and(y2, y1), U32::<4>([0, 2, 0, 4]));
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
all_eq!(simd_or(x1, x2), i32x4(3, 3, 7, 5));
|
|
|
|
all_eq!(simd_or(x2, x1), i32x4(3, 3, 7, 5));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_or(y1, y2), U32::<4>([3, 3, 7, 5]));
|
|
|
|
all_eq_!(simd_or(y2, y1), U32::<4>([3, 3, 7, 5]));
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
all_eq!(simd_xor(x1, x2), i32x4(3, 1, 7, 1));
|
|
|
|
all_eq!(simd_xor(x2, x1), i32x4(3, 1, 7, 1));
|
2019-07-13 10:16:57 -05:00
|
|
|
all_eq_!(simd_xor(y1, y2), U32::<4>([3, 1, 7, 1]));
|
|
|
|
all_eq_!(simd_xor(y2, y1), U32::<4>([3, 1, 7, 1]));
|
2015-08-12 23:12:36 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|