Skip to content

Commit 237d877

Browse files
committed
Catch overflow into high bit in signed multiplication
1 parent e5fb871 commit 237d877

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,10 +3373,11 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
33733373
let result = DoubleWidth(extendingOrTruncating: product)
33743374

33753375
let isNegative = (self < 0) != (rhs < 0)
3376-
let overflow = isNegative
3377-
? carry != ~0
3378-
: carry != 0
3379-
return (result, ArithmeticOverflow(overflow))
3376+
let didCarry = isNegative ? carry != ~0 : carry != 0
3377+
let hadPositiveOverflow = !isNegative &&
3378+
DoubleWidth.isSigned && product.leadingZeroBitCount == 0
3379+
3380+
return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow))
33803381
}
33813382

33823383
public func quotientAndRemainder(dividingBy other: DoubleWidth)

0 commit comments

Comments
 (0)