Craig Topper | 61f71c3 | 2013-09-27 03:57:18 +0000 | [diff] [blame] | 1 | /*===---- f16cintrin.h - F16C intrinsics -----------------------------------=== |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 2 | * |
Craig Topper | 61f71c3 | 2013-09-27 03:57:18 +0000 | [diff] [blame] | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | * THE SOFTWARE. |
| 20 | * |
| 21 | *===-----------------------------------------------------------------------=== |
| 22 | */ |
| 23 | |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 24 | #if !defined __IMMINTRIN_H |
| 25 | #error "Never use <f16cintrin.h> directly; include <immintrin.h> instead." |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 28 | #ifndef __F16CINTRIN_H |
| 29 | #define __F16CINTRIN_H |
| 30 | |
Eric Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 31 | /* Define the default attributes for the functions in this file. */ |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 32 | #define __DEFAULT_FN_ATTRS \ |
Ekaterina Romanova | 08d1f24 | 2016-01-22 06:50:50 +0000 | [diff] [blame] | 33 | __attribute__((__always_inline__, __nodebug__, __target__("f16c"))) |
Eric Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 34 | |
Craig Topper | 25caca7 | 2018-05-22 22:19:19 +0000 | [diff] [blame^] | 35 | // NOTE: Intel documents the 128-bit versions of these as being in emmintrin.h, |
| 36 | // but that's because icc can emulate these without f16c using a library call. |
| 37 | // Since we don't do that let's leave these in f16cintrin.h. |
| 38 | |
| 39 | /// Converts a 16-bit half-precision float value into a 32-bit float |
| 40 | /// value. |
| 41 | /// |
| 42 | /// \headerfile <x86intrin.h> |
| 43 | /// |
| 44 | /// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction. |
| 45 | /// |
| 46 | /// \param __a |
| 47 | /// A 16-bit half-precision float value. |
| 48 | /// \returns The converted 32-bit float value. |
| 49 | static __inline float __DEFAULT_FN_ATTRS |
| 50 | _cvtsh_ss(unsigned short __a) |
| 51 | { |
| 52 | __v8hi v = {(short)__a, 0, 0, 0, 0, 0, 0, 0}; |
| 53 | __v4sf r = __builtin_ia32_vcvtph2ps(v); |
| 54 | return r[0]; |
| 55 | } |
| 56 | |
| 57 | /// Converts a 32-bit single-precision float value to a 16-bit |
| 58 | /// half-precision float value. |
| 59 | /// |
| 60 | /// \headerfile <x86intrin.h> |
| 61 | /// |
| 62 | /// \code |
| 63 | /// unsigned short _cvtss_sh(float a, const int imm); |
| 64 | /// \endcode |
| 65 | /// |
| 66 | /// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction. |
| 67 | /// |
| 68 | /// \param a |
| 69 | /// A 32-bit single-precision float value to be converted to a 16-bit |
| 70 | /// half-precision float value. |
| 71 | /// \param imm |
| 72 | /// An immediate value controlling rounding using bits [2:0]: \n |
| 73 | /// 000: Nearest \n |
| 74 | /// 001: Down \n |
| 75 | /// 010: Up \n |
| 76 | /// 011: Truncate \n |
| 77 | /// 1XX: Use MXCSR.RC for rounding |
| 78 | /// \returns The converted 16-bit half-precision float value. |
| 79 | #define _cvtss_sh(a, imm) __extension__ ({ \ |
| 80 | (unsigned short)(((__v8hi)__builtin_ia32_vcvtps2ph((__v4sf){a, 0, 0, 0}, \ |
| 81 | (imm)))[0]); }) |
| 82 | |
| 83 | /// Converts a 128-bit vector containing 32-bit float values into a |
| 84 | /// 128-bit vector containing 16-bit half-precision float values. |
| 85 | /// |
| 86 | /// \headerfile <x86intrin.h> |
| 87 | /// |
| 88 | /// \code |
| 89 | /// __m128i _mm_cvtps_ph(__m128 a, const int imm); |
| 90 | /// \endcode |
| 91 | /// |
| 92 | /// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction. |
| 93 | /// |
| 94 | /// \param a |
| 95 | /// A 128-bit vector containing 32-bit float values. |
| 96 | /// \param imm |
| 97 | /// An immediate value controlling rounding using bits [2:0]: \n |
| 98 | /// 000: Nearest \n |
| 99 | /// 001: Down \n |
| 100 | /// 010: Up \n |
| 101 | /// 011: Truncate \n |
| 102 | /// 1XX: Use MXCSR.RC for rounding |
| 103 | /// \returns A 128-bit vector containing converted 16-bit half-precision float |
| 104 | /// values. The lower 64 bits are used to store the converted 16-bit |
| 105 | /// half-precision floating-point values. |
| 106 | #define _mm_cvtps_ph(a, imm) __extension__ ({ \ |
| 107 | (__m128i)__builtin_ia32_vcvtps2ph((__v4sf)(__m128)(a), (imm)); }) |
| 108 | |
| 109 | /// Converts a 128-bit vector containing 16-bit half-precision float |
| 110 | /// values into a 128-bit vector containing 32-bit float values. |
| 111 | /// |
| 112 | /// \headerfile <x86intrin.h> |
| 113 | /// |
| 114 | /// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction. |
| 115 | /// |
| 116 | /// \param __a |
| 117 | /// A 128-bit vector containing 16-bit half-precision float values. The lower |
| 118 | /// 64 bits are used in the conversion. |
| 119 | /// \returns A 128-bit vector of [4 x float] containing converted float values. |
| 120 | static __inline __m128 __DEFAULT_FN_ATTRS |
| 121 | _mm_cvtph_ps(__m128i __a) |
| 122 | { |
| 123 | return (__m128)__builtin_ia32_vcvtph2ps((__v8hi)__a); |
| 124 | } |
Ekaterina Romanova | 08d1f24 | 2016-01-22 06:50:50 +0000 | [diff] [blame] | 125 | |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 126 | /// Converts a 256-bit vector of [8 x float] into a 128-bit vector |
| 127 | /// containing 16-bit half-precision float values. |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 128 | /// |
| 129 | /// \headerfile <x86intrin.h> |
| 130 | /// |
| 131 | /// \code |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 132 | /// __m128i _mm256_cvtps_ph(__m256 a, const int imm); |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 133 | /// \endcode |
| 134 | /// |
Ekaterina Romanova | 0c1c3bb | 2016-12-09 18:35:50 +0000 | [diff] [blame] | 135 | /// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction. |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 136 | /// |
| 137 | /// \param a |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 138 | /// A 256-bit vector containing 32-bit single-precision float values to be |
| 139 | /// converted to 16-bit half-precision float values. |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 140 | /// \param imm |
Ekaterina Romanova | dffe45b | 2016-12-27 00:49:38 +0000 | [diff] [blame] | 141 | /// An immediate value controlling rounding using bits [2:0]: \n |
| 142 | /// 000: Nearest \n |
| 143 | /// 001: Down \n |
| 144 | /// 010: Up \n |
| 145 | /// 011: Truncate \n |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 146 | /// 1XX: Use MXCSR.RC for rounding |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 147 | /// \returns A 128-bit vector containing the converted 16-bit half-precision |
| 148 | /// float values. |
| 149 | #define _mm256_cvtps_ph(a, imm) __extension__ ({ \ |
| 150 | (__m128i)__builtin_ia32_vcvtps2ph256((__v8sf)(__m256)(a), (imm)); }) |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 151 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 152 | /// Converts a 128-bit vector containing 16-bit half-precision float |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 153 | /// values into a 256-bit vector of [8 x float]. |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 154 | /// |
| 155 | /// \headerfile <x86intrin.h> |
| 156 | /// |
Ekaterina Romanova | 0c1c3bb | 2016-12-09 18:35:50 +0000 | [diff] [blame] | 157 | /// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction. |
Ekaterina Romanova | a61946d | 2016-02-10 00:12:24 +0000 | [diff] [blame] | 158 | /// |
Ekaterina Romanova | 1168fdc | 2016-05-16 22:54:45 +0000 | [diff] [blame] | 159 | /// \param __a |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 160 | /// A 128-bit vector containing 16-bit half-precision float values to be |
| 161 | /// converted to 32-bit single-precision float values. |
| 162 | /// \returns A vector of [8 x float] containing the converted 32-bit |
| 163 | /// single-precision float values. |
| 164 | static __inline __m256 __attribute__((__always_inline__, __nodebug__, __target__("f16c"))) |
| 165 | _mm256_cvtph_ps(__m128i __a) |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 166 | { |
Craig Topper | 34c8c0d | 2018-05-22 18:54:19 +0000 | [diff] [blame] | 167 | return (__m256)__builtin_ia32_vcvtph2ps256((__v8hi)__a); |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 170 | #undef __DEFAULT_FN_ATTRS |
Eric Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 171 | |
Manman Ren | a45358c | 2012-10-11 00:59:55 +0000 | [diff] [blame] | 172 | #endif /* __F16CINTRIN_H */ |