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