]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/simd-intrinsic-generic-cast.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / test / run-pass / simd-intrinsic-generic-cast.rs
CommitLineData
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
14extern crate test;
15
16#[repr(simd)]
17#[derive(PartialEq, Debug)]
18struct i32x4(i32, i32, i32, i32);
19#[repr(simd)]
20#[derive(PartialEq, Debug)]
21struct i8x4(i8, i8, i8, i8);
22
23#[repr(simd)]
24#[derive(PartialEq, Debug)]
25struct u32x4(u32, u32, u32, u32);
26#[repr(simd)]
27#[derive(PartialEq, Debug)]
28struct u8x4(u8, u8, u8, u8);
29
30#[repr(simd)]
31#[derive(PartialEq, Debug)]
32struct f32x4(f32, f32, f32, f32);
33
34#[repr(simd)]
35#[derive(PartialEq, Debug)]
36struct f64x4(f64, f64, f64, f64);
37
38
39extern "platform-intrinsic" {
40 fn simd_cast<T, U>(x: T) -> U;
41}
42
43const A: i32 = -1234567;
44const B: i32 = 12345678;
45const C: i32 = -123456789;
46const D: i32 = 1234567890;
47
48trait Foo {
49 fn is_float() -> bool { false }
50 fn in_range(x: i32) -> bool;
51}
52impl Foo for i32 {
53 fn in_range(_: i32) -> bool { true }
54}
55impl Foo for i8 {
56 fn in_range(x: i32) -> bool { -128 <= x && x < 128 }
57}
58impl Foo for u32 {
59 fn in_range(x: i32) -> bool { 0 <= x }
60}
61impl Foo for u8 {
62 fn in_range(x: i32) -> bool { 0 <= x && x < 128 }
63}
64impl Foo for f32 {
65 fn is_float() -> bool { true }
66 fn in_range(_: i32) -> bool { true }
67}
68impl Foo for f64 {
69 fn is_float() -> bool { true }
70 fn in_range(_: i32) -> bool { true }
71}
72
73fn 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}