Skip to content

Conversation

@NevinBR
Copy link
Contributor

@NevinBR NevinBR commented Feb 22, 2022

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.

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.
@stephentyrone
Copy link
Member

@swift-ci test

@stephentyrone
Copy link
Member

@swift-ci test macOS

@stephentyrone
Copy link
Member

@swift-ci test

@stephentyrone stephentyrone merged commit b6018fa into apple:main Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants