[BACK]Return to cmo-gmp.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/cmo-gmp.c, Revision 1.10

1.10    ! takayama    1: /*$OpenXM: OpenXM/src/kan96xx/plugin/cmo-gmp.c,v 1.9 2005/07/03 11:08:54 ohara Exp $ */
1.1       maekawa     2: #include <stdio.h>
1.9       ohara       3: #include <stdlib.h>
1.1       maekawa     4: #include <string.h>
1.2       takayama    5: /* #include <netinet/in.h> */
1.7       takayama    6: #include <limits.h>
1.1       maekawa     7: #include "datatype.h"
                      8: #include "stackm.h"
                      9: #include "extern.h"
                     10: #include "extern2.h"
                     11: #include "mathcap.h"
                     12: #include "kclass.h"
                     13:
                     14: #include "gmp.h"
1.8       takayama   15: #include "gmp-impl.h"
1.4       takayama   16:
1.1       maekawa    17:
                     18: #include "file2.h"
                     19: #include "cmo.h"
                     20:
1.10    ! takayama   21: /* for gmp6.0.0 */
        !            22: #ifndef BYTES_PER_MP_LIMB
        !            23: #define BYTES_PER_MP_LIMB SIZEOF_MP_LIMB_T
        !            24: #endif
        !            25:
1.1       maekawa    26: extern int OxVersion;
                     27: size_t cmoOutGMPCoeff_old(mpz_srcptr x);
                     28: size_t cmoOutGMPCoeff_new(mpz_srcptr x);
                     29: size_t
                     30: cmoOutGMPCoeff(mpz_srcptr x) {
                     31:   if (OxVersion >= 199907170)
                     32:     return(cmoOutGMPCoeff_new(x));
                     33:   else
                     34:     return(cmoOutGMPCoeff_old(x));
                     35: }
                     36:
                     37: cmoGetGMPCoeff(MP_INT *x, struct cmoBuffer *cb) {
                     38:   if (OxVersion >= 199907170)
                     39:     return(cmoGetGMPCoeff_new(x,cb));
                     40:   else
                     41:     return(cmoGetGMPCoeff_old(x,cb));
                     42: }
                     43:
                     44:
                     45: /* From gmp/mpz/out_raw.c and inp_raw.c */
                     46:
                     47: /* mpz_out_raw -- Output a mpz_t in binary.  Use an endianess and word size
                     48:    independent format.
                     49:
                     50: Copyright (C) 1995 Free Software Foundation, Inc.
                     51:
                     52: This file is part of the GNU MP Library.
                     53:
                     54: The GNU MP Library is free software; you can redistribute it and/or modify
                     55: it under the terms of the GNU Library General Public License as published by
                     56: the Free Software Foundation; either version 2 of the License, or (at your
                     57: option) any later version.
                     58:
                     59: The GNU MP Library is distributed in the hope that it will be useful, but
                     60: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     61: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                     62: License for more details.
                     63:
                     64: You should have received a copy of the GNU Library General Public License
                     65: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     66: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     67: MA 02111-1307, USA. */
                     68:
                     69: static int myfputc(int i) {
                     70:   char tmp[1];
                     71:   tmp[0] = i;
                     72:   cmoOutputToBuf(CMOPUT,tmp,1);
                     73: }
                     74: static outRawInt32(int k)
                     75: {
                     76:   int tmp[1];
                     77:   tmp[0] = htonl((int) k);
                     78:   cmoOutputToBuf(CMOPUT,tmp,4);
                     79: }
                     80:
                     81: size_t
                     82: cmoOutGMPCoeff_old(mpz_srcptr x)
                     83: {
1.4       takayama   84:   fprintf(stderr,"cmoOutGMPCoeff_old is no longer supported.\n");
                     85:   exit(10);
1.1       maekawa    86: }
                     87:
                     88:
                     89: /* mpz_inp_raw -- Input a mpz_t in raw, but endianess, and wordsize
                     90:    independent format (as output by mpz_out_raw).
                     91:
                     92: Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc.
                     93:
                     94: This file is part of the GNU MP Library.
                     95:
                     96: The GNU MP Library is free software; you can redistribute it and/or modify
                     97: it under the terms of the GNU Library General Public License as published by
                     98: the Free Software Foundation; either version 2 of the License, or (at your
                     99: option) any later version.
                    100:
                    101: The GNU MP Library is distributed in the hope that it will be useful, but
                    102: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                    103: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                    104: License for more details.
                    105:
                    106: You should have received a copy of the GNU Library General Public License
                    107: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                    108: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                    109: MA 02111-1307, USA. */
                    110:
                    111:
                    112: static int myfgetc(struct cmoBuffer *cb)
                    113: {
                    114:   int k;
                    115:   if (cb->isStream) {
                    116:     k = fp2fgetc(cb->fp);
                    117:   }else{
                    118:     k = ((unsigned char *)(cb->buf))[cb->rpos];
                    119:     cb->rpos++;
                    120:     if (cb->rpos > cb->pos) {
                    121:       cb->rpos--;
                    122:       errorCmo(" cmo-gmp.c : myfgetc(): no data in the buffer.");
                    123:     }
                    124:   }
1.3       takayama  125:   return(k);
1.1       maekawa   126: }
1.5       noro      127: #if BYTES_PER_MP_LIMB == 8
                    128: static unsigned int getRawInt32(struct cmoBuffer *cb)
                    129: {
                    130:   char d[4];
                    131:   int i;
                    132:   int r;
                    133:   for (i=0; i<4; i++) {
                    134:     d[i] = myfgetc(cb);
                    135:   }
                    136:   return( ntohl(* ( (unsigned int *)d)));
                    137: }
                    138: #elif BYTES_PER_MP_LIMB == 4
