Clang Project

clang_source_code/test/CodeGen/sse41-builtins.c
1// RUN: %clang_cc1 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +sse4.1 -emit-llvm -o - -Wall -Werror | FileCheck %s
2// RUN: %clang_cc1 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +sse4.1 -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s
3
4
5#include <immintrin.h>
6
7// NOTE: This should match the tests in llvm/test/CodeGen/X86/sse41-intrinsics-fast-isel.ll
8
9__m128i test_mm_blend_epi16(__m128i V1, __m128i V2) {
10  // CHECK-LABEL: test_mm_blend_epi16
11  // CHECK: shufflevector <8 x i16> %{{.*}}, <8 x i16> %{{.*}}, <8 x i32> <i32 0, i32 9, i32 2, i32 11, i32 4, i32 13, i32 6, i32 7>
12  return _mm_blend_epi16(V1, V2, 42);
13}
14
15__m128d test_mm_blend_pd(__m128d V1, __m128d V2) {
16  // CHECK-LABEL: test_mm_blend_pd
17  // CHECK: shufflevector <2 x double> %{{.*}}, <2 x double> %{{.*}}, <2 x i32> <i32 0, i32 3>
18  return _mm_blend_pd(V1, V2, 2);
19}
20
21__m128 test_mm_blend_ps(__m128 V1, __m128 V2) {
22  // CHECK-LABEL: test_mm_blend_ps
23  // CHECK: shufflevector <4 x float> %{{.*}}, <4 x float> %{{.*}}, <4 x i32> <i32 0, i32 5, i32 6, i32 3>
24  return _mm_blend_ps(V1, V2, 6);
25}
26
27__m128i test_mm_blendv_epi8(__m128i V1, __m128i V2, __m128i V3) {
28  // CHECK-LABEL: test_mm_blendv_epi8
29  // CHECK: call <16 x i8> @llvm.x86.sse41.pblendvb(<16 x i8> %{{.*}}, <16 x i8> %{{.*}}, <16 x i8> %{{.*}})
30  return _mm_blendv_epi8(V1, V2, V3);
31}
32
33__m128d test_mm_blendv_pd(__m128d V1, __m128d V2, __m128d V3) {
34  // CHECK-LABEL: test_mm_blendv_pd
35  // CHECK: call <2 x double> @llvm.x86.sse41.blendvpd(<2 x double> %{{.*}}, <2 x double> %{{.*}}, <2 x double> %{{.*}})
36  return _mm_blendv_pd(V1, V2, V3);
37}
38
39__m128 test_mm_blendv_ps(__m128 V1, __m128 V2, __m128 V3) {
40  // CHECK-LABEL: test_mm_blendv_ps
41  // CHECK: call <4 x float> @llvm.x86.sse41.blendvps(<4 x float> %{{.*}}, <4 x float> %{{.*}}, <4 x float> %{{.*}})
42  return _mm_blendv_ps(V1, V2, V3);
43}
44
45__m128d test_mm_ceil_pd(__m128d x) {
46  // CHECK-LABEL: test_mm_ceil_pd
47  // CHECK: call <2 x double> @llvm.x86.sse41.round.pd(<2 x double> %{{.*}}, i32 2)
48  return _mm_ceil_pd(x);
49}
50
51__m128 test_mm_ceil_ps(__m128 x) {
52  // CHECK-LABEL: test_mm_ceil_ps
53  // CHECK: call <4 x float> @llvm.x86.sse41.round.ps(<4 x float> %{{.*}}, i32 2)
54  return _mm_ceil_ps(x);
55}
56
57__m128d test_mm_ceil_sd(__m128d x, __m128d y) {
58  // CHECK-LABEL: test_mm_ceil_sd
59  // CHECK: call <2 x double> @llvm.x86.sse41.round.sd(<2 x double> %{{.*}}, <2 x double> %{{.*}}, i32 2)
60  return _mm_ceil_sd(x, y);
61}
62
63__m128 test_mm_ceil_ss(__m128 x, __m128 y) {
64  // CHECK-LABEL: test_mm_ceil_ss
65  // CHECK: call <4 x float> @llvm.x86.sse41.round.ss(<4 x float> %{{.*}}, <4 x float> %{{.*}}, i32 2)
66  return _mm_ceil_ss(x, y);
67}
68
69__m128i test_mm_cmpeq_epi64(__m128i A, __m128i B) {
70  // CHECK-LABEL: test_mm_cmpeq_epi64
71  // CHECK: icmp eq <2 x i64>
72  // CHECK: sext <2 x i1> %{{.*}} to <2 x i64>
73  return _mm_cmpeq_epi64(A, B);
74}
75
76__m128i test_mm_cvtepi8_epi16(__m128i a) {
77  // CHECK-LABEL: test_mm_cvtepi8_epi16
78  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
79  // CHECK: sext <8 x i8> {{.*}} to <8 x i16>
80  return _mm_cvtepi8_epi16(a);
81}
82
83__m128i test_mm_cvtepi8_epi32(__m128i a) {
84  // CHECK-LABEL: test_mm_cvtepi8_epi32
85  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
86  // CHECK: sext <4 x i8> {{.*}} to <4 x i32>
87  return _mm_cvtepi8_epi32(a);
88}
89
90__m128i test_mm_cvtepi8_epi64(__m128i a) {
91  // CHECK-LABEL: test_mm_cvtepi8_epi64
92  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <2 x i32> <i32 0, i32 1>
93  // CHECK: sext <2 x i8> {{.*}} to <2 x i64>
94  return _mm_cvtepi8_epi64(a);
95}
96
97__m128i test_mm_cvtepi16_epi32(__m128i a) {
98  // CHECK-LABEL: test_mm_cvtepi16_epi32
99  // CHECK: shufflevector <8 x i16> {{.*}}, <8 x i16> {{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
100  // CHECK: sext <4 x i16> {{.*}} to <4 x i32>
101  return _mm_cvtepi16_epi32(a);
102}
103
104__m128i test_mm_cvtepi16_epi64(__m128i a) {
105  // CHECK-LABEL: test_mm_cvtepi16_epi64
106  // CHECK: shufflevector <8 x i16> {{.*}}, <8 x i16> {{.*}}, <2 x i32> <i32 0, i32 1>
107  // CHECK: sext <2 x i16> {{.*}} to <2 x i64>
108  return _mm_cvtepi16_epi64(a);
109}
110
111__m128i test_mm_cvtepi32_epi64(__m128i a) {
112  // CHECK-LABEL: test_mm_cvtepi32_epi64
113  // CHECK: shufflevector <4 x i32> {{.*}}, <4 x i32> {{.*}}, <2 x i32> <i32 0, i32 1>
114  // CHECK: sext <2 x i32> {{.*}} to <2 x i64>
115  return _mm_cvtepi32_epi64(a);
116}
117
118__m128i test_mm_cvtepu8_epi16(__m128i a) {
119  // CHECK-LABEL: test_mm_cvtepu8_epi16
120  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
121  // CHECK: zext <8 x i8> {{.*}} to <8 x i16>
122  return _mm_cvtepu8_epi16(a);
123}
124
125__m128i test_mm_cvtepu8_epi32(__m128i a) {
126  // CHECK-LABEL: test_mm_cvtepu8_epi32
127  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
128  // CHECK: zext <4 x i8> {{.*}} to <4 x i32>
129  return _mm_cvtepu8_epi32(a);
130}
131
132__m128i test_mm_cvtepu8_epi64(__m128i a) {
133  // CHECK-LABEL: test_mm_cvtepu8_epi64
134  // CHECK: shufflevector <16 x i8> {{.*}}, <16 x i8> {{.*}}, <2 x i32> <i32 0, i32 1>
135  // CHECK: zext <2 x i8> {{.*}} to <2 x i64>
136  return _mm_cvtepu8_epi64(a);
137}
138
139__m128i test_mm_cvtepu16_epi32(__m128i a) {
140  // CHECK-LABEL: test_mm_cvtepu16_epi32
141  // CHECK: shufflevector <8 x i16> {{.*}}, <8 x i16> {{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
142  // CHECK: zext <4 x i16> {{.*}} to <4 x i32>
143  return _mm_cvtepu16_epi32(a);
144}
145
146__m128i test_mm_cvtepu16_epi64(__m128i a) {
147  // CHECK-LABEL: test_mm_cvtepu16_epi64
148  // CHECK: shufflevector <8 x i16> {{.*}}, <8 x i16> {{.*}}, <2 x i32> <i32 0, i32 1>
149  // CHECK: zext <2 x i16> {{.*}} to <2 x i64>
150  return _mm_cvtepu16_epi64(a);
151}
152
153__m128i test_mm_cvtepu32_epi64(__m128i a) {
154  // CHECK-LABEL: test_mm_cvtepu32_epi64
155  // CHECK: shufflevector <4 x i32> {{.*}}, <4 x i32> {{.*}}, <2 x i32> <i32 0, i32 1>
156  // CHECK: zext <2 x i32> {{.*}} to <2 x i64>
157  return _mm_cvtepu32_epi64(a);
158}
159
160__m128d test_mm_dp_pd(__m128d x, __m128d y) {
161  // CHECK-LABEL: test_mm_dp_pd
162  // CHECK: call <2 x double> @llvm.x86.sse41.dppd(<2 x double> {{.*}}, <2 x double> {{.*}}, i8 7)
163  return _mm_dp_pd(x, y, 7);
164}
165
166__m128 test_mm_dp_ps(__m128 x, __m128 y) {
167  // CHECK-LABEL: test_mm_dp_ps
168  // CHECK: call <4 x float> @llvm.x86.sse41.dpps(<4 x float> {{.*}}, <4 x float> {{.*}}, i8 7)
169  return _mm_dp_ps(x, y, 7);
170}
171
172int test_mm_extract_epi8(__m128i x) {
173  // CHECK-LABEL: test_mm_extract_epi8
174  // CHECK: extractelement <16 x i8> %{{.*}}, {{i32|i64}} 1
175  // CHECK: zext i8 %{{.*}} to i32
176  return _mm_extract_epi8(x, 1);
177}
178
179int test_mm_extract_epi32(__m128i x) {
180  // CHECK-LABEL: test_mm_extract_epi32
181  // CHECK: extractelement <4 x i32> %{{.*}}, {{i32|i64}} 1
182  return _mm_extract_epi32(x, 1);
183}
184
185long long test_mm_extract_epi64(__m128i x) {
186  // CHECK-LABEL: test_mm_extract_epi64
187  // CHECK: extractelement <2 x i64> %{{.*}}, {{i32|i64}} 1
188  return _mm_extract_epi64(x, 1);
189}
190
191int test_mm_extract_ps(__m128 x) {
192  // CHECK-LABEL: test_mm_extract_ps
193  // CHECK: extractelement <4 x float> %{{.*}}, {{i32|i64}} 1
194  return _mm_extract_ps(x, 1);
195}
196
197__m128d test_mm_floor_pd(__m128d x) {
198  // CHECK-LABEL: test_mm_floor_pd
199  // CHECK: call <2 x double> @llvm.x86.sse41.round.pd(<2 x double> %{{.*}}, i32 1)
200  return _mm_floor_pd(x);
201}
202
203__m128 test_mm_floor_ps(__m128 x) {
204  // CHECK-LABEL: test_mm_floor_ps
205  // CHECK: call <4 x float> @llvm.x86.sse41.round.ps(<4 x float> %{{.*}}, i32 1)
206  return _mm_floor_ps(x);
207}
208
209__m128d test_mm_floor_sd(__m128d x, __m128d y) {
210  // CHECK-LABEL: test_mm_floor_sd
211  // CHECK: call <2 x double> @llvm.x86.sse41.round.sd(<2 x double> %{{.*}}, <2 x double> %{{.*}}, i32 1)
212  return _mm_floor_sd(x, y);
213}
214
215__m128 test_mm_floor_ss(__m128 x, __m128 y) {
216  // CHECK-LABEL: test_mm_floor_ss
217  // CHECK: call <4 x float> @llvm.x86.sse41.round.ss(<4 x float> %{{.*}}, <4 x float> %{{.*}}, i32 1)
218  return _mm_floor_ss(x, y);
219}
220
221__m128i test_mm_insert_epi8(__m128i x, char b) {
222  // CHECK-LABEL: test_mm_insert_epi8
223  // CHECK: insertelement <16 x i8> %{{.*}}, i8 %{{.*}}, {{i32|i64}} 1
224  return _mm_insert_epi8(x, b, 1);
225}
226
227__m128i test_mm_insert_epi32(__m128i x, int b) {
228  // CHECK-LABEL: test_mm_insert_epi32
229  // CHECK: insertelement <4 x i32> %{{.*}}, i32 %{{.*}}, {{i32|i64}} 1
230  return _mm_insert_epi32(x, b, 1);
231}
232
233__m128i test_mm_insert_epi64(__m128i x, long long b) {
234  // CHECK-LABEL: test_mm_insert_epi64
235  // CHECK: insertelement <2 x i64> %{{.*}}, i64 %{{.*}}, {{i32|i64}} 1
236  return _mm_insert_epi64(x, b, 1);
237}
238
239__m128 test_mm_insert_ps(__m128 x, __m128 y) {
240  // CHECK-LABEL: test_mm_insert_ps
241  // CHECK: call <4 x float> @llvm.x86.sse41.insertps(<4 x float> %{{.*}}, <4 x float> %{{.*}}, i8 4)
242  return _mm_insert_ps(x, y, 4);
243}
244
245__m128i test_mm_max_epi8(__m128i x, __m128i y) {
246  // CHECK-LABEL: test_mm_max_epi8
247  // CHECK:       [[CMP:%.*]] = icmp sgt <16 x i8> [[X:%.*]], [[Y:%.*]]
248  // CHECK-NEXT:  select <16 x i1> [[CMP]], <16 x i8> [[X]], <16 x i8> [[Y]]
249  return _mm_max_epi8(x, y);
250}
251
252__m128i test_mm_max_epi32(__m128i x, __m128i y) {
253  // CHECK-LABEL: test_mm_max_epi32
254  // CHECK:       [[CMP:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
255  // CHECK-NEXT:  select <4 x i1> [[CMP]], <4 x i32> [[X]], <4 x i32> [[Y]]
256  return _mm_max_epi32(x, y);
257}
258
259__m128i test_mm_max_epu16(__m128i x, __m128i y) {
260  // CHECK-LABEL: test_mm_max_epu16
261  // CHECK:       [[CMP:%.*]] = icmp ugt <8 x i16> [[X:%.*]], [[Y:%.*]]
262  // CHECK-NEXT:  select <8 x i1> [[CMP]], <8 x i16> [[X]], <8 x i16> [[Y]]
263  return _mm_max_epu16(x, y);
264}
265
266__m128i test_mm_max_epu32(__m128i x, __m128i y) {
267  // CHECK-LABEL: test_mm_max_epu32
268  // CHECK:       [[CMP:%.*]] = icmp ugt <4 x i32> [[X:%.*]], [[Y:%.*]]
269  // CHECK-NEXT:  select <4 x i1> [[CMP]], <4 x i32> [[X]], <4 x i32> [[Y]]
270  return _mm_max_epu32(x, y);
271}
272
273__m128i test_mm_min_epi8(__m128i x, __m128i y) {
274  // CHECK-LABEL: test_mm_min_epi8
275  // CHECK:       [[CMP:%.*]] = icmp slt <16 x i8> [[X:%.*]], [[Y:%.*]]
276  // CHECK-NEXT:  select <16 x i1> [[CMP]], <16 x i8> [[X]], <16 x i8> [[Y]]
277  return _mm_min_epi8(x, y);
278}
279
280__m128i test_mm_min_epi32(__m128i x, __m128i y) {
281  // CHECK-LABEL: test_mm_min_epi32
282  // CHECK:       [[CMP:%.*]] = icmp slt <4 x i32> [[X:%.*]], [[Y:%.*]]
283  // CHECK-NEXT:  select <4 x i1> [[CMP]], <4 x i32> [[X]], <4 x i32> [[Y]]
284  return _mm_min_epi32(x, y);
285}
286
287__m128i test_mm_min_epu16(__m128i x, __m128i y) {
288  // CHECK-LABEL: test_mm_min_epu16
289  // CHECK:       [[CMP:%.*]] = icmp ult <8 x i16> [[X:%.*]], [[Y:%.*]]
290  // CHECK-NEXT:  select <8 x i1> [[CMP]], <8 x i16> [[X]], <8 x i16> [[Y]]
291  return _mm_min_epu16(x, y);
292}
293
294__m128i test_mm_min_epu32(__m128i x, __m128i y) {
295  // CHECK-LABEL: test_mm_min_epu32
296  // CHECK:       [[CMP:%.*]] = icmp ult <4 x i32> [[X:%.*]], [[Y:%.*]]
297  // CHECK-NEXT:  select <4 x i1> [[CMP]], <4 x i32> [[X]], <4 x i32> [[Y]]
298  return _mm_min_epu32(x, y);
299}
300
301__m128i test_mm_minpos_epu16(__m128i x) {
302  // CHECK-LABEL: test_mm_minpos_epu16
303  // CHECK: call <8 x i16> @llvm.x86.sse41.phminposuw(<8 x i16> %{{.*}})
304  return _mm_minpos_epu16(x);
305}
306
307__m128i test_mm_mpsadbw_epu8(__m128i x, __m128i y) {
308  // CHECK-LABEL: test_mm_mpsadbw_epu8
309  // CHECK: call <8 x i16> @llvm.x86.sse41.mpsadbw(<16 x i8> %{{.*}}, <16 x i8> %{{.*}}, i8 1)
310  return _mm_mpsadbw_epu8(x, y, 1);
311}
312
313__m128i test_mm_mul_epi32(__m128i x, __m128i y) {
314  // CHECK-LABEL: test_mm_mul_epi32
315  // CHECK: shl <2 x i64> %{{.*}}, <i64 32, i64 32>
316  // CHECK: ashr <2 x i64> %{{.*}}, <i64 32, i64 32>
317  // CHECK: shl <2 x i64> %{{.*}}, <i64 32, i64 32>
318  // CHECK: ashr <2 x i64> %{{.*}}, <i64 32, i64 32>
319  // CHECK: mul <2 x i64> %{{.*}}, %{{.*}}
320  return _mm_mul_epi32(x, y);
321}
322
323__m128i test_mm_mullo_epi32(__m128i x, __m128i y) {
324  // CHECK-LABEL: test_mm_mullo_epi32
325  // CHECK: mul <4 x i32>
326  return _mm_mullo_epi32(x, y);
327}
328
329__m128i test_mm_packus_epi32(__m128i x, __m128i y) {
330  // CHECK-LABEL: test_mm_packus_epi32
331  // CHECK: call <8 x i16> @llvm.x86.sse41.packusdw(<4 x i32> %{{.*}}, <4 x i32> %{{.*}})
332  return _mm_packus_epi32(x, y);
333}
334
335__m128d test_mm_round_pd(__m128d x) {
336  // CHECK-LABEL: test_mm_round_pd
337  // CHECK: call <2 x double> @llvm.x86.sse41.round.pd(<2 x double> %{{.*}}, i32 4)
338  return _mm_round_pd(x, 4);
339}
340
341__m128 test_mm_round_ps(__m128 x) {
342  // CHECK-LABEL: test_mm_round_ps
343  // CHECK: call <4 x float> @llvm.x86.sse41.round.ps(<4 x float> %{{.*}}, i32 4)
344  return _mm_round_ps(x, 4);
345}
346
347__m128d test_mm_round_sd(__m128d x, __m128d y) {
348  // CHECK-LABEL: test_mm_round_sd
349  // CHECK: call <2 x double> @llvm.x86.sse41.round.sd(<2 x double> %{{.*}}, <2 x double> %{{.*}}, i32 4)
350  return _mm_round_sd(x, y, 4);
351}
352
353__m128 test_mm_round_ss(__m128 x, __m128 y) {
354  // CHECK-LABEL: test_mm_round_ss
355  // CHECK: call <4 x float> @llvm.x86.sse41.round.ss(<4 x float> %{{.*}}, <4 x float> %{{.*}}, i32 4)
356  return _mm_round_ss(x, y, 4);
357}
358
359__m128i test_mm_stream_load_si128(__m128i const *a) {
360  // CHECK-LABEL: test_mm_stream_load_si128
361  // CHECK: load <2 x i64>, <2 x i64>* %{{.*}}, align 16, !nontemporal
362  return _mm_stream_load_si128(a);
363}
364
365int test_mm_test_all_ones(__m128i x) {
366  // CHECK-LABEL: test_mm_test_all_ones
367  // CHECK: call i32 @llvm.x86.sse41.ptestc(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
368  return _mm_test_all_ones(x);
369}
370
371int test_mm_test_all_zeros(__m128i x, __m128i y) {
372  // CHECK-LABEL: test_mm_test_all_zeros
373  // CHECK: call i32 @llvm.x86.sse41.ptestz(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
374  return _mm_test_all_zeros(x, y);
375}
376
377int test_mm_test_mix_ones_zeros(__m128i x, __m128i y) {
378  // CHECK-LABEL: test_mm_test_mix_ones_zeros
379  // CHECK: call i32 @llvm.x86.sse41.ptestnzc(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
380  return _mm_test_mix_ones_zeros(x, y);
381}
382
383int test_mm_testc_si128(__m128i x, __m128i y) {
384  // CHECK-LABEL: test_mm_testc_si128
385  // CHECK: call i32 @llvm.x86.sse41.ptestc(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
386  return _mm_testc_si128(x, y);
387}
388
389int test_mm_testnzc_si128(__m128i x, __m128i y) {
390  // CHECK-LABEL: test_mm_testnzc_si128
391  // CHECK: call i32 @llvm.x86.sse41.ptestnzc(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
392  return _mm_testnzc_si128(x, y);
393}
394
395int test_mm_testz_si128(__m128i x, __m128i y) {
396  // CHECK-LABEL: test_mm_testz_si128
397  // CHECK: call i32 @llvm.x86.sse41.ptestz(<2 x i64> %{{.*}}, <2 x i64> %{{.*}})
398  return _mm_testz_si128(x, y);
399}
400