cpp_int.hpp 74 KB

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