Skip to content

Commit 3a84a0c

Browse files
authored
Merge pull request #1136 from moiseev/integers-revised
Reflect changes to the integer types
2 parents 7d4da50 + 958e22b commit 3a84a0c

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

Foundation/NSDecimal.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct Decimal {
2424
return Int32(__exponent)
2525
}
2626
set {
27-
__exponent = Int8(extendingOrTruncating: newValue)
27+
__exponent = Int8(truncatingIfNeeded: newValue)
2828
}
2929
}
3030
// length == 0 && isNegative -> NaN
@@ -90,7 +90,7 @@ public struct Decimal {
9090

9191
public init(_exponent: Int32, _length: UInt32, _isNegative: UInt32, _isCompact: UInt32, _reserved: UInt32, _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)){
9292
self._mantissa = _mantissa
93-
self.__exponent = Int8(extendingOrTruncating: _exponent)
93+
self.__exponent = Int8(truncatingIfNeeded: _exponent)
9494
self.__lengthAndFlags = UInt8(_length & 0b1111)
9595
self.__reserved = 0
9696
self._isNegative = _isNegative
@@ -412,21 +412,21 @@ extension Decimal {
412412
while mantissa != 0 && i < NSDecimalMaxSize {
413413
switch i {
414414
case 0:
415-
_mantissa.0 = UInt16(extendingOrTruncating:mantissa)
415+
_mantissa.0 = UInt16(truncatingIfNeeded:mantissa)
416416
case 1:
417-
_mantissa.1 = UInt16(extendingOrTruncating:mantissa)
417+
_mantissa.1 = UInt16(truncatingIfNeeded:mantissa)
418418
case 2:
419-
_mantissa.2 = UInt16(extendingOrTruncating:mantissa)
419+
_mantissa.2 = UInt16(truncatingIfNeeded:mantissa)
420420
case 3:
421-
_mantissa.3 = UInt16(extendingOrTruncating:mantissa)
421+
_mantissa.3 = UInt16(truncatingIfNeeded:mantissa)
422422
case 4:
423-
_mantissa.4 = UInt16(extendingOrTruncating:mantissa)
423+
_mantissa.4 = UInt16(truncatingIfNeeded:mantissa)
424424
case 5:
425-
_mantissa.5 = UInt16(extendingOrTruncating:mantissa)
425+
_mantissa.5 = UInt16(truncatingIfNeeded:mantissa)
426426
case 6:
427-
_mantissa.6 = UInt16(extendingOrTruncating:mantissa)
427+
_mantissa.6 = UInt16(truncatingIfNeeded:mantissa)
428428
case 7:
429-
_mantissa.7 = UInt16(extendingOrTruncating:mantissa)
429+
_mantissa.7 = UInt16(truncatingIfNeeded:mantissa)
430430
default:
431431
fatalError("initialization overflow")
432432
}
@@ -592,13 +592,13 @@ fileprivate func multiplyByShort<T:VariableLengthNumber>(_ d: inout T, _ mul:UIn
592592
for i in 0..<d._length {
593593
let accumulator: UInt32 = UInt32(d[i]) * UInt32(mul) + carry
594594
carry = accumulator >> 16
595-
d[i] = UInt16(extendingOrTruncating: accumulator)
595+
d[i] = UInt16(truncatingIfNeeded: accumulator)
596596
}
597597
if carry != 0 {
598598
if d._length >= Decimal.maxSize {
599599
return .overflow
600600
}
601-
d[d._length] = UInt16(extendingOrTruncating: carry)
601+
d[d._length] = UInt16(truncatingIfNeeded: carry)
602602
d._length += 1
603603
}
604604
return .noError
@@ -609,13 +609,13 @@ fileprivate func addShort<T:VariableLengthNumber>(_ d: inout T, _ add:UInt16) ->
609609
for i in 0..<d._length {
610610
let accumulator: UInt32 = UInt32(d[i]) + carry
611611
carry = accumulator >> 16
612-
d[i] = UInt16(extendingOrTruncating: accumulator)
612+
d[i] = UInt16(truncatingIfNeeded: accumulator)
613613
}
614614
if carry != 0 {
615615
if d._length >= Decimal.maxSize {
616616
return .overflow
617617
}
618-
d[d._length] = UInt16(extendingOrTruncating: carry)
618+
d[d._length] = UInt16(truncatingIfNeeded: carry)
619619
d._length += 1
620620
}
621621
return .noError
@@ -763,8 +763,8 @@ fileprivate func integerMultiply<T:VariableLengthNumber>(_ big: inout T,
763763
if i + j < big._length {
764764
let bigij = UInt32(big[i+j])
765765
accumulator = UInt32(carry) + bigij + UInt32(right[j]) * UInt32(left[i])
766-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
767-
big[i+j] = UInt16(extendingOrTruncating:accumulator)
766+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
767+
big[i+j] = UInt16(truncatingIfNeeded:accumulator)
768768
} else if carry != 0 || (right[j] == 0 && left[j] == 0) {
769769
return .overflow
770770
}
@@ -904,7 +904,7 @@ fileprivate func integerDivide<T:VariableLengthNumber>(_ r: inout T,
904904
acc = acc & 0xffff;
905905
acc = 0xffff + UInt32(u[ul - vl + i - UInt32(j) - UInt32(1)]) - acc + sk; // subtract
906906
sk = acc >> 16;
907-
u[ul - vl + i - UInt32(j) - UInt32(1)] = UInt16(extendingOrTruncating:acc)
907+
u[ul - vl + i - UInt32(j) - UInt32(1)] = UInt16(truncatingIfNeeded:acc)
908908
}
909909

910910
// D5: test remainder
@@ -920,7 +920,7 @@ fileprivate func integerDivide<T:VariableLengthNumber>(_ r: inout T,
920920
let vl = v._length
921921
acc = UInt32(v[i]) + UInt32(u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)]) + k
922922
k = acc >> 16;
923-
u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)] = UInt16(extendingOrTruncating:acc)
923+
u[UInt32(ul) - UInt32(vl) + UInt32(i) - UInt32(j) - UInt32(1)] = UInt16(truncatingIfNeeded:acc)
924924
}
925925
// k must be == 1 here
926926
}
@@ -1173,17 +1173,17 @@ fileprivate func integerAdd(_ result: inout WideDecimal, _ left: inout Decimal,
11731173
let li = UInt32(left[i])
11741174
let ri = UInt32(right[i])
11751175
accumulator = li + ri + UInt32(carry)
1176-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1177-
result[i] = UInt16(extendingOrTruncating:accumulator)
1176+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1177+
result[i] = UInt16(truncatingIfNeeded:accumulator)
11781178
i += 1
11791179
}
11801180

