Skip to content

Commit 3914360

Browse files
committed
Use smallest possible type for GCD divisions in the case of small integers.
1 parent f364ab8 commit 3914360

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/quicktions.pyx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ cdef _py_gcd(ullong a, ullong b):
167167
return a
168168

169169

170+
cdef ullong _c_gcd(ullong a, ullong b):
171+
if a <= <ullong>INT_MAX and b <= <ullong>INT_MAX:
172+
return _igcd[uint](<uint> a, <uint> b)
173+
elif a <= <ullong>LONG_MAX and b <= <ullong>LONG_MAX:
174+
return _igcd[ulong](<ulong> a, <ulong> b)
175+
else:
176+
return _igcd[ullong](a, b)
177+
178+
170179
cdef _gcd_fallback(a, b):
171180
"""Fallback GCD implementation if _PyLong_GCD() is not available.
172181
"""
@@ -1660,7 +1669,7 @@ cdef tuple _parse_fraction(AnyString s, Py_ssize_t s_len):
16601669
# Special case for 'small' numbers: normalise directly in C space.
16611670
if inum and decimal_len:
16621671
idenom = 10 ** <ullong> decimal_len
1663-
igcd = _igcd[ullong](inum, idenom)
1672+
igcd = _c_gcd(inum, idenom)
16641673
if igcd > 1:
16651674
inum //= igcd
16661675
denom = idenom // igcd

0 commit comments

Comments
 (0)