mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Add test using non-power-of-two vector
This commit is contained in:
parent
329412dad3
commit
20413b1947
@ -6,6 +6,9 @@
|
|||||||
#[repr(simd, packed)]
|
#[repr(simd, packed)]
|
||||||
struct Simd<T, const N: usize>([T; N]);
|
struct Simd<T, const N: usize>([T; N]);
|
||||||
|
|
||||||
|
#[repr(simd)]
|
||||||
|
struct FullSimd<T, const N: usize>([T; N]);
|
||||||
|
|
||||||
fn check_size_align<T, const N: usize>() {
|
fn check_size_align<T, const N: usize>() {
|
||||||
use std::mem;
|
use std::mem;
|
||||||
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
|
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
|
||||||
@ -37,9 +40,20 @@ fn main() {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// powers-of-two have no padding and work as usual
|
// powers-of-two have no padding and work as usual
|
||||||
// non-powers-of-two have padding and need to be expanded to full vectors
|
|
||||||
let x: Simd<f64, 4> =
|
let x: Simd<f64, 4> =
|
||||||
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
|
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
|
||||||
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
|
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
|
||||||
|
|
||||||
|
// non-powers-of-two have padding and need to be expanded to full vectors
|
||||||
|
fn load<T, const N: usize>(v: Simd<T, N>) -> FullSimd<T, N> {
|
||||||
|
unsafe {
|
||||||
|
let mut tmp = core::mem::MaybeUninit::<FullSimd<T, N>>::uninit();
|
||||||
|
std::ptr::copy_nonoverlapping(&v as *const _, tmp.as_mut_ptr().cast(), 1);
|
||||||
|
tmp.assume_init()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let x: FullSimd<f64, 3> =
|
||||||
|
simd_add(load(Simd::<f64, 3>([0., 1., 2.])), load(Simd::<f64, 3>([2., 2., 2.])));
|
||||||
|
assert_eq!(x.0, [2., 3., 4.]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user