11811181
while i < left._length {
11821182
if carry != 0 {
11831183
let li = UInt32(left[i])
11841184
accumulator = li + UInt32(carry)
1185-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1186-
result[i] = UInt16(extendingOrTruncating:accumulator)
1185+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1186+
result[i] = UInt16(truncatingIfNeeded:accumulator)
11871187
i += 1
11881188
} else {
11891189
while i < left._length {
@@ -1197,8 +1197,8 @@ fileprivate func integerAdd(_ result: inout WideDecimal, _ left: inout Decimal,
11971197
if carry != 0 {
11981198
let ri = UInt32(right[i])
11991199
accumulator = ri + UInt32(carry)
1200-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1201-
result[i] = UInt16(extendingOrTruncating:accumulator)
1200+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1201+
result[i] = UInt16(truncatingIfNeeded:accumulator)
12021202
i += 1
12031203
} else {
12041204
while i < right._length {
@@ -1240,17 +1240,17 @@ fileprivate func integerSubtract(_ result: inout Decimal, _ left: inout Decimal,
12401240
let li = UInt32(left[i])
12411241
let ri = UInt32(right[i])
12421242
accumulator = 0xffff + li - ri + UInt32(carry)
1243-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1244-
result[i] = UInt16(extendingOrTruncating:accumulator)
1243+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1244+
result[i] = UInt16(truncatingIfNeeded:accumulator)
12451245
i += 1
12461246
}
12471247

12481248
while i < left._length {
12491249
if carry != 0 {
12501250
let li = UInt32(left[i])
12511251
accumulator = 0xffff + li // + no carry
1252-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1253-
result[i] = UInt16(extendingOrTruncating:accumulator)
1252+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1253+
result[i] = UInt16(truncatingIfNeeded:accumulator)
12541254
i += 1
12551255
} else {
12561256
while i < left._length {
@@ -1263,8 +1263,8 @@ fileprivate func integerSubtract(_ result: inout Decimal, _ left: inout Decimal,
12631263
while i < right._length {
12641264
let ri = UInt32(right[i])
12651265
accumulator = 0xffff - ri + UInt32(carry)
1266-
carry = UInt16(extendingOrTruncating:accumulator >> 16)
1267-
result[i] = UInt16(extendingOrTruncating:accumulator)
1266+
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
1267+
result[i] = UInt16(truncatingIfNeeded:accumulator)
12681268
i += 1
12691269
}
12701270

Foundation/NSNumber.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,49 +416,49 @@ open class NSNumber : NSValue {
416416
open var int8Value: Int8 {
417417
var value: Int64 = 0
418418
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
419-
return .init(extendingOrTruncating: value)
419+
return .init(truncatingIfNeeded: value)
420420
}
421421

422422
open var uint8Value: UInt8 {
423423
var value: Int64 = 0
424424
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
425-
return .init(extendingOrTruncating: value)
425+
return .init(truncatingIfNeeded: value)
426426
}
427427

428428
open var int16Value: Int16 {
429429
var value: Int64 = 0
430430
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
431-
return .init(extendingOrTruncating: value)
431+
return .init(truncatingIfNeeded: value)
432432
}
433433

434434
open var uint16Value: UInt16 {
435435
var value: Int64 = 0
436436
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
437-
return .init(extendingOrTruncating: value)
437+
return .init(truncatingIfNeeded: value)
438438
}
439439

440440
open var int32Value: Int32 {
441441
var value: Int64 = 0
442442
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
443-
return .init(extendingOrTruncating: value)
443+
return .init(truncatingIfNeeded: value)
444444
}
445445

446446
open var uint32Value: UInt32 {
447447
var value: Int64 = 0
448448
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
449-
return .init(extendingOrTruncating: value)
449+
return .init(truncatingIfNeeded: value)
450450
}
451451

452452
open var int64Value: Int64 {
453453
var value: Int64 = 0
454454
CFNumberGetValue(_cfObject, kCFNumberSInt64Type, &value)
455-
return .init(extendingOrTruncating: value)
455+
return .init(truncatingIfNeeded: value)
456456
}
457457

458458
open var uint64Value: UInt64 {
459459
var value = CFSInt128Struct(high: 0, low: 0)
460460
CFNumberGetValue(_cfObject, kCFNumberSInt128Type, &value)
461-
return .init(extendingOrTruncating: value.low)
461+
return .init(truncatingIfNeeded: value.low)
462462
}
463463

464464
open var floatValue: Float {

Foundation/ProgressFraction.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
5454
return _ProgressFraction(completed: simplified.0, total: simplified.1)
5555
}
5656

57-
static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: ArithmeticOverflow)) -> _ProgressFraction {
57+
static private func _math(lhs: _ProgressFraction, rhs: _ProgressFraction, whichOperator: (_ lhs : Double, _ rhs : Double) -> Double, whichOverflow : (_ lhs: Int64, _ rhs: Int64) -> (Int64, overflow: Bool)) -> _ProgressFraction {
5858
// 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.
5959
precondition(!(lhs.total == 0 && rhs.total == 0), "Attempt to add or subtract invalid fraction")
6060
guard lhs.total != 0 else {
@@ -71,7 +71,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
7171

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

8484
if let lcm = _leastCommonMultiple(lhsSimplified.total, rhsSimplified.total) {
8585
let result = whichOverflow(lhsSimplified.completed * (lcm / lhsSimplified.total), rhsSimplified.completed * (lcm / rhsSimplified.total))
86-
if result.overflow == .overflow {
86+
if result.overflow {
8787
// Use original lhs/rhs here
8888
return _ProgressFraction(double: whichOperator(lhs.fractionCompleted, rhs.fractionCompleted), overflow: true)
8989
} else {
@@ -113,15 +113,15 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
113113
let newCompleted = lhs.completed.multipliedReportingOverflow(by: rhs.completed)
114114
let newTotal = lhs.total.multipliedReportingOverflow(by: rhs.total)
115115

116-
if newCompleted.overflow == .overflow || newTotal.overflow == .overflow {
116+
if newCompleted.overflow || newTotal.overflow {
117117
// Try simplifying, then do it again
118118
let lhsSimplified = lhs.simplified()
119119
let rhsSimplified = rhs.simplified()
120120

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

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

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

143-
if newTotal.overflow == .overflow {
143+
if newTotal.overflow {
144144
let simplified = lhs.simplified()
145145

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

148-
if newTotalSimplified.overflow == .overflow {
148+
if newTotalSimplified.overflow {
149149
// Still overflow
150150
return _ProgressFraction(double: lhs.fractionCompleted / Double(rhs), overflow: true)
151151
} else {
@@ -178,7 +178,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
178178
let left = lhs.completed.multipliedReportingOverflow(by: rhs.total)
179179
let right = lhs.total.multipliedReportingOverflow(by: rhs.completed)
180180

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

193-
if leftSimplified.overflow == .none && rightSimplified.overflow == .none {
193+
if !leftSimplified.overflow && !rightSimplified.overflow {
194194
if leftSimplified.0 == rightSimplified.0 {
195195
return true
196196
}
@@ -261,7 +261,7 @@ internal struct _ProgressFraction : Equatable, CustomDebugStringConvertible {
261261
// This division always results in an integer value because gcd(a,b) is a divisor of a.
262262
// lcm(a,b) == (|a|/gcd(a,b))*b == (|b|/gcd(a,b))*a
263263
let result = (a / _greatestCommonDivisor(a, b)).multipliedReportingOverflow(by: b)
264-
if result.overflow == .overflow {
264+
if result.overflow {
265265
return nil
266266
} else {
267267
return result.0

0 commit comments

Comments
 (0)