| version 1.4, 1999/11/27 13:24:41 |
version 1.11, 2020/10/06 11:33:47 |
|
|
| /*$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.10 2015/09/14 07:56:20 takayama 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 <arpa/inet.h> |
| #include "datatype.h" |
#include "datatype.h" |
| #include "stackm.h" |
#include "stackm.h" |
| #include "extern.h" |
#include "extern.h" |
|
|
| #include "kclass.h" |
#include "kclass.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); |
| size_t |
size_t cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb); |
| cmoOutGMPCoeff(mpz_srcptr x) { |
size_t cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb); |
| |
|
| |
size_t cmoOutGMPCoeff(mpz_srcptr x) { |
| if (OxVersion >= 199907170) |
if (OxVersion >= 199907170) |
| return(cmoOutGMPCoeff_new(x)); |
return(cmoOutGMPCoeff_new(x)); |
| else |
else |
| return(cmoOutGMPCoeff_old(x)); |
return(cmoOutGMPCoeff_old(x)); |
| } |
} |
| |
|
| cmoGetGMPCoeff(MP_INT *x, struct cmoBuffer *cb) { |
size_t cmoGetGMPCoeff(MP_INT *x, struct cmoBuffer *cb) { |
| if (OxVersion >= 199907170) |
if (OxVersion >= 199907170) |
| return(cmoGetGMPCoeff_new(x,cb)); |
return(cmoGetGMPCoeff_new(x,cb)); |
| else |
else |
| Line 64 static int myfputc(int i) { |
|
| Line 74 static int myfputc(int i) { |
|
| tmp[0] = i; |
tmp[0] = i; |
| cmoOutputToBuf(CMOPUT,tmp,1); |
cmoOutputToBuf(CMOPUT,tmp,1); |
| } |
} |
| static outRawInt32(int k) |
static void outRawInt32(int k) |
| { |
{ |
| int tmp[1]; |
int tmp[1]; |
| tmp[0] = htonl((int) k); |
tmp[0] = htonl((int) k); |
| cmoOutputToBuf(CMOPUT,tmp,4); |
cmoOutputToBuf(CMOPUT,tmp,4); |
| } |
} |
| |
|
| size_t |
size_t cmoOutGMPCoeff_old(mpz_srcptr x) |
| cmoOutGMPCoeff_old(mpz_srcptr x) |
|
| { |
{ |
| fprintf(stderr,"cmoOutGMPCoeff_old is no longer supported.\n"); |
fprintf(stderr,"cmoOutGMPCoeff_old is no longer supported.\n"); |
| exit(10); |
exit(10); |
| |
return 0; |
| } |
} |
| |
|
| |
|
| Line 117 static int myfgetc(struct cmoBuffer *cb) |
|
| Line 127 static int myfgetc(struct cmoBuffer *cb) |
|
| } |
} |
| 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]; |
| Line 127 static int getRawInt32(struct cmoBuffer *cb) |
|
| Line 149 static int getRawInt32(struct cmoBuffer *cb) |
|
| } |
} |
| return( ntohl(* ( (int *)d))); |
return( ntohl(* ( (int *)d))); |
| } |
} |
| |
#endif |
| |
|
| cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) |
size_t cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb) |
| { |
{ |
| fprintf(stderr,"cmoGetGMPCoeff_old is no longer supported.\n"); |
fprintf(stderr,"cmoGetGMPCoeff_old is no longer supported.\n"); |
| exit(10); |
exit(10); |
| |
return 0; |
| } |
} |
| |
|
| /*****************************************************/ |
/*****************************************************/ |
| /***** new version for CMO_ZZ *********************/ |
/***** new version for CMO_ZZ *********************/ |
| /*****************************************************/ |
/*****************************************************/ |
| size_t |
#if BYTES_PER_MP_LIMB == 8 |
| cmoOutGMPCoeff_new(mpz_srcptr x) |
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 xsize = ABS (x->_mp_size); |
| |
mp_size_t xsize0; |
| mp_srcptr xp = x->_mp_d; |
mp_srcptr xp = x->_mp_d; |
| mp_size_t out_bytesize; |
mp_size_t out_bytesize; |
| mp_limb_t hi_limb; |
mp_limb_t hi_limb; |
| Line 151 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
| Line 176 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
| tmp[0] = htonl(CMO_ZZ); |
tmp[0] = htonl(CMO_ZZ); |
| cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint)); |
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) ); |
| |
} |
| |
|
| |
size_t 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<xsize0-1; i++) { |
| |
xp[i] = ((unsigned long)getRawInt32(cb)) |
| |
|(((unsigned long)getRawInt32(cb))<<32); |
| |
} |
| |
if ( !(xsize&1) ) |
| |
xp[i] = ((unsigned long)getRawInt32(cb)) |
| |
|(((unsigned long)getRawInt32(cb))<<32); |
| |
else |
| |
xp[i] = (unsigned long)getRawInt32(cb); |
| |
|
| |
MPN_NORMALIZE (xp, xsize0); |
| |
x->_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) { |
if (BYTES_PER_MP_LIMB != 4) { |
| fprintf(stderr,"BYTES_PER_MP_LIMB = %d\n",BYTES_PER_MP_LIMB); |
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,"cmo-gmp.c does not work on this CPU.\n"); |
| 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 186 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
| Line 309 cmoOutGMPCoeff_new(mpz_srcptr x) |
|
| return ( ABS (xsize) ); |
return ( ABS (xsize) ); |
| } |
} |
| |
|
| cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
size_t cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
| { |
{ |
| int i; |
int i; |
| mp_size_t s; |
mp_size_t s; |
| Line 203 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
| Line 326 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 219 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
| Line 341 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 234 cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb) |
|
| Line 356 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 */ |