blob: 1516946ec324ff2e68f13d2997387e14bc43ce11 [file] [log] [blame]
Craig Topper61f71c32013-09-27 03:57:18 +00001/*===---- f16cintrin.h - F16C intrinsics -----------------------------------===
Manman Rena45358c2012-10-11 00:59:55 +00002 *
Chandler Carruth4cf57432019-04-08 20:51:30 +00003 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Manman Rena45358c2012-10-11 00:59:55 +00006 *
7 *===-----------------------------------------------------------------------===
8 */
9
Craig Topper34c8c0d2018-05-22 18:54:19 +000010#if !defined __IMMINTRIN_H
11#error "Never use <f16cintrin.h> directly; include <immintrin.h> instead."
Manman Rena45358c2012-10-11 00:59:55 +000012#endif
13
Manman Rena45358c2012-10-11 00:59:55 +000014#ifndef __F16CINTRIN_H
15#define __F16CINTRIN_H
16
Eric Christopher4d1851682015-06-17 07:09:20 +000017/* Define the default attributes for the functions in this file. */
Craig Topper74c10e32018-07-09 19:00:16 +000018#define __DEFAULT_FN_ATTRS128 \
19 __attribute__((__always_inline__, __nodebug__, __target__("f16c"), __min_vector_width__(128)))
20#define __DEFAULT_FN_ATTRS256 \
21 __attribute__((__always_inline__, __nodebug__, __target__("f16c"), __min_vector_width__(256)))
Eric Christopher4d1851682015-06-17 07:09:20 +000022
Craig Topper73d1d402018-05-30 22:33:21 +000023/* NOTE: Intel documents the 128-bit versions of these as being in emmintrin.h,
24 * but that's because icc can emulate these without f16c using a library call.
25 * Since we don't do that let's leave these in f16cintrin.h.
26 */
Craig Topper25caca72018-05-22 22:19:19 +000027
28/// Converts a 16-bit half-precision float value into a 32-bit float
29/// value.
30///
31/// \headerfile <x86intrin.h>
32///
33/// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction.
34///
35/// \param __a
36/// A 16-bit half-precision float value.
37/// \returns The converted 32-bit float value.
Craig Topper74c10e32018-07-09 19:00:16 +000038static __inline float __DEFAULT_FN_ATTRS128
Craig Topper25caca72018-05-22 22:19:19 +000039_cvtsh_ss(unsigned short __a)
40{
41 __v8hi v = {(short)__a, 0, 0, 0, 0, 0, 0, 0};
42 __v4sf r = __builtin_ia32_vcvtph2ps(v);
43 return r[0];
44}
45
46/// Converts a 32-bit single-precision float value to a 16-bit
47/// half-precision float value.
48///
49/// \headerfile <x86intrin.h>
50///
51/// \code
52/// unsigned short _cvtss_sh(float a, const int imm);
53/// \endcode
54///
55/// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction.
56///
57/// \param a
58/// A 32-bit single-precision float value to be converted to a 16-bit
59/// half-precision float value.
60/// \param imm
61/// An immediate value controlling rounding using bits [2:0]: \n
62/// 000: Nearest \n
63/// 001: Down \n
64/// 010: Up \n
65/// 011: Truncate \n
66/// 1XX: Use MXCSR.RC for rounding
67/// \returns The converted 16-bit half-precision float value.
Craig Topperc6338672018-05-31 00:51:20 +000068#define _cvtss_sh(a, imm) \
Craig Topper25caca72018-05-22 22:19:19 +000069 (unsigned short)(((__v8hi)__builtin_ia32_vcvtps2ph((__v4sf){a, 0, 0, 0}, \
Martin Storsjocad7a5f2018-06-01 09:40:50 +000070 (imm)))[0])
Craig Topper25caca72018-05-22 22:19:19 +000071
72/// Converts a 128-bit vector containing 32-bit float values into a
73/// 128-bit vector containing 16-bit half-precision float values.
74///
75/// \headerfile <x86intrin.h>
76///
77/// \code
78/// __m128i _mm_cvtps_ph(__m128 a, const int imm);
79/// \endcode
80///
81/// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction.
82///
83/// \param a
84/// A 128-bit vector containing 32-bit float values.
85/// \param imm
86/// An immediate value controlling rounding using bits [2:0]: \n
87/// 000: Nearest \n
88/// 001: Down \n
89/// 010: Up \n
90/// 011: Truncate \n
91/// 1XX: Use MXCSR.RC for rounding
92/// \returns A 128-bit vector containing converted 16-bit half-precision float
93/// values. The lower 64 bits are used to store the converted 16-bit
94/// half-precision floating-point values.
Craig Topperc6338672018-05-31 00:51:20 +000095#define _mm_cvtps_ph(a, imm) \
Martin Storsjocad7a5f2018-06-01 09:40:50 +000096 (__m128i)__builtin_ia32_vcvtps2ph((__v4sf)(__m128)(a), (imm))
Craig Topper25caca72018-05-22 22:19:19 +000097
98/// Converts a 128-bit vector containing 16-bit half-precision float
99/// values into a 128-bit vector containing 32-bit float values.
100///
101/// \headerfile <x86intrin.h>
102///
103/// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction.
104///
105/// \param __a
106/// A 128-bit vector containing 16-bit half-precision float values. The lower
107/// 64 bits are used in the conversion.
108/// \returns A 128-bit vector of [4 x float] containing converted float values.
Craig Topper74c10e32018-07-09 19:00:16 +0000109static __inline __m128 __DEFAULT_FN_ATTRS128
Craig Topper25caca72018-05-22 22:19:19 +0000110_mm_cvtph_ps(__m128i __a)
111{
112 return (__m128)__builtin_ia32_vcvtph2ps((__v8hi)__a);
113}
Ekaterina Romanova08d1f242016-01-22 06:50:50 +0000114
Craig Topper34c8c0d2018-05-22 18:54:19 +0000115/// Converts a 256-bit vector of [8 x float] into a 128-bit vector
116/// containing 16-bit half-precision float values.
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000117///
118/// \headerfile <x86intrin.h>
119///
120/// \code
Craig Topper34c8c0d2018-05-22 18:54:19 +0000121/// __m128i _mm256_cvtps_ph(__m256 a, const int imm);
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000122/// \endcode
123///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000124/// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction.
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000125///
126/// \param a
Craig Topper34c8c0d2018-05-22 18:54:19 +0000127/// A 256-bit vector containing 32-bit single-precision float values to be
128/// converted to 16-bit half-precision float values.
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000129/// \param imm
Ekaterina Romanovadffe45b2016-12-27 00:49:38 +0000130/// An immediate value controlling rounding using bits [2:0]: \n
131/// 000: Nearest \n
132/// 001: Down \n
133/// 010: Up \n
134/// 011: Truncate \n
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000135/// 1XX: Use MXCSR.RC for rounding
Craig Topper34c8c0d2018-05-22 18:54:19 +0000136/// \returns A 128-bit vector containing the converted 16-bit half-precision
137/// float values.
Craig Topperc6338672018-05-31 00:51:20 +0000138#define _mm256_cvtps_ph(a, imm) \
Martin Storsjocad7a5f2018-06-01 09:40:50 +0000139 (__m128i)__builtin_ia32_vcvtps2ph256((__v8sf)(__m256)(a), (imm))
Manman Rena45358c2012-10-11 00:59:55 +0000140
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000141/// Converts a 128-bit vector containing 16-bit half-precision float
Craig Topper34c8c0d2018-05-22 18:54:19 +0000142/// values into a 256-bit vector of [8 x float].
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000143///
144/// \headerfile <x86intrin.h>
145///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000146/// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction.
Ekaterina Romanovaa61946d2016-02-10 00:12:24 +0000147///
Ekaterina Romanova1168fdc2016-05-16 22:54:45 +0000148/// \param __a
Craig Topper34c8c0d2018-05-22 18:54:19 +0000149/// A 128-bit vector containing 16-bit half-precision float values to be
150/// converted to 32-bit single-precision float values.
151/// \returns A vector of [8 x float] containing the converted 32-bit
152/// single-precision float values.
Craig Topper74c10e32018-07-09 19:00:16 +0000153static __inline __m256 __DEFAULT_FN_ATTRS256
Craig Topper34c8c0d2018-05-22 18:54:19 +0000154_mm256_cvtph_ps(__m128i __a)
Manman Rena45358c2012-10-11 00:59:55 +0000155{
Craig Topper34c8c0d2018-05-22 18:54:19 +0000156 return (__m256)__builtin_ia32_vcvtph2ps256((__v8hi)__a);
Manman Rena45358c2012-10-11 00:59:55 +0000157}
158
Craig Topper74c10e32018-07-09 19:00:16 +0000159#undef __DEFAULT_FN_ATTRS128
160#undef __DEFAULT_FN_ATTRS256
Eric Christopher4d1851682015-06-17 07:09:20 +0000161
Manman Rena45358c2012-10-11 00:59:55 +0000162#endif /* __F16CINTRIN_H */