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

source code of boost/libs/multiprecision/include/boost/multiprecision/cpp_int/intel_intrinsics.hpp