=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/cmo-gmp.c,v retrieving revision 1.4 retrieving revision 1.7 diff -u -p -r1.4 -r1.7 --- OpenXM/src/kan96xx/plugin/cmo-gmp.c 1999/11/27 13:24:41 1.4 +++ OpenXM/src/kan96xx/plugin/cmo-gmp.c 2003/08/26 05:04:41 1.7 @@ -1,7 +1,8 @@ -/*$OpenXM: OpenXM/src/kan96xx/plugin/cmo-gmp.c,v 1.3 1999/11/18 00:54:17 takayama Exp $ */ +/*$OpenXM: OpenXM/src/kan96xx/plugin/cmo-gmp.c,v 1.6 2001/05/04 01:06:29 takayama Exp $ */ #include #include /* #include */ +#include #include "datatype.h" #include "stackm.h" #include "extern.h" @@ -117,6 +118,18 @@ static int myfgetc(struct cmoBuffer *cb) } 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]; @@ -127,6 +140,7 @@ static int getRawInt32(struct cmoBuffer *cb) } return( ntohl(* ( (int *)d))); } +#endif cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) { @@ -137,12 +151,14 @@ cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) /*****************************************************/ /***** 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; @@ -151,16 +167,115 @@ cmoOutGMPCoeff_new(mpz_srcptr x) 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 */ + + xsize = (int)getRawInt32(cb); + neg_flag = xsize < 0; + xsize = ABS(xsize); + + if (xsize == 0) + { + x->_mp_size = 0; + return 1; /* we've read 4 bytes */ + } + + xsize0 = (xsize+1)/2; + if (x->_mp_alloc < xsize0) + _mpz_realloc (x, xsize0); + xp = x->_mp_d; + + for (i=0; i_mp_size = neg_flag ? -xsize0 : xsize0; + return( xsize0 ); +} +#elif BYTES_PER_MP_LIMB == 4 +size_t +cmoOutGMPCoeff_new(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); + cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint)); + if (BYTES_PER_MP_LIMB != 4) { fprintf(stderr,"BYTES_PER_MP_LIMB = %d\n",BYTES_PER_MP_LIMB); 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); } - 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); } @@ -203,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); } @@ -219,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) @@ -234,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 */