-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_int.hpp
1944 lines (1832 loc) · 82.2 KB
/
cpp_int.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////3/////////////////////////////////////////////
// Copyright 2012 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
#ifndef BOOST_MP_CPP_INT_HPP
#define BOOST_MP_CPP_INT_HPP
#include <iostream>
#include <iomanip>
#include <boost/cstdint.hpp>
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/detail/integer_ops.hpp>
#include <boost/array.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/multiprecision/cpp_int/cpp_int_config.hpp>
#include <boost/multiprecision/rational_adaptor.hpp>
#include <boost/multiprecision/traits/is_byte_container.hpp>
#include <boost/detail/endian.hpp>
#include <boost/integer/static_min_max.hpp>
#include <boost/type_traits/common_type.hpp>
#include <boost/type_traits/make_signed.hpp>
#include <boost/multiprecision/cpp_int/checked.hpp>
#ifdef BOOST_MP_USER_DEFINED_LITERALS
#include <boost/multiprecision/cpp_int/value_pack.hpp>
#endif
namespace boost{
namespace multiprecision{
namespace backends{
using boost::enable_if;
#ifdef BOOST_MSVC
// warning C4127: conditional expression is constant
#pragma warning(push)
#pragma warning(disable:4127 4351 4293 4996 4307 4702 6285)
#endif
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 >
struct cpp_int_backend;
} // namespace backends
namespace detail {
template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct is_byte_container<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> > : public boost::false_type {};
} // namespace detail
namespace backends{
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator, bool trivial = false>
struct cpp_int_base;
//
// Traits class determines the maximum and minimum precision values:
//
template <class T> struct max_precision;
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
{
static const unsigned value = is_void<Allocator>::value ?
static_unsigned_max<MinBits, MaxBits>::value
: (((MaxBits >= MinBits) && MaxBits) ? MaxBits : UINT_MAX);
};
template <class T> struct min_precision;
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct min_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
{
static const unsigned value = (is_void<Allocator>::value ? static_unsigned_max<MinBits, MaxBits>::value : MinBits);
};
//
// Traits class determines whether the number of bits precision requested could fit in a native type,
// we call this a "trivial" cpp_int:
//
template <class T>
struct is_trivial_cpp_int
{
static const bool value = false;
};
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct is_trivial_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
{
typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> self;
static const bool value = is_void<Allocator>::value && (max_precision<self>::value <= (sizeof(double_limb_type) * CHAR_BIT) - (SignType == signed_packed ? 1 : 0));
};
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct is_trivial_cpp_int<cpp_int_base<MinBits, MaxBits, SignType, Checked, Allocator, true> >
{
static const bool value = true;
};
} // namespace backends
//
// Traits class to determine whether a cpp_int_backend is signed or not:
//
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct is_unsigned_number<backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
: public mpl::bool_<(SignType == unsigned_magnitude) || (SignType == unsigned_packed)>{};
namespace backends{
//
// Traits class determines whether T should be implicitly convertible to U, or
// whether the constructor should be made explicit. The latter happens if we
// are losing the sign, or have fewer digits precision in the target type:
//
template <class T, class U>
struct is_implicit_cpp_int_conversion;
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>
struct is_implicit_cpp_int_conversion<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >
{
typedef cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> t1;
typedef cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> t2;
static const bool value =
(is_signed_number<t2>::value || !is_signed_number<t1>::value)
&& (max_precision<t1>::value <= max_precision<t2>::value);
};
//
// Traits class to determine whether operations on a cpp_int may throw:
//
template <class T>
struct is_non_throwing_cpp_int : public mpl::false_{};
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType>
struct is_non_throwing_cpp_int<cpp_int_backend<MinBits, MaxBits, SignType, unchecked, void> > : public mpl::true_ {};
//
// Traits class, determines whether the cpp_int is fixed precision or not:
//
template <class T>
struct is_fixed_precision;
template <unsigned MinBits, unsigned MaxBits, cpp_integer_type SignType, cpp_int_check_type Checked, class Allocator>
struct is_fixed_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >
: public mpl::bool_<max_precision<cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value != UINT_MAX> {};
namespace detail{
inline void verify_new_size(unsigned new_size, unsigned min_size, const mpl::int_<checked>&)
{
if(new_size < min_size)
BOOST_THROW_EXCEPTION(std::overflow_error("Unable to allocate sufficient storage for the value of the result: value overflows the maximum allowable magnitude."));
}
inline void verify_new_size(unsigned /*new_size*/, unsigned /*min_size*/, const mpl::int_<unchecked>&){}
template <class U>
inline void verify_limb_mask(bool b, U limb, U mask, const mpl::int_<checked>&)
{
// When we mask out "limb" with "mask", do we loose bits? If so it's an overflow error:
if(b && (limb & ~mask))
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."));
}
template <class U>
inline void verify_limb_mask(bool /*b*/, U /*limb*/, U /*mask*/, const mpl::int_<unchecked>&){}
}
//
// Now define the various data layouts that are possible as partial specializations of the base class,
// starting with the default arbitrary precision signed integer type:
//
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
struct cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false> : private Allocator::template rebind<limb_type>::other
{
typedef typename Allocator::template rebind<limb_type>::other allocator_type;
typedef typename allocator_type::pointer limb_pointer;
typedef typename allocator_type::const_pointer const_limb_pointer;
typedef mpl::int_<Checked> checked_type;
//
// Interface invariants:
//
BOOST_STATIC_ASSERT(!is_void<Allocator>::value);
private:
struct limb_data
{
unsigned capacity;
limb_pointer data;
};
public:
BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
BOOST_STATIC_CONSTANT(unsigned, internal_limb_count =
MinBits
? (MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0))
: (sizeof(limb_data) / sizeof(limb_type)));
BOOST_STATIC_CONSTANT(bool, variable = true);
private:
union data_type
{
limb_data ld;
limb_type la[internal_limb_count];
limb_type first;
double_limb_type double_first;
BOOST_CONSTEXPR data_type() : first(0) {}
BOOST_CONSTEXPR data_type(limb_type i) : first(i) {}
BOOST_CONSTEXPR data_type(signed_limb_type i) : first(i < 0 ? static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i) {}
#ifdef BOOST_LITTLE_ENDIAN
BOOST_CONSTEXPR data_type(double_limb_type i) : double_first(i) {}
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) {}
#endif
};
data_type m_data;
unsigned m_limbs;
bool m_sign, m_internal;
public:
//
// Direct construction:
//
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
: m_data(i), m_limbs(1), m_sign(false), m_internal(true) { }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT
: m_data(i), m_limbs(1), m_sign(i < 0), m_internal(true) { }
#if defined(BOOST_LITTLE_ENDIAN)
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
: m_data(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false), m_internal(true) { }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT
: 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)),
m_sign(i < 0), m_internal(true) { }
#endif
//
// Helper functions for getting at our internal data, and manipulating storage:
//
BOOST_MP_FORCEINLINE allocator_type& allocator() BOOST_NOEXCEPT { return *this; }
BOOST_MP_FORCEINLINE const allocator_type& allocator()const BOOST_NOEXCEPT { return *this; }
BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
BOOST_MP_FORCEINLINE const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_internal ? m_data.la : m_data.ld.data; }
BOOST_MP_FORCEINLINE unsigned capacity()const BOOST_NOEXCEPT { return m_internal ? internal_limb_count : m_data.ld.capacity; }
BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; }
void sign(bool b) BOOST_NOEXCEPT
{
m_sign = b;
// Check for zero value:
if(m_sign && (m_limbs == 1))
{
if(limbs()[0] == 0)
m_sign = false;
}
}
void resize(unsigned new_size, unsigned min_size)
{
static const unsigned max_limbs = MaxBits / (CHAR_BIT * sizeof(limb_type)) + ((MaxBits % (CHAR_BIT * sizeof(limb_type))) ? 1 : 0);
// We never resize beyond MaxSize:
if(new_size > max_limbs)
new_size = max_limbs;
detail::verify_new_size(new_size, min_size, checked_type());
// See if we have enough capacity already:
unsigned cap = capacity();
if(new_size > cap)
{
// Allocate a new buffer and copy everything over:
cap = (std::min)((std::max)(cap * 4, new_size), max_limbs);
limb_pointer pl = allocator().allocate(cap);
std::memcpy(pl, limbs(), size() * sizeof(limbs()[0]));
if(!m_internal)
allocator().deallocate(limbs(), capacity());
else
m_internal = false;
m_limbs = new_size;
m_data.ld.capacity = cap;
m_data.ld.data = pl;
}
else
{
m_limbs = new_size;
}
}
BOOST_MP_FORCEINLINE void normalize() BOOST_NOEXCEPT
{
limb_pointer p = limbs();
while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(), m_limbs(1), m_sign(false), m_internal(true) {}
BOOST_MP_FORCEINLINE cpp_int_base(const cpp_int_base& o) : allocator_type(o), m_limbs(0), m_internal(true)
{
resize(o.size(), o.size());
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
m_sign = o.m_sign;
}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
cpp_int_base(cpp_int_base&& o)
: allocator_type(static_cast<allocator_type&&>(o)), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal)
{
if(m_internal)
{
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
}
else
{
m_data.ld = o.m_data.ld;
o.m_limbs = 0;
o.m_internal = true;
}
}
cpp_int_base& operator = (cpp_int_base&& o) BOOST_NOEXCEPT
{
if(!m_internal)
allocator().deallocate(m_data.ld.data, m_data.ld.capacity);
*static_cast<allocator_type*>(this) = static_cast<allocator_type&&>(o);
m_limbs = o.m_limbs;
m_sign = o.m_sign;
m_internal = o.m_internal;
if(m_internal)
{
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
}
else
{
m_data.ld = o.m_data.ld;
o.m_limbs = 0;
o.m_internal = true;
}
return *this;
}
#endif
BOOST_MP_FORCEINLINE ~cpp_int_base() BOOST_NOEXCEPT
{
if(!m_internal)
allocator().deallocate(limbs(), capacity());
}
void assign(const cpp_int_base& o)
{
if(this != &o)
{
static_cast<allocator_type&>(*this) = static_cast<const allocator_type&>(o);
m_limbs = 0;
resize(o.size(), o.size());
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
m_sign = o.m_sign;
}
}
BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
{
m_sign = !m_sign;
// Check for zero value:
if(m_sign && (m_limbs == 1))
{
if(limbs()[0] == 0)
m_sign = false;
}
}
BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
{
return m_sign;
}
BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
{
std::swap(m_data, o.m_data);
std::swap(m_sign, o.m_sign);
std::swap(m_internal, o.m_internal);
std::swap(m_limbs, o.m_limbs);
}
protected:
template <class A>
void check_in_range(const A&) BOOST_NOEXCEPT {}
};
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::limb_bits;
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::max_limb_value;
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
const limb_type cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::sign_bit_mask;
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
const unsigned cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::internal_limb_count;
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
const bool cpp_int_base<MinBits, MaxBits, signed_magnitude, Checked, Allocator, false>::variable;
#endif
template <unsigned MinBits, unsigned MaxBits, cpp_int_check_type Checked, class Allocator>
struct cpp_int_base<MinBits, MaxBits, unsigned_magnitude, Checked, Allocator, false> : private Allocator::template rebind<limb_type>::other
{
//
// There is currently no support for unsigned arbitrary precision arithmetic, largely
// because it's not clear what subtraction should do:
//
BOOST_STATIC_ASSERT_MSG(((sizeof(Allocator) == 0) && !is_void<Allocator>::value), "There is curently no support for unsigned arbitrary precision integers.");
};
//
// Fixed precision (i.e. no allocator), signed-magnitude type with limb-usage count:
//
template <unsigned MinBits, cpp_int_check_type Checked>
struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>
{
typedef limb_type* limb_pointer;
typedef const limb_type* const_limb_pointer;
typedef mpl::int_<Checked> checked_type;
//
// Interface invariants:
//
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?");
public:
BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
BOOST_STATIC_CONSTANT(bool, variable = false);
BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0)));
BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
private:
union data_type{
limb_type m_data[internal_limb_count];
limb_type m_first_limb;
double_limb_type m_double_first_limb;
BOOST_CONSTEXPR data_type() : m_first_limb(0) {}
BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {}
BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {}
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
template <limb_type...VALUES>
BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{ VALUES... } {}
#endif
} m_wrapper;
boost::uint16_t m_limbs;
bool m_sign;
public:
//
// Direct construction:
//
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
: m_wrapper(i), m_limbs(1), m_sign(false) {}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_limb_type i)BOOST_NOEXCEPT
: 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) {}
#if defined(BOOST_LITTLE_ENDIAN)
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
: m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1), m_sign(false) {}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(signed_double_limb_type i)BOOST_NOEXCEPT
: m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
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)),
m_sign(i < 0) {}
#endif
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
template <limb_type...VALUES>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
: m_wrapper(i), m_limbs(sizeof...(VALUES)), m_sign(false) {}
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<> i)
: m_wrapper(i), m_limbs(1), m_sign(false) {}
BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)
: m_wrapper(a.m_wrapper), m_limbs(a.m_limbs), m_sign((a.m_limbs == 1) && (*a.limbs() == 0) ? false : !a.m_sign) {}
#endif
//
// Helper functions for getting at our internal data, and manipulating storage:
//
BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return m_sign; }
BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT
{
m_sign = b;
// Check for zero value:
if(m_sign && (m_limbs == 1))
{
if(limbs()[0] == 0)
m_sign = false;
}
}
BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
m_limbs = static_cast<boost::uint16_t>((std::min)(new_size, internal_limb_count));
detail::verify_new_size(m_limbs, min_size, checked_type());
}
BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
limb_pointer p = limbs();
detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type());
p[internal_limb_count-1] &= upper_limb_mask;
while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
if((m_limbs == 1) && (!*p)) m_sign = false; // zero is always unsigned
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base()BOOST_NOEXCEPT : m_wrapper(limb_type(0u)), m_limbs(1), m_sign(false) {}
// Not defaulted, it breaks constexpr support in the Intel compiler for some reason:
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o)BOOST_NOEXCEPT
: m_wrapper(o.m_wrapper), m_limbs(o.m_limbs), m_sign(o.m_sign) {}
// Defaulted functions:
//~cpp_int_base() BOOST_NOEXCEPT {}
void assign(const cpp_int_base& o) BOOST_NOEXCEPT
{
if(this != &o)
{
m_limbs = o.m_limbs;
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(o.limbs()[0]));
m_sign = o.m_sign;
}
}
BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
{
m_sign = !m_sign;
// Check for zero value:
if(m_sign && (m_limbs == 1))
{
if(limbs()[0] == 0)
m_sign = false;
}
}
BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
{
return m_sign;
}
BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
{
for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
std::swap(m_sign, o.m_sign);
std::swap(m_limbs, o.m_limbs);
}
protected:
template <class A>
void check_in_range(const A&) BOOST_NOEXCEPT {}
};
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
template <unsigned MinBits, cpp_int_check_type Checked>
const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::limb_bits;
template <unsigned MinBits, cpp_int_check_type Checked>
const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::max_limb_value;
template <unsigned MinBits, cpp_int_check_type Checked>
const limb_type cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::sign_bit_mask;
template <unsigned MinBits, cpp_int_check_type Checked>
const unsigned cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::internal_limb_count;
template <unsigned MinBits, cpp_int_check_type Checked>
const bool cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, false>::variable;
#endif
//
// Fixed precision (i.e. no allocator), unsigned type with limb-usage count:
//
template <unsigned MinBits, cpp_int_check_type Checked>
struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>
{
typedef limb_type* limb_pointer;
typedef const limb_type* const_limb_pointer;
typedef mpl::int_<Checked> checked_type;
//
// Interface invariants:
//
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?");
public:
BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(limb_type) * CHAR_BIT);
BOOST_STATIC_CONSTANT(limb_type, max_limb_value = ~static_cast<limb_type>(0u));
BOOST_STATIC_CONSTANT(limb_type, sign_bit_mask = static_cast<limb_type>(1u) << (limb_bits - 1));
BOOST_STATIC_CONSTANT(unsigned, internal_limb_count = MinBits / limb_bits + ((MinBits % limb_bits) ? 1 : 0));
BOOST_STATIC_CONSTANT(bool, variable = false);
BOOST_STATIC_CONSTANT(limb_type, upper_limb_mask = (MinBits % limb_bits) ? (limb_type(1) << (MinBits % limb_bits)) -1 : (~limb_type(0)));
BOOST_STATIC_ASSERT_MSG(internal_limb_count >= 2, "A fixed precision integer type must have at least 2 limbs");
private:
union data_type{
limb_type m_data[internal_limb_count];
limb_type m_first_limb;
double_limb_type m_double_first_limb;
BOOST_CONSTEXPR data_type() : m_first_limb(0) {}
BOOST_CONSTEXPR data_type(limb_type i) : m_first_limb(i) {}
BOOST_CONSTEXPR data_type(double_limb_type i) : m_double_first_limb(i) {}
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
template <limb_type...VALUES>
BOOST_CONSTEXPR data_type(literals::detail::value_pack<VALUES...>) : m_data{ VALUES... } {}
#endif
} m_wrapper;
limb_type m_limbs;
public:
//
// Direct construction:
//
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(limb_type i)BOOST_NOEXCEPT
: m_wrapper(i), m_limbs(1) {}
BOOST_MP_FORCEINLINE cpp_int_base(signed_limb_type i)BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
: 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(); }
#ifdef BOOST_LITTLE_ENDIAN
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(double_limb_type i)BOOST_NOEXCEPT
: m_wrapper(i), m_limbs(i > max_limb_value ? 2 : 1) {}
BOOST_MP_FORCEINLINE cpp_int_base(signed_double_limb_type i)BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
: m_wrapper(double_limb_type(i < 0 ? static_cast<double_limb_type>(boost::multiprecision::detail::unsigned_abs(i)) : i)),
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))
{
if (i < 0) negate();
}
#endif
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
template <limb_type...VALUES>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<VALUES...> i)
: m_wrapper(i), m_limbs(sizeof...(VALUES)) {}
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>)
: m_wrapper(static_cast<limb_type>(0u)), m_limbs(1) {}
#endif
//
// Helper functions for getting at our internal data, and manipulating storage:
//
BOOST_MP_FORCEINLINE unsigned size()const BOOST_NOEXCEPT { return m_limbs; }
BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return m_wrapper.m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return m_wrapper.m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; }
BOOST_MP_FORCEINLINE void sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked)) { if(b) negate(); }
BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
m_limbs = (std::min)(new_size, internal_limb_count);
detail::verify_new_size(m_limbs, min_size, checked_type());
}
BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
limb_pointer p = limbs();
detail::verify_limb_mask(m_limbs == internal_limb_count, p[internal_limb_count-1], upper_limb_mask, checked_type());
p[internal_limb_count-1] &= upper_limb_mask;
while((m_limbs-1) && !p[m_limbs - 1])--m_limbs;
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT
: m_wrapper(limb_type(0u)), m_limbs(1) {}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
: m_wrapper(o.m_wrapper), m_limbs(o.m_limbs) {}
// Defaulted functions:
//~cpp_int_base() BOOST_NOEXCEPT {}
BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
{
if(this != &o)
{
m_limbs = o.m_limbs;
std::memcpy(limbs(), o.limbs(), o.size() * sizeof(limbs()[0]));
}
}
private:
void check_negate(const mpl::int_<checked>&)
{
BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned number."));
}
void check_negate(const mpl::int_<unchecked>&){}
public:
void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
// Not so much a negate as a complement - this gets called when subtraction
// would result in a "negative" number:
unsigned i;
if((m_limbs == 1) && (m_wrapper.m_data[0] == 0))
return; // negating zero is always zero, and always OK.
check_negate(checked_type());
for(i = m_limbs; i < internal_limb_count; ++i)
m_wrapper.m_data[i] = 0;
m_limbs = internal_limb_count;
for(i = 0; i < internal_limb_count; ++i)
m_wrapper.m_data[i] = ~m_wrapper.m_data[i];
normalize();
eval_increment(static_cast<cpp_int_backend<MinBits, MinBits, unsigned_magnitude, Checked, void>& >(*this));
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT
{
return false;
}
BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
{
for(unsigned i = 0; i < (std::max)(size(), o.size()); ++i)
std::swap(m_wrapper.m_data[i], o.m_wrapper.m_data[i]);
std::swap(m_limbs, o.m_limbs);
}
protected:
template <class A>
void check_in_range(const A&) BOOST_NOEXCEPT {}
};
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
template <unsigned MinBits, cpp_int_check_type Checked>
const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::limb_bits;
template <unsigned MinBits, cpp_int_check_type Checked>
const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::max_limb_value;
template <unsigned MinBits, cpp_int_check_type Checked>
const limb_type cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::sign_bit_mask;
template <unsigned MinBits, cpp_int_check_type Checked>
const unsigned cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::internal_limb_count;
template <unsigned MinBits, cpp_int_check_type Checked>
const bool cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, false>::variable;
#endif
//
// Traits classes to figure out a native type with N bits, these vary from boost::uint_t<N> only
// because some platforms have native integer types longer than boost::long_long_type, "really boost::long_long_type" anyone??
//
template <unsigned N, bool s>
struct trivial_limb_type_imp
{
typedef double_limb_type type;
};
template <unsigned N>
struct trivial_limb_type_imp<N, true>
{
typedef typename boost::uint_t<N>::least type;
};
template <unsigned N>
struct trivial_limb_type : public trivial_limb_type_imp<N, N <= sizeof(boost::long_long_type) * CHAR_BIT> {};
//
// Backend for fixed precision signed-magnitude type which will fit entirely inside a "double_limb_type":
//
template <unsigned MinBits, cpp_int_check_type Checked>
struct cpp_int_base<MinBits, MinBits, signed_magnitude, Checked, void, true>
{
typedef typename trivial_limb_type<MinBits>::type local_limb_type;
typedef local_limb_type* limb_pointer;
typedef const local_limb_type* const_limb_pointer;
typedef mpl::int_<Checked> checked_type;
protected:
BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
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)));
private:
local_limb_type m_data;
bool m_sign;
//
// Interface invariants:
//
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?");
protected:
template <class T>
typename boost::disable_if_c<!boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
check_in_range(T val, const mpl::int_<checked>&)
{
typedef typename common_type<typename make_unsigned<T>::type, local_limb_type>::type common_type;
if(static_cast<common_type>(boost::multiprecision::detail::unsigned_abs(val)) > static_cast<common_type>(limb_mask))
BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
}
template <class T>
typename boost::disable_if_c<boost::is_integral<T>::value || (std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits))>::type
check_in_range(T val, const mpl::int_<checked>&)
{
using std::abs;
typedef typename common_type<T, local_limb_type>::type common_type;
if (static_cast<common_type>(abs(val)) > static_cast<common_type>(limb_mask))
BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
}
template <class T, int C>
void check_in_range(T, const mpl::int_<C>&) BOOST_NOEXCEPT {}
template <class T>
void check_in_range(T val) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<cpp_int_base>().check_in_range(std::declval<T>(), checked_type())))
{
check_in_range(val, checked_type());
}
public:
//
// Direct construction:
//
template <class SI>
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>())))
: 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) {}
template <class SI>
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>())))
: 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)
{ check_in_range(i); }
template <class UI>
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
: m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) {}
template <class UI>
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>())))
: m_data(static_cast<local_limb_type>(i) & limb_mask), m_sign(false) { check_in_range(i); }
template <class F>
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
: m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) {}
template <class F>
BOOST_MP_FORCEINLINE cpp_int_base(F i, typename boost::enable_if_c<is_floating_point<F>::value && (Checked == checked)>::type const* = 0)
: m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask), m_sign(i < 0) { check_in_range(i); }
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(0u)), m_sign(false) {}
template <limb_type a>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>)BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(a)), m_sign(false) {}
template <limb_type a, limb_type b>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>)BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)), m_sign(false) {}
BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& a, const literals::detail::negate_tag&)BOOST_NOEXCEPT
: m_data(a.m_data), m_sign(a.m_data ? !a.m_sign : false) {}
#endif
//
// Helper functions for getting at our internal data, and manipulating storage:
//
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; }
BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; }
BOOST_MP_FORCEINLINE bool sign()const BOOST_NOEXCEPT { return m_sign; }
BOOST_MP_FORCEINLINE void sign(bool b) BOOST_NOEXCEPT
{
m_sign = b;
// Check for zero value:
if(m_sign && !m_data)
{
m_sign = false;
}
}
BOOST_MP_FORCEINLINE void resize(unsigned new_size, unsigned min_size)
{
detail::verify_new_size(2, min_size, checked_type());
}
BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
if(!m_data)
m_sign = false; // zero is always unsigned
detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
m_data &= limb_mask;
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0), m_sign(false) {}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
: m_data(o.m_data), m_sign(o.m_sign) {}
//~cpp_int_base() BOOST_NOEXCEPT {}
BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
{
m_data = o.m_data;
m_sign = o.m_sign;
}
BOOST_MP_FORCEINLINE void negate() BOOST_NOEXCEPT
{
m_sign = !m_sign;
// Check for zero value:
if(m_data == 0)
{
m_sign = false;
}
}
BOOST_MP_FORCEINLINE bool isneg()const BOOST_NOEXCEPT
{
return m_sign;
}
BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
{
std::swap(m_sign, o.m_sign);
std::swap(m_data, o.m_data);
}
};
//
// Backend for unsigned fixed precision (i.e. no allocator) type which will fit entirely inside a "double_limb_type":
//
template <unsigned MinBits, cpp_int_check_type Checked>
struct cpp_int_base<MinBits, MinBits, unsigned_magnitude, Checked, void, true>
{
typedef typename trivial_limb_type<MinBits>::type local_limb_type;
typedef local_limb_type* limb_pointer;
typedef const local_limb_type* const_limb_pointer;
private:
BOOST_STATIC_CONSTANT(unsigned, limb_bits = sizeof(local_limb_type) * CHAR_BIT);
BOOST_STATIC_CONSTANT(local_limb_type, limb_mask = limb_bits != MinBits ?
static_cast<local_limb_type>(static_cast<local_limb_type>(~local_limb_type(0)) >> (limb_bits - MinBits))
: static_cast<local_limb_type>(~local_limb_type(0)));
local_limb_type m_data;
typedef mpl::int_<Checked> checked_type;
//
// Interface invariants:
//
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?");
protected:
template <class T>
typename boost::disable_if_c<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::digits <= (int)MinBits)>::type
check_in_range(T val, const mpl::int_<checked>&, const boost::false_type&)
{
typedef typename common_type<T, local_limb_type>::type common_type;
if(static_cast<common_type>(val) > limb_mask)
BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
}
template <class T>
void check_in_range(T val, const mpl::int_<checked>&, const boost::true_type&)
{
typedef typename common_type<T, local_limb_type>::type common_type;
if(static_cast<common_type>(val) > limb_mask)
BOOST_THROW_EXCEPTION(std::range_error("The argument to a cpp_int constructor exceeded the largest value it can represent."));
if(val < 0)
BOOST_THROW_EXCEPTION(std::range_error("The argument to an unsigned cpp_int constructor was negative."));
}
template <class T, int C, bool B>
BOOST_MP_FORCEINLINE void check_in_range(T, const mpl::int_<C>&, const boost::integral_constant<bool, B>&) BOOST_NOEXCEPT {}
template <class T>
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>())))
{
check_in_range(val, checked_type(), is_signed<T>());
}
public:
//
// Direct construction:
//
template <class SI>
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
: m_data(i < 0 ? (1 + ~static_cast<local_limb_type>(-i)) & limb_mask : static_cast<local_limb_type>(i) & limb_mask) {}
template <class SI>
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>())))
: m_data(i < 0 ? 1 + ~static_cast<local_limb_type>(-i) : static_cast<local_limb_type>(i)) { check_in_range(i); }
template <class UI>
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
: m_data(static_cast<local_limb_type>(i) & limb_mask) {}
template <class UI>
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>())))
: m_data(static_cast<local_limb_type>(i)) { check_in_range(i); }
template <class F>
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))
: m_data(static_cast<local_limb_type>(std::fabs(i)) & limb_mask)
{
check_in_range(i);
if(i < 0)
negate();
}
#if defined(BOOST_MP_USER_DEFINED_LITERALS)
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<>) BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(0u)) {}
template <limb_type a>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a>) BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(a)) {}
template <limb_type a, limb_type b>
BOOST_CONSTEXPR cpp_int_base(literals::detail::value_pack<a, b>) BOOST_NOEXCEPT
: m_data(static_cast<local_limb_type>(a) | (static_cast<local_limb_type>(b) << bits_per_limb)) {}
#endif
//
// Helper functions for getting at our internal data, and manipulating storage:
//
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR unsigned size()const BOOST_NOEXCEPT { return 1; }
BOOST_MP_FORCEINLINE limb_pointer limbs() BOOST_NOEXCEPT { return &m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const_limb_pointer limbs()const BOOST_NOEXCEPT { return &m_data; }
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool sign()const BOOST_NOEXCEPT { return false; }
BOOST_MP_FORCEINLINE void sign(bool b) BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
if(b)
negate();
}
BOOST_MP_FORCEINLINE void resize(unsigned, unsigned min_size)
{
detail::verify_new_size(2, min_size, checked_type());
}
BOOST_MP_FORCEINLINE void normalize() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
detail::verify_limb_mask(true, m_data, limb_mask, checked_type());
m_data &= limb_mask;
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base() BOOST_NOEXCEPT : m_data(0) {}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR cpp_int_base(const cpp_int_base& o) BOOST_NOEXCEPT
: m_data(o.m_data) {}
//~cpp_int_base() BOOST_NOEXCEPT {}
BOOST_MP_FORCEINLINE void assign(const cpp_int_base& o) BOOST_NOEXCEPT
{
m_data = o.m_data;
}
BOOST_MP_FORCEINLINE void negate() BOOST_MP_NOEXCEPT_IF((Checked == unchecked))
{
if(Checked == checked)
{
BOOST_THROW_EXCEPTION(std::range_error("Attempt to negate an unsigned type."));
}
m_data = ~m_data;
++m_data;
}
BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool isneg()const BOOST_NOEXCEPT
{
return false;
}
BOOST_MP_FORCEINLINE void do_swap(cpp_int_base& o) BOOST_NOEXCEPT
{
std::swap(m_data, o.m_data);
}
};
//
// Traits class, lets us know whether type T can be directly converted to the base type,
// used to enable/disable constructors etc:
//
template <class Arg, class Base>
struct is_allowed_cpp_int_base_conversion : public mpl::if_c<