2018-03-13 10:46:55 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-03-15 04:10:16 -05:00
|
|
|
// min-llvm-version 5.0
|
|
|
|
|
2018-03-13 10:46:55 -05:00
|
|
|
// Test that the simd_reduce_{op} intrinsics produce the correct results.
|
|
|
|
|
|
|
|
#![feature(repr_simd, platform_intrinsics)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct i32x4(pub i32, pub i32, pub i32, pub i32);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct u32x4(pub u32, pub u32, pub u32, pub u32);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct f32x4(pub f32, pub f32, pub f32, pub f32);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct b8x4(pub i8, pub i8, pub i8, pub i8);
|
|
|
|
|
|
|
|
#[repr(simd)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct b8x16(
|
|
|
|
pub i8, pub i8, pub i8, pub i8,
|
|
|
|
pub i8, pub i8, pub i8, pub i8,
|
|
|
|
pub i8, pub i8, pub i8, pub i8,
|
|
|
|
pub i8, pub i8, pub i8, pub i8
|
|
|
|
);
|
|
|
|
|
|
|
|
extern "platform-intrinsic" {
|
2018-03-14 11:22:40 -05:00
|
|
|
fn simd_reduce_add_unordered<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_mul_unordered<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_add_ordered<T, U>(x: T, acc: U) -> U;
|
|
|
|
fn simd_reduce_mul_ordered<T, U>(x: T, acc: U) -> U;
|
2018-03-13 10:46:55 -05:00
|
|
|
fn simd_reduce_min<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_max<T, U>(x: T) -> U;
|
2018-03-14 11:22:40 -05:00
|
|
|
fn simd_reduce_min_nanless<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_max_nanless<T, U>(x: T) -> U;
|
2018-03-13 10:46:55 -05:00
|
|
|
fn simd_reduce_and<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_or<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_xor<T, U>(x: T) -> U;
|
|
|
|
fn simd_reduce_all<T>(x: T) -> bool;
|
|
|
|
fn simd_reduce_any<T>(x: T) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
let x = i32x4(1, -2, 3, 4);
|
2018-03-14 11:22:40 -05:00
|
|
|
let r: i32 = simd_reduce_add_unordered(x);
|
|
|
|
assert_eq!(r, 6_i32);
|
|
|
|
let r: i32 = simd_reduce_mul_unordered(x);
|
|
|
|
assert_eq!(r, -24_i32);
|
|
|
|
let r: i32 = simd_reduce_add_ordered(x, -1);
|
|
|
|
assert_eq!(r, 5_i32);
|
|
|
|
let r: i32 = simd_reduce_mul_ordered(x, -1);
|
|
|
|
assert_eq!(r, 24_i32);
|
|
|
|
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_min(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -2_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_max(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 4_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let x = i32x4(-1, -1, -1, -1);
|
|
|
|
let r: i32 = simd_reduce_and(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -1_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_or(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -1_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_xor(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 0_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let x = i32x4(-1, -1, 0, -1);
|
|
|
|
let r: i32 = simd_reduce_and(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 0_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_or(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -1_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: i32 = simd_reduce_xor(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -1_i32);
|
2018-03-13 10:46:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let x = u32x4(1, 2, 3, 4);
|
2018-03-14 11:22:40 -05:00
|
|
|
let r: u32 = simd_reduce_add_unordered(x);
|
|
|
|
assert_eq!(r, 10_u32);
|
|
|
|
let r: u32 = simd_reduce_mul_unordered(x);
|
|
|
|
assert_eq!(r, 24_u32);
|
|
|
|
let r: u32 = simd_reduce_add_ordered(x, 1);
|
|
|
|
assert_eq!(r, 11_u32);
|
|
|
|
let r: u32 = simd_reduce_mul_ordered(x, 2);
|
|
|
|
assert_eq!(r, 48_u32);
|
|
|
|
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_min(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 1_u32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_max(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 4_u32);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let t = u32::max_value();
|
|
|
|
let x = u32x4(t, t, t, t);
|
|
|
|
let r: u32 = simd_reduce_and(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, t);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_or(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, t);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_xor(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 0_u32);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let x = u32x4(t, t, 0, t);
|
|
|
|
let r: u32 = simd_reduce_and(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 0_u32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_or(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, t);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: u32 = simd_reduce_xor(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, t);
|
2018-03-13 10:46:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let x = f32x4(1., -2., 3., 4.);
|
2018-03-14 11:22:40 -05:00
|
|
|
let r: f32 = simd_reduce_add_unordered(x);
|
|
|
|
assert_eq!(r, 6_f32);
|
|
|
|
let r: f32 = simd_reduce_mul_unordered(x);
|
|
|
|
assert_eq!(r, -24_f32);
|
|
|
|
// FIXME: only works correctly for accumulator, 0:
|
|
|
|
// https://bugs.llvm.org/show_bug.cgi?id=36734
|
|
|
|
let r: f32 = simd_reduce_add_ordered(x, 0.);
|
|
|
|
assert_eq!(r, 6_f32);
|
|
|
|
// FIXME: only works correctly for accumulator, 1:
|
|
|
|
// https://bugs.llvm.org/show_bug.cgi?id=36734
|
|
|
|
let r: f32 = simd_reduce_mul_ordered(x, 1.);
|
|
|
|
assert_eq!(r, -24_f32);
|
|
|
|
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: f32 = simd_reduce_min(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, -2_f32);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: f32 = simd_reduce_max(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, 4_f32);
|
|
|
|
let r: f32 = simd_reduce_min_nanless(x);
|
|
|
|
assert_eq!(r, -2_f32);
|
|
|
|
let r: f32 = simd_reduce_max_nanless(x);
|
|
|
|
assert_eq!(r, 4_f32);
|
2018-03-13 10:46:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let x = b8x4(!0, !0, !0, !0);
|
|
|
|
let r: bool = simd_reduce_all(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, true);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: bool = simd_reduce_any(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, true);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let x = b8x4(!0, !0, 0, !0);
|
|
|
|
let r: bool = simd_reduce_all(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, false);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: bool = simd_reduce_any(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, true);
|
2018-03-13 10:46:55 -05:00
|
|
|
|
|
|
|
let x = b8x4(0, 0, 0, 0);
|
|
|
|
let r: bool = simd_reduce_all(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, false);
|
2018-03-13 10:46:55 -05:00
|
|
|
let r: bool = simd_reduce_any(x);
|
2018-03-14 11:22:40 -05:00
|
|
|
assert_eq!(r, false);
|
2018-03-13 10:46:55 -05:00
|
|
|
}
|
|
|
|
}
|