Branch: Tag:

2003-05-19

2003-05-19 09:47:15 by Henrik Grubbström (Grubba) <grubba@grubba.org>

More fixes. It seems INT_TYPE_BITS already was compensated for the sign bit...

Rev: src/modules/Gmp/mpz_glue.c:1.147

2:   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: mpz_glue.c,v 1.146 2003/05/18 14:43:13 grubba Exp $ + || $Id: mpz_glue.c,v 1.147 2003/05/19 09:47:15 grubba Exp $   */      #include "global.h" - RCSID("$Id: mpz_glue.c,v 1.146 2003/05/18 14:43:13 grubba Exp $"); + RCSID("$Id: mpz_glue.c,v 1.147 2003/05/19 09:47:15 grubba Exp $");   #include "gmp_machine.h"   #include "module.h"   
96: Inside #if defined(AUTO_BIGNUM)
   int neg = mpz_sgn (mpz) < 0;    INT_TYPE res = 0;    -  /* Get the index of the highest limb that have bits within the range +  /* Get the index of the highest limb that has bits within the range    * of the INT_TYPE. */    size_t pos = (INT_TYPE_BITS + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS - 1;    -  if (mpz_size (mpz) <= pos + 1) { +  if (mpz_size(mpz) <= pos + 1) {    /* NOTE: INT_TYPE is signed, while GMP_NUMB is unsigned. -  * We subtract 1 from INT_TYPE_BITS to make sure there's -  * place left over for the sign. +  * Thus INT_TYPE_BITS is usually 31 and GMP_NUMB_BITS 32.    */    /* NOTE: In gmp 2.0 -0x80000000 is encoded as -0x00000000. */ - #if INT_TYPE_BITS <= GMP_NUMB_BITS -  mp_limb_t val = mpz_getlimbn (mpz, 0) & GMP_NUMB_MASK; + #if INT_TYPE_BITS == GMP_NUMB_BITS +  mp_limb_t val = mpz_getlimbn(mpz, 0); +  /* NOTE: Overflow is not possible. */ +  if (neg) { +  res = ~((val - 1) & GMP_NUMB_MASK); +  } else { +  res = val & GMP_NUMB_MASK; +  } + #elif INT_TYPE_BITS < GMP_NUMB_BITS +  mp_limb_t val = mpz_getlimbn(mpz, 0) & GMP_NUMB_MASK;    /* Potential overflow if the MSB is set. */ -  if (val & ~((1<<(INT_TYPE_BITS - 1)) - 1)) goto overflow; +  if (val & ~((((mp_limb_t)1)<<INT_TYPE_BITS) - 1)) goto overflow;    if (neg) { -  res = ~((val - 1) & ((1<<(INT_TYPE_BITS - 1))-1)); +  res = ~((val - 1) & ((((mp_limb_t)1)<<INT_TYPE_BITS)-1));    } else { -  res = val & ((1<<(INT_TYPE_BITS - 1))-1); +  res = val & ((((mp_limb_t)1)<<INT_TYPE_BITS)-1);    }   #else    /* FIXME: May need to account for encoding of -0x80000000 here too. */    for (;; pos--) { -  res |= mpz_getlimbn (mpz, pos) & GMP_NUMB_MASK; +  res |= mpz_getlimbn(mpz, pos) & GMP_NUMB_MASK;    if (pos == 0) break; -  if (res >= (INT_TYPE) 1 << (INT_TYPE_BITS - (GMP_NUMB_BITS+1))) goto overflow; +  if (res >= (INT_TYPE) 1 << (INT_TYPE_BITS - GMP_NUMB_BITS)) goto overflow;    res <<= GMP_NUMB_BITS;    }    if (neg) res = -res;