intel_intrinsics.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
  22. #undef BOOST_MP_HAS_IMMINTRIN_H
  23. #endif
  24. #elif defined(BOOST_MP_HAS_IMMINTRIN_H) && defined(__GNUC__) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
  25. #undef BOOST_MP_HAS_IMMINTRIN_H
  26. #endif
  27. #if defined(__clang__) && (__clang__ < 9)
  28. // We appear to crash the compiler if we try to use these intrinsics?
  29. #undef BOOST_MP_HAS_IMMINTRIN_H
  30. #endif
  31. //
  32. // If the compiler supports the intrinsics used by GCC internally
  33. // inside <immintrin.h> then we'll use them directly.
  34. // This is a bit of defensive programming, mostly for a modern clang
  35. // sitting on top of an older GCC header install.
  36. //
  37. #if defined(__has_builtin) && !defined(BOOST_INTEL)
  38. # if __has_builtin(__builtin_ia32_addcarryx_u64)
  39. # define BOOST_MP_ADDC __builtin_ia32_addcarryx_u
  40. # endif
  41. # if __has_builtin(__builtin_ia32_subborrow_u64)
  42. # define BOOST_MP_SUBB __builtin_ia32_subborrow_u
  43. # elif __has_builtin(__builtin_ia32_sbb_u64)
  44. # define BOOST_MP_SUBB __builtin_ia32_sbb_u
  45. # endif
  46. #endif
  47. #ifndef BOOST_MP_ADDC
  48. #define BOOST_MP_ADDC _addcarry_u
  49. #endif
  50. #ifndef BOOST_MP_SUBB
  51. #define BOOST_MP_SUBB _subborrow_u
  52. #endif
  53. #ifdef BOOST_MP_HAS_IMMINTRIN_H
  54. #ifdef BOOST_MSVC
  55. //
  56. // This is a subset of the full <immintrin.h> :
  57. //
  58. #include <intrin.h>
  59. #else
  60. #include <immintrin.h>
  61. #endif
  62. #if defined(BOOST_HAS_INT128)
  63. namespace boost { namespace multiprecision { namespace detail {
  64. BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  65. {
  66. #ifdef BOOST_INTEL
  67. using cast_type = unsigned __int64;
  68. #else
  69. using cast_type = unsigned long long;
  70. #endif
  71. return BOOST_JOIN(BOOST_MP_ADDC, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
  72. }
  73. BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  74. {
  75. #ifdef BOOST_INTEL
  76. using cast_type = unsigned __int64;
  77. #else
  78. using cast_type = unsigned long long;
  79. #endif
  80. return BOOST_JOIN(BOOST_MP_SUBB, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
  81. }
  82. }}} // namespace boost::multiprecision::detail
  83. #else
  84. namespace boost { namespace multiprecision { namespace detail {
  85. BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  86. {
  87. return BOOST_JOIN(BOOST_MP_ADDC, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
  88. }
  89. BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
  90. {
  91. return BOOST_JOIN(BOOST_MP_SUBB, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
  92. }
  93. }}} // namespace boost::multiprecision::detail
  94. #endif
  95. #endif
  96. #endif