/
IntX.cs
2061 lines (1783 loc) · 59.7 KB
/
IntX.cs
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
/*
Copyright (c) 2005-2010, Andriy Kozachuk a.k.a. Oyster
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Andriy Kozachuk nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// TODO: move IronScheme extras to partial file.
using System;
using System.Diagnostics;
namespace Oyster.Math
{
/// <summary>
/// Numeric class which represents arbitrary-precision integers.
/// (c) Andriy Kozachuk a.k.a. Oyster [dev.oyster@gmail.com] 2005-2010
/// </summary>
[Serializable]
sealed public class IntX : IConvertible,
IEquatable<IntX>, IEquatable<int>, IEquatable<uint>, IEquatable<long>, IEquatable<ulong>,
IComparable, IComparable<IntX>, IComparable<int>, IComparable<uint>, IComparable<long>, IComparable<ulong>
{
#if DEBUG
/// <summary>
/// Lock for maximal error during FHT rounding (debug-mode only).
/// </summary>
static readonly internal object _maxFhtRoundErrorLock = new object();
/// <summary>
/// Maximal error during FHT rounding (debug-mode only).
/// </summary>
static public double MaxFhtRoundError;
#endif
#region Static fields
static IntXGlobalSettings _globalSettings = new IntXGlobalSettings();
#endregion Static fields
#region Internal fields
internal uint[] _digits; // big integer digits
internal uint _length; // big integer digits length
internal bool _negative; // big integer sign ("-" if true)
//IntXSettings _settings = new IntXSettings(GlobalSettings);
#endregion Internal fields
#region Constructors
/// <summary>
/// Creates new big integer with zero value.
/// </summary>
public IntX() : this(0) {}
/// <summary>
/// Creates new big integer from integer value.
/// </summary>
/// <param name="value">Integer value to create big integer from.</param>
public IntX(int value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
}
else
{
// Prepare internal fields
_digits = new uint[_length = 1];
// Fill the only big integer digit
DigitHelper.ToUInt32WithSign(value, out _digits[0], out _negative);
}
}
/// <summary>
/// Creates new big integer from unsigned integer value.
/// </summary>
/// <param name="value">Unsigned integer value to create big integer from.</param>
[CLSCompliant(false)]
public IntX(uint value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
}
else
{
// Prepare internal fields
_digits = new uint[] { value };
_length = 1;
}
}
/// <summary>
/// Creates new big integer from long value.
/// </summary>
/// <param name="value">Long value to create big integer from.</param>
public IntX(long value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
}
else
{
// Fill the only big integer digit
ulong newValue;
DigitHelper.ToUInt64WithSign(value, out newValue, out _negative);
InitFromUlong(newValue);
}
}
/// <summary>
/// Creates new big integer from unsigned long value.
/// </summary>
/// <param name="value">Unsigned long value to create big integer from.</param>
[CLSCompliant(false)]
public IntX(ulong value)
{
if (value == 0)
{
// Very specific fast processing for zero values
InitFromZero();
}
else
{
InitFromUlong(value);
}
}
/// <summary>
/// Creates new big integer from array of it's "digits".
/// Digit with lower index has less weight.
/// </summary>
/// <param name="digits">Array of <see cref="IntX" /> digits.</param>
/// <param name="negative">True if this number is negative.</param>
/// <exception cref="ArgumentNullException"><paramref name="digits" /> is a null reference.</exception>
[CLSCompliant(false)]
public IntX(uint[] digits, bool negative)
{
// Exceptions
if (digits == null)
{
throw new ArgumentNullException("values");
}
InitFromDigits(digits, negative, DigitHelper.GetRealDigitsLength(digits, (uint)digits.LongLength));
}
/// <summary>
/// Creates new <see cref="IntX" /> from string.
/// </summary>
/// <param name="value">Number as string.</param>
public IntX(string value)
{
IntX intX = Parse(value);
InitFromIntX(intX);
}
/// <summary>
/// Creates new <see cref="IntX" /> from string.
/// </summary>
/// <param name="value">Number as string.</param>
/// <param name="numberBase">Number base.</param>
[CLSCompliant(false)]
public IntX(string value, uint numberBase)
{
IntX intX = Parse(value, numberBase);
InitFromIntX(intX);
}
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="value">Value to copy from.</param>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
public IntX(IntX value)
{
// Exceptions
if (value == null)
{
throw new ArgumentNullException("value");
}
InitFromIntX(value);
}
/// <summary>
/// Creates new empty big integer with desired sign and length.
///
/// For internal use.
/// </summary>
/// <param name="length">Desired digits length.</param>
/// <param name="negative">Desired integer sign.</param>
internal IntX(uint length, bool negative)
{
_digits = new uint[_length = length];
_negative = negative;
}
/// <summary>
/// Creates new big integer from array of it's "digits" but with given length.
/// Digit with lower index has less weight.
///
/// For internal use.
/// </summary>
/// <param name="digits">Array of <see cref="IntX" /> digits.</param>
/// <param name="negative">True if this number is negative.</param>
/// <param name="length">Length to use for internal digits array.</param>
/// <exception cref="ArgumentNullException"><paramref name="digits" /> is a null reference.</exception>
internal IntX(uint[] digits, bool negative, uint length)
{
// Exceptions
if (digits == null)
{
throw new ArgumentNullException("values");
}
InitFromDigits(digits, negative, length);
}
#endregion Constructors
#region Static public properties
/// <summary>
/// <see cref="IntX" /> global settings.
/// </summary>
static public IntXGlobalSettings GlobalSettings
{
get { return _globalSettings; }
}
#endregion Static public properties
#region Public properties
/// <summary>
/// <see cref="IntX" /> instance settings.
/// </summary>
public IntXSettings Settings
{
get { return new IntXSettings(GlobalSettings); }
}
#endregion Public properties
#region Operators
#region operator==
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if equals.</returns>
static public bool operator ==(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, false) == 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if equals.</returns>
static public bool operator ==(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) == 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if equals.</returns>
static public bool operator ==(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) == 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsinged integer and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsinged integer.</param>
/// <returns>True if equals.</returns>
[CLSCompliant(false)]
static public bool operator ==(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) == 0;
}
/// <summary>
/// Compares unsinged integer with <see cref="IntX" /> object and returns true if their internal state is equal.
/// </summary>
/// <param name="int1">First unsinged integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if equals.</returns>
[CLSCompliant(false)]
static public bool operator ==(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) == 0;
}
#endregion operator==
#region operator!=
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if not equals.</returns>
static public bool operator !=(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, false) != 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if not equals.</returns>
static public bool operator !=(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) != 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if not equals.</returns>
static public bool operator !=(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) != 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsigned integer and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsigned integer.</param>
/// <returns>True if not equals.</returns>
[CLSCompliant(false)]
static public bool operator !=(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) != 0;
}
/// <summary>
/// Compares unsigned integer with <see cref="IntX" /> object and returns true if their internal state is not equal.
/// </summary>
/// <param name="int1">First unsigned integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if not equals.</returns>
[CLSCompliant(false)]
static public bool operator !=(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) != 0;
}
#endregion operator!=
#region operator>
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is greater.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater.</returns>
static public bool operator >(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, true) > 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if first is greater.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if first is greater.</returns>
static public bool operator >(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) > 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if first is greater.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater.</returns>
static public bool operator >(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) < 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsigned integer and returns true if first is greater.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsigned integer.</param>
/// <returns>True if first is greater.</returns>
[CLSCompliant(false)]
static public bool operator >(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) > 0;
}
/// <summary>
/// Compares unsigned integer with <see cref="IntX" /> object and returns true if first is greater.
/// </summary>
/// <param name="int1">First unsigned integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater.</returns>
[CLSCompliant(false)]
static public bool operator >(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) < 0;
}
#endregion operator>
#region operator>=
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater or equal.</returns>
static public bool operator >=(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, true) >= 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if first is greater or equal.</returns>
static public bool operator >=(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) >= 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater or equal.</returns>
static public bool operator >=(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) <= 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsinged integer and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsinged integer.</param>
/// <returns>True if first is greater or equal.</returns>
[CLSCompliant(false)]
static public bool operator >=(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) >= 0;
}
/// <summary>
/// Compares unsinged integer with <see cref="IntX" /> object and returns true if first is greater or equal.
/// </summary>
/// <param name="int1">First unsinged integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is greater or equal.</returns>
[CLSCompliant(false)]
static public bool operator >=(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) <= 0;
}
#endregion operator>=
#region operator<
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is lighter.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter.</returns>
static public bool operator <(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, true) < 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if first is lighter.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if first is lighter.</returns>
static public bool operator <(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) < 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if first is lighter.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter.</returns>
static public bool operator <(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) > 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsinged integer and returns true if first is lighter.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsinged integer.</param>
/// <returns>True if first is lighter.</returns>
[CLSCompliant(false)]
static public bool operator <(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) < 0;
}
/// <summary>
/// Compares unsinged integer with <see cref="IntX" /> object and returns true if first is lighter.
/// </summary>
/// <param name="int1">First unsinged integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter.</returns>
[CLSCompliant(false)]
static public bool operator <(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) > 0;
}
#endregion operator<
#region operator<=
/// <summary>
/// Compares two <see cref="IntX" /> objects and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter or equal.</returns>
static public bool operator <=(IntX int1, IntX int2)
{
return OpHelper.Cmp(int1, int2, true) <= 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with integer and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second integer.</param>
/// <returns>True if first is lighter or equal.</returns>
static public bool operator <=(IntX int1, int int2)
{
return OpHelper.Cmp(int1, int2) <= 0;
}
/// <summary>
/// Compares integer with <see cref="IntX" /> object and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter or equal.</returns>
static public bool operator <=(int int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) >= 0;
}
/// <summary>
/// Compares <see cref="IntX" /> object with unsinged integer and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second unsinged integer.</param>
/// <returns>True if first is lighter or equal.</returns>
[CLSCompliant(false)]
static public bool operator <=(IntX int1, uint int2)
{
return OpHelper.Cmp(int1, int2) <= 0;
}
/// <summary>
/// Compares unsinged integer with <see cref="IntX" /> object and returns true if first is lighter or equal.
/// </summary>
/// <param name="int1">First unsinged integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>True if first is lighter or equal.</returns>
[CLSCompliant(false)]
static public bool operator <=(uint int1, IntX int2)
{
return OpHelper.Cmp(int2, int1) >= 0;
}
#endregion operator<=
#region operator+ and operator-
/// <summary>
/// Adds one <see cref="IntX" /> object to another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Addition result.</returns>
static public IntX operator +(IntX int1, IntX int2)
{
return OpHelper.AddSub(int1, int2, false);
}
/// <summary>
/// Subtracts one <see cref="IntX" /> object from another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Subtraction result.</returns>
static public IntX operator -(IntX int1, IntX int2)
{
return OpHelper.AddSub(int1, int2, true);
}
#endregion operator+ and operator-
#region operator*
/// <summary>
/// Multiplies one <see cref="IntX" /> object on another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Multiply result.</returns>
static public IntX operator *(IntX int1, IntX int2)
{
return MultiplyManager.GetCurrentMultiplier().Multiply(int1, int2);
}
#endregion operator*
#region operator/ and operator%
/// <summary>
/// Divides one <see cref="IntX" /> object by another.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Division result.</returns>
static public IntX operator /(IntX int1, IntX int2)
{
IntX modRes;
return DivideManager.GetCurrentDivider().DivMod(int1, int2, out modRes, DivModResultFlags.Div);
}
/// <summary>
/// Divides one <see cref="IntX" /> object by another and returns division modulo.
/// </summary>
/// <param name="int1">First big integer.</param>
/// <param name="int2">Second big integer.</param>
/// <returns>Modulo result.</returns>
static public IntX operator %(IntX int1, IntX int2)
{
IntX modRes;
DivideManager.GetCurrentDivider().DivMod(int1, int2, out modRes, DivModResultFlags.Mod);
return modRes;
}
#endregion operator/ and operator%
#region operator<< and operator>>
/// <summary>
/// Shifts <see cref="IntX" /> object on selected bits count to the left.
/// </summary>
/// <param name="intX">Big integer.</param>
/// <param name="shift">Bits count.</param>
/// <returns>Shifting result.</returns>
static public IntX operator <<(IntX intX, int shift)
{
return OpHelper.Sh(intX, shift, true);
}
/// <summary>
/// Shifts <see cref="IntX" /> object on selected bits count to the right.
/// </summary>
/// <param name="intX">Big integer.</param>
/// <param name="shift">Bits count.</param>
/// <returns>Shifting result.</returns>
static public IntX operator >>(IntX intX, int shift)
{
return OpHelper.Sh(intX, shift, false);
}
#endregion operator<< and operator>>
#region +, -, ++, -- unary operators
/// <summary>
/// Returns the same <see cref="IntX" /> value.
/// </summary>
/// <param name="value">Initial value.</param>
/// <returns>The same value, but new object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
static public IntX operator +(IntX value)
{
// Exception
if (ReferenceEquals(value, null))
{
throw new ArgumentNullException("value");
}
return new IntX(value);
}
/// <summary>
/// Returns the same <see cref="IntX" /> value, but with other sign.
/// </summary>
/// <param name="value">Initial value.</param>
/// <returns>The same value, but with other sign.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
static public IntX operator -(IntX value)
{
// Exception
if (ReferenceEquals(value, null))
{
throw new ArgumentNullException("value");
}
IntX newValue = new IntX(value);
if (newValue._length != 0)
{
newValue._negative = !newValue._negative;
}
return newValue;
}
/// <summary>
/// Returns increased <see cref="IntX" /> value.
/// </summary>
/// <param name="value">Initial value.</param>
/// <returns>Increased value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
static public IntX operator ++(IntX value)
{
// Exception
if (ReferenceEquals(value, null))
{
throw new ArgumentNullException("value");
}
return value + 1U;
}
/// <summary>
/// Returns decreased <see cref="IntX" /> value.
/// </summary>
/// <param name="value">Initial value.</param>
/// <returns>Decreased value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception>
static public IntX operator --(IntX value)
{
// Exception
if (ReferenceEquals(value, null))
{
throw new ArgumentNullException("value");
}
return value - 1U;
}
#endregion +, -, ++, -- unary operators
#region Conversion operators
#region To IntX (Implicit)
/// <summary>
/// Implicitly converts <see cref="Int32" /> to <see cref="IntX" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
static public implicit operator IntX(int value)
{
return new IntX(value);
}
/// <summary>
/// Implicitly converts <see cref="UInt32" /> to <see cref="IntX" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
[CLSCompliant(false)]
static public implicit operator IntX(uint value)
{
return new IntX(value);
}
/// <summary>
/// Implicitly converts <see cref="UInt16" /> to <see cref="IntX" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
[CLSCompliant(false)]
static public implicit operator IntX(ushort value)
{
return new IntX(value);
}
/// <summary>
/// Implicitly converts <see cref="Int64" /> to <see cref="IntX" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
static public implicit operator IntX(long value)
{
return new IntX(value);
}
/// <summary>
/// Implicitly converts <see cref="UInt64" /> to <see cref="IntX" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
[CLSCompliant(false)]
static public implicit operator IntX(ulong value)
{
return new IntX(value);
}
#endregion To IntX (Implicit)
#region From IntX (Explicit)
public bool IsInt => _length <= 1;
public bool IsLong => _length <= 2;
public bool IsInt32 => _length == 0 || (_length == 1 && (_digits[0] & 0x80000000U) == 0 || (_negative && _digits[0] == 0x80000000U));
/// <summary>
/// Explicitly converts <see cref="IntX" /> to <see cref="int" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
static public explicit operator int(IntX value)
{
int res = (int)(uint)value;
return value._negative ? -res : res;
}
/// <summary>
/// Explicitly converts <see cref="IntX" /> to <see cref="uint" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
[CLSCompliant(false)]
static public explicit operator uint(IntX value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (value._length == 0) return 0;
return value._digits[0];
}
/// <summary>
/// Explicitly converts <see cref="IntX" /> to <see cref="long" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
static public explicit operator long(IntX value)
{
long res = (long)(ulong)value;
return value._negative ? -res : res;
}
/// <summary>
/// Explicitly converts <see cref="IntX" /> to <see cref="ulong" />.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Conversion result.</returns>
[CLSCompliant(false)]
static public explicit operator ulong(IntX value)
{
ulong res = (uint)value;
if (value._length > 1)
{
res |= (ulong)value._digits[1] << Constants.DigitBitCount;
}
return res;
}
#endregion Conversion operators
#region bitwise
public static IntX operator ~(IntX x)
{
if (object.ReferenceEquals(x, null))
{
throw new ArgumentNullException("x");
}
return -(x + 1);
}
private static uint extend(uint v, ref bool seenNonZero)
{
if (seenNonZero)
{
return ~v;
}
else
{
if (v == 0)
{
return 0;
}
else
{
seenNonZero = true;
return ~v + 1;
}
}
}
private static uint getOne(bool isNeg, uint[] data, int i, ref bool seenNonZero)
{
if (i < data.Length)
{
uint ret = data[i];
return isNeg ? extend(ret, ref seenNonZero) : ret;
}
else
{
return isNeg ? uint.MaxValue : 0;
}
}
private static uint[] resize(uint[] v, int len)
{
if (v.Length == len) return v;
uint[] ret = new uint[len];
int n = System.Math.Min(v.Length, len);
for (int i = 0; i < n; i++)
{
ret[i] = v[i];
}