f16cintrin.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*===---- f16cintrin.h - F16C intrinsics -----------------------------------===
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  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. #if !defined __X86INTRIN_H && !defined __EMMINTRIN_H && !defined __IMMINTRIN_H
  24. #error "Never use <f16cintrin.h> directly; include <emmintrin.h> instead."
  25. #endif
  26. #ifndef __F16CINTRIN_H
  27. #define __F16CINTRIN_H
  28. /* Define the default attributes for the functions in this file. */
  29. #define __DEFAULT_FN_ATTRS \
  30. __attribute__((__always_inline__, __nodebug__, __target__("f16c")))
  31. /// \brief Converts a 16-bit half-precision float value into a 32-bit float
  32. /// value.
  33. ///
  34. /// \headerfile <x86intrin.h>
  35. ///
  36. /// This intrinsic corresponds to the <c> VCVTPH2PS </c> instruction.
  37. ///
  38. /// \param __a
  39. /// A 16-bit half-precision float value.
  40. /// \returns The converted 32-bit float value.
  41. static __inline float __DEFAULT_FN_ATTRS
  42. _cvtsh_ss(unsigned short __a)
  43. {
  44. __v8hi v = {(short)__a, 0, 0, 0, 0, 0, 0, 0};
  45. __v4sf r = __builtin_ia32_vcvtph2ps(v);
  46. return r[0];
  47. }
  48. /// \brief Converts a 32-bit single-precision float value to a 16-bit
  49. /// half-precision float value.
  50. ///
  51. /// \headerfile <x86intrin.h>
  52. ///
  53. /// \code
  54. /// unsigned short _cvtss_sh(float a, const int imm);
  55. /// \endcode
  56. ///
  57. /// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction.
  58. ///
  59. /// \param a
  60. /// A 32-bit single-precision float value to be converted to a 16-bit
  61. /// half-precision float value.
  62. /// \param imm
  63. /// An immediate value controlling rounding using bits [2:0]: \n
  64. /// 000: Nearest \n
  65. /// 001: Down \n
  66. /// 010: Up \n
  67. /// 011: Truncate \n
  68. /// 1XX: Use MXCSR.RC for rounding
  69. /// \returns The converted 16-bit half-precision float value.
  70. #define _cvtss_sh(a, imm) __extension__ ({ \
  71. (unsigned short)(((__v8hi)__builtin_ia32_vcvtps2ph((__v4sf){a, 0, 0, 0}, \
  72. (imm)))[0]); })
  73. /// \brief Converts a 128-bit vector containing 32-bit float values into a
  74. /// 128-bit vector containing 16-bit half-precision float values.
  75. ///
  76. /// \headerfile <x86intrin.h>
  77. ///
  78. /// \code
  79. /// __m128i _mm_cvtps_ph(__m128 a, const int imm);
  80. /// \endcode
  81. ///
  82. /// This intrinsic corresponds to the <c> VCVTPS2PH </c> instruction.
  83. ///
  84. /// \param a
  85. /// A 128-bit vector containing 32-bit float values.
  86. /// \param imm
  87. /// An immediate value controlling rounding using bits [2:0]: \n
  88. /// 000: Nearest \n
  89. /// 001: Down \n
  90. /// 010: Up \n
  91. /// 011: Truncate \n
  92. /// 1XX: Use MXCSR.RC for rounding
  93. /// \returns A 128-bit vector containing converted 16-bit half-precision float
  94. /// values. The lower 64 bits are used to store the converted 16-bit
  95. /// half-precision floating-point values.
  96. #define _mm_cvtps_ph(a, imm) __extension__ ({ \
  97. (__m128i)__builtin_ia32_vcvtps2ph((__v4sf)(__m128)(a), (imm)); })
  98. /// \brief 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.
  109. static __inline __m128 __DEFAULT_FN_ATTRS
  110. _mm_cvtph_ps(__m128i __a)
  111. {
  112. return (__m128)__builtin_ia32_vcvtph2ps((__v8hi)__a);
  113. }
  114. #undef __DEFAULT_FN_ATTRS
  115. #endif /* __F16CINTRIN_H */