cpp_int.hpp 91 KB

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