From 909a77c3d90052ade70ab8003112e6bab84f10ec Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 26 Aug 2021 14:51:05 -0400 Subject: [PATCH 01/11] Create GCD.swift Binary GCD algorithm --- Sources/IntegerUtilities/GCD.swift | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Sources/IntegerUtilities/GCD.swift diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift new file mode 100644 index 0000000..f1c471b --- /dev/null +++ b/Sources/IntegerUtilities/GCD.swift @@ -0,0 +1,44 @@ +//===--- GCD.swift --------------------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +extension BinaryInteger { + + /// The greatest common divisor of `a` and `b`. + /// + /// If both inputs are zero, the result is zero. + @inlinable + public static func gcd(_ a: Self, _ b: Self) -> Self { + var x = a.magnitude + var y = b.magnitude + + if x == 0 { return Self(y) } + if y == 0 { return Self(x) } + + let xtz = x.trailingZeroBitCount + let ytz = y.trailingZeroBitCount + + x >>= xtz + y >>= ytz + + // The binary GCD algorithm + // + // At the top of the loop both x and y are odd. Each pass removes at least + // one low-order bit from the larger of the two, so the number of iterations + // is bounded by the sum of the bit-widths of the inputs. + while x != 0 { + if x < y { swap(&x, &y) } + x -= y + x >>= x.trailingZeroBitCount + } + + return Self(y << min(xtz, ytz)) + } +} From 632919ce2a8f50c4878d7707ab5fcba27223d1d0 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 26 Aug 2021 14:56:04 -0400 Subject: [PATCH 02/11] Create GCDTests.swift --- Tests/IntegerUtilitiesTests/GCDTests.swift | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Tests/IntegerUtilitiesTests/GCDTests.swift diff --git a/Tests/IntegerUtilitiesTests/GCDTests.swift b/Tests/IntegerUtilitiesTests/GCDTests.swift new file mode 100644 index 0000000..5fb15c7 --- /dev/null +++ b/Tests/IntegerUtilitiesTests/GCDTests.swift @@ -0,0 +1,38 @@ +//===--- GCDTests.swift ---------------------------------------*- swift -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import IntegerUtilities +import XCTest + +final class IntegerUtilitiesGCDTests: XCTestCase { + func testGCDInt() { + XCTAssertEqual(Int.gcd(0, 0), 0) + XCTAssertEqual(Int.gcd(0, 1), 1) + XCTAssertEqual(Int.gcd(1, 0), 1) + XCTAssertEqual(Int.gcd(0, -1), 1) + XCTAssertEqual(Int.gcd(1, 1), 1) + XCTAssertEqual(Int.gcd(1, 2), 1) + XCTAssertEqual(Int.gcd(2, 2), 2) + XCTAssertEqual(Int.gcd(4, 2), 2) + XCTAssertEqual(Int.gcd(6, 8), 2) + XCTAssertEqual(Int.gcd(77, 91), 7) + XCTAssertEqual(Int.gcd(24, -36), 12) + XCTAssertEqual(Int.gcd(-24, -36), 12) + XCTAssertEqual(Int.gcd(51, 34), 17) + XCTAssertEqual(Int.gcd(64, 96), 32) + XCTAssertEqual(Int.gcd(-64, 96), 32) + XCTAssertEqual(Int.gcd(4*7*19, 27*25), 1) + XCTAssertEqual(Int.gcd(16*315, 11*315), 315) + XCTAssertEqual(Int.gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) + XCTAssertEqual(Int.gcd(Int.min, 2), 2) + } +} From 4fbcb11f039e39c2122505715cad12e0bfd2ac1e Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 26 Aug 2021 19:49:13 -0400 Subject: [PATCH 03/11] Improved GCD documentation --- Sources/IntegerUtilities/GCD.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift index f1c471b..c8c2400 100644 --- a/Sources/IntegerUtilities/GCD.swift +++ b/Sources/IntegerUtilities/GCD.swift @@ -13,7 +13,10 @@ extension BinaryInteger { /// The greatest common divisor of `a` and `b`. /// - /// If both inputs are zero, the result is zero. + /// If both inputs are zero, the result is zero. If one input is zero, the + /// result is the absolute value of the other input. + /// + /// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor @inlinable public static func gcd(_ a: Self, _ b: Self) -> Self { var x = a.magnitude From b8f452189bc15f8a21cef4471c828c136af6acdd Mon Sep 17 00:00:00 2001 From: NevinBR Date: Mon, 30 Aug 2021 17:28:44 -0400 Subject: [PATCH 04/11] Made gcd a free function --- Sources/IntegerUtilities/GCD.swift | 63 ++++++++++++++---------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift index c8c2400..f67014a 100644 --- a/Sources/IntegerUtilities/GCD.swift +++ b/Sources/IntegerUtilities/GCD.swift @@ -9,39 +9,36 @@ // //===----------------------------------------------------------------------===// -extension BinaryInteger { +/// The greatest common divisor of `a` and `b`. +/// +/// If both inputs are zero, the result is zero. If one input is zero, the +/// result is the absolute value of the other input. +/// +/// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor +@inlinable +public func gcd(_ a: T, _ b: T) -> T { + var x = a.magnitude + var y = b.magnitude - /// The greatest common divisor of `a` and `b`. - /// - /// If both inputs are zero, the result is zero. If one input is zero, the - /// result is the absolute value of the other input. - /// - /// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor - @inlinable - public static func gcd(_ a: Self, _ b: Self) -> Self { - var x = a.magnitude - var y = b.magnitude - - if x == 0 { return Self(y) } - if y == 0 { return Self(x) } - - let xtz = x.trailingZeroBitCount - let ytz = y.trailingZeroBitCount - - x >>= xtz - y >>= ytz - - // The binary GCD algorithm - // - // At the top of the loop both x and y are odd. Each pass removes at least - // one low-order bit from the larger of the two, so the number of iterations - // is bounded by the sum of the bit-widths of the inputs. - while x != 0 { - if x < y { swap(&x, &y) } - x -= y - x >>= x.trailingZeroBitCount - } - - return Self(y << min(xtz, ytz)) + if x == 0 { return T(y) } + if y == 0 { return T(x) } + + let xtz = x.trailingZeroBitCount + let ytz = y.trailingZeroBitCount + + x >>= xtz + y >>= ytz + + // The binary GCD algorithm + // + // At the top of the loop both x and y are odd. Each pass removes at least + // one low-order bit from the larger of the two, so the number of iterations + // is bounded by the sum of the bit-widths of the inputs. + while x != 0 { + if x < y { swap(&x, &y) } + x -= y + x >>= x.trailingZeroBitCount } + + return T(y << min(xtz, ytz)) } From 83c378f2f47cb5e2a0741b535e7abc257d6dd685 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Mon, 30 Aug 2021 17:29:05 -0400 Subject: [PATCH 05/11] Update GCDTests.swift --- Tests/IntegerUtilitiesTests/GCDTests.swift | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Tests/IntegerUtilitiesTests/GCDTests.swift b/Tests/IntegerUtilitiesTests/GCDTests.swift index 5fb15c7..e095bf7 100644 --- a/Tests/IntegerUtilitiesTests/GCDTests.swift +++ b/Tests/IntegerUtilitiesTests/GCDTests.swift @@ -15,24 +15,24 @@ import XCTest final class IntegerUtilitiesGCDTests: XCTestCase { func testGCDInt() { - XCTAssertEqual(Int.gcd(0, 0), 0) - XCTAssertEqual(Int.gcd(0, 1), 1) - XCTAssertEqual(Int.gcd(1, 0), 1) - XCTAssertEqual(Int.gcd(0, -1), 1) - XCTAssertEqual(Int.gcd(1, 1), 1) - XCTAssertEqual(Int.gcd(1, 2), 1) - XCTAssertEqual(Int.gcd(2, 2), 2) - XCTAssertEqual(Int.gcd(4, 2), 2) - XCTAssertEqual(Int.gcd(6, 8), 2) - XCTAssertEqual(Int.gcd(77, 91), 7) - XCTAssertEqual(Int.gcd(24, -36), 12) - XCTAssertEqual(Int.gcd(-24, -36), 12) - XCTAssertEqual(Int.gcd(51, 34), 17) - XCTAssertEqual(Int.gcd(64, 96), 32) - XCTAssertEqual(Int.gcd(-64, 96), 32) - XCTAssertEqual(Int.gcd(4*7*19, 27*25), 1) - XCTAssertEqual(Int.gcd(16*315, 11*315), 315) - XCTAssertEqual(Int.gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) - XCTAssertEqual(Int.gcd(Int.min, 2), 2) + XCTAssertEqual(gcd(0, 0), 0) + XCTAssertEqual(gcd(0, 1), 1) + XCTAssertEqual(gcd(1, 0), 1) + XCTAssertEqual(gcd(0, -1), 1) + XCTAssertEqual(gcd(1, 1), 1) + XCTAssertEqual(gcd(1, 2), 1) + XCTAssertEqual(gcd(2, 2), 2) + XCTAssertEqual(gcd(4, 2), 2) + XCTAssertEqual(gcd(6, 8), 2) + XCTAssertEqual(gcd(77, 91), 7) + XCTAssertEqual(gcd(24, -36), 12) + XCTAssertEqual(gcd(-24, -36), 12) + XCTAssertEqual(gcd(51, 34), 17) + XCTAssertEqual(gcd(64, 96), 32) + XCTAssertEqual(gcd(-64, 96), 32) + XCTAssertEqual(gcd(4*7*19, 27*25), 1) + XCTAssertEqual(gcd(16*315, 11*315), 315) + XCTAssertEqual(gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) + XCTAssertEqual(gcd(Int.min, 2), 2) } } From 67af2fe0d9c360869a70bf9798cf3afa8163005e Mon Sep 17 00:00:00 2001 From: NevinBR Date: Mon, 30 Aug 2021 17:32:03 -0400 Subject: [PATCH 06/11] Simplified gcd implementation --- Sources/IntegerUtilities/GCD.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift index f67014a..3e6afdf 100644 --- a/Sources/IntegerUtilities/GCD.swift +++ b/Sources/IntegerUtilities/GCD.swift @@ -20,24 +20,22 @@ public func gcd(_ a: T, _ b: T) -> T { var x = a.magnitude var y = b.magnitude - if x == 0 { return T(y) } if y == 0 { return T(x) } let xtz = x.trailingZeroBitCount let ytz = y.trailingZeroBitCount - x >>= xtz y >>= ytz // The binary GCD algorithm // - // At the top of the loop both x and y are odd. Each pass removes at least - // one low-order bit from the larger of the two, so the number of iterations - // is bounded by the sum of the bit-widths of the inputs. + // After the right-shift in the loop, both x and y are odd. Each pass removes + // at least one low-order bit from the larger of the two, so the number of + // iterations is bounded by the sum of the bit-widths of the inputs. while x != 0 { + x >>= x.trailingZeroBitCount if x < y { swap(&x, &y) } x -= y - x >>= x.trailingZeroBitCount } return T(y << min(xtz, ytz)) From 89c4e42e48dcaeb047e0f5cea49cc4884fe3f79a Mon Sep 17 00:00:00 2001 From: NevinBR Date: Tue, 31 Aug 2021 11:10:40 -0400 Subject: [PATCH 07/11] Added tests for gcd overflow on Int.min --- Tests/IntegerUtilitiesTests/GCDTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/IntegerUtilitiesTests/GCDTests.swift b/Tests/IntegerUtilitiesTests/GCDTests.swift index e095bf7..e4000fd 100644 --- a/Tests/IntegerUtilitiesTests/GCDTests.swift +++ b/Tests/IntegerUtilitiesTests/GCDTests.swift @@ -34,5 +34,9 @@ final class IntegerUtilitiesGCDTests: XCTestCase { XCTAssertEqual(gcd(16*315, 11*315), 315) XCTAssertEqual(gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) XCTAssertEqual(gcd(Int.min, 2), 2) + + XCTExpectFailure{ gcd(0, Int.min) } + XCTExpectFailure{ gcd(Int.min, 0) } + XCTExpectFailure{ gcd(Int.min, Int.min) } } } From 8552becf915b5481be385901fbdfabb7798c585f Mon Sep 17 00:00:00 2001 From: NevinBR Date: Tue, 31 Aug 2021 11:19:22 -0400 Subject: [PATCH 08/11] Documented overflow behavior of gcd --- Sources/IntegerUtilities/GCD.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift index 3e6afdf..8b3d519 100644 --- a/Sources/IntegerUtilities/GCD.swift +++ b/Sources/IntegerUtilities/GCD.swift @@ -14,6 +14,13 @@ /// If both inputs are zero, the result is zero. If one input is zero, the /// result is the absolute value of the other input. /// +/// The resulting value must be representable within the value's type. In +/// particular, the gcd of a signed, fixed-width integer type's minimum +/// with itself or zero results in a value that cannot be represented: +/// +/// gcd(Int.min, Int.min) // Overflow error +/// gcd(Int.min, 0) // Overflow error +/// /// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor @inlinable public func gcd(_ a: T, _ b: T) -> T { From 85264c808c6fb9eecf5f4224495fdce20ac346e9 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 9 Sep 2021 23:05:45 -0400 Subject: [PATCH 09/11] Reworded doc comment for `gcd` --- Sources/IntegerUtilities/GCD.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/IntegerUtilities/GCD.swift b/Sources/IntegerUtilities/GCD.swift index 8b3d519..97e8ad3 100644 --- a/Sources/IntegerUtilities/GCD.swift +++ b/Sources/IntegerUtilities/GCD.swift @@ -14,9 +14,9 @@ /// If both inputs are zero, the result is zero. If one input is zero, the /// result is the absolute value of the other input. /// -/// The resulting value must be representable within the value's type. In -/// particular, the gcd of a signed, fixed-width integer type's minimum -/// with itself or zero results in a value that cannot be represented: +/// The result must be representable within its type. In particular, the gcd +/// of a signed, fixed-width integer type's minimum with itself (or zero) +/// cannot be represented, and results in a trap. /// /// gcd(Int.min, Int.min) // Overflow error /// gcd(Int.min, 0) // Overflow error From bd685695334c36a0c7717887794b5f4f877e778d Mon Sep 17 00:00:00 2001 From: NevinBR Date: Fri, 10 Sep 2021 00:03:31 -0400 Subject: [PATCH 10/11] Commented out `ExpectFailure` tests for `gcd` --- Tests/IntegerUtilitiesTests/GCDTests.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Tests/IntegerUtilitiesTests/GCDTests.swift b/Tests/IntegerUtilitiesTests/GCDTests.swift index e4000fd..b0f339f 100644 --- a/Tests/IntegerUtilitiesTests/GCDTests.swift +++ b/Tests/IntegerUtilitiesTests/GCDTests.swift @@ -35,8 +35,10 @@ final class IntegerUtilitiesGCDTests: XCTestCase { XCTAssertEqual(gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) XCTAssertEqual(gcd(Int.min, 2), 2) - XCTExpectFailure{ gcd(0, Int.min) } - XCTExpectFailure{ gcd(Int.min, 0) } - XCTExpectFailure{ gcd(Int.min, Int.min) } + // TODO: Enable these when version compability allows. + // + // XCTExpectFailure{ gcd(0, Int.min) } + // XCTExpectFailure{ gcd(Int.min, 0) } + // XCTExpectFailure{ gcd(Int.min, Int.min) } } } From f2cfa8d668428ae2e25ac3c15f09ece6cb324bac Mon Sep 17 00:00:00 2001 From: NevinBR Date: Fri, 10 Sep 2021 00:04:53 -0400 Subject: [PATCH 11/11] Fixed typo --- Tests/IntegerUtilitiesTests/GCDTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/IntegerUtilitiesTests/GCDTests.swift b/Tests/IntegerUtilitiesTests/GCDTests.swift index b0f339f..7ada6e4 100644 --- a/Tests/IntegerUtilitiesTests/GCDTests.swift +++ b/Tests/IntegerUtilitiesTests/GCDTests.swift @@ -35,7 +35,7 @@ final class IntegerUtilitiesGCDTests: XCTestCase { XCTAssertEqual(gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8) XCTAssertEqual(gcd(Int.min, 2), 2) - // TODO: Enable these when version compability allows. + // TODO: Enable these when version compatibility allows. // // XCTExpectFailure{ gcd(0, Int.min) } // XCTExpectFailure{ gcd(Int.min, 0) }