summaryrefslogtreecommitdiffstats
path: root/third_party/rust/packed_simd/src/codegen/reductions/mask/x86.rs
blob: 4bf5098065452edc5b665a059485354294c53215 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Mask reductions implementation for `x86` and `x86_64` targets

#[cfg(target_feature = "sse")]
#[macro_use]
mod sse;

#[cfg(target_feature = "sse2")]
#[macro_use]
mod sse2;

#[cfg(target_feature = "avx")]
#[macro_use]
mod avx;

#[cfg(target_feature = "avx2")]
#[macro_use]
mod avx2;

/// x86 64-bit m8x8 implementation
macro_rules! x86_m8x8_impl {
    ($id:ident) => {
        fallback_impl!($id);
    };
}

/// x86 128-bit m8x16 implementation
macro_rules! x86_m8x16_impl {
    ($id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "sse2")] {
                x86_m8x16_sse2_impl!($id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// x86 128-bit m32x4 implementation
macro_rules! x86_m32x4_impl {
    ($id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "sse")] {
                x86_m32x4_sse_impl!($id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// x86 128-bit m64x2 implementation
macro_rules! x86_m64x2_impl {
    ($id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "sse2")] {
                x86_m64x2_sse2_impl!($id);
            } else if #[cfg(target_feature = "sse")] {
                x86_m32x4_sse_impl!($id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// x86 256-bit m8x32 implementation
macro_rules! x86_m8x32_impl {
    ($id:ident, $half_id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "avx2")] {
                x86_m8x32_avx2_impl!($id);
            } else if #[cfg(target_feature = "avx")] {
                x86_m8x32_avx_impl!($id);
            } else if #[cfg(target_feature = "sse2")] {
                recurse_half!($id, $half_id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// x86 256-bit m32x8 implementation
macro_rules! x86_m32x8_impl {
    ($id:ident, $half_id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "avx")] {
                x86_m32x8_avx_impl!($id);
            } else if #[cfg(target_feature = "sse")] {
                recurse_half!($id, $half_id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// x86 256-bit m64x4 implementation
macro_rules! x86_m64x4_impl {
    ($id:ident, $half_id:ident) => {
        cfg_if! {
            if #[cfg(target_feature = "avx")] {
                x86_m64x4_avx_impl!($id);
            } else if #[cfg(target_feature = "sse")] {
                recurse_half!($id, $half_id);
            } else {
                fallback_impl!($id);
            }
        }
    };
}

/// Fallback implementation.
macro_rules! x86_intr_impl {
    ($id:ident) => {
        impl All for $id {
            #[inline]
            unsafe fn all(self) -> bool {
                use crate::llvm::simd_reduce_all;
                simd_reduce_all(self.0)
            }
        }
        impl Any for $id {
            #[inline]
            unsafe fn any(self) -> bool {
                use crate::llvm::simd_reduce_any;
                simd_reduce_any(self.0)
            }
        }
    };
}

/// Mask reduction implementation for `x86` and `x86_64` targets
macro_rules! impl_mask_reductions {
    // 64-bit wide masks
    (m8x8) => {
        x86_m8x8_impl!(m8x8);
    };
    (m16x4) => {
        x86_m8x8_impl!(m16x4);
    };
    (m32x2) => {
        x86_m8x8_impl!(m32x2);
    };
    // 128-bit wide masks
    (m8x16) => {
        x86_m8x16_impl!(m8x16);
    };
    (m16x8) => {
        x86_m8x16_impl!(m16x8);
    };
    (m32x4) => {
        x86_m32x4_impl!(m32x4);
    };
    (m64x2) => {
        x86_m64x2_impl!(m64x2);
    };
    (m128x1) => {
        x86_intr_impl!(m128x1);
    };
    // 256-bit wide masks:
    (m8x32) => {
        x86_m8x32_impl!(m8x32, m8x16);
    };
    (m16x16) => {
        x86_m8x32_impl!(m16x16, m16x8);
    };
    (m32x8) => {
        x86_m32x8_impl!(m32x8, m32x4);
    };
    (m64x4) => {
        x86_m64x4_impl!(m64x4, m64x2);
    };
    (m128x2) => {
        x86_intr_impl!(m128x2);
    };
    (msizex2) => {
        cfg_if! {
            if #[cfg(target_pointer_width = "64")] {
                fallback_to_other_impl!(msizex2, m64x2);
            } else if #[cfg(target_pointer_width = "32")] {
                fallback_to_other_impl!(msizex2, m32x2);
            } else {
                compile_error!("unsupported target_pointer_width");
            }
        }
    };
    (msizex4) => {
        cfg_if! {
            if #[cfg(target_pointer_width = "64")] {
                fallback_to_other_impl!(msizex4, m64x4);
            } else if #[cfg(target_pointer_width = "32")] {
                fallback_to_other_impl!(msizex4, m32x4);
            } else {
                compile_error!("unsupported target_pointer_width");
            }
        }
    };
    (msizex8) => {
        cfg_if! {
            if #[cfg(target_pointer_width = "64")] {
                fallback_to_other_impl!(msizex8, m64x8);
            } else if #[cfg(target_pointer_width = "32")] {
                fallback_to_other_impl!(msizex8, m32x8);
            } else {
                compile_error!("unsupported target_pointer_width");
            }
        }
    };

    // Fallback to LLVM's default code-generation:
    ($id:ident) => {
        fallback_impl!($id);
    };
}