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