-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
BigInt.swift
1878 lines (1568 loc) · 53.4 KB
/
BigInt.swift
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
//===--- BigInt.swift -----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -swift-version 4 -o %t/a.out %s
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: CPU=x86_64
import StdlibUnittest
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Android)
import Android
#elseif os(Windows)
import CRT
#else
#error("Unsupported platform")
#endif
extension FixedWidthInteger {
/// Returns the high and low parts of a potentially overflowing addition.
func addingFullWidth(_ other: Self) ->
(high: Self, low: Self) {
let sum = self.addingReportingOverflow(other)
return (sum.overflow ? 1 : 0, sum.partialValue)
}
/// Returns the high and low parts of two seqeuential potentially overflowing
/// additions.
static func addingFullWidth(_ x: Self, _ y: Self, _ z: Self) ->
(high: Self, low: Self) {
let xy = x.addingReportingOverflow(y)
let xyz = xy.partialValue.addingReportingOverflow(z)
let high: Self = (xy.overflow ? 1 : 0) +
(xyz.overflow ? 1 : 0)
return (high, xyz.partialValue)
}
/// Returns a tuple containing the value that would be borrowed from a higher
/// place and the partial difference of this value and `rhs`.
func subtractingWithBorrow(_ rhs: Self) ->
(borrow: Self, partialValue: Self) {
let difference = subtractingReportingOverflow(rhs)
return (difference.overflow ? 1 : 0, difference.partialValue)
}
/// Returns a tuple containing the value that would be borrowed from a higher
/// place and the partial value of `x` and `y` subtracted from this value.
func subtractingWithBorrow(_ x: Self, _ y: Self) ->
(borrow: Self, partialValue: Self) {
let firstDifference = subtractingReportingOverflow(x)
let secondDifference =
firstDifference.partialValue.subtractingReportingOverflow(y)
let borrow: Self = (firstDifference.overflow ? 1 : 0) +
(secondDifference.overflow ? 1 : 0)
return (borrow, secondDifference.partialValue)
}
}
//===--- BigInt -----------------------------------------------------------===//
//===----------------------------------------------------------------------===//
/// A dynamically-sized signed integer.
///
/// The `_BigInt` type is fully generic on the size of its "word" -- the
/// `BigInt` alias uses the system's word-sized `UInt` as its word type, but
/// any word size should work properly.
public struct _BigInt<Word: FixedWidthInteger & UnsignedInteger> :
BinaryInteger, SignedInteger, CustomStringConvertible,
CustomDebugStringConvertible
where Word.Magnitude == Word
{
/// The binary representation of the value's magnitude, with the least
/// significant word at index `0`.
///
/// - `_data` has no trailing zero elements
/// - If `self == 0`, then `isNegative == false` and `_data == []`
internal var _data: [Word] = []
/// A Boolean value indicating whether this instance is negative.
public private(set) var isNegative = false
/// A Boolean value indicating whether this instance is equal to zero.
public var isZero: Bool {
return _data.isEmpty
}
//===--- Numeric initializers -------------------------------------------===//
/// Creates a new instance equal to zero.
public init() { }
/// Creates a new instance using `_data` as the data collection.
init<C: Collection>(_ _data: C) where C.Iterator.Element == Word {
self._data = Array(_data)
_standardize()
}
public init(integerLiteral value: Int) {
self.init(value)
}
public init<T : BinaryInteger>(_ source: T) {
var source = source
if source < 0 as T {
if source.bitWidth <= UInt64.bitWidth {
let sourceMag = Int(truncatingIfNeeded: source).magnitude
self = _BigInt(sourceMag)
self.isNegative = true
return
} else {
// Have to kind of assume that we're working with another BigInt here
self.isNegative = true
source *= -1
}
}
// FIXME: This is broken on 32-bit arch w/ Word = UInt64
let wordRatio = UInt.bitWidth / Word.bitWidth
assert(wordRatio != 0)
for var sourceWord in source.words {
for _ in 0..<wordRatio {
_data.append(Word(truncatingIfNeeded: sourceWord))
sourceWord >>= Word.bitWidth
}
}
_standardize()
}
public init?<T : BinaryInteger>(exactly source: T) {
self.init(source)
}
public init<T : BinaryInteger>(truncatingIfNeeded source: T) {
self.init(source)
}
public init<T : BinaryInteger>(clamping source: T) {
self.init(source)
}
public init<T : BinaryFloatingPoint>(_ source: T) {
fatalError("Not implemented")
}
public init?<T : BinaryFloatingPoint>(exactly source: T) {
fatalError("Not implemented")
}
/// Returns a randomly-generated word.
static func _randomWord() -> Word {
// This handles up to a 64-bit word
if Word.bitWidth > UInt32.bitWidth {
return Word(UInt32.random(in: 0...UInt32.max)) << 32 | Word(UInt32.random(in: 0...UInt32.max))
} else {
return Word(truncatingIfNeeded: UInt32.random(in: 0...UInt32.max))
}
}
/// Creates a new instance whose magnitude has `randomBits` bits of random
/// data. The sign of the new value is randomly selected.
public init(randomBits: Int) {
let (words, extraBits) =
randomBits.quotientAndRemainder(dividingBy: Word.bitWidth)
// Get the bits for any full words.
self._data = (0..<words).map({ _ in _BigInt._randomWord() })
// Get another random number - the highest bit will determine the sign,
// while the lower `Word.bitWidth - 1` bits are available for any leftover
// bits in `randomBits`.
let word = _BigInt._randomWord()
if extraBits != 0 {
let mask = ~((~0 as Word) << Word(extraBits))
_data.append(word & mask)
}
isNegative = word & ~(~0 >> 1) == 0
_standardize()
}
//===--- Private methods ------------------------------------------------===//
/// Standardizes this instance after mutation, removing trailing zeros
/// and making sure zero is nonnegative. Calling this method satisfies the
/// two invariants.
mutating func _standardize(source: String = #function) {
defer { _checkInvariants(source: source + " >> _standardize()") }
while _data.last == 0 {
_data.removeLast()
}
// Zero is never negative.
isNegative = isNegative && _data.count != 0
}
/// Checks and asserts on invariants -- all invariants must be satisfied
/// at the end of every mutating method.
///
/// - `_data` has no trailing zero elements
/// - If `self == 0`, then `isNegative == false`
func _checkInvariants(source: String = #function) {
if _data.isEmpty {
assert(isNegative == false,
"\(source): isNegative with zero length _data")
}
assert(_data.last != 0, "\(source): extra zeroes on _data")
}
//===--- Word-based arithmetic ------------------------------------------===//
mutating func _unsignedAdd(_ rhs: Word) {
defer { _standardize() }
// Quick return if `rhs == 0`
guard rhs != 0 else { return }
// Quick return if `self == 0`
if isZero {
_data.append(rhs)
return
}
// Add `rhs` to the first word, catching any carry.
var carry: Word
(carry, _data[0]) = _data[0].addingFullWidth(rhs)
// Handle any additional carries
for i in 1..<_data.count {
// No more action needed if there's nothing to carry
if carry == 0 { break }
(carry, _data[i]) = _data[i].addingFullWidth(carry)
}
// If there's any carry left, add it now
if carry != 0 {
_data.append(1)
}
}
/// Subtracts `rhs` from this instance, ignoring the sign.
///
/// - Precondition: `rhs <= self.magnitude`
mutating func _unsignedSubtract(_ rhs: Word) {
precondition(_data.count > 1 || _data[0] > rhs)
// Quick return if `rhs == 0`
guard rhs != 0 else { return }
// If `isZero == true`, then `rhs` must also be zero.
precondition(!isZero)
var carry: Word
(carry, _data[0]) = _data[0].subtractingWithBorrow(rhs)
for i in 1..<_data.count {
// No more action needed if there's nothing to carry
if carry == 0 { break }
(carry, _data[i]) = _data[i].subtractingWithBorrow(carry)
}
assert(carry == 0)
_standardize()
}
/// Adds `rhs` to this instance.
mutating func add(_ rhs: Word) {
if isNegative {
// If _data only contains one word and `rhs` is greater, swap them,
// make self positive and continue with unsigned subtraction.
var rhs = rhs
if _data.count == 1 && _data[0] < rhs {
swap(&rhs, &_data[0])
isNegative = false
}
_unsignedSubtract(rhs)
} else { // positive or zero
_unsignedAdd(rhs)
}
}
/// Subtracts `rhs` from this instance.
mutating func subtract(_ rhs: Word) {
guard rhs != 0 else { return }
if isNegative {
_unsignedAdd(rhs)
} else if isZero {
isNegative = true
_data.append(rhs)
} else {
var rhs = rhs
if _data.count == 1 && _data[0] < rhs {
swap(&rhs, &_data[0])
isNegative = true
}
_unsignedSubtract(rhs)
}
}
/// Multiplies this instance by `rhs`.
mutating func multiply(by rhs: Word) {
// If either `self` or `rhs` is zero, the result is zero.
guard !isZero && rhs != 0 else {
self = 0
return
}
// If `rhs` is a power of two, can just left shift `self`.
let rhsLSB = rhs.trailingZeroBitCount
if rhs >> rhsLSB == 1 {
self <<= rhsLSB
return
}
var carry: Word = 0
for i in 0..<_data.count {
let product = _data[i].multipliedFullWidth(by: rhs)
(carry, _data[i]) = product.low.addingFullWidth(carry)
carry = carry &+ product.high
}
// Add the leftover carry
if carry != 0 {
_data.append(carry)
}
_standardize()
}
/// Divides this instance by `rhs`, returning the remainder.
@discardableResult
mutating func divide(by rhs: Word) -> Word {
precondition(rhs != 0, "divide by zero")
// No-op if `rhs == 1` or `self == 0`.
if rhs == 1 || isZero {
return 0
}
// If `rhs` is a power of two, can just right shift `self`.
let rhsLSB = rhs.trailingZeroBitCount
if rhs >> rhsLSB == 1 {
defer { self >>= rhsLSB }
return _data[0] & ~(~0 << rhsLSB)
}
var carry: Word = 0
for i in (0..<_data.count).reversed() {
let lhs = (high: carry, low: _data[i])
(_data[i], carry) = rhs.dividingFullWidth(lhs)
}
_standardize()
return carry
}
//===--- Numeric --------------------------------------------------------===//
public typealias Magnitude = _BigInt
public var magnitude: _BigInt {
var result = self
result.isNegative = false
return result
}
/// Adds `rhs` to this instance, ignoring any signs.
mutating func _unsignedAdd(_ rhs: _BigInt) {
defer { _checkInvariants() }
let commonCount = Swift.min(_data.count, rhs._data.count)
let maxCount = Swift.max(_data.count, rhs._data.count)
_data.reserveCapacity(maxCount)
// Add the words up to the common count, carrying any overflows
var carry: Word = 0
for i in 0..<commonCount {
(carry, _data[i]) = Word.addingFullWidth(_data[i], rhs._data[i], carry)
}
// If there are leftover words in `self`, just need to handle any carries
if _data.count > rhs._data.count {
for i in commonCount..<maxCount {
// No more action needed if there's nothing to carry
if carry == 0 { break }
(carry, _data[i]) = _data[i].addingFullWidth(carry)
}
// If there are leftover words in `rhs`, need to copy to `self` with carries
} else if _data.count < rhs._data.count {
for i in commonCount..<maxCount {
// Append remaining words if nothing to carry
if carry == 0 {
_data.append(contentsOf: rhs._data.suffix(from: i))
break
}
let sum: Word
(carry, sum) = rhs._data[i].addingFullWidth(carry)
_data.append(sum)
}
}
// If there's any carry left, add it now
if carry != 0 {
_data.append(1)
}
}
/// Subtracts `rhs` from this instance, ignoring the sign.
///
/// - Precondition: `rhs.magnitude <= self.magnitude` (unchecked)
/// - Precondition: `rhs._data.count <= self._data.count`
mutating func _unsignedSubtract(_ rhs: _BigInt) {
precondition(rhs._data.count <= _data.count)
var carry: Word = 0
for i in 0..<rhs._data.count {
(carry, _data[i]) = _data[i].subtractingWithBorrow(rhs._data[i], carry)
}
for i in rhs._data.count..<_data.count {
// No more action needed if there's nothing to carry
if carry == 0 { break }
(carry, _data[i]) = _data[i].subtractingWithBorrow(carry)
}
assert(carry == 0)
_standardize()
}
public static func +=(lhs: inout _BigInt, rhs: _BigInt) {
defer { lhs._checkInvariants() }
if lhs.isNegative == rhs.isNegative {
lhs._unsignedAdd(rhs)
} else {
lhs -= -rhs
}
}
public static func -=(lhs: inout _BigInt, rhs: _BigInt) {
defer { lhs._checkInvariants() }
// Subtracting something of the opposite sign just adds magnitude.
guard lhs.isNegative == rhs.isNegative else {
lhs._unsignedAdd(rhs)
return
}
// Comare `lhs` and `rhs` so we can use `_unsignedSubtract` to subtract
// the smaller magnitude from the larger magnitude.
switch lhs._compareMagnitude(to: rhs) {
case .equal:
lhs = 0
case .greaterThan:
lhs._unsignedSubtract(rhs)
case .lessThan:
// x - y == -y + x == -(y - x)
var result = rhs
result._unsignedSubtract(lhs)
result.isNegative = !lhs.isNegative
lhs = result
}
}
public static func *=(lhs: inout _BigInt, rhs: _BigInt) {
// If either `lhs` or `rhs` is zero, the result is zero.
guard !lhs.isZero && !rhs.isZero else {
lhs = 0
return
}
var newData: [Word] = Array(repeating: 0,
count: lhs._data.count + rhs._data.count)
let (a, b) = lhs._data.count > rhs._data.count
? (lhs._data, rhs._data)
: (rhs._data, lhs._data)
assert(a.count >= b.count)
var carry: Word = 0
for ai in 0..<a.count {
carry = 0
for bi in 0..<b.count {
// Each iteration needs to perform this operation:
//
// newData[ai + bi] += (a[ai] * b[bi]) + carry
//
// However, `a[ai] * b[bi]` produces a double-width result, and both
// additions can overflow to a higher word. The following two lines
// capture the low word of the multiplication and additions in
// `newData[ai + bi]` and any addition overflow in `carry`.
let product = a[ai].multipliedFullWidth(by: b[bi])
(carry, newData[ai + bi]) = Word.addingFullWidth(
newData[ai + bi], product.low, carry)
// Now we combine the high word of the multiplication with any addition
// overflow. It is safe to add `product.high` and `carry` here without
// checking for overflow, because if `product.high == .max - 1`, then
// `carry <= 1`. Otherwise, `carry <= 2`.
//
// Worst-case (aka 9 + 9*9 + 9):
//
// newData a[ai] b[bi] carry
// 0b11111111 + (0b11111111 * 0b11111111) + 0b11111111
// 0b11111111 + (0b11111110_____00000001) + 0b11111111
// (0b11111111_____00000000) + 0b11111111
// (0b11111111_____11111111)
//
// Second-worse case:
//
// 0b11111111 + (0b11111111 * 0b11111110) + 0b11111111
// 0b11111111 + (0b11111101_____00000010) + 0b11111111
// (0b11111110_____00000001) + 0b11111111
// (0b11111111_____00000000)
assert(!product.high.addingReportingOverflow(carry).overflow)
carry = product.high &+ carry
}
// Leftover `carry` is inserted in new highest word.
assert(newData[ai + b.count] == 0)
newData[ai + b.count] = carry
}
lhs._data = newData
lhs.isNegative = lhs.isNegative != rhs.isNegative
lhs._standardize()
}
/// Divides this instance by `rhs`, returning the remainder.
@discardableResult
mutating func _internalDivide(by rhs: _BigInt) -> _BigInt {
precondition(!rhs.isZero, "Divided by zero")
defer { _checkInvariants() }
// Handle quick cases that don't require division:
// If `abs(self) < abs(rhs)`, the result is zero, remainder = self
// If `abs(self) == abs(rhs)`, the result is 1 or -1, remainder = 0
switch _compareMagnitude(to: rhs) {
case .lessThan:
defer { self = 0 }
return self
case .equal:
self = isNegative != rhs.isNegative ? -1 : 1
return 0
default:
break
}
var tempSelf = self.magnitude
let n = tempSelf.bitWidth - rhs.magnitude.bitWidth
var quotient: _BigInt = 0
var tempRHS = rhs.magnitude << n
var tempQuotient: _BigInt = 1 << n
for _ in (0...n).reversed() {
if tempRHS._compareMagnitude(to: tempSelf) != .greaterThan {
tempSelf -= tempRHS
quotient += tempQuotient
}
tempRHS >>= 1
tempQuotient >>= 1
}
// `tempSelf` is the remainder - match sign of original `self`
tempSelf.isNegative = self.isNegative
tempSelf._standardize()
quotient.isNegative = isNegative != rhs.isNegative
self = quotient
_standardize()
return tempSelf
}
public static func /=(lhs: inout _BigInt, rhs: _BigInt) {
lhs._internalDivide(by: rhs)
}
// FIXME: Remove once default implementations are provided:
public static func +(_ lhs: _BigInt, _ rhs: _BigInt) -> _BigInt {
var lhs = lhs
lhs += rhs
return lhs
}
public static func -(_ lhs: _BigInt, _ rhs: _BigInt) -> _BigInt {
var lhs = lhs
lhs -= rhs
return lhs
}
public static func *(_ lhs: _BigInt, _ rhs: _BigInt) -> _BigInt {
var lhs = lhs
lhs *= rhs
return lhs
}
public static func /(_ lhs: _BigInt, _ rhs: _BigInt) -> _BigInt {
var lhs = lhs
lhs /= rhs
return lhs
}
public static func %(_ lhs: _BigInt, _ rhs: _BigInt) -> _BigInt {
var lhs = lhs
lhs %= rhs
return lhs
}
//===--- BinaryInteger --------------------------------------------------===//
/// Creates a new instance using the given data array in two's complement
/// representation.
init(_twosComplementData: [Word]) {
guard _twosComplementData.count > 0 else {
self = 0
return
}
// Is the highest bit set?
isNegative = _twosComplementData.last!.leadingZeroBitCount == 0
if isNegative {
_data = _twosComplementData.map(~)
self._unsignedAdd(1 as Word)
} else {
_data = _twosComplementData
}
_standardize()
}
/// Returns an array of the value's data using two's complement representation.
func _dataAsTwosComplement() -> [Word] {
// Special cases:
// * Nonnegative values are already in 2's complement
if !isNegative {
// Positive values need to have a leading zero bit
if _data.last?.leadingZeroBitCount == 0 {
return _data + [0]
} else {
return _data
}
}
// * -1 will get zeroed out below, easier to handle here
if _data.count == 1 && _data.first == 1 { return [~0] }
var x = self
x._unsignedSubtract(1 as Word)
if x._data.last!.leadingZeroBitCount == 0 {
// The highest bit is set to 1, which moves to 0 after negation.
// We need to add another word at the high end so the highest bit is 1.
return x._data.map(~) + [Word.max]
} else {
// The highest bit is set to 0, which moves to 1 after negation.
return x._data.map(~)
}
}
public var words: [UInt] {
assert(UInt.bitWidth % Word.bitWidth == 0)
let twosComplementData = _dataAsTwosComplement()
var words: [UInt] = []
words.reserveCapacity((twosComplementData.count * Word.bitWidth
+ UInt.bitWidth - 1) / UInt.bitWidth)
var word: UInt = 0
var shift = 0
for w in twosComplementData {
word |= UInt(truncatingIfNeeded: w) << shift
shift += Word.bitWidth
if shift == UInt.bitWidth {
words.append(word)
word = 0
shift = 0
}
}
if shift != 0 {
if isNegative {
word |= ~((1 << shift) - 1)
}
words.append(word)
}
return words
}
/// The number of bits used for storage of this value. Always a multiple of
/// `Word.bitWidth`.
public var bitWidth: Int {
if isZero {
return 0
} else {
let twosComplementData = _dataAsTwosComplement()
// If negative, it's okay to have 1s padded on high end
if isNegative {
return twosComplementData.count * Word.bitWidth
}
// If positive, need to make space for at least one zero on high end
return twosComplementData.count * Word.bitWidth
- twosComplementData.last!.leadingZeroBitCount + 1
}
}
/// The number of sequential zeros in the least-significant position of this
/// value's binary representation.
///
/// The numbers 1 and zero have zero trailing zeros.
public var trailingZeroBitCount: Int {
guard !isZero else {
return 0
}
let i = _data.firstIndex(where: { $0 != 0 })!
assert(_data[i] != 0)
return i * Word.bitWidth + _data[i].trailingZeroBitCount
}
public static func %=(lhs: inout _BigInt, rhs: _BigInt) {
defer { lhs._checkInvariants() }
lhs = lhs._internalDivide(by: rhs)
}
public func quotientAndRemainder(dividingBy rhs: _BigInt) ->
(_BigInt, _BigInt)
{
var x = self
let r = x._internalDivide(by: rhs)
return (x, r)
}
public static func &=(lhs: inout _BigInt, rhs: _BigInt) {
var lhsTemp = lhs._dataAsTwosComplement()
let rhsTemp = rhs._dataAsTwosComplement()
// If `lhs` is longer than `rhs`, behavior depends on sign of `rhs`
// * If `rhs < 0`, length is extended with 1s
// * If `rhs >= 0`, length is extended with 0s, which crops `lhsTemp`
if lhsTemp.count > rhsTemp.count && !rhs.isNegative {
lhsTemp.removeLast(lhsTemp.count - rhsTemp.count)
}
// If `rhs` is longer than `lhs`, behavior depends on sign of `lhs`
// * If `lhs < 0`, length is extended with 1s, so `lhs` should get extra
// bits from `rhs`
// * If `lhs >= 0`, length is extended with 0s
if lhsTemp.count < rhsTemp.count && lhs.isNegative {
lhsTemp.append(contentsOf: rhsTemp[lhsTemp.count..<rhsTemp.count])
}
// Perform bitwise & on words that both `lhs` and `rhs` have.
for i in 0..<Swift.min(lhsTemp.count, rhsTemp.count) {
lhsTemp[i] &= rhsTemp[i]
}
lhs = _BigInt(_twosComplementData: lhsTemp)
}
public static func |=(lhs: inout _BigInt, rhs: _BigInt) {
var lhsTemp = lhs._dataAsTwosComplement()
let rhsTemp = rhs._dataAsTwosComplement()
// If `lhs` is longer than `rhs`, behavior depends on sign of `rhs`
// * If `rhs < 0`, length is extended with 1s, so those bits of `lhs`
// should all be 1
// * If `rhs >= 0`, length is extended with 0s, which is a no-op
if lhsTemp.count > rhsTemp.count && rhs.isNegative {
lhsTemp.replaceSubrange(rhsTemp.count..<lhsTemp.count,
with: repeatElement(Word.max, count: lhsTemp.count - rhsTemp.count))
}
// If `rhs` is longer than `lhs`, behavior depends on sign of `lhs`
// * If `lhs < 0`, length is extended with 1s, so those bits of lhs
// should all be 1
// * If `lhs >= 0`, length is extended with 0s, so those bits should be
// copied from rhs
if lhsTemp.count < rhsTemp.count {
if lhs.isNegative {
lhsTemp.append(contentsOf:
repeatElement(Word.max, count: rhsTemp.count - lhsTemp.count))
} else {
lhsTemp.append(contentsOf: rhsTemp[lhsTemp.count..<rhsTemp.count])
}
}
// Perform bitwise | on words that both `lhs` and `rhs` have.
for i in 0..<Swift.min(lhsTemp.count, rhsTemp.count) {
lhsTemp[i] |= rhsTemp[i]
}
lhs = _BigInt(_twosComplementData: lhsTemp)
}
public static func ^=(lhs: inout _BigInt, rhs: _BigInt) {
var lhsTemp = lhs._dataAsTwosComplement()
let rhsTemp = rhs._dataAsTwosComplement()
// If `lhs` is longer than `rhs`, behavior depends on sign of `rhs`
// * If `rhs < 0`, length is extended with 1s, so those bits of `lhs`
// should all be flipped
// * If `rhs >= 0`, length is extended with 0s, which is a no-op
if lhsTemp.count > rhsTemp.count && rhs.isNegative {
for i in rhsTemp.count..<lhsTemp.count {
lhsTemp[i] = ~lhsTemp[i]
}
}
// If `rhs` is longer than `lhs`, behavior depends on sign of `lhs`
// * If `lhs < 0`, length is extended with 1s, so those bits of `lhs`
// should all be flipped copies of `rhs`
// * If `lhs >= 0`, length is extended with 0s, so those bits should
// be copied from rhs
if lhsTemp.count < rhsTemp.count {
if lhs.isNegative {
lhsTemp += rhsTemp.suffix(from: lhsTemp.count).map(~)
} else {
lhsTemp.append(contentsOf: rhsTemp[lhsTemp.count..<rhsTemp.count])
}
}
// Perform bitwise ^ on words that both `lhs` and `rhs` have.
for i in 0..<Swift.min(lhsTemp.count, rhsTemp.count) {
lhsTemp[i] ^= rhsTemp[i]
}
lhs = _BigInt(_twosComplementData: lhsTemp)
}
public static prefix func ~(x: _BigInt) -> _BigInt {
return -x - 1
}
//===--- SignedNumeric --------------------------------------------------===//
public static prefix func -(x: inout _BigInt) {
defer { x._checkInvariants() }
guard x._data.count > 0 else { return }
x.isNegative = !x.isNegative
}
//===--- Strideable -----------------------------------------------------===//
public func distance(to other: _BigInt) -> _BigInt {
return other - self
}
public func advanced(by n: _BigInt) -> _BigInt {
return self + n
}
//===--- Other arithmetic -----------------------------------------------===//
/// Returns the greatest common divisor for this value and `other`.
public func greatestCommonDivisor(with other: _BigInt) -> _BigInt {
// Quick return if either is zero
if other.isZero {
return magnitude
}
if isZero {
return other.magnitude
}
var (x, y) = (self.magnitude, other.magnitude)
let (xLSB, yLSB) = (x.trailingZeroBitCount, y.trailingZeroBitCount)
// Remove any common factor of two
let commonPower = Swift.min(xLSB, yLSB)
x >>= commonPower
y >>= commonPower
// Remove any remaining factor of two
if xLSB != commonPower {
x >>= xLSB - commonPower
}
if yLSB != commonPower {
y >>= yLSB - commonPower
}
while !x.isZero {
// Swap values to ensure that `x >= y`.
if x._compareMagnitude(to: y) == .lessThan {
swap(&x, &y)
}
// Subtract smaller and remove any factors of two
x._unsignedSubtract(y)
x >>= x.trailingZeroBitCount
}
// Add original common factor of two back into result
y <<= commonPower
return y
}
/// Returns the lowest common multiple for this value and `other`.
public func lowestCommonMultiple(with other: _BigInt) -> _BigInt {
let gcd = greatestCommonDivisor(with: other)
if _compareMagnitude(to: other) == .lessThan {
return ((self / gcd) * other).magnitude
} else {
return ((other / gcd) * self).magnitude
}
}
//===--- String methods ------------------------------------------------===//
/// Creates a new instance from the given string.
///
/// - Parameters:
/// - source: The string to parse for the new instance's value. If a
/// character in `source` is not in the range `0...9` or `a...z`, case
/// insensitive, or is not less than `radix`, the result is `nil`.
/// - radix: The radix to use when parsing `source`. `radix` must be in the
/// range `2...36`. The default is `10`.
public init?(_ source: String, radix: Int = 10) {
assert(2...36 ~= radix, "radix must be in range 2...36")
let radix = Word(radix)
func valueForCodeUnit(_ unit: Unicode.UTF16.CodeUnit) -> Word? {
switch unit {
// "0"..."9"
case 48...57: return Word(unit - 48)
// "a"..."z"
case 97...122: return Word(unit - 87)
// "A"..."Z"
case 65...90: return Word(unit - 55)
// invalid character
default: return nil
}
}
var source = source
// Check for a single prefixing hyphen
let negative = source.hasPrefix("-")
if negative {
source = String(source.dropFirst())
}
// Loop through characters, multiplying
for v in source.utf16.map(valueForCodeUnit) {
// Character must be valid and less than radix
guard let v = v else { return nil }
guard v < radix else { return nil }
self.multiply(by: radix)
self.add(v)
}
self.isNegative = negative
}
/// Returns a string representation of this instance.
///
/// - Parameters:
/// - radix: The radix to use when converting this instance to a string.
/// The value passed as `radix` must be in the range `2...36`. The
/// default is `10`.
/// - lowercase: Whether to use lowercase letters to represent digits
/// greater than 10. The default is `true`.
public func toString(radix: Int = 10, lowercase: Bool = true) -> String {
assert(2...36 ~= radix, "radix must be in range 2...36")
let digitsStart = ("0" as Unicode.Scalar).value
let lettersStart = ((lowercase ? "a" : "A") as Unicode.Scalar).value - 10
func toLetter(_ x: UInt32) -> Unicode.Scalar {
return x < 10
? Unicode.Scalar(digitsStart + x)!
: Unicode.Scalar(lettersStart + x)!
}
let radix = _BigInt(radix)
var result: [Unicode.Scalar] = []
var x = self.magnitude
while !x.isZero {
let remainder: _BigInt
(x, remainder) = x.quotientAndRemainder(dividingBy: radix)
result.append(toLetter(UInt32(remainder)))
}
let sign = isNegative ? "-" : ""
let rest = result.count == 0
? "0"
: String(String.UnicodeScalarView(result.reversed()))
return sign + rest
}