Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
pre_create_environment:
- |
PREFIX=$(pwd)/.local
ZZ_VERSION=0.9.0a3
ZZ_VERSION=0.9.0a4
ZZ_DIR=zz-${ZZ_VERSION}
GITHUB_URL=https://github.com/diofant/zz/releases/download/
ZZ_URL=${GITHUB_URL}v${ZZ_VERSION}/${ZZ_DIR}.tar.gz
Expand Down
11 changes: 10 additions & 1 deletion bench/collatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ def collatz1(n):
total += 1
return total

def collatz2(n):
total = 0
n = mpz(n)
while n > 1:
n = n*3 + 1 if n & one else n//2
total += 1
return total


runner = pyperf.Runner()
for f in [collatz0, collatz1]:
for f in [collatz0, collatz1, collatz2]:
for v in ["97", "871", "(1<<128)+31"]:
h = f"{f.__name__}({v})"
i = eval(v)
Expand Down
16 changes: 10 additions & 6 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#else
# define MAX_CACHE_SIZE 0
#endif
#define MAX_CACHED_NDIGITS 16
#define MAX_CACHED_SIZEOF 256

typedef struct {
MPZ_Object *gmp_cache[MAX_CACHE_SIZE + 1];
Expand Down Expand Up @@ -660,7 +660,7 @@ dealloc(PyObject *self)
MPZ_Object *u = (MPZ_Object *)self;

if (global.gmp_cache_size < MAX_CACHE_SIZE
&& (u->z).alloc <= MAX_CACHED_NDIGITS
&& zz_sizeof(&u->z) <= MAX_CACHED_SIZEOF
&& MPZ_CheckExact(self))
{
global.gmp_cache[global.gmp_cache_size++] = u;
Expand Down Expand Up @@ -850,8 +850,11 @@ hash(PyObject *self)
return u->hash_cache;
}

zz_digit_t digits[1];
zz_t w = {false, 1, 1, digits};
zz_t w;

if (zz_init(&w)) {
return -1; /* LCOV_EXCL_LINE */
}

assert((int64_t)INT64_MAX > pyhash_modulus);
(void)zz_div(&u->z, (int64_t)pyhash_modulus, NULL, &w);
Expand All @@ -860,6 +863,7 @@ hash(PyObject *self)

assert(sizeof(Py_hash_t) == 8);
(void)zz_get(&w, (int64_t *)&r);
zz_clear(&w);
if (zz_isneg(&u->z) && r) {
r = -(pyhash_modulus - r);
}
Expand Down Expand Up @@ -1880,8 +1884,8 @@ __sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
{
MPZ_Object *u = (MPZ_Object *)self;

return PyLong_FromSize_t(sizeof(MPZ_Object)
+ (size_t)(u->z).alloc*sizeof(zz_digit_t));
return PyLong_FromSize_t(sizeof(MPZ_Object) - sizeof(zz_t)
+ zz_sizeof(&u->z));
}

static PyObject *
Expand Down
2 changes: 1 addition & 1 deletion scripts/cibw_before_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ make --silent all install

cd ..

ZZ_VERSION=0.9.0a3
ZZ_VERSION=0.9.0a4
ZZ_DIR=zz-${ZZ_VERSION}
ZZ_URL=https://github.com/diofant/zz/releases/download/v${ZZ_VERSION}/${ZZ_DIR}.tar.gz

Expand Down