Skip to content

Commit 6e2db49

Browse files
committed
Mark critical functions as "nogil" to prevent Python interactions from sneaking in.
1 parent 1af3c24 commit 6e2db49

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/quicktions.pyx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ cdef extern from *:
190190
object _PyLong_GCD(object a, object b)
191191

192192
const bint HAS_FAST_CTZ_uint "__Quicktions_HAS_FAST_CTZ_uint"
193-
int trailing_zeros_uint "__Quicktions_trailing_zeros_uint" (unsigned int x)
193+
int trailing_zeros_uint "__Quicktions_trailing_zeros_uint" (unsigned int x) nogil
194194
const bint HAS_FAST_CTZ_ulong "__Quicktions_HAS_FAST_CTZ_ulong"
195-
int trailing_zeros_ulong "__Quicktions_trailing_zeros_ulong" (unsigned long x)
195+
int trailing_zeros_ulong "__Quicktions_trailing_zeros_ulong" (unsigned long x) nogil
196196
const bint HAS_FAST_CTZ_ullong "__Quicktions_HAS_FAST_CTZ_ullong"
197-
int trailing_zeros_ullong "__Quicktions_trailing_zeros_ullong" (unsigned long long x)
197+
int trailing_zeros_ullong "__Quicktions_trailing_zeros_ullong" (unsigned long long x) nogil
198198

199199

200200
def _gcd(a, b):
@@ -233,21 +233,21 @@ ctypedef fused cunumber:
233233
uint
234234

235235

236-
cdef ullong _abs(long long x) noexcept:
236+
cdef ullong _abs(long long x) noexcept nogil:
237237
if x == PY_LLONG_MIN:
238238
return (<ullong>PY_LLONG_MAX) + 1
239239
return abs(x)
240240

241241

242242
@cython.cdivision(True)
243-
cdef cunumber _euclid_gcd(cunumber a, cunumber b) noexcept:
243+
cdef cunumber _euclid_gcd(cunumber a, cunumber b) noexcept nogil:
244244
"""Euclid's GCD algorithm"""
245245
while b:
246246
a, b = b, a%b
247247
return a
248248

249249

250-
cdef inline int trailing_zeros(cunumber x) noexcept:
250+
cdef inline int trailing_zeros(cunumber x) noexcept nogil:
251251
if cunumber is uint:
252252
return trailing_zeros_uint(x)
253253
elif cunumber is ulong:
@@ -256,7 +256,7 @@ cdef inline int trailing_zeros(cunumber x) noexcept:
256256
return trailing_zeros_ullong(x)
257257

258258

259-
cdef cunumber _binary_gcd(cunumber a, cunumber b) noexcept:
259+
cdef cunumber _binary_gcd(cunumber a, cunumber b) noexcept nogil:
260260
# See https://en.wikipedia.org/wiki/Binary_GCD_algorithm
261261
if not a:
262262
return b
@@ -280,7 +280,7 @@ cdef cunumber _binary_gcd(cunumber a, cunumber b) noexcept:
280280

281281

282282
@cython.cdivision(True)
283-
cdef cunumber _hybrid_binary_gcd(cunumber a, cunumber b) noexcept:
283+
cdef cunumber _hybrid_binary_gcd(cunumber a, cunumber b) noexcept nogil:
284284
# See https://lemire.me/blog/2024/04/13/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
285285
if a < b:
286286
a,b = b,a
@@ -315,7 +315,7 @@ cdef cunumber _hybrid_binary_gcd(cunumber a, cunumber b) noexcept:
315315

316316

317317
@cython.cdivision(True)
318-
cdef cunumber _hybrid_binary_gcd2(cunumber a, cunumber b) noexcept:
318+
cdef cunumber _hybrid_binary_gcd2(cunumber a, cunumber b) noexcept nogil:
319319
# See https://en.algorithmica.org/hpc/algorithms/gcd/
320320
# See https://lemire.me/blog/2024/04/13/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
321321
if a < b:
@@ -348,7 +348,7 @@ cdef _py_gcd(ullong a, ullong b):
348348
return _c_gcd(a, b) if b else a
349349

350350

351-
cdef ullong _c_gcd_euclid(ullong a, ullong b):
351+
cdef ullong _c_gcd_euclid(ullong a, ullong b) nogil:
352352
if not b:
353353
return a
354354
if a <= <ullong>INT_MAX*2+1 and b <= <ullong>INT_MAX*2+1:
@@ -359,7 +359,7 @@ cdef ullong _c_gcd_euclid(ullong a, ullong b):
359359
return _euclid_gcd[ullong](a, b)
360360

361361

362-
cdef ullong _c_gcd_binary(ullong a, ullong b):
362+
cdef ullong _c_gcd_binary(ullong a, ullong b) nogil:
363363
if not b:
364364
return a
365365
if HAS_FAST_CTZ_uint and a <= <ullong>INT_MAX*2+1 and b <= <ullong>INT_MAX*2+1:
@@ -372,7 +372,7 @@ cdef ullong _c_gcd_binary(ullong a, ullong b):
372372
return _c_gcd_euclid(a, b)
373373

374374

375-
cdef ullong _c_gcd_hybrid2(ullong a, ullong b):
375+
cdef ullong _c_gcd_hybrid2(ullong a, ullong b) nogil:
376376
if not b:
377377
return a
378378
if HAS_FAST_CTZ_uint and a <= <ullong>INT_MAX*2+1 and b <= <ullong>INT_MAX*2+1:
@@ -385,7 +385,7 @@ cdef ullong _c_gcd_hybrid2(ullong a, ullong b):
385385
return _c_gcd_euclid(a, b)
386386

387387

388-
cdef ullong _c_gcd_hybrid(ullong a, ullong b):
388+
cdef ullong _c_gcd_hybrid(ullong a, ullong b) nogil:
389389
if not b:
390390
return a
391391
if HAS_FAST_CTZ_uint and a <= <ullong>INT_MAX and b <= <ullong>INT_MAX:
@@ -398,7 +398,7 @@ cdef ullong _c_gcd_hybrid(ullong a, ullong b):
398398
return _c_gcd_euclid(a, b)
399399

400400

401-
ctypedef ullong (*fast_cgcd)(ullong a, ullong b)
401+
ctypedef ullong (*fast_cgcd)(ullong a, ullong b) nogil
402402

403403
cdef fast_cgcd _c_gcd = NULL
404404
cdef fast_cgcd _c_gcd_best_hybrid = NULL

0 commit comments

Comments
 (0)