Skip to content

Commit a138cca

Browse files
author
Max Moiseev
committed
[stdlib] Get rid of ArithmeticOverflow type
(cherry picked from commit 1c3e597)
1 parent a47932b commit a138cca

File tree

5 files changed

+48
-72
lines changed

5 files changed

+48
-72
lines changed

stdlib/public/core/IntegerParsing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where Rest.Element : UnsignedInteger {
4040
if !positive {
4141
let (result0, overflow0)
4242
= (0 as Result).subtractingReportingOverflow(result)
43-
guard _fastPath(overflow0 == .none) else { return nil }
43+
guard _fastPath(!overflow0) else { return nil }
4444
result = result0
4545
}
4646

@@ -51,7 +51,7 @@ where Rest.Element : UnsignedInteger {
5151
let (result2, overflow2) = positive
5252
? result1.addingReportingOverflow(d)
5353
: result1.subtractingReportingOverflow(d)
54-
guard _fastPath(overflow1 == .none && overflow2 == .none)
54+
guard _fastPath(!overflow1 && !overflow2)
5555
else { return nil }
5656
result = result2
5757
}

stdlib/public/core/Integers.swift.gyb

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,8 @@ def overflowOperationComment(operator):
819819
/// - Parameter rhs: The value to add to this value.
820820
/// - Returns: A tuple containing the result of the addition along with a
821821
/// flag indicating whether overflow occurred. If the `overflow` component
822-
/// is `.none`, the `partialValue` component contains the entire sum. If
823-
/// the `overflow` component is `.overflow`, an overflow occurred and the
822+
/// is `false`, the `partialValue` component contains the entire sum. If
823+
/// the `overflow` component is `true`, an overflow occurred and the
824824
/// `partialValue` component contains the truncated sum of this value and
825825
/// `rhs`.
826826
""",
@@ -831,8 +831,8 @@ def overflowOperationComment(operator):
831831
/// - Parameter rhs: The value to subtract from this value.
832832
/// - Returns: A tuple containing the result of the subtraction along with a
833833
/// flag indicating whether overflow occurred. If the `overflow` component
834-
/// is `.none`, the `partialValue` component contains the entire
835-
/// difference. If the `overflow` component is `.overflow`, an overflow
834+
/// is `false`, the `partialValue` component contains the entire
835+
/// difference. If the `overflow` component is `true`, an overflow
836836
/// occurred and the `partialValue` component contains the truncated
837837
/// result of `rhs` subtracted from this value.
838838
""",
@@ -843,8 +843,8 @@ def overflowOperationComment(operator):
843843
/// - Parameter rhs: The value to multiply by this value.
844844
/// - Returns: A tuple containing the result of the multiplication along with
845845
/// a flag indicating whether overflow occurred. If the `overflow`
846-
/// component is `.none`, the `partialValue` component contains the entire
847-
/// product. If the `overflow` component is `.overflow`, an overflow
846+
/// component is `false`, the `partialValue` component contains the entire
847+
/// product. If the `overflow` component is `true`, an overflow
848848
/// occurred and the `partialValue` component contains the truncated
849849
/// product of this value and `rhs`.
850850
""",
@@ -853,13 +853,13 @@ def overflowOperationComment(operator):
853853
/// a flag indicating whether overflow occurred in the operation.
854854
///
855855
/// Dividing by zero is not an error when using this method. For a value `x`,
856-
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, .overflow)`.
856+
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`.
857857
///
858858
/// - Parameter rhs: The value to divide this value by.
859859
/// - Returns: A tuple containing the result of the division along with a
860860
/// flag indicating whether overflow occurred. If the `overflow` component
861-
/// is `.none`, the `partialValue` component contains the entire quotient.
862-
/// If the `overflow` component is `.overflow`, an overflow occurred and
861+
/// is `false`, the `partialValue` component contains the entire quotient.
862+
/// If the `overflow` component is `true`, an overflow occurred and
863863
/// the `partialValue` component contains the truncated quotient.
864864
""",
865865
'%': """\
@@ -868,13 +868,13 @@ def overflowOperationComment(operator):
868868
/// a flag indicating whether overflow occurred in the operation.
869869
///
870870
/// Dividing by zero is not an error when using this method. For a value `x`,
871-
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, .overflow)`.
871+
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`.
872872
///
873873
/// - Parameter rhs: The value to divide this value by.
874874
/// - Returns: A tuple containing the result of the division along with a
875875
/// flag indicating whether overflow occurred. If the `overflow` component
876-
/// is `.none`, the `partialValue` component contains the entire quotient.
877-
/// If the `overflow` component is `.overflow`, an overflow occurred and
876+
/// is `false`, the `partialValue` component contains the entire quotient.
877+
/// If the `overflow` component is `true`, an overflow occurred and
878878
/// the `partialValue` component contains the truncated quotient.
879879
""",
880880
}
@@ -1844,28 +1844,6 @@ extension BinaryInteger {
18441844
//===--- FixedWidthInteger ------------------------------------------------===//
18451845
//===----------------------------------------------------------------------===//
18461846

1847-
/// An indicator of whether an arithmetic operation overflowed.
1848-
///
1849-
/// Some arithmetic operations on fixed-width integers return an
1850-
/// `ArithmeticOverflow` instance to indicate whether an overflow has
1851-
/// occurred. For example, adding `UInt8.max` to itself results in a value that
1852-
/// can't be represented by an `UInt8` instance without overflowing.
1853-
///
1854-
/// let x = UInt8.max
1855-
/// // x == 255
1856-
/// let (y, overflow) = x.addingReportingOverflow(x)
1857-
/// // y == 254
1858-
/// // overflow == ArithmeticOverflow.overflow
1859-
@_fixed_layout
1860-
public enum ArithmeticOverflow {
1861-
@_transparent
1862-
public init(_ overflow: Bool) { self = overflow ? .overflow : .none }
1863-
/// An indication that no overflow occurred in the operation.
1864-
case none
1865-
/// An indication that an overflow did occur in the operation.
1866-
case overflow
1867-
}
1868-
18691847
/// An integer type that uses a fixed size for every instance.
18701848
///
18711849
/// The `FixedWidthInteger` protocol adds binary bitwise operations, bit
@@ -1909,7 +1887,7 @@ public enum ArithmeticOverflow {
19091887
///
19101888
/// func squared<T: FixedWidthInteger>(_ x: T) -> T? {
19111889
/// let (result, overflow) = x.multipliedReportingOverflow(by: x)
1912-
/// guard overflow == .none else {
1890+
/// if overflow {
19131891
/// return nil
19141892
/// }
19151893
/// return result
@@ -1960,7 +1938,7 @@ public protocol FixedWidthInteger : BinaryInteger, _BitwiseOperations {
19601938
${overflowOperationComment(x.operator)}
19611939
func ${x.name}ReportingOverflow(
19621940
${x.firstArg} rhs: Self
1963-
) -> (partialValue: Self, overflow: ArithmeticOverflow)
1941+
) -> (partialValue: Self, overflow: Bool)
19641942
% end
19651943

19661944
/// Returns a tuple containing the high and low parts of the result of
@@ -2251,7 +2229,7 @@ ${assignmentOperatorComment(x.operator, True)}
22512229
@_transparent
22522230
public static func ${x.operator}=(_ lhs: inout Self, _ rhs: Self) {
22532231
let (result, overflow) = lhs.${x.name}ReportingOverflow(${callLabel}rhs)
2254-
_precondition(overflow == .none, "Overflow in ${x.operator}=")
2232+
_precondition(!overflow, "Overflow in ${x.operator}=")
22552233
lhs = result
22562234
}
22572235
#endif
@@ -2262,7 +2240,7 @@ ${unsafeOperationComment(x.operator)}
22622240
public func unsafe${capitalize(x.name)}(${x.firstArg} other: Self) -> Self {
22632241
let (result, overflow) = self.${x.name}ReportingOverflow(${callLabel}other)
22642242

2265-
if (overflow != .none) {
2243+
if overflow {
22662244
if (_isDebugAssertConfiguration()) {
22672245
_preconditionFailure("overflow in unsafe${capitalize(x.name)}")
22682246
}
@@ -2646,7 +2624,7 @@ public struct ${Self}
26462624
@_transparent
26472625
public func ${x.name}ReportingOverflow(
26482626
${x.firstArg} other: ${Self}
2649-
) -> (partialValue: ${Self}, overflow: ArithmeticOverflow) {
2627+
) -> (partialValue: ${Self}, overflow: Bool) {
26502628

26512629
% if x.kind == '/':
26522630
// No LLVM primitives for checking overflow of division
@@ -2655,7 +2633,7 @@ public struct ${Self}
26552633
other == (0 as ${Self})
26562634
${'|| self == %s.min && other == (-1 as %s)' % (Self, Self) if signed else ''}
26572635
) {
2658-
return (partialValue: self, overflow: .overflow)
2636+
return (partialValue: self, overflow: true)
26592637
}
26602638

26612639
let (newStorage, overflow) = (
@@ -2671,7 +2649,7 @@ public struct ${Self}
26712649

26722650
return (
26732651
partialValue: ${Self}(newStorage),
2674-
overflow: ArithmeticOverflow(Bool(overflow)))
2652+
overflow: Bool(overflow))
26752653
}
26762654
% end
26772655

@@ -3218,7 +3196,7 @@ extension FixedWidthInteger {
32183196
) -> (Self, overflow: Bool) {
32193197
let (partialValue, overflow) =
32203198
lhs.${newPrefix}ReportingOverflow(${argLabel} rhs)
3221-
return (partialValue, overflow == .overflow)
3199+
return (partialValue, overflow: overflow)
32223200
}
32233201

32243202
% end

test/Prototypes/BigInt.swift

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension FixedWidthInteger {
2424
func addingFullWidth(_ other: Self) ->
2525
(high: Self, low: Self) {
2626
let sum = self.addingReportingOverflow(other)
27-
return (sum.overflow == .overflow ? 1 : 0, sum.partialValue)
27+
return (sum.overflow ? 1 : 0, sum.partialValue)
2828
}
2929

3030
/// Returns the high and low parts of two seqeuential potentially overflowing
@@ -33,8 +33,8 @@ extension FixedWidthInteger {
3333
(high: Self, low: Self) {
3434
let xy = x.addingReportingOverflow(y)
3535
let xyz = xy.partialValue.addingReportingOverflow(z)
36-
let high: Self = (xy.overflow == .overflow ? 1 : 0) +
37-
(xyz.overflow == .overflow ? 1 : 0)
36+
let high: Self = (xy.overflow ? 1 : 0) +
37+
(xyz.overflow ? 1 : 0)
3838
return (high, xyz.partialValue)
3939
}
4040

@@ -43,7 +43,7 @@ extension FixedWidthInteger {
4343
func subtractingWithBorrow(_ rhs: Self) ->
4444
(borrow: Self, partialValue: Self) {
4545
let difference = subtractingReportingOverflow(rhs)
46-
return (difference.overflow == .overflow ? 1 : 0, difference.partialValue)
46+
return (difference.overflow ? 1 : 0, difference.partialValue)
4747
}
4848

4949
/// Returns a tuple containing the value that would be borrowed from a higher
@@ -53,8 +53,8 @@ extension FixedWidthInteger {
5353
let firstDifference = subtractingReportingOverflow(x)
5454
let secondDifference =
5555
firstDifference.partialValue.subtractingReportingOverflow(y)
56-
let borrow: Self = (firstDifference.overflow == .overflow ? 1 : 0) +
57-
(secondDifference.overflow == .overflow ? 1 : 0)
56+
let borrow: Self = (firstDifference.overflow ? 1 : 0) +
57+
(secondDifference.overflow ? 1 : 0)
5858
return (borrow, secondDifference.partialValue)
5959
}
6060
}
@@ -513,8 +513,7 @@ public struct _BigInt<Word: FixedWidthInteger & UnsignedInteger> :
513513
// 0b11111111 + (0b11111101_____00000010) + 0b11111111
514514
// (0b11111110_____00000001) + 0b11111111
515515
// (0b11111111_____00000000)
516-
_sanityCheck(
517-
product.high.addingReportingOverflow(carry).overflow == .none)
516+
_sanityCheck(!product.high.addingReportingOverflow(carry).overflow)
518517
carry = product.high &+ carry
519518
}
520519

@@ -1293,60 +1292,60 @@ struct Bit : FixedWidthInteger, UnsignedInteger {
12931292

12941293
// Arithmetic Operations / Operators
12951294

1296-
func _checkOverflow(_ v: UInt8) -> ArithmeticOverflow {
1295+
func _checkOverflow(_ v: UInt8) -> Bool {
12971296
let mask: UInt8 = ~0 << 1
1298-
return v & mask == 0 ? .none : .overflow
1297+
return v & mask != 0
12991298
}
13001299

13011300
func addingReportingOverflow(_ rhs: Bit) ->
1302-
(partialValue: Bit, overflow: ArithmeticOverflow) {
1301+
(partialValue: Bit, overflow: Bool) {
13031302
let result = value &+ rhs.value
13041303
return (Bit(result & 1), _checkOverflow(result))
13051304
}
13061305

13071306
func subtractingReportingOverflow(_ rhs: Bit) ->
1308-
(partialValue: Bit, overflow: ArithmeticOverflow) {
1307+
(partialValue: Bit, overflow: Bool) {
13091308
let result = value &- rhs.value
13101309
return (Bit(result & 1), _checkOverflow(result))
13111310
}
13121311

13131312
func multipliedReportingOverflow(by rhs: Bit) ->
1314-
(partialValue: Bit, overflow: ArithmeticOverflow) {
1313+
(partialValue: Bit, overflow: Bool) {
13151314
let result = value &* rhs.value
1316-
return (Bit(result), .none)
1315+
return (Bit(result), false)
13171316
}
13181317

13191318
func dividedReportingOverflow(by rhs: Bit) ->
1320-
(partialValue: Bit, overflow: ArithmeticOverflow) {
1321-
return rhs == 0 ? (self, .none) : (self, .overflow)
1319+
(partialValue: Bit, overflow: Bool) {
1320+
return (self, rhs != 0)
13221321
}
13231322

13241323
func remainderReportingOverflow(dividingBy rhs: Bit) ->
1325-
(partialValue: Bit, overflow: ArithmeticOverflow) {
1324+
(partialValue: Bit, overflow: Bool) {
13261325
fatalError()
13271326
}
13281327

13291328
static func +=(lhs: inout Bit, rhs: Bit) {
13301329
let result = lhs.addingReportingOverflow(rhs)
1331-
assert(result.overflow == .none, "Addition overflow")
1330+
assert(!result.overflow, "Addition overflow")
13321331
lhs = result.partialValue
13331332
}
13341333

13351334
static func -=(lhs: inout Bit, rhs: Bit) {
13361335
let result = lhs.subtractingReportingOverflow(rhs)
1337-
assert(result.overflow == .none, "Subtraction overflow")
1336+
assert(!result.overflow, "Subtraction overflow")
13381337
lhs = result.partialValue
13391338
}
13401339

13411340
static func *=(lhs: inout Bit, rhs: Bit) {
13421341
let result = lhs.multipliedReportingOverflow(by: rhs)
1343-
assert(result.overflow == .none, "Multiplication overflow")
1342+
assert(!result.overflow, "Multiplication overflow")
13441343
lhs = result.partialValue
13451344
}
13461345

13471346
static func /=(lhs: inout Bit, rhs: Bit) {
13481347
let result = lhs.dividedReportingOverflow(by: rhs)
1349-
assert(result.overflow == .none, "Division overflow")
1348+
assert(!result.overflow, "Division overflow")
13501349
lhs = result.partialValue
13511350
}
13521351

test/stdlib/Integers.swift.gyb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func expectEqual<T : FixedWidthInteger>(
8787
}
8888

8989
func expectEqual<T : FixedWidthInteger>(
90-
_ expected: (T, ArithmeticOverflow), _ actual: (T, ArithmeticOverflow),
90+
_ expected: (T, Bool), _ actual: (T, Bool),
9191
_ message: @autoclosure () -> String = "",
9292
stackTrace: SourceLocStack = SourceLocStack(),
9393
showFrame: Bool = true,
@@ -621,5 +621,4 @@ tests.test("signum/concrete") {
621621
% end
622622
}
623623

624-
625624
runAllTests()

validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,28 @@ func expectNoOverflow<T>(
4242
}
4343

4444
func expectOverflow<T>(
45-
_ res: (partialValue: T, overflow: ArithmeticOverflow),
45+
_ res: (partialValue: T, overflow: Bool),
4646
//===--- TRACE boilerplate ----------------------------------------------===//
4747
_ message: @autoclosure () -> String = "",
4848
showFrame: Bool = true,
4949
stackTrace: SourceLocStack = SourceLocStack(),
5050
file: String = #file, line: UInt = #line
5151
) {
5252
expectTrue(
53-
res.overflow == .overflow, "expected overflow",
53+
res.overflow, "expected overflow",
5454
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
5555
}
5656

5757
func expectNoOverflow<T>(
58-
_ res: (partialValue: T, overflow: ArithmeticOverflow),
58+
_ res: (partialValue: T, overflow: Bool),
5959
//===--- TRACE boilerplate ----------------------------------------------===//
6060
_ message: @autoclosure () -> String = "",
6161
showFrame: Bool = true,
6262
stackTrace: SourceLocStack = SourceLocStack(),
6363
file: String = #file, line: UInt = #line
6464
) {
65-
expectTrue(
66-
res.overflow == .none, "expected no overflow",
65+
expectFalse(
66+
res.overflow, "expected no overflow",
6767
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
6868
}
6969

0 commit comments

Comments
 (0)