Skip to content

Reflect changes to the integer types #1136

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
Jul 28, 2017
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
60 changes: 30 additions & 30 deletions Foundation/NSDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct Decimal {
return Int32(__exponent)
}
set {
__exponent = Int8(extendingOrTruncating: newValue)
__exponent = Int8(truncatingIfNeeded: newValue)
}
}
// length == 0 && isNegative -> NaN
Expand Down Expand Up @@ -90,7 +90,7 @@ public struct Decimal {

public init(_exponent: Int32, _length: UInt32, _isNegative: UInt32, _isCompact: UInt32, _reserved: UInt32, _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)){
self._mantissa = _mantissa
self.__exponent = Int8(extendingOrTruncating: _exponent)
self.__exponent = Int8(truncatingIfNeeded: _exponent)
self.__lengthAndFlags = UInt8(_length & 0b1111)
self.__reserved = 0
self._isNegative = _isNegative
Expand Down Expand Up @@ -412,21 +412,21 @@ extension Decimal {
while mantissa != 0 && i < NSDecimalMaxSize {
switch i {
case 0:
_mantissa.0 = UInt16(extendingOrTruncating:mantissa)
_mantissa.0 = UInt16(truncatingIfNeeded:mantissa)
case 1:
_mantissa.1 = UInt16(extendingOrTruncating:mantissa)
_mantissa.1 = UInt16(truncatingIfNeeded:mantissa)
case 2:
_mantissa.2 = UInt16(extendingOrTruncating:mantissa)
_mantissa.2 = UInt16(truncatingIfNeeded:mantissa)
case 3:
_mantissa.3 = UInt16(extendingOrTruncating:mantissa)
_mantissa.3 = UInt16(truncatingIfNeeded:mantissa)
case 4:
_mantissa.4 = UInt16(extendingOrTruncating:mantissa)
_mantissa.4 = UInt16(truncatingIfNeeded:mantissa)
case 5:
_mantissa.5 = UInt16(extendingOrTruncating:mantissa)
_mantissa.5 = UInt16(truncatingIfNeeded:mantissa)
case 6:
_mantissa.6 = UInt16(extendingOrTruncating:mantissa)
_mantissa.6 = UInt16(truncatingIfNeeded:mantissa)
case 7:
_mantissa.7 = UInt16(extendingOrTruncating:mantissa)
_mantissa.7 = UInt16(truncatingIfNeeded:mantissa)
default:
fatalError("initialization overflow")
}
Expand Down Expand Up @@ -592,13 +592,13 @@ fileprivate func multiplyByShort<T:VariableLengthNumber>(_ d: inout T, _ mul:UIn
for i in 0..<d._length {
let accumulator: UInt32 = UInt32(d[i]) * UInt32(mul) + carry
carry = accumulator >> 16
d[i] = UInt16(extendingOrTruncating: accumulator)
d[i] = UInt16(truncatingIfNeeded: accumulator)
}
if carry != 0 {
if d._length >= Decimal.maxSize {
return .overflow
}
d[d._length] = UInt16(extendingOrTruncating: carry)
d[d._length] = UInt16(truncatingIfNeeded: carry)
d._length += 1
}
return .noError
Expand All @@ -609,13 +609,13 @@ fileprivate func addShort<T:VariableLengthNumber>(_ d: inout T, _ add:UInt16) ->
for i in 0..<d._length {
let accumulator: UInt32 = UInt32(d[i]) + carry
carry = accumulator >> 16
d[i] = UInt16(extendingOrTruncating: accumulator)
d[i] = UInt16(truncatingIfNeeded: accumulator)
}
if carry != 0 {
if d._length >= Decimal.maxSize {
return .overflow
}
d[d._length] = UInt16(extendingOrTruncating: carry)
d[d._length] = UInt16(truncatingIfNeeded: carry)
d._length += 1
}
return .noError
Expand Down Expand Up @@ -763,8 +763,8 @@ fileprivate func integerMultiply<T:VariableLengthNumber>(_ big: inout T,
if i + j < big._length {
let bigij = UInt32(big[i+j])
accumulator = UInt32(carry) + bigij + UInt32(right[j]) * UInt32(left[i])
carry = UInt16(extendingOrTruncating:accumulator >> 16)
big[i+j] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
big[i+j] = UInt16(truncatingIfNeeded:accumulator)
} else if carry != 0 || (right[j] == 0 && left[j] == 0) {
return .overflow
}
Expand Down Expand Up @@ -904,7 +904,7 @@ fileprivate func integerDivide<T:VariableLengthNumber>(_ r: inout T,
acc = acc & 0xffff;
acc = 0xffff + UInt32(u[ul - vl + i - UInt32(j) - UInt32(1)]) - acc + sk; // subtract
sk = acc >> 16;
u[ul - vl + i - UInt32(j) - UInt32(1)] = UInt16(extendingOrTruncating:acc)
u[ul - vl + i - UInt32(j) - UInt32(1)] = UInt16(truncatingIfNeeded:acc)
}

// D5: test remainder
Expand All @@ -920,7 +920,7 @@ fileprivate func integerDivide<T:VariableLengthNumber>(_ r: inout T,
let vl = v._length
acc = UInt32(v[i]) + UInt32(u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)]) + k
k = acc >> 16;
u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)] = UInt16(extendingOrTruncating:acc)
u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)] = UInt16(truncatingIfNeeded:acc)
}
// k must be == 1 here
}
Expand Down Expand Up @@ -1173,17 +1173,17 @@ fileprivate func integerAdd(_ result: inout WideDecimal, _ left: inout Decimal,
let li = UInt32(left[i])
let ri = UInt32(right[i])
accumulator = li + ri + UInt32(carry)
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
}

