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

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

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