]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
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 | ||
9e0c209e | 11 | #![feature(repr_simd, platform_intrinsics, concat_idents, test)] |
e9174d1e SL |
12 | #![allow(non_camel_case_types)] |
13 | ||
14 | extern crate test; | |
15 | ||
16 | #[repr(simd)] | |
17 | #[derive(PartialEq, Debug)] | |
18 | struct i32x4(i32, i32, i32, i32); | |
19 | #[repr(simd)] | |
20 | #[derive(PartialEq, Debug)] | |
21 | struct i8x4(i8, i8, i8, i8); | |
22 | ||
23 | #[repr(simd)] | |
24 | #[derive(PartialEq, Debug)] | |
25 | struct u32x4(u32, u32, u32, u32); | |
26 | #[repr(simd)] | |
27 | #[derive(PartialEq, Debug)] | |
28 | struct u8x4(u8, u8, u8, u8); | |
29 | ||
30 | #[repr(simd)] | |
31 | #[derive(PartialEq, Debug)] | |
32 | struct f32x4(f32, f32, f32, f32); | |
33 | ||
34 | #[repr(simd)] | |
35 | #[derive(PartialEq, Debug)] | |
36 | struct f64x4(f64, f64, f64, f64); | |
37 | ||
38 | ||
39 | extern "platform-intrinsic" { | |
40 | fn simd_cast<T, U>(x: T) -> U; | |
41 | } | |
42 | ||
43 | const A: i32 = -1234567; | |
44 | const B: i32 = 12345678; | |
45 | const C: i32 = -123456789; | |
46 | const D: i32 = 1234567890; | |
47 | ||
48 | trait Foo { | |
49 | fn is_float() -> bool { false } | |
50 | fn in_range(x: i32) -> bool; | |
51 | } | |
52 | impl Foo for i32 { | |
53 | fn in_range(_: i32) -> bool { true } | |
54 | } | |
55 | impl Foo for i8 { | |
56 | fn in_range(x: i32) -> bool { -128 <= x && x < 128 } | |
57 | } | |
58 | impl Foo for u32 { | |
59 | fn in_range(x: i32) -> bool { 0 <= x } | |
60 | } | |
61 | impl Foo for u8 { | |
62 | fn in_range(x: i32) -> bool { 0 <= x && x < 128 } | |
63 | } | |
64 | impl Foo for f32 { | |
65 | fn is_float() -> bool { true } | |
66 | fn in_range(_: i32) -> bool { true } | |
67 | } | |
68 | impl Foo for f64 { | |
69 | fn is_float() -> bool { true } | |
70 | fn in_range(_: i32) -> bool { true } | |
71 | } | |
72 | ||
73 | fn main() { | |
74 | macro_rules! test { | |
75 | ($from: ident, $to: ident) => {{ | |
76 | // force the casts to actually happen, or else LLVM/rustc | |
77 | // may fold them and get slightly different results. | |
78 | let (a, b, c, d) = test::black_box((A as $from, B as $from, C as $from, D as $from)); | |
79 | // the SIMD vectors are all FOOx4, so we can concat_idents | |
80 | // so we don't have to pass in the extra args to the macro | |
81 | let mut from = simd_cast(concat_idents!($from, x4)(a, b, c, d)); | |
82 | let mut to = concat_idents!($to, x4)(a as $to, | |
83 | b as $to, | |
84 | c as $to, | |
85 | d as $to); | |
86 | // assist type inference, it needs to know what `from` is | |
87 | // for the `if` statements. | |
88 | to == from; | |
89 | ||
90 | // there are platform differences for some out of range | |
91 | // casts, so we just normalize such things: it's OK for | |
92 | // "invalid" calculations to result in nonsense answers. | |
93 | // (E.g. negative float to unsigned integer goes through a | |
94 | // library routine on the default i686 platforms, and the | |
95 | // implementation of that routine differs on e.g. Linux | |
96 | // vs. OSX, resulting in different answers.) | |
97 | if $from::is_float() { | |
98 | if !$to::in_range(A) { from.0 = 0 as $to; to.0 = 0 as $to; } | |
99 | if !$to::in_range(B) { from.1 = 0 as $to; to.1 = 0 as $to; } | |
100 | if !$to::in_range(C) { from.2 = 0 as $to; to.2 = 0 as $to; } | |
101 | if !$to::in_range(D) { from.3 = 0 as $to; to.3 = 0 as $to; } | |
102 | } | |
103 | ||
104 | assert!(to == from, | |
105 | "{} -> {} ({:?} != {:?})", stringify!($from), stringify!($to), | |
106 | from, to); | |
107 | }} | |
108 | } | |
109 | macro_rules! tests { | |
110 | (: $($to: ident),*) => { () }; | |
111 | // repeating the list twice is easier than writing a cartesian | |
112 | // product macro | |
113 | ($from: ident $(, $from_: ident)*: $($to: ident),*) => { | |
114 | fn $from() { unsafe { $( test!($from, $to); )* } } | |
115 | tests!($($from_),*: $($to),*) | |
116 | }; | |
117 | ($($types: ident),*) => {{ | |
118 | tests!($($types),* : $($types),*); | |
119 | $($types();)* | |
120 | }} | |
121 | } | |
122 | ||
123 | // test various combinations, including truncation, | |
124 | // signed/unsigned extension, and floating point casts. | |
125 | tests!(i32, i8, u32, u8, f32); | |
126 | tests!(i32, u32, f32, f64) | |
127 | } |