Skip to content

Commit

Permalink
Use #trailing_zeros_count in Int#gcd (#14069)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Dec 9, 2023
1 parent d7f2512 commit 80f59e7
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -487,30 +487,23 @@ struct Int
return v if u == 0
return u if v == 0

shift = self.class.zero
# Let shift := lg K, where K is the greatest power of 2
# dividing both u and v.
while (u | v) & 1 == 0
shift &+= 1
u = u.unsafe_shr 1
v = v.unsafe_shr 1
end
while u & 1 == 0
u = u.unsafe_shr 1
end
shift = (u | v).trailing_zeros_count
u = u.unsafe_shr(u.trailing_zeros_count)

# From here on, u is always odd.
loop do
# remove all factors of 2 in v -- they are not common
# note: v is not zero, so while will terminate
while v & 1 == 0
v = v.unsafe_shr 1
end
v = v.unsafe_shr(v.trailing_zeros_count)

# Now u and v are both odd. Swap if necessary so u <= v,
# then set v = v - u (which is even).
u, v = v, u if u > v
v &-= u
break if v.zero?
end

# restore common factors of 2
u.unsafe_shl shift
end
Expand Down

0 comments on commit 80f59e7

Please sign in to comment.