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