cpp_int.hpp 95 KB

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