|
@@ -1,6 +1,6 @@
|
|
//
|
|
//
|
|
// Division.swift
|
|
// Division.swift
|
|
-// BigInt
|
|
|
|
|
|
+// CS.BigInt
|
|
//
|
|
//
|
|
// Created by Károly Lőrentey on 2016-01-03.
|
|
// Created by Károly Lőrentey on 2016-01-03.
|
|
// Copyright © 2016-2017 Károly Lőrentey.
|
|
// Copyright © 2016-2017 Károly Lőrentey.
|
|
@@ -139,7 +139,7 @@ extension FixedWidthInteger {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-extension BigUInt {
|
|
|
|
|
|
+extension CS.BigUInt {
|
|
//MARK: Division
|
|
//MARK: Division
|
|
|
|
|
|
/// Divide this integer by the word `y`, leaving the quotient in its place and returning the remainder.
|
|
/// Divide this integer by the word `y`, leaving the quotient in its place and returning the remainder.
|
|
@@ -163,7 +163,7 @@ extension BigUInt {
|
|
/// - Requires: y > 0
|
|
/// - Requires: y > 0
|
|
/// - Returns: (quotient, remainder) where quotient = floor(x/y), remainder = x - quotient * y
|
|
/// - Returns: (quotient, remainder) where quotient = floor(x/y), remainder = x - quotient * y
|
|
/// - Complexity: O(x.count)
|
|
/// - Complexity: O(x.count)
|
|
- internal func quotientAndRemainder(dividingByWord y: Word) -> (quotient: BigUInt, remainder: Word) {
|
|
|
|
|
|
+ internal func quotientAndRemainder(dividingByWord y: Word) -> (quotient: CS.BigUInt, remainder: Word) {
|
|
var div = self
|
|
var div = self
|
|
let mod = div.divide(byWord: y)
|
|
let mod = div.divide(byWord: y)
|
|
return (div, mod)
|
|
return (div, mod)
|
|
@@ -171,7 +171,7 @@ extension BigUInt {
|
|
|
|
|
|
/// Divide `x` by `y`, putting the quotient in `x` and the remainder in `y`.
|
|
/// Divide `x` by `y`, putting the quotient in `x` and the remainder in `y`.
|
|
/// Reusing integers like this reduces the number of allocations during the calculation.
|
|
/// Reusing integers like this reduces the number of allocations during the calculation.
|
|
- static func divide(_ x: inout BigUInt, by y: inout BigUInt) {
|
|
|
|
|
|
+ static func divide(_ x: inout CS.BigUInt, by y: inout CS.BigUInt) {
|
|
// This is a Swift adaptation of "divmnu" from Hacker's Delight, which is in
|
|
// This is a Swift adaptation of "divmnu" from Hacker's Delight, which is in
|
|
// turn a C adaptation of Knuth's Algorithm D (TAOCP vol 2, 4.3.1).
|
|
// turn a C adaptation of Knuth's Algorithm D (TAOCP vol 2, 4.3.1).
|
|
|
|
|
|
@@ -184,7 +184,7 @@ extension BigUInt {
|
|
}
|
|
}
|
|
if y.count == 1 {
|
|
if y.count == 1 {
|
|
// The single-word case reduces to a simpler loop.
|
|
// The single-word case reduces to a simpler loop.
|
|
- y = BigUInt(x.divide(byWord: y[0]))
|
|
|
|
|
|
+ y = CS.BigUInt(x.divide(byWord: y[0]))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
@@ -214,14 +214,14 @@ extension BigUInt {
|
|
let z = y.leadingZeroBitCount
|
|
let z = y.leadingZeroBitCount
|
|
y <<= z
|
|
y <<= z
|
|
x <<= z // We'll calculate the remainder in the normalized dividend.
|
|
x <<= z // We'll calculate the remainder in the normalized dividend.
|
|
- var quotient = BigUInt()
|
|
|
|
|
|
+ var quotient = CS.BigUInt()
|
|
assert(y.leadingZeroBitCount == 0)
|
|
assert(y.leadingZeroBitCount == 0)
|
|
|
|
|
|
// We're ready to start the long division!
|
|
// We're ready to start the long division!
|
|
let dc = y.count
|
|
let dc = y.count
|
|
let d1 = y[dc - 1]
|
|
let d1 = y[dc - 1]
|
|
let d0 = y[dc - 2]
|
|
let d0 = y[dc - 2]
|
|
- var product: BigUInt = 0
|
|
|
|
|
|
+ var product: CS.BigUInt = 0
|
|
for j in (dc ... x.count).reversed() {
|
|
for j in (dc ... x.count).reversed() {
|
|
// Approximate dividing the top dc+1 words of `remainder` using the topmost 3/2 words.
|
|
// Approximate dividing the top dc+1 words of `remainder` using the topmost 3/2 words.
|
|
let r2 = x[j]
|
|
let r2 = x[j]
|
|
@@ -253,12 +253,12 @@ extension BigUInt {
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `x` by `y`, putting the remainder in `x`.
|
|
/// Divide `x` by `y`, putting the remainder in `x`.
|
|
- mutating func formRemainder(dividingBy y: BigUInt, normalizedBy shift: Int) {
|
|
|
|
|
|
+ mutating func formRemainder(dividingBy y: CS.BigUInt, normalizedBy shift: Int) {
|
|
precondition(!y.isZero)
|
|
precondition(!y.isZero)
|
|
assert(y.leadingZeroBitCount == 0)
|
|
assert(y.leadingZeroBitCount == 0)
|
|
if y.count == 1 {
|
|
if y.count == 1 {
|
|
let remainder = self.divide(byWord: y[0] >> shift)
|
|
let remainder = self.divide(byWord: y[0] >> shift)
|
|
- self.load(BigUInt(remainder))
|
|
|
|
|
|
+ self.load(CS.BigUInt(remainder))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
self <<= shift
|
|
self <<= shift
|
|
@@ -266,7 +266,7 @@ extension BigUInt {
|
|
let dc = y.count
|
|
let dc = y.count
|
|
let d1 = y[dc - 1]
|
|
let d1 = y[dc - 1]
|
|
let d0 = y[dc - 2]
|
|
let d0 = y[dc - 2]
|
|
- var product: BigUInt = 0
|
|
|
|
|
|
+ var product: CS.BigUInt = 0
|
|
for j in (dc ... self.count).reversed() {
|
|
for j in (dc ... self.count).reversed() {
|
|
let r2 = self[j]
|
|
let r2 = self[j]
|
|
let r1 = self[j - 1]
|
|
let r1 = self[j - 1]
|
|
@@ -292,24 +292,24 @@ extension BigUInt {
|
|
/// - Requires: `y > 0`
|
|
/// - Requires: `y > 0`
|
|
/// - Returns: `(quotient, remainder)` where `quotient = floor(self/y)`, `remainder = self - quotient * y`
|
|
/// - Returns: `(quotient, remainder)` where `quotient = floor(self/y)`, `remainder = self - quotient * y`
|
|
/// - Complexity: O(count^2)
|
|
/// - Complexity: O(count^2)
|
|
- public func quotientAndRemainder(dividingBy y: BigUInt) -> (quotient: BigUInt, remainder: BigUInt) {
|
|
|
|
|
|
+ public func quotientAndRemainder(dividingBy y: CS.BigUInt) -> (quotient: CS.BigUInt, remainder: CS.BigUInt) {
|
|
var x = self
|
|
var x = self
|
|
var y = y
|
|
var y = y
|
|
- BigUInt.divide(&x, by: &y)
|
|
|
|
|
|
+ CS.BigUInt.divide(&x, by: &y)
|
|
return (x, y)
|
|
return (x, y)
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `x` by `y` and return the quotient.
|
|
/// Divide `x` by `y` and return the quotient.
|
|
///
|
|
///
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
- public static func /(x: BigUInt, y: BigUInt) -> BigUInt {
|
|
|
|
|
|
+ public static func /(x: CS.BigUInt, y: CS.BigUInt) -> CS.BigUInt {
|
|
return x.quotientAndRemainder(dividingBy: y).quotient
|
|
return x.quotientAndRemainder(dividingBy: y).quotient
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `x` by `y` and return the remainder.
|
|
/// Divide `x` by `y` and return the remainder.
|
|
///
|
|
///
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
- public static func %(x: BigUInt, y: BigUInt) -> BigUInt {
|
|
|
|
|
|
+ public static func %(x: CS.BigUInt, y: CS.BigUInt) -> CS.BigUInt {
|
|
var x = x
|
|
var x = x
|
|
let shift = y.leadingZeroBitCount
|
|
let shift = y.leadingZeroBitCount
|
|
x.formRemainder(dividingBy: y << shift, normalizedBy: shift)
|
|
x.formRemainder(dividingBy: y << shift, normalizedBy: shift)
|
|
@@ -319,57 +319,57 @@ extension BigUInt {
|
|
/// Divide `x` by `y` and store the quotient in `x`.
|
|
/// Divide `x` by `y` and store the quotient in `x`.
|
|
///
|
|
///
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
- public static func /=(x: inout BigUInt, y: BigUInt) {
|
|
|
|
|
|
+ public static func /=(x: inout CS.BigUInt, y: CS.BigUInt) {
|
|
var y = y
|
|
var y = y
|
|
- BigUInt.divide(&x, by: &y)
|
|
|
|
|
|
+ CS.BigUInt.divide(&x, by: &y)
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `x` by `y` and store the remainder in `x`.
|
|
/// Divide `x` by `y` and store the remainder in `x`.
|
|
///
|
|
///
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
/// - Note: Use `divided(by:)` if you also need the remainder.
|
|
- public static func %=(x: inout BigUInt, y: BigUInt) {
|
|
|
|
|
|
+ public static func %=(x: inout CS.BigUInt, y: CS.BigUInt) {
|
|
let shift = y.leadingZeroBitCount
|
|
let shift = y.leadingZeroBitCount
|
|
x.formRemainder(dividingBy: y << shift, normalizedBy: shift)
|
|
x.formRemainder(dividingBy: y << shift, normalizedBy: shift)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-extension BigInt {
|
|
|
|
|
|
+extension CS.BigInt {
|
|
/// Divide this integer by `y` and return the resulting quotient and remainder.
|
|
/// Divide this integer by `y` and return the resulting quotient and remainder.
|
|
///
|
|
///
|
|
/// - Requires: `y > 0`
|
|
/// - Requires: `y > 0`
|
|
/// - Returns: `(quotient, remainder)` where `quotient = floor(self/y)`, `remainder = self - quotient * y`
|
|
/// - Returns: `(quotient, remainder)` where `quotient = floor(self/y)`, `remainder = self - quotient * y`
|
|
/// - Complexity: O(count^2)
|
|
/// - Complexity: O(count^2)
|
|
- public func quotientAndRemainder(dividingBy y: BigInt) -> (quotient: BigInt, remainder: BigInt) {
|
|
|
|
|
|
+ public func quotientAndRemainder(dividingBy y: CS.BigInt) -> (quotient: CS.BigInt, remainder: CS.BigInt) {
|
|
var a = self.magnitude
|
|
var a = self.magnitude
|
|
var b = y.magnitude
|
|
var b = y.magnitude
|
|
- BigUInt.divide(&a, by: &b)
|
|
|
|
- return (BigInt(sign: self.sign == y.sign ? .plus : .minus, magnitude: a),
|
|
|
|
- BigInt(sign: self.sign, magnitude: b))
|
|
|
|
|
|
+ CS.BigUInt.divide(&a, by: &b)
|
|
|
|
+ return ( CS.BigInt(sign: self.sign == y.sign ? .plus : .minus, magnitude: a),
|
|
|
|
+ CS.BigInt(sign: self.sign, magnitude: b))
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `a` by `b` and return the quotient. Traps if `b` is zero.
|
|
/// Divide `a` by `b` and return the quotient. Traps if `b` is zero.
|
|
- public static func /(a: BigInt, b: BigInt) -> BigInt {
|
|
|
|
- return BigInt(sign: a.sign == b.sign ? .plus : .minus, magnitude: a.magnitude / b.magnitude)
|
|
|
|
|
|
+ public static func /(a: CS.BigInt, b: CS.BigInt) -> CS.BigInt {
|
|
|
|
+ return CS.BigInt(sign: a.sign == b.sign ? .plus : .minus, magnitude: a.magnitude / b.magnitude)
|
|
}
|
|
}
|
|
|
|
|
|
/// Divide `a` by `b` and return the remainder. The result has the same sign as `a`.
|
|
/// Divide `a` by `b` and return the remainder. The result has the same sign as `a`.
|
|
- public static func %(a: BigInt, b: BigInt) -> BigInt {
|
|
|
|
- return BigInt(sign: a.sign, magnitude: a.magnitude % b.magnitude)
|
|
|
|
|
|
+ public static func %(a: CS.BigInt, b: CS.BigInt) -> CS.BigInt {
|
|
|
|
+ return CS.BigInt(sign: a.sign, magnitude: a.magnitude % b.magnitude)
|
|
}
|
|
}
|
|
|
|
|
|
/// Return the result of `a` mod `b`. The result is always a nonnegative integer that is less than the absolute value of `b`.
|
|
/// Return the result of `a` mod `b`. The result is always a nonnegative integer that is less than the absolute value of `b`.
|
|
- public func modulus(_ mod: BigInt) -> BigInt {
|
|
|
|
|
|
+ public func modulus(_ mod: CS.BigInt) -> CS.BigInt {
|
|
let remainder = self.magnitude % mod.magnitude
|
|
let remainder = self.magnitude % mod.magnitude
|
|
- return BigInt(
|
|
|
|
|
|
+ return CS.BigInt(
|
|
self.sign == .minus && !remainder.isZero
|
|
self.sign == .minus && !remainder.isZero
|
|
? mod.magnitude - remainder
|
|
? mod.magnitude - remainder
|
|
: remainder)
|
|
: remainder)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-extension BigInt {
|
|
|
|
|
|
+extension CS.BigInt {
|
|
/// Divide `a` by `b` storing the quotient in `a`.
|
|
/// Divide `a` by `b` storing the quotient in `a`.
|
|
- public static func /=(a: inout BigInt, b: BigInt) { a = a / b }
|
|
|
|
|
|
+ public static func /=(a: inout CS.BigInt, b: CS.BigInt) { a = a / b }
|
|
/// Divide `a` by `b` storing the remainder in `a`.
|
|
/// Divide `a` by `b` storing the remainder in `a`.
|
|
- public static func %=(a: inout BigInt, b: BigInt) { a = a % b }
|
|
|
|
|
|
+ public static func %=(a: inout CS.BigInt, b: CS.BigInt) { a = a % b }
|
|
}
|
|
}
|