cpp_int.hpp 100 KB

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