]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/portable-simd.rs
eca8e8f377d6d6ac16fb6383cb8f9b201896c0a4
[rust.git] / tests / run-pass / portable-simd.rs
1 #![feature(portable_simd, platform_intrinsics)]
2 use std::simd::*;
3
4 fn simd_ops_f32() {
5     let a = f32x4::splat(10.0);
6     let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
7     assert_eq!(-b, f32x4::from_array([-1.0, -2.0, -3.0, 4.0]));
8     assert_eq!(a + b, f32x4::from_array([11.0, 12.0, 13.0, 6.0]));
9     assert_eq!(a - b, f32x4::from_array([9.0, 8.0, 7.0, 14.0]));
10     assert_eq!(a * b, f32x4::from_array([10.0, 20.0, 30.0, -40.0]));
11     assert_eq!(b / a, f32x4::from_array([0.1, 0.2, 0.3, -0.4]));
12     assert_eq!(a / f32x4::splat(2.0), f32x4::splat(5.0));
13     assert_eq!(a % b, f32x4::from_array([0.0, 0.0, 1.0, 2.0]));
14     assert_eq!(b.abs(), f32x4::from_array([1.0, 2.0, 3.0, 4.0]));
15     assert_eq!(a.max(b * f32x4::splat(4.0)), f32x4::from_array([10.0, 10.0, 12.0, 10.0]));
16     assert_eq!(a.min(b * f32x4::splat(4.0)), f32x4::from_array([4.0, 8.0, 10.0, -16.0]));
17
18     assert_eq!(a.lanes_eq(f32x4::splat(5.0) * b), Mask::from_array([false, true, false, false]));
19     assert_eq!(a.lanes_ne(f32x4::splat(5.0) * b), Mask::from_array([true, false, true, true]));
20     assert_eq!(a.lanes_le(f32x4::splat(5.0) * b), Mask::from_array([false, true, true, false]));
21     assert_eq!(a.lanes_lt(f32x4::splat(5.0) * b), Mask::from_array([false, false, true, false]));
22     assert_eq!(a.lanes_ge(f32x4::splat(5.0) * b), Mask::from_array([true, true, false, true]));
23     assert_eq!(a.lanes_gt(f32x4::splat(5.0) * b), Mask::from_array([true, false, false, true]));
24
25     assert_eq!(a.reduce_sum(), 40.0);
26     assert_eq!(b.reduce_sum(), 2.0);
27     assert_eq!(a.reduce_product(), 100.0 * 100.0);
28     assert_eq!(b.reduce_product(), -24.0);
29     assert_eq!(a.reduce_max(), 10.0);
30     assert_eq!(b.reduce_max(), 3.0);
31     assert_eq!(a.reduce_min(), 10.0);
32     assert_eq!(b.reduce_min(), -4.0);
33
34     assert_eq!(
35         f32x2::from_array([0.0, f32::NAN]).max(f32x2::from_array([f32::NAN, 0.0])),
36         f32x2::from_array([0.0, 0.0])
37     );
38     assert_eq!(f32x2::from_array([0.0, f32::NAN]).reduce_max(), 0.0);
39     assert_eq!(f32x2::from_array([f32::NAN, 0.0]).reduce_max(), 0.0);
40     assert_eq!(
41         f32x2::from_array([0.0, f32::NAN]).min(f32x2::from_array([f32::NAN, 0.0])),
42         f32x2::from_array([0.0, 0.0])
43     );
44     assert_eq!(f32x2::from_array([0.0, f32::NAN]).reduce_min(), 0.0);
45     assert_eq!(f32x2::from_array([f32::NAN, 0.0]).reduce_min(), 0.0);
46 }
47
48 fn simd_ops_f64() {
49     let a = f64x4::splat(10.0);
50     let b = f64x4::from_array([1.0, 2.0, 3.0, -4.0]);
51     assert_eq!(-b, f64x4::from_array([-1.0, -2.0, -3.0, 4.0]));
52     assert_eq!(a + b, f64x4::from_array([11.0, 12.0, 13.0, 6.0]));
53     assert_eq!(a - b, f64x4::from_array([9.0, 8.0, 7.0, 14.0]));
54     assert_eq!(a * b, f64x4::from_array([10.0, 20.0, 30.0, -40.0]));
55     assert_eq!(b / a, f64x4::from_array([0.1, 0.2, 0.3, -0.4]));
56     assert_eq!(a / f64x4::splat(2.0), f64x4::splat(5.0));
57     assert_eq!(a % b, f64x4::from_array([0.0, 0.0, 1.0, 2.0]));
58     assert_eq!(b.abs(), f64x4::from_array([1.0, 2.0, 3.0, 4.0]));
59     assert_eq!(a.max(b * f64x4::splat(4.0)), f64x4::from_array([10.0, 10.0, 12.0, 10.0]));
60     assert_eq!(a.min(b * f64x4::splat(4.0)), f64x4::from_array([4.0, 8.0, 10.0, -16.0]));
61
62     assert_eq!(a.lanes_eq(f64x4::splat(5.0) * b), Mask::from_array([false, true, false, false]));
63     assert_eq!(a.lanes_ne(f64x4::splat(5.0) * b), Mask::from_array([true, false, true, true]));
64     assert_eq!(a.lanes_le(f64x4::splat(5.0) * b), Mask::from_array([false, true, true, false]));
65     assert_eq!(a.lanes_lt(f64x4::splat(5.0) * b), Mask::from_array([false, false, true, false]));
66     assert_eq!(a.lanes_ge(f64x4::splat(5.0) * b), Mask::from_array([true, true, false, true]));
67     assert_eq!(a.lanes_gt(f64x4::splat(5.0) * b), Mask::from_array([true, false, false, true]));
68
69     assert_eq!(a.reduce_sum(), 40.0);
70     assert_eq!(b.reduce_sum(), 2.0);
71     assert_eq!(a.reduce_product(), 100.0 * 100.0);
72     assert_eq!(b.reduce_product(), -24.0);
73     assert_eq!(a.reduce_max(), 10.0);
74     assert_eq!(b.reduce_max(), 3.0);
75     assert_eq!(a.reduce_min(), 10.0);
76     assert_eq!(b.reduce_min(), -4.0);
77
78     assert_eq!(
79         f64x2::from_array([0.0, f64::NAN]).max(f64x2::from_array([f64::NAN, 0.0])),
80         f64x2::from_array([0.0, 0.0])
81     );
82     assert_eq!(f64x2::from_array([0.0, f64::NAN]).reduce_max(), 0.0);
83     assert_eq!(f64x2::from_array([f64::NAN, 0.0]).reduce_max(), 0.0);
84     assert_eq!(
85         f64x2::from_array([0.0, f64::NAN]).min(f64x2::from_array([f64::NAN, 0.0])),
86         f64x2::from_array([0.0, 0.0])
87     );
88     assert_eq!(f64x2::from_array([0.0, f64::NAN]).reduce_min(), 0.0);
89     assert_eq!(f64x2::from_array([f64::NAN, 0.0]).reduce_min(), 0.0);
90 }
91
92 fn simd_ops_i32() {
93     let a = i32x4::splat(10);
94     let b = i32x4::from_array([1, 2, 3, -4]);
95     assert_eq!(-b, i32x4::from_array([-1, -2, -3, 4]));
96     assert_eq!(a + b, i32x4::from_array([11, 12, 13, 6]));
97     assert_eq!(a - b, i32x4::from_array([9, 8, 7, 14]));
98     assert_eq!(a * b, i32x4::from_array([10, 20, 30, -40]));
99     assert_eq!(a / b, i32x4::from_array([10, 5, 3, -2]));
100     assert_eq!(a / i32x4::splat(2), i32x4::splat(5));
101     assert_eq!(i32x2::splat(i32::MIN) / i32x2::splat(-1), i32x2::splat(i32::MIN));
102     assert_eq!(a % b, i32x4::from_array([0, 0, 1, 2]));
103     assert_eq!(i32x2::splat(i32::MIN) % i32x2::splat(-1), i32x2::splat(0));
104     assert_eq!(b.abs(), i32x4::from_array([1, 2, 3, 4]));
105     // FIXME not a per-lane method (https://github.com/rust-lang/portable-simd/issues/247)
106     // assert_eq!(a.max(b * i32x4::splat(4)), i32x4::from_array([10, 10, 12, 10]));
107     // assert_eq!(a.min(b * i32x4::splat(4)), i32x4::from_array([4, 8, 10, -16]));
108
109     assert_eq!(
110         i8x4::from_array([i8::MAX, -23, 23, i8::MIN]).saturating_add(i8x4::from_array([1, i8::MIN, i8::MAX, 28])),
111         i8x4::from_array([i8::MAX, i8::MIN, i8::MAX, -100])
112     );
113     assert_eq!(
114         i8x4::from_array([i8::MAX, -28, 27, 42]).saturating_sub(i8x4::from_array([1, i8::MAX, i8::MAX, -80])),
115         i8x4::from_array([126, i8::MIN, -100, 122])
116     );
117     assert_eq!(
118         u8x4::from_array([u8::MAX, 0, 23, 42]).saturating_add(u8x4::from_array([1, 1, u8::MAX, 200])),
119         u8x4::from_array([u8::MAX, 1, u8::MAX, 242])
120     );
121     assert_eq!(
122         u8x4::from_array([u8::MAX, 0, 23, 42]).saturating_sub(u8x4::from_array([1, 1, u8::MAX, 200])),
123         u8x4::from_array([254, 0, 0, 0])
124     );
125
126     assert_eq!(!b, i32x4::from_array([!1, !2, !3, !-4]));
127     assert_eq!(b << i32x4::splat(2), i32x4::from_array([4, 8, 12, -16]));
128     assert_eq!(b >> i32x4::splat(1), i32x4::from_array([0, 1, 1, -2]));
129     assert_eq!(b & i32x4::splat(2), i32x4::from_array([0, 2, 2, 0]));
130     assert_eq!(b | i32x4::splat(2), i32x4::from_array([3, 2, 3, -2]));
131     assert_eq!(b ^ i32x4::splat(2), i32x4::from_array([3, 0, 1, -2]));
132
133     assert_eq!(a.lanes_eq(i32x4::splat(5) * b), Mask::from_array([false, true, false, false]));
134     assert_eq!(a.lanes_ne(i32x4::splat(5) * b), Mask::from_array([true, false, true, true]));
135     assert_eq!(a.lanes_le(i32x4::splat(5) * b), Mask::from_array([false, true, true, false]));
136     assert_eq!(a.lanes_lt(i32x4::splat(5) * b), Mask::from_array([false, false, true, false]));
137     assert_eq!(a.lanes_ge(i32x4::splat(5) * b), Mask::from_array([true, true, false, true]));
138     assert_eq!(a.lanes_gt(i32x4::splat(5) * b), Mask::from_array([true, false, false, true]));
139
140     assert_eq!(a.reduce_sum(), 40);
141     assert_eq!(b.reduce_sum(), 2);
142     assert_eq!(a.reduce_product(), 100 * 100);
143     assert_eq!(b.reduce_product(), -24);
144     assert_eq!(a.reduce_max(), 10);
145     assert_eq!(b.reduce_max(), 3);
146     assert_eq!(a.reduce_min(), 10);
147     assert_eq!(b.reduce_min(), -4);
148
149     assert_eq!(a.reduce_and(), 10);
150     assert_eq!(b.reduce_and(), 0);
151     assert_eq!(a.reduce_or(), 10);
152     assert_eq!(b.reduce_or(), -1);
153     assert_eq!(a.reduce_xor(), 0);
154     assert_eq!(b.reduce_xor(), -4);
155 }
156
157 fn simd_mask() {
158     let intmask = Mask::from_int(i32x4::from_array([0, -1, 0, 0]));
159     assert_eq!(intmask, Mask::from_array([false, true, false, false]));
160     assert_eq!(intmask.to_array(), [false, true, false, false]);
161 }
162
163 fn simd_cast() {
164     // between integer types
165     assert_eq!(i32x4::from_array([1, 2, 3, -4]), i16x4::from_array([1, 2, 3, -4]).cast());
166     assert_eq!(i16x4::from_array([1, 2, 3, -4]), i32x4::from_array([1, 2, 3, -4]).cast());
167     assert_eq!(i32x4::from_array([1, -1, 3, 4]), u64x4::from_array([1, u64::MAX, 3, 4]).cast());
168
169     // float -> int
170     assert_eq!(
171         i8x4::from_array([127, -128, 127, -128]),
172         f32x4::from_array([127.99, -128.99, 999.0, -999.0]).cast()
173     );
174     assert_eq!(
175         i32x4::from_array([0, 1, -1, 2147483520]),
176         f32x4::from_array([
177             -0.0,
178             /*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd),
179             /*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd),
180             2147483520.0
181         ])
182         .cast()
183     );
184     assert_eq!(
185         i32x8::from_array([i32::MAX, i32::MIN, i32::MAX, i32::MIN, i32::MAX, i32::MIN, 0, 0]),
186         f32x8::from_array([
187             2147483648.0f32,
188             -2147483904.0f32,
189             f32::MAX,
190             f32::MIN,
191             f32::INFINITY,
192             f32::NEG_INFINITY,
193             f32::NAN,
194             -f32::NAN,
195         ])
196         .cast()
197     );
198
199     // int -> float
200     assert_eq!(
201         f32x4::from_array([
202             -2147483648.0,
203             /*0x1.26580cp+30*/ f32::from_bits(0x4e932c06),
204             16777220.0,
205             -16777220.0,
206         ]),
207         i32x4::from_array([-2147483647i32, 1234567890i32, 16777219i32, -16777219i32]).cast()
208     );
209
210     // float -> float
211     assert_eq!(
212         f32x4::from_array([f32::INFINITY, f32::INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY]),
213         f64x4::from_array([f64::MAX, f64::INFINITY, f64::MIN, f64::NEG_INFINITY]).cast()
214     );
215
216     // unchecked casts
217     unsafe {
218         assert_eq!(
219             i32x4::from_array([0, 1, -1, 2147483520]),
220             f32x4::from_array([
221                 -0.0,
222                 /*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd),
223                 /*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd),
224                 2147483520.0
225             ])
226             .to_int_unchecked()
227         );
228         assert_eq!(
229             u64x4::from_array([0, 10000000000000000, u64::MAX - 2047, 9223372036854775808]),
230             f64x4::from_array([
231                 -0.99999999999,
232                 1e16,
233                 (u64::MAX - 1024) as f64,
234                 9223372036854775808.0
235             ])
236             .to_int_unchecked()
237         );
238     }
239 }
240
241 fn simd_swizzle() {
242     use Which::*;
243
244     let a = f32x4::splat(10.0);
245     let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
246
247     assert_eq!(simd_swizzle!(b, [3, 0, 0, 2]), f32x4::from_array([-4.0, 1.0, 1.0, 3.0]));
248     assert_eq!(simd_swizzle!(b, [1, 2]), f32x2::from_array([2.0, 3.0]));
249     assert_eq!(simd_swizzle!(b, a, [First(3), Second(0)]), f32x2::from_array([-4.0, 10.0]));
250 }
251
252 fn simd_gather_scatter() {
253     let mut vec: Vec<i16> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
254     let idxs = Simd::from_array([9, 3, 0, 17]);
255     let result = Simd::gather_or_default(&vec, idxs); // Note the lane that is out-of-bounds.
256     assert_eq!(result, Simd::from_array([0, 13, 10, 0]));
257
258     let idxs = Simd::from_array([9, 3, 0, 0]);
259     Simd::from_array([-27, 82, -41, 124]).scatter(&mut vec, idxs);
260     assert_eq!(vec, vec![124, 11, 12, 82, 14, 15, 16, 17, 18]);
261 }
262
263 fn simd_intrinsics() {
264     extern "platform-intrinsic" {
265         fn simd_eq<T, U>(x: T, y: T) -> U;
266         fn simd_reduce_any<T>(x: T) -> bool;
267         fn simd_reduce_all<T>(x: T) -> bool;
268         fn simd_select<M, T>(m: M, yes: T, no: T) -> T;
269     }
270     unsafe {
271         // Make sure simd_eq returns all-1 for `true`
272         let a = i32x4::splat(10);
273         let b = i32x4::from_array([1, 2, 10, 4]);
274         let c: i32x4 = simd_eq(a, b);
275         assert_eq!(c, i32x4::from_array([0, 0, -1, 0]));
276
277         assert!(!simd_reduce_any(i32x4::splat(0)));
278         assert!(simd_reduce_any(i32x4::splat(-1)));
279         assert!(simd_reduce_any(i32x2::from_array([0, -1])));
280         assert!(!simd_reduce_all(i32x4::splat(0)));
281         assert!(simd_reduce_all(i32x4::splat(-1)));
282         assert!(!simd_reduce_all(i32x2::from_array([0, -1])));
283
284         assert_eq!(
285             simd_select(i8x4::from_array([0, -1, -1, 0]), a, b),
286             i32x4::from_array([1, 10, 10, 4])
287         );
288         assert_eq!(
289             simd_select(i8x4::from_array([0, -1, -1, 0]), b, a),
290             i32x4::from_array([10, 2, 10, 10])
291         );
292     }
293 }
294
295 fn main() {
296     simd_mask();
297     simd_ops_f32();
298     simd_ops_f64();
299     simd_ops_i32();
300     simd_cast();
301     simd_swizzle();
302     simd_gather_scatter();
303     simd_intrinsics();
304 }