=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/cmo-gmp.c,v retrieving revision 1.1.1.1 retrieving revision 1.7 diff -u -p -r1.1.1.1 -r1.7 --- OpenXM/src/kan96xx/plugin/cmo-gmp.c 1999/10/08 02:12:05 1.1.1.1 +++ OpenXM/src/kan96xx/plugin/cmo-gmp.c 2003/08/26 05:04:41 1.7 @@ -1,6 +1,8 @@ +/*$OpenXM: OpenXM/src/kan96xx/plugin/cmo-gmp.c,v 1.6 2001/05/04 01:06:29 takayama Exp $ */ #include #include -#include +/* #include */ +#include #include "datatype.h" #include "stackm.h" #include "extern.h" @@ -9,8 +11,9 @@ #include "kclass.h" #include "gmp.h" -#include "gmp-impl.h" +#include "gmp-impl.h" + #include "file2.h" #include "cmo.h" @@ -72,57 +75,8 @@ static outRawInt32(int k) size_t cmoOutGMPCoeff_old(mpz_srcptr x) { - int i; - mp_size_t s; - mp_size_t xsize = ABS (x->_mp_size); - mp_srcptr xp = x->_mp_d; - mp_size_t out_bytesize; - mp_limb_t hi_limb; - int n_bytes_in_hi_limb; - cmoint tmp[1]; - tmp[0] = htonl(CMO_ZZ_OLD); - cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint)); - - if (xsize == 0) - { - for (i = 4 - 1; i >= 0; i--) - myfputc (0); - return 4; - } - - hi_limb = xp[xsize - 1]; - for (i = BYTES_PER_MP_LIMB - 1; i > 0; i--) - { - if ((hi_limb >> i * BITS_PER_CHAR) != 0) - break; - } - n_bytes_in_hi_limb = i + 1; - out_bytesize = BYTES_PER_MP_LIMB * (xsize - 1) + n_bytes_in_hi_limb; - if (x->_mp_size < 0) - out_bytesize = -out_bytesize; - - /* Make the size 4 bytes on all machines, to make the format portable. */ - for (i = 4 - 1; i >= 0; i--) - myfputc ((out_bytesize >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR)); - - /* Output from the most significant limb to the least significant limb, - with each limb also output in decreasing significance order. */ - - /* Output the most significant limb separately, since we will only - output some of its bytes. */ - for (i = n_bytes_in_hi_limb - 1; i >= 0; i--) - myfputc ((hi_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR)); - - /* Output the remaining limbs. */ - for (s = xsize - 2; s >= 0; s--) - { - mp_limb_t x_limb; - - x_limb = xp[s]; - for (i = BYTES_PER_MP_LIMB - 1; i >= 0; i--) - myfputc ((x_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR)); - } - return ( ABS (out_bytesize) + 4); + fprintf(stderr,"cmoOutGMPCoeff_old is no longer supported.\n"); + exit(10); } @@ -161,87 +115,144 @@ static int myfgetc(struct cmoBuffer *cb) cb->rpos--; errorCmo(" cmo-gmp.c : myfgetc(): no data in the buffer."); } - return(k); } + return(k); } +#if BYTES_PER_MP_LIMB == 8 +static unsigned int getRawInt32(struct cmoBuffer *cb) +{ + char d[4]; + int i; + int r; + for (i=0; i<4; i++) { + d[i] = myfgetc(cb); + } + return( ntohl(* ( (unsigned int *)d))); +} +#elif BYTES_PER_MP_LIMB == 4 static int getRawInt32(struct cmoBuffer *cb) { char d[4]; int i; + int r; for (i=0; i<4; i++) { d[i] = myfgetc(cb); } - return(ntohl(* ( (int *)d))); + return( ntohl(* ( (int *)d))); } +#endif cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) { + fprintf(stderr,"cmoGetGMPCoeff_old is no longer supported.\n"); + exit(10); +} + +/*****************************************************/ +/***** new version for CMO_ZZ *********************/ +/*****************************************************/ +#if BYTES_PER_MP_LIMB == 8 +size_t +cmoOutGMPCoeff_new(mpz_srcptr x) +{ int i; mp_size_t s; + mp_size_t xsize = ABS (x->_mp_size); + mp_size_t xsize0; + mp_srcptr xp = x->_mp_d; + mp_size_t out_bytesize; + mp_limb_t hi_limb; + int n_bytes_in_hi_limb; + cmoint tmp[1]; + tmp[0] = htonl(CMO_ZZ); + cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint)); + + if (CHAR_BIT != 8) { + fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); + fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); + exit(10); + } + + if (xsize == 0) + { + outRawInt32(0); + return 1; + } + + xsize0 = xsize; + if ( (unsigned long)xp[xsize-1] < (unsigned long)0x100000000 ) + xsize = xsize0*2-1; + else + xsize = xsize0*2; + + if (x->_mp_size >= 0) + outRawInt32(xsize); + else + outRawInt32(-xsize); + + for (s = 0; s < xsize0-1; s++) + { + outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff); + outRawInt32(((unsigned long)xp[s])>>32); + } + if ( !(xsize&1) ) { + outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff); + outRawInt32(((unsigned long)xp[s])>>32); + } else + outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff); + return ( ABS (xsize) ); +} + +cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) +{ + int i; + mp_size_t s; mp_size_t xsize; + mp_size_t xsize0; mp_ptr xp; unsigned int c; mp_limb_t x_limb; mp_size_t in_bytesize; int neg_flag; + if (CHAR_BIT != 8) { + fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); + fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); + exit(10); + } + /* Read 4-byte size */ - in_bytesize = 0; - for (i = 4 - 1; i >= 0; i--) - { - c = myfgetc (cb); - in_bytesize = (in_bytesize << BITS_PER_CHAR) | c; - } - /* Size is stored as a 32 bit word; sign extend in_bytesize for non-32 bit - machines. */ - if (sizeof (mp_size_t) > 4) - in_bytesize |= (-(in_bytesize < 0)) << 31; - - neg_flag = in_bytesize < 0; - in_bytesize = ABS (in_bytesize); - xsize = (in_bytesize + BYTES_PER_MP_LIMB - 1) / BYTES_PER_MP_LIMB; - + xsize = (int)getRawInt32(cb); + neg_flag = xsize < 0; + xsize = ABS(xsize); + if (xsize == 0) { x->_mp_size = 0; - return 4; /* we've read 4 bytes */ + return 1; /* we've read 4 bytes */ } - if (x->_mp_alloc < xsize) - _mpz_realloc (x, xsize); + xsize0 = (xsize+1)/2; + if (x->_mp_alloc < xsize0) + _mpz_realloc (x, xsize0); xp = x->_mp_d; - x_limb = 0; - for (i = (in_bytesize - 1) % BYTES_PER_MP_LIMB; i >= 0; i--) - { - c = myfgetc (cb); - x_limb = (x_limb << BITS_PER_CHAR) | c; - } - xp[xsize - 1] = x_limb; + for (i=0; i= 0; s--) - { - x_limb = 0; - for (i = BYTES_PER_MP_LIMB - 1; i >= 0; i--) - { - c = myfgetc (cb); - x_limb = (x_limb << BITS_PER_CHAR) | c; - } - xp[s] = x_limb; - } - - if (c == EOF) - return 0; /* error */ - - MPN_NORMALIZE (xp, xsize); - x->_mp_size = neg_flag ? -xsize : xsize; - return in_bytesize + 4; + MPN_NORMALIZE (xp, xsize0); + x->_mp_size = neg_flag ? -xsize0 : xsize0; + return( xsize0 ); } - -/*****************************************************/ -/***** new version for CMO_ZZ *********************/ -/*****************************************************/ +#elif BYTES_PER_MP_LIMB == 4 size_t cmoOutGMPCoeff_new(mpz_srcptr x) { @@ -262,10 +273,9 @@ cmoOutGMPCoeff_new(mpz_srcptr x) fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); exit(10); } - if (BITS_PER_CHAR != 8) { - fprintf(stderr,"BITS_PER_CHAR = %d\n",BITS_PER_CHAR); + if (CHAR_BIT != 8) { + fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); - fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); exit(10); } @@ -308,10 +318,9 @@ cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); exit(10); } - if (BITS_PER_CHAR != 8) { - fprintf(stderr,"BITS_PER_CHAR = %d\n",BITS_PER_CHAR); + if (CHAR_BIT != 8) { + fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); - fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); exit(10); } @@ -324,7 +333,7 @@ cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) if (xsize == 0) { x->_mp_size = 0; - return 1; /* we've read 4 bytes */ + return 1; /* we've read 4 bytes */ } if (x->_mp_alloc < xsize) @@ -339,3 +348,4 @@ cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) x->_mp_size = neg_flag ? -xsize : xsize; return( xsize ); } +#endif /* BYTES_PER_MP_LIMB */