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
8 #ifndef BOOST_MP_INTEL_INTRINSICS_HPP
9 #define BOOST_MP_INTEL_INTRINSICS_HPP
11 // Select which actual implementation header to use:
14 #if __has_include(<immintrin.h>)
15 #define BOOST_MP_HAS_IMMINTRIN_H
19 // If this is GCC/clang, then check that the actual intrinsic exists:
21 #if defined(__has_builtin) && defined(__GNUC__)
22 #if !__has_builtin(__builtin_ia32_addcarryx_u64) && defined(BOOST_MP_HAS_IMMINTRIN_H) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
23 #undef BOOST_MP_HAS_IMMINTRIN_H
25 #elif defined(BOOST_MP_HAS_IMMINTRIN_H) && defined(__GNUC__) && !(defined(BOOST_GCC) && (__GNUC__ >= 9))
26 #undef BOOST_MP_HAS_IMMINTRIN_H
29 #if defined(__clang__) && (__clang__ < 9)
30 // We appear to crash the compiler if we try to use these intrinsics?
31 #undef BOOST_MP_HAS_IMMINTRIN_H
34 // If the compiler supports the intrinsics used by GCC internally
35 // inside <immintrin.h> then we'll use them directly.
36 // This is a bit of defensive programming, mostly for a modern clang
37 // sitting on top of an older GCC header install.
39 #if defined(__has_builtin) && !defined(BOOST_INTEL)
41 # if __has_builtin(__builtin_ia32_addcarryx_u64)
42 # define BOOST_MP_ADDC __builtin_ia32_addcarryx_u
45 # if __has_builtin(__builtin_ia32_subborrow_u64)
46 # define BOOST_MP_SUBB __builtin_ia32_subborrow_u
47 # elif __has_builtin(__builtin_ia32_sbb_u64)
48 # define BOOST_MP_SUBB __builtin_ia32_sbb_u
54 #define BOOST_MP_ADDC _addcarry_u
57 #define BOOST_MP_SUBB _subborrow_u
60 #ifdef BOOST_MP_HAS_IMMINTRIN_H
64 // This is a subset of the full <immintrin.h> :
68 #include <immintrin.h>
71 #if defined(BOOST_HAS_INT128)
73 namespace boost { namespace multiprecision { namespace detail {
75 BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
78 typedef unsigned __int64 cast_type;
80 typedef unsigned long long cast_type;
82 return BOOST_JOIN(BOOST_MP_ADDC, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
85 BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
88 typedef unsigned __int64 cast_type;
90 typedef unsigned long long cast_type;
92 return BOOST_JOIN(BOOST_MP_SUBB, 64)(carry, a, b, reinterpret_cast<cast_type*>(p_result));
95 }}} // namespace boost::multiprecision::detail
99 namespace boost { namespace multiprecision { namespace detail {
101 BOOST_MP_FORCEINLINE unsigned char addcarry_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
103 return BOOST_JOIN(BOOST_MP_ADDC, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
106 BOOST_MP_FORCEINLINE unsigned char subborrow_limb(unsigned char carry, limb_type a, limb_type b, limb_type* p_result)
108 return BOOST_JOIN(BOOST_MP_SUBB, 32)(carry, a, b, reinterpret_cast<unsigned int*>(p_result));
111 }}} // namespace boost::multiprecision::detail