Skip to content

Implement Decimal.init?<T : BinaryInteger>(exactly source: T) #2574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

import CoreFoundation

public var NSDecimalMaxSize: Int32 { 8 }
public var NSDecimalNoScale: Int32 { Int32(Int16.max) }

Expand Down Expand Up @@ -349,9 +347,50 @@ extension Decimal : SignedNumeric {
_reserved: 0, _mantissa: self._mantissa)
}

// FIXME(integers): implement properly
public init?<T : BinaryInteger>(exactly source: T) {
fatalError()
let zero = 0 as T

if source == zero {
self = Decimal.zero
return
}

let negative: UInt32 = (T.isSigned && source < zero) ? 1 : 0
var mantissa = source.magnitude
var exponent: Int32 = 0

let maxExponent = type(of: __exponent).max
while mantissa.isMultiple(of: 10) && (exponent < maxExponent) {
exponent += 1
mantissa /= 10
}

// If the matinssa still requires more than 128bits of storage then it is too large.
if mantissa.bitWidth > 128 && (mantissa >> 128 != zero) { return nil }

let mantissaParts: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)
let loWord = UInt64(truncatingIfNeeded: mantissa)
var length = ((loWord.bitWidth - loWord.leadingZeroBitCount) + (UInt16.bitWidth - 1)) / UInt16.bitWidth
mantissaParts.0 = UInt16(truncatingIfNeeded: loWord >> 0)
mantissaParts.1 = UInt16(truncatingIfNeeded: loWord >> 16)
mantissaParts.2 = UInt16(truncatingIfNeeded: loWord >> 32)
mantissaParts.3 = UInt16(truncatingIfNeeded: loWord >> 48)

let hiWord = mantissa.bitWidth > 64 ? UInt64(truncatingIfNeeded: mantissa >> 64) : 0
if hiWord != 0 {
length = 4 + ((hiWord.bitWidth - hiWord.leadingZeroBitCount) + (UInt16.bitWidth - 1)) / UInt16.bitWidth
mantissaParts.4 = UInt16(truncatingIfNeeded: hiWord >> 0)
mantissaParts.5 = UInt16(truncatingIfNeeded: hiWord >> 16)
mantissaParts.6 = UInt16(truncatingIfNeeded: hiWord >> 32)
mantissaParts.7 = UInt16(truncatingIfNeeded: hiWord >> 48)
} else {
mantissaParts.4 = 0
mantissaParts.5 = 0
mantissaParts.6 = 0
mantissaParts.7 = 0
}

self = Decimal(_exponent: exponent, _length: UInt32(length), _isNegative: negative, _isCompact: 1, _reserved: 0, _mantissa: mantissaParts)
}

public static func +=(lhs: inout Decimal, rhs: Decimal) {
Expand Down
45 changes: 45 additions & 0 deletions TestFoundation/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,50 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(NSDecimalNumber(value: 1).multiplying(byPowerOf10: -129).stringValue, "NaN")
}

func test_initExactly() {
// This really requires some tests using a BinaryInteger of bitwidth > 128 to test failures.
let d1 = Decimal(exactly: UInt64.max)
XCTAssertNotNil(d1)
XCTAssertEqual(d1?.description, UInt64.max.description)
XCTAssertEqual(d1?._length, 4)

let d2 = Decimal(exactly: Int64.min)
XCTAssertNotNil(d2)
XCTAssertEqual(d2?.description, Int64.min.description)
XCTAssertEqual(d2?._length, 4)

let d3 = Decimal(exactly: Int64.max)
XCTAssertNotNil(d3)
XCTAssertEqual(d3?.description, Int64.max.description)
XCTAssertEqual(d3?._length, 4)

let d4 = Decimal(exactly: Int32.min)
XCTAssertNotNil(d4)
XCTAssertEqual(d4?.description, Int32.min.description)
XCTAssertEqual(d4?._length, 2)

let d5 = Decimal(exactly: Int32.max)
XCTAssertNotNil(d5)
XCTAssertEqual(d5?.description, Int32.max.description)
XCTAssertEqual(d5?._length, 2)

let d6 = Decimal(exactly: 0)
XCTAssertNotNil(d6)
XCTAssertEqual(d6, Decimal.zero)
XCTAssertEqual(d6?.description, "0")
XCTAssertEqual(d6?._length, 0)

let d7 = Decimal(exactly: 1)
XCTAssertNotNil(d7)
XCTAssertEqual(d7?.description, "1")
XCTAssertEqual(d7?._length, 1)

let d8 = Decimal(exactly: -1)
XCTAssertNotNil(d8)
XCTAssertEqual(d8?.description, "-1")
XCTAssertEqual(d8?._length, 1)
}

static var allTests : [(String, (TestDecimal) -> () throws -> Void)] {
return [
("test_NSDecimalNumberInit", test_NSDecimalNumberInit),
Expand Down Expand Up @@ -1191,6 +1235,7 @@ class TestDecimal: XCTestCase {
("test_stringWithLocale", test_stringWithLocale),
("test_NSDecimalString", test_NSDecimalString),
("test_multiplyingByPowerOf10", test_multiplyingByPowerOf10),
("test_initExactly", test_initExactly),
]
}
}