Debian Codegraph @

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

raw
1 // RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror | FileCheck %s
2 // RUN: %clang_cc1 -x c -ffreestanding %s -triple=i386-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror | FileCheck %s
3 // RUN: %clang_cc1 -x c++ -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror | FileCheck %s
4 // RUN: %clang_cc1 -x c++ -ffreestanding %s -triple=i386-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror | FileCheck %s
5
6 // RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s
7 // RUN: %clang_cc1 -x c -ffreestanding %s -triple=i386-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s
8 // RUN: %clang_cc1 -x c++ -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s
9 // RUN: %clang_cc1 -x c++ -ffreestanding %s -triple=i386-apple-darwin -target-feature +f16c -emit-llvm -o - -Wall -Werror -fexperimental-new-constant-interpreter | FileCheck %s
10
11
12 #include <immintrin.h>
13 #include "builtin_test_helpers.h"
14
15 float test_cvtsh_ss(unsigned short a) {
16 // CHECK-LABEL: test_cvtsh_ss
17 // CHECK: [[CONV:%.*]] = fpext half %{{.*}} to float
18 // CHECK: ret float [[CONV]]
19 return _cvtsh_ss(a);
20 }
21
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
26 unsigned short test_cvtss_sh(float a) {
27 // CHECK-LABEL: test_cvtss_sh
28 // CHECK: insertelement <4 x float> poison, float %{{.*}}, i32 0
29 // CHECK: insertelement <4 x float> %{{.*}}, float 0.000000e+00, i32 1
30 // CHECK: insertelement <4 x float> %{{.*}}, float 0.000000e+00, i32 2
31 // CHECK: insertelement <4 x float> %{{.*}}, float 0.000000e+00, i32 3
32 // CHECK: call <8 x i16> @llvm.x86.vcvtps2ph.128(<4 x float> %{{.*}}, i32 0)
33 // CHECK: extractelement <8 x i16> %{{.*}}, i32 0
34 return _cvtss_sh(a, 0);
35 }
36
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
42 __m128 test_mm_cvtph_ps(__m128i a) {
43 // CHECK-LABEL: test_mm_cvtph_ps
44 // CHECK: shufflevector <8 x i16> %{{.*}}, <8 x i16> %{{.*}}, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
45 // CHECK: fpext <4 x half> %{{.*}} to <4 x float>
46 return _mm_cvtph_ps(a);
47 }
48
49 // A value exactly halfway between 1.0 and the next representable FP16 number.
50 // In binary, its significand ends in ...000, followed by a tie-bit 1.
51 #define POS_HALFWAY (1.0f + 0.00048828125f) // 1.0 + 2^-11, a tie-breaking case
52
53 //
54 // _mm_cvtps_ph (128-bit, 4 floats -> 8 shorts, 4 are zero-padded)
55 //
56 // Test values: -2.5f, 1.123f, POS_HALFWAY
57 TEST_CONSTEXPR(match_v8hi(
58 _mm_cvtps_ph(_mm_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_NEAREST_INT),
59 0xC100, 0x3C7E, 0x3C00, 0x0000, 0, 0, 0, 0
60 ));
61 TEST_CONSTEXPR(match_v8hi(
62 _mm_cvtps_ph(_mm_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_NEG_INF),
63 0xC100, 0x3C7D, 0x3C00, 0x0000, 0, 0, 0, 0
64 ));
65 TEST_CONSTEXPR(match_v8hi(
66 _mm_cvtps_ph(_mm_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_POS_INF),
67 0xC100, 0x3C7E, 0x3C01, 0x0000, 0, 0, 0, 0
68 ));
69 TEST_CONSTEXPR(match_v8hi(
70 _mm_cvtps_ph(_mm_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_ZERO),
71 0xC100, 0x3C7D, 0x3C00, 0x0000, 0, 0, 0, 0
72 ));
73
74 __m256 test_mm256_cvtph_ps(__m128i a) {
75 // CHECK-LABEL: test_mm256_cvtph_ps
76 // CHECK: fpext <8 x half> %{{.*}} to <8 x float>
77 return _mm256_cvtph_ps(a);
78 }
79 TEST_CONSTEXPR(match_m256(
80 _mm256_cvtph_ps(_mm_setr_epi16(0x3C00, 0x4000, 0x4200, 0x4400, 0x4500, 0x3800, 0xC000, 0x0000)),
81 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 0.5f, -2.0f, 0.0f
82 ));
83
84 //
85 // _mm256_cvtps_ph (256-bit, 8 floats -> 8 shorts)
86 //
87 // Test values: -2.5f, 1.123f, POS_HALFWAY
88 TEST_CONSTEXPR(match_v8hi(
89 _mm256_cvtps_ph(_mm256_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f, -2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_NEAREST_INT),
90 0xC100, 0x3C7E, 0x3C00, 0x0000, 0xC100, 0x3C7E, 0x3C00, 0x0000
91 ));
92 TEST_CONSTEXPR(match_v8hi(
93 _mm256_cvtps_ph(_mm256_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f, -2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_NEG_INF),
94 0xC100, 0x3C7D, 0x3C00, 0x0000, 0xC100, 0x3C7D, 0x3C00, 0x0000
95 ));
96 TEST_CONSTEXPR(match_v8hi(
97 _mm256_cvtps_ph(_mm256_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f, -2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_POS_INF),
98 0xC100, 0x3C7E, 0x3C01, 0x0000, 0xC100, 0x3C7E, 0x3C01, 0x0000
99 ));
100 TEST_CONSTEXPR(match_v8hi(
101 _mm256_cvtps_ph(_mm256_setr_ps(-2.5f, 1.123f, POS_HALFWAY, 0.0f, -2.5f, 1.123f, POS_HALFWAY, 0.0f), _MM_FROUND_TO_ZERO),
102 0xC100, 0x3C7D, 0x3C00, 0x0000, 0xC100, 0x3C7D, 0x3C00, 0x0000
103 ));
104
105 __m128i test_mm_cvtps_ph(__m128 a) {
106 // CHECK-LABEL: test_mm_cvtps_ph
107 // CHECK: call <8 x i16> @llvm.x86.vcvtps2ph.128(<4 x float> %{{.*}}, i32 0)
108 return _mm_cvtps_ph(a, 0);
109 }
110
111 //
112 // Tests for Exact Dynamic Rounding
113 //
114 // Test that dynamic rounding SUCCEEDS for exactly representable values.
115 // We use _MM_FROUND_CUR_DIRECTION (value 4) to specify dynamic rounding.
116 // Inputs: -2.5f, 0.125f, -16.0f are all exactly representable in FP16.
117 TEST_CONSTEXPR(match_v8hi(
118 __builtin_ia32_vcvtps2ph256(_mm256_setr_ps(-2.5f, 0.125f, -16.0f, 0.0f, -2.5f, 0.125f, -16.0f, 0.0f), _MM_FROUND_CUR_DIRECTION),
119 0xC100, 0x3000, 0xCC00, 0x0000, 0xC100, 0x3000, 0xCC00, 0x0000
120 ));
121
122 __m128i test_mm256_cvtps_ph(__m256 a) {
123 // CHECK-LABEL: test_mm256_cvtps_ph
124 // CHECK: call <8 x i16> @llvm.x86.vcvtps2ph.256(<8 x float> %{{.*}}, i32 0)
125 return _mm256_cvtps_ph(a, 0);
126 }