while i < left._length {
if carry != 0 {
let li = UInt32(left[i])
accumulator = li + UInt32(carry)
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
} else {
while i < left._length {
Expand All @@ -1197,8 +1197,8 @@ fileprivate func integerAdd(_ result: inout WideDecimal, _ left: inout Decimal,
if carry != 0 {
let ri = UInt32(right[i])
accumulator = ri + UInt32(carry)
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
} else {
while i < right._length {
Expand Down Expand Up @@ -1240,17 +1240,17 @@ fileprivate func integerSubtract(_ result: inout Decimal, _ left: inout Decimal,
let li = UInt32(left[i])
let ri = UInt32(right[i])
accumulator = 0xffff + li - ri + UInt32(carry)
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
}

while i < left._length {
if carry != 0 {
let li = UInt32(left[i])
accumulator = 0xffff + li // + no carry
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
} else {
while i < left._length {
Expand All @@ -1263,8 +1263,8 @@ fileprivate func integerSubtract(_ result: inout Decimal, _ left: inout Decimal,
while i < right._length {
let ri = UInt32(right[i])
accumulator = 0xffff - ri + UInt32(carry)
carry = UInt16(extendingOrTruncating:accumulator >> 16)
result[i] = UInt16(extendingOrTruncating:accumulator)
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
result[i] = UInt16(truncatingIfNeeded:accumulator)
i += 1
}

Expand Down
16 changes: 8 additions & 8 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,49 +416,49 @@ open class NSNumber : NSValue {
open var int8Value: Int8 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var uint8Value: UInt8 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var int16Value: Int16 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var uint16Value: UInt16 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var int32Value: Int32 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var uint32Value: UInt32 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var int64Value: Int64 {
var value: Int64 = 0
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
return .init(extendingOrTruncating: value)
return .init(truncatingIfNeeded: value)
}

open var uint64Value: UInt64 {
var value = CFSInt128Struct(high: 0, low: 0)
CFNumberGetValue(_cfObject, kCFNumberSInt128Type, &value)
return .init(extendingOrTruncating: value.low)
return .init(truncatingIfNeeded: value.low)
}

open var floatValue: Float {
Expand Down
20 changes: 10 additions & 10 deletions Foundation/ProgressFraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
return _ProgressFraction(completed: simplified.0, total: simplified.1)
}

static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: ArithmeticOverflow)) -> _ProgressFraction {
static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: Bool)) -> _ProgressFraction {
// Mathematically, it is nonsense to add or subtract something with a denominator of 0. However, for the purposes of implementing Progress' fractions, we just assume that a zero-denominator fraction is "weightless" and return the other value. We still need to check for the case where they are both nonsense though.
precondition(!(lhs.total == 0 && rhs.total == 0), "Attempt to add or subtract invalid fraction")
guard lhs.total != 0 else {
Expand All @@ -71,7 +71,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {

if let lcm = _leastCommonMultiple(lhs.total, rhs.total) {
let result = whichOverflow(lhs.completed * (lcm / lhs.total), rhs.completed * (lcm / rhs.total))
if result.overflow == .overflow {
if result.overflow {
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
} else {
return _ProgressFraction(completed: result.0, total: lcm)
Expand All @@ -83,7 +83,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {

if let lcm = _leastCommonMultiple(lhsSimplified.total, rhsSimplified.total) {
let result = whichOverflow(lhsSimplified.completed * (lcm / lhsSimplified.total), rhsSimplified.completed * (lcm / rhsSimplified.total))
if result.overflow == .overflow {
if result.overflow {
// Use original lhs/rhs here
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
} else {
Expand Down Expand Up @@ -113,15 +113,15 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
let newCompleted = lhs.completed.multipliedReportingOverflow(by: rhs.completed)
let newTotal = lhs.total.multipliedReportingOverflow(by: rhs.total)

if newCompleted.overflow == .overflow || newTotal.overflow == .overflow {
if newCompleted.overflow || newTotal.overflow {
// Try simplifying, then do it again
let lhsSimplified = lhs.simplified()
let rhsSimplified = rhs.simplified()

let newCompletedSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.completed)
let newTotalSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.total)

if newCompletedSimplified.overflow == .overflow || newTotalSimplified.overflow == .overflow {
if newCompletedSimplified.overflow || newTotalSimplified.overflow {
// Still overflow
return _ProgressFraction(double: lhs.fractionCompleted * rhs.fractionCompleted, overflow: true)
} else {
Expand All @@ -140,12 +140,12 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {

let newTotal = lhs.total.multipliedReportingOverflow(by: rhs)

if newTotal.overflow == .overflow {
if newTotal.overflow {
let simplified = lhs.simplified()

let newTotalSimplified = simplified.total.multipliedReportingOverflow(by: rhs)

if newTotalSimplified.overflow == .overflow {
if newTotalSimplified.overflow {
// Still overflow
return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true)
} else {
Expand Down Expand Up @@ -178,7 +178,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
let left = lhs.completed.multipliedReportingOverflow(by: rhs.total)
let right = lhs.total.multipliedReportingOverflow(by: rhs.completed)

if left.overflow == .none && right.overflow == .none {
if !left.overflow && !right.overflow {
if left.0 == right.0 {
return true
}
Expand All @@ -190,7 +190,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
let leftSimplified = lhsSimplified.completed.multipliedReportingOverflow(by: rhsSimplified.total)
let rightSimplified = lhsSimplified.total.multipliedReportingOverflow(by: rhsSimplified.completed)

if leftSimplified.overflow == .none && rightSimplified.overflow == .none {
if !leftSimplified.overflow && !rightSimplified.overflow {
if leftSimplified.0 == rightSimplified.0 {
return true
}
Expand Down Expand Up @@ -261,7 +261,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
// This division always results in an integer value because gcd(a,b) is a divisor of a.
// lcm(a,b) == (|a|/gcd(a,b))*b == (|b|/gcd(a,b))*a
let result = (a / _greatestCommonDivisor(a, b)).multipliedReportingOverflow(by: b)
if result.overflow == .overflow {
if result.overflow {
return nil
} else {
return result.0
Expand Down