summaryrefslogtreecommitdiffstats
path: root/tests/ui/simd/intrinsic/generic-reduction-pass.rs
blob: 4a54afee8075899771243858c438a7f62841ba52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// run-pass
#![allow(non_camel_case_types)]

// ignore-emscripten

// Test that the simd_reduce_{op} intrinsics produce the correct results.

#![feature(repr_simd, platform_intrinsics)]
#[allow(non_camel_case_types)]

#[repr(simd)]
#[derive(Copy, Clone)]
struct i32x4(pub i32, pub i32, pub i32, pub i32);

#[repr(simd)]
#[derive(Copy, Clone)]
struct u32x4(pub u32, pub u32, pub u32, pub u32);

#[repr(simd)]
#[derive(Copy, Clone)]
struct f32x4(pub f32, pub f32, pub f32, pub f32);

#[repr(simd)]
#[derive(Copy, Clone)]
struct b8x4(pub i8, pub i8, pub i8, pub i8);

extern "platform-intrinsic" {
    fn simd_reduce_add_unordered<T, U>(x: T) -> U;
    fn simd_reduce_mul_unordered<T, U>(x: T) -> U;
    fn simd_reduce_add_ordered<T, U>(x: T, acc: U) -> U;
    fn simd_reduce_mul_ordered<T, U>(x: T, acc: U) -> U;
    fn simd_reduce_min<T, U>(x: T) -> U;
    fn simd_reduce_max<T, U>(x: T) -> U;
    fn simd_reduce_min_nanless<T, U>(x: T) -> U;
    fn simd_reduce_max_nanless<T, U>(x: T) -> U;
    fn simd_reduce_and<T, U>(x: T) -> U;
    fn simd_reduce_or<T, U>(x: T) -> U;
    fn simd_reduce_xor<T, U>(x: T) -> U;
    fn simd_reduce_all<T>(x: T) -> bool;
    fn simd_reduce_any<T>(x: T) -> bool;
}

fn main() {
    unsafe {
        let x = i32x4(1, -2, 3, 4);
        let r: i32 = simd_reduce_add_unordered(x);
        assert_eq!(r, 6_i32);
        let r: i32 = simd_reduce_mul_unordered(x);
        assert_eq!(r, -24_i32);
        let r: i32 = simd_reduce_add_ordered(x, -1);
        assert_eq!(r, 5_i32);
        let r: i32 = simd_reduce_mul_ordered(x, -1);
        assert_eq!(r, 24_i32);

        let r: i32 = simd_reduce_min(x);
        assert_eq!(r, -2_i32);
        let r: i32 = simd_reduce_max(x);
        assert_eq!(r, 4_i32);

        let x = i32x4(-1, -1, -1, -1);
        let r: i32 = simd_reduce_and(x);
        assert_eq!(r, -1_i32);
        let r: i32 = simd_reduce_or(x);
        assert_eq!(r, -1_i32);
        let r: i32 = simd_reduce_xor(x);
        assert_eq!(r, 0_i32);

        let x = i32x4(-1, -1, 0, -1);
        let r: i32 = simd_reduce_and(x);
        assert_eq!(r, 0_i32);
        let r: i32 = simd_reduce_or(x);
        assert_eq!(r, -1_i32);
        let r: i32 = simd_reduce_xor(x);
        assert_eq!(r, -1_i32);
    }

    unsafe {
        let x = u32x4(1, 2, 3, 4);
        let r: u32 = simd_reduce_add_unordered(x);
        assert_eq!(r, 10_u32);
        let r: u32 = simd_reduce_mul_unordered(x);
        assert_eq!(r, 24_u32);
        let r: u32 = simd_reduce_add_ordered(x, 1);
        assert_eq!(r, 11_u32);
        let r: u32 = simd_reduce_mul_ordered(x, 2);
        assert_eq!(r, 48_u32);

        let r: u32 = simd_reduce_min(x);
        assert_eq!(r, 1_u32);
        let r: u32 = simd_reduce_max(x);
        assert_eq!(r, 4_u32);

        let t = u32::MAX;
        let x = u32x4(t, t, t, t);
        let r: u32 = simd_reduce_and(x);
        assert_eq!(r, t);
        let r: u32 = simd_reduce_or(x);
        assert_eq!(r, t);
        let r: u32 = simd_reduce_xor(x);
        assert_eq!(r, 0_u32);

        let x = u32x4(t, t, 0, t);
        let r: u32 = simd_reduce_and(x);
        assert_eq!(r, 0_u32);
        let r: u32 = simd_reduce_or(x);
        assert_eq!(r, t);
        let r: u32 = simd_reduce_xor(x);
        assert_eq!(r, t);
    }

    unsafe {
        let x = f32x4(1., -2., 3., 4.);
        let r: f32 = simd_reduce_add_unordered(x);
        assert_eq!(r, 6_f32);
        let r: f32 = simd_reduce_mul_unordered(x);
        assert_eq!(r, -24_f32);
        let r: f32 = simd_reduce_add_ordered(x, 0.);
        assert_eq!(r, 6_f32);
        let r: f32 = simd_reduce_mul_ordered(x, 1.);
        assert_eq!(r, -24_f32);
        let r: f32 = simd_reduce_add_ordered(x, 1.);
        assert_eq!(r, 7_f32);
        let r: f32 = simd_reduce_mul_ordered(x, 2.);
        assert_eq!(r, -48_f32);

        let r: f32 = simd_reduce_min(x);
        assert_eq!(r, -2_f32);
        let r: f32 = simd_reduce_max(x);
        assert_eq!(r, 4_f32);
        let r: f32 = simd_reduce_min_nanless(x);
        assert_eq!(r, -2_f32);
        let r: f32 = simd_reduce_max_nanless(x);
        assert_eq!(r, 4_f32);
    }

    unsafe {
        let x = b8x4(!0, !0, !0, !0);
        let r: bool = simd_reduce_all(x);
        assert_eq!(r, true);
        let r: bool = simd_reduce_any(x);
        assert_eq!(r, true);

        let x = b8x4(!0, !0, 0, !0);
        let r: bool = simd_reduce_all(x);
        assert_eq!(r, false);
        let r: bool = simd_reduce_any(x);
        assert_eq!(r, true);

        let x = b8x4(0, 0, 0, 0);
        let r: bool = simd_reduce_all(x);
        assert_eq!(r, false);
        let r: bool = simd_reduce_any(x);
        assert_eq!(r, false);
    }
}