]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/simd-intrinsic-generic-comparison.rs
Imported Upstream version 1.4.0+dfsg1
[rustc.git] / src / test / run-pass / simd-intrinsic-generic-comparison.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(repr_simd, platform_intrinsics, concat_idents)]
12 #![allow(non_camel_case_types)]
13
14 use std::f32::NAN;
15
16 #[repr(simd)]
17 #[derive(Copy, Clone)]
18 struct i32x4(i32, i32, i32, i32);
19 #[repr(simd)]
20 #[derive(Copy, Clone)]
21 struct u32x4(pub u32, pub u32, pub u32, pub u32);
22 #[repr(simd)]
23 #[derive(Copy, Clone)]
24 struct f32x4(pub f32, pub f32, pub f32, pub f32);
25
26 extern "platform-intrinsic" {
27 fn simd_eq<T, U>(x: T, y: T) -> U;
28 fn simd_ne<T, U>(x: T, y: T) -> U;
29 fn simd_lt<T, U>(x: T, y: T) -> U;
30 fn simd_le<T, U>(x: T, y: T) -> U;
31 fn simd_gt<T, U>(x: T, y: T) -> U;
32 fn simd_ge<T, U>(x: T, y: T) -> U;
33 }
34
35 macro_rules! cmp {
36 ($method: ident($lhs: expr, $rhs: expr)) => {{
37 let lhs = $lhs;
38 let rhs = $rhs;
39 let e: u32x4 = concat_idents!(simd_, $method)($lhs, $rhs);
40 // assume the scalar version is correct/the behaviour we want.
41 assert!((e.0 != 0) == lhs.0 .$method(&rhs.0));
42 assert!((e.1 != 0) == lhs.1 .$method(&rhs.1));
43 assert!((e.2 != 0) == lhs.2 .$method(&rhs.2));
44 assert!((e.3 != 0) == lhs.3 .$method(&rhs.3));
45 }}
46 }
47 macro_rules! tests {
48 ($($lhs: ident, $rhs: ident;)*) => {{
49 $(
50 (|| {
51 cmp!(eq($lhs, $rhs));
52 cmp!(ne($lhs, $rhs));
53
54 // test both directions
55 cmp!(lt($lhs, $rhs));
56 cmp!(lt($rhs, $lhs));
57
58 cmp!(le($lhs, $rhs));
59 cmp!(le($rhs, $lhs));
60
61 cmp!(gt($lhs, $rhs));
62 cmp!(gt($rhs, $lhs));
63
64 cmp!(ge($lhs, $rhs));
65 cmp!(ge($rhs, $lhs));
66 })();
67 )*
68 }}
69 }
70 fn main() {
71 // 13 vs. -100 tests that we get signed vs. unsigned comparisons
72 // correct (i32: 13 > -100, u32: 13 < -100). let i1 = i32x4(10, -11, 12, 13);
73 let i1 = i32x4(10, -11, 12, 13);
74 let i2 = i32x4(5, -5, 20, -100);
75 let i3 = i32x4(10, -11, 20, -100);
76
77 let u1 = u32x4(10, !11+1, 12, 13);
78 let u2 = u32x4(5, !5+1, 20, !100+1);
79 let u3 = u32x4(10, !11+1, 20, !100+1);
80
81 let f1 = f32x4(10.0, -11.0, 12.0, 13.0);
82 let f2 = f32x4(5.0, -5.0, 20.0, -100.0);
83 let f3 = f32x4(10.0, -11.0, 20.0, -100.0);
84
85 unsafe {
86 tests! {
87 i1, i1;
88 u1, u1;
89 f1, f1;
90
91 i1, i2;
92 u1, u2;
93 f1, f2;
94
95 i1, i3;
96 u1, u3;
97 f1, f3;
98 }
99 }
100
101 // NAN comparisons are special:
102 // -11 (*) 13
103 // -5 -100 (*)
104 let f4 = f32x4(NAN, f1.1, NAN, f2.3);
105
106 unsafe {
107 tests! {
108 f1, f4;
109 f2, f4;
110 f4, f4;
111 }
112 }
113 }