intel_intrinsics.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2020 Madhur Chauhan.
  3. // Copyright 2020 John Maddock.
  4. // Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  7. #ifndef BOOST_MP_INTEL_INTRINSICS_HPP
  8. #define BOOST_MP_INTEL_INTRINSICS_HPP
  9. //
  10. // Select which actual implementation header to use:
  11. //
  12. #ifdef __has_include
  13. #if __has_include(<immintrin.h>)
  14. #define BOOST_MP_HAS_IMMINTRIN_H
  15. #endif
  16. #endif
  17. //
  18. // If this is GCC/clang, then check that the actual intrinsic exists:
  19. //
  20. #if defined(__has_builtin) && defined(__GNUC__)
  21. #if !__has_builtin(__builtin_ia32_addcarryx_u64) && defined(BOOST_MP_HAS_IMMINTRIN_H) \
  22. && !(defined(BOOST_GCC) && (__GNUC__ >= 9) \
  23. && (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)\
  24. || defined(i386) || defined(__i386) || defined(__i386__) || defined(_M_AMD64) \
  25. || defined(_M_X64) || defined(__amd64__) || defined(_M_X64)))
  26. #undef BOOST_MP_HAS_IMMINTRIN_H
  27. #endif
  28. #elif defined(BOOST_MP_HAS_IMMINTRIN_H) && defined(__GNUC__) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
  29. #undef BOOST_MP_HAS_IMMINTRIN_H
  30. #endif
  31. #if defined(__clang_major__) && (__clang_major__ < 9)
  32. // We appear to crash the compiler if we try to use these intrinsics?
  33. #undef BOOST_MP_HAS_IMMINTRIN_H
  34. #endif
  35. #if defined(_WIN32) && (defined(_M_ARM64) || defined(_M_ARM))
  36. //
  37. // When targeting platforms such as ARM, msvc (and also clang when emulating msvc) still has the
  38. // Intel headers in its include path even though they're not usable.
  39. // See https://github.com/boostorg/multiprecision/issues/321
  40. // Also https://github.com/boostorg/multiprecision/issues/475
  41. //
  42. #undef BOOST_MP_HAS_IMMINTRIN_H
  43. #endif
  44. #if defined(__APPLE_CC__) && defined(__clang_major__) && (__clang_major__ < 11) && defined(BOOST_MP_HAS_IMMINTRIN_H)
  45. // Apple clang has it's own version numbers.
  46. #undef BOOST_MP_HAS_IMMINTRIN_H
  47. #endif
  48. //
  49. // If the compiler supports the intrinsics used by GCC internally
  50. // inside <immintrin.h> then we'll use them directly.
  51. // This is a bit of defensive programming, mostly for a modern clang
  52. // sitting on top of an older GCC header install.
  53. //
  54. #if defined(__has_builtin) && !defined(BOOST_INTEL)
  55. # if __has_builtin(__builtin_ia32_addcarryx_u64)
  56. # define BOOST_MP_ADDC __builtin_ia32_addcarryx_u
  57. # endif
  58. # if __has_builtin(__builtin_ia32_subborrow_u64)
  59. # define BOOST_MP_SUBB __builtin_ia32_subborrow_u
  60. # elif __has_builtin(__builtin_ia32_sbb_u64)
  61. # define BOOST_MP_SUBB __builtin_ia32_sbb_u
  62. # endif
  63. #endif
  64. #ifndef BOOST_MP_ADDC
  65. #define BOOST_MP_ADDC _addcarry_u
  66. #endif
  67. #ifndef BOOST_MP_SUBB
  68. #define BOOST_MP_SUBB _subborrow_u
  69. #endif
  70. #ifdef BOOST_MP_HAS_IMMINTRIN_H
  71. #ifdef BOOST_MSVC
  72. //
  73. // This is a subset of the full <immintrin.h> :
  74. //
  75. #include <intrin.h>
  76. #else
  77. #include <immintrin.h>
  78. #endif
  79. #if defined(BOOST_HAS_INT128)
  80. namespace boost { namespace multiprecision { namespace detail {
  81. BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  82. {
  83. #ifdef BOOST_INTEL
  84. using cast_type = unsigned __int64;
  85. #else
  86. using cast_type = unsigned long long;
  87. #endif
  88. return BOOST_JOIN(BOOST_MP_ADDC, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
  89. }
  90. BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  91. {
  92. #ifdef BOOST_INTEL
  93. using cast_type = unsigned __int64;
  94. #else
  95. using cast_type = unsigned long long;
  96. #endif
  97. return BOOST_JOIN(BOOST_MP_SUBB, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
  98. }
  99. }}} // namespace boost::multiprecision::detail
  100. #else
  101. namespace boost { namespace multiprecision { namespace detail {
  102. BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  103. {
  104. return BOOST_JOIN(BOOST_MP_ADDC, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
  105. }
  106. BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  107. {
  108. return BOOST_JOIN(BOOST_MP_SUBB, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
  109. }
  110. }}} // namespace boost::multiprecision::detail
  111. #endif
  112. #endif
  113. #endif