Skip to content

Commit acd0899

Browse files
authored
[X86] Add F16C f16 -> f32 constexpr support (#158142)
Fixes #154310
1 parent 8036edb commit acd0899

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

clang/lib/Headers/f16cintrin.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
#define __DEFAULT_FN_ATTRS256 \
2121
__attribute__((__always_inline__, __nodebug__, __target__("f16c"), __min_vector_width__(256)))
2222

23+
#if defined(__cplusplus) && (__cplusplus >= 201103L)
24+
#define __DEFAULT_FN_ATTRS128_CONSTEXPR __DEFAULT_FN_ATTRS128 constexpr
25+
#define __DEFAULT_FN_ATTRS256_CONSTEXPR __DEFAULT_FN_ATTRS256 constexpr
26+
#else
27+
#define __DEFAULT_FN_ATTRS128_CONSTEXPR __DEFAULT_FN_ATTRS128
28+
#define __DEFAULT_FN_ATTRS256_CONSTEXPR __DEFAULT_FN_ATTRS256
29+
#endif
30+
2331
/* NOTE: Intel documents the 128-bit versions of these as being in emmintrin.h,
2432
* but that's because icc can emulate these without f16c using a library call.
2533
* Since we don't do that let's leave these in f16cintrin.h.
@@ -35,7 +43,7 @@
3543
/// \param __a
3644
/// A 16-bit half-precision float value.
3745
/// \returns The converted 32-bit float value.
38-
static __inline float __DEFAULT_FN_ATTRS128
46+
static __inline float __DEFAULT_FN_ATTRS128_CONSTEXPR
3947
_cvtsh_ss(unsigned short __a)
4048
{
4149
return (float)__builtin_bit_cast(__fp16, __a);
@@ -104,7 +112,7 @@ _cvtsh_ss(unsigned short __a)
104112
/// A 128-bit vector containing 16-bit half-precision float values. The lower
105113
/// 64 bits are used in the conversion.
106114
/// \returns A 128-bit vector of [4 x float] containing converted float values.
107-
static __inline __m128 __DEFAULT_FN_ATTRS128
115+
static __inline __m128 __DEFAULT_FN_ATTRS128_CONSTEXPR
108116
_mm_cvtph_ps(__m128i __a)
109117
{
110118
typedef __fp16 __v4fp16 __attribute__((__vector_size__(8)));
@@ -151,7 +159,7 @@ _mm_cvtph_ps(__m128i __a)
151159
/// converted to 32-bit single-precision float values.
152160
/// \returns A vector of [8 x float] containing the converted 32-bit
153161
/// single-precision float values.
154-
static __inline __m256 __DEFAULT_FN_ATTRS256
162+
static __inline __m256 __DEFAULT_FN_ATTRS256_CONSTEXPR
155163
_mm256_cvtph_ps(__m128i __a)
156164
{
157165
typedef __fp16 __v8fp16 __attribute__((__vector_size__(16), __aligned__(16)));
@@ -161,5 +169,7 @@ _mm256_cvtph_ps(__m128i __a)
161169

162170
#undef __DEFAULT_FN_ATTRS128
163171
#undef __DEFAULT_FN_ATTRS256
172+
#undef __DEFAULT_FN_ATTRS128_CONSTEXPR
173+
#undef __DEFAULT_FN_ATTRS256_CONSTEXPR
164174

165175
#endif /* __F16CINTRIN_H */

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

100644100755
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
#include <immintrin.h>
13+
#include "builtin_test_helpers.h"
1314

1415
float test_cvtsh_ss(unsigned short a) {
1516
// CHECK-LABEL: test_cvtsh_ss
@@ -18,6 +19,10 @@ float test_cvtsh_ss(unsigned short a) {
1819
return _cvtsh_ss(a);
1920
}
2021

22+
TEST_CONSTEXPR(_cvtsh_ss(0x0000) == 0.0f);
23+
TEST_CONSTEXPR(_cvtsh_ss(0x4500) == 5.0f);
24+
TEST_CONSTEXPR(_cvtsh_ss(0xC000) == -2.0f);
25+
2126
unsigned short test_cvtss_sh(float a) {
2227
// CHECK-LABEL: test_cvtss_sh
2328
// CHECK: insertelement <4 x float> poison, float %{{.*}}, i32 0
@@ -29,6 +34,11 @@ unsigned short test_cvtss_sh(float a) {
2934
return _cvtss_sh(a, 0);
3035
}
3136

37+
TEST_CONSTEXPR(match_m128(
38+
_mm_cvtph_ps(_mm_setr_epi16(0x3C00, 0x4000, 0x4200, 0x4400, 0, 0, 0, 0)),
39+
1.0f, 2.0f, 3.0f, 4.0f
40+
));
41+
3242
__m128 test_mm_cvtph_ps(__m128i a) {
3343
// CHECK-LABEL: test_mm_cvtph_ps
3444
// CHECK: shufflevector <8 x i16> %{{.*}}, <8 x i16> %{{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
@@ -41,6 +51,10 @@ __m256 test_mm256_cvtph_ps(__m128i a) {
4151
// CHECK: fpext <8 x half> %{{.*}} to <8 x float>
4252
return _mm256_cvtph_ps(a);
4353
}
54+
TEST_CONSTEXPR(match_m256(
55+
_mm256_cvtph_ps(_mm_setr_epi16(0x3C00, 0x4000, 0x4200, 0x4400, 0x4500, 0x3800, 0xC000, 0x0000)),
56+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 0.5f, -2.0f, 0.0f
57+
));
4458

4559
__m128i test_mm_cvtps_ph(__m128 a) {
4660
// CHECK-LABEL: test_mm_cvtps_ph

0 commit comments

Comments
 (0)