Skip to content

Commit c955228

Browse files
committed
[clang][x86] Add constexpr support for MMX _mm_set*_pi* intrinsics
1 parent 6737ba4 commit c955228

File tree

3 files changed

+73
-41
lines changed

3 files changed

+73
-41
lines changed

clang/lib/Headers/mmintrin.h

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ typedef char __v16qi __attribute__((__vector_size__(16)));
4949
__min_vector_width__(128)))
5050
#endif
5151

52+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
53+
#define __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR __DEFAULT_FN_ATTRS_SSE2 constexpr
54+
#else
55+
#define __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR __DEFAULT_FN_ATTRS_SSE2
56+
#endif
57+
5258
#define __trunc64(x) \
5359
(__m64) __builtin_shufflevector((__v2di)(x), __extension__(__v2di){}, 0)
5460
#define __anyext128(x) \
@@ -1332,10 +1338,9 @@ _mm_cmpgt_pi32(__m64 __m1, __m64 __m2)
13321338
/// This intrinsic corresponds to the <c> PXOR </c> instruction.
13331339
///
13341340
/// \returns An initialized 64-bit integer vector with all elements set to zero.
1335-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1336-
_mm_setzero_si64(void)
1337-
{
1338-
return __extension__ (__m64){ 0LL };
1341+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1342+
_mm_setzero_si64(void) {
1343+
return __extension__(__m64){0LL};
13391344
}
13401345

13411346
/// Constructs a 64-bit integer vector initialized with the specified
@@ -1353,10 +1358,9 @@ _mm_setzero_si64(void)
13531358
/// A 32-bit integer value used to initialize the lower 32 bits of the
13541359
/// result.
13551360
/// \returns An initialized 64-bit integer vector.
1356-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1357-
_mm_set_pi32(int __i1, int __i0)
1358-
{
1359-
return __extension__ (__m64)(__v2si){__i0, __i1};
1361+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1362+
_mm_set_pi32(int __i1, int __i0) {
1363+
return __extension__(__m64)(__v2si){__i0, __i1};
13601364
}
13611365

13621366
/// Constructs a 64-bit integer vector initialized with the specified
@@ -1376,10 +1380,9 @@ _mm_set_pi32(int __i1, int __i0)
13761380
/// \param __s0
13771381
/// A 16-bit integer value used to initialize bits [15:0] of the result.
13781382
/// \returns An initialized 64-bit integer vector.
1379-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1380-
_mm_set_pi16(short __s3, short __s2, short __s1, short __s0)
1381-
{
1382-
return __extension__ (__m64)(__v4hi){__s0, __s1, __s2, __s3};
1383+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1384+
_mm_set_pi16(short __s3, short __s2, short __s1, short __s0) {
1385+
return __extension__(__m64)(__v4hi){__s0, __s1, __s2, __s3};
13831386
}
13841387

13851388
/// Constructs a 64-bit integer vector initialized with the specified
@@ -1407,12 +1410,11 @@ _mm_set_pi16(short __s3, short __s2, short __s1, short __s0)
14071410
/// \param __b0
14081411
/// An 8-bit integer value used to initialize bits [7:0] of the result.
14091412
/// \returns An initialized 64-bit integer vector.
1410-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1413+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
14111414
_mm_set_pi8(char __b7, char __b6, char __b5, char __b4, char __b3, char __b2,
1412-
char __b1, char __b0)
1413-
{
1414-
return __extension__ (__m64)(__v8qi){__b0, __b1, __b2, __b3,
1415-
__b4, __b5, __b6, __b7};
1415+
char __b1, char __b0) {
1416+
return __extension__(__m64)(__v8qi){__b0, __b1, __b2, __b3,
1417+
__b4, __b5, __b6, __b7};
14161418
}
14171419

14181420
/// Constructs a 64-bit integer vector of [2 x i32], with each of the
@@ -1428,10 +1430,9 @@ _mm_set_pi8(char __b7, char __b6, char __b5, char __b4, char __b3, char __b2,
14281430
/// A 32-bit integer value used to initialize each vector element of the
14291431
/// result.
14301432
/// \returns An initialized 64-bit integer vector of [2 x i32].
1431-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1432-
_mm_set1_pi32(int __i)
1433-
{
1434-
return _mm_set_pi32(__i, __i);
1433+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1434+
_mm_set1_pi32(int __i) {
1435+
return _mm_set_pi32(__i, __i);
14351436
}
14361437

14371438
/// Constructs a 64-bit integer vector of [4 x i16], with each of the
@@ -1447,10 +1448,9 @@ _mm_set1_pi32(int __i)
14471448
/// A 16-bit integer value used to initialize each vector element of the
14481449
/// result.
14491450
/// \returns An initialized 64-bit integer vector of [4 x i16].
1450-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1451-
_mm_set1_pi16(short __w)
1452-
{
1453-
return _mm_set_pi16(__w, __w, __w, __w);
1451+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1452+
_mm_set1_pi16(short __w) {
1453+
return _mm_set_pi16(__w, __w, __w, __w);
14541454
}
14551455

14561456
/// Constructs a 64-bit integer vector of [8 x i8], with each of the
@@ -1465,10 +1465,9 @@ _mm_set1_pi16(short __w)
14651465
/// An 8-bit integer value used to initialize each vector element of the
14661466
/// result.
14671467
/// \returns An initialized 64-bit integer vector of [8 x i8].
1468-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1469-
_mm_set1_pi8(char __b)
1470-
{
1471-
return _mm_set_pi8(__b, __b, __b, __b, __b, __b, __b, __b);
1468+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1469+
_mm_set1_pi8(char __b) {
1470+
return _mm_set_pi8(__b, __b, __b, __b, __b, __b, __b, __b);
14721471
}
14731472

14741473
/// Constructs a 64-bit integer vector, initialized in reverse order with
@@ -1486,10 +1485,9 @@ _mm_set1_pi8(char __b)
14861485
/// A 32-bit integer value used to initialize the upper 32 bits of the
14871486
/// result.
14881487
/// \returns An initialized 64-bit integer vector.
1489-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1490-
_mm_setr_pi32(int __i0, int __i1)
1491-
{
1492-
return _mm_set_pi32(__i1, __i0);
1488+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1489+
_mm_setr_pi32(int __i0, int __i1) {
1490+
return _mm_set_pi32(__i1, __i0);
14931491
}
14941492

14951493
/// Constructs a 64-bit integer vector, initialized in reverse order with
@@ -1509,10 +1507,9 @@ _mm_setr_pi32(int __i0, int __i1)
15091507
/// \param __w3
15101508
/// A 16-bit integer value used to initialize bits [63:48] of the result.
15111509
/// \returns An initialized 64-bit integer vector.
1512-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1513-
_mm_setr_pi16(short __w0, short __w1, short __w2, short __w3)
1514-
{
1515-
return _mm_set_pi16(__w3, __w2, __w1, __w0);
1510+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
1511+
_mm_setr_pi16(short __w0, short __w1, short __w2, short __w3) {
1512+
return _mm_set_pi16(__w3, __w2, __w1, __w0);
15161513
}
15171514

15181515
/// Constructs a 64-bit integer vector, initialized in reverse order with
@@ -1540,11 +1537,10 @@ _mm_setr_pi16(short __w0, short __w1, short __w2, short __w3)
15401537
/// \param __b7
15411538
/// An 8-bit integer value used to initialize bits [63:56] of the result.
15421539
/// \returns An initialized 64-bit integer vector.
1543-
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2
1540+
static __inline__ __m64 __DEFAULT_FN_ATTRS_SSE2_CONSTEXPR
15441541
_mm_setr_pi8(char __b0, char __b1, char __b2, char __b3, char __b4, char __b5,
1545-
char __b6, char __b7)
1546-
{
1547-
return _mm_set_pi8(__b7, __b6, __b5, __b4, __b3, __b2, __b1, __b0);
1542+
char __b6, char __b7) {
1543+
return _mm_set_pi8(__b7, __b6, __b5, __b4, __b3, __b2, __b1, __b0);
15481544
}
15491545

15501546
#undef __anyext128

clang/test/CodeGen/X86/builtin_test_helpers.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ constexpr bool match_m64(__m64 _v, unsigned long long a) {
99
return v[0] == a;
1010
}
1111

12+
constexpr bool match_v1di(__m64 v, long long a) {
13+
return v[0] == a;
14+
}
15+
16+
constexpr bool match_v2si(__m64 _v, int a, int b) {
17+
__v2si v = (__v2si)_v;
18+
return v[0] == a && v[1] == b;
19+
}
20+
21+
constexpr bool match_v4hi(__m64 _v, short a, short b, short c, short d) {
22+
__v4hi v = (__v4hi)_v;
23+
return v[0] == a && v[1] == b && v[2] == c && v[3] == d;
24+
}
25+
26+
constexpr bool match_v8qi(__m64 _v, char a, char b, char c, char d, char e, char f, char g, char h) {
27+
__v8qi v = (__v8qi)_v;
28+
return v[0] == a && v[1] == b && v[2] == c && v[3] == d && v[4] == e && v[5] == f && v[6] == g && v[7] == h;
29+
}
30+
1231
constexpr bool match_m128(__m128 v, float a, float b, float c, float d) {
1332
return v[0] == a && v[1] == b && v[2] == c && v[3] == d;
1433
}

clang/test/CodeGen/X86/mmx-builtins.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
#include <immintrin.h>
12+
#include "builtin_test_helpers.h"
1213

1314
__m64 test_mm_abs_pi8(__m64 a) {
1415
// CHECK-LABEL: test_mm_abs_pi8
@@ -409,6 +410,7 @@ __m64 test_mm_set_pi8(char a, char b, char c, char d, char e, char f, char g, ch
409410
// CHECK: insertelement <8 x i8>
410411
return _mm_set_pi8(a, b, c, d, e, f, g, h);
411412
}
413+
TEST_CONSTEXPR(match_v8qi(_mm_set_pi8(0, -1, 2, -3, 4, -5, 6, -7), -7, 6, -5, 4, -3, 2, -1, 0));
412414

413415
__m64 test_mm_set_pi16(short a, short b, short c, short d) {
414416
// CHECK-LABEL: test_mm_set_pi16
@@ -418,13 +420,15 @@ __m64 test_mm_set_pi16(short a, short b, short c, short d) {
418420
// CHECK: insertelement <4 x i16>
419421
return _mm_set_pi16(a, b, c, d);
420422
}
423+
TEST_CONSTEXPR(match_v4hi(_mm_set_pi16(101, 102, -103, -104), -104, -103, 102, 101));
421424

422425
__m64 test_mm_set_pi32(int a, int b) {
423426
// CHECK-LABEL: test_mm_set_pi32
424427
// CHECK: insertelement <2 x i32>
425428
// CHECK: insertelement <2 x i32>
426429
return _mm_set_pi32(a, b);
427430
}
431+
TEST_CONSTEXPR(match_v2si(_mm_set_pi32(5000, -1500), -1500, 5000));
428432

429433
__m64 test_mm_setr_pi8(char a, char b, char c, char d, char e, char f, char g, char h) {
430434
// CHECK-LABEL: test_mm_setr_pi8
@@ -438,6 +442,7 @@ __m64 test_mm_setr_pi8(char a, char b, char c, char d, char e, char f, char g, c
438442
// CHECK: insertelement <8 x i8>
439443
return _mm_setr_pi8(a, b, c, d, e, f, g, h);
440444
}
445+
TEST_CONSTEXPR(match_v8qi(_mm_setr_pi8(0, -1, 2, -3, 4, -5, 6, -7), 0, -1, 2, -3, 4, -5, 6, -7));
441446

442447
__m64 test_mm_setr_pi16(short a, short b, short c, short d) {
443448
// CHECK-LABEL: test_mm_setr_pi16
@@ -447,13 +452,22 @@ __m64 test_mm_setr_pi16(short a, short b, short c, short d) {
447452
// CHECK: insertelement <4 x i16>
448453
return _mm_setr_pi16(a, b, c, d);
449454
}
455+
TEST_CONSTEXPR(match_v4hi(_mm_setr_pi16(101, 102, -103, -104), 101, 102, -103, -104));
450456

451457
__m64 test_mm_setr_pi32(int a, int b) {
452458
// CHECK-LABEL: test_mm_setr_pi32
453459
// CHECK: insertelement <2 x i32>
454460
// CHECK: insertelement <2 x i32>
455461
return _mm_setr_pi32(a, b);
456462
}
463+
TEST_CONSTEXPR(match_v2si(_mm_setr_pi32(5000, -1500), 5000, -1500));
464+
465+
__m64 test_mm_setzero_si64() {
466+
// CHECK-LABEL: test_mm_setzero_si64
467+
// CHECK: zeroinitializer
468+
return _mm_setzero_si64();
469+
}
470+
TEST_CONSTEXPR(match_m64(_mm_setzero_si64(), 0ULL));
457471

458472
__m64 test_mm_set1_pi8(char a) {
459473
// CHECK-LABEL: test_mm_set1_pi8
@@ -467,6 +481,7 @@ __m64 test_mm_set1_pi8(char a) {
467481
// CHECK: insertelement <8 x i8>
468482
return _mm_set1_pi8(a);
469483
}
484+
TEST_CONSTEXPR(match_v8qi(_mm_set1_pi8(99), 99, 99, 99, 99, 99, 99, 99, 99));
470485

471486
__m64 test_mm_set1_pi16(short a) {
472487
// CHECK-LABEL: test_mm_set1_pi16
@@ -476,13 +491,15 @@ __m64 test_mm_set1_pi16(short a) {
476491
// CHECK: insertelement <4 x i16>
477492
return _mm_set1_pi16(a);
478493
}
494+
TEST_CONSTEXPR(match_v4hi(_mm_set1_pi16(-128), -128, -128, -128, -128));
479495

480496
__m64 test_mm_set1_pi32(int a) {
481497
// CHECK-LABEL: test_mm_set1_pi32
482498
// CHECK: insertelement <2 x i32>
483499
// CHECK: insertelement <2 x i32>
484500
return _mm_set1_pi32(a);
485501
}
502+
TEST_CONSTEXPR(match_v2si(_mm_set1_pi32(55), 55, 55));
486503

487504
__m64 test_mm_shuffle_pi8(__m64 a, __m64 b) {
488505
// CHECK-LABEL: test_mm_shuffle_pi8

0 commit comments

Comments
 (0)