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
5 changes: 3 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ jobs:
timeout-minutes: 20
runs-on: ${{ matrix.os }}
env:
CFLAGS: -Wall -Werror -std=c17 -Wconversion
CFLAGS: -Wall -Werror -Wconversion
strategy:
matrix:
os: [ubuntu-26.04]
python-version: [pypy3.12, "graalpy-25", "3.x"]
python-version: [pypy3.12, "graalpy-25", "3.x", 3.16]
steps:
- uses: actions/checkout@v7
with:
Expand All @@ -18,6 +18,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
cache: 'pip'
- run: sudo apt-get update
- run: sudo apt-get install autoconf-archive lcov
- name: Setup locales
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: check-yaml
- id: check-toml
Expand All @@ -10,13 +10,13 @@ repos:
args: [--pytest-test-first]
exclude: ^tests/utils.py$
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
rev: v0.17.4
hooks:
- id: ruff
name: Run ruff
args: [--exit-non-zero-on-fix]
- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: v1.0.0
rev: v1.0.2
hooks:
- id: sphinx-lint
name: Run sphinx-lint
Expand Down
8 changes: 3 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ integer type (`mpz`_), compatible with Python's `int`_. It also includes
functions, compatible with the Python stdlib's submodule `math.integer
<https://docs.python.org/3.15/library/math.integer.html>`_.

This module requires Python 3.11 or later versions and has been tested with
CPython 3.11 through 3.15, with PyPy3.12 8.0.0 and with GraalPy 25.3.4.1.
Free-threading builds of the CPython are supported.
This module requires Python 3.12 or later versions and has been tested with
CPython 3.12 through 3.16, with PyPy3.12 and with GraalPy3.13. Free-threading
builds of the CPython are supported.

