mirror of
https://github.com/apple/swift-numerics
synced 2026-11-23 04:34:25 +00:00
The standard-library documentation for `trailingZeroBitCount` states: > If the value is zero, then `trailingZeroBitCount` is equal to `bitWidth`. Thus, if `T` is not a fixed-width integer type, and `x == 0`, and `y` has more trailing zeros than the representation of `x`, then the gcd calculation will be incorrect. In particular, the left-shift by `min(xtz, ytz) == xtz` will not “undo” the earlier right-shift by `ytz`, so the final result will be incorrectly right-shifted by `ytz - xtz`. To fix this I have added an early exit when `x == 0`. I am not sure if this is the optimal solution, and I’m open to alternatives. *** This change ensures that `x != 0` at the top of the loop, so I have also converted it from a `while` to a `repeat-while` loop, in order to eliminate a redundant extra comparison on the first iteration. I don’t know if this actually affects performance, and I’m happy to change it back if that’s preferred.
54 lines
1.8 KiB
Swift
54 lines
1.8 KiB
Swift
//===--- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// 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.
|
|
///
|
|
/// 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
|
|
///
|
|
/// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor
|
|
@inlinable
|
|
public func gcd<T: BinaryInteger>(_ 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
|
|
|
|
y >>= ytz
|
|
|
|
// The binary GCD algorithm
|
|
//
|
|
// 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.
|
|
//
|
|
// A tighter bound is the maximum bit-width of the inputs, which is achieved
|
|
// by odd numbers that sum to a power of 2, though the proof is more involved.
|
|
repeat {
|
|
x >>= x.trailingZeroBitCount
|
|
if x < y { swap(&x, &y) }
|
|
x -= y
|
|
} while x != 0
|
|
|
|
return T(y << min(xtz, ytz))
|
|
}
|