cpp_int.hpp 85 KB

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