Skip to content

Commit

Permalink
add an 'overflow' label to the second result of the "withoverflow" ar…
Browse files Browse the repository at this point in the history
…itmetic operations,

as suggested by Dmitri



Swift SVN r19251
  • Loading branch information
lattner committed Jun 26, 2014
1 parent d67b3d5 commit 4ac0f31
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions stdlib/core/Bit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@
}

extension Bit : IntegerArithmetic {
static func _withOverflow(x: Int, _ b: Bool) -> (Bit, Bool) {
return (Bit.fromRaw(x)!, b)
static func _withOverflow(v: (Int, overflow: Bool)) -> (Bit, overflow: Bool) {
return (Bit.fromRaw(v.0)!, v.overflow)
}

@public static func addWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, Bool) {
@public static func addWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, overflow: Bool) {
return _withOverflow(Int.addWithOverflow(lhs.toRaw(), rhs.toRaw()))
}

@public static func subtractWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, Bool) {
@public static func subtractWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, overflow: Bool) {
return _withOverflow(Int.subtractWithOverflow(lhs.toRaw(), rhs.toRaw()))
}

@public static func multiplyWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, Bool) {
@public static func multiplyWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, overflow: Bool) {
return _withOverflow(Int.multiplyWithOverflow(lhs.toRaw(), rhs.toRaw()))
}

@public static func divideWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, Bool) {
@public static func divideWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, overflow: Bool) {
return _withOverflow(Int.divideWithOverflow(lhs.toRaw(), rhs.toRaw()))
}

@public static func modulusWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, Bool) {
@public static func modulusWithOverflow(lhs: Bit, _ rhs: Bit) -> (Bit, overflow: Bool) {
return _withOverflow(Int.modulusWithOverflow(lhs.toRaw(), rhs.toRaw()))
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/core/FixedPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ extension ${Self} : RandomAccessIndex {

% for Method,op in [('add', 'add'), ('subtract', 'sub'), ('multiply', 'mul')]:
@transparent @public
static func ${Method}WithOverflow(lhs: ${Self}, _ rhs: ${Self}) -> (${Self}, Bool) {
static func ${Method}WithOverflow(lhs: ${Self}, _ rhs: ${Self}) -> (${Self}, overflow: Bool) {
var tmp = Builtin.${sign}${op}_with_overflow_${BuiltinName}(lhs.value, rhs.value, false.value)
return (${Self}(tmp.0), Bool(tmp.1))
}
Expand All @@ -288,7 +288,7 @@ extension ${Self} : RandomAccessIndex {
/// Operations that return an overflow bit in addition to a partial result,
/// helpful for checking for overflow when you want to handle it.
@transparent @public
static func ${Method}WithOverflow(lhs: ${Self}, _ rhs: ${Self}) -> (${Self}, Bool) {
static func ${Method}WithOverflow(lhs: ${Self}, _ rhs: ${Self}) -> (${Self}, overflow: Bool) {
if rhs == 0 {
return (0, true)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/core/IntegerArithmetic.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@public protocol _IntegerArithmetic {
% for name,_ in integerBinaryOps:
class func ${name}WithOverflow(lhs: Self, _ rhs: Self) -> (Self, Bool)
class func ${name}WithOverflow(lhs: Self, _ rhs: Self) -> (Self, overflow: Bool)
% end
}

Expand Down

0 comments on commit 4ac0f31

Please sign in to comment.