Skip to content

Commit 5957d10

Browse files
committed
using pythoncpi-compat
1 parent 146ba87 commit 5957d10

4 files changed

Lines changed: 29 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,5 @@ compile_commands.json
141141
# quaddtype
142142
/quaddtype/subprojects/qblas/
143143
/quaddtype/subprojects/sleef/
144+
/quaddtype/subprojects/pythoncapi-compat/
144145
.wraplock

quaddtype/meson.build

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ incdir_numpy = run_command(py,
7979
check : true
8080
).stdout().strip()
8181

82+
# pythoncapi-compat for portable C API usage across Python versions
83+
pythoncapi_compat_subproj = subproject('pythoncapi-compat')
84+
pythoncapi_compat_inc = pythoncapi_compat_subproj.get_variable('incdir')
85+
8286
# print numpy version used
8387
numpy_version = run_command(py,
8488
['-c', 'import numpy; print(numpy.__version__)'],
@@ -154,6 +158,7 @@ includes = include_directories(
154158
'numpy_quaddtype/src',
155159
]
156160
)
161+
pythoncapi_includes = pythoncapi_compat_inc
157162

158163
srcs = [
159164
'numpy_quaddtype/src/quad_common.h',
@@ -208,5 +213,5 @@ py.extension_module('_quaddtype_main',
208213
dependencies: dependencies,
209214
install: true,
210215
subdir: 'numpy_quaddtype',
211-
include_directories: [includes, build_includes],
216+
include_directories: [includes, build_includes, pythoncapi_includes],
212217
)

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lock.h"
1919
#include "utilities.h"
2020
#include "constants.hpp"
21+
#include "pythoncapi_compat.h"
2122

2223

2324
QuadPrecisionObject *
@@ -638,21 +639,13 @@ static PyGetSetDef QuadPrecision_getset[] = {
638639
* hash(QuadPrecision(1.0)) == hash(1.0) == hash(1)
639640
*
640641
* The algorithm:
641-
* 1. Handle special cases: inf returns PYHASH_INF, nan uses pointer hash
642+
* 1. Handle special cases: inf returns PyHASH_INF, nan uses pointer hash
642643
* 2. Extract mantissa m in [0.5, 1.0) and exponent e via frexp(v) = m * 2^e
643644
* 3. Process mantissa 28 bits at a time, accumulating into hash value x
644-
* 4. Adjust for exponent using bit rotation (since 2^PYHASH_BITS ≡ 1 mod P)
645+
* 4. Adjust for exponent using bit rotation (since 2^PyHASH_BITS ≡ 1 mod P)
645646
* 5. Apply sign and handle the special case of -1 -> -2
646647
*/
647648

648-
#if SIZEOF_VOID_P >= 8
649-
# define PYHASH_BITS 61
650-
#else
651-
# define PYHASH_BITS 31
652-
#endif
653-
#define PYHASH_MODULUS (((Py_uhash_t)1 << PYHASH_BITS) - 1)
654-
#define PYHASH_INF 314159
655-
656649
static Py_hash_t
657650
QuadPrecision_hash(QuadPrecisionObject *self)
658651
{
@@ -669,14 +662,14 @@ QuadPrecision_hash(QuadPrecisionObject *self)
669662
// Check for NaN - use pointer hash (each NaN instance gets unique hash)
670663
// This prevents hash table catastrophic pileups from NaN instances
671664
if (Sleef_iunordq1(value, value)) {
672-
return _Py_HashPointer((void *)self);
665+
return Py_HashPointer((void *)self);
673666
}
674667

675668
if (Sleef_icmpeqq1(value, QUAD_PRECISION_INF)) {
676-
return PYHASH_INF;
669+
return PyHASH_INF;
677670
}
678671
if (Sleef_icmpeqq1(value, QUAD_PRECISION_NINF)) {
679-
return -PYHASH_INF;
672+
return -PyHASH_INF;
680673
}
681674

682675
// Handle sign
@@ -698,8 +691,8 @@ QuadPrecision_hash(QuadPrecisionObject *self)
698691

699692
// Continue until mantissa becomes zero (all bits processed)
700693
while (Sleef_icmpneq1(mantissa, zero)) {
701-
// Rotate x left by 28 bits within PYHASH_MODULUS
702-
x = ((x << 28) & PYHASH_MODULUS) | (x >> (PYHASH_BITS - 28));
694+
// Rotate x left by 28 bits within PyHASH_MODULUS
695+
x = ((x << 28) & PyHASH_MODULUS) | (x >> (PyHASH_BITS - 28));
703696

704697
// Scale mantissa by 2^28
705698
mantissa = Sleef_mulq1_u05(mantissa, multiplier);
@@ -714,19 +707,19 @@ QuadPrecision_hash(QuadPrecisionObject *self)
714707

715708
// Accumulate
716709
x += y;
717-
if (x >= PYHASH_MODULUS) {
718-
x -= PYHASH_MODULUS;
710+
if (x >= PyHASH_MODULUS) {
711+
x -= PyHASH_MODULUS;
719712
}
720713
}
721714

722-
// Adjust for exponent: reduce e modulo PYHASH_BITS
723-
// For negative exponents: PYHASH_BITS - 1 - ((-1 - e) % PYHASH_BITS)
715+
// Adjust for exponent: reduce e modulo PyHASH_BITS
716+
// For negative exponents: PyHASH_BITS - 1 - ((-1 - e) % PyHASH_BITS)
724717
int e = exponent >= 0
725-
? exponent % PYHASH_BITS
726-
: PYHASH_BITS - 1 - ((-1 - exponent) % PYHASH_BITS);
718+
? exponent % PyHASH_BITS
719+
: PyHASH_BITS - 1 - ((-1 - exponent) % PyHASH_BITS);
727720

728721
// Rotate x left by e bits
729-
x = ((x << e) & PYHASH_MODULUS) | (x >> (PYHASH_BITS - e));
722+
x = ((x << e) & PyHASH_MODULUS) | (x >> (PyHASH_BITS - e));
730723

731724
// Apply sign
732725
x = x * sign;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[wrap-git]
2+
directory=pythoncapi-compat
3+
url=https://github.com/python/pythoncapi-compat.git
4+
revision=main
5+
patch_directory = pythoncapi-compat
6+
[provide]
7+
pythoncapi_compat = pythoncapi_compat_dep

0 commit comments

Comments
 (0)