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