Skip to content

Commit 1c3e597

Browse files
author
Max Moiseev
committed
[stdlib] Get rid of ArithmeticOverflow type
1 parent 5d47a0d commit 1c3e597

File tree

6 files changed

+66
-94
lines changed

6 files changed

+66
-94
lines changed

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,25 +269,21 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
269269
% for (operator, name) in [('+', 'adding'), ('-', 'subtracting')]:
270270
% highAffectedByLowOverflow = 'Base.max' if operator == '+' else 'Base.min'
271271
public func ${name}ReportingOverflow(_ rhs: DoubleWidth)
272-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
272+
-> (partialValue: DoubleWidth, overflow: Bool) {
273273
let (low, lowOverflow) =
274274
_storage.low.${name}ReportingOverflow(rhs._storage.low)
275275
let (high, highOverflow) =
276276
_storage.high.${name}ReportingOverflow(rhs._storage.high)
277-
let isLowOverflow = lowOverflow == .overflow
278-
let result = (high &${operator} (isLowOverflow ? 1 : 0), low)
279-
let overflow = ArithmeticOverflow(
280-
highOverflow == .overflow ||
281-
high == ${highAffectedByLowOverflow} && isLowOverflow
282-
)
283-
return (partialValue: DoubleWidth(result),
284-
overflow: overflow)
277+
let result = (high &${operator} (lowOverflow ? 1 : 0), low)
278+
let overflow = highOverflow ||
279+
high == ${highAffectedByLowOverflow} && lowOverflow
280+
return (partialValue: DoubleWidth(result), overflow: overflow)
285281
}
286282
% end
287283

288284
public func multipliedReportingOverflow(
289285
by rhs: DoubleWidth
290-
) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
286+
) -> (partialValue: DoubleWidth, overflow: Bool) {
291287
let (carry, product) = multipliedFullWidth(by: rhs)
292288
let result = DoubleWidth(truncatingIfNeeded: product)
293289

@@ -298,7 +294,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
298294
let hadPositiveOverflow = !isNegative &&
299295
DoubleWidth.isSigned && product.leadingZeroBitCount == 0
300296

301-
return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow))
297+
return (result, didCarry || hadPositiveOverflow)
302298
}
303299

304300
public func quotientAndRemainder(dividingBy other: DoubleWidth)
@@ -350,25 +346,25 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
350346
}
351347

352348
public func dividedReportingOverflow(by other: DoubleWidth)
353-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
349+
-> (partialValue: DoubleWidth, overflow: Bool) {
354350
if other == (0 as DoubleWidth) ||
355351
(DoubleWidth.isSigned && other == (-1 as Int) && self == .min)
356352
{
357-
return (self, .overflow)
353+
return (self, true)
358354
}
359355

360-
return (quotientAndRemainder(dividingBy: other).quotient, .none)
356+
return (quotientAndRemainder(dividingBy: other).quotient, false)
361357
}
362358

363359
public func remainderReportingOverflow(dividingBy other: DoubleWidth)
364-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
360+
-> (partialValue: DoubleWidth, overflow: Bool) {
365361
if other == 0 ||
366362
(DoubleWidth.isSigned && other == -1 && self == .min)
367363
{
368-
return (self, .overflow)
364+
return (self, true)
369365
}
370366

371-
return (quotientAndRemainder(dividingBy: other).remainder, .none)
367+
return (quotientAndRemainder(dividingBy: other).remainder, false)
372368
}
373369

374370
public func multipliedFullWidth(by other: DoubleWidth)
@@ -384,8 +380,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
384380
func sum(_ x: Low, _ y: Low, _ z: Low) -> (partial: Low, carry: Low) {
385381
let (sum1, overflow1) = x.addingReportingOverflow(y)
386382
let (sum2, overflow2) = sum1.addingReportingOverflow(z)
387-
let carry: Low = (overflow1 == .overflow ? 1 : 0) +
388-
(overflow2 == .overflow ? 1 : 0)
383+
let carry: Low = (overflow1 ? 1 : 0) + (overflow2 ? 1 : 0)
389384
return (sum2, carry)
390385
}
391386

@@ -406,7 +401,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
406401