Releases are available in the Python Package Index (PyPI) at
https://pypi.org/project/python-gmp/.
Expand All @@ -36,8 +36,6 @@ will work if you set address space limit for the Python interpreter (e.g. by
.. code:: pycon

>>> import gmpy2
>>> gmpy2.__version__
'2.2.1'
>>> z = gmpy2.mpz(29925959575501)
>>> while True: # this loop will crash interpter
... z = z*z
Expand Down
42 changes: 20 additions & 22 deletions extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
static const zz_layout bytes_layout = {8, 1, 1, 0};

zz_err
zz_get_bytes(const zz_t *u, size_t length, bool is_signed,
unsigned char **buffer)
zz_get_bytes(const zz_t *u, size_t len, bool is_signed, unsigned char **buf)
{
zz_t tmp;
bool is_negative = zz_isneg(u);
Expand All @@ -21,7 +20,7 @@ zz_get_bytes(const zz_t *u, size_t length, bool is_signed,
if (!is_signed) {
return ZZ_BUF;
}
if (zz_set(1, &tmp) || zz_mul_2exp(&tmp, 8*length, &tmp)
if (zz_set(1, &tmp) || zz_mul_2exp(&tmp, 8*len, &tmp)
|| zz_add(&tmp, u, &tmp))
{
/* LCOV_EXCL_START */
Expand All @@ -34,43 +33,42 @@ zz_get_bytes(const zz_t *u, size_t length, bool is_signed,

size_t nbits = zz_bitlen(u);

if (zz_isneg(u) || nbits > 8*length
if (zz_isneg(u) || nbits > 8*len
|| (is_signed && ((!nbits && is_negative)
|| (nbits && (nbits == 8 * length ? !is_negative : is_negative)))))
|| (nbits && (nbits == 8 * len ? !is_negative : is_negative)))))
{
zz_clear(&tmp);
return ZZ_BUF;
}

size_t gap = length - (nbits + bits_per_digit/8 - 1)/(bits_per_digit/8);
size_t gap = len - (nbits + bits_per_digit/8 - 1)/(bits_per_digit/8);

(void)zz_export(u, bytes_layout, length - gap, *buffer + gap);
memset(*buffer, is_negative ? 0xFF : 0, gap);
(void)zz_export(u, bytes_layout, len - gap, *buf + gap);
memset(*buf, is_negative ? 0xFF : 0, gap);
zz_clear(&tmp);
return ZZ_OK;
}

zz_err
zz_set_bytes(const unsigned char *buffer, size_t length, bool is_signed,
zz_t *u)
zz_set_bytes(const unsigned char *buf, size_t len, bool is_signed, zz_t *u)
{
if (!length) {
if (!len) {
return zz_set(0, u);
}

zz_err ret = zz_import(length, buffer, bytes_layout, u);
zz_err ret = zz_import(len, buf, bytes_layout, u);

if (ret) {
return ret; /* LCOV_EXCL_LINE */
}
(void)zz_abs(u, u);
if (is_signed && zz_bitlen(u) == 8*(size_t)length) {
if (is_signed && zz_bitlen(u) == 8*(size_t)len) {
zz_t tmp;

/* we assume, that bytes were created by zz_get_bytes(),
else there could be overflow in zz_mul_2exp() */
if (zz_init(&tmp) || zz_set(1, &tmp)
|| zz_mul_2exp(&tmp, 8*length, &tmp)
|| zz_mul_2exp(&tmp, 8*len, &tmp)
|| zz_sub(&tmp, u, u) || zz_neg(u, u))
{
/* LCOV_EXCL_START */
Expand Down Expand Up @@ -150,13 +148,13 @@ zz_divnear(const zz_t *u, const zz_t *v, zz_t *q, zz_t *r)
}

zz_err
zz_truediv(const zz_t *u, const zz_t *v, double *res)
zz_truediv(const zz_t *u, const zz_t *v, double *w)
{
if (zz_iszero(v)) {
return ZZ_VAL;
}
if (zz_iszero(u)) {
*res = zz_isneg(v) ? -0.0 : 0.0;
*w = zz_isneg(v) ? -0.0 : 0.0;
return ZZ_OK;
}

Expand All @@ -167,7 +165,7 @@ zz_truediv(const zz_t *u, const zz_t *v, double *res)
return ZZ_BUF;
}
if (ubits < vbits && vbits - ubits > -DBL_MIN_EXP + DBL_MANT_DIG + 1) {
*res = zz_isneg(u) != zz_isneg(v) ? -0.0 : 0.0;
*w = zz_isneg(u) != zz_isneg(v) ? -0.0 : 0.0;
return ZZ_OK;
}

Expand Down Expand Up @@ -241,13 +239,13 @@ zz_truediv(const zz_t *u, const zz_t *v, double *res)
/* LCOV_EXCL_STOP */
}
zz_clear(&b);
(void)zz_get(&a, res);
(void)zz_get(&a, w);
zz_clear(&a);
*res = ldexp(*res, -shift);
*w = ldexp(*w, -shift);
if (zz_isneg(u) != zz_isneg(v)) {
*res = -*res;
*w = -*w;
}
if (isinf(*res)) {
if (isinf(*w)) {
return ZZ_BUF;
}
return ZZ_OK;
Expand Down Expand Up @@ -285,7 +283,7 @@ zz_rshift(const zz_t *u, const zz_t *v, zz_t *w)

zz_err
zz_mpmath_normalize(zz_bitcnt_t prec, zz_rnd rnd, bool *negative,
zz_t *man, zz_t *exp, zz_bitcnt_t *bc)
zz_t *man, zz_t *exp, zz_bitcnt_t *bc)
{
/* If the mantissa is 0, return the normalized representation. */
if (zz_iszero(man)) {
Expand Down
8 changes: 4 additions & 4 deletions extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

extern uint8_t bits_per_digit;

zz_err zz_get_bytes(const zz_t *u, size_t length, bool is_signed,
unsigned char **buffer);
zz_err zz_set_bytes(const unsigned char *buffer, size_t length,
zz_err zz_get_bytes(const zz_t *u, size_t len, bool is_signed,
unsigned char **buf);
zz_err zz_set_bytes(const unsigned char *buf, size_t len,
bool is_signed, zz_t *u);

#define zz_quo(u, v, w) zz_div((u), (v), (w), NULL)
#define zz_rem(u, v, w) zz_div((u), (v), NULL, (w))

zz_err zz_divnear(const zz_t *u, const zz_t *v, zz_t *q, zz_t *r);
zz_err zz_truediv(const zz_t *u, const zz_t *v, double *res);

zz_err zz_truediv(const zz_t *u, const zz_t *v, double *w);
zz_err zz_lshift(const zz_t *u, const zz_t *v, zz_t *w);
zz_err zz_rshift(const zz_t *u, const zz_t *v, zz_t *w);

Expand Down
Loading
Loading