1 #![feature(portable_simd, platform_intrinsics)]
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]));
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]));
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);
35 f32x2::from_array([0.0, f32::NAN]).max(f32x2::from_array([f32::NAN, 0.0])),
36 f32x2::from_array([0.0, 0.0])
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);
41 f32x2::from_array([0.0, f32::NAN]).min(f32x2::from_array([f32::NAN, 0.0])),
42 f32x2::from_array([0.0, 0.0])
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);
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]));
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]));
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);
79 f64x2::from_array([0.0, f64::NAN]).max(f64x2::from_array([f64::NAN, 0.0])),
80 f64x2::from_array([0.0, 0.0])
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);
85 f64x2::from_array([0.0, f64::NAN]).min(f64x2::from_array([f64::NAN, 0.0])),
86 f64x2::from_array([0.0, 0.0])
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);
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]));
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])
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])
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])
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])
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]));
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]));
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);
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);
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]);
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());
171 i8x4::from_array([127, -128, 127, -128]),
172 f32x4::from_array([127.99, -128.99, 999.0, -999.0]).cast()
175 i32x4::from_array([0, 1, -1, 2147483520]),
178 /*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd),
179 /*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd),
185 i32x8::from_array([i32::MAX, i32::MIN, i32::MAX, i32::MIN, i32::MAX, i32::MIN, 0, 0]),
203 /*0x1.26580cp+30*/ f32::from_bits(0x4e932c06),
207 i32x4::from_array([-2147483647i32, 1234567890i32, 16777219i32, -16777219i32]).cast()
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()
219 i32x4::from_array([0, 1, -1, 2147483520]),
222 /*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd),
223 /*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd),
229 u64x4::from_array([0, 10000000000000000, u64::MAX - 2047, 9223372036854775808]),
233 (u64::MAX - 1024) as f64,
234 9223372036854775808.0
244 let a = f32x4::splat(10.0);
245 let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
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]));
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]));
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]);
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;
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]));
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])));
285 simd_select(i8x4::from_array([0, -1, -1, 0]), a, b),
286 i32x4::from_array([1, 10, 10, 4])
289 simd_select(i8x4::from_array([0, -1, -1, 0]), b, a),
290 i32x4::from_array([10, 2, 10, 10])
302 simd_gather_scatter();