From 9f162728f199f65a714199f0876098810d7b08aa Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Sun, 20 Dec 2015 17:21:13 +1100 Subject: [PATCH 1/5] [stdlib] Fix bug in Bit with-overflow arithmetic The original version would result in a crash for negative results. --- stdlib/public/core/Bit.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/Bit.swift b/stdlib/public/core/Bit.swift index 02fd6fc6eb3d7..dde05af249c23 100644 --- a/stdlib/public/core/Bit.swift +++ b/stdlib/public/core/Bit.swift @@ -98,7 +98,8 @@ extension Bit : IntegerArithmeticType { if let b = Bit(rawValue: v.0) { return (b, v.overflow) } else { - return (Bit(rawValue: v.0 % 2)!, true) + let bitRaw = v.0 > 0 ? v.0 % 2 : v.0 % 2 + 2 + return (Bit(rawValue: bitRaw)!, true) } } From 56ed11ee3dbf1f46328a5b5c8ff05dda7a06e83c Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Sun, 20 Dec 2015 17:29:54 +1100 Subject: [PATCH 2/5] [stdlib] Refactor local variables names --- stdlib/public/core/Bit.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/public/core/Bit.swift b/stdlib/public/core/Bit.swift index dde05af249c23..62a16216a22ee 100644 --- a/stdlib/public/core/Bit.swift +++ b/stdlib/public/core/Bit.swift @@ -94,12 +94,12 @@ public func < (lhs: Bit, rhs: Bit) -> Bool { } extension Bit : IntegerArithmeticType { - static func _withOverflow(v: (Int, overflow: Bool)) -> (Bit, overflow: Bool) { - if let b = Bit(rawValue: v.0) { - return (b, v.overflow) + static func _withOverflow(intResult: Int, overflow: Bool) -> (Bit, overflow: Bool) { + if let bit = Bit(rawValue: intResult) { + return (bit, overflow) } else { - let bitRaw = v.0 > 0 ? v.0 % 2 : v.0 % 2 + 2 - return (Bit(rawValue: bitRaw)!, true) + let bitRaw = intResult > 0 ? intResult % 2 : intResult % 2 + 2 + return (Bit(rawValue: bitRaw)!, overflow: true) } } From 3e80f8d30ff7001e47ac47430e3a7f91259d1a9a Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Sun, 20 Dec 2015 17:30:30 +1100 Subject: [PATCH 3/5] [stdlib] Add test for Bit with-overflow arithmetic bug --- test/1_stdlib/Bit.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/1_stdlib/Bit.swift b/test/1_stdlib/Bit.swift index feb5d5e4994e5..bbe8a41dc5e31 100644 --- a/test/1_stdlib/Bit.swift +++ b/test/1_stdlib/Bit.swift @@ -27,6 +27,8 @@ print(one.predecessor().rawValue) // CHECK-NEXT: 0 print((one &+ one).rawValue) +// CHECK-NEXT: 1 +print((zero &- one).rawValue) // CHECK: done. print("done.") From f8c155a80ec289331e214ddb7966de229f2e53dd Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Sun, 20 Dec 2015 17:41:49 +1100 Subject: [PATCH 4/5] [stdlib] Refactor Bit._withOverflow() --- stdlib/public/core/Bit.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/public/core/Bit.swift b/stdlib/public/core/Bit.swift index 62a16216a22ee..775e95fce31de 100644 --- a/stdlib/public/core/Bit.swift +++ b/stdlib/public/core/Bit.swift @@ -96,10 +96,11 @@ public func < (lhs: Bit, rhs: Bit) -> Bool { extension Bit : IntegerArithmeticType { static func _withOverflow(intResult: Int, overflow: Bool) -> (Bit, overflow: Bool) { if let bit = Bit(rawValue: intResult) { - return (bit, overflow) + return (bit, overflow: overflow) } else { - let bitRaw = intResult > 0 ? intResult % 2 : intResult % 2 + 2 - return (Bit(rawValue: bitRaw)!, overflow: true) + let bitRaw = intResult % 2 + (intResult < 0 ? 2 : 0) + let bit = Bit(rawValue: bitRaw)! + return (bit, overflow: true) } } From f81968341d84026cf0dc9aef19009ad00158df6a Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Mon, 21 Dec 2015 13:06:24 +1100 Subject: [PATCH 5/5] [stdlib] Adjust formatting to fit 80 columns --- stdlib/public/core/Bit.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/Bit.swift b/stdlib/public/core/Bit.swift index 775e95fce31de..717ebd9e27eaf 100644 --- a/stdlib/public/core/Bit.swift +++ b/stdlib/public/core/Bit.swift @@ -94,7 +94,9 @@ public func < (lhs: Bit, rhs: Bit) -> Bool { } extension Bit : IntegerArithmeticType { - static func _withOverflow(intResult: Int, overflow: Bool) -> (Bit, overflow: Bool) { + static func _withOverflow( + intResult: Int, overflow: Bool + ) -> (Bit, overflow: Bool) { if let bit = Bit(rawValue: intResult) { return (bit, overflow: overflow) } else {