Skip to content

Commit 38c649a

Browse files
committed
Make usage of StaticBigInt conditionally. Reduce deployment target requirements down to macOS 10.13 and iOS 13.
1 parent e83875a commit 38c649a

File tree

6 files changed

+100
-42
lines changed

6 files changed

+100
-42
lines changed

NumberKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@
420420
GCC_WARN_UNUSED_FUNCTION = YES;
421421
GCC_WARN_UNUSED_VARIABLE = YES;
422422
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
423-
MACOSX_DEPLOYMENT_TARGET = 13.3;
423+
MACOSX_DEPLOYMENT_TARGET = 10.13;
424424
MTL_ENABLE_DEBUG_INFO = YES;
425425
ONLY_ACTIVE_ARCH = YES;
426426
SDKROOT = macosx;
@@ -478,7 +478,7 @@
478478
GCC_WARN_UNUSED_FUNCTION = YES;
479479
GCC_WARN_UNUSED_VARIABLE = YES;
480480
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
481-
MACOSX_DEPLOYMENT_TARGET = 13.3;
481+
MACOSX_DEPLOYMENT_TARGET = 10.13;
482482
MTL_ENABLE_DEBUG_INFO = NO;
483483
SDKROOT = macosx;
484484
SWIFT_COMPILATION_MODE = wholemodule;
@@ -561,7 +561,7 @@
561561
"@executable_path/../Frameworks",
562562
"@loader_path/../Frameworks",
563563
);
564-
MACOSX_DEPLOYMENT_TARGET = 13.3;
564+
MACOSX_DEPLOYMENT_TARGET = 10.13;
565565
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.NumberKitTests;
566566
PRODUCT_NAME = "$(TARGET_NAME)";
567567
SWIFT_VERSION = 5.0;
@@ -579,7 +579,7 @@
579579
"@executable_path/../Frameworks",
580580
"@loader_path/../Frameworks",
581581
);
582-
MACOSX_DEPLOYMENT_TARGET = 13.3;
582+
MACOSX_DEPLOYMENT_TARGET = 10.13;
583583
PRODUCT_BUNDLE_IDENTIFIER = net.objecthub.NumberKitTests;
584584
PRODUCT_NAME = "$(TARGET_NAME)";
585585
SWIFT_VERSION = 5.0;

Package.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ import PackageDescription
2323

