-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdig.hpp
2096 lines (1982 loc) · 72.3 KB
/
bdig.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
/*
Template class of big numbers with fixed point arithmetic for
simplifying usage of big numbers. Usage from C++98.
Author: Andrey Svyatovets
*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#include <regex>
#endif
namespace sag {
const char* pPI_1000 = "3.14159265358979323846264338327950288419716939937510"
"58209749445923078164062862089986280348253421170679"
"82148086513282306647093844609550582231725359408128"
"48111745028410270193852110555964462294895493038196"
"44288109756659334461284756482337867831652712019091"
"45648566923460348610454326648213393607260249141273"
"72458700660631558817488152092096282925409171536436"
"78925903600113305305488204665213841469519415116094"
"33057270365759591953092186117381932611793105118548"
"07446237996274956735188575272489122793818301194912"
"98336733624406566430860213949463952247371907021798"
"60943702770539217176293176752384674818467669405132"
"00056812714526356082778577134275778960917363717872"
"14684409012249534301465495853710507922796892589235"
"42019956112129021960864034418159813629774771309960"
"51870721134999999837297804995105973173281609631859"
"50244594553469083026425223082533446850352619311881"
"71010003137838752886587533208381420617177669147303"
"59825349042875546873115956286388235378759375195778"
"18577805321712268066130019278766111959092164201989";
const char* pE_1000 = "2.71828182845904523536028747135266249775724709369995"
"95749669676277240766303535475945713821785251664274"
"27466391932003059921817413596629043572900334295260"
"59563073813232862794349076323382988075319525101901"
"15738341879307021540891499348841675092447614606680"
"82264800168477411853742345442437107539077744992069"
"55170276183860626133138458300075204493382656029760"
"67371132007093287091274437470472306969772093101416"
"92836819025515108657463772111252389784425056953696"
"77078544996996794686445490598793163688923009879312"
"77361782154249992295763514822082698951936680331825"
"28869398496465105820939239829488793320362509443117"
"30123819706841614039701983767932068328237646480429"
"53118023287825098194558153017567173613320698112509"
"96181881593041690351598888519345807273866738589422"
"87922849989208680582574927961048419844436346324496"
"84875602336248270419786232090021609902353043699418"
"49146314093431738143640546253152096183690888707016"
"76839642437814059271456354906130310720851038375051"
"01157477041718986106873969655212671546889570350354";
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#ifndef __SIZEOF_INT128__
using uint128_t = unsigned long long;
#else
using uint128_t = unsigned __int128;
#endif
using namespace std;
#define QUOTE(x) #x
#define STR(x) QUOTE(x)
#define PREC_FOR_MATH_FUNC 5
#define PREC_FOR_MATH_FUNC_TEXT \
"The precision should be more then " STR(PREC_FOR_MATH_FUNC) " digits"
#define SIZE_FOR_MATH_FUNC 300
#define SIZE_FOR_MATH_FUNC_TEXT "The size should be more then " STR(SIZE_FOR_MATH_FUNC) " digits"
#define FOR_INTEGER_ONLY_TEXT "This function only for integer digits without precision"
#else // C++98
template<bool B, class T = void>
struct enable_if {
};
template<class T>
struct enable_if<true, T> {
typedef T type;
};
struct true_type {
enum { value = true };
};
struct false_type {
enum { value = false };
};
template<class T, class U>
struct is_same : false_type {
};
template<class T>
struct is_same<T, T> : true_type {
};
template<bool B, class T, class F>
struct conditional {
typedef T type;
};
template<class T, class F>
struct conditional<false, T, F> {
typedef F type;
};
#endif
template<typename T>
struct is_bdig {
template<typename U>
static char Test(typename U::bdig*);
template<typename U>
static int Test(...);
static const bool value = (sizeof(Test<T>(0)) == sizeof(char));
};
/* Allocation objects in heap for functions with deep recursion */
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#define DECLOBJ(name) std::unique_ptr<bdig> name
#define ASSIGNOBJ(name, param) name.reset(new bdig(param)) // name = std::make_unique<bdig>(param)
#define CRTHEAPOBJ(name, param) \
DECLOBJ(name) \
= std::unique_ptr<bdig>(new bdig(param)) // DECLOBJ(name) = std::make_unique<bdig>(param)
#define CRTHEAPOBJNP(name) CRTHEAPOBJ(name, )
#else
#define DECLOBJ(name) std::auto_ptr<bdig> name
#define ASSIGNOBJ(name, param) name.reset(new bdig(param))
#define CRTHEAPOBJ(name, param) std::auto_ptr<bdig> name(new bdig(param))
#define CRTHEAPOBJNP(name) std::auto_ptr<bdig> name(new bdig())
#endif
/// @brief This template class cover manipulation with big numbers. Number size up to indicated in parameters.
/// @tparam T Type of element in array to hold number. This type can be unsigned integer type from char to long long and specific to compiler int128_t
/// @tparam digits Number of digits in integer part. Depends on system resources and can be as big as it required. Tested values up to 20'000
/// @tparam prec Number of digits after point.
template<const int digits = 100, const int prec = 0, class T = unsigned char>
class bdig {
static const typename enable_if<!std::numeric_limits<T>::is_signed, std::size_t>::type isz
= ((prec * 2 + digits) / (std::numeric_limits<T>::digits * 301 / 1000)) + 1; //!< Size of required buffer size of bdig value
/// @brief Internal buffer for holding value of bdig type
/// @tparam Tb Type of element of array of buffer
/// @tparam size of the buffer array
template<class Tb, std::size_t size = isz>
class buffer {
Tb _buffer[size]; //!< Array of holding bdig value
std::size_t least_significant_index; //!< Most index of element with value in array.
public:
/// @brief Default constructor of Buffer class
buffer() {
clear();
}
/// @brief Clear buffer and reset buffer data
void clear() {
memset(_buffer, 0, size * sizeof(Tb));
least_significant_index = size - 1;
}
/// @brief Subscript operator of Buffer type
/// @param idx index to element in buffer
/// @return reference to element value in array
const T& operator[](std::size_t idx) const {
return _buffer[idx];
}
/// @brief method for setting value to array of bdig value
/// @param idx index to element in buffer
/// @param _v value to set
inline void set(std::size_t idx, const Tb _v) {
if (idx < size) {
_buffer[idx] = _v;
if (idx < least_significant_index && _v)
least_significant_index = idx;
else if (idx == least_significant_index && !_v) {
while (!_buffer[least_significant_index] && least_significant_index < size - 1)
least_significant_index++;
}
}
}
/// @brief getter of most index of element with value in array.
/// @return index value
inline std::size_t get_lsi() const {
return least_significant_index;
}
};
buffer<T> integer; //!< Buffer of value.
bool is_negative; //!< holer of negative flag.
static bdig i10; //!< value of precision divider.
const static T mask_max_bit = static_cast<T>(1) << (std::numeric_limits<T>::digits - 1); //!< mask of maximum bit for element type.
/// @brief Shift buffer to left on elements counter
/// @param bytes Counter of elements to shift. If number of bytes more then buffer size then bdig value will be cleared.
void shlb(std::size_t bytes)
{
if (!bytes)
return;
if (bytes >= isz) {
integer.clear();
return;
}
std::size_t idx_src = integer.get_lsi();
std::size_t idx_dst = integer.get_lsi() - bytes;
std::size_t move_size = isz - integer.get_lsi();
for (std::size_t i = 0; i < move_size; i++, idx_dst++, idx_src++)
integer.set(idx_dst, integer[idx_src]);
for (std::size_t i = 0, idx_set = (isz - bytes); i < bytes; i++, idx_set++)
integer.set(idx_set, 0);
}
/// @brief Shift to left one element in digit buffer.
/// @param idx Index of element in the buffer
/// @param bits Number of bits to shift the buffer element.
/// @param set_bit Bitmask from previous shifted element for fill of freed part.
/// @return Bitmask for next element
inline T shl_sop(std::size_t idx, const int bits, const T set_bit)
{
if (!integer[idx] && !set_bit)
return 0;
T bit = integer[idx] >> (std::numeric_limits<T>::digits - bits);
integer.set(idx, integer[idx] << bits);
if (set_bit)
integer.set(idx, integer[idx] | set_bit);
return bit;
}
/// @brief Shift buffer to left on bits counter less then element size in bits.
/// @param bits Counter of bits to shift
void shl(const int bits = 1)
{
if (!bits)
return;
std::size_t msi = most_significant_index();
msi = msi == 0 ? 0 : msi - 1;
T set_bit = 0;
for (std::size_t i = isz - 1; i >= msi; i--) {
set_bit = shl_sop(i, bits, set_bit);
if (!i)
break;
}
}
/// @brief Shift buffer to right on elements counter
/// @param bytes Number of elements to shift
void shrb(std::size_t bytes)
{
for (std::size_t i = 0; i < isz - bytes; i++)
integer.set(isz - i - 1, integer[isz - bytes - i - 1]);
for (std::size_t i = 0; i < bytes; i++)
integer.set(i, 0);
}
/// @brief Shift to right one element in digit buffer.
/// @param idx Index of element in the buffer
/// @param bits Number of bits to shift the buffer element.
/// @param set_bit Bitmask from previous shifted element for fill of freed part.
/// @return Bitmask for next element
inline T shr_sop(std::size_t idx, const int bits, const T set_bit)
{
if (!integer[idx] && !set_bit)
return 0;
T bit = integer[idx] << (std::numeric_limits<T>::digits - bits);
integer.set(idx, integer[idx] >> bits);
if (set_bit)
integer.set(idx, integer[idx] | set_bit);
return bit;
}
/// @brief Shift buffer to right on bits counter less then element size in bits.
/// @param bits Counter of bits to shift
void shr(const unsigned bits = 1)
{
T set_bit = 0;
std::size_t msi = most_significant_index();
for (std::size_t i = msi; i < isz; i++) {
set_bit = shr_sop(i, bits, set_bit);
}
}
inline std::size_t most_significant_index() const {
return integer.get_lsi();
}
/// @brief Find most significant bit in value
/// @tparam Ti Type of value
/// @param value parameter where to find most significant bit
/// @param dummy parameter for template substitution
/// @return number of most significant bit
template<class Ti>
int most_significant_bit(
Ti value,
typename enable_if<
is_same<Ti, unsigned short>::value || is_same<Ti, unsigned long>::value
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
|| is_same<Ti, unsigned int>::value || is_same<Ti, unsigned long long>::value
|| is_same<Ti, uint128_t>::value
#endif
,
Ti>::type
= 0) const
{
typedef typename conditional<
is_same<Ti, unsigned short>::value,
unsigned char,
typename conditional<
is_same<Ti, unsigned long>::value,
unsigned short,
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
typename conditional<
is_same_v<Ti, unsigned int>,
unsigned short,
typename conditional<
is_same_v<Ti, unsigned long long>,
typename conditional<
numeric_limits<Ti>::digits == numeric_limits<unsigned long>::digits,
unsigned int,
unsigned long>::type,
typename conditional<
is_same_v<Ti, uint128_t>,
unsigned long long,
#endif
void>::type
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
>::type>::type>::type
#endif
>::type subT;
if (value & (Ti)std::numeric_limits<subT>::max() << std::numeric_limits<subT>::digits)
return most_significant_bit((subT)(value >> std::numeric_limits<subT>::digits))
+ std::numeric_limits<subT>::digits;
return most_significant_bit((subT)value);
}
/// @brief Overloaded method for finding most significant bit in type unsigned char.
/// @param value unsigned char value
/// @return number of most significant bit
int most_significant_bit(unsigned char value) const
{
if (value & 0xF0) {
if (value & 0xC0)
return (value & 0x80) ? 7 : 6;
return (value & 0x20) ? 5 : 4;
}
if (value & 0x0C)
return (value & 0x08) ? 3 : 2;
return (value & 0x02) ? 1 : 0;
}
/// @brief Division method for bdig type value by elements in buffer array.
/// @param v value of right side operand of division
/// @param premainder pointer to remainder after division. If NULL then id not filled.
/// @return reference to himself
bdig& div(const bdig& v, bdig* premainder = NULL)
{
if (!v)
return *this;
CRTHEAPOBJNP(remainder);
CRTHEAPOBJNP(remainder_tmp);
CRTHEAPOBJNP(borrow);
CRTHEAPOBJ(divisor, v);
CRTHEAPOBJ(result, 0);
bool store_is_negative = is_negative;
is_negative = false;
*remainder = *this;
integer.clear();
*divisor = (*divisor).abs();
// Shift Divider to Value max significant index
const std::size_t ri = (*remainder).most_significant_index();
const std::size_t di = (*divisor).most_significant_index();
std::size_t shift = 0;
if (ri < di) {
shift = di - ri;
(*divisor).shlb(shift);
}
// If Divider > Value Shit Divider back to one position(In current sample is not needeed)
if ((*divisor) > (*remainder) && shift) {
(*divisor).shrb(1);
shift--;
}
// Both items have value only in one first limb
std::size_t index = isz - 1;
if (ri == index && di == index) {
(*result).integer.set(index, (*remainder).integer[index] / (*divisor).integer[index]);
(*remainder).integer.set(index, (*remainder).integer[index] % (*divisor).integer[index]);
} else {
const upT BaseValue = static_cast<upT>(std::numeric_limits<T>::max()) + 1;
while ((*divisor) <= (*remainder)) {
upT result_value = 0;
upT last_diff = 0;
const std::size_t result_index = (*divisor).most_significant_index();
const std::size_t remainder_index = (*remainder).most_significant_index();
std::size_t current_index = result_index;
while (current_index < isz) {
if (result_index == current_index) {
result_value = (*remainder).integer[current_index];
if (remainder_index < result_index) {
result_value += (*remainder).integer[current_index - 1] * BaseValue;
}
result_value -= (*borrow).integer[current_index];
upT remainder_value = result_value;
result_value /= (*divisor).integer[current_index];
if (result_value >= BaseValue)
result_value = BaseValue - 1;
remainder_value -= (*divisor).integer[current_index] * result_value;
(*remainder_tmp).integer.set(current_index, static_cast<T>(remainder_value));
current_index++;
} else {
upT need_value = (*divisor).integer[current_index] * result_value;
upT borrow_value = 0;
if (need_value > (*remainder).integer[current_index]) {
upT full_value = need_value + (*borrow).integer[current_index] - (*remainder).integer[current_index];
borrow_value = full_value / BaseValue + ((full_value % BaseValue) ? 1 : 0);
}
upT diff = borrow_value < (*borrow).integer[current_index - 1] ?
(*borrow).integer[current_index - 1] - borrow_value :
borrow_value - (*borrow).integer[current_index - 1];
if (diff && diff == last_diff) {
if ((*borrow).integer[current_index - 1] != borrow_value) {
(*borrow).integer.set(current_index - 1, static_cast<T>(borrow_value));
}
(*remainder).div_bits((*divisor), remainder_tmp.get());
result_value = (*remainder).integer[isz - 1];
break;
}
last_diff = diff;
if (borrow_value != (*borrow).integer[current_index - 1]) {
(*borrow).integer.set(current_index - 1, static_cast<T>(borrow_value));
current_index--;
} else {
upT remainder_value = (*borrow).integer[current_index - 1] * BaseValue
+ (*remainder).integer[current_index]
- (*borrow).integer[current_index]
- (*divisor).integer[current_index] * result_value;
if (remainder_value >= BaseValue) {
(*remainder_tmp).integer.set(current_index - 1, (*remainder_tmp).integer[current_index - 1] + 1);
}
(*remainder_tmp).integer.set(current_index, static_cast<T>(remainder_value));
current_index++;
}
}
}
(*result).integer.set(isz - 1 - shift, static_cast<T>(result_value));
(*remainder) = (*remainder_tmp);
(*remainder_tmp) = 0;
(*borrow) = 0;
if (!shift)
break;
shift--;
(*divisor).shrb(1);
}
}
is_negative = store_is_negative != v.is_negative;
(*remainder).is_negative = store_is_negative != v.is_negative;
if (premainder)
*premainder = *remainder;
(*result).is_negative = store_is_negative != v.is_negative;
if (premainder != this)
*this = *result;
return *this;
}
/// @brief Method for subtraction of v element from this element by index.
/// @param idx index in buffer array.
/// @param v value for subtraction
/// @param c carry flag
/// @return new carry flag
inline bool sub_sop(std::size_t idx, const bdig& v, const bool c)
{
if (!v.integer[idx] && !c)
return c;
bool c2 = false;
if (c) {
if (integer[idx] == 0)
c2 = true;
integer.set(idx, integer[idx] - 1);
}
bool cr = (integer[idx] < v.integer[idx]) || c2;
integer.set(idx, integer[idx] - v.integer[idx]);
return cr;
}
/// @brief Classic division method for bdig type value by bits.
/// @param v value of right side operand of division
/// @param premainder pointer to remainder after division. If NULL then id not filled.
/// @return reference to himself
bdig& div_bits(const bdig& v, bdig* premainder = NULL)
{
if (!v)
return *this;
CRTHEAPOBJNP(remainder);
CRTHEAPOBJ(divisor, v);
bool store_is_negative = is_negative;
is_negative = false;
*remainder = *this;
integer.clear();
CRTHEAPOBJNP(shift);
(*shift).integer.set(isz - 1, 1);
*divisor = (*divisor).abs();
// Align divisor with remainder in array
std::size_t ri = (*remainder).most_significant_index();
std::size_t di = (*divisor).most_significant_index();
if (ri < di) {
(*divisor).shlb(di - ri);
(*shift).shlb(di - ri);
}
while (*remainder >= v) {
while (!(*remainder).integer[ri] && ri < isz) {
if ((*divisor).integer[ri]) {
int d_msb = most_significant_bit((*divisor).integer[ri]);
(*divisor).shr(d_msb + 1);
(*shift).shr(d_msb + 1);
}
ri++;
}
int d_msb = most_significant_bit((*divisor).integer[ri]);
int r_msb = most_significant_bit((*remainder).integer[ri]);
int bits = r_msb - d_msb;
std::size_t msi_divisor = (*divisor).most_significant_index();
std::size_t msi_shift = (*shift).most_significant_index();
std::size_t msi = std::min(msi_shift, msi_divisor);
msi -= msi ? 1 : 0;
if (bits) {
T set_bit = 0, set_bit1 = 0;
if (bits > 0) {
for (std::size_t i = isz - 1; i >= msi; i--) {
set_bit = (*divisor).shl_sop(i, bits, set_bit);
set_bit1 = (*shift).shl_sop(i, bits, set_bit1);
if (!i)
break;
}
} else {
for (std::size_t i = msi; i < isz; ++i) {
set_bit = (*divisor).shr_sop(i, -bits, set_bit);
set_bit1 = (*shift).shr_sop(i, -bits, set_bit1);
}
}
}
while ((*divisor) > (*remainder)) {
T set_bit = 0, set_bit1 = 0;
for (std::size_t i = msi; i < isz; i++) {
set_bit = (*divisor).shr_sop(i, 1, set_bit);
set_bit1 = (*shift).shr_sop(i, 1, set_bit1);
}
}
std::size_t msi_quotient = most_significant_index();
std::size_t msi_remainder = (*remainder).most_significant_index();
msi = std::min(msi_remainder, msi_quotient);
msi -= msi ? 1 : 0;
bool cs = false, csm = false;
for (std::size_t i = isz - 1; i >= msi; --i) {
csm = sum_sop(i, *shift, csm);
cs = (*remainder).sub_sop(i, *divisor, cs);
if (!i)
break;
}
if (!*divisor)
break;
}
is_negative = store_is_negative != v.is_negative;
(*remainder).is_negative = store_is_negative != v.is_negative;
if (premainder)
*premainder = *remainder;
return *this;
}
/// @brief Method to prepare and update i10 static value of precision divider.
/// @return i10 static value of precision divider
bdig& prec_value()
{
if (!i10) {
i10.integer.set(isz - 1, (prec ? 10 : 1));
i10 = i10.pow(prec, false);
}
return i10;
}
/// @brief Make bdig value integer by multiply to prec_value i10.
/// @return reference to himself
bdig& prec_up()
{
if (prec) {
return mul<T>(prec_value());
}
return *this;
}
/// @brief Make bdig value with digits after point by dividing to prec_value i10.
/// @return reference to himself
bdig& prec_down()
{
if (prec)
return div(prec_value());
return *this;
}
/// @brief Found type bigger as type of buffer array element
typedef typename conditional<
is_same<T, unsigned char>::value
&& std::numeric_limits<unsigned short>::digits / std::numeric_limits<T>::digits == 2,
unsigned short,
typename conditional<
is_same<T, unsigned short>::value
&& std::numeric_limits<unsigned int>::digits / std::numeric_limits<T>::digits == 2,
unsigned int,
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
typename conditional<is_same<T, unsigned int>::value && std::numeric_limits<unsigned long>::digits / std::numeric_limits<T>::digits == 2,
unsigned long,
#ifndef __SIZEOF_INT128__
unsigned long long>::type
#else
typename conditional<is_same<T, unsigned long>::value && std::numeric_limits<unsigned long long>::digits / std::numeric_limits<T>::digits == 2,
unsigned long long,
uint128_t>::type>::type
#endif
#else
unsigned long>::type
#endif
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
>::type
#endif
>::type upT;
/// @brief Static method for add value to buffer element by index
/// @param index index of element to add value
/// @param result bdig object to modify value
/// @param value value to add
static void add(std::size_t index, bdig& result, T value)
{
upT x = result.integer[index];
upT y = value;
upT d = x + y;
// upT d = result.integer[index] + value;
T r1 = d >> std::numeric_limits<T>::digits;
T r0 = d & std::numeric_limits<T>::max();
if (r1) // overflow
{
// add high part to high order limb
add(index - 1, result, r1);
}
// Set low order part to current limb
result.integer.set(index, r0);
}
/// @brief Template of multiplication method for types with can have the bigger type.
/// @tparam Ti type to check and work with.
/// @param v right side value to multiply
/// @param dummy parameter for method substitution
/// @return reference to himself with result
template<class Ti>
bdig& mul(
const bdig& v,
typename enable_if<
is_same<Ti, unsigned char>::value || is_same<Ti, unsigned short>::value
|| (is_same<Ti, unsigned int>::value
&& std::numeric_limits<Ti>::digits != std::numeric_limits<unsigned long>::digits
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
&& std::numeric_limits<Ti>::digits
!= std::numeric_limits<unsigned long long>::digits
#endif
),
Ti>::type
= 0)
{
bdig result;
std::size_t lsi = v.most_significant_index();
for (std::size_t v_i = isz - 1; v_i >= lsi; v_i--) {
if (v.integer[v_i]) {
for (std::size_t idx = isz - 1; idx >= most_significant_index(); idx--) {
if (integer[idx]) {
upT d = static_cast<upT>(v.integer[v_i]) * static_cast<upT>(integer[idx]);
Ti r1 = d >> std::numeric_limits<Ti>::digits;
Ti r0 = d & std::numeric_limits<Ti>::max();
const std::size_t set_index = idx - (isz - v_i);
add(set_index, result, r1);
add(set_index + 1, result, r0);
}
if (!idx)
break;
}
}
if (!v_i)
break;
}
bool store_is_negative = is_negative;
bool store_is_negative_v = v.is_negative;
*this = result;
is_negative = store_is_negative != store_is_negative_v;
return *this;
}
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
/// @brief Template of multiplication method for types which does not have the bigger type. Multiplication with bitwise method
/// @tparam Ti type to check and work with.
/// @param v right side value to multiply
/// @param dummy parameter for method substitution
/// @return reference to himself with result
template<class Ti>
bdig& mul(
const bdig& v,
typename enable_if<
is_same<Ti, unsigned long long>::value || is_same<Ti, uint128_t>::value
|| (is_same<Ti, unsigned int>::value
&& std::numeric_limits<Ti>::digits
== std::numeric_limits<unsigned long>::digits),
Ti>::type
= 0)
{
CRTHEAPOBJ(multiplicant, v);
*multiplicant = (*multiplicant).abs();
CRTHEAPOBJ(sum, *this);
if (sum->isnegative())
*sum = -(*sum);
bool store_is_negative = is_negative;
if (is_negative)
is_negative = false;
integer.clear();
std::size_t shift = 0;
for (std::size_t i = (*multiplicant).most_significant_index(); i < isz; i++) {
for (int b = 0; b < std::numeric_limits<T>::digits; b++) {
if ((*multiplicant).integer[isz - 1] & (Ti)1) {
(*sum).shlb(shift / std::numeric_limits<Ti>::digits);
shift %= std::numeric_limits<T>::digits;
Ti set_bit = 0;
bool c = false;
std::size_t msi_sum = (*sum).most_significant_index();
std::size_t msi = most_significant_index();
msi = std::min(msi, msi_sum);
msi -= msi ? 1 : 0;
for (std::size_t j = isz - 1; j >= msi; j--) {
if (shift)
set_bit = (*sum).shl_sop(j, shift, set_bit);
c = sum_sop(j, *sum, c);
if (!j)
break;
}
shift = 0;
}
(*multiplicant).integer.set(isz - 1, (*multiplicant).integer[isz - 1] >> 1);
shift += 1;
}
(*multiplicant).shrb(1);
}
if (&v == this) {
is_negative = store_is_negative;
}
is_negative = store_is_negative != v.is_negative;
return *this;
}
#endif
/// @brief Method for comparison of two bdig values.
/// @param v1 left bdig value
/// @param v2 right bdig value
/// @param r1 flag of left bdig value check
/// @param r2 flag of right bdig value check
/// @return Combination r1 - true, r2 false
/// result true - v1 < v2, false v1 > v2.
/// Combination r1 - false, r2 true
/// result true - v1 > v2, false v1 < v2.
/// Combination r1 - false, r2 false
/// result true - v1 == v2, false v1 != v2.
/// Combination r1 - true, r2 true is equivalent to r1 - true, r2 false
static bool _Cmp(const bdig& v1, const bdig& v2, bool r1, bool r2)
{
std::size_t lsi1 = v1.integer.get_lsi();
std::size_t lsi2 = v2.integer.get_lsi();
int res = 0;
bool zero = true;
if (lsi1 == lsi2) {
for (std::size_t i = lsi1; i < isz; ++i) {
res = (int)(v1.integer[i] < v2.integer[i]
? -1
: v1.integer[i] == v2.integer[i] ? 0 : 1);
if (v1.integer[i] || v2.integer[i])
zero = false;
if (res)
break;
}
} else {
res = lsi2 < lsi1 ? -1 : 1;
zero = false;
}
if (v1.is_negative != v2.is_negative)
// v1 < v2
return r1 ? v1.is_negative :
// v1 > v2
r2 ? v2.is_negative
:
// v1 == v2
zero ? true
: false;
// int res1 = memcmp (v1.integer, v2.integer, sizeof (v1.integer)); // Can
// works only with big-endian
// v1 < v2
if (r1)
return (res < 0);
// v1 > v2
if (r2)
return (res > 0);
// v1 == v2
// !r1 && !r2
return (res == 0);
}
/// @brief Convert starting part of buffer array to bigger integer value.
/// @tparam Ti type to check. Type size bigger then element type size.
/// @param dummy parameter for template substitution
/// @return converted value
template<class Ti>
Ti _toll(typename enable_if<!is_same<Ti, T>::value, Ti>::type = 0) const {
Ti result = 0;
const int type_size = numeric_limits<Ti>::digits / numeric_limits<T>::digits;
for (std::size_t i = isz > type_size ? isz - type_size : 0; i < isz; i++) {
result += integer[i];
if (i < isz - 1)
result <<= std::numeric_limits<T>::digits; // UB if Ti == T !!!
}
return result;
}
/// @brief Variant for same type element. Convert starting part of buffer array to bigger integer value.
/// @tparam Ti type to check. Type size is equivalent to buffer element type size.
/// @param dummy parameter for template substitution
/// @return value of element
template<class Ti>
Ti _toll(typename enable_if<is_same<Ti, T>::value, Ti>::type = 0) const {
return integer[isz - 1];
}
/// @brief Method for addition of v element to this element by index.
/// @param idx index in buffer array.
/// @param v value for add
/// @param c carry flag
/// @return new carry flag
inline bool sum_sop(std::size_t idx, const bdig& v, bool c)
{
if (v.integer[idx] == 0 && !c)
return c;
T one = (c ? 1 : 0);
const T diff = std::numeric_limits<T>::max() - integer[idx];
c = (diff < v.integer[idx] || (diff == v.integer[idx] && one));
if (c)
integer.set(idx, (v.integer[idx] + one) - diff - 1);
else
integer.set(idx, integer[idx] + v.integer[idx] + one);
return c;
}
/// @brief Calculate power of value of bdig type. Internal method.
/// @param _s value to calculating power of value of bdig type.
/// @param with_prec calculate digits with digits after point
/// @return result of method as reference to himself
bdig& _pow(const int _s, bool with_prec = true)
{
if (_s == 1)
return *this;
if (_s == 0) {
if (with_prec)
*this = 1;
else
integer.set(isz - 1, 1);
return *this;
}
if (_s == 2) {
mul<T>(*this);
if (with_prec)
prec_down();
return *this;
}
CRTHEAPOBJ(result, *this);
int v = (_s < 0 ? -_s : _s);
_pow(v / 2, with_prec)._pow(2, with_prec);
if (v % 2) {
mul<T>(*result);
if (with_prec)
prec_down();
}
return *this;
}
public:
const static int digits10 = digits + prec;
const static int size = (prec * 2 / (std::numeric_limits<T>::digits * 301 / 1000)) + 1;
////////////////////////////////////
// Constructors
/// @brief Default constructor
bdig() : is_negative(false) {
}
/// @brief Constructor for all unsigned integer values.
/// @param v unsigned integer value
template<class Ti>
bdig(
Ti v,
typename enable_if<
std::numeric_limits<Ti>::is_integer && !std::numeric_limits<T>::is_signed,
Ti>::type
= 0)
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
: bdig()
{
#else
{
is_negative = false;
#endif
*this = v;
}
/// @brief Constructor for all char pointers values.
/// @param v char pointer value
template<class Ti>
bdig(
const Ti* v,
typename enable_if<
(is_same<unsigned char, Ti>::value || is_same<char, Ti>::value
|| is_same<signed char, Ti>::value)
&& !std::numeric_limits<T>::is_signed,
Ti>::type
= 0)
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
: bdig(){
#else
{
is_negative = false;
#endif
*this = v;
}
/// @brief Constructor for std::string values.
/// @param v std::string value
bdig(const std::string& v)
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
: bdig(){
#else
{
is_negative = false;
#endif
*this = v;
}
////////////////////////////////////
// Math methods
/// @brief Absolute value of current bdig type.
/// @return Result as reference to himself
bdig& abs()
{
is_negative = false;
return *this;
}
/// @brief Calculate power of value of bdig type.
/// @param _s value to calculate power of value bdig type
/// @param with_prec calculate with digits after point
/// @return result of method as reference to himself.
bdig& pow(const int _s, bool with_prec = true)
{
_pow(_s, with_prec);
if (_s < 0) {
// dividing by zero
if (*this == 0) {
return *this;
}
bdig one;
one = 1;
*this = one.prec_up().div(*this);
}