Skip to content

Commit 0529a76

Browse files
committed
Enable const-testing for the ported SIMD intrinsics
1 parent f1d8fdb commit 0529a76

18 files changed

+826
-585
lines changed

tests/auxiliary/minisimd.rs

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
#![allow(unused)]
1212
#![allow(non_camel_case_types)]
13+
#![allow(unexpected_cfgs)]
1314

1415
// The field is currently left `pub` for convenience in porting tests, many of
1516
// which attempt to just construct it directly. That still works; it's just the
@@ -24,39 +25,32 @@ impl<T: Copy, const N: usize> Clone for Simd<T, N> {
2425
}
2526
}
2627

27-
impl<T: PartialEq, const N: usize> PartialEq for Simd<T, N> {
28-
fn eq(&self, other: &Self) -> bool {
29-
self.as_array() == other.as_array()
30-
}
31-
}
32-
3328
impl<T: core::fmt::Debug, const N: usize> core::fmt::Debug for Simd<T, N> {
3429
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
3530
<[T; N] as core::fmt::Debug>::fmt(self.as_array(), f)
3631
}
3732
}
3833

39-
impl<T, const N: usize> core::ops::Index<usize> for Simd<T, N> {
40-
type Output = T;
41-
fn index(&self, i: usize) -> &T {
42-
&self.as_array()[i]
43-
}
44-
}
45-
4634
impl<T, const N: usize> Simd<T, N> {
4735
pub const fn from_array(a: [T; N]) -> Self {
4836
Simd(a)
4937
}
50-
pub fn as_array(&self) -> &[T; N] {
38+
pub const fn as_array(&self) -> &[T; N] {
5139
let p: *const Self = self;
5240
unsafe { &*p.cast::<[T; N]>() }
5341
}
54-
pub fn into_array(self) -> [T; N]
42+
pub const fn into_array(self) -> [T; N]
5543
where
5644
T: Copy,
5745
{
5846
*self.as_array()
5947
}
48+
pub const fn splat(a: T) -> Self
49+
where
50+
T: Copy,
51+
{
52+
Self([a; N])
53+
}
6054
}
6155

6256
pub type u8x2 = Simd<u8, 2>;
@@ -109,6 +103,14 @@ pub type i64x8 = Simd<i64, 8>;
109103
pub type i128x2 = Simd<i128, 2>;
110104
pub type i128x4 = Simd<i128, 4>;
111105

106+
pub type usizex2 = Simd<usize, 2>;
107+
pub type usizex4 = Simd<usize, 4>;
108+
pub type usizex8 = Simd<usize, 8>;
109+
110+
pub type isizex2 = Simd<isize, 2>;
111+
pub type isizex4 = Simd<isize, 4>;
112+
pub type isizex8 = Simd<isize, 8>;
113+
112114
pub type f32x2 = Simd<f32, 2>;
113115
pub type f32x4 = Simd<f32, 4>;
114116
pub type f32x8 = Simd<f32, 8>;
@@ -122,7 +124,7 @@ pub type f64x8 = Simd<f64, 8>;
122124
// which attempt to just construct it directly. That still works; it's just the
123125
// `.0` projection that doesn't.
124126
#[repr(simd, packed)]
125-
#[derive(Copy)]
127+
#[derive(Copy, Eq)]
126128
pub struct PackedSimd<T, const N: usize>(pub [T; N]);
127129

128130
impl<T: Copy, const N: usize> Clone for PackedSimd<T, N> {
@@ -131,12 +133,6 @@ impl<T: Copy, const N: usize> Clone for PackedSimd<T, N> {
131133
}
132134
}
133135

134-
impl<T: PartialEq, const N: usize> PartialEq for PackedSimd<T, N> {
135-
fn eq(&self, other: &Self) -> bool {
136-
self.as_array() == other.as_array()
137-
}
138-
}
139-
140136
impl<T: core::fmt::Debug, const N: usize> core::fmt::Debug for PackedSimd<T, N> {
141137
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
142138
<[T; N] as core::fmt::Debug>::fmt(self.as_array(), f)
@@ -147,14 +143,100 @@ impl<T, const N: usize> PackedSimd<T, N> {
147143
pub const fn from_array(a: [T; N]) -> Self {
148144
PackedSimd(a)
149145
}
150-
pub fn as_array(&self) -> &[T; N] {
146+
pub const fn as_array(&self) -> &[T; N] {
151147
let p: *const Self = self;
152148
unsafe { &*p.cast::<[T; N]>() }
153149
}
154-
pub fn into_array(self) -> [T; N]
150+
pub const fn into_array(self) -> [T; N]
155151
where
156152
T: Copy,
157153
{
158154
*self.as_array()
159155
}
156+
pub const fn splat(a: T) -> Self
157+
where
158+
T: Copy,
159+
{
160+
Self([a; N])
161+
}
160162
}
163+
164+
// As `const_trait_impl` is a language feature with specialized syntax, we have to use them in a way
165+
// such that it doesn't get parsed as Rust code unless `cfg(minisimd_const)` is on. The easiest way
166+
// for that is a macro
167+
168+
#[cfg(minisimd_const)]
169+
macro_rules! impl_traits {
170+
() => {
171+
impl<T: [const] PartialEq, const N: usize> const PartialEq for Simd<T, N> {
172+
fn eq(&self, other: &Self) -> bool {
173+
self.as_array() == other.as_array()
174+
}
175+
}
176+
177+
impl<T, const N: usize> const core::ops::Index<usize> for Simd<T, N> {
178+
type Output = T;
179+
fn index(&self, i: usize) -> &T {
180+
&self.as_array()[i]
181+
}
182+
}
183+
184+
impl<T: [const] PartialEq, const N: usize> const PartialEq for PackedSimd<T, N> {
185+
fn eq(&self, other: &Self) -> bool {
186+
self.as_array() == other.as_array()
187+
}
188+
}
189+
};
190+
}
191+
192+
#[cfg(not(minisimd_const))]
193+
macro_rules! impl_traits {
194+
() => {
195+
impl<T: PartialEq, const N: usize> PartialEq for Simd<T, N> {
196+
fn eq(&self, other: &Self) -> bool {
197+
self.as_array() == other.as_array()
198+
}
199+
}
200+
201+
impl<T, const N: usize> core::ops::Index<usize> for Simd<T, N> {
202+
type Output = T;
203+
fn index(&self, i: usize) -> &T {
204+
&self.as_array()[i]
205+
}
206+
}
207+
208+
impl<T: PartialEq, const N: usize> PartialEq for PackedSimd<T, N> {
209+
fn eq(&self, other: &Self) -> bool {
210+
self.as_array() == other.as_array()
211+
}
212+
}
213+
};
214+
}
215+
216+
impl_traits!();
217+
218+
/// Version of `assert_eq` that ignores fancy runtime printing in const context
219+
#[cfg(minisimd_const)]
220+
#[macro_export]
221+
macro_rules! assert_eq_const_safe {
222+
($left:expr, $right:expr $(,)?) => {
223+
assert_eq_const_safe!(
224+
$left,
225+
$right,
226+
concat!("`", stringify!($left), "` == `", stringify!($right), "`")
227+
);
228+
};
229+
($left:expr, $right:expr$(, $($arg:tt)+)?) => {
230+
{
231+
let left = $left;
232+
let right = $right;
233+
// type inference works better with the concrete type on the
234+
// left, but humans work better with the expected on the
235+
// right
236+
assert!(right == left, $($($arg)*),*);
237+
}
238+
};
239+
}
240+
241+
#[cfg(minisimd_const)]
242+
use assert_eq_const_safe;

tests/ui/simd/intrinsic/float-math-pass.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//@ run-pass
22
//@ ignore-emscripten
33
//@ ignore-android
4+
//@ compile-flags: --cfg minisimd_const
45

56
// FIXME: this test fails on arm-android because the NDK version 14 is too old.
67
// It needs at least version 18. We disable it on all android build bots because
78
// there is no way in compile-test to disable it for an (arch,os) pair.
89

910
// Test that the simd floating-point math intrinsics produce correct results.
1011

11-
#![feature(repr_simd, intrinsics, core_intrinsics)]
12+
#![feature(repr_simd, core_intrinsics, const_trait_impl, const_cmp, const_index)]
1213
#![allow(non_camel_case_types)]
1314

1415
#[path = "../../../auxiliary/minisimd.rs"]
@@ -34,7 +35,7 @@ macro_rules! assert_approx_eq {
3435
}};
3536
}
3637

37-
fn main() {
38+
const fn abs_and_rounding() {
3839
let x = f32x4::from_array([1.0, 1.0, 1.0, 1.0]);
3940
let y = f32x4::from_array([-1.0, -1.0, -1.0, -1.0]);
4041
let z = f32x4::from_array([0.0, 0.0, 0.0, 0.0]);
@@ -43,8 +44,33 @@ fn main() {
4344

4445
unsafe {
4546
let r = simd_fabs(y);
46-
assert_approx_eq!(x, r);
47+
assert_eq_const_safe!(x, r);
48+
49+
// rounding functions
50+
let r = simd_floor(h);
51+
assert_eq_const_safe!(z, r);
52+
53+
let r = simd_ceil(h);
54+
assert_eq_const_safe!(x, r);
55+
56+
let r = simd_round(h);
57+
assert_eq_const_safe!(x, r);
58+
59+
let r = simd_round_ties_even(h);
60+
assert_eq_const_safe!(z, r);
61+
62+
let r = simd_trunc(h);
63+
assert_eq_const_safe!(z, r);
64+
}
65+
}
66+
67+
fn math_functions() {
68+
let x = f32x4::from_array([1.0, 1.0, 1.0, 1.0]);
69+
let z = f32x4::from_array([0.0, 0.0, 0.0, 0.0]);
4770

71+
let h = f32x4::from_array([0.5, 0.5, 0.5, 0.5]);
72+
73+
unsafe {
4874
let r = simd_fcos(z);
4975
assert_approx_eq!(x, r);
5076

@@ -74,21 +100,11 @@ fn main() {
74100

75101
let r = simd_fsin(z);
76102
assert_approx_eq!(z, r);
77-
78-
// rounding functions
79-
let r = simd_floor(h);
80-
assert_eq!(z, r);
81-
82-
let r = simd_ceil(h);
83-
assert_eq!(x, r);
84-
85-
let r = simd_round(h);
86-
assert_eq!(x, r);
87-
88-
let r = simd_round_ties_even(h);
89-
assert_eq!(z, r);
90-
91-
let r = simd_trunc(h);
92-
assert_eq!(z, r);
93103
}
94104
}
105+
106+
fn main() {
107+
const { abs_and_rounding() };
108+
abs_and_rounding();
109+
math_functions();
110+
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//@ run-pass
22
//@ ignore-emscripten
3+
//@ compile-flags: --cfg minisimd_const
34

45
// Test that the simd_f{min,max} intrinsics produce the correct results.
56

6-
#![feature(repr_simd, core_intrinsics)]
7+
#![feature(repr_simd, core_intrinsics, const_trait_impl, const_cmp, const_index)]
78
#![allow(non_camel_case_types)]
89

910
#[path = "../../../auxiliary/minisimd.rs"]
@@ -12,7 +13,7 @@ use minisimd::*;
1213

1314
use std::intrinsics::simd::*;
1415

15-
fn main() {
16+
const fn minmax() {
1617
let x = f32x4::from_array([1.0, 2.0, 3.0, 4.0]);
1718
let y = f32x4::from_array([2.0, 1.0, 4.0, 3.0]);
1819

@@ -28,22 +29,27 @@ fn main() {
2829
unsafe {
2930
let min0 = simd_fmin(x, y);
3031
let min1 = simd_fmin(y, x);
31-
assert_eq!(min0, min1);
32+
assert_eq_const_safe!(min0, min1);
3233
let e = f32x4::from_array([1.0, 1.0, 3.0, 3.0]);
33-
assert_eq!(min0, e);
34+
assert_eq_const_safe!(min0, e);
3435
let minn = simd_fmin(x, n);
35-
assert_eq!(minn, x);
36+
assert_eq_const_safe!(minn, x);
3637
let minn = simd_fmin(y, n);
37-
assert_eq!(minn, y);
38+
assert_eq_const_safe!(minn, y);
3839

3940
let max0 = simd_fmax(x, y);
4041
let max1 = simd_fmax(y, x);
41-
assert_eq!(max0, max1);
42+
assert_eq_const_safe!(max0, max1);
4243
let e = f32x4::from_array([2.0, 2.0, 4.0, 4.0]);
43-
assert_eq!(max0, e);
44+
assert_eq_const_safe!(max0, e);
4445
let maxn = simd_fmax(x, n);
45-
assert_eq!(maxn, x);
46+
assert_eq_const_safe!(maxn, x);
4647
let maxn = simd_fmax(y, n);
47-
assert_eq!(maxn, y);
48+
assert_eq_const_safe!(maxn, y);
4849
}
4950
}
51+
52+
fn main() {
53+
const { minmax() };
54+
minmax();
55+
}

0 commit comments

Comments
 (0)