Files
swift-numerics/Sources/IntegerUtilities
NevinBR 7f2d022d3d GCD bug fix
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.
2022-02-22 11:46:06 -05:00
..
2022-02-22 11:46:06 -05:00
2021-11-03 13:53:38 -04:00

Integer Utilities

Note: This module is only present on main, and has not yet been stabilized and released in a tag.

Utilities defined for BinaryInteger

The following API are defined for all integer types conforming to BinaryInteger:

  • The gcd(_:_:) free function implements the Greatest Common Divisor operation.

  • The shifted(rightBy:rounding:) method implements bitwise shift with rounding.

  • The divided(by:rounding:) method implements division with specified rounding. (See also SignedInteger.divided(by:rounding:), remainder(dividingBy:rounding:), and euclideanDivision(_:_:) below).

Utilities defined for SignedInteger

The following API are defined for signed integer types:

  • The divided(by:rounding:) method implementing division with specified rounding, returning both quotient and remainder. This requires a signed type because the remainder is not generally representable for unsigned types. This is a disfavored overload; by default, you will get only the quotient as the result:

    let p = 5.divided(by: 3, rounding: .up)      // p = 2
    let (q, r) = 5.divided(by: 3, rounding: .up) // q = 2, r = -1
    
  • The remainder(dividingBy:rounding:) method implementing the remainder operation; the rounding argument describes how to round the quotient, which is not returned. (The remainder is always exact, and hence is not rounded).

  • The euclideanDivision(_:_:) free function implements Euclidean division. In this operation, the remainder is chosen to always be non-negative. This does not correspond to any rounding rule on the quotient, which is why it uses a distinct API.

Utilities defined for FixedWidthInteger

  • The rotated(right:) and rotated(left:) methods implement bitwise rotation for signed and unsigned integer types. The count parameter may be any BinaryInteger type.

Types

The RoundingRule enum is used with shift, division, and round operations to specify how to round their results to a representable value.