407402
if isNegative {
408403
let (lowComplement, overflow) = (~low).addingReportingOverflow(1)
409-
return (~high + (overflow == .overflow ? 1 : 0), lowComplement)
404+
return (~high + (overflow ? 1 : 0), lowComplement)
410405
} else {
411406
return (high, low)
412407
}
@@ -535,7 +530,7 @@ binaryOperators = [
535530
lhs: inout DoubleWidth, rhs: DoubleWidth
536531
) {
537532
let (result, overflow) = lhs.${name}ReportingOverflow(${argumentLabel}rhs)
538-
_precondition(overflow == .none, "Overflow in ${operator}=")
533+
_precondition(!overflow, "Overflow in ${operator}=")
539534
lhs = result
540535
}
541536
% end

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
}
@@ -1883,28 +1883,6 @@ extension BinaryInteger {
18831883
//===--- FixedWidthInteger ------------------------------------------------===//
18841884
//===----------------------------------------------------------------------===//
18851885

1886-
/// An indicator of whether an arithmetic operation overflowed.
1887-
///
1888-
/// Some arithmetic operations on fixed-width integers return an
1889-
/// `ArithmeticOverflow` instance to indicate whether an overflow has
1890-
/// occurred. For example, adding `UInt8.max` to itself results in a value that
1891-
/// can't be represented by an `UInt8` instance without overflowing.
1892-
///
1893-
/// let x = UInt8.max
1894-
/// // x == 255
1895-
/// let (y, overflow) = x.addingReportingOverflow(x)
1896-
/// // y == 254
1897-
/// // overflow == ArithmeticOverflow.overflow
1898-
@_fixed_layout
1899-
public enum ArithmeticOverflow {
1900-
@_transparent
1901-
public init(_ overflow: Bool) { self = overflow ? .overflow : .none }
1902-
/// An indication that no overflow occurred in the operation.
1903-
case none
1904-
/// An indication that an overflow did occur in the operation.
1905-
case overflow
1906-
}
1907-
19081886
/// An integer type that uses a fixed size for every instance.
19091887
///
19101888
/// The `FixedWidthInteger` protocol adds binary bitwise operations, bit
@@ -1948,7 +1926,7 @@ public enum ArithmeticOverflow {
19481926
///
19491927
/// func squared<T: FixedWidthInteger>(_ x: T) -> T? {
19501928
/// let (result, overflow) = x.multipliedReportingOverflow(by: x)
1951-
/// guard overflow == .none else {
1929+
/// if overflow {
19521930
/// return nil
19531931
/// }
19541932
/// return result
@@ -2001,7 +1979,7 @@ public protocol FixedWidthInteger : BinaryInteger, _BitwiseOperations
20011979
${overflowOperationComment(x.operator)}
20021980
func ${x.name}ReportingOverflow(
20031981
${x.firstArg} rhs: Self
2004-
) -> (partialValue: Self, overflow: ArithmeticOverflow)
1982+
) -> (partialValue: Self, overflow: Bool)
20051983
% end
20061984

20071985
/// Returns a tuple containing the high and low parts of the result of
@@ -2267,7 +2245,7 @@ ${assignmentOperatorComment(x.operator, True)}
22672245
@_transparent
22682246
public static func ${x.operator}=(_ lhs: inout Self, _ rhs: Self) {
22692247
let (result, overflow) = lhs.${x.name}ReportingOverflow(${callLabel}rhs)
2270-
_precondition(overflow == .none, "Overflow in ${x.operator}=")
2248+
_precondition(!overflow, "Overflow in ${x.operator}=")
22712249
lhs = result
22722250
}
22732251
#endif
@@ -2278,7 +2256,7 @@ ${unsafeOperationComment(x.operator)}
22782256
public func unsafe${capitalize(x.name)}(${x.firstArg} other: Self) -> Self {
22792257
let (result, overflow) = self.${x.name}ReportingOverflow(${callLabel}other)
22802258

2281-
if (overflow != .none) {
2259+
if overflow {
22822260
if (_isDebugAssertConfiguration()) {
22832261
_preconditionFailure("overflow in unsafe${capitalize(x.name)}")
22842262
}
@@ -2660,7 +2638,7 @@ public struct ${Self}
26602638
@_transparent
26612639
public func ${x.name}ReportingOverflow(
26622640
${x.firstArg} other: ${Self}
2663-
) -> (partialValue: ${Self}, overflow: ArithmeticOverflow) {
2641+
) -> (partialValue: ${Self}, overflow: Bool) {
26642642

26652643
% if x.kind == '/':
26662644
// No LLVM primitives for checking overflow of division
@@ -2669,7 +2647,7 @@ public struct ${Self}
26692647
other == (0 as ${Self})
26702648
${'|| self == %s.min && other == (-1 as %s)' % (Self, Self) if signed else ''}
26712649
) {
2672-
return (partialValue: self, overflow: .overflow)
2650+
return (partialValue: self, overflow: true)
26732651
}
26742652

26752653
let (newStorage, overflow) = (
@@ -2685,7 +2663,7 @@ public struct ${Self}
26852663

26862664
return (
26872665
partialValue: ${Self}(newStorage),
2688-
overflow: ArithmeticOverflow(Bool(overflow)))
2666+
overflow: Bool(overflow))
26892667
}
26902668
% end
26912669

@@ -3230,7 +3208,7 @@ extension FixedWidthInteger {
32303208
) -> (Self, overflow: Bool) {
32313209
let (partialValue, overflow) =
32323210
lhs.${newPrefix}ReportingOverflow(${argLabel} rhs)
3233-
return (partialValue, overflow == .overflow)
3211+
return (partialValue, overflow: overflow)
32343212
}
32353213

32363214
% end

0 commit comments

Comments
 (0)