cpp_int.hpp 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 2012 - 2022 John Maddock.
  3. // Copyright 2022 Christopher Kormanyos.
  4. // Distributed under the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt
  6. // or copy at https://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MP_CPP_INT_HPP
  8. #define BOOST_MP_CPP_INT_HPP
  9. #include <cstdint>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <type_traits>
  14. #include <string>
  15. #include <boost/multiprecision/detail/standalone_config.hpp>
  16. #include <boost/multiprecision/detail/endian.hpp>
  17. #include <boost/multiprecision/number.hpp>
  18. #include <boost/multiprecision/detail/integer_ops.hpp>
  19. #include <boost/multiprecision/detail/rebind.hpp>
  20. #include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
  21. #include <boost/multiprecision/rational_adaptor.hpp>
  22. #include <boost/multiprecision/traits/is_byte_container.hpp>
  23. #include <boost/multiprecision/cpp_int/checked.hpp>
  24. #include <boost/multiprecision/detail/constexpr.hpp>
  25. #include <boost/multiprecision/detail/float128_functions.hpp>
  26. #include <boost/multiprecision/cpp_int/value_pack.hpp>
  27. #include <boost/multiprecision/detail/empty_value.hpp>
  28. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  29. #include <boost/multiprecision/detail/assert.hpp>
  30. #include <boost/multiprecision/detail/fpclassify.hpp>
  31. namespace boost {
  32. namespace multiprecision {
  33. #ifdef BOOST_MSVC
  34. #pragma warning(push)
  35. #pragma warning(disable : 4307) // integral constant overflow (oveflow is in a branch not taken when it would overflow)
  36. #pragma warning(disable : 4127) // conditional expression is constant
  37. #pragma warning(disable : 4702) // Unreachable code (reachability depends on template params)
  38. #endif
  39. #if defined(__GNUC__) && !defined(__clang__)
  40. // see https://github.com/boostorg/multiprecision/issues/413
  41. // and https://github.com/boostorg/multiprecision/issues/431
  42. #pragma GCC diagnostic push
  43. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  44. #endif
  45. namespace detail {
  46. template <std::size_t MinBits, std::size_t MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  47. struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::false_type
  48. {};
  49. } // namespace detail
  50. namespace backends {
  51. namespace detail {
  52. template <std::size_t Value1, std::size_t Value2>
  53. struct static_unsigned_max
  54. {
  55. static constexpr std::size_t value = (Value1 > Value2) ? Value1 : Value2;
  56. };
  57. } // Namespace detail
  58. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
  59. struct cpp_int_base;
  60. //
  61. // Traits class determines the maximum and minimum precision values:
  62. //
  63. template <class T>
  64. struct max_precision;
  65. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  66. struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  67. {
  68. static constexpr std::size_t value = std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value
  69. : (((MaxBits >= MinBits) && MaxBits) ? MaxBits : SIZE_MAX);
  70. };
  71. template <class T>
  72. struct min_precision;
  73. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  74. struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  75. {
  76. static constexpr std::size_t value = (std::is_void<Allocator>::value ? detail::static_unsigned_max<MinBits, MaxBits>::value : MinBits);
  77. };
  78. //
  79. // Traits class determines whether the number of bits precision requested could fit in a native type,
  80. // we call this a "trivial" cpp_int:
  81. //
  82. template <class T>
  83. struct is_trivial_cpp_int
  84. {
  85. static constexpr bool value = false;
  86. };
  87. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  88. struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  89. {
  90. using self = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
  91. static constexpr bool value = std::is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
  92. };
  93. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  94. struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
  95. {
  96. static constexpr bool value = true;
  97. };
  98. } // namespace backends
  99. //
  100. // Traits class to determine whether a cpp_int_backend is signed or not:
  101. //
  102. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  103. struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  104. : public std::integral_constant<bool, (SignType == unsigned_magnitude) || (SignType == unsigned_packed)>
  105. {};
  106. namespace backends {
  107. //
  108. // Traits class determines whether T should be implicitly convertible to U, or
  109. // whether the constructor should be made explicit. The latter happens if we
  110. // are losing the sign, or have fewer digits precision in the target type:
  111. //
  112. template <class T, class U>
  113. struct is_implicit_cpp_int_conversion;
  114. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  115. struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
  116. {
  117. using t1 = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
  118. using t2 = cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>;
  119. static constexpr bool value =
  120. (is_signed_number<t2>::value || !is_signed_number<t1>::value) && (max_precision<t1>::value <= max_precision<t2>::value);
  121. };
  122. //
  123. // Traits class to determine whether operations on a cpp_int may throw:
  124. //
  125. template <class T>
  126. struct is_non_throwing_cpp_int : public std::integral_constant<bool, false>
  127. {};
  128. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType>
  129. struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public std::integral_constant<bool, true>
  130. {};
  131. //
  132. // Traits class, determines whether the cpp_int is fixed precision or not:
  133. //
  134. template <class T>
  135. struct is_fixed_precision;
  136. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  137. struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  138. : public std::integral_constant<bool, max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != SIZE_MAX>
  139. {};
  140. namespace detail {
  141. inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(std::size_t new_size, std::size_t min_size, const std::integral_constant<int, checked>&)
  142. {
  143. if (new_size < min_size)
  144. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
  145. }
  146. inline BOOST_MP_CXX14_CONSTEXPR void verify_new_size(std::size_t /*new_size*/, std::size_t /*min_size*/, const std::integral_constant<int, unchecked>&) {}
  147. template <class U>
  148. inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool b, U limb, U mask, const std::integral_constant<int, checked>&)
  149. {
  150. // When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error:
  151. if (b && (limb & ~mask))
  152. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Overflow in cpp_int arithmetic: there is insufficient precision in the target type to hold all of the bits of the result."));
  153. }
  154. template <class U>
  155. inline BOOST_MP_CXX14_CONSTEXPR void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const std::integral_constant<int, unchecked>&) {}
  156. } // namespace detail
  157. //
  158. // Now define the various data layouts that are possible as partial specializations of the base class,
  159. // starting with the default arbitrary precision signed integer type:
  160. //
  161. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  162. struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>
  163. : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
  164. {
  165. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, bool trivial2>
  166. friend struct cpp_int_base;
  167. using allocator_type = typename detail::rebind<limb_type, Allocator>::type;
  168. using limb_pointer = typename std::allocator_traits<allocator_type>::pointer ;
  169. using const_limb_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  170. using checked_type = std::integral_constant<int, Checked>;
  171. //
  172. // Interface invariants:
  173. //
  174. static_assert(!std::is_void<Allocator>::value, "Allocator must not be void here");
  175. using base_type = boost::multiprecision::detail::empty_value<allocator_type>;
  176. private:
  177. struct limb_data
  178. {
  179. std::size_t capacity;
  180. limb_pointer data;
  181. };
  182. public:
  183. static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
  184. static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
  185. static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
  186. static constexpr std::size_t internal_limb_count =
  187. MinBits
  188. ? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
  189. : (sizeof(limb_data) / sizeof(limb_type)) > 1 ? (sizeof(limb_data) / sizeof(limb_type)) : 2;
  190. private:
  191. union data_type
  192. {
  193. limb_data ld;
  194. limb_type la[internal_limb_count];
  195. limb_type first;
  196. double_limb_type double_first;
  197. constexpr data_type() noexcept : first(0) {}
  198. constexpr data_type(limb_type i) noexcept : first(i) {}
  199. constexpr data_type(signed_limb_type i) noexcept : first(static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i))) {}
  200. #if BOOST_MP_ENDIAN_LITTLE_BYTE
  201. constexpr data_type(double_limb_type i) noexcept : double_first(i)
  202. {}
  203. constexpr data_type(signed_double_limb_type i) noexcept : double_first(static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i))) {}
  204. #endif
  205. #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) && !(defined(BOOST_MSVC) && (BOOST_MSVC < 1900))
  206. constexpr data_type(limb_type* limbs, std::size_t len) noexcept : ld{ len, limbs }
  207. {}
  208. #else
  209. constexpr data_type(limb_type* limbs, std::size_t len) noexcept
  210. {
  211. ld.capacity = len;
  212. ld.data = limbs;
  213. }
  214. #endif
  215. };
  216. data_type m_data;
  217. std::size_t m_limbs;
  218. bool m_sign, m_internal, m_alias;
  219. public:
  220. //
  221. // Direct construction:
  222. //
  223. BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
  224. : m_data(i),
  225. m_limbs(1),
  226. m_sign(false),
  227. m_internal(true),
  228. m_alias(false) {}
  229. BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
  230. : m_data(i),
  231. m_limbs(1),
  232. m_sign(i < 0),
  233. m_internal(true),
  234. m_alias(false) {}
  235. #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
  236. BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
  237. : m_data(i),
  238. m_limbs(i > max_limb_value ? 2 : 1),
  239. m_sign(false),
  240. m_internal(true),
  241. m_alias(false)
  242. {}
  243. BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
  244. : m_data(i),
  245. m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > static_cast<double_limb_type>(max_limb_value) ? 2 : 1) : (i > max_limb_value ? 2 : 1)),
  246. m_sign(i < 0),
  247. m_internal(true),
  248. m_alias(false) {}
  249. #endif
  250. //
  251. // Aliasing constructor aliases data:
  252. //
  253. struct scoped_shared_storage : private boost::multiprecision::detail::empty_value<allocator_type>
  254. {
  255. private:
  256. limb_type* data;
  257. std::size_t capacity;
  258. std::size_t allocated;
  259. bool is_alias;
  260. allocator_type& allocator() noexcept { return boost::multiprecision::detail::empty_value<allocator_type>::get(); }
  261. public:
  262. scoped_shared_storage(const allocator_type& a, std::size_t len)
  263. : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), a), capacity(len), allocated(0), is_alias(false)
  264. {
  265. data = allocator().allocate(len);
  266. }
  267. scoped_shared_storage(const cpp_int_base& i, std::size_t len)
  268. : boost::multiprecision::detail::empty_value<allocator_type>(boost::multiprecision::detail::empty_init_t(), i.allocator()), capacity(len), allocated(0), is_alias(false)
  269. {
  270. data = allocator().allocate(len);
  271. }
  272. scoped_shared_storage(limb_type* limbs, std::size_t n) : data(limbs), capacity(n), allocated(0), is_alias(true) {}
  273. ~scoped_shared_storage()
  274. {
  275. if(!is_alias)
  276. allocator().deallocate(data, capacity);
  277. }
  278. limb_type* allocate(std::size_t n) noexcept
  279. {
  280. limb_type* result = data + allocated;
  281. allocated += n;
  282. BOOST_MP_ASSERT(allocated <= capacity);
  283. return result;
  284. }
  285. void deallocate(std::size_t n)
  286. {
  287. BOOST_MP_ASSERT(n <= allocated);
  288. allocated -= n;
  289. }
  290. };
  291. explicit constexpr cpp_int_base(limb_type* data, std::size_t offset, std::size_t len) noexcept
  292. : m_data(data + offset, len),
  293. m_limbs(len),
  294. m_sign(false),
  295. m_internal(false),
  296. m_alias(true) {}
  297. // This next constructor is for constructing const objects from const limb_type*'s only.
  298. // Unfortunately we appear to have no way to assert that within the language, and the const_cast
  299. // is a side effect of that :(
  300. explicit constexpr cpp_int_base(const limb_type* data, std::size_t offset, std::size_t len) noexcept
  301. : m_data(const_cast<limb_type*>(data) + offset, len),
  302. m_limbs(len),
  303. m_sign(false),
  304. m_internal(false),
  305. m_alias(true) {}
  306. explicit cpp_int_base(scoped_shared_storage& data, std::size_t len) noexcept
  307. : m_data(data.allocate(len), len),
  308. m_limbs(len),
  309. m_sign(false),
  310. m_internal(false),
  311. m_alias(true) {}
  312. //
  313. // Helper functions for getting at our internal data, and manipulating storage:
  314. //
  315. BOOST_MP_FORCEINLINE allocator_type& allocator() noexcept { return base_type::get(); }
  316. BOOST_MP_FORCEINLINE const allocator_type& allocator() const noexcept { return base_type::get(); }
  317. BOOST_MP_FORCEINLINE std::size_t size() const noexcept { return m_limbs; }
  318. BOOST_MP_FORCEINLINE limb_pointer limbs() noexcept { return m_internal ? m_data.la : m_data.ld.data; }
  319. BOOST_MP_FORCEINLINE const_limb_pointer limbs() const noexcept { return m_internal ? m_data.la : m_data.ld.data; }
  320. BOOST_MP_FORCEINLINE std::size_t capacity() const noexcept { return m_internal ? internal_limb_count : m_data.ld.capacity; }
  321. BOOST_MP_FORCEINLINE bool sign() const noexcept { return m_sign; }
  322. void sign(bool b) noexcept
  323. {
  324. m_sign = b;
  325. // Check for zero value:
  326. if (m_sign && (m_limbs == 1))
  327. {
  328. if (limbs()[0] == 0)
  329. m_sign = false;
  330. }
  331. }
  332. void resize(std::size_t new_size, std::size_t min_size)
  333. {
  334. constexpr std::size_t max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
  335. // We never resize beyond MaxSize:
  336. if (new_size > max_limbs)
  337. new_size = max_limbs;
  338. detail::verify_new_size(new_size, min_size, checked_type());
  339. // See if we have enough capacity already:
  340. std::size_t cap = capacity();
  341. if (new_size > cap)
  342. {
  343. // We must not be an alias, memory allocation here defeats the whole point of aliasing:
  344. BOOST_MP_ASSERT(!m_alias);
  345. // Allocate a new buffer and copy everything over:
  346. cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
  347. limb_pointer pl = allocator().allocate(cap);
  348. std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
  349. if (!m_internal && !m_alias)
  350. allocator().deallocate(limbs(), capacity());
  351. else
  352. m_internal = false;
  353. m_limbs = new_size;
  354. m_data.ld.capacity = cap;
  355. m_data.ld.data = pl;
  356. }
  357. else
  358. {
  359. m_limbs = new_size;
  360. }
  361. }
  362. BOOST_MP_FORCEINLINE void normalize() noexcept
  363. {
  364. limb_pointer p = limbs();
  365. while ((m_limbs - 1) && !p[m_limbs - 1])
  366. --m_limbs;
  367. }
  368. BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(), m_limbs(1), m_sign(false), m_internal(true), m_alias(false){}
  369. BOOST_MP_FORCEINLINE cpp_int_base(const cpp_int_base& o) : base_type(o), m_limbs(o.m_alias ? o.m_limbs : 0), m_sign(o.m_sign), m_internal(o.m_alias ? false : true), m_alias(o.m_alias)
  370. {
  371. if (m_alias)
  372. {
  373. m_data.ld = o.m_data.ld;
  374. }
  375. else
  376. {
  377. resize(o.size(), o.size());
  378. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
  379. }
  380. }
  381. // rvalue copy:
  382. cpp_int_base(cpp_int_base&& o)
  383. : base_type(static_cast<base_type&&>(o)), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal), m_alias(o.m_alias)
  384. {
  385. if (m_internal)
  386. {
  387. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
  388. }
  389. else
  390. {
  391. m_data.ld = o.m_data.ld;
  392. o.m_limbs = 0;
  393. o.m_internal = true;
  394. }
  395. }
  396. cpp_int_base& operator=(cpp_int_base&& o) noexcept
  397. {
  398. if (!m_internal && !m_alias)
  399. allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
  400. *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
  401. m_limbs = o.m_limbs;
  402. m_sign = o.m_sign;
  403. m_internal = o.m_internal;
  404. m_alias = o.m_alias;
  405. if (m_internal)
  406. {
  407. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
  408. }
  409. else
  410. {
  411. m_data.ld = o.m_data.ld;
  412. o.m_limbs = 0;
  413. o.m_internal = true;
  414. }
  415. return *this;
  416. }
  417. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_int_check_type Checked2>
  418. cpp_int_base& operator=(cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>&& o) noexcept
  419. {
  420. if(o.m_internal)
  421. {
  422. m_sign = o.m_sign;
  423. this->resize(o.size(), o.size());
  424. std::memcpy(this->limbs(), o.limbs(), o.size() * sizeof(*(o.limbs())));
  425. return *this;
  426. }
  427. if (!m_internal && !m_alias)
  428. allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
  429. *static_cast<base_type*>(this) = static_cast<typename cpp_int_base<MinBits2, MaxBits2, signed_magnitude, Checked2, Allocator>::base_type&&>(o);
  430. m_limbs = o.m_limbs;
  431. m_sign = o.m_sign;
  432. m_internal = o.m_internal;
  433. m_alias = o.m_alias;
  434. m_data.ld.capacity = o.m_data.ld.capacity;
  435. m_data.ld.data = o.limbs();
  436. o.m_limbs = 0;
  437. o.m_internal = true;
  438. return *this;
  439. }
  440. BOOST_MP_FORCEINLINE ~cpp_int_base() noexcept
  441. {
  442. if (!m_internal && !m_alias)
  443. allocator().deallocate(limbs(), capacity());
  444. }
  445. void assign(const cpp_int_base& o)
  446. {
  447. if (this != &o)
  448. {
  449. static_cast<base_type&>(*this) = static_cast<const base_type&>(o);
  450. m_limbs = 0;
  451. resize(o.size(), o.size());
  452. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
  453. m_sign = o.m_sign;
  454. }
  455. }
  456. BOOST_MP_FORCEINLINE void negate() noexcept
  457. {
  458. m_sign = !m_sign;
  459. // Check for zero value:
  460. if (m_sign && (m_limbs == 1))
  461. {
  462. if (limbs()[0] == 0)
  463. m_sign = false;
  464. }
  465. }
  466. BOOST_MP_FORCEINLINE bool isneg() const noexcept
  467. {
  468. return m_sign;
  469. }
  470. BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) noexcept
  471. {
  472. std::swap(m_data, o.m_data);
  473. std::swap(m_sign, o.m_sign);
  474. std::swap(m_internal, o.m_internal);
  475. std::swap(m_limbs, o.m_limbs);
  476. std::swap(m_alias, o.m_alias);
  477. }
  478. protected:
  479. template <class A>
  480. void check_in_range(const A&) noexcept {}
  481. };
  482. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  483. constexpr std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
  484. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  485. constexpr limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
  486. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  487. constexpr limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
  488. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  489. constexpr std::size_t cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
  490. template <std::size_t MinBits, std::size_t MaxBits, cpp_int_check_type Checked, class Allocator>
  491. struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false>
  492. : private boost::multiprecision::detail::empty_value<typename detail::rebind<limb_type, Allocator>::type>
  493. {
  494. //
  495. // There is currently no support for unsigned arbitrary precision arithmetic, largely
  496. // because it's not clear what subtraction should do:
  497. //
  498. static_assert(((sizeof(Allocator) == 0) && !std::is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
  499. };
  500. //
  501. // Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
  502. //
  503. template <std::size_t MinBits, cpp_int_check_type Checked>
  504. struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
  505. {
  506. using limb_pointer = limb_type* ;
  507. using const_limb_pointer = const limb_type* ;
  508. using checked_type = std::integral_constant<int, Checked>;
  509. struct scoped_shared_storage
  510. {
  511. BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
  512. BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
  513. };
  514. //
  515. // Interface invariants:
  516. //
  517. static_assert(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
  518. public:
  519. static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
  520. static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
  521. static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
  522. static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
  523. static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
  524. static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
  525. private:
  526. union data_type
  527. {
  528. limb_type m_data[internal_limb_count];
  529. limb_type m_first_limb;
  530. double_limb_type m_double_first_limb;
  531. constexpr data_type()
  532. : m_data{0}
  533. {}
  534. constexpr data_type(limb_type i)
  535. : m_data{i}
  536. {}
  537. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  538. constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
  539. {}
  540. #endif
  541. constexpr data_type(double_limb_type i) : m_double_first_limb(i)
  542. {
  543. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  544. if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
  545. {
  546. data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
  547. *this = t;
  548. }
  549. #endif
  550. }
  551. template <limb_type... VALUES>
  552. constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
  553. {}
  554. } m_wrapper;
  555. std::uint16_t m_limbs;
  556. bool m_sign;
  557. public:
  558. //
  559. // Direct construction:
  560. //
  561. BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
  562. : m_wrapper(i),
  563. m_limbs(1),
  564. m_sign(false) {}
  565. BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_limb_type i) noexcept
  566. : m_wrapper(limb_type(i < 0 ? static_cast<limb_type>(-static_cast<signed_double_limb_type>(i)) : i)),
  567. m_limbs(1),
  568. m_sign(i < 0) {}
  569. #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
  570. BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
  571. : m_wrapper(i),
  572. m_limbs(i > max_limb_value ? 2 : 1),
  573. m_sign(false)
  574. {}
  575. BOOST_MP_FORCEINLINE constexpr cpp_int_base(signed_double_limb_type i) noexcept
  576. : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
  577. m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1)),
  578. m_sign(i < 0) {}
  579. #endif
  580. template <limb_type... VALUES>
  581. constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
  582. : m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false)
  583. {}
  584. constexpr cpp_int_base(literals::detail::value_pack<> i)
  585. : m_wrapper(i), m_limbs(1), m_sign(false) {}
  586. constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
  587. : m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
  588. explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(0), m_sign(false)
  589. {}
  590. //
  591. // These are deprecated in C++20 unless we make them explicit:
  592. //
  593. BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
  594. //
  595. // Helper functions for getting at our internal data, and manipulating storage:
  596. //
  597. BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
  598. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
  599. BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
  600. BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return m_sign; }
  601. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
  602. {
  603. m_sign = b;
  604. // Check for zero value:
  605. if (m_sign && (m_limbs == 1))
  606. {
  607. if (limbs()[0] == 0)
  608. m_sign = false;
  609. }
  610. }
  611. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
  612. {
  613. m_limbs = static_cast<std::uint16_t>((std::min)(new_size, internal_limb_count));
  614. detail::verify_new_size(m_limbs, min_size, checked_type());
  615. }
  616. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
  617. {
  618. limb_pointer p = limbs();
  619. detail::verify_limb_mask(m_limbs == internal_limb_count, p[m_limbs - 1], upper_limb_mask, checked_type());
  620. p[internal_limb_count - 1] &= upper_limb_mask;
  621. while ((m_limbs - 1) && !p[m_limbs - 1])
  622. --m_limbs;
  623. if ((m_limbs == 1) && (!*p))
  624. m_sign = false; // zero is always unsigned
  625. }
  626. BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
  627. // Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
  628. BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
  629. : m_wrapper(o.m_wrapper),
  630. m_limbs(o.m_limbs),
  631. m_sign(o.m_sign) {}
  632. // Defaulted functions:
  633. //~cpp_int_base() noexcept {}
  634. void BOOST_MP_CXX14_CONSTEXPR assign(const cpp_int_base& o) noexcept
  635. {
  636. if (this != &o)
  637. {
  638. m_limbs = o.m_limbs;
  639. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  640. if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
  641. {
  642. for (std::size_t i = 0; i < m_limbs; ++i)
  643. limbs()[i] = o.limbs()[i];
  644. }
  645. else
  646. #endif
  647. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
  648. m_sign = o.m_sign;
  649. }
  650. }
  651. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
  652. {
  653. m_sign = !m_sign;
  654. // Check for zero value:
  655. if (m_sign && (m_limbs == 1))
  656. {
  657. if (limbs()[0] == 0)
  658. m_sign = false;
  659. }
  660. }
  661. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
  662. {
  663. return m_sign;
  664. }
  665. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
  666. {
  667. for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
  668. std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
  669. std_constexpr::swap(m_sign, o.m_sign);
  670. std_constexpr::swap(m_limbs, o.m_limbs);
  671. }
  672. protected:
  673. template <class A>
  674. BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
  675. };
  676. template <std::size_t MinBits, cpp_int_check_type Checked>
  677. constexpr std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
  678. template <std::size_t MinBits, cpp_int_check_type Checked>
  679. constexpr limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
  680. template <std::size_t MinBits, cpp_int_check_type Checked>
  681. constexpr limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
  682. template <std::size_t MinBits, cpp_int_check_type Checked>
  683. constexpr std::size_t cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
  684. //
  685. // Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
  686. //
  687. template <std::size_t MinBits, cpp_int_check_type Checked>
  688. struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
  689. {
  690. using limb_pointer = limb_type* ;
  691. using const_limb_pointer = const limb_type* ;
  692. using checked_type = std::integral_constant<int, Checked>;
  693. struct scoped_shared_storage
  694. {
  695. BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
  696. BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
  697. };
  698. //
  699. // Interface invariants:
  700. //
  701. static_assert(MinBits > sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
  702. public:
  703. static constexpr std::size_t limb_bits = sizeof(limb_type) * CHAR_BIT;
  704. static constexpr limb_type max_limb_value = ~static_cast<limb_type>(0u);
  705. static constexpr limb_type sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1);
  706. static constexpr std::size_t internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0);
  707. static constexpr limb_type upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) - 1 : (~limb_type(0));
  708. static_assert(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
  709. private:
  710. union data_type
  711. {
  712. limb_type m_data[internal_limb_count];
  713. limb_type m_first_limb;
  714. double_limb_type m_double_first_limb;
  715. constexpr data_type()
  716. : m_data{0}
  717. {}
  718. constexpr data_type(limb_type i)
  719. : m_data{i}
  720. {}
  721. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  722. constexpr data_type(limb_type i, limb_type j) : m_data{i, j}
  723. {}
  724. #endif
  725. constexpr data_type(double_limb_type i) : m_double_first_limb(i)
  726. {
  727. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  728. if (BOOST_MP_IS_CONST_EVALUATED(m_double_first_limb))
  729. {
  730. data_type t(static_cast<limb_type>(i & max_limb_value), static_cast<limb_type>(i >> limb_bits));
  731. *this = t;
  732. }
  733. #endif
  734. }
  735. template <limb_type... VALUES>
  736. constexpr data_type(literals::detail::value_pack<VALUES...>) : m_data{VALUES...}
  737. {}
  738. } m_wrapper;
  739. std::size_t m_limbs;
  740. public:
  741. //
  742. // Direct construction:
  743. //
  744. BOOST_MP_FORCEINLINE constexpr cpp_int_base(limb_type i) noexcept
  745. : m_wrapper(i),
  746. m_limbs(1) {}
  747. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_limb_type i) noexcept((Checked == unchecked))
  748. : m_wrapper(static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i))),
  749. m_limbs(1)
  750. {
  751. if (i < 0)
  752. negate();
  753. }
  754. #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
  755. BOOST_MP_FORCEINLINE constexpr cpp_int_base(double_limb_type i) noexcept
  756. : m_wrapper(i),
  757. m_limbs(i > max_limb_value ? 2 : 1)
  758. {}
  759. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(signed_double_limb_type i) noexcept((Checked == unchecked))
  760. : m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
  761. m_limbs(i < 0 ? (static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) > max_limb_value ? 2 : 1) : (i > max_limb_value ? 2 : 1))
  762. {
  763. if (i < 0)
  764. negate();
  765. }
  766. #endif
  767. template <limb_type... VALUES>
  768. constexpr cpp_int_base(literals::detail::value_pack<VALUES...> i)
  769. : m_wrapper(i), m_limbs(sizeof...(VALUES))
  770. {}
  771. constexpr cpp_int_base(literals::detail::value_pack<>)
  772. : m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
  773. explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_wrapper(), m_limbs(1)
  774. {}
  775. //
  776. // Helper functions for getting at our internal data, and manipulating storage:
  777. //
  778. BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return m_limbs; }
  779. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return m_wrapper.m_data; }
  780. BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return m_wrapper.m_data; }
  781. BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
  782. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
  783. {
  784. if (b)
  785. negate();
  786. }
  787. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t new_size, std::size_t min_size) noexcept((Checked == unchecked))
  788. {
  789. m_limbs = (std::min)(new_size, internal_limb_count);
  790. detail::verify_new_size(m_limbs, min_size, checked_type());
  791. }
  792. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
  793. {
  794. limb_pointer p = limbs();
  795. detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count - 1], upper_limb_mask, checked_type());
  796. p[internal_limb_count - 1] &= upper_limb_mask;
  797. while ((m_limbs - 1) && !p[m_limbs - 1])
  798. --m_limbs;
  799. }
  800. BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept
  801. : m_wrapper(limb_type(0u)),
  802. m_limbs(1) {}
  803. BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
  804. : m_wrapper(o.m_wrapper),
  805. m_limbs(o.m_limbs) {}
  806. // Defaulted functions:
  807. //~cpp_int_base() noexcept {}
  808. //
  809. // These are deprecated in C++20 unless we make them explicit:
  810. //
  811. BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
  812. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
  813. {
  814. if (this != &o)
  815. {
  816. m_limbs = o.m_limbs;
  817. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  818. if (BOOST_MP_IS_CONST_EVALUATED(m_limbs))
  819. {
  820. for (std::size_t i = 0; i < m_limbs; ++i)
  821. limbs()[i] = o.limbs()[i];
  822. }
  823. else
  824. #endif
  825. std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
  826. }
  827. }
  828. private:
  829. void check_negate(const std::integral_constant<int, checked>&)
  830. {
  831. BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
  832. }
  833. BOOST_MP_CXX14_CONSTEXPR void check_negate(const std::integral_constant<int, unchecked>&) {}
  834. public:
  835. BOOST_MP_CXX14_CONSTEXPR void negate() noexcept((Checked == unchecked))
  836. {
  837. // Not so much a negate as a complement - this gets called when subtraction
  838. // would result in a "negative" number:
  839. if ((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
  840. return; // negating zero is always zero, and always OK.
  841. check_negate(checked_type());
  842. std::size_t i = m_limbs;
  843. for (; i < internal_limb_count; ++i)
  844. m_wrapper.m_data[i] = 0;
  845. m_limbs = internal_limb_count;
  846. for (i = 0; i < internal_limb_count; ++i)
  847. m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
  848. normalize();
  849. eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>&>(*this));
  850. }
  851. BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
  852. {
  853. return false;
  854. }
  855. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
  856. {
  857. for (std::size_t i = 0; i < (std::max)(size(), o.size()); ++i)
  858. std_constexpr::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
  859. std_constexpr::swap(m_limbs, o.m_limbs);
  860. }
  861. protected:
  862. template <class A>
  863. BOOST_MP_CXX14_CONSTEXPR void check_in_range(const A&) noexcept {}
  864. };
  865. template <std::size_t MinBits, cpp_int_check_type Checked>
  866. constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
  867. template <std::size_t MinBits, cpp_int_check_type Checked>
  868. constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
  869. template <std::size_t MinBits, cpp_int_check_type Checked>
  870. constexpr limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
  871. template <std::size_t MinBits, cpp_int_check_type Checked>
  872. constexpr std::size_t cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
  873. //
  874. // Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
  875. // because some platforms have native integer types longer than long long, "really long long" anyone??
  876. //
  877. template <unsigned N, bool s>
  878. struct trivial_limb_type_imp
  879. {
  880. using type = double_limb_type;
  881. };
  882. template <unsigned N>
  883. struct trivial_limb_type_imp<N, true>
  884. {
  885. using type = typename boost::multiprecision::detail::uint_t<N>::least;
  886. };
  887. template <unsigned N>
  888. struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(long long) * CHAR_BIT>
  889. {};
  890. //
  891. // Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
  892. //
  893. template <std::size_t MinBits, cpp_int_check_type Checked>
  894. struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
  895. {
  896. using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
  897. using limb_pointer = local_limb_type*;
  898. using const_limb_pointer = const local_limb_type*;
  899. using checked_type = std::integral_constant<int, Checked>;
  900. struct scoped_shared_storage
  901. {
  902. BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
  903. BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
  904. };
  905. protected:
  906. static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
  907. static constexpr local_limb_type limb_mask = (MinBits < limb_bits) ? local_limb_type((local_limb_type(~local_limb_type(0))) >> (limb_bits - MinBits)) : local_limb_type(~local_limb_type(0));
  908. private:
  909. local_limb_type m_data;
  910. bool m_sign;
  911. //
  912. // Interface invariants:
  913. //
  914. static_assert(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
  915. protected:
  916. template <class T>
  917. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!boost::multiprecision::detail::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits))))>::type
  918. check_in_range(T val, const std::integral_constant<int, checked>&)
  919. {
  920. using common_type = typename std::common_type<typename boost::multiprecision::detail::make_unsigned<T>::type, local_limb_type>::type;
  921. if (static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
  922. BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
  923. }
  924. template <class T>
  925. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(boost::multiprecision::detail::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits))))>::type
  926. check_in_range(T val, const std::integral_constant<int, checked>&)
  927. {
  928. using std::abs;
  929. using common_type = typename std::common_type<T, local_limb_type>::type;
  930. if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
  931. BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
  932. }
  933. template <class T, int C>
  934. BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&) noexcept {}
  935. template <class T>
  936. BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type())))
  937. {
  938. check_in_range(val, checked_type());
  939. }
  940. public:
  941. //
  942. // Direct construction:
  943. //
  944. template <class SI>
  945. BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
  946. : m_data(i < 0 ? static_cast<local_limb_type>(static_cast<typename boost::multiprecision::detail::make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0) {}
  947. template <class SI>
  948. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
  949. : m_data(i < 0 ? (static_cast<local_limb_type>(static_cast<typename boost::multiprecision::detail::make_unsigned<SI>::type>(boost::multiprecision::detail::unsigned_abs(i)) & limb_mask)) : static_cast<local_limb_type>(i & limb_mask)), m_sign(i < 0)
  950. {
  951. check_in_range(i);
  952. }
  953. template <class UI>
  954. BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  955. : m_data(static_cast<local_limb_type>(i) & limb_mask),
  956. m_sign(false) {}
  957. template <class UI>
  958. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
  959. : m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
  960. #if !(defined(__clang__) && defined(__MINGW32__))
  961. template <class F>
  962. BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  963. : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask),
  964. m_sign(i < 0) {}
  965. template <class F>
  966. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == checked)>::type const* = nullptr)
  967. : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask), m_sign(i < 0) { check_in_range(i); }
  968. #else
  969. //
  970. // conversion from float to __int128 is broken on clang/mingw,
  971. // see: https://bugs.llvm.org/show_bug.cgi?id=48940
  972. // Since no floating point type has more than 64 bits of
  973. // precision, we can simply cast to an intermediate type to
  974. // solve the issue:
  975. //
  976. template <class F>
  977. BOOST_MP_FORCEINLINE constexpr cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  978. : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask),
  979. m_sign(i < 0) {}
  980. template <class F>
  981. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value && (Checked == checked)>::type const* = nullptr)
  982. : m_data(static_cast<local_limb_type>(static_cast<std::uint64_t>(i < 0 ? -i : i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
  983. #endif
  984. constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
  985. : m_data(static_cast<local_limb_type>(0u)),
  986. m_sign(false)
  987. {}
  988. template <limb_type a>
  989. constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
  990. : m_data(static_cast<local_limb_type>(a)),
  991. m_sign(false) {}
  992. template <limb_type a, limb_type b>
  993. constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
  994. : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)),
  995. m_sign(false) {}
  996. constexpr cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&) noexcept
  997. : m_data(a.m_data),
  998. m_sign(a.m_data ? !a.m_sign : false) {}
  999. //
  1000. // These are deprecated in C++20 unless we make them explicit:
  1001. //
  1002. BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
  1003. explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0), m_sign(false)
  1004. {}
  1005. //
  1006. // Helper functions for getting at our internal data, and manipulating storage:
  1007. //
  1008. BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
  1009. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
  1010. BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
  1011. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool sign() const noexcept { return m_sign; }
  1012. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept
  1013. {
  1014. m_sign = b;
  1015. // Check for zero value:
  1016. if (m_sign && !m_data)
  1017. {
  1018. m_sign = false;
  1019. }
  1020. }
  1021. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(std::size_t /* new_size */, std::size_t min_size)
  1022. {
  1023. detail::verify_new_size(2, min_size, checked_type());
  1024. }
  1025. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
  1026. {
  1027. if (!m_data)
  1028. m_sign = false; // zero is always unsigned
  1029. detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
  1030. m_data &= limb_mask;
  1031. }
  1032. BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0), m_sign(false) {}
  1033. BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
  1034. : m_data(o.m_data),
  1035. m_sign(o.m_sign) {}
  1036. //~cpp_int_base() noexcept {}
  1037. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
  1038. {
  1039. m_data = o.m_data;
  1040. m_sign = o.m_sign;
  1041. }
  1042. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate() noexcept
  1043. {
  1044. m_sign = !m_sign;
  1045. // Check for zero value:
  1046. if (m_data == 0)
  1047. {
  1048. m_sign = false;
  1049. }
  1050. }
  1051. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool isneg() const noexcept
  1052. {
  1053. return m_sign;
  1054. }
  1055. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
  1056. {
  1057. std_constexpr::swap(m_sign, o.m_sign);
  1058. std_constexpr::swap(m_data, o.m_data);
  1059. }
  1060. };
  1061. //
  1062. // Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
  1063. //
  1064. template <std::size_t MinBits, cpp_int_check_type Checked>
  1065. struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
  1066. {
  1067. using local_limb_type = typename trivial_limb_type<static_cast<unsigned>(MinBits)>::type;
  1068. using limb_pointer = local_limb_type* ;
  1069. using const_limb_pointer = const local_limb_type* ;
  1070. struct scoped_shared_storage
  1071. {
  1072. BOOST_MP_CXX14_CONSTEXPR scoped_shared_storage(const cpp_int_base&, std::size_t) {}
  1073. BOOST_MP_CXX14_CONSTEXPR void deallocate(std::size_t) {}
  1074. };
  1075. private:
  1076. static constexpr std::size_t limb_bits = sizeof(local_limb_type) * CHAR_BIT;
  1077. static constexpr local_limb_type limb_mask = limb_bits != MinBits ? static_cast<local_limb_type>(static_cast<local_limb_type>(~local_limb_type(0)) >> (limb_bits - MinBits))
  1078. : static_cast<local_limb_type>(~local_limb_type(0));
  1079. local_limb_type m_data;
  1080. using checked_type = std::integral_constant<int, Checked>;
  1081. //
  1082. // Interface invariants:
  1083. //
  1084. static_assert(MinBits <= sizeof(double_limb_type) * CHAR_BIT, "Template parameter MinBits is inconsistent with the parameter trivial - did you mistakingly try to override the trivial parameter?");
  1085. protected:
  1086. template <class T>
  1087. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= static_cast<int>(MinBits)))>::type
  1088. check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, false>&)
  1089. {
  1090. using common_type = typename std::common_type<T, local_limb_type>::type;
  1091. if (static_cast<common_type>(val) > limb_mask)
  1092. BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
  1093. }
  1094. template <class T>
  1095. BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val, const std::integral_constant<int, checked>&, const std::integral_constant<bool, true>&)
  1096. {
  1097. using common_type = typename std::common_type<T, local_limb_type>::type;
  1098. if (static_cast<common_type>(val) > static_cast<common_type>(limb_mask))
  1099. BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
  1100. if (val < 0)
  1101. BOOST_MP_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
  1102. }
  1103. template <class T, int C, bool B>
  1104. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void check_in_range(T, const std::integral_constant<int, C>&, const std::integral_constant<bool, B>&) noexcept {}
  1105. template <class T>
  1106. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void check_in_range(T val) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type(), boost::multiprecision::detail::is_signed<T>())))
  1107. {
  1108. check_in_range(val, checked_type(), boost::multiprecision::detail::is_signed<T>());
  1109. }
  1110. public:
  1111. //
  1112. // Direct construction:
  1113. //
  1114. #ifdef __MSVC_RUNTIME_CHECKS
  1115. template <class SI>
  1116. BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  1117. : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i & limb_mask)) & limb_mask : static_cast<local_limb_type>(i & limb_mask))
  1118. {}
  1119. template <class SI>
  1120. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
  1121. : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i & limb_mask) : static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
  1122. template <class UI>
  1123. BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  1124. : m_data(static_cast<local_limb_type>(i& limb_mask)) {}
  1125. template <class UI>
  1126. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
  1127. : m_data(static_cast<local_limb_type>(i & limb_mask)) { check_in_range(i); }
  1128. #else
  1129. template <class SI>
  1130. BOOST_MP_FORCEINLINE constexpr cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  1131. : m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask)
  1132. {}
  1133. template <class SI>
  1134. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(SI i, typename std::enable_if<boost::multiprecision::detail::is_signed<SI>::value && boost::multiprecision::detail::is_integral<SI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<SI>())))
  1135. : m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
  1136. template <class UI>
  1137. BOOST_MP_FORCEINLINE constexpr cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == unchecked)>::type const* = nullptr) noexcept
  1138. : m_data(static_cast<local_limb_type>(i) & limb_mask) {}
  1139. template <class UI>
  1140. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(UI i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<UI>::value && (Checked == checked)>::type const* = nullptr) noexcept(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<UI>())))
  1141. : m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
  1142. #endif
  1143. template <class F>
  1144. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_base(F i, typename std::enable_if<std::is_floating_point<F>::value >::type const* = nullptr) noexcept((Checked == unchecked))
  1145. : m_data(static_cast<local_limb_type>(i < 0 ? -i : i) & limb_mask)
  1146. {
  1147. check_in_range(i);
  1148. if (i < 0)
  1149. negate();
  1150. }
  1151. constexpr cpp_int_base(literals::detail::value_pack<>) noexcept
  1152. : m_data(static_cast<local_limb_type>(0u))
  1153. {}
  1154. template <limb_type a>
  1155. constexpr cpp_int_base(literals::detail::value_pack<a>) noexcept
  1156. : m_data(static_cast<local_limb_type>(a)) {}
  1157. template <limb_type a, limb_type b>
  1158. constexpr cpp_int_base(literals::detail::value_pack<a, b>) noexcept
  1159. : m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
  1160. //
  1161. // These are deprecated in C++20 unless we make them explicit:
  1162. //
  1163. BOOST_MP_CXX14_CONSTEXPR cpp_int_base& operator=(const cpp_int_base&) = default;
  1164. explicit constexpr cpp_int_base(scoped_shared_storage&, std::size_t) noexcept : m_data(0)
  1165. {}
  1166. //
  1167. // Helper functions for getting at our internal data, and manipulating storage:
  1168. //
  1169. BOOST_MP_FORCEINLINE constexpr std::size_t size() const noexcept { return 1; }
  1170. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR limb_pointer limbs() noexcept { return &m_data; }
  1171. BOOST_MP_FORCEINLINE constexpr const_limb_pointer limbs() const noexcept { return &m_data; }
  1172. BOOST_MP_FORCEINLINE constexpr bool sign() const noexcept { return false; }
  1173. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void sign(bool b) noexcept((Checked == unchecked))
  1174. {
  1175. if (b)
  1176. negate();
  1177. }
  1178. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void resize(unsigned, std::size_t min_size)
  1179. {
  1180. detail::verify_new_size(2, min_size, checked_type());
  1181. }
  1182. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void normalize() noexcept((Checked == unchecked))
  1183. {
  1184. detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
  1185. m_data &= limb_mask;
  1186. }
  1187. BOOST_MP_FORCEINLINE constexpr cpp_int_base() noexcept : m_data(0) {}
  1188. BOOST_MP_FORCEINLINE constexpr cpp_int_base(const cpp_int_base& o) noexcept
  1189. : m_data(o.m_data) {}
  1190. //~cpp_int_base() noexcept {}
  1191. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void assign(const cpp_int_base& o) noexcept
  1192. {
  1193. m_data = o.m_data;
  1194. }
  1195. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void negate()
  1196. #if !defined(BOOST_NO_CXX17_IF_CONSTEXPR)
  1197. noexcept((Checked == unchecked))
  1198. #endif
  1199. {
  1200. BOOST_IF_CONSTEXPR(Checked == checked)
  1201. {
  1202. BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
  1203. }
  1204. m_data = ~m_data;
  1205. ++m_data;
  1206. }
  1207. BOOST_MP_FORCEINLINE constexpr bool isneg() const noexcept
  1208. {
  1209. return false;
  1210. }
  1211. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void do_swap(cpp_int_base& o) noexcept
  1212. {
  1213. std_constexpr::swap(m_data, o.m_data);
  1214. }
  1215. };
  1216. //
  1217. // Traits class, lets us know whether type T can be directly converted to the base type,
  1218. // used to enable/disable constructors etc:
  1219. //
  1220. template <class Arg, class Base>
  1221. struct is_allowed_cpp_int_base_conversion : public std::conditional<
  1222. std::is_same<Arg, limb_type>::value || std::is_same<Arg, signed_limb_type>::value
  1223. #if BOOST_MP_ENDIAN_LITTLE_BYTE && !defined(BOOST_MP_TEST_NO_LE)
  1224. || std::is_same<Arg, double_limb_type>::value || std::is_same<Arg, signed_double_limb_type>::value
  1225. #endif
  1226. || literals::detail::is_value_pack<Arg>::value || (is_trivial_cpp_int<Base>::value && boost::multiprecision::detail::is_arithmetic<Arg>::value),
  1227. std::integral_constant<bool, true>,
  1228. std::integral_constant<bool, false>>::type
  1229. {};
  1230. //
  1231. // Now the actual backend, normalising parameters passed to the base class:
  1232. //
  1233. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  1234. struct cpp_int_backend
  1235. : public cpp_int_base<
  1236. min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
  1237. max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
  1238. SignType,
  1239. Checked,
  1240. Allocator,
  1241. is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>
  1242. {
  1243. using self_type = cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>;
  1244. using base_type = cpp_int_base<
  1245. min_precision<self_type>::value,
  1246. max_precision<self_type>::value,
  1247. SignType,
  1248. Checked,
  1249. Allocator,
  1250. is_trivial_cpp_int<self_type>::value>;
  1251. using trivial_tag = std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>;
  1252. public:
  1253. using signed_types = typename std::conditional<
  1254. is_trivial_cpp_int<self_type>::value,
  1255. std::tuple<
  1256. signed char, short, int, long,
  1257. long long, signed_double_limb_type>,
  1258. std::tuple<signed_limb_type, signed_double_limb_type> >::type;
  1259. using unsigned_types = typename std::conditional<
  1260. is_trivial_cpp_int<self_type>::value,
  1261. std::tuple<unsigned char, unsigned short, unsigned,
  1262. unsigned long, unsigned long long, double_limb_type>,
  1263. std::tuple<limb_type, double_limb_type> >::type;
  1264. using float_types = typename std::conditional<
  1265. is_trivial_cpp_int<self_type>::value,
  1266. std::tuple<float, double, long double>,
  1267. std::tuple<long double> >::type;
  1268. using checked_type = std::integral_constant<int, Checked> ;
  1269. BOOST_MP_FORCEINLINE constexpr cpp_int_backend() noexcept {}
  1270. BOOST_MP_FORCEINLINE constexpr cpp_int_backend(const cpp_int_backend& o) noexcept(std::is_void<Allocator>::value) : base_type(o) {}
  1271. // rvalue copy:
  1272. BOOST_MP_FORCEINLINE constexpr cpp_int_backend(cpp_int_backend&& o) noexcept
  1273. : base_type(static_cast<base_type&&>(o))
  1274. {}
  1275. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
  1276. BOOST_MP_FORCEINLINE BOOST_CXX14_CONSTEXPR cpp_int_backend(cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&& o, typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>, self_type>::value>::type* = nullptr) noexcept
  1277. {
  1278. *this = static_cast<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>&&>(o);
  1279. }
  1280. //
  1281. // Direct construction from arithmetic type:
  1282. //
  1283. template <class Arg>
  1284. BOOST_MP_FORCEINLINE constexpr cpp_int_backend(Arg i, typename std::enable_if<is_allowed_cpp_int_base_conversion<Arg, base_type>::value>::type const* = nullptr) noexcept(noexcept(base_type(std::declval<Arg>())))
  1285. : base_type(i) {}
  1286. //
  1287. // Aliasing constructor: the result will alias the memory referenced, unless
  1288. // we have fixed precision and storage, in which case we copy the memory:
  1289. //
  1290. explicit constexpr cpp_int_backend(limb_type* data, std::size_t offset, std::size_t len) noexcept
  1291. : base_type(data, offset, len) {}
  1292. explicit cpp_int_backend(const limb_type* data, std::size_t offset, std::size_t len) noexcept
  1293. : base_type(data, offset, len) { this->normalize(); }
  1294. explicit constexpr cpp_int_backend(typename base_type::scoped_shared_storage& data, std::size_t len) noexcept
  1295. : base_type(data, len) {}
  1296. private:
  1297. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1298. BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::true_type const&, std::true_type const&)
  1299. {
  1300. // Assigning trivial type to trivial type:
  1301. this->check_in_range(*other.limbs());
  1302. *this->limbs() = static_cast<typename self_type::local_limb_type>(*other.limbs());
  1303. this->sign(other.sign());
  1304. this->normalize();
  1305. }
  1306. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1307. BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::true_type const&, std::false_type const&)
  1308. {
  1309. // non-trivial to trivial narrowing conversion:
  1310. double_limb_type v = *other.limbs();
  1311. if (other.size() > 1)
  1312. {
  1313. v |= static_cast<double_limb_type>(other.limbs()[1]) << bits_per_limb;
  1314. BOOST_IF_CONSTEXPR(Checked == checked)
  1315. {
  1316. if (other.size() > 2)
  1317. {
  1318. BOOST_MP_THROW_EXCEPTION(std::range_error("Assignment of a cpp_int that is out of range for the target type."));
  1319. }
  1320. }
  1321. }
  1322. *this = v;
  1323. this->sign(other.sign());
  1324. this->normalize();
  1325. }
  1326. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1327. BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::false_type const&, std::true_type const&)
  1328. {
  1329. // trivial to non-trivial, treat the trivial argument as if it were an unsigned arithmetic type, then set the sign afterwards:
  1330. *this = static_cast<
  1331. typename boost::multiprecision::detail::canonical<
  1332. typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>::local_limb_type,
  1333. cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type>(*other.limbs());
  1334. this->sign(other.sign());
  1335. }
  1336. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1337. BOOST_MP_CXX14_CONSTEXPR void do_assign(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other, std::false_type const&, std::false_type const&)
  1338. {
  1339. // regular non-trivial to non-trivial assign:
  1340. this->resize(other.size(), other.size());
  1341. #if !defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
  1342. std::size_t count = (std::min)(other.size(), this->size());
  1343. for (std::size_t i = 0; i < count; ++i)
  1344. this->limbs()[i] = other.limbs()[i];
  1345. #else
  1346. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  1347. if (BOOST_MP_IS_CONST_EVALUATED(other.size()))
  1348. {
  1349. std::size_t count = (std::min)(other.size(), this->size());
  1350. for (std::size_t i = 0; i < count; ++i)
  1351. this->limbs()[i] = other.limbs()[i];
  1352. }
  1353. else
  1354. #endif
  1355. {
  1356. static_assert(sizeof(other.limbs()[0]) == sizeof(this->limbs()[0]), "This method requires equal limb sizes");
  1357. std::memcpy(this->limbs(), other.limbs(), (std::min)(other.size() * sizeof(other.limbs()[0]), this->size() * sizeof(this->limbs()[0])));
  1358. }
  1359. #endif
  1360. this->sign(other.sign());
  1361. this->normalize();
  1362. }
  1363. public:
  1364. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1365. BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
  1366. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
  1367. typename std::enable_if<is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value>::type* = nullptr)
  1368. : base_type()
  1369. {
  1370. do_assign(
  1371. other,
  1372. std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
  1373. std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
  1374. }
  1375. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1376. explicit BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(
  1377. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other,
  1378. typename std::enable_if< !(is_implicit_cpp_int_conversion<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, self_type>::value)>::type* = nullptr)
  1379. : base_type()
  1380. {
  1381. do_assign(
  1382. other,
  1383. std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
  1384. std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
  1385. }
  1386. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  1387. BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(
  1388. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& other)
  1389. {
  1390. do_assign(
  1391. other,
  1392. std::integral_constant<bool, is_trivial_cpp_int<self_type>::value>(),
  1393. std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>());
  1394. return *this;
  1395. }
  1396. constexpr cpp_int_backend(const cpp_int_backend& a, const literals::detail::negate_tag& tag)
  1397. : base_type(static_cast<const base_type&>(a), tag)
  1398. {}
  1399. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(const cpp_int_backend& o) noexcept(noexcept(std::declval<cpp_int_backend>().assign(std::declval<const cpp_int_backend&>())))
  1400. {
  1401. this->assign(o);
  1402. return *this;
  1403. }
  1404. // rvalue copy:
  1405. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR cpp_int_backend& operator=(cpp_int_backend&& o) noexcept(noexcept(std::declval<base_type&>() = std::declval<base_type>()))
  1406. {
  1407. *static_cast<base_type*>(this) = static_cast<base_type&&>(o);
  1408. return *this;
  1409. }
  1410. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2>
  1411. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<((MaxBits2 <= MaxBits) || (MaxBits == 0)) && !std::is_void<Allocator>::value, cpp_int_backend&>::type operator=(cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator>&& o) noexcept
  1412. {
  1413. *static_cast<base_type*>(this) = static_cast<typename cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2>::base_type&&>(o);
  1414. return *this;
  1415. }
  1416. template <class A>
  1417. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  1418. boost::multiprecision::detail::is_unsigned<A>::value
  1419. && trivial_tag::value, cpp_int_backend&>::type
  1420. operator=(const A& val)
  1421. noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())))
  1422. {
  1423. this->check_in_range(val);
  1424. *this->limbs() = static_cast<typename self_type::local_limb_type>(val);
  1425. this->sign(false);
  1426. this->normalize();
  1427. return *this;
  1428. }
  1429. template <class A>
  1430. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  1431. !(boost::multiprecision::detail::is_unsigned<A>::value || !boost::multiprecision::detail::is_integral<A>::value)
  1432. && trivial_tag::value, cpp_int_backend&>::type
  1433. operator=(const A& val)
  1434. noexcept(noexcept(std::declval<cpp_int_backend>().check_in_range(std::declval<A>())) && noexcept(std::declval<cpp_int_backend>().sign(true)))
  1435. {
  1436. this->check_in_range(val);
  1437. *this->limbs() = (val < 0) ? static_cast<typename self_type::local_limb_type>(boost::multiprecision::detail::unsigned_abs(val)) : static_cast<typename self_type::local_limb_type>(val);
  1438. this->sign(val < 0);
  1439. this->normalize();
  1440. return *this;
  1441. }
  1442. template <class A>
  1443. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  1444. std::is_convertible<A, limb_type>::value
  1445. && !boost::multiprecision::detail::is_integral<A>::value
  1446. && trivial_tag::value, cpp_int_backend&>::type
  1447. operator=(const A& val)
  1448. {
  1449. this->check_in_range(val);
  1450. *this->limbs() = (val < 0) ? static_cast<typename self_type::local_limb_type>(boost::multiprecision::detail::abs(val)) : static_cast<typename self_type::local_limb_type>(val);
  1451. this->sign(val < 0);
  1452. this->normalize();
  1453. return *this;
  1454. }
  1455. template <class A>
  1456. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  1457. std::is_same<A, limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
  1458. operator=(A i) noexcept
  1459. {
  1460. this->resize(1, 1);
  1461. *this->limbs() = i;
  1462. this->sign(false);
  1463. return *this;
  1464. }
  1465. template <class A>
  1466. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
  1467. std::is_same<A, signed_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
  1468. operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
  1469. {
  1470. this->resize(1, 1);
  1471. *this->limbs() = static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i));
  1472. this->sign(i < 0);
  1473. return *this;
  1474. }
  1475. template <class A>
  1476. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
  1477. std::is_same<A, double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
  1478. operator=(A i) noexcept
  1479. {
  1480. static_assert(sizeof(i) == 2 * sizeof(limb_type), "Failed integer size check");
  1481. static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count");
  1482. typename base_type::limb_pointer p = this->limbs();
  1483. #ifdef __MSVC_RUNTIME_CHECKS
  1484. *p = static_cast<limb_type>(i & ~static_cast<limb_type>(0));
  1485. #else
  1486. *p = static_cast<limb_type>(i);
  1487. #endif
  1488. p[1] = static_cast<limb_type>(i >> base_type::limb_bits);
  1489. this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
  1490. this->sign(false);
  1491. return *this;
  1492. }
  1493. template <class A>
  1494. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
  1495. std::is_same<A, signed_double_limb_type>::value && !trivial_tag::value, cpp_int_backend&>::type
  1496. operator=(A i) noexcept(noexcept(std::declval<cpp_int_backend>().sign(true)))
  1497. {
  1498. static_assert(sizeof(i) == 2 * sizeof(limb_type), "double limb type size check failed");
  1499. static_assert(base_type::internal_limb_count >= 2, "Failed internal limb count check");
  1500. bool s = false;
  1501. if (i < 0)
  1502. s = true;
  1503. double_limb_type ui = static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i));
  1504. typename base_type::limb_pointer p = this->limbs();
  1505. #ifdef __MSVC_RUNTIME_CHECKS
  1506. *p = static_cast<limb_type>(ui & ~static_cast<limb_type>(0));
  1507. #else
  1508. *p = static_cast<limb_type>(ui);
  1509. #endif
  1510. p[1] = static_cast<limb_type>(ui >> base_type::limb_bits);
  1511. this->resize(p[1] ? 2 : 1, p[1] ? 2 : 1);
  1512. this->sign(s);
  1513. return *this;
  1514. }
  1515. private:
  1516. template <class F>
  1517. BOOST_MP_CXX14_CONSTEXPR void do_assign_float(F a)
  1518. {
  1519. using default_ops::eval_add;
  1520. using default_ops::eval_subtract;
  1521. BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
  1522. if (a < 0)
  1523. {
  1524. do_assign_float(-a);
  1525. this->sign(true);
  1526. return;
  1527. }
  1528. if (a == 0)
  1529. {
  1530. *this = static_cast<limb_type>(0u);
  1531. }
  1532. if (a == 1)
  1533. {
  1534. *this = static_cast<limb_type>(1u);
  1535. }
  1536. if (!BOOST_MP_ISFINITE(a))
  1537. {
  1538. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Cannot convert a non-finite number to an integer."));
  1539. }
  1540. int e = 0;
  1541. F f(0), term(0);
  1542. *this = static_cast<limb_type>(0u);
  1543. f = frexp(a, &e);
  1544. #if !(defined(__clang__) && (__clang_major__ <= 7))
  1545. constexpr limb_type shift = std::numeric_limits<limb_type>::digits;
  1546. #else
  1547. // clang 7 has an issue converting long double to unsigned long long in
  1548. // release mode (bits get dropped, conversion appears to go via float)
  1549. // Never extract more than double bits at a time:
  1550. constexpr limb_type shift = std::numeric_limits<limb_type>::digits > std::numeric_limits<double>::digits
  1551. ? std::numeric_limits<double>::digits : std::numeric_limits<limb_type>::digits;
  1552. #endif
  1553. while (f != static_cast<F>(0.0f))
  1554. {
  1555. // extract int sized bits from f:
  1556. f = ldexp(f, shift);
  1557. term = floor(f);
  1558. e = e - static_cast<int>(shift);
  1559. eval_left_shift(*this, shift);
  1560. #if !(defined(__clang__) && (__clang_major__ <= 7))
  1561. if (term > 0)
  1562. eval_add(*this, static_cast<limb_type>(term));
  1563. else
  1564. eval_subtract(*this, static_cast<limb_type>(-term));
  1565. #else
  1566. // clang 7 requires extra cast to double to avoid buggy code generation:
  1567. if (term > 0)
  1568. eval_add(*this, static_cast<limb_type>(static_cast<double>(term)));
  1569. else
  1570. eval_subtract(*this, static_cast<limb_type>(static_cast<double>(-term)));
  1571. #endif
  1572. f -= term;
  1573. }
  1574. if (e > 0)
  1575. eval_left_shift(*this, static_cast<unsigned int>(e));
  1576. else if (e < 0)
  1577. eval_right_shift(*this, static_cast<unsigned int>(-e));
  1578. }
  1579. public:
  1580. template <class A>
  1581. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if <
  1582. std::is_floating_point<A>::value && !trivial_tag::value, cpp_int_backend&>::type
  1583. operator=(A a)
  1584. {
  1585. do_assign_float(a);
  1586. return *this;
  1587. }
  1588. private:
  1589. void do_assign_string(const char* s, const std::integral_constant<bool, true>&)
  1590. {
  1591. std::size_t n = s ? std::strlen(s) : 0;
  1592. *this = 0;
  1593. unsigned radix = 10;
  1594. bool isneg = false;
  1595. if (n && (*s == '-'))
  1596. {
  1597. --n;
  1598. ++s;
  1599. isneg = true;
  1600. }
  1601. if (n && (*s == '0'))
  1602. {
  1603. if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
  1604. {
  1605. radix = 16;
  1606. s += 2;
  1607. n -= 2;
  1608. }
  1609. else
  1610. {
  1611. radix = 8;
  1612. n -= 1;
  1613. }
  1614. }
  1615. if (n)
  1616. {
  1617. unsigned val;
  1618. while (*s)
  1619. {
  1620. if (*s >= '0' && *s <= '9')
  1621. val = static_cast<unsigned>(*s - '0');
  1622. else if (*s >= 'a' && *s <= 'f')
  1623. val = 10u + static_cast<unsigned>(*s - 'a');
  1624. else if (*s >= 'A' && *s <= 'F')
  1625. val = 10u + static_cast<unsigned>(*s - 'A');
  1626. else
  1627. val = radix + 1u;
  1628. if (val >= radix)
  1629. {
  1630. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
  1631. }
  1632. *this->limbs() = detail::checked_multiply(*this->limbs(), static_cast<typename base_type::local_limb_type>(radix), checked_type());
  1633. *this->limbs() = detail::checked_add(*this->limbs(), static_cast<typename base_type::local_limb_type>(val), checked_type());
  1634. ++s;
  1635. }
  1636. }
  1637. if (isneg)
  1638. this->negate();
  1639. }
  1640. void do_assign_string(const char* s, const std::integral_constant<bool, false>&)
  1641. {
  1642. using default_ops::eval_add;
  1643. using default_ops::eval_multiply;
  1644. std::size_t n = s ? std::strlen(s) : 0;
  1645. *this = static_cast<limb_type>(0u);
  1646. unsigned radix = 10;
  1647. bool isneg = false;
  1648. if (n && (*s == '-'))
  1649. {
  1650. --n;
  1651. ++s;
  1652. isneg = true;
  1653. }
  1654. if (n && (*s == '0'))
  1655. {
  1656. if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
  1657. {
  1658. radix = 16;
  1659. s += 2;
  1660. n -= 2;
  1661. }
  1662. else
  1663. {
  1664. radix = 8;
  1665. n -= 1;
  1666. }
  1667. }
  1668. //
  1669. // Exception guarantee: create the result in stack variable "result"
  1670. // then do a swap at the end. In the event of a throw, *this will
  1671. // be left unchanged.
  1672. //
  1673. cpp_int_backend result;
  1674. if (n)
  1675. {
  1676. if (radix == 16)
  1677. {
  1678. while (*s == '0')
  1679. ++s;
  1680. std::size_t bitcount = 4 * std::strlen(s);
  1681. limb_type val;
  1682. std::size_t limb, shift;
  1683. if (bitcount > 4)
  1684. bitcount -= 4;
  1685. else
  1686. bitcount = 0;
  1687. std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
  1688. result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
  1689. std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
  1690. while (*s)
  1691. {
  1692. if (*s >= '0' && *s <= '9')
  1693. val = static_cast<unsigned>(*s - '0');
  1694. else if (*s >= 'a' && *s <= 'f')
  1695. val = 10u + static_cast<unsigned>(*s - 'a');
  1696. else if (*s >= 'A' && *s <= 'F')
  1697. val = 10u + static_cast<unsigned>(*s - 'A');
  1698. else
  1699. {
  1700. #if defined(BOOST_NO_EXCEPTIONS)
  1701. val = static_cast<unsigned>('0');
  1702. #endif
  1703. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
  1704. }
  1705. limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
  1706. shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
  1707. val <<= shift;
  1708. if (result.size() > limb)
  1709. {
  1710. result.limbs()[limb] |= val;
  1711. }
  1712. ++s;
  1713. bitcount -= 4;
  1714. }
  1715. result.normalize();
  1716. }
  1717. else if (radix == 8)
  1718. {
  1719. while (*s == '0')
  1720. ++s;
  1721. std::size_t bitcount = 3 * std::strlen(s);
  1722. limb_type val;
  1723. std::size_t limb, shift;
  1724. if (bitcount > 3)
  1725. bitcount -= 3;
  1726. else
  1727. bitcount = 0;
  1728. std::size_t newsize = bitcount / (sizeof(limb_type) * CHAR_BIT) + 1;
  1729. result.resize(static_cast<unsigned>(newsize), static_cast<unsigned>(newsize)); // will throw if this is a checked integer that cannot be resized
  1730. std::memset(result.limbs(), 0, result.size() * sizeof(limb_type));
  1731. while (*s)
  1732. {
  1733. if (*s >= '0' && *s <= '7')
  1734. val = static_cast<unsigned>(*s - '0');
  1735. else
  1736. {
  1737. #if defined(BOOST_NO_EXCEPTIONS)
  1738. val = static_cast<unsigned>('0');
  1739. #endif
  1740. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected content found while parsing character string."));
  1741. }
  1742. limb = bitcount / (sizeof(limb_type) * CHAR_BIT);
  1743. shift = bitcount % (sizeof(limb_type) * CHAR_BIT);
  1744. if (result.size() > limb)
  1745. {
  1746. result.limbs()[limb] |= (val << shift);
  1747. if (shift > sizeof(limb_type) * CHAR_BIT - 3)
  1748. {
  1749. // Deal with the bits in val that overflow into the next limb:
  1750. val >>= (sizeof(limb_type) * CHAR_BIT - shift);
  1751. if (val)
  1752. {
  1753. // If this is the most-significant-limb, we may need to allocate an extra one for the overflow:
  1754. if (limb + 1 == newsize)
  1755. {
  1756. result.resize(static_cast<unsigned>(newsize + 1), static_cast<unsigned>(newsize + 1));
  1757. result.limbs()[limb + 1] = 0;
  1758. }
  1759. if (result.size() > limb + 1)
  1760. {
  1761. result.limbs()[limb + 1] |= val;
  1762. }
  1763. }
  1764. }
  1765. }
  1766. ++s;
  1767. bitcount -= 3;
  1768. }
  1769. result.normalize();
  1770. }
  1771. else
  1772. {
  1773. // Base 10, we extract blocks of size 10^9 at a time, that way
  1774. // the number of multiplications is kept to a minimum:
  1775. limb_type block_mult = max_block_10;
  1776. while (*s)
  1777. {
  1778. limb_type block = 0;
  1779. for (unsigned i = 0; i < digits_per_block_10; ++i)
  1780. {
  1781. limb_type val;
  1782. if (*s >= '0' && *s <= '9')
  1783. val = static_cast<limb_type>(*s - '0');
  1784. else
  1785. {
  1786. #if defined(BOOST_NO_EXCEPTIONS)
  1787. val = static_cast<unsigned>('0');
  1788. #endif
  1789. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Unexpected character encountered in input."));
  1790. }
  1791. block *= 10;
  1792. block += val;
  1793. if (!*++s)
  1794. {
  1795. block_mult = block_multiplier(i);
  1796. break;
  1797. }
  1798. }
  1799. eval_multiply(result, block_mult);
  1800. eval_add(result, block);
  1801. }
  1802. }
  1803. }
  1804. if (isneg)
  1805. result.negate();
  1806. result.swap(*this);
  1807. }
  1808. public:
  1809. cpp_int_backend& operator=(const char* s)
  1810. {
  1811. do_assign_string(s, trivial_tag());
  1812. return *this;
  1813. }
  1814. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(cpp_int_backend& o) noexcept
  1815. {
  1816. this->do_swap(o);
  1817. }
  1818. private:
  1819. std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
  1820. {
  1821. using io_type = typename std::conditional<sizeof(typename base_type::local_limb_type) == 1, unsigned, typename base_type::local_limb_type>::type;
  1822. if (this->sign() && (((f & std::ios_base::hex) == std::ios_base::hex) || ((f & std::ios_base::oct) == std::ios_base::oct)))
  1823. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
  1824. std::stringstream ss;
  1825. ss.flags(f & ~std::ios_base::showpos);
  1826. ss << static_cast<io_type>(*this->limbs());
  1827. std::string result;
  1828. if (this->sign())
  1829. result += '-';
  1830. else if (f & std::ios_base::showpos)
  1831. result += '+';
  1832. result += ss.str();
  1833. return result;
  1834. }
  1835. std::string do_get_trivial_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
  1836. {
  1837. // Even though we have only one limb, we can't do IO on it :-(
  1838. int base = 10;
  1839. if ((f & std::ios_base::oct) == std::ios_base::oct)
  1840. base = 8;
  1841. else if ((f & std::ios_base::hex) == std::ios_base::hex)
  1842. base = 16;
  1843. std::string result;
  1844. std::size_t Bits = sizeof(typename base_type::local_limb_type) * CHAR_BIT;
  1845. if (base == 8 || base == 16)
  1846. {
  1847. if (this->sign())
  1848. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
  1849. limb_type shift = base == 8 ? 3 : 4;
  1850. limb_type mask = static_cast<limb_type>((1u << shift) - 1);
  1851. typename base_type::local_limb_type v = *this->limbs();
  1852. result.assign(Bits / shift + (Bits % shift ? 1 : 0), '0');
  1853. std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
  1854. char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
  1855. for (std::size_t i = 0; i < Bits / shift; ++i)
  1856. {
  1857. char c = static_cast<char>('0' + static_cast<char>(v & mask));
  1858. if (c > '9')
  1859. c = static_cast<char>(c + letter_a - '9' - 1);
  1860. result[static_cast<std::size_t>(pos)] = c;
  1861. --pos;
  1862. v >>= shift;
  1863. }
  1864. if (Bits % shift)
  1865. {
  1866. mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
  1867. char c = static_cast<char>('0' + static_cast<char>(v & mask));
  1868. if (c > '9')
  1869. c = static_cast<char>(c + letter_a - '9');
  1870. result[static_cast<std::size_t>(pos)] = c;
  1871. }
  1872. //
  1873. // Get rid of leading zeros:
  1874. //
  1875. std::string::size_type n = result.find_first_not_of('0');
  1876. if (!result.empty() && (n == std::string::npos))
  1877. n = result.size() - 1;
  1878. result.erase(0, n);
  1879. if (f & std::ios_base::showbase)
  1880. {
  1881. const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
  1882. result.insert(static_cast<std::string::size_type>(0), pp);
  1883. }
  1884. }
  1885. else
  1886. {
  1887. result.assign(Bits / 3 + 1, '0');
  1888. std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
  1889. typename base_type::local_limb_type v(*this->limbs());
  1890. bool neg = false;
  1891. if (this->sign())
  1892. {
  1893. neg = true;
  1894. }
  1895. while (v)
  1896. {
  1897. result[static_cast<std::string::size_type>(pos)] = static_cast<char>(static_cast<char>(v % 10) + '0');
  1898. --pos;
  1899. v /= 10;
  1900. }
  1901. std::string::size_type n = result.find_first_not_of('0');
  1902. result.erase(0, n);
  1903. if (result.empty())
  1904. result = "0";
  1905. if (neg)
  1906. result.insert(static_cast<std::string::size_type>(0), 1, '-');
  1907. else if (f & std::ios_base::showpos)
  1908. result.insert(static_cast<std::string::size_type>(0), 1, '+');
  1909. }
  1910. return result;
  1911. }
  1912. std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, true>&) const
  1913. {
  1914. #ifdef BOOST_MP_NO_DOUBLE_LIMB_TYPE_IO
  1915. return do_get_trivial_string(f, std::integral_constant<bool, std::is_same<typename base_type::local_limb_type, double_limb_type>::value>());
  1916. #else
  1917. return do_get_trivial_string(f, std::integral_constant<bool, false>());
  1918. #endif
  1919. }
  1920. std::string do_get_string(std::ios_base::fmtflags f, const std::integral_constant<bool, false>&) const
  1921. {
  1922. using default_ops::eval_get_sign;
  1923. int base = 10;
  1924. if ((f & std::ios_base::oct) == std::ios_base::oct)
  1925. base = 8;
  1926. else if ((f & std::ios_base::hex) == std::ios_base::hex)
  1927. base = 16;
  1928. std::string result;
  1929. std::size_t Bits = this->size() * base_type::limb_bits;
  1930. if (base == 8 || base == 16)
  1931. {
  1932. if (this->sign())
  1933. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Base 8 or 16 printing of negative numbers is not supported."));
  1934. limb_type shift = base == 8 ? 3 : 4;
  1935. limb_type mask = static_cast<limb_type>((1u << shift) - 1);
  1936. cpp_int_backend t(*this);
  1937. result.assign(Bits / shift + ((Bits % shift) ? 1 : 0), '0');
  1938. std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
  1939. char letter_a = f & std::ios_base::uppercase ? 'A' : 'a';
  1940. for (std::size_t i = 0; i < Bits / shift; ++i)
  1941. {
  1942. char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
  1943. if (c > '9')
  1944. c = static_cast<char>(c + letter_a - '9' - 1);
  1945. result[static_cast<std::size_t>(pos)] = c;
  1946. --pos;
  1947. eval_right_shift(t, shift);
  1948. }
  1949. if (Bits % shift)
  1950. {
  1951. mask = static_cast<limb_type>((1u << (Bits % shift)) - 1);
  1952. char c = static_cast<char>('0' + static_cast<char>(t.limbs()[0] & mask));
  1953. if (c > '9')
  1954. c = static_cast<char>(c + letter_a - '9');
  1955. result[static_cast<std::size_t>(pos)] = c;
  1956. }
  1957. //
  1958. // Get rid of leading zeros:
  1959. //
  1960. std::string::size_type n = result.find_first_not_of('0');
  1961. if (!result.empty() && (n == std::string::npos))
  1962. n = result.size() - 1;
  1963. result.erase(0, n);
  1964. if (f & std::ios_base::showbase)
  1965. {
  1966. const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
  1967. result.insert(static_cast<std::string::size_type>(0), pp);
  1968. }
  1969. }
  1970. else
  1971. {
  1972. result.assign(Bits / 3 + 1, '0');
  1973. std::string::difference_type pos = static_cast<std::string::difference_type>(result.size() - 1u);
  1974. cpp_int_backend t(*this);
  1975. cpp_int_backend r;
  1976. bool neg = false;
  1977. if (t.sign())
  1978. {
  1979. t.negate();
  1980. neg = true;
  1981. }
  1982. if (this->size() == 1)
  1983. {
  1984. result = std::to_string(t.limbs()[0]);
  1985. }
  1986. else
  1987. {
  1988. cpp_int_backend block10;
  1989. block10 = max_block_10;
  1990. while (eval_get_sign(t) != 0)
  1991. {
  1992. cpp_int_backend t2;
  1993. divide_unsigned_helper(&t2, t, block10, r);
  1994. t = t2;
  1995. limb_type v = r.limbs()[0];
  1996. for (std::size_t i = 0; i < digits_per_block_10; ++i)
  1997. {
  1998. char c = static_cast<char>('0' + static_cast<char>(v % 10));
  1999. v /= 10;
  2000. result[static_cast<std::size_t>(pos)] = c;
  2001. if (pos-- == 0u)
  2002. break;
  2003. }
  2004. }
  2005. }
  2006. std::string::size_type n = result.find_first_not_of('0');
  2007. result.erase(0, n);
  2008. if (result.empty())
  2009. result = std::string(static_cast<std::size_t>(1u), '0');
  2010. if (neg)
  2011. result.insert(static_cast<std::string::size_type>(0), 1, '-');
  2012. else if (f & std::ios_base::showpos)
  2013. result.insert(static_cast<std::string::size_type>(0), 1, '+');
  2014. }
  2015. return result;
  2016. }
  2017. public:
  2018. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
  2019. {
  2020. return do_get_string(f, trivial_tag());
  2021. }
  2022. private:
  2023. template <class Container>
  2024. void construct_from_container(const Container& c, const std::integral_constant<bool, false>&)
  2025. {
  2026. //
  2027. // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
  2028. //
  2029. std::size_t newsize = static_cast<unsigned>(c.size() / sizeof(limb_type));
  2030. if (c.size() % sizeof(limb_type))
  2031. {
  2032. ++newsize;
  2033. }
  2034. if (newsize)
  2035. {
  2036. this->resize(newsize, newsize); // May throw
  2037. std::memset(this->limbs(), 0, this->size());
  2038. typename Container::const_iterator i(c.begin()), j(c.end());
  2039. std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
  2040. while (i != j)
  2041. {
  2042. std::size_t limb = byte_location / sizeof(limb_type);
  2043. std::size_t shift = (byte_location % sizeof(limb_type)) * CHAR_BIT;
  2044. if (this->size() > limb)
  2045. this->limbs()[limb] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
  2046. ++i;
  2047. --byte_location;
  2048. }
  2049. }
  2050. }
  2051. template <class Container>
  2052. BOOST_MP_CXX14_CONSTEXPR void construct_from_container(const Container& c, const std::integral_constant<bool, true>&)
  2053. {
  2054. //
  2055. // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
  2056. //
  2057. using local_limb_type = typename base_type::local_limb_type;
  2058. *this->limbs() = 0;
  2059. if (c.size())
  2060. {
  2061. typename Container::const_iterator i(c.begin()), j(c.end());
  2062. std::size_t byte_location = static_cast<unsigned>(c.size() - 1);
  2063. while (i != j)
  2064. {
  2065. std::size_t limb = byte_location / sizeof(local_limb_type);
  2066. std::size_t shift = (byte_location % sizeof(local_limb_type)) * CHAR_BIT;
  2067. if (limb == 0)
  2068. this->limbs()[0] |= static_cast<limb_type>(static_cast<unsigned char>(*i)) << shift;
  2069. ++i;
  2070. --byte_location;
  2071. }
  2072. }
  2073. }
  2074. public:
  2075. template <class Container>
  2076. BOOST_MP_CXX14_CONSTEXPR cpp_int_backend(const Container& c, typename std::enable_if<boost::multiprecision::detail::is_byte_container<Container>::value>::type const* = nullptr)
  2077. {
  2078. //
  2079. // We assume that c is a sequence of (unsigned) bytes with the most significant byte first:
  2080. //
  2081. construct_from_container(c, trivial_tag());
  2082. }
  2083. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2084. BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&) const noexcept
  2085. {
  2086. if (this->sign() != o.sign())
  2087. return this->sign() ? -1 : 1;
  2088. // Only do the compare if the same sign:
  2089. int result = compare_unsigned(o);
  2090. if (this->sign())
  2091. result = -result;
  2092. return result;
  2093. }
  2094. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2095. BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&) const
  2096. {
  2097. cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t(*this);
  2098. return t.compare(o);
  2099. }
  2100. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2101. BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&) const
  2102. {
  2103. cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t(o);
  2104. return compare(t);
  2105. }
  2106. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2107. BOOST_MP_CXX14_CONSTEXPR int compare_imp(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&) const noexcept
  2108. {
  2109. if (this->sign())
  2110. {
  2111. if (o.sign())
  2112. {
  2113. return *this->limbs() < *o.limbs() ? 1 : (*this->limbs() > *o.limbs() ? -1 : 0);
  2114. }
  2115. else
  2116. return -1;
  2117. }
  2118. else
  2119. {
  2120. if (o.sign())
  2121. return 1;
  2122. return *this->limbs() < *o.limbs() ? -1 : (*this->limbs() > *o.limbs() ? 1 : 0);
  2123. }
  2124. }
  2125. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2126. BOOST_MP_CXX14_CONSTEXPR int compare(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
  2127. {
  2128. using t1 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> ;
  2129. using t2 = std::integral_constant<bool, is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>;
  2130. return compare_imp(o, t1(), t2());
  2131. }
  2132. template <std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2133. BOOST_MP_CXX14_CONSTEXPR int compare_unsigned(const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& o) const noexcept
  2134. {
  2135. if (this->size() != o.size())
  2136. {
  2137. return this->size() > o.size() ? 1 : -1;
  2138. }
  2139. typename base_type::const_limb_pointer pa = this->limbs();
  2140. typename base_type::const_limb_pointer pb = o.limbs();
  2141. for (std::ptrdiff_t i = static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(this->size()) - 1); i >= 0; --i)
  2142. {
  2143. if (pa[i] != pb[i])
  2144. return pa[i] > pb[i] ? 1 : -1;
  2145. }
  2146. return 0;
  2147. }
  2148. template <class Arithmetic>
  2149. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Arithmetic>::value, int>::type compare(Arithmetic i) const
  2150. {
  2151. // braindead version:
  2152. cpp_int_backend t;
  2153. t = i;
  2154. return compare(t);
  2155. }
  2156. };
  2157. } // namespace backends
  2158. namespace default_ops {
  2159. template <class Backend>
  2160. struct double_precision_type;
  2161. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  2162. struct double_precision_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  2163. {
  2164. using type = typename std::conditional<
  2165. backends::is_fixed_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
  2166. backends::cpp_int_backend<
  2167. (std::is_void<Allocator>::value ? 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value
  2168. : MinBits),
  2169. 2 * backends::max_precision<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value,
  2170. SignType,
  2171. Checked,
  2172. Allocator>,
  2173. backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::type;
  2174. };
  2175. } // namespace default_ops
  2176. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, std::size_t MinBits2, std::size_t MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  2177. struct is_equivalent_number_type<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
  2178. : public std::integral_constant<bool, std::numeric_limits<number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, et_on> >::digits == std::numeric_limits<number<backends::cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>, et_on> >::digits>{};
  2179. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  2180. struct number_category<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public std::integral_constant<int, number_kind_integer>
  2181. {};
  2182. #ifdef BOOST_HAS_INT128
  2183. namespace detail {
  2184. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  2185. struct is_convertible_arithmetic<int128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  2186. {
  2187. static constexpr bool value = true;
  2188. };
  2189. template <std::size_t MinBits, std::size_t MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
  2190. struct is_convertible_arithmetic<uint128_type, backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
  2191. {
  2192. static constexpr bool value = true;
  2193. };
  2194. }
  2195. #endif
  2196. #if defined(__GNUC__) && !defined(__clang__)
  2197. // see https://github.com/boostorg/multiprecision/issues/413
  2198. // and https://github.com/boostorg/multiprecision/issues/431
  2199. #pragma GCC diagnostic pop
  2200. #endif
  2201. #ifdef BOOST_MSVC
  2202. #pragma warning(pop)
  2203. #endif
  2204. }} // namespace boost::multiprecision
  2205. //
  2206. // Last of all we include the implementations of all the eval_* non member functions:
  2207. //
  2208. #include <boost/multiprecision/cpp_int/limits.hpp>
  2209. #include <boost/multiprecision/cpp_int/comparison.hpp>
  2210. #include <boost/multiprecision/cpp_int/add.hpp>
  2211. #include <boost/multiprecision/cpp_int/multiply.hpp>
  2212. #include <boost/multiprecision/cpp_int/divide.hpp>
  2213. #include <boost/multiprecision/cpp_int/bitwise.hpp>
  2214. #include <boost/multiprecision/cpp_int/misc.hpp>
  2215. #include <boost/multiprecision/cpp_int/literals.hpp>
  2216. #include <boost/multiprecision/cpp_int/serialize.hpp>
  2217. #include <boost/multiprecision/cpp_int/import_export.hpp>
  2218. #endif