From bc2f9fb7c6741b502f962a08da4d0f4a10323108 Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Tue, 17 Dec 2019 13:17:24 -0500 Subject: [PATCH] Use NumericShims for math functions --- Package.swift | 5 +- Sources/BigInt/BigInt.swift | 33 ++++----- Sources/NumericsShims/include/NumericsShims.h | 36 +++++++++ Tests/BigIntTests/BigIntTests.swift | 73 +++++++++++++++++++ 4 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 Tests/BigIntTests/BigIntTests.swift diff --git a/Package.swift b/Package.swift index f91f441..a085c78 100644 --- a/Package.swift +++ b/Package.swift @@ -18,16 +18,19 @@ let package = Package( .library(name: "Complex", targets: ["Complex"]), .library(name: "Numerics", targets: ["Numerics"]), .library(name: "Real", targets: ["Real"]), + .library(name: "BigInt", targets: ["BigInt"]), ], dependencies: [ ], targets: [ .target(name: "Complex", dependencies: ["Real"]), - .target(name: "Numerics", dependencies: ["Complex", "Real"]), + .target(name: "Numerics", dependencies: ["Complex", "Real", "BigInt"]), .target(name: "NumericsShims", dependencies: []), .target(name: "Real", dependencies: ["NumericsShims"]), + .target(name: "BigInt", dependencies: ["NumericsShims"]), .testTarget(name: "ComplexTests", dependencies: ["Complex", "NumericsShims"]), .testTarget(name: "RealTests", dependencies: ["Real"]), + .testTarget(name: "BigIntTests", dependencies: ["BigInt", "NumericsShims"]), ] ) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index b650ebd..80222e1 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -1,19 +1,16 @@ // // BigInt.swift -// lispish +// swift-numerics // // Created by Robert Thompson on 2/4/19. // -#if os(Linux) -import Glibc -#else -import Darwin -#endif +import NumericsShims fileprivate let digitsAndDash = Set("-0123456789") public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringConvertible, LosslessStringConvertible, Hashable { + public private(set) var words: Array public init?(_ description: String) { @@ -163,17 +160,17 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC public init(_ source: T) where T : BinaryFloatingPoint { let isNegative = source < 0.0 - var float = isNegative ? -source : source + var float = Double(isNegative ? -source : source) var words = Words() while float > 0.0 { - let digit = UInt(remainder(float, T(UInt.max) + 1.0)) + let digit = UInt(libm_remainder(float, Double(UInt.max) + 1.0)) words.append(digit) - - float = floor(float / (T(UInt.max) + 1.0)) + + float = libm_floor(float / (Double(UInt.max) + 1.0)) } - + self.words = words - + if isNegative { self = -self } @@ -206,7 +203,7 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC } public init?(exactly source: T) where T : BinaryFloatingPoint { - if ceil(source) != source { return nil } + if libm_ceil(Double(source)) != Double(source) { return nil } self.init(source) } @@ -452,15 +449,17 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC let bitWidth = UInt(UInt.bitWidth) let s = normalize(x: rhsWords[n - 1]) - guard let rn = calloc(n, MemoryLayout.stride)?.assumingMemoryBound(to: UInt.self) else { fatalError("OOM") } - defer { free(rn) } + 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 - guard let ln = calloc(m + n + 1, MemoryLayout.stride)?.assumingMemoryBound(to: UInt.self) else { fatalError("OOM") } - defer { free(ln) } + 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)) diff --git a/Sources/NumericsShims/include/NumericsShims.h b/Sources/NumericsShims/include/NumericsShims.h index 4e1b40b..3f3c1d2 100644 --- a/Sources/NumericsShims/include/NumericsShims.h +++ b/Sources/NumericsShims/include/NumericsShims.h @@ -143,6 +143,18 @@ HEADER_SHIM float libm_log10f(float x) { return __builtin_log10f(x); } +HEADER_SHIM float libm_ceilf(float x) { + return __builtin_ceilf(x); +} + +HEADER_SHIM float libm_floorf(float x) { + return __builtin_floorf(x); +} + +HEADER_SHIM float libm_remainderf(float x, float y) { + return __builtin_remainderf(x, y); +} + #if !defined _WIN32 HEADER_SHIM float libm_lgammaf(float x, int *signp) { extern float lgammaf_r(float, int *); @@ -263,6 +275,18 @@ HEADER_SHIM double libm_log10(double x) { return __builtin_log10(x); } +HEADER_SHIM double libm_ceil(double x) { + return __builtin_ceil(x); +} + +HEADER_SHIM double libm_floor(double x) { + return __builtin_floor(x); +} + +HEADER_SHIM double libm_remainder(double x, double y) { + return __builtin_remainder(x, y); +} + #if !defined _WIN32 HEADER_SHIM double libm_lgamma(double x, int *signp) { extern double lgamma_r(double, int *); @@ -380,6 +404,18 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) { extern long double lgammal_r(long double, int *); return lgammal_r(x, signp); } + +HEADER_SHIM long double libm_ceill(long double x) { + return __builtin_ceill(x); +} + +HEADER_SHIM long double libm_floorl(long double x) { + return __builtin_floorl(x); +} + +HEADER_SHIM long double libm_remainderl(long double x, long double y) { + return __builtin_remainderl(x, y); +} #endif // MARK: - shims to import C complex operations for timing purposes diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift new file mode 100644 index 0000000..cd358f8 --- /dev/null +++ b/Tests/BigIntTests/BigIntTests.swift @@ -0,0 +1,73 @@ +import XCTest +import BigInt + +func fac(_ n: BigInt) -> BigInt { + 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 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) + } + + 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) + } +}