9
9
//
10
10
//===----------------------------------------------------------------------===//
11
11
12
+ #if true
13
+ extension BigInt {
14
+ /// The significand of a `BigInt` value, a nonempty collection of the
15
+ /// significant digits of that value's magnitude stored in words of type
16
+ /// `UInt`.
17
+ ///
18
+ /// The first element of the collection is the lowest word.
19
+ @usableFromInline
20
+ internal typealias _Significand = [ UInt ]
21
+ }
22
+
23
+ extension BigInt . _Significand {
24
+ /// Creates a new significand with the given words.
25
+ @inlinable
26
+ internal init ( _ low: UInt , _ rest: [ UInt ] = [ ] ) {
27
+ self = [ low]
28
+ reserveCapacity ( rest. count + 1 )
29
+ insert ( contentsOf: rest, at: 1 )
30
+ }
31
+ }
32
+ #else
12
33
extension BigInt {
13
34
/// The significand of a `BigInt` value, a nonempty collection of the
14
35
/// significant digits of that value's magnitude stored in words of type
@@ -26,13 +47,6 @@ extension BigInt {
26
47
/// highest.
27
48
@usableFromInline
28
49
internal var _rest : [ UInt ]
29
-
30
- /// Creates a new significand with a single low word equal to zero.
31
- @inlinable
32
- internal init ( ) {
33
- _low = 0
34
- _rest = [ ]
35
- }
36
50
37
51
/// Creates a new significand with the given words.
38
52
@inlinable
@@ -417,6 +431,7 @@ extension BigInt._Significand {
417
431
}
418
432
419
433
extension BigInt . _Significand : Hashable { }
434
+ #endif
420
435
421
436
/// Nota bene:
422
437
/// While operations implemented on `BigInt` expect inputs that are normalized
@@ -595,7 +610,7 @@ extension BigInt._Significand {
595
610
596
611
// @inlinable
597
612
internal func multiplying( by other: Self ) -> Self {
598
- var result = Self ( )
613
+ var result = Self ( 0 )
599
614
result. reserveCapacity ( count + other. count)
600
615
for i in 0 ..< other. count {
601
616
var temporary = self
@@ -609,25 +624,25 @@ extension BigInt._Significand {
609
624
// words) of the lower half of each operand.
610
625
// @inlinable
611
626
internal func multiplying( by other: Self , karatsubaThreshold: Int ) -> Self {
612
- func add( _ lhs: Slice < Self > , _ rhs: Slice < Self > ) -> Self {
627
+ func add( _ lhs: SubSequence , _ rhs: SubSequence ) -> Self {
613
628
// Recall that we have a precondition for `Self<C: Collection>(_: C)` that
614
629
// the argument not be empty.
615
- if lhs. isEmpty { return rhs. isEmpty ? Self ( ) : Self ( rhs) }
630
+ if lhs. isEmpty { return rhs. isEmpty ? Self ( 0 ) : Self ( rhs) }
616
631
if rhs. isEmpty { return Self ( lhs) }
617
632
618
633
var result = Self ( lhs)
619
634
result. add ( Self ( rhs) )
620
635
return result
621
636
}
622
637
623
- func multiply( _ lhs: Slice < Self > , _ rhs: Slice < Self > ) -> Self {
638
+ func multiply( _ lhs: SubSequence , _ rhs: SubSequence ) -> Self {
624
639
625
640
// Based on Karatsuba's method. For details see:
626
641
// <https://mathworld.wolfram.com/KaratsubaMultiplication.html>.
627
642
628
643
let m = ( Swift . max ( lhs. count, rhs. count) + 1 ) / 2
629
644
guard m >= karatsubaThreshold else {
630
- if lhs. isEmpty || rhs. isEmpty { return Self ( ) }
645
+ if lhs. isEmpty || rhs. isEmpty { return Self ( 0 ) }
631
646
return Self ( lhs) . multiplying ( by: Self ( rhs) )
632
647
}
633
648
@@ -655,7 +670,7 @@ extension BigInt._Significand {
655
670
@inlinable
656
671
@discardableResult
657
672
internal mutating func divide( by other: UInt ) -> /* remainder: */ Self {
658
- if other == 1 { return Self ( ) }
673
+ if other == 1 { return Self ( 0 ) }
659
674
var remainder = 0 as UInt
660
675
for i in ( 0 ..< count) . reversed ( ) {
661
676
( self [ i] , remainder) = other. dividingFullWidth ( ( remainder, self [ i] ) )
@@ -706,11 +721,11 @@ extension BigInt._Significand {
706
721
other. removeLast ( n &- ( i &+ 1 ) )
707
722
n = i &+ 1
708
723
}
709
- guard n > 1 else { return divide ( by: other. _low ) }
724
+ guard n > 1 else { return divide ( by: other [ 0 ] ) }
710
725
let clz = other [ n &- 1 ] . leadingZeroBitCount
711
726
712
727
var m = count - n
713
- guard let j = lastIndex ( where: { $0 != 0 } ) else { return Self ( ) }
728
+ guard let j = lastIndex ( where: { $0 != 0 } ) else { return Self ( 0 ) }
714
729
if m > j &+ 1 {
715
730
removeLast ( m &- ( j &+ 1 ) )
716
731
m = j &+ 1
0 commit comments