-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpHelper.h
More file actions
1381 lines (1171 loc) · 37.3 KB
/
OpHelper.h
File metadata and controls
1381 lines (1171 loc) · 37.3 KB
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
#pragma once
#ifndef OPHELPER_H
#define OPHELPER_H
// data types
typedef unsigned long long UInt64;
typedef unsigned int UInt32;
#include "../MillerRabin/MillerRabin.h"
#include "../PcgRandom/PcgRandomMinimal.h"
#include "../Multipliers/IMultiplier.h"
#include "../Multipliers/MultiplyManager.h"
#include "DigitOpHelper.h"
#include "DigitHelper.h"
#include "../Utils/Constants.h"
#include "../Utils/Utils.h"
#include "../Utils/Strings.h"
#include "Bits.h"
#include "IntX.h"
#include <cmath>
using namespace std;
class OpHelper
{
public:
/// <summary>
/// Determines <see cref="IntX" /> object with lower length.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="smallerInt">Resulting smaller big integer (by length only).</param>
/// <param name="biggerInt">Resulting bigger big integer (by length only).</param>
static void GetMinMaxLengthObjects(const IntX &int1, const IntX &int2, IntX &smallerInt, IntX &biggerInt)
{
if (int1.length < int2.length)
{
smallerInt = IntX(int1);
biggerInt = IntX(int2);
} // end if
else
{
smallerInt = IntX(int2);
biggerInt = IntX(int1);
} // end else
} // end function GetMinMaxLengthObjects
/// <summary>
/// Adds two big integers.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Resulting big integer.</returns>
/// <exception cref="ArgumentException"><paramref name="int1" /> or <paramref name="int2" /> is too big for add operation.</exception>
static IntX Add(const IntX &int1, const IntX &int2)
{
// Process zero values in special way
if (int2.length == 0) return int1;
if (int1.length == 0)
{
IntX x = int2;
x.negative = int1.negative; // always get sign of the first big integer
return x;
} // end if
// Determine big int with lower length
IntX smallerInt;
IntX biggerInt;
GetMinMaxLengthObjects(int1, int2, smallerInt, biggerInt);
// Check for add operation possibility
if (biggerInt.length == Constants::MaxIntValue)
{
throw ArgumentException(Strings::IntegerTooBig);
} // end if
// Create new big int object of needed length
IntX newInt = IntX(biggerInt.length + 1, int1.negative);
// Do actual addition
newInt.length = DigitOpHelper::Add(
&biggerInt.digits[0],
biggerInt.length,
&smallerInt.digits[0],
smallerInt.length,
&newInt.digits[0]);
// Normalization may be needed
newInt.TryNormalize();
return newInt;
} // end function Add
/// <summary>
/// Subtracts two big integers.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Resulting big integer.</returns>
static IntX Sub(const IntX &int1, const IntX &int2)
{
// Process zero values in special way
if (int1.length == 0) return IntX(int2.digits, true, int2.length);
if (int2.length == 0) return IntX(int1);
// Determine lower big int (without sign)
IntX smallerInt;
IntX biggerInt;
int compareResult = DigitOpHelper::Cmp(&int1.digits[0], int1.length, &int2.digits[0], int2.length);
if (compareResult == 0) return IntX(); // integers are equal
if (compareResult < 0)
{
smallerInt = IntX(int1);
biggerInt = IntX(int2);
} // end if
else
{
smallerInt = IntX(int2);
biggerInt = IntX(int1);
} // end else
// Create new big int object
IntX newInt = IntX(biggerInt.length, IntX::ReferenceEquals(int1, smallerInt) ^ int1.negative);
// Do actual subtraction
newInt.length = DigitOpHelper::Sub(
&biggerInt.digits[0],
biggerInt.length,
&smallerInt.digits[0],
smallerInt.length,
&newInt.digits[0]);
// Normalization may be needed
newInt.TryNormalize();
return newInt;
} // end function Sub
/// <summary>
/// Adds/subtracts one <see cref="IntX" /> to/from another.
/// Determines which operation to use basing on operands signs.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <param name="subtract">Was subtraction initially.</param>
/// <returns>Add/subtract operation result.</returns>
/// <exception cref="ArgumentNullException"><paramref name="int1" /> or <paramref name="int2" /> is a null reference.</exception>
static IntX AddSub(const IntX &int1, const IntX &int2, const bool subtract)
{
// Determine real operation type and result sign
return ((subtract ^ int1.negative) == int2.negative) ? Add(int1, int2) : Sub(int1, int2);
} // end function AddSub
/// <summary>
/// Returns a specified big integer raised to the specified power.
/// </summary>
/// <param name="value">Number to raise.</param>
/// <param name="power">Power.</param>
/// <param name="multiplyMode">Multiply mode set explicitly.</param>
/// <returns>Number in given power.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
static IntX Pow(const IntX &value, const UInt32 power, MultiplyMode multiplyMode)
{
// Return one for zero pow
if (power == 0) return IntX(1);
// Return the number itself from a power of one
if (power == 1) return IntX(value);
// Return zero for a zero
if (value.length == 0) return IntX();
// optimization if value (base) is 2.
if (value == 2)
{
return IntX(1) << power;
} // end if
// Get first one bit
int msb = Bits::Msb(power);
// Get multiplier
IMultiplier *multiplier = MultiplyManager::GetMultiplier(multiplyMode);
// Do actual raising
IntX res = value;
for (UInt32 powerMask = 1U << (msb - 1); powerMask != 0; powerMask >>= 1)
{
// Always square
res = multiplier->Multiply(res, res);
// Maybe mul
if ((power & powerMask) != 0)
{
res = multiplier->Multiply(res, value);
} // end if
} // end for
return res;
} // end function Pow
//==================================================================
// Thanks to Xor-el that made these function possible
//==================================================================
/// <summary>
/// Constant used for internal operations.
/// </summary>
static const int kcbitUint = 32;
/// <summary>
/// Record used for internal building operations.
/// </summary>
struct Builder
{
private:
/// <summary>
/// Integer variable used for internal operations.
/// </summary>
/// <remarks>For a single UInt32, _iuLast is 0.</remarks>
int _iuLast;
/// <summary>
/// UInt32 variable used for internal operations.
/// </summary>
/// <remarks>Used if _iuLast = 0.</remarks>
UInt32 _uSmall;
/// <summary>
/// <see cref="TIntXLibUInt32Array" /> used for internal operations.
/// </summary>
/// <remarks>Used if _iuLast > 0.</remarks>
vector<UInt32> _rgu;
public:
/// <summary>
/// Used to create an Instance of <see cref="TOpHelper.TBuilder" />. for internal use only.
/// </summary>
Builder(const IntX &bn, int &sign)
{
int n, mask;
_rgu = bn.digits;
if (bn == 0) n = 0;
else if (bn.negative) n = -1;
else n = 1;
mask = n >> (kcbitUint - 1);
sign = (sign ^ mask) - mask;
_iuLast = _rgu.size() - 1;
_uSmall = _rgu[0];
while (_iuLast > 0 && _rgu[_iuLast] == 0) --_iuLast;
} // end constructor
/// <summary>
/// Function used for Internal operations.
/// </summary>
void GetApproxParts(int &exp, UInt64 &man)
{
int cuLeft, cbit;
if (_iuLast == 0)
{
man = UInt64(_uSmall);
exp = 0;
return;
} // end if
cuLeft = _iuLast - 1;
man = MakeUlong(_rgu[cuLeft + 1], _rgu[cuLeft]);
exp = cuLeft * kcbitUint;
cbit = CbitHighZero(_rgu[cuLeft + 1]);
if (cuLeft > 0 && cbit > 0)
{
// Get 64 bits.
man = (man << cbit) | (_rgu[cuLeft - 1] >> (kcbitUint - cbit));
exp = exp - cbit;
} // end if
} // end function GetApproxParts
}; // end struct Builder
/// <summary>
/// Record used for internal operations.
/// </summary>
/// <remarks>
/// Both Record Fields are aligned at Zero Offsets.
/// </remarks>
struct DoubleUlong
{
union UULomg
{
/// <summary>
/// Double variable used for internal operations.
/// </summary>
double dbl;
/// <summary>
/// UInt64 variable used for internal operations.
/// </summary>
UInt64 uu;
};
}; // end struct DoubleUlong
/// <summary>
/// Returns a Non-Negative Random <see cref="TIntX" /> object using Pcg Random.
/// </summary>
/// <returns>Random TIntX value.</returns>
static IntX Random()
{
return Pcg::NextUInt32();
} // end function Random
/// <summary>
/// Returns a Non-Negative Random <see cref="TIntX" /> object using Pcg Random within the specified Range. (Max not Included)
/// </summary>
/// <param name="Min">Minimum value.</param>
/// <param name="Max">Maximum value (Max not Included)</param>
/// <returns>Random TIntX value.</returns>
static IntX RandomRange(const UInt32 Min, const UInt32 Max)
{
return Pcg::NextUInt32(Min, Max);
} // end function RandomRange
/// <summary>
/// Returns a non negative big integer.
/// </summary>
/// <param name="value">value to get its absolute value.</param>
/// <returns>Absolute number.</returns>
static IntX AbsoluteValue(const IntX &value)
{
if (value.negative)
return -(IntX)value;
else
return value;
} // end function AbsoluteValue
/// <summary>
/// Returns a specified big integer raised to the power of 2.
/// </summary>
/// <param name="value">Number to get its square.</param>
/// <returns>Squared number.</returns>
static IntX Square(const IntX &value)
{
return IntX::Pow(value, 2);
} // end function Square
/// <summary>
/// Calculates Integer SquareRoot of <see cref="TIntX" /> object
/// </summary>
/// <param name="value">value to get Integer squareroot of.</param>
/// <returns>Integer SquareRoot.</returns>
/// <seealso href="http://www.dahuatu.com/RkWdPBx6W8.html">[IntegerSquareRoot Implementation]</seealso>
static IntX IntegerSquareRoot(const IntX &value)
{
IntX a, b, mid, eight;
if (value == 0) return 0;
if (value == 1) return 1;
a = 1;
eight = 8;
b = (value >> 5) + eight;
while (b.CompareTo(a) >= 0)
{
mid = (a + b) >> 1;
if ((mid * mid).CompareTo(value) > 0)
b = (mid - 1);
else
a = (mid + 1);
} // end while
return a - 1;
} // end function IntegerSquareRoot
/// <summary>
/// Returns a specified big integer holding the factorial of value.
/// </summary>
/// <param name="value">Number to get its factorial.</param>
/// <returns>factorialed number.</returns>
/// <exception cref="EArgumentException"><paramref name="value" /> is a negative value.</exception>
static IntX Factorial(const IntX &value)
{
IntX i;
// Exception
if (value < 0)
throw ArgumentException(Strings::NegativeFactorial + string(" [") + value.ToString() + string("]"));
IntX result = 1;
// using iterative approach
// recursive approach is slower and causes much overhead. it is also limiting (stackoverflow)
i = 1;
while (i <= value)
{
result = result * i;
++i;
} // end while
return result;
} // end function
/// <summary>
/// Returns the number of trailing 0-bits in x, starting at the least significant
/// bit position.
/// </summary>
/// <param name="x">value to get number of trailing zero bits.</param>
/// <returns>number of trailing 0-bits.</returns>
/// <remarks>If x is 0, the result is undefined as per GCC Implementation.</remarks>
static int __builtin_ctz(UInt32 x)
{
int n;
// This uses a binary search algorithm from Hacker's Delight.
n = 1;
if ((x & 0x0000FFFF) == 0)
{
n += 16;
x >>= 16;
} // end if
if ((x & 0x000000FF) == 0)
{
n += 8;
x >>= 8;
} // end if
if ((x & 0x0000000F) == 0)
{
n += 4;
x >>= 4;
} // end if
if ((x & 0x00000003) == 0)
{
n += 2;
x >>= 2;
} // end if
return n - int(x & 1);
} // end function __builtin_ctz
/// <summary>
/// (Optimized GCD).
/// Returns a specified big integer holding the GCD (Greatest common Divisor) of
/// two big integers using Binary GCD (Stein's algorithm).
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>GCD number.</returns>
/// <seealso href="http://lemire.me/blog/archives/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/">[GCD Implementation]</seealso>
/// <seealso href="https://hbfs.wordpress.com/2013/12/10/the-speed-of-gcd/">[GCD Implementation Optimizations]</seealso>
static IntX GCD(const IntX &intX1, const IntX &intX2)
{
IntX int1 = intX1, int2 = intX2;
int shift;
IntX temp;
// check if int1 is negative and returns the absolute value of it.
if (int1.negative)
int1 = AbsoluteValue(int1);
// check if int2 is negative and returns the absolute value of it.
if (int2.negative)
int2 = AbsoluteValue(int2);
// simple cases (termination)
if (int1 == int2) return int1;
if (int1 == 0) return int2;
if (int2 == 0) return int1;
shift = __builtin_ctz(UInt32(int1 | int2));
int1 = int1 >> __builtin_ctz(UInt32(int1));
while (int2 != 0)
{
int2 = int2 >> __builtin_ctz(UInt32(int2));
if (int1 > int2)
{
temp = int2;
int2 = int1;
int1 = temp;
} // end if
int2 = int2 - int1;
} // end while
return int1 << shift;
} // end function GCD
/// <summary>
/// (LCM).
/// Returns a specified big integer holding the LCM (Least Common Multiple) of
/// two big integers.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>LCM number.</returns>
/// <exception cref="EArgumentNilException"><paramref name="int1" /> is a null reference.</exception>
/// <exception cref="EArgumentNilException"><paramref name="int2" /> is a null reference.</exception>
static IntX LCM(const IntX &int1, const IntX &int2)
{
return IntX::AbsoluteValue(int1 * int2) / IntX::GCD(int1, int2);
} // end function LCM
/// <summary>
/// Calculate Modular Inverse for two <see cref="TIntX" /> objects using Euclids Extended Algorithm.
/// returns Zero if no Modular Inverse Exists for the Inputs
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Modular Inverse.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Modular_multiplicative_inverse">[Modular Inverse Explanation]</seealso>
/// <seealso href="http://www.di-mgt.com.au/euclidean.html">[Modular Inverse Implementation]</seealso>
static IntX InvMod(const IntX &int1, const IntX &int2)
{
IntX u1, u3, v1, v3, t1, t3, q, iter;
/* Step X1. Initialize */
u1 = 1;
u3 = int1;
v1 = 0;
v3 = int2;
/* Remember odd/even iterations */
iter = 1;
/* Step X2. Loop while v3 != 0 */
while (v3 != 0)
{
/* Step X3. Divide and "Subtract" */
q = u3 / v3;
t3 = u3 % v3;
t1 = u1 + q * v1;
/* Swap */
u1 = v1;
v1 = t1;
u3 = v3;
v3 = t3;
iter = -iter;
} // end while
/* Make sure u3 = gcd(u,v) == 1 */
if (u3 != 1) // u3 is now holding the GCD Value
return 0; /* Error: No inverse exists */
/* Ensure a positive result */
// result will hold the Modular Inverse (InvMod)
if (iter < 0) return int2 - u1;
return u1;
} // end function InvMod
/// <summary>
/// Calculates Calculates Modular Exponentiation of <see cref="TIntX" /> object.
/// </summary>
/// <param name="value">value to compute ModPow of.</param>
/// <param name="exponent">exponent to use.</param>
/// <param name="modulus">modulus to use.</param>
/// <returns>Computed value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Modular_exponentiation">[Modular Exponentiation Explanation]</seealso>
static IntX ModPow(const IntX &value, const IntX &exponent, const IntX &modulus)
{
IntX result = 1;
IntX mValue = value % modulus;
IntX mExponent = exponent;
while (mExponent > 0)
{
if (mExponent.IsOdd()) result = (result * mValue) % modulus;
mExponent = mExponent >> 1;
mValue = (mValue * mValue) % modulus;
} // end while
return result;
} // end function ModPow
/// <summary>
/// Calculates Bézoutsidentity for two <see cref="TIntX" /> objects using Euclids Extended Algorithm
/// </summary>
/// <param name="int1">first value.</param>
/// <param name="int2">second value.</param>
/// <param name="bezOne">first bezout value.</param>
/// <param name="bezTwo">second bezout value.</param>
/// <returns>GCD (Greatest Common Divisor) value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/Bézout's_identity">[Bézout's identity Explanation]</seealso>
/// <seealso href="https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Pseudocode">[Bézout's identity Pseudocode using Extended Euclidean algorithm]</seealso>
static IntX Bezoutsidentity(const IntX &int1, const IntX &int2, IntX &bezOne, IntX &bezTwo)
{
IntX s, t, r, old_s, old_t, old_r, quotient, prov;
if (int1.negative)
throw ArgumentNullException(Strings::BezoutNegativeNotAllowed + string(" int1"));
if (int2.negative)
throw ArgumentNullException(Strings::BezoutNegativeNotAllowed + string(" int2"));
if (int1 == 0 || int2 == 0)
throw ArgumentException(Strings::BezoutNegativeCantComputeZero);
s = 0;
t = 1;
r = int2;
old_s = 1;
old_t = 0;
old_r = int1;
while (r != 0)
{
quotient = old_r / r;
prov = r;
r = old_r - quotient * prov;
old_r = prov;
prov = s;
s = old_s - quotient * prov;
old_s = prov;
prov = t;
t = old_t - quotient * prov;
old_t = prov;
} // end while
if (old_r.negative) old_r = IntX::AbsoluteValue(old_r);
bezOne = old_s; // old_s holds the first value of bezout identity
bezTwo = old_t; // old_t holds the second value of bezout identity
return old_r; // old_r holds the GCD Value
} // end function Bezoutsidentity
/// <summary>
/// Checks if a <see cref="TIntX" /> object is Probably Prime using MillerRabin primality test.
/// </summary>
/// <param name="value">big integer to check primality.</param>
/// <param name="Accuracy">Accuracy parameter `k´ of the Miller-Rabin algorithm. Default is 5. The execution time is proportional to the value of the accuracy parameter.</param>
/// <returns>Boolean value.</returns>
/// <seealso href="https://en.wikipedia.org/wiki/MillerRabin_primality_test">[MillerRabin primality test Explanation]</seealso>
/// <seealso href="https://github.com/cslarsen/miller-rabin">[MillerRabin primality test Implementation in C]</seealso>
static bool IsProbablyPrime(const IntX &value, const int Accuracy = 5)
{
return MillerRabin::IsProbablyPrimeMR(value, Accuracy);
} // end function IsProbablyPrime
/// <summary>
/// The Max Between Two TIntX values.
/// </summary>
/// <param name="left">left value.</param>
/// <param name="right">right value.</param>
/// <returns>The Maximum TIntX value.</returns>
static IntX Max(const IntX &left, const IntX &right)
{
if (left.CompareTo(right) < 0)
return right;
else
return left;
} // end function Max
/// <summary>
/// The Min Between Two TIntX values.
/// </summary>
/// <param name="left">left value.</param>
/// <param name="right">right value.</param>
/// <returns>The Minimum TIntX value.</returns>
static IntX Min(const IntX &left, const IntX &right)
{
if (left.CompareTo(right) <= 0)
return left;
else
return right;
} // end function Min
/// <summary>
/// Function used for Internal operations.
/// </summary>
static UInt64 MakeUlong(const UInt32 uHi, const UInt32 uLo)
{
return (UInt64(uHi) << kcbitUint) | uLo;
} // end function MakeUlong
/// <summary>
/// Function used for Internal operations.
/// </summary>
static int CbitHighZero(UInt32 u)
{
int cbit;
if (u == 0) return 32;
cbit = 0;
if ((u & 0xFFFF0000) == 0)
{
cbit += 16;
u <<= 16;
} // end if
if ((u & 0xFF000000) == 0)
{
cbit += 8;
u <<= 8;
} // end if
if ((u & 0xF0000000) == 0)
{
cbit += 4;
u <<= 4;
} // end if
if ((u & 0xC0000000) == 0)
{
cbit += 2;
u <<= 2;
} // end if
if ((u & 0x80000000) == 0) cbit += 1;
return cbit;
} // end function CbitHighZero
/// <summary>
/// Function used for Internal operations.
/// </summary>
static int CbitHighZero(const UInt64 uu)
{
if ((uu & 0xFFFFFFFF00000000) == 0) return 32 + CbitHighZero(UInt32(uu));
return CbitHighZero(UInt32(uu >> 32));
} // end function CbitHighZero
// Gotten from stack overflow
static double logN(const double base, const double x)
{
return log(x) / log(base);
} // end function logN
/// <summary>
/// Calculates Logarithm of a number <see cref="TIntX" /> object for a specified base.
/// the largest power the base can be raised to that does not exceed the number.
/// </summary>
/// <param name="base">base.</param>
/// <param name="value">number to get log of.</param>
/// <returns>Log value.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
static double LogN(const double base, const IntX &value)
{
double tempD, tempTwo, pa, pb, pc, tempDouble;
int c;
long long b;
UInt64 h, m, l, x;
const double constOne = 1.0, DoubleNaN = FP_NAN, DoublePositiveInfinity = FP_INFINITE, DoubleNegativeInfinity = -FP_INFINITE, ZeroPointZero = FP_ZERO;
if (value.negative || base == constOne) return DoubleNaN;
if (base == DoublePositiveInfinity)
{
if (value == 1) return ZeroPointZero;
return DoubleNaN;
} // end if
if (base == ZeroPointZero && !(value == 1)) return DoubleNaN;
if (value.digits.empty()) return DoubleNegativeInfinity;
if (value <= Constants::MaxUInt64Value)
{
tempDouble = (UInt64)value;
return logN(base, tempDouble);
} // end if
h = value.digits[value.digits.size() - 1];
if (value.digits.size() > 1)
m = value.digits[value.digits.size() - 2];
else
m = 0;
if (value.digits.size() > 2)
l = value.digits[value.digits.size() - 3];
else
l = 0;
// measure the exact bit count
c = CbitHighZero(UInt32(h));
b = ((long long)value.digits.size() * 32) - c;
// extract most significant bits
x = (h << (32 + c)) | (m << c) | (l >> (32 - c));
tempD = (double)x;
tempTwo = 2;
pa = logN(base, tempD);
pc = logN(tempTwo, base);
pb = (b - 64) / pc;
return pa + pb;
} // end function LogN
/// <summary>
/// Calculates Integer Logarithm of a number <see cref="TIntX" /> object for a specified base.
/// the largest power the base can be raised to that does not exceed the number.
/// </summary>
/// <param name="base">base.</param>
/// <param name="number">number to get Integer log of.</param>
/// <returns>Integer Log.</returns>
/// <seealso href="http://gist.github.com/dharmatech/409723">[IntegerLogN Implementation]</seealso>
static IntX IntegerLogN(const IntX &base, const IntX &number)
{
IntX lo, b_lo, hi, mid, b_mid, b_hi;
lo = 0;
b_lo = 1;
hi = 1;
b_hi = base;
while (b_hi < number)
{
lo = hi;
b_lo = b_hi;
hi = hi * 2;
b_hi = b_hi * b_hi;
} // end while
while (hi - lo > 1)
{
mid = (lo + hi) / 2;
b_mid = b_lo * (int)IntX::Pow(base, UInt32(mid - lo));
if (number < b_mid)
{
hi = mid;
b_hi = b_mid;
} // end if
if (number > b_mid)
{
lo = mid;
b_lo = b_mid;
} // end if
if (number == b_mid) return mid;
} // end while
if (b_hi == number) return hi;
return lo;
} // end function IntegerLogN
/// <summary>
/// The base-10 logarithm of the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The base-10 logarithm of the value.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
static double Log10(const IntX &value)
{
return OpHelper::LogN(10, value);
} // end function Log10
/// <summary>
/// Calculates the natural logarithm of the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The natural logarithm.</returns>
/// <remarks> Source : Microsoft .NET Reference on GitHub </remarks>
static double Ln(const IntX &value)
{
return OpHelper::LogN(Constants::EulersNumber, value);
} // end function Ln
/// <summary>
/// Function used for Internal operations.
/// </summary>
/// <param name="mdbl">
/// internal variable
/// </param>
/// <param name="sign">
/// variable used to indicate sign
/// </param>
/// <param name="exp">
/// internal variable
/// </param>
/// <param name="man">
/// internal variable
/// </param>
/// <param name="fFinite">
/// internal variable
/// </param>
/// <remarks>
/// Source : Microsoft .NET Reference on GitHub
/// </remarks>
static void GetDoubleParts(const double mdbl, int &sign, int &exp, UInt64 &man, bool &fFinite)
{
DoubleUlong::UULomg du;
du.uu = 0;
du.dbl = mdbl;
sign = 1 - (int(du.uu >> 62) & 2);
man = du.uu & 0x000FFFFFFFFFFFFF;
exp = int(du.uu >> 52) & 0x7FF;
if (exp == 0)
{
// Denormalized number.
fFinite = true;
if (man != 0)
exp = -1074;
} // end if
else
{
if (exp == 0x7FF)
{
// NaN or Inifite.
fFinite = false;
exp = Constants::MaxIntValue;
} // end if
else
{
fFinite = true;
man = man | 0x0010000000000000;
exp = exp - 1075;
} // end else
} // end else
} // end function GetDoubleParts
/// <summary>
/// Function used for Internal operations.
/// </summary>
/// <param name="sign">
/// variable indicating sign
/// </param>
/// <param name="exp">
/// variable used for internal operations
/// </param>
/// <param name="man">
/// variable used for internal operations
/// </param>
/// <remarks>
/// Source : Microsoft .NET Reference on GitHub
/// </remarks>
static double GetDoubleFromParts(int sign, int exp, UInt64 man)
{
DoubleUlong::UULomg du;
int cbitShift;
du.dbl = 0;
if (man == 0) du.uu = 0;
else
{
// Normalize so that $0010 0000 0000 0000 is the highest bit set.
cbitShift = CbitHighZero(man) - 11;
if (cbitShift < 0) man = man >> -cbitShift;
else man = man << cbitShift;
exp = exp - cbitShift;
// Move the point to just behind the leading 1: $001.0 0000 0000 0000
// (52 bits) and skew the exponent (by $3FF == 1023).
exp = exp + 1075;
if (exp >= 0x7FF)
// Infinity.
du.uu = 0x7FF0000000000000;
else if (exp <= 0)
{
// Denormalized.
--exp;
if (exp < -52)
// Underflow to zero.
du.uu = 0;
else
du.uu = man >> -exp;
} // end else if
else
// Mask off the implicit high bit.
du.uu = (man & 0x000FFFFFFFFFFFFF) | (UInt64(exp) << 52);
} // end else
if (sign < 0)
du.uu = du.uu | 0x8000000000000000;
return du.dbl;
} // end function GetDoubleFromParts
/// <summary>
/// function used for Internal operations