[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.3

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

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