diff --git a/Package.swift b/Package.swift index b3d6ffe..19bec32 100644 --- a/Package.swift +++ b/Package.swift @@ -15,22 +15,22 @@ import PackageDescription let package = Package( name: "swift-numerics", products: [ + .library(name: "BigInt", targets: ["BigInt"]), .library(name: "Complex", targets: ["Complex"]), .library(name: "Numerics", targets: ["Numerics"]), .library(name: "Real", targets: ["Real"]), - .library(name: "BigInt", targets: ["BigInt"]), ], dependencies: [ ], targets: [ + .target(name: "BigInt", dependencies: []), .target(name: "Complex", dependencies: ["Real"]), .target(name: "Numerics", dependencies: ["Complex", "Real", "BigInt"]), .target(name: "NumericsShims", dependencies: []), .target(name: "Real", dependencies: ["NumericsShims"]), - .target(name: "BigInt", dependencies: []), + .testTarget(name: "BigIntTests", dependencies: ["BigInt"]), .testTarget(name: "ComplexTests", dependencies: ["Complex", "NumericsShims"]), .testTarget(name: "RealTests", dependencies: ["Real"]), - .testTarget(name: "BigIntTests", dependencies: ["BigInt"]), ] ) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index 48b8fea..0b3eba6 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -1,750 +1,752 @@ +//===--- BigInt.swift ----------------------------------------*- swift -*-===// // -// BigInt.swift -// swift-numerics +// This source file is part of the Swift Numerics open source project // -// Created by Robert Thompson on 2/4/19. +// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception // +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// -public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringConvertible, LosslessStringConvertible, Hashable { - - public private(set) var words: Array +public struct BigInt: SignedInteger, LosslessStringConvertible { + public private(set) var words: [UInt] - public init?(_ description: String) { - guard let firstCharacter = description.first else { return nil } - - var description = description - var isNegative: Bool = false - if description.count > 1 { - if firstCharacter == "-" { - isNegative = true - description.removeFirst() - } else if firstCharacter == "+" { - description.removeFirst() - } - } - - let digits = Set("0123456789") - guard description.allSatisfy({ digits.contains($0) }) else { return nil } - - var result: BigInt = 0 - for (i, char) in description.reversed().enumerated() { - // We're ok to force unwrap here because of the guard above. - result += BigInt(char.wholeNumberValue!) * pow(10, BigInt(i)) - } - - if isNegative { - result = -result - } - - words = result.words - } - - public init?(exactly source: T) where T : BinaryInteger { - self.init(source) - } - - public init(bitPattern source: T) where T : BinaryInteger { - words = Words(source.words) - } - - public var magnitude: BigInt { - if _isNegative { - return -self - } - return self - } - - public static func <<= (lhs: inout BigInt, rhs: RHS) where RHS : BinaryInteger { - if rhs.signum() < 0 { - lhs >>= rhs.magnitude - return - } - - let wordLength = UInt.bitWidth - let isNegative = lhs._isNegative - - let (fullWords, remainder) = rhs.quotientAndRemainder(dividingBy: RHS(wordLength)) - lhs.words = Words(repeating: 0, count: Int(fullWords)) + lhs.words + [isNegative ? UInt.max : 0] - - if remainder > 0 { - var value: UInt = 0 - for i in 0..> (wordLength - Int(remainder)) - lhs.words[i] <<= Int(remainder) - if i > 0 { - lhs.words[i] &= UInt.max << Int(remainder) - lhs.words[i] |= value - } - - value = temp - } - - if isNegative { - lhs.words[lhs.words.count - 1] |= (UInt.max << Int(remainder)) - } - } - - BigInt._dropExcessWords(words: &lhs.words) + public init?(_ description: String) { + var description = description + let isNegative = description.hasPrefix("-") + if isNegative || description.hasPrefix("+") { + _ = description.removeFirst() } - public static func >>= (lhs: inout BigInt, rhs: RHS) where RHS : BinaryInteger { - if rhs.signum() < 0 { - lhs <<= rhs.magnitude - return - } - - let wordLength = UInt.bitWidth - let isNegative = lhs._isNegative + let digits = Set("0123456789") + guard description.allSatisfy({ digits.contains($0) }) else { return nil } - let (fullWords, remainder) = rhs.quotientAndRemainder(dividingBy: RHS(wordLength)) - if fullWords < lhs.words.count { - lhs.words.removeFirst(Int(fullWords)) + var result: BigInt = 0 + for (i, char) in description.reversed().enumerated() { + // We're ok to force unwrap here because of the guard above. + result += BigInt(char.wholeNumberValue!) * pow(10, BigInt(i)) + } + + if isNegative { + result = -result + } + + words = result.words + } + + public init?(exactly source: T) where T: BinaryInteger { + self.init(source) + } + + public init(bitPattern source: T) where T: BinaryInteger { + words = Words(source.words) + } + + public var magnitude: BigInt { + if _isNegative { + return -self + } + return self + } + + public static func <<= (lhs: inout BigInt, rhs: RHS) where RHS: BinaryInteger { + if rhs.signum() < 0 { + lhs >>= rhs.magnitude + return + } + + let wordLength = UInt.bitWidth + let isNegative = lhs._isNegative + + let (fullWords, remainder) = rhs.quotientAndRemainder(dividingBy: RHS(wordLength)) + lhs.words = Words(repeating: 0, count: Int(fullWords)) + lhs.words + [isNegative ? UInt.max : 0] + + if remainder > 0 { + var value: UInt = 0 + for i in 0 ..< lhs.words.count { + let temp = lhs.words[i] >> (wordLength - Int(remainder)) + lhs.words[i] <<= Int(remainder) + if i > 0 { + lhs.words[i] &= UInt.max << Int(remainder) + lhs.words[i] |= value + } + + value = temp + } + + if isNegative { + lhs.words[lhs.words.count - 1] |= (UInt.max << Int(remainder)) + } + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + public static func >>= (lhs: inout BigInt, rhs: RHS) where RHS: BinaryInteger { + if rhs.signum() < 0 { + lhs <<= rhs.magnitude + return + } + + let wordLength = UInt.bitWidth + let isNegative = lhs._isNegative + + let (fullWords, remainder) = rhs.quotientAndRemainder(dividingBy: RHS(wordLength)) + if fullWords < lhs.words.count { + lhs.words.removeFirst(Int(fullWords)) + } else { + lhs = isNegative ? -1 : 0 + return + } + + if remainder > 0 { + let mask = ~(UInt.max << remainder) + var value: UInt = 0 + for i in stride(from: lhs.words.count - 1, through: 0, by: -1) { + let temp = lhs.words[i] & mask + lhs.words[i] >>= remainder + lhs.words[i] |= (value << (UInt.bitWidth - Int(remainder))) + value = temp + } + } + + if isNegative { + lhs.words[lhs.words.count - 1] |= (UInt.max << (UInt.bitWidth - Int(remainder))) + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + @inlinable + public func quotientAndRemainder(dividingBy rhs: BigInt) -> (quotient: BigInt, remainder: BigInt) { + return BigInt._div(lhs: self, rhs: rhs) + } + + @inlinable + public func signum() -> BigInt { + if _isNegative { + return -1 + } else if self == 0 { + return 0 + } + + return 1 + } + + @inlinable + public static prefix func ~ (x: BigInt) -> BigInt { + let newWords = x.words.map { ~$0 } + return BigInt(words: Words(newWords)) + } + + @inlinable + public static prefix func - (x: BigInt) -> BigInt { + var newWords = x.words.map { ~$0 } + let carry: UInt = 1 + for i in 0 ..< newWords.count { + let isOverflow: Bool + (newWords[i], isOverflow) = newWords[i].addingReportingOverflow(carry) + if !isOverflow { + break + } + } + + return BigInt(words: Words(newWords)) + } + + @usableFromInline + internal var _isNegative: Bool { + words[words.endIndex - 1] > Int.max + } + + public init(_ source: T) where T: BinaryFloatingPoint { + precondition( + !(source.isNaN || source.isInfinite), + "\(type(of: source)) value cannot be converted to BigInt because it is either infinite or NaN" + ) + + let isNegative = source < 0.0 + var float = isNegative ? -source : source + + if let _ = UInt(exactly: T.greatestFiniteMagnitude) { + words = [UInt(float)] + } else { + var words = Words() + let radix = T(sign: .plus, exponent: T.Exponent(UInt.bitWidth), significand: 1) + repeat { + let digit = UInt(float.truncatingRemainder(dividingBy: radix)) + words.append(digit) + float = (float / radix).rounded(.towardZero) + } while float != 0 + + self.words = words + } + + if isNegative { + self = -self + } + } + + public init(_ source: T) where T: BinaryInteger { + if source >= 0, source < BigInt._digits.count { + self = BigInt._digits[Int(source)] + } else { + words = Words(source.words) + if source > Int.max { + words.append(0) + } + } + } + + public init(clamping source: T) where T: BinaryInteger { + self.init(source) + } + + public init(truncatingIfNeeded source: T) where T: BinaryInteger { + words = Words(source.words) + } + + @inlinable + public static func - (lhs: BigInt, rhs: BigInt) -> BigInt { + var result = lhs + result -= rhs + return result + } + + public init?(exactly source: T) where T: BinaryFloatingPoint { + if (source.isNaN || source.isInfinite) || + (source.rounded(.towardZero) != source) { + return nil + } + + self.init(source) + } + + @usableFromInline + internal init(words: Words) { + self.words = words + } + + public static func += (lhs: inout BigInt, rhs: BigInt) { + if lhs.words.count == 1, rhs.words.count == 1 { + let lhsWord = lhs.words[0] + let rhsWord = rhs.words[0] + + let (result, isOverflow) = lhsWord.addingReportingOverflow(rhsWord) + + if !isOverflow { + lhs.words[0] = result + return + } + let knownNegativeResult = lhsWord > Int.max && rhsWord > Int.max + + if lhsWord > Int.max || rhsWord > Int.max, !knownNegativeResult { + // positive + negative is always smaller, so overflow is a red herring + lhs.words[0] = result + return + } + } + + var isOverflow = false + + var rhsWords = rhs.words + + lhs.words.append(lhs._isNegative ? UInt.max : 0) + rhsWords.append(rhs._isNegative ? UInt.max : 0) + + BigInt._signExtend(lhsWords: &lhs.words, rhsWords: &rhsWords) + var temp: UInt = 0 + for index in 0 ..< lhs.words.count { + var carryOverflow = false + + if isOverflow { + (temp, carryOverflow) = rhsWords[index].addingReportingOverflow(1) + } else { + temp = rhsWords[index] + } + + (lhs.words[index], isOverflow) = lhs.words[index].addingReportingOverflow(temp) + + isOverflow = carryOverflow || isOverflow + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + public init(integerLiteral value: Int) { + if value >= 0, value < BigInt._digits.count { + self = BigInt._digits[value] + } else { + words = [UInt(bitPattern: value)] + } + } + + public static var isSigned: Bool { true } + + public var bitWidth: Int { words.count * MemoryLayout.size * 8 } + + public var trailingZeroBitCount: Int { words.first?.trailingZeroBitCount ?? 0 } + + @inlinable + public static func < (lhs: BigInt, rhs: BigInt) -> Bool { + let lhsNegative = lhs._isNegative + let rhsNegative = rhs._isNegative + + if lhsNegative && !rhsNegative { return true } + if rhsNegative && !lhsNegative { return false } + + if (lhsNegative && rhsNegative) || (!lhsNegative && !rhsNegative) { + if lhs.words.count > rhs.words.count { + return lhsNegative ? true : false + } + if lhs.words.count < rhs.words.count { + return lhsNegative ? false : true + } + + for i in stride(from: lhs.words.count - 1, through: 0, by: -1) { + if lhs.words[i] > rhs.words[i] { return false } + } + } + + return true + } + + @inlinable + public static func == (lhs: BigInt, rhs: BigInt) -> Bool { + lhs.words == rhs.words + } + + @inlinable + public static func != (lhs: BigInt, rhs: BigInt) -> Bool { + !(lhs == rhs) + } + + @inlinable + public static func / (lhs: BigInt, rhs: BigInt) -> BigInt { + let (result, _) = _div(lhs: lhs, rhs: rhs) + return result + } + + private static func findQhat(high: UInt, low: UInt.Magnitude, divisor: UInt, nextVdigit: UInt, nextUdigit: UInt) -> UInt { + var (qhat, rhat) = divisor.dividingFullWidth((high, low)) + + if high >= divisor { // This means qhat >= b + qhat = UInt.max + } + + let (tempHigh, tempLow) = qhat.multipliedFullWidth(by: nextVdigit) + var (rtempHigh, rtempLow) = rhat.multipliedFullWidth(by: UInt.max) + var overflow = false + (rtempLow, overflow) = rtempLow.addingReportingOverflow(1) + if overflow { + rtempHigh += 1 + } + + (rtempLow, overflow) = rtempLow.addingReportingOverflow(nextUdigit) + if overflow { + rtempHigh += 1 + } + + while true { + if (tempHigh > rtempHigh) || ((tempHigh == rtempHigh) && (tempLow > rtempLow)) { + qhat -= 1 + (rhat, overflow) = rhat.addingReportingOverflow(divisor) + if !overflow { + continue } else { - lhs = isNegative ? -1 : 0 - return + break + } + } else { + break + } + } + + return qhat + } + + @usableFromInline + internal static func _div(lhs: BigInt, rhs: BigInt) -> (quotient: BigInt, remainder: BigInt) { + precondition(rhs != _digits[0], "Division by zero error!") + + if lhs.words.count == 1, rhs.words.count == 1 { + let (quot, rem) = Int(bitPattern: lhs.words[0]).quotientAndRemainder(dividingBy: Int(bitPattern: rhs.words[0])) + return (BigInt(words: [UInt(bitPattern: quot)]), BigInt(words: [UInt(bitPattern: rem)])) + } + + let lhsIsNeg = lhs._isNegative + let rhsIsNeg = rhs._isNegative + + var lhsWords = lhsIsNeg ? (-lhs).words : lhs.words + var rhsWords = rhsIsNeg ? (-rhs).words : rhs.words + + while rhsWords[rhsWords.endIndex - 1] == 0, rhsWords.count > 2 { + rhsWords.removeLast() + } + + if rhsWords.count > lhsWords.count { return (0, lhs) } + + if rhsWords.count == 1 { + rhsWords.append(0) + } + + func normalize(x: UInt) -> UInt { + #if arch(arm64) || arch(x86_64) + if x == 0 { + return 64 + } + var x = UInt64(x) + var n: UInt = 0 + if x <= 0x0000_0000_FFFF_FFFF { + n += 32 + x <<= 32 + } + if x <= 0x0000_FFFF_FFFF_FFFF { + n += 16 + x <<= 16 + } + if x <= 0x00FF_FFFF_FFFF_FFFF { + n += 8 + x <<= 8 + } + if x <= 0x0FFF_FFFF_FFFF_FFFF { + n += 4 + x <<= 4 + } + if x <= 0x3FFF_FFFF_FFFF_FFFF { + n += 2 + x <<= 2 + } + if x <= 0x7FFF_FFFF_FFFF_FFFF { + n += 1 } - if remainder > 0 { - let mask = ~(UInt.max << remainder) - var value: UInt = 0 - for i in stride(from: lhs.words.count - 1, through: 0, by: -1) { - let temp = lhs.words[i] & mask - lhs.words[i] >>= remainder - lhs.words[i] |= (value << (UInt.bitWidth - Int(remainder))) - value = temp - } + return n + #else + if x == 0 { + return 32 } - if isNegative { - lhs.words[lhs.words.count - 1] |= (UInt.max << (UInt.bitWidth - Int(remainder))) + var x = x + var n: UInt = 0 + if x <= 0x0000_FFFF { + n += 16 + x <<= 16 + } + if x <= 0x00FF_FFFF { + n += 8 + x <<= 8 + } + if x <= 0x0FFF_FFFF { + n += 4 + x <<= 4 + } + if x <= 0x3FFF_FFFF { + n += 2 + x <<= 2 + } + if x <= 0x7FFF_FFFF { + n += 1 } - BigInt._dropExcessWords(words: &lhs.words) + return n + #endif } - - @inlinable - public func quotientAndRemainder(dividingBy rhs: BigInt) -> (quotient: BigInt, remainder: BigInt) { - return BigInt._div(lhs: self, rhs: rhs) - } - - @inlinable - public func signum() -> BigInt { - return _isNegative ? -1 : 1 - } - - @inlinable - public static prefix func ~ (x: BigInt) -> BigInt { - let newWords = x.words.map { ~$0 } - return BigInt(words: Words(newWords)) - } - - @inlinable - public static prefix func - (x: BigInt) -> BigInt { - var newWords = x.words.map { ~$0 } - let carry: UInt = 1 - for i in 0.. Int.max - } - - public init(_ source: T) where T : BinaryFloatingPoint { - precondition( - !(source.isNaN || source.isInfinite), - "\(type(of: source)) value cannot be converted to BigInt because it is either infinite or NaN" - ) - - let isNegative = source < 0.0 - var float = isNegative ? -source : source - - if let _ = UInt(exactly: T.greatestFiniteMagnitude) { - self.words = [UInt(float)] - } else { - var words = Words() - let radix = T(sign: .plus, exponent: T.Exponent(UInt.bitWidth), significand: 1) - repeat { - let digit = UInt(float.truncatingRemainder(dividingBy: radix)) - words.append(digit) - float = (float / radix).rounded(.towardZero) - } while float != 0 - self.words = words + if lhsWords.count < rhsWords.count { + for _ in 0 ..< (rhsWords.count - lhsWords.count) { + lhsWords.append(0) + } + } + + let m = lhsWords.count + let n = rhsWords.count + + let bitWidth = UInt(UInt.bitWidth) + + let s = normalize(x: rhsWords[n - 1]) + let rn = UnsafeMutablePointer.allocate(capacity: n) + rn.initialize(repeating: 0, count: n) + defer { rn.deallocate() } + for i in (1 ... (n - 1)).reversed() { + rn[i] = (rhsWords[i] << s) | (rhsWords[i - 1] >> (bitWidth - s)) + } + rn[0] <<= s + + let ln = UnsafeMutablePointer.allocate(capacity: m + n + 1) + ln.initialize(repeating: 0, count: m + n + 1) + defer { ln.deallocate() } + ln[m] = lhsWords[m - 1] >> (bitWidth - s) + for i in (1 ... (m - 1)).reversed() { + ln[i] = (lhsWords[i] << s) | (lhsWords[i - 1] >> (bitWidth - s)) + } + ln[0] <<= s + + let resultSize = m + 1 + var quot = Words(repeating: 0, count: resultSize) + + for j in (0 ... m).reversed() { + let qhat = findQhat(high: ln[j + n], low: UInt.Magnitude(ln[j + n - 1]), divisor: rn[n - 1], nextVdigit: rn[n - 2], nextUdigit: ln[j + n - 2]) + + var carry: UInt = 0 + var isOverflow = false + var borrow: UInt = 0 + var underflow = false + for i in 0 ..< n { + if borrow > 0 { + (ln[i + j], underflow) = ln[i + j].subtractingReportingOverflow(borrow) + borrow = underflow ? 1 : 0 } - if isNegative { - self = -self + var (pHigh, pLow) = qhat.multipliedFullWidth(by: rn[i]) + (pLow, isOverflow) = pLow.addingReportingOverflow(carry) + if isOverflow { + pHigh += 1 } - } - - public init(_ source: T) where T : BinaryInteger { - if source >= 0 && source < BigInt._digits.count { - self = BigInt._digits[Int(source)] - } else { - words = Words(source.words) - if source > Int.max { - words.append(0) - } - } - } - - public init(clamping source: T) where T : BinaryInteger { - self.init(source) - } - - public init(truncatingIfNeeded source: T) where T : BinaryInteger { - words = Words(source.words) - } - - @inlinable - public static func - (lhs: BigInt, rhs: BigInt) -> BigInt { - var result = lhs - result -= rhs - return result - } - - public init?(exactly source: T) where T : BinaryFloatingPoint { - if (source.isNaN || source.isInfinite) || - (source.rounded(.towardZero) != source) { - return nil - } - - self.init(source) - } - - @usableFromInline - internal init(words: Words) { - self.words = words - } - - public static func += (lhs: inout BigInt, rhs: BigInt) { - if lhs.words.count == 1 && rhs.words.count == 1 { - let lhsWord = lhs.words[0] - let rhsWord = rhs.words[0] - - let (result, isOverflow) = lhsWord.addingReportingOverflow(rhsWord) - - if !isOverflow { - lhs.words[0] = result - return - } - let knownNegativeResult = lhsWord > Int.max && rhsWord > Int.max - - if (lhsWord > Int.max || rhsWord > Int.max) && !knownNegativeResult { - // positive + negative is always smaller, so overflow is a red herring - lhs.words[0] = result - return - } - } - - var isOverflow = false - - var rhsWords = rhs.words - - lhs.words.append(lhs._isNegative ? UInt.max : 0) - rhsWords.append(rhs._isNegative ? UInt.max : 0) - - BigInt._signExtend(lhsWords: &lhs.words, rhsWords: &rhsWords) - var temp: UInt = 0 - for index in 0..= 0 && value < BigInt._digits.count { - self = BigInt._digits[value] - } else { - words = [UInt(bitPattern: value)] - } - } - - public static var isSigned: Bool { return true } - - public var bitWidth: Int { return words.count * MemoryLayout.size * 8 } - - public var trailingZeroBitCount: Int { return words.first?.trailingZeroBitCount ?? 0 } - - @inlinable - public static func < (lhs: BigInt, rhs: BigInt) -> Bool { - let lhsNegative = lhs._isNegative - let rhsNegative = rhs._isNegative - - if lhsNegative && !rhsNegative { return true } - if rhsNegative && !lhsNegative { return false } - - if (lhsNegative && rhsNegative) || (!lhsNegative && !rhsNegative) { - if lhs.words.count > rhs.words.count { - return lhsNegative ? true : false - } - if lhs.words.count < rhs.words.count { - return lhsNegative ? false : true - } - - for i in stride(from: lhs.words.count - 1, through: 0, by: -1) { - if lhs.words[i] > rhs.words[i] { return false } - } - } - - return true - } - - @inlinable - public static func ==(lhs: BigInt, rhs: BigInt) -> Bool { - return lhs.words == rhs.words - } - - @inlinable - public static func !=(lhs: BigInt, rhs: BigInt) -> Bool { - return !(lhs == rhs) - } - - @inlinable - public static func / (lhs: BigInt, rhs: BigInt) -> BigInt { - let (result, _) = _div(lhs: lhs, rhs: rhs) - return result - } - - private static func findQhat(high: UInt, low: UInt.Magnitude, divisor: UInt, nextVdigit: UInt, nextUdigit: UInt) -> UInt { - var (qhat, rhat) = divisor.dividingFullWidth((high, low)) - - if high >= divisor {// This means qhat >= b - qhat = UInt.max - } - - let (tempHigh, tempLow) = qhat.multipliedFullWidth(by: nextVdigit) - var (rtempHigh, rtempLow) = rhat.multipliedFullWidth(by: UInt.max) - var overflow = false - (rtempLow, overflow) = rtempLow.addingReportingOverflow(1) - if overflow { - rtempHigh += 1 - } - - (rtempLow, overflow) = rtempLow.addingReportingOverflow(nextUdigit) - if overflow { - rtempHigh += 1 - } - - while true { - if (tempHigh > rtempHigh) || ((tempHigh == rtempHigh) && (tempLow > rtempLow)) { - qhat -= 1 - (rhat, overflow) = rhat.addingReportingOverflow(divisor) - if !overflow { - continue - } else { - break - } - } else { - break - } - } - - return qhat - } - - @usableFromInline - internal static func _div(lhs: BigInt, rhs: BigInt) -> (quotient: BigInt, remainder: BigInt) { - precondition(rhs != _digits[0], "Division by zero error!") - - if lhs.words.count == 1 && rhs.words.count == 1 { - let (quot, rem) = Int(bitPattern: lhs.words[0]).quotientAndRemainder(dividingBy: Int(bitPattern: rhs.words[0])) - return (BigInt(words: [UInt(bitPattern: quot)]), BigInt(words: [UInt(bitPattern: rem)])) - } - - let lhsIsNeg = lhs._isNegative - let rhsIsNeg = rhs._isNegative - - var lhsWords = lhsIsNeg ? (-lhs).words : lhs.words - var rhsWords = rhsIsNeg ? (-rhs).words : rhs.words - - while rhsWords[rhsWords.endIndex - 1] == 0 && rhsWords.count > 2 { - rhsWords.removeLast() - } - - if rhsWords.count > lhsWords.count { return (0, lhs) } - - if rhsWords.count == 1 { - rhsWords.append(0) - } - - func normalize(x: UInt) -> UInt { - #if arch(arm64) || arch(x86_64) - if x == 0 { - return 64 - } - var x = UInt64(x) - var n: UInt = 0 - if x <= 0x00000000FFFFFFFF { - n += 32 - x <<= 32 - } - if x <= 0x0000FFFFFFFFFFFF { - n += 16 - x <<= 16 - } - if x <= 0x00FFFFFFFFFFFFFF { - n += 8 - x <<= 8 - } - if x <= 0x0FFFFFFFFFFFFFFF { - n += 4 - x <<= 4 - } - if x <= 0x3FFFFFFFFFFFFFFF { - n += 2 - x <<= 2 - } - if x <= 0x7FFFFFFFFFFFFFFF { - n += 1 - } - - return n - #else - if x == 0 { - return 32 - } - - var x = x - var n: UInt = 0 - if x <= 0x0000FFFF { - n += 16 - x <<= 16 - } - if x <= 0x00FFFFFF { - n += 8 - x <<= 8 - } - if x <= 0x0FFFFFFF { - n += 4 - x <<= 4 - } - if x <= 0x3FFFFFFF { - n += 2 - x <<= 2 - } - if x <= 0x7FFFFFFF { - n += 1 - } - - return n - #endif - } - - if lhsWords.count < rhsWords.count { - for _ in 0..<(rhsWords.count - lhsWords.count) { - lhsWords.append(0) - } - } - - let m = lhsWords.count - let n = rhsWords.count - - let bitWidth = UInt(UInt.bitWidth) - - let s = normalize(x: rhsWords[n - 1]) - let rn = UnsafeMutablePointer.allocate(capacity: n) - rn.initialize(repeating: 0, count: n) - defer { rn.deallocate() } - for i in (1...(n - 1)).reversed() { - rn[i] = (rhsWords[i] << s) | (rhsWords[i - 1] >> (bitWidth - s)) - } - rn[0] <<= s - - let ln = UnsafeMutablePointer.allocate(capacity: m + n + 1) - ln.initialize(repeating: 0, count: m + n + 1) - defer { ln.deallocate() } - ln[m] = lhsWords[m - 1] >> (bitWidth - s) - for i in (1...(m - 1)).reversed() { - ln[i] = (lhsWords[i] << s) | (lhsWords[i - 1] >> (bitWidth - s)) - } - ln[0] <<= s - - let resultSize = m + 1 - var quot = Words(repeating: 0, count: resultSize) - - for j in (0...m).reversed() { - let qhat = findQhat(high: ln[j + n], low: UInt.Magnitude(ln[j + n - 1]), divisor: rn[n - 1], nextVdigit: rn[n - 2], nextUdigit: ln[j + n - 2]) - - var carry: UInt = 0 - var isOverflow = false - var borrow: UInt = 0 - var underflow = false - for i in 0.. 0 { - (ln[i + j], underflow) = ln[i + j].subtractingReportingOverflow(borrow) - borrow = underflow ? 1 : 0 - } - - var (pHigh, pLow) = qhat.multipliedFullWidth(by: rn[i]) - (pLow, isOverflow) = pLow.addingReportingOverflow(carry) - if isOverflow { - pHigh += 1 - } - - (ln[i + j], underflow) = ln[i + j].subtractingReportingOverflow(pLow) - if underflow { - borrow += 1 - } - - carry = pHigh - } - - (ln[j + n], underflow) = ln[j + n].subtractingReportingOverflow(carry + borrow) - - if underflow { - let newQhat = qhat - 1 - - carry = 0 - var total: UInt = 0 - for i in 0..> s) | ln[i + 1] << (bitWidth - s) - } - rem[n - 1] = ln[n - 1] >> s - - BigInt._dropExcessWords(words: ") - BigInt._dropExcessWords(words: &rem) - - return (BigInt(words: quot), BigInt(words: rem)) - } - - @inlinable - public static func /= (lhs: inout BigInt, rhs: BigInt) { - lhs = lhs / rhs - } - - @inlinable - public static func % (lhs: BigInt, rhs: BigInt) -> BigInt { - let (_, result) = _div(lhs: lhs, rhs: rhs) - - return result - } - - @inlinable - public static func %= (lhs: inout BigInt, rhs: BigInt) { - lhs = lhs % rhs - } - - public static func * (lhs: BigInt, rhs: BigInt) -> BigInt { - let lhsIsNeg = lhs.words[lhs.words.endIndex - 1] > Int.max - let rhsIsNeg = rhs.words[rhs.words.endIndex - 1] > Int.max - - let lhsWords = lhsIsNeg ? (-lhs).words : lhs.words - let rhsWords = rhsIsNeg ? (-rhs).words : rhs.words - - let count = lhsWords.count + rhsWords.count + 1 - var newWords = Words(repeating: 0, count: count) - - for i in 0.. BigInt { - var result = lhs - result += rhs - return result - } - - @inlinable - public static func -= (lhs: inout BigInt, rhs: BigInt) { - lhs += -rhs - } - - private static func _signExtend(lhsWords: inout Words, rhsWords: inout Words) { - let lhsIsNeg = (lhsWords.last ?? 0) >> (UInt.bitWidth - Int(1)) == 1 - let rhsIsNeg = (rhsWords.last ?? 0) >> (UInt.bitWidth - Int(1)) == 1 - - if lhsWords.count > rhsWords.count { - for _ in 0..<(lhsWords.count - rhsWords.count) { - rhsIsNeg ? rhsWords.append(UInt.max) : rhsWords.append(0) - } - } else if rhsWords.count > lhsWords.count { - for _ in 0..<(rhsWords.count - lhsWords.count) { - lhsIsNeg ? lhsWords.append(UInt.max) : lhsWords.append(0) - } - } - } - - private static func _dropExcessWords(words: inout Words) { - while words.count > 1 && words[words.endIndex - 1] == 0 { - if words[words.endIndex - 2] <= Int.max { - words.removeLast() - } else { - break - } - } - - while words.count > 1 && words[words.endIndex - 1] == UInt.max { - if words[words.endIndex - 2] > Int.max { - words.removeLast() - } else { - break - } - } - } - - public var description: String { - var result = "" - if words.count == 1 { - return Int64(bitPattern: UInt64(words[0])).description - } else { - var next = abs(self) - while next != 0 { - let digit: BigInt - (next, digit) = BigInt._div(lhs: next, rhs: 10) - result += "\(digit.words[0])" - } + (ln[i + j], underflow) = ln[i + j].subtractingReportingOverflow(pLow) + if underflow { + borrow += 1 } - - return (self < 0 ? "-" : "") + String(result.reversed()) + + carry = pHigh + } + + (ln[j + n], underflow) = ln[j + n].subtractingReportingOverflow(carry + borrow) + + if underflow { + let newQhat = qhat - 1 + + carry = 0 + var total: UInt = 0 + for i in 0 ..< n { + (total, isOverflow) = ln[i + j].addingReportingOverflow(carry) + carry = isOverflow ? 1 : 0 + (ln[i + j], isOverflow) = total.addingReportingOverflow(rn[i]) + if carry == 0 { carry = isOverflow ? 1 : 0 } + } + ln[j + n] += carry + + quot[j] = newQhat + } else { + quot[j] = qhat + } } - - private static let _digits = { - (0...10).map { BigInt(words: [UInt(bitPattern: $0)]) } - }() + + var rem = Words(repeating: 0, count: n) + + for i in 0 ..< (n - 1) { + rem[i] = (ln[i] >> s) | ln[i + 1] << (bitWidth - s) + } + rem[n - 1] = ln[n - 1] >> s + + BigInt._dropExcessWords(words: ") + BigInt._dropExcessWords(words: &rem) + + return (BigInt(words: quot), BigInt(words: rem)) + } + + @inlinable + public static func /= (lhs: inout BigInt, rhs: BigInt) { + lhs = lhs / rhs + } + + @inlinable + public static func % (lhs: BigInt, rhs: BigInt) -> BigInt { + let (_, result) = _div(lhs: lhs, rhs: rhs) + + return result + } + + @inlinable + public static func %= (lhs: inout BigInt, rhs: BigInt) { + lhs = lhs % rhs + } + + public static func * (lhs: BigInt, rhs: BigInt) -> BigInt { + let lhsIsNeg = lhs.words[lhs.words.endIndex - 1] > Int.max + let rhsIsNeg = rhs.words[rhs.words.endIndex - 1] > Int.max + + let lhsWords = lhsIsNeg ? (-lhs).words : lhs.words + let rhsWords = rhsIsNeg ? (-rhs).words : rhs.words + + let count = lhsWords.count + rhsWords.count + 1 + var newWords = Words(repeating: 0, count: count) + + for i in 0 ..< rhsWords.count { + var carry: UInt = 0 + var digit: UInt = 0 + + var lastJ: Int = 0 + for j in i ..< (lhsWords.count + i) { + var (high, low) = rhsWords[i].multipliedFullWidth(by: lhsWords[j - i]) + var isOverflow: Bool + (digit, isOverflow) = low.addingReportingOverflow(newWords[j]) + if isOverflow { + high += 1 + } + + (digit, isOverflow) = digit.addingReportingOverflow(carry) + if isOverflow { + high += 1 + } + + carry = high + newWords[j] = digit + lastJ = j + } + + if carry != 0 { + let isOverflow: Bool + (digit, isOverflow) = newWords[lastJ + 1].addingReportingOverflow(carry) + if isOverflow { + carry = 1 + } + newWords[lastJ + 1] = digit + } + } + + for i in stride(from: count - 1, through: 1, by: -1) { + if newWords[i] == 0, newWords[i - 1] <= Int.max { + newWords.removeLast() + } else { + break + } + } + + if lhsIsNeg || rhsIsNeg, !(lhsIsNeg && rhsIsNeg) { + return -BigInt(words: newWords) + } + + return BigInt(words: newWords) + } + + @inlinable + public static func *= (lhs: inout BigInt, rhs: BigInt) { + lhs = lhs * rhs + } + + public static func &= (lhs: inout BigInt, rhs: BigInt) { + var rhsWords = rhs.words + BigInt._signExtend(lhsWords: &lhs.words, rhsWords: &rhsWords) + + for i in 0 ..< rhsWords.count { + lhs.words[i] &= rhsWords[i] + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + public static func |= (lhs: inout BigInt, rhs: BigInt) { + var rhsWords = rhs.words + BigInt._signExtend(lhsWords: &lhs.words, rhsWords: &rhsWords) + + for i in 0 ..< rhsWords.count { + lhs.words[i] |= rhsWords[i] + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + public static func ^= (lhs: inout BigInt, rhs: BigInt) { + var rhsWords = rhs.words + BigInt._signExtend(lhsWords: &lhs.words, rhsWords: &rhsWords) + + for i in 0 ..< rhsWords.count { + lhs.words[i] &= rhsWords[i] + } + + BigInt._dropExcessWords(words: &lhs.words) + } + + @inlinable + public static func + (lhs: BigInt, rhs: BigInt) -> BigInt { + var result = lhs + result += rhs + return result + } + + @inlinable + public static func -= (lhs: inout BigInt, rhs: BigInt) { + lhs += -rhs + } + + private static func _signExtend(lhsWords: inout Words, rhsWords: inout Words) { + let lhsIsNeg = (lhsWords.last ?? 0) >> (UInt.bitWidth - Int(1)) == 1 + let rhsIsNeg = (rhsWords.last ?? 0) >> (UInt.bitWidth - Int(1)) == 1 + + if lhsWords.count > rhsWords.count { + for _ in 0 ..< (lhsWords.count - rhsWords.count) { + rhsIsNeg ? rhsWords.append(UInt.max) : rhsWords.append(0) + } + } else if rhsWords.count > lhsWords.count { + for _ in 0 ..< (rhsWords.count - lhsWords.count) { + lhsIsNeg ? lhsWords.append(UInt.max) : lhsWords.append(0) + } + } + } + + private static func _dropExcessWords(words: inout Words) { + while words.count > 1, words[words.endIndex - 1] == 0 { + if words[words.endIndex - 2] <= Int.max { + words.removeLast() + } else { + break + } + } + + while words.count > 1, words[words.endIndex - 1] == UInt.max { + if words[words.endIndex - 2] > Int.max { + words.removeLast() + } else { + break + } + } + } + + public var description: String { + var result = "" + + if words.count == 1 { + return Int64(bitPattern: UInt64(words[0])).description + } else { + var next = abs(self) + while next != 0 { + let digit: BigInt + (next, digit) = BigInt._div(lhs: next, rhs: 10) + result += "\(digit.words[0])" + } + } + + return (self < 0 ? "-" : "") + String(result.reversed()) + } + + private static let _digits = { + (0 ... 10).map { BigInt(words: [UInt(bitPattern: $0)]) } + }() } // inspired by https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms public func pow(_ lhs: BigInt, _ rhs: BigInt) -> BigInt { - let bits_of_n = { - (n: BigInt) -> Array in - var bits: Array = [] - var n = n - while n != 0 { - bits.append(Int(n % 2)) - n /= 2 - } - - return bits + let bits_of_n = { + (n: BigInt) -> [Int] in + var bits: [Int] = [] + var n = n + while n != 0 { + bits.append(Int(n % 2)) + n /= 2 } - - var r: BigInt = 1 - for bit in bits_of_n(rhs).reversed() { - r *= r - if bit == 1 { - r *= lhs - } + + return bits + } + + var r: BigInt = 1 + for bit in bits_of_n(rhs).reversed() { + r *= r + if bit == 1 { + r *= lhs } - - return r + } + + return r } diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index 7e5de1c..d79da0f 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -1,76 +1,101 @@ -import XCTest +//===--- BigIntTests.swift -----------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + import BigInt +import XCTest func fac(_ n: BigInt) -> BigInt { - var result: BigInt = 1 - var count = n - while count >= 1 { - result *= count - count -= 1 - } - - return result + var result: BigInt = 1 + var count = n + while count >= 1 { + result *= count + count -= 1 + } + + return result } final class BigIntTests: XCTestCase { - func testExample() throws { - let bar = BigInt(exactly: -100) - XCTAssertNotNil(bar) - XCTAssert(bar! < 0) - - XCTAssert(-(bar!) > 0) - XCTAssertEqual(-(bar!), BigInt(100)) - - XCTAssertEqual(-BigInt("-1234567890123456789012345678901234567890")!, BigInt("1234567890123456789012345678901234567890")!) - } + func testExample() throws { + let bar = BigInt(exactly: -100) + XCTAssertNotNil(bar) + XCTAssert(bar! < 0) + + XCTAssert(-(bar!) > 0) + XCTAssertEqual(-(bar!), BigInt(100)) + + XCTAssertEqual(-BigInt("-1234567890123456789012345678901234567890")!, BigInt("1234567890123456789012345678901234567890")!) + } + + func testFloatingConversion() { + let bar = BigInt(3.14159) + XCTAssertEqual(bar, BigInt(3)) + let foo = BigInt(exactly: 3.14159) + XCTAssertNil(foo) + + let baz = BigInt(exactly: 2.4e39) + XCTAssertNotNil(baz) + let equal = (baz ?? 0) / BigInt(1e38) == BigInt(24) + XCTAssertEqual(equal, true) + + let infinite = BigInt(exactly: Double.infinity) + XCTAssertNil(infinite) + } + + func testUIntConversion() { + let foo = BigInt(UInt.max) + XCTAssertNotEqual(foo, BigInt(-1)) + + let bar = BigInt(bitPattern: UInt.max) + XCTAssertEqual(bar, BigInt(-1)) + } + + func testComparison() { + let foo = BigInt(-10) + let bar = BigInt(-20) + + XCTAssert(foo > bar) + XCTAssert(bar < foo) + XCTAssert(foo == BigInt(-10)) + + let baz = pow(foo, -bar) + XCTAssertEqual(baz, BigInt("100000000000000000000")!) + } + + func testMath() { + let foo = pow(BigInt(10), 20) + let bar = BigInt("1234567890123456789012345678901234567890")! + + let baz = foo + bar + + XCTAssertEqual(baz, BigInt("1234567890123456789112345678901234567890")!) + + let fooz = foo >> BigInt(10) + XCTAssertEqual(fooz, foo / 1024) + + let barz = BigInt(1) << 64 + XCTAssertEqual(barz, BigInt(UInt.max) + 1) + } + + func testHashable(){ + let foo = BigInt("1234567890123456789012345678901234567890")! + let bar = BigInt("1234567890123456789112345678901234567890")! + let baz: BigInt = 153 - func testFloatingConversion() { - let bar = BigInt(3.14159) - XCTAssertEqual(bar, BigInt(3)) - let foo = BigInt(exactly: 3.14159) - XCTAssertNil(foo) - - let baz = BigInt(exactly: 2.4e39) - XCTAssertNotNil(baz) - let equal = (baz ?? 0) / BigInt(1e38) == BigInt(24) - XCTAssertEqual(equal, true) - - let infinite = BigInt(exactly: Double.infinity) - XCTAssertNil(infinite) - } + let dict = [ foo: "Hello", bar: "World", baz: "!" ] - func testUIntConversion() { - let foo = BigInt(UInt.max) - XCTAssertNotEqual(foo, BigInt(-1)) - - let bar = BigInt(bitPattern: UInt.max) - XCTAssertEqual(bar, BigInt(-1)) - } + let hash = foo.hashValue + print(hash) - func testComparison() { - let foo = BigInt(-10) - let bar = BigInt(-20) - - XCTAssert(foo > bar) - XCTAssert(bar < foo) - XCTAssert(foo == BigInt(-10)) - - let baz = pow(foo, -bar) - XCTAssertEqual(baz, BigInt("100000000000000000000")!) - } - - func testMath() { - let foo = pow(BigInt(10), 20) - let bar = BigInt("1234567890123456789012345678901234567890")! - - let baz = foo + bar - - XCTAssertEqual(baz, BigInt("1234567890123456789112345678901234567890")!) - - let fooz = foo >> BigInt(10) - XCTAssertEqual(fooz, foo / 1024) - - let barz = BigInt(1) << 64 - XCTAssertEqual(barz, BigInt(UInt.max) + 1) - } + XCTAssertEqual(dict[foo]!, "Hello") + XCTAssertEqual(dict[bar]!, "World") + } }