Skip to content
Closed
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
19 changes: 19 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ build:
os: ubuntu-24.04
apt_packages:
- libgmp-dev
<<<<<<< HEAD
- gcc
=======
- build-essential
- pkg-config
jobs:
pre_create_environment:
- |
PREFIX=$(pwd)/.local
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
wget ${ZZ_URL}
tar xzf ${ZZ_DIR}.tar.gz
cd ${ZZ_DIR}
./configure -q --prefix=$PREFIX
make -s
make -s install
>>>>>>> 33cfa0e (Use libzz v0.9.0a4)
tools:
python: "3"
python:
Expand Down
53 changes: 53 additions & 0 deletions bench/collatz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# collatz.py

import os

import pyperf

if os.getenv("T") == "gmpy2.mpz":
from gmpy2 import mpz
elif os.getenv("T") == "flint.fmpz":
from flint import fmpz as mpz
elif os.getenv("T") == "int":
mpz = int
else:
from gmp import mpz

zero = mpz(0)
one = mpz(1)
two = mpz(2)
three = mpz(3)

# https://en.wikipedia.org/wiki/Collatz_conjecture

def collatz0(n):
total = 0
n = mpz(n)
while n > one:
n = n*three + one if n & one else n//two
total += 1
return total

def collatz1(n):
total = 0
n = mpz(n)
while n > 1:
n = n*3 + 1 if n & 1 else n//2
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, collatz2]:
for v in ["97", "871", "(1<<128)+31"]:
h = f"{f.__name__}({v})"
i = eval(v)
runner.bench_func(h, f, i)
30 changes: 30 additions & 0 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#if defined(_MSC_VER)
# define _Thread_local __declspec(thread)
#endif
<<<<<<< HEAD

#if !defined(PYPY_VERSION)
# define CACHE_SIZE (99)
#else
# define CACHE_SIZE (0)
#endif
#define MAX_CACHE_MPZ_LIMBS (64)
=======
#define MAX_CACHED_SIZEOF 256
>>>>>>> ede9094 (Use zz_sizeof())

typedef struct {
MPZ_Object *gmp_cache[CACHE_SIZE + 1];
Expand Down Expand Up @@ -602,8 +606,13 @@ dealloc(PyObject *self)
MPZ_Object *u = (MPZ_Object *)self;
PyTypeObject *type = Py_TYPE(self);

<<<<<<< HEAD
if (global.gmp_cache_size < CACHE_SIZE
&& (u->z).alloc <= MAX_CACHE_MPZ_LIMBS
=======
if (global.gmp_cache_size < MAX_CACHE_SIZE
&& zz_sizeof(&u->z) <= MAX_CACHED_SIZEOF
>>>>>>> ede9094 (Use zz_sizeof())
&& MPZ_CheckExact(self))
{
global.gmp_cache[(global.gmp_cache_size)++] = u;
Expand Down Expand Up @@ -743,19 +752,35 @@ hash(PyObject *self)
return u->hash_cache;
}

<<<<<<< HEAD
bool negative = zz_isneg(&u->z);
=======
zz_t w;

if (zz_init(&w)) {
return -1; /* LCOV_EXCL_LINE */
}
>>>>>>> 3b1f672 (Avoid static zz_t initialization in heap())

if (negative) {
(void)zz_abs(&u->z, &u->z);
}

Py_hash_t r;

<<<<<<< HEAD
assert(-(uint64_t)INT64_MIN > PyHASH_MODULUS);
(void)zz_rem_u64(&u->z, (uint64_t)PyHASH_MODULUS, (uint64_t *)&r);
if (negative) {
(void)zz_neg(&u->z, &u->z);
r = -r;
=======
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);
>>>>>>> 3b1f672 (Avoid static zz_t initialization in heap())
}
if (r == -1) {
r = -2;
Expand Down Expand Up @@ -1523,8 +1548,13 @@ __sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
{
MPZ_Object *u = (MPZ_Object *)self;

<<<<<<< HEAD
return PyLong_FromSize_t(sizeof(MPZ_Object)
+ (unsigned int)(u->z).alloc*sizeof(zz_limb_t));
=======
return PyLong_FromSize_t(sizeof(MPZ_Object) - sizeof(zz_t)
+ zz_sizeof(&u->z));
>>>>>>> ede9094 (Use zz_sizeof())
}

static PyObject *
Expand Down
24 changes: 24 additions & 0 deletions scripts/cibw_before_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,32 @@ unset CFLAGS
# We replace config.guess with configfsf.guess to avoid microarchitecture
# specific code in common code.
rm config.guess && mv configfsf.guess config.guess && chmod +x config.guess
<<<<<<< HEAD
./configure --enable-fat \
--enable-shared \
=======

./configure ${CONFIG_ARGS}

make --silent all install

cd ..

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

download ${ZZ_URL}
tar --extract --file ${ZZ_DIR}.tar.gz
cd ${ZZ_DIR}

if [ "$OSTYPE" = "cygwin" ] && [ "${RUNNER_ARCH}" = "ARM64" ]
then
autoreconf -if
fi

./configure --enable-shared \
>>>>>>> 33cfa0e (Use libzz v0.9.0a4)
--disable-static \
--with-pic \
--disable-alloca \
Expand Down