1.1       maekawa   139: static int getRawInt32(struct cmoBuffer *cb)
                    140: {
                    141:   char d[4];
                    142:   int i;
1.3       takayama  143:   int r;
1.1       maekawa   144:   for (i=0; i<4; i++) {
                    145:     d[i] = myfgetc(cb);
                    146:   }
1.3       takayama  147:   return( ntohl(* ( (int *)d)));
1.1       maekawa   148: }
1.5       noro      149: #endif
1.1       maekawa   150:
                    151: cmoGetGMPCoeff_old(MP_INT *x, struct cmoBuffer *cb)
                    152: {
1.4       takayama  153:   fprintf(stderr,"cmoGetGMPCoeff_old is no longer supported.\n");
                    154:   exit(10);
1.1       maekawa   155: }
                    156:
                    157: /*****************************************************/
                    158: /*****   new version for CMO_ZZ  *********************/
                    159: /*****************************************************/
1.5       noro      160: #if BYTES_PER_MP_LIMB == 8
                    161: size_t
                    162: cmoOutGMPCoeff_new(mpz_srcptr x)
                    163: {
                    164:   int i;
                    165:   mp_size_t s;
                    166:   mp_size_t xsize = ABS (x->_mp_size);
                    167:   mp_size_t xsize0;
                    168:   mp_srcptr xp = x->_mp_d;
                    169:   mp_size_t out_bytesize;
                    170:   mp_limb_t hi_limb;
                    171:   int n_bytes_in_hi_limb;
                    172:   cmoint tmp[1];
                    173:   tmp[0] = htonl(CMO_ZZ);
                    174:   cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint));
                    175:
1.7       takayama  176:   if (CHAR_BIT != 8) {
                    177:     fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT);
1.5       noro      178:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    179:     exit(10);
                    180:   }
                    181:
                    182:   if (xsize == 0)
                    183:     {
                    184:       outRawInt32(0);
                    185:       return  1;
                    186:     }
                    187:
                    188:   xsize0 = xsize;
                    189:   if ( (unsigned long)xp[xsize-1] < (unsigned long)0x100000000 )
                    190:     xsize = xsize0*2-1;
                    191:   else
                    192:     xsize = xsize0*2;
                    193:
                    194:   if (x->_mp_size >= 0)
                    195:     outRawInt32(xsize);
                    196:   else
                    197:     outRawInt32(-xsize);
                    198:
                    199:   for (s = 0; s < xsize0-1; s++)
                    200:     {
                    201:       outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff);
                    202:       outRawInt32(((unsigned long)xp[s])>>32);
                    203:     }
1.6       takayama  204:   if ( !(xsize&1) ) {
                    205:     outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff);
                    206:     outRawInt32(((unsigned long)xp[s])>>32);
                    207:   } else
                    208:     outRawInt32(((unsigned long)xp[s])&(unsigned long)0xffffffff);
1.5       noro      209:   return ( ABS (xsize) );
                    210: }
                    211:
                    212: cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb)
                    213: {
                    214:   int i;
                    215:   mp_size_t s;
                    216:   mp_size_t xsize;
                    217:   mp_size_t xsize0;
                    218:   mp_ptr xp;
                    219:   unsigned int c;
                    220:   mp_limb_t x_limb;
                    221:   mp_size_t in_bytesize;
                    222:   int neg_flag;
                    223:
1.7       takayama  224:   if (CHAR_BIT != 8) {
                    225:     fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT);
1.5       noro      226:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    227:     exit(10);
                    228:   }
                    229:
                    230:   /* Read 4-byte size */
                    231:
                    232:   xsize = (int)getRawInt32(cb);
                    233:   neg_flag = xsize < 0;
                    234:   xsize = ABS(xsize);
                    235:
                    236:   if (xsize == 0)
                    237:     {
                    238:       x->_mp_size = 0;
1.6       takayama  239:       return 1;         /* we've read 4 bytes */
1.5       noro      240:     }
                    241:
                    242:   xsize0 = (xsize+1)/2;
                    243:   if (x->_mp_alloc < xsize0)
                    244:     _mpz_realloc (x, xsize0);
                    245:   xp = x->_mp_d;
                    246:
                    247:   for (i=0; i<xsize0-1; i++) {
                    248:     xp[i] = ((unsigned long)getRawInt32(cb))
1.6       takayama  249:       |(((unsigned long)getRawInt32(cb))<<32);
1.5       noro      250:   }
                    251:   if ( !(xsize&1) )
                    252:     xp[i] = ((unsigned long)getRawInt32(cb))
