Skip to content

Commit

Permalink
Make usage of StaticBigInt conditionally. Reduce deployment target …
Browse files Browse the repository at this point in the history
…requirements down to macOS 10.13 and iOS 13.
  • Loading branch information
objecthub committed Apr 17, 2024
1 parent e83875a commit 38c649a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 42 deletions.
8 changes: 4 additions & 4 deletions NumberKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -478,7 +478,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -561,7 +561,7 @@
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.NumberKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -579,7 +579,7 @@
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.NumberKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
5 changes: 0 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import PackageDescription

let package = Package(
name: "NumberKit",
platforms: [
.macOS("13.3"),
.iOS(.v13),
.tvOS(.v13)
],
products: [
.library(name: "NumberKit", targets: ["NumberKit"]),
],
Expand Down
52 changes: 30 additions & 22 deletions Sources/NumberKit/BigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,6 @@ public struct BigInt: Hashable,
/// all signed integer arithmetic functions.
extension BigInt: IntegerNumber,
SignedInteger,
ExpressibleByIntegerLiteral,
ExpressibleByStringLiteral {

/// This is a signed type
Expand Down Expand Up @@ -1205,26 +1204,6 @@ extension BigInt: IntegerNumber,
self.init(Int64(value))
}

public init(integerLiteral value: StaticBigInt) {
var uwords = ContiguousArray<UInt32>()
let numWords = (value.bitWidth/UInt.bitWidth) + 1
for index in 0..<numWords {
let myword: UInt64 = UInt64(value[index])
uwords.append(BigInt.loword(myword))
uwords.append(BigInt.hiword(myword))
}
if value.signum() < 0 {
var subOne = true
for index in 0..<uwords.count {
if subOne {
(uwords[index], subOne) = uwords[index].subtractingReportingOverflow(1)
}
uwords[index] = ~uwords[index]
}
}
self.init(words: uwords, negative: value.signum() < 0)
}

public init(stringLiteral value: String) {
if let bigInt = BigInt(from: value) {
self = bigInt
Expand Down Expand Up @@ -1280,6 +1259,36 @@ extension BigInt: IntegerNumber,
}
}

#if canImport(Swift.StaticBigInt)
extension BigInt: ExpressibleByIntegerLiteral {
public init(integerLiteral value: StaticBigInt) {
var uwords = ContiguousArray<UInt32>()
let numWords = (value.bitWidth/UInt.bitWidth) + 1
for index in 0..<numWords {
let myword: UInt64 = UInt64(value[index])
uwords.append(BigInt.loword(myword))
uwords.append(BigInt.hiword(myword))
}
if value.signum() < 0 {
var subOne = true
for index in 0..<uwords.count {
if subOne {
(uwords[index], subOne) = uwords[index].subtractingReportingOverflow(1)
}
uwords[index] = ~uwords[index]
}
}
self.init(words: uwords, negative: value.signum() < 0)
}
}
#else
extension BigInt: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int64) {
self.init(Int64(value))
}
}
#endif

/// Returns the sum of `lhs` and `rhs`
public func +(lhs: BigInt, rhs: BigInt) -> BigInt {
return lhs.plus(rhs)
Expand Down Expand Up @@ -1430,4 +1439,3 @@ public func max(_ fst: BigInt, _ snd: BigInt) -> BigInt {
public func min(_ fst: BigInt, _ snd: BigInt) -> BigInt {
return fst.compare(to: snd) <= 0 ? fst : snd
}

21 changes: 15 additions & 6 deletions Sources/NumberKit/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public enum Integer: IntegerNumber,
Codable,
Sendable,
CustomStringConvertible,
CustomDebugStringConvertible,
ExpressibleByIntegerLiteral {
CustomDebugStringConvertible {
case int(Int64)
case bigInt(BigInt)

Expand Down Expand Up @@ -135,10 +134,6 @@ public enum Integer: IntegerNumber,
}
}

public init(integerLiteral value: StaticBigInt) {
self = Integer(BigInt(integerLiteral: value))
}

public init<T: BinaryInteger>(_ source: T) {
if let value = Int64(exactly: source) {
self = .int(value)
Expand Down Expand Up @@ -642,6 +637,20 @@ public enum Integer: IntegerNumber,
}
}

#if canImport(Swift.StaticBigInt)
extension Integer: ExpressibleByIntegerLiteral {
public init(integerLiteral value: StaticBigInt) {
self = Integer(BigInt(integerLiteral: value))
}
}
#else
extension Integer: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int64) {
self.init(value)
}
}
#endif

/// Returns the maximum of `fst` and `snd`.
public func max(_ fst: Integer, _ snd: Integer) -> Integer {
return fst.compare(to: snd) >= 0 ? fst : snd
Expand Down
6 changes: 6 additions & 0 deletions Tests/NumberKitTests/BigIntTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ class BigIntTests: XCTestCase {
"9234767018865286542195788146602543645444")
}

#if canImport(Swift.StaticBigInt)
func testPlusLiteral() {
let x1: BigInt = 31415926535897932384626433832809454874529274584894502801830108383756373931835
XCTAssertEqual(x1.plus(x1), 62831853071795864769252867665618909749058549169789005603660216767512747863670)
XCTAssertEqual(x1.plus(x1).plus(x1), 94247779607693797153879301498428364623587823754683508405490325151269121795505)
XCTAssertEqual(x1.plus(x1).plus(x1).plus(x1), 125663706143591729538505735331237819498117098339578011207320433535025495727340)
}
#endif

func testMinus() {
let x1: BigInt = "1134345924505409852113434"
Expand All @@ -67,13 +69,15 @@ class BigIntTests: XCTestCase {
XCTAssert(x3.minus(x2) == -1)
}

#if canImport(Swift.StaticBigInt)
func testMinusLiteral() {
let x1: BigInt = -9999998765438765432131415926535897932384626433832809454874529274584894502801830108383756373931835
let x2: BigInt = -19999998765438765432131415926535897932384626433832809454874529274584894502801830108383756373931835
XCTAssertEqual(x1.minus(x2), 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
XCTAssertEqual(x1.plus(x1).plus(x1), -29999996296316296296394247779607693797153879301498428364623587823754683508405490325151269121795505)
XCTAssertEqual(x1.plus(x1).plus(x1).plus(x2), -49999995061755061728525663706143591729538505735331237819498117098339578011207320433535025495727340)
}
#endif

func testTimes() {
let x1: BigInt = "89248574598402980294572048572242498123"
Expand All @@ -84,6 +88,7 @@ class BigIntTests: XCTestCase {
"-75537574353998534693615828134454330968785329792741330257228043492082567")
}

#if canImport(Swift.StaticBigInt)
func testTimesLiteral() {
let x1: BigInt = 89248574598402980294572048572242498123
let x2: BigInt = 84759720710000012134
Expand All @@ -92,6 +97,7 @@ class BigIntTests: XCTestCase {
XCTAssert(x1.times(x3) ==
-75537574353998534693615828134454330968785329792741330257228043492082567)
}
#endif

func testDividedBy() {
let x1: BigInt = "38274945700000001034304000022452452525224002449"
Expand Down
Loading

0 comments on commit 38c649a

Please sign in to comment.