Annotation of OpenXM_contrib2/asir2018/io/bsave.c, Revision 1.3
1.1 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.3 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2018/io/bsave.c,v 1.2 2019/08/28 23:27:34 noro Exp $
1.1 noro 48: */
49: /* saveXXX must not use GC_malloc(), GC_malloc_atomic(). */
50:
51: #include "ca.h"
52: #include "parse.h"
53: #include "com.h"
54: #include "ox.h"
55:
56: void savenbp(FILE *s,NBP p);
57:
58: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
59: savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat,
1.2 noro 60: savebytearray, 0, 0, 0, 0, 0, 0, 0, 0, savenbp, savedpm };
1.1 noro 61: #if defined(INTERVAL)
62: void saveitv();
63: void saveitvd();
64: void (*nsavef[])() = { saveq, savereal, 0, savebf, saveitv, saveitvd, 0, saveitv, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
65: #else
66: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
67: #endif
68:
69: static short zeroval = 0;
70:
71: void saveobj(FILE *s,Obj p)
72: {
73: if ( !p )
1.3 ! noro 74: write_short(s,(unsigned short *)&zeroval);
1.1 noro 75: else if ( !savef[OID(p)] )
76: error("saveobj : not implemented");
77: else
78: (*savef[OID(p)])(s,p);
79: }
80:
81: void savenum(FILE *s,Num p)
82: {
83: char sgn;
84:
85: if ( !nsavef[NID(p)] )
86: error("savenum : not implemented");
87: else {
1.3 ! noro 88: write_short(s,(unsigned short *)&OID(p)); write_char(s,(unsigned char *)&NID(p));
1.1 noro 89: if ( NID(p) == N_Q ) {
90: sgn = sgnq((Q)p);
1.3 ! noro 91: write_char(s,(unsigned char *)&sgn);
1.1 noro 92: } else
1.3 ! noro 93: write_char(s,(unsigned char *)&(p->pad));
1.1 noro 94: (*nsavef[NID(p)])(s,p);
95: }
96: }
97:
98: void saveq(FILE *s,Q p)
99: {
100: size_t lnm,ldn;
101: int size[2];
102: int len = 2;
103: int *bnm,*bdn;
104: Z nm,dn;
105:
106: if ( INT(p) ) {
107: bnm = (int *)mpz_export(0,&lnm,-1,sizeof(int),0,0,BDY((Z)p));
108: size[0] = lnm;
109: size[1] = 0;
1.3 ! noro 110: write_intarray(s,(unsigned int *)size,len);
! 111: write_intarray(s,(unsigned int *)bnm,size[0]);
1.1 noro 112: } else {
113: nmq(p,&nm);
114: bnm = (int *)mpz_export(0,&lnm,-1,sizeof(int),0,0,BDY(nm));
115: dnq(p,&dn);
116: bdn = (int *)mpz_export(0,&ldn,-1,sizeof(int),0,0,BDY(dn));
117: size[0] = lnm;
118: size[1] = ldn;
1.3 ! noro 119: write_intarray(s,(unsigned int *)size,len);
! 120: write_intarray(s,(unsigned int *)bnm,size[0]);
! 121: write_intarray(s,(unsigned int *)bdn,size[1]);
1.1 noro 122: }
123: }
124:
125: void savereal(FILE *s,Real p)
126: { write_double(s,&BDY(p)); }
127:
128: /*
129: * BDY(p) = | z[0] | z[1] | z[2] | ... | z[lg(z)-1] |
130: * -> | id(2) | nid(1) | sgn(1) | expo>>32 | expo&0xffffffff | len | ... |
131: */
132:
133: void savebf(FILE *s,BF p)
134: {
135: unsigned int zero = 0;
136: unsigned int prec;
137: L exp;
138: int sgn,len,t;
139:
140: prec = MPFR_PREC(p->body);
141: exp = MPFR_EXP(p->body);
142: sgn = MPFR_SIGN(p->body);
143: len = MPFR_LIMB_SIZE(p->body);
144:
1.3 ! noro 145: write_int(s,(unsigned int *)&sgn);
! 146: write_int(s,&prec);
1.1 noro 147: write_int64(s,(UL *)&exp);
148: #if defined(VISUAL)
149: #if !defined(_WIN64)
1.3 ! noro 150: write_int(s,(unsigned int *)&len);
1.1 noro 151: write_intarray(s,p->body->_mpfr_d,len);
152: #else
153: t = (prec+31)/32;
1.3 ! noro 154: write_int(s,(unsigned int *)&t);
1.1 noro 155: write_longarray(s,(long long *)p->body->_mpfr_d,t);
156: #endif
157: #else
158: #if SIZEOF_LONG == 4
1.3 ! noro 159: write_int(s,(unsigned int *)&len);
1.1 noro 160: write_intarray(s,p->body->_mpfr_d,len);
161: #else /* SIZEOF_LONG == 8 */
162: t = (prec+31)/32;
1.3 ! noro 163: write_int(s,(unsigned int *)&t);
1.1 noro 164: write_longarray(s,p->body->_mpfr_d,t);
165: #endif
166: #endif
167: }
168:
169: #if defined(INTERVAL)
170: void saveitv(FILE *s,Itv p)
171: {
172: saveobj(s,(Obj)INF(p));
173: saveobj(s,(Obj)SUP(p));
174: }
175:
176: void saveitvd(FILE *s,IntervalDouble p)
177: {
178: write_double(s,&INF(p));
179: write_double(s,&SUP(p));
180: }
181: #endif
182:
183: void savecplx(FILE *s,C p)
184: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
185:
186: void savemi(FILE *s,MQ p)
1.3 ! noro 187: { write_int(s,(unsigned int *)&CONT(p)); }
1.1 noro 188:
189: void savelm(FILE *s,LM p)
190: {
191: size_t l;
192: int size;
193: int *b;
194:
195: b = (int *)mpz_export(0,&l,-1,sizeof(int),0,0,BDY(p));
196: size = l;
1.3 ! noro 197: write_int(s,(unsigned int *)&size);
! 198: write_intarray(s,(unsigned int *)b,size);
1.1 noro 199: }
200:
201: void savegf2n(FILE *s,GF2N p)
202: {
203: int len;
204:
205: len = p->body->w;
1.3 ! noro 206: write_int(s,(unsigned int *)&len);
1.1 noro 207: write_intarray(s,p->body->b,len);
208: }
209:
210: void savegfpn(FILE *s,GFPN p)
211: {
212: int d,i;
213:
214: d = p->body->d;
1.3 ! noro 215: write_int(s,(unsigned int *)&d);
1.1 noro 216: for ( i = 0; i <= d; i++ )
217: saveobj(s,(Obj)p->body->c[i]);
218: }
219:
220: void savegfs(FILE *s,GFS p)
1.3 ! noro 221: { write_int(s,(unsigned int *)&CONT(p)); }
1.1 noro 222:
223: void savegfsn(FILE *s,GFSN p)
224: {
225: int d;
226:
227: d = DEG(BDY(p));
1.3 ! noro 228: write_int(s,(unsigned int *)&d);
! 229: write_intarray(s,(unsigned int *)COEF(BDY(p)),d+1);
1.1 noro 230: }
231:
232: void savedalg(FILE *s,DAlg p)
233: {
234: saveobj(s,(Obj)p->nm);
235: saveobj(s,(Obj)p->dn);
236: }
237:
238: void savep(FILE *s,P p)
239: {
240: DCP dc;
241: int n;
242: int vindex;
243:
244: if ( NUM(p) )
245: savenum(s,(Num)p);
246: else {
247: vindex = save_convv(VR(p));
248: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
1.3 ! noro 249: write_short(s,(unsigned short *)&OID(p));
! 250: write_int(s,(unsigned int *)&vindex);
1.1 noro 251: if ( vindex < 0 )
252: savepfins(s,VR(p));
1.3 ! noro 253: write_int(s,(unsigned int *)&n);
1.1 noro 254: for ( dc = DC(p); dc; dc = NEXT(dc) ) {
255: saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
256: }
257: }
258: }
259:
260: /* save a pure function (v->attr = V_PF) */
261: /* |name(str)|argc(int)|darray(intarray)|args| */
262:
263: void savepfins(FILE *s,V v)
264: {
265: PFINS ins;
266: PF pf;
267: int argc,i;
268: int *darray;
269:
270: ins = (PFINS)v->priv;
271: pf = ins->pf;
272: savestr(s,NAME(pf));
273: argc = pf->argc;
1.3 ! noro 274: write_int(s,(unsigned int *)&argc);
1.1 noro 275: darray = (int *)ALLOCA(argc*sizeof(int));
276: for ( i = 0; i < argc; i++ )
277: darray[i] = ins->ad[i].d;
1.3 ! noro 278: write_intarray(s,(unsigned int *)darray,argc);
1.1 noro 279: for ( i = 0; i < argc; i++ )
280: saveobj(s,ins->ad[i].arg);
281: }
282:
283: void saver(FILE *s,R p)
284: {
285: if ( !RAT(p) )
286: savep(s,(P)p);
287: else {
1.3 ! noro 288: write_short(s,(unsigned short *)&OID(p)); write_short(s,(unsigned short *)&p->reduced);
1.1 noro 289: savep(s,NM(p)); savep(s,DN(p));
290: }
291: }
292:
293: void savelist(FILE *s,LIST p)
294: {
295: int n;
296: NODE tn;
297:
298: for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
1.3 ! noro 299: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&n);
1.1 noro 300: for ( tn = BDY(p); tn; tn = NEXT(tn) )
301: saveobj(s,(Obj)BDY(tn));
302: }
303:
304: void savevect(FILE *s,VECT p)
305: {
306: int i,len = 2;
307:
1.3 ! noro 308: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&p->len);
1.1 noro 309: for ( i = 0, len = p->len; i < len; i++ )
310: saveobj(s,(Obj)BDY(p)[i]);
311: }
312:
313: void savemat(FILE *s,MAT p)
314: {
315: int i,j,row,col;
316: int len = 3;
317:
1.3 ! noro 318: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&p->row); write_int(s,(unsigned int *)&p->col);
1.1 noro 319: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
320: for ( j = 0; j < col; j++ )
321: saveobj(s,(Obj)BDY(p)[i][j]);
322: }
323:
324: void savestring(FILE *s,STRING p)
325: {
1.3 ! noro 326: write_short(s,(unsigned short *)&OID(p)); savestr(s,BDY(p));
1.1 noro 327: }
328:
329: void savestr(FILE *s,char *p)
330: {
331: int size;
332:
1.3 ! noro 333: size = p ? strlen(p) : 0; write_int(s,(unsigned int *)&size);
1.1 noro 334: if ( size )
1.3 ! noro 335: write_string(s,(unsigned char *)p,size);
1.1 noro 336: }
337:
338: void savedp(FILE *s,DP p)
339: {
340: int nv,n,i,sugar;
341: MP m,t;
342:
343: nv = p->nv; m = p->body; sugar = p->sugar;
344: for ( n = 0, t = m; t; t = NEXT(t), n++ );
1.3 ! noro 345: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&n);
1.1 noro 346: for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
347: saveobj(s,(Obj)t->c);
1.3 ! noro 348: write_int(s,(unsigned int *)&t->dl->td); write_intarray(s,(unsigned int *)&(t->dl->d[0]),nv);
1.1 noro 349: }
350: }
351:
1.2 noro 352: void savedpm(FILE *s,DPM p)
353: {
354: int nv,n,i,sugar;
355: DMM m,t;
356:
357: nv = p->nv; m = p->body; sugar = p->sugar;
358: for ( n = 0, t = m; t; t = NEXT(t), n++ );
1.3 ! noro 359: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&nv); write_int(s,(unsigned int *)&sugar); write_int(s,(unsigned int *)&n);
1.2 noro 360: for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
361: saveobj(s,(Obj)t->c);
1.3 ! noro 362: write_int(s,(unsigned int *)&t->pos);
! 363: write_int(s,(unsigned int *)&t->dl->td); write_intarray(s,(unsigned int *)&(t->dl->d[0]),nv);
1.2 noro 364: }
365: }
366:
1.1 noro 367: void saveui(FILE *s,USINT u)
368: {
1.3 ! noro 369: write_short(s,(unsigned short *)&OID(u)); write_int(s,(unsigned int *)&BDY(u));
1.1 noro 370: }
371:
372: void saveerror(FILE *s,ERR e)
373: {
1.3 ! noro 374: write_short(s,(unsigned short *)&OID(e)); saveobj(s,(Obj)BDY(e));
1.1 noro 375: }
376:
377: void savegfmmat(FILE *s,GFMMAT p)
378: {
379: int i,row,col;
380:
1.3 ! noro 381: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&p->row); write_int(s,(unsigned int *)&p->col);
1.1 noro 382: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
1.3 ! noro 383: write_intarray(s,(unsigned int *)p->body[i],col);
1.1 noro 384: }
385:
386: void savebytearray(FILE *s,BYTEARRAY p)
387: {
1.3 ! noro 388: write_short(s,(unsigned short *)&OID(p)); write_int(s,(unsigned int *)&p->len);
1.1 noro 389: write_string(s,p->body,p->len);
390: }
391:
392: void savenbp(FILE *s,NBP p)
393: {
394: int i,n;
395: NODE t;
396: NBM m;
397:
1.3 ! noro 398: write_short(s,(unsigned short *)&OID(p));
1.1 noro 399: for ( n = 0, t = BDY(p); t; t = NEXT(t), n++ );
1.3 ! noro 400: write_int(s,(unsigned int *)&n);
1.1 noro 401: for ( i = 0, t = BDY(p); i < n; t = NEXT(t), i++ ) {
402: m = (NBM)BDY(t);
403: saveobj(s,(Obj)m->c);
1.3 ! noro 404: write_int(s,(unsigned int *)&m->d);
! 405: write_intarray(s,(unsigned int *)m->b,(m->d+31)/32);
1.1 noro 406: }
407: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>