From cb56525312ad1f541ef3df8ebfd5b6014041896c Mon Sep 17 00:00:00 2001 From: Robert Thompson Date: Tue, 17 Dec 2019 15:29:19 -0500 Subject: [PATCH] Address PR feedback Eliminate unnecessary math function calls, and also handle floating point edge cases better. --- Sources/BigInt/BigInt.swift | 35 +++++++++++------- Sources/NumericsShims/include/NumericsShims.h | 36 ------------------- Tests/BigIntTests/BigIntTests.swift | 3 ++ 3 files changed, 26 insertions(+), 48 deletions(-) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index 80222e1..0370714 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -4,9 +4,6 @@ // // Created by Robert Thompson on 2/4/19. // - -import NumericsShims - fileprivate let digitsAndDash = Set("-0123456789") public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringConvertible, LosslessStringConvertible, Hashable { @@ -159,18 +156,28 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC } 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 = Double(isNegative ? -source : source) - var words = Words() - while float > 0.0 { - let digit = UInt(libm_remainder(float, Double(UInt.max) + 1.0)) - words.append(digit) + 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 - float = libm_floor(float / (Double(UInt.max) + 1.0)) + self.words = words } - self.words = words - if isNegative { self = -self } @@ -203,7 +210,11 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC } public init?(exactly source: T) where T : BinaryFloatingPoint { - if libm_ceil(Double(source)) != Double(source) { return nil } + if (source.isNaN || source.isInfinite) || + (source.rounded(.towardZero) != source) { + return nil + } + self.init(source) } diff --git a/Sources/NumericsShims/include/NumericsShims.h b/Sources/NumericsShims/include/NumericsShims.h index 3f3c1d2..4e1b40b 100644 --- a/Sources/NumericsShims/include/NumericsShims.h +++ b/Sources/NumericsShims/include/NumericsShims.h @@ -143,18 +143,6 @@ 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 *); @@ -275,18 +263,6 @@ 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 *); @@ -404,18 +380,6 @@ 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 index cd358f8..7e5de1c 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -34,6 +34,9 @@ final class BigIntTests: XCTestCase { XCTAssertNotNil(baz) let equal = (baz ?? 0) / BigInt(1e38) == BigInt(24) XCTAssertEqual(equal, true) + + let infinite = BigInt(exactly: Double.infinity) + XCTAssertNil(infinite) } func testUIntConversion() {