1.6       takayama  253:       |(((unsigned long)getRawInt32(cb))<<32);
1.5       noro      254:   else
                    255:     xp[i] = (unsigned long)getRawInt32(cb);
                    256:
                    257:   MPN_NORMALIZE (xp, xsize0);
                    258:   x->_mp_size = neg_flag ? -xsize0 : xsize0;
                    259:   return( xsize0 );
                    260: }
                    261: #elif BYTES_PER_MP_LIMB == 4
1.1       maekawa   262: size_t
                    263: cmoOutGMPCoeff_new(mpz_srcptr x)
                    264: {
                    265:   int i;
                    266:   mp_size_t s;
                    267:   mp_size_t xsize = ABS (x->_mp_size);
                    268:   mp_srcptr xp = x->_mp_d;
                    269:   mp_size_t out_bytesize;
                    270:   mp_limb_t hi_limb;
                    271:   int n_bytes_in_hi_limb;
                    272:   cmoint tmp[1];
                    273:   tmp[0] = htonl(CMO_ZZ);
                    274:   cmoOutputToBuf(CMOPUT,tmp,sizeof(cmoint));
                    275:
                    276:   if (BYTES_PER_MP_LIMB != 4) {
                    277:     fprintf(stderr,"BYTES_PER_MP_LIMB = %d\n",BYTES_PER_MP_LIMB);
                    278:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    279:     fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n");
                    280:     exit(10);
                    281:   }
1.7       takayama  282:   if (CHAR_BIT != 8) {
                    283:     fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT);
1.1       maekawa   284:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    285:     exit(10);
                    286:   }
                    287:
                    288:   if (xsize == 0)
                    289:     {
                    290:       outRawInt32(0);
                    291:       return  1;
                    292:     }
                    293:
                    294:   if (x->_mp_size >= 0)
                    295:     outRawInt32(xsize);
                    296:   else
                    297:     outRawInt32(-xsize);
                    298:
                    299:
                    300:   /* Output from the least significant limb to the most significant limb */
                    301:   /* We assume that the size of limb is 4. */
                    302:
                    303:   for (s = 0; s < xsize; s++)
                    304:     {
                    305:       outRawInt32((int) xp[s]);
                    306:     }
                    307:   return ( ABS (xsize) );
                    308: }
                    309:
                    310: cmoGetGMPCoeff_new(MP_INT *x, struct cmoBuffer *cb)
                    311: {
                    312:   int i;
                    313:   mp_size_t s;
                    314:   mp_size_t xsize;
                    315:   mp_ptr xp;
                    316:   unsigned int c;
                    317:   mp_limb_t x_limb;
                    318:   mp_size_t in_bytesize;
                    319:   int neg_flag;
                    320:
                    321:   if (BYTES_PER_MP_LIMB != 4) {
                    322:     fprintf(stderr,"BYTES_PER_MP_LIMB = %d\n",BYTES_PER_MP_LIMB);
                    323:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    324:     fprintf(stderr,"Read the GMP source code and rewrite cmo-gmp.c.\n");
                    325:     exit(10);
                    326:   }
1.7       takayama  327:   if (CHAR_BIT != 8) {
                    328:     fprintf(stderr,"CHAR_BIT = %d\n",CHAR_BIT);
1.1       maekawa   329:     fprintf(stderr,"cmo-gmp.c does not work on this CPU.\n");
                    330:     exit(10);
                    331:   }
                    332:
                    333:   /* Read 4-byte size */
                    334:
                    335:   xsize = getRawInt32(cb);
                    336:   neg_flag = xsize < 0;
                    337:   xsize = ABS(xsize);
                    338:
                    339:   if (xsize == 0)
                    340:     {
                    341:       x->_mp_size = 0;
1.6       takayama  342:       return 1;         /* we've read 4 bytes */
1.1       maekawa   343:     }
                    344:
                    345:   if (x->_mp_alloc < xsize)
                    346:     _mpz_realloc (x, xsize);
                    347:   xp = x->_mp_d;
                    348:
                    349:   for (i=0; i<xsize; i++) {
                    350:     xp[i] = getRawInt32(cb);
                    351:   }
                    352:
                    353:   MPN_NORMALIZE (xp, xsize);
                    354:   x->_mp_size = neg_flag ? -xsize : xsize;
                    355:   return( xsize );
                    356: }
1.5       noro      357: #endif /* BYTES_PER_MP_LIMB */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>