2424
let package = Package(
2525
name: "NumberKit",
26-
platforms: [
27-
.macOS("13.3"),
28-
.iOS(.v13),
29-
.tvOS(.v13)
30-
],
3126
products: [
3227
.library(name: "NumberKit", targets: ["NumberKit"]),
3328
],

Sources/NumberKit/BigInt.swift

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,6 @@ public struct BigInt: Hashable,
10261026
/// all signed integer arithmetic functions.
10271027
extension BigInt: IntegerNumber,
10281028
SignedInteger,
1029-
ExpressibleByIntegerLiteral,
10301029
ExpressibleByStringLiteral {
10311030

10321031
/// This is a signed type
@@ -1205,26 +1204,6 @@ extension BigInt: IntegerNumber,
12051204
self.init(Int64(value))
12061205
}
12071206

1208-
public init(integerLiteral value: StaticBigInt) {
1209-
var uwords = ContiguousArray<UInt32>()
1210-
let numWords = (value.bitWidth/UInt.bitWidth) + 1
1211-
for index in 0..<numWords {
1212-
let myword: UInt64 = UInt64(value[index])
1213-
uwords.append(BigInt.loword(myword))
1214-
uwords.append(BigInt.hiword(myword))
1215-
}
1216-
if value.signum() < 0 {
1217-
var subOne = true
1218-
for index in 0..<uwords.count {
1219-
if subOne {
1220-
(uwords[index], subOne) = uwords[index].subtractingReportingOverflow(1)
1221-
}
1222-
uwords[index] = ~uwords[index]
1223-
}
1224-
}
1225-
self.init(words: uwords, negative: value.signum() < 0)
1226-
}
1227-
12281207
public init(stringLiteral value: String) {
12291208
if let bigInt = BigInt(from: value) {
12301209
self = bigInt
@@ -1280,6 +1259,36 @@ extension BigInt: IntegerNumber,
12801259
}
12811260
}
12821261

1262+
#if canImport(Swift.StaticBigInt)
1263+
extension BigInt: ExpressibleByIntegerLiteral {
1264+
public init(integerLiteral value: StaticBigInt) {
1265+
var uwords = ContiguousArray<UInt32>()
1266+
let numWords = (value.bitWidth/UInt.bitWidth) + 1
1267+
for index in 0..<numWords {
1268+
let myword: UInt64 = UInt64(value[index])
1269+
uwords.append(BigInt.loword(myword))
1270+
uwords.append(BigInt.hiword(myword))
1271+
}
1272+
if value.signum() < 0 {
1273+
var subOne = true
1274+
for index in 0..<uwords.count {
1275+
if subOne {
1276+
(uwords[index], subOne) = uwords[index].subtractingReportingOverflow(1)
1277+
}
1278+
uwords[index] = ~uwords[index]
1279+
}
1280+
}
1281+
self.init(words: uwords, negative: value.signum() < 0)
1282+
}
1283+
}
1284+
#else
1285+
extension BigInt: ExpressibleByIntegerLiteral {
1286+
public init(integerLiteral value: Int64) {
1287+
self.init(Int64(value))
1288+
}
1289+
}
1290+
#endif
1291+
12831292
/// Returns the sum of `lhs` and `rhs`
12841293
public func +(lhs: BigInt, rhs: BigInt) -> BigInt {
12851294
return lhs.plus(rhs)
@@ -1430,4 +1439,3 @@ public func max(_ fst: BigInt, _ snd: BigInt) -> BigInt {
14301439
public func min(_ fst: BigInt, _ snd: BigInt) -> BigInt {
14311440
return fst.compare(to: snd) <= 0 ? fst : snd
14321441
}
1433-

Sources/NumberKit/Integer.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public enum Integer: IntegerNumber,
3131
Codable,
3232
Sendable,
3333
CustomStringConvertible,
34-
CustomDebugStringConvertible,
35-
ExpressibleByIntegerLiteral {
34+
CustomDebugStringConvertible {
3635
case int(Int64)
3736
case bigInt(BigInt)
3837

@@ -135,10 +134,6 @@ public enum Integer: IntegerNumber,
135134
}
136135
}
137136

138-
public init(integerLiteral value: StaticBigInt) {
139-
self = Integer(BigInt(integerLiteral: value))
140-
}
141-
142137
public init<T: BinaryInteger>(_ source: T) {
143138
if let value = Int64(exactly: source) {
144139
self = .int(value)
@@ -642,6 +637,20 @@ public enum Integer: IntegerNumber,
642637
}
643638
}
644639

640+
#if canImport(Swift.StaticBigInt)
641+
extension Integer: ExpressibleByIntegerLiteral {
642+
public init(integerLiteral value: StaticBigInt) {
643+
self = Integer(BigInt(integerLiteral: value))
644+
}
645+
}
646+
#else
647+
extension Integer: ExpressibleByIntegerLiteral {
648+
public init(integerLiteral value: Int64) {
649+
self.init(value)
650+
}
651+
}
652+
#endif
653+
645654
/// Returns the maximum of `fst` and `snd`.
646655
public func max(_ fst: Integer, _ snd: Integer) -> Integer {
647656
return fst.compare(to: snd) >= 0 ? fst : snd

Tests/NumberKitTests/BigIntTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ class BigIntTests: XCTestCase {
4848
"9234767018865286542195788146602543645444")
4949
}
5050

51+
#if canImport(Swift.StaticBigInt)
5152
func testPlusLiteral() {
5253
let x1: BigInt = 31415926535897932384626433832809454874529274584894502801830108383756373931835
5354
XCTAssertEqual(x1.plus(x1), 62831853071795864769252867665618909749058549169789005603660216767512747863670)
5455
XCTAssertEqual(x1.plus(x1).plus(x1), 94247779607693797153879301498428364623587823754683508405490325151269121795505)
5556
XCTAssertEqual(x1.plus(x1).plus(x1).plus(x1), 125663706143591729538505735331237819498117098339578011207320433535025495727340)
5657
}
58+
#endif
5759

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

72+
#if canImport(Swift.StaticBigInt)
7073
func testMinusLiteral() {
7174
let x1: BigInt = -9999998765438765432131415926535897932384626433832809454874529274584894502801830108383756373931835
7275
let x2: BigInt = -19999998765438765432131415926535897932384626433832809454874529274584894502801830108383756373931835
7376
XCTAssertEqual(x1.minus(x2), 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
7477
XCTAssertEqual(x1.plus(x1).plus(x1), -29999996296316296296394247779607693797153879301498428364623587823754683508405490325151269121795505)
7578
XCTAssertEqual(x1.plus(x1).plus(x1).plus(x2), -49999995061755061728525663706143591729538505735331237819498117098339578011207320433535025495727340)
7679
}
80+
#endif
7781

7882
func testTimes() {
7983
let x1: BigInt = "89248574598402980294572048572242498123"
@@ -84,6 +88,7 @@ class BigIntTests: XCTestCase {
8488
"-75537574353998534693615828134454330968785329792741330257228043492082567")
8589
}
8690

91+
#if canImport(Swift.StaticBigInt)
8792
func testTimesLiteral() {
8893
let x1: BigInt = 89248574598402980294572048572242498123
8994
let x2: BigInt = 84759720710000012134
@@ -92,6 +97,7 @@ class BigIntTests: XCTestCase {
9297
XCTAssert(x1.times(x3) ==
9398
-75537574353998534693615828134454330968785329792741330257228043492082567)
9499
}
100+
#endif
95101

96102
func testDividedBy() {
97103
let x1: BigInt = "38274945700000001034304000022452452525224002449"

0 commit comments

Comments
 (0)