|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// ignore-emscripten |
| 12 | +// min-llvm-version 6.0 |
| 13 | + |
| 14 | +// Test that the simd_{gather,scatter} intrinsics produce the correct results. |
| 15 | + |
| 16 | +#![feature(repr_simd, platform_intrinsics)] |
| 17 | +#![allow(non_camel_case_types)] |
| 18 | + |
| 19 | +#[repr(simd)] |
| 20 | +#[derive(Copy, Clone, PartialEq, Debug)] |
| 21 | +struct x4<T>(pub T, pub T, pub T, pub T); |
| 22 | + |
| 23 | +extern "platform-intrinsic" { |
| 24 | + fn simd_gather<T, U, V>(x: T, y: U, z: V) -> T; |
| 25 | + fn simd_scatter<T, U, V>(x: T, y: U, z: V) -> (); |
| 26 | +} |
| 27 | + |
| 28 | +fn main() { |
| 29 | + let mut x = [0_f32, 1., 2., 3., 4., 5., 6., 7.]; |
| 30 | + |
| 31 | + let default = x4(-3_f32, -3., -3., -3.); |
| 32 | + let s_strided = x4(0_f32, 2., -3., 6.); |
| 33 | + let mask = x4(-1_i32, -1, 0, -1); |
| 34 | + |
| 35 | + // reading from *const |
| 36 | + unsafe { |
| 37 | + let pointer = &x[0] as *const f32; |
| 38 | + let pointers = x4( |
| 39 | + pointer.offset(0) as *const f32, |
| 40 | + pointer.offset(2), |
| 41 | + pointer.offset(4), |
| 42 | + pointer.offset(6) |
| 43 | + ); |
| 44 | + |
| 45 | + let r_strided = simd_gather(default, pointers, mask); |
| 46 | + |
| 47 | + assert_eq!(r_strided, s_strided); |
| 48 | + } |
| 49 | + |
| 50 | + // reading from *mut |
| 51 | + unsafe { |
| 52 | + let pointer = &mut x[0] as *mut f32; |
| 53 | + let pointers = x4( |
| 54 | + pointer.offset(0) as *mut f32, |
| 55 | + pointer.offset(2), |
| 56 | + pointer.offset(4), |
| 57 | + pointer.offset(6) |
| 58 | + ); |
| 59 | + |
| 60 | + let r_strided = simd_gather(default, pointers, mask); |
| 61 | + |
| 62 | + assert_eq!(r_strided, s_strided); |
| 63 | + } |
| 64 | + |
| 65 | + // writing to *mut |
| 66 | + unsafe { |
| 67 | + let pointer = &mut x[0] as *mut f32; |
| 68 | + let pointers = x4( |
| 69 | + pointer.offset(0) as *mut f32, |
| 70 | + pointer.offset(2), |
| 71 | + pointer.offset(4), |
| 72 | + pointer.offset(6) |
| 73 | + ); |
| 74 | + |
| 75 | + let values = x4(42_f32, 43_f32, 44_f32, 45_f32); |
| 76 | + simd_scatter(values, pointers, mask); |
| 77 | + |
| 78 | + assert_eq!(x, [42., 1., 43., 3., 4., 5., 45., 7.]); |
| 79 | + } |
| 80 | +} |
0 commit comments