Skip to content

Commit

Permalink
Use int instead of char for flag variables
Browse files Browse the repository at this point in the history
The char type can be equivalent to either signed char or unsigned char.
If the latter, assigning -1 to endian in GMPy_MPZ_Method_From_Bytes
actually stores the value 255, a positive value.  This leads to
incorrect conversions, manifesting as failure of test_mpz_from_bytes.

Using char doesn't save any space, since local variables are stored in
32- or 64-bit CPU registers, or in 32-bit aligned stack slots.  Also,
using char instead of int generates less efficient code.  Modern 64-bit
processors are optimized for 64- and 32-bit quantities.  Operating on
8-bit quantities takes more clock cycles.  Finally, negative values
cannot be stored safely in char variables due to implicit conversion to
positive values of platforms where char == unsigned char.
  • Loading branch information
jamesjer committed Jul 11, 2024
1 parent fe17d8b commit 4b3aee6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/gmpy2_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ GMPy_RemoveIgnoredASCII(PyObject *s)
static int
mpz_set_PyStr(mpz_t z, PyObject *s, int base)
{
char *cp, negative = 0;
char *cp;
int negative = 0;
PyObject *ascii_str;

ascii_str = GMPy_RemoveIgnoredASCII(s);
Expand Down
8 changes: 4 additions & 4 deletions src/gmpy2_mpz_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,8 +1869,8 @@ GMPy_MPZ_Method_To_Bytes(PyObject *self, PyObject *const *args,
Py_ssize_t i, nkws = 0, size, gap, length = 1;
PyObject *bytes, *arg;
mpz_t tmp, *px = &MPZ(self);
char *buffer, sign, is_signed = 0, is_negative, is_big;
int argidx[2] = {-1, -1};
char *buffer;
int sign, is_signed = 0, is_negative, is_big, argidx[2] = {-1, -1};
const char *byteorder = NULL, *kwname;

if (nargs > 2) {
Expand Down Expand Up @@ -2023,8 +2023,8 @@ GMPy_MPZ_Method_From_Bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t
{
Py_ssize_t i, nkws = 0, length;
PyObject *arg, *bytes;
char is_signed = 0, endian, *buffer;
int argidx[2] = {-1, -1};
char *buffer;
int is_signed = 0, endian, argidx[2] = {-1, -1};
const char *byteorder = NULL, *kwname;
mpz_t tmp;
MPZ_Object *result;
Expand Down

0 comments on commit 4b3aee6

Please sign in to comment.