version 1.2, 1999/10/25 01:39:03 |
version 1.10, 2015/09/14 07:56:20 |
|
|
|
/*$OpenXM: OpenXM/src/kan96xx/plugin/cmo-gmp.c,v 1.9 2005/07/03 11:08:54 ohara Exp $ */ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
/* #include <netinet/in.h> */ |
/* #include <netinet/in.h> */ |
|
#include <limits.h> |
#include "datatype.h" |
#include "datatype.h" |
#include "stackm.h" |
#include "stackm.h" |
#include "extern.h" |
#include "extern.h" |
|
|
#include "gmp.h" |
#include "gmp.h" |
#include "gmp-impl.h" |
#include "gmp-impl.h" |
|
|
|
|
#include "file2.h" |
#include "file2.h" |
#include "cmo.h" |
#include "cmo.h" |
|
|
|
/* for gmp6.0.0 */ |
|
#ifndef BYTES_PER_MP_LIMB |
|
#define BYTES_PER_MP_LIMB SIZEOF_MP_LIMB_T |
|
#endif |
|
|
extern int OxVersion; |
extern int OxVersion; |
size_t cmoOutGMPCoeff_old(mpz_srcptr x); |
size_t cmoOutGMPCoeff_old(mpz_srcptr x); |
size_t cmoOutGMPCoeff_new(mpz_srcptr x); |
size_t cmoOutGMPCoeff_new(mpz_srcptr x); |
Line 72 static outRawInt32(int k) |
|
Line 81 static outRawInt32(int k) |
|
size_t |
size_t |
cmoOutGMPCoeff_old(mpz_srcptr x) |
cmoOutGMPCoeff_old(mpz_srcptr x) |
{ |
{ |
int i; |
fprintf(stderr,"cmoOutGMPCoeff_old is no longer supported.\n"); |
mp_size_t s; |
exit(10); |
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); |
|
} |
} |
|
|
|
|
Line 161 static int myfgetc(struct cmoBuffer *cb) |
|
Line 121 static int myfgetc(struct cmoBuffer *cb) |
|
cb->rpos--; |
cb->rpos--; |
errorCmo(" cmo-gmp.c : myfgetc(): no data in the buffer."); |
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) |
static int getRawInt32(struct cmoBuffer *cb) |
{ |
{ |
char d[4]; |
char d[4]; |
int i; |
int i; |
|
int r; |
for (i=0; i<4; i++) { |
for (i=0; i<4; i++) { |
d[i] = myfgetc(cb); |
d[i] = myfgetc(cb); |
} |
} |
return(ntohl(* ( (int *)d))); |
return( ntohl(* ( (int *)d))); |
} |
} |
|
#endif |
|
|
cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) |
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; |
int i; |
mp_size_t s; |
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 xsize; |
|
mp_size_t xsize0; |
mp_ptr xp; |
mp_ptr xp; |
unsigned int c; |
unsigned int c; |
mp_limb_t x_limb; |
mp_limb_t x_limb; |
mp_size_t in_bytesize; |
mp_size_t in_bytesize; |
int neg_flag; |
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 */ |
/* 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 |
xsize = (int)getRawInt32(cb); |
machines. */ |
neg_flag = xsize < 0; |
if (sizeof (mp_size_t) > 4) |
xsize = ABS(xsize); |
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; |
|
|
|
if (xsize == 0) |
if (xsize == 0) |
{ |
{ |
x->_mp_size = 0; |
x->_mp_size = 0; |
return 4; /* we've read 4 bytes */ |
return 1; /* we've read 4 bytes */ |
} |
} |
|
|
if (x->_mp_alloc < xsize) |
xsize0 = (xsize+1)/2; |
_mpz_realloc (x, xsize); |
if (x->_mp_alloc < xsize0) |
|
_mpz_realloc (x, xsize0); |
xp = x->_mp_d; |
xp = x->_mp_d; |
|
|
x_limb = 0; |
for (i=0; i<xsize0-1; i++) { |
for (i = (in_bytesize - 1) % BYTES_PER_MP_LIMB; i >= 0; i--) |
xp[i] = ((unsigned long)getRawInt32(cb)) |
{ |
|(((unsigned long)getRawInt32(cb))<<32); |
c = myfgetc (cb); |
} |
x_limb = (x_limb << BITS_PER_CHAR) | c; |
if ( !(xsize&1) ) |
} |
xp[i] = ((unsigned long)getRawInt32(cb)) |
xp[xsize - 1] = x_limb; |
|(((unsigned long)getRawInt32(cb))<<32); |
|
else |
|
xp[i] = (unsigned long)getRawInt32(cb); |
|
|
for (s = xsize - 2; s >= 0; s--) |
MPN_NORMALIZE (xp, xsize0); |
{ |
x->_mp_size = neg_flag ? -xsize0 : xsize0; |
x_limb = 0; |
return( xsize0 ); |
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; |
|
} |
} |
|
#elif BYTES_PER_MP_LIMB == 4 |
/*****************************************************/ |
|
/***** new version for CMO_ZZ *********************/ |
|
/*****************************************************/ |
|
size_t |
size_t |
cmoOutGMPCoeff_new(mpz_srcptr x) |
cmoOutGMPCoeff_new(mpz_srcptr x) |
{ |
{ |
Line 262 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
Line 279 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); |
fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); |
exit(10); |
exit(10); |
} |
} |
if (BITS_PER_CHAR != 8) { |
if (CHAR_BIT != 8) { |
fprintf(stderr,"BITS_PER_CHAR = %d\n",BITS_PER_CHAR); |
fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); |
fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); |
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); |
exit(10); |
} |
} |
|
|
Line 308 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
Line 324 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); |
fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n"); |
exit(10); |
exit(10); |
} |
} |
if (BITS_PER_CHAR != 8) { |
if (CHAR_BIT != 8) { |
fprintf(stderr,"BITS_PER_CHAR = %d\n",BITS_PER_CHAR); |
fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT); |
fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n"); |
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); |
exit(10); |
} |
} |
|
|
Line 324 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
Line 339 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
if (xsize == 0) |
if (xsize == 0) |
{ |
{ |
x->_mp_size = 0; |
x->_mp_size = 0; |
return 1; /* we've read 4 bytes */ |
return 1; /* we've read 4 bytes */ |
} |
} |
|
|
if (x->_mp_alloc < xsize) |
if (x->_mp_alloc < xsize) |
Line 339 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
Line 354 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
x->_mp_size = neg_flag ? -xsize : xsize; |
x->_mp_size = neg_flag ? -xsize : xsize; |
return( xsize ); |
return( xsize ); |
} |
} |
|
#endif /* BYTES_PER_MP_LIMB */ |