2 #![allow(unused_must_use)]
3 // ignore-emscripten FIXME(#45351) hits an LLVM assert
5 #![feature(repr_simd, platform_intrinsics, concat_idents, test)]
6 #![allow(non_camel_case_types)]
11 #[derive(PartialEq, Debug)]
12 struct i32x4(i32, i32, i32, i32);
14 #[derive(PartialEq, Debug)]
15 struct i8x4(i8, i8, i8, i8);
18 #[derive(PartialEq, Debug)]
19 struct u32x4(u32, u32, u32, u32);
21 #[derive(PartialEq, Debug)]
22 struct u8x4(u8, u8, u8, u8);
25 #[derive(PartialEq, Debug)]
26 struct f32x4(f32, f32, f32, f32);
29 #[derive(PartialEq, Debug)]
30 struct f64x4(f64, f64, f64, f64);
33 extern "platform-intrinsic" {
34 fn simd_cast<T, U>(x: T) -> U;
37 const A: i32 = -1234567;
38 const B: i32 = 12345678;
39 const C: i32 = -123456789;
40 const D: i32 = 1234567890;
43 fn is_float() -> bool { false }
44 fn in_range(x: i32) -> bool;
47 fn in_range(_: i32) -> bool { true }
50 fn in_range(x: i32) -> bool { -128 <= x && x < 128 }
53 fn in_range(x: i32) -> bool { 0 <= x }
56 fn in_range(x: i32) -> bool { 0 <= x && x < 128 }
59 fn is_float() -> bool { true }
60 fn in_range(_: i32) -> bool { true }
63 fn is_float() -> bool { true }
64 fn in_range(_: i32) -> bool { true }
69 ($from: ident, $to: ident) => {{
70 // force the casts to actually happen, or else LLVM/rustc
71 // may fold them and get slightly different results.
72 let (a, b, c, d) = test::black_box((A as $from, B as $from, C as $from, D as $from));
73 // the SIMD vectors are all FOOx4, so we can concat_idents
74 // so we don't have to pass in the extra args to the macro
75 let mut from = simd_cast(concat_idents!($from, x4)(a, b, c, d));
76 let mut to = concat_idents!($to, x4)(a as $to,
80 // assist type inference, it needs to know what `from` is
81 // for the `if` statements.
84 // there are platform differences for some out of range
85 // casts, so we just normalize such things: it's OK for
86 // "invalid" calculations to result in nonsense answers.
87 // (e.g., negative float to unsigned integer goes through a
88 // library routine on the default i686 platforms, and the
89 // implementation of that routine differs on e.g., Linux
90 // vs. macOS, resulting in different answers.)
91 if $from::is_float() {
92 if !$to::in_range(A) { from.0 = 0 as $to; to.0 = 0 as $to; }
93 if !$to::in_range(B) { from.1 = 0 as $to; to.1 = 0 as $to; }
94 if !$to::in_range(C) { from.2 = 0 as $to; to.2 = 0 as $to; }
95 if !$to::in_range(D) { from.3 = 0 as $to; to.3 = 0 as $to; }
99 "{} -> {} ({:?} != {:?})", stringify!($from), stringify!($to),
104 (: $($to: ident),*) => { () };
105 // repeating the list twice is easier than writing a cartesian
107 ($from: ident $(, $from_: ident)*: $($to: ident),*) => {
108 fn $from() { unsafe { $( test!($from, $to); )* } }
109 tests!($($from_),*: $($to),*)
111 ($($types: ident),*) => {{
112 tests!($($types),* : $($types),*);
117 // test various combinations, including truncation,
118 // signed/unsigned extension, and floating point casts.
119 tests!(i32, i8, u32, u8, f32);
120 tests!(i32, u32, f32, f64)