forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#117116 - calebzulawski:repr-simd-packed, r=wo…
…rkingjubilee Implement repr(packed) for repr(simd) This allows creating vectors with non-power-of-2 lengths that do not have padding. See rust-lang/portable-simd#319
- Loading branch information
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// compile-flags: -C no-prepopulate-passes | ||
|
||
#![crate_type = "lib"] | ||
#![feature(repr_simd, platform_intrinsics)] | ||
|
||
#[repr(simd, packed)] | ||
pub struct Simd<T, const N: usize>([T; N]); | ||
|
||
#[repr(simd)] | ||
#[derive(Copy, Clone)] | ||
pub struct FullSimd<T, const N: usize>([T; N]); | ||
|
||
extern "platform-intrinsic" { | ||
fn simd_mul<T>(a: T, b: T) -> T; | ||
} | ||
|
||
// 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() | ||
} | ||
} | ||
|
||
// CHECK-LABEL: @square_packed | ||
#[no_mangle] | ||
pub fn square_packed(x: Simd<f32, 3>) -> FullSimd<f32, 3> { | ||
// CHECK: align 4 dereferenceable(12) %x | ||
let x = load(x); | ||
unsafe { simd_mul(x, x) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// run-pass | ||
|
||
#![feature(repr_simd, platform_intrinsics)] | ||
#![allow(non_camel_case_types)] | ||
|
||
#[repr(simd, packed)] | ||
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>() { | ||
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::align_of::<Simd<T, N>>(), 0); | ||
} | ||
|
||
fn check_ty<T>() { | ||
check_size_align::<T, 1>(); | ||
check_size_align::<T, 2>(); | ||
check_size_align::<T, 3>(); | ||
check_size_align::<T, 4>(); | ||
check_size_align::<T, 8>(); | ||
check_size_align::<T, 9>(); | ||
check_size_align::<T, 15>(); | ||
} | ||
|
||
extern "platform-intrinsic" { | ||
fn simd_add<T>(a: T, b: T) -> T; | ||
} | ||
|
||
fn main() { | ||
check_ty::<u8>(); | ||
check_ty::<i16>(); | ||
check_ty::<u32>(); | ||
check_ty::<i64>(); | ||
check_ty::<usize>(); | ||
check_ty::<f32>(); | ||
check_ty::<f64>(); | ||
|
||
unsafe { | ||
// powers-of-two have no padding and work as usual | ||
let x: Simd<f64, 4> = | ||
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.]); | ||
|
||
// 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.]); | ||
} | ||
} |