Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.3
1.3 ! 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@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.
! 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cio.c,v 1.2 2000/03/28 06:32:22 noro Exp $
! 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
51: #include "ox.h"
52:
53: #define ISIZ sizeof(int)
54:
55: int write_cmo_zz(FILE *,int,N);
56: int read_cmo_zz(FILE *,int *,N *);
57:
58: int valid_as_cmo(obj)
59: Obj obj;
60: {
61: NODE m;
62:
63: if ( !obj )
64: return 1;
65: switch ( OID(obj) ) {
66: case O_MATHCAP: case O_P: case O_R: case O_DP: case O_STR:
67: case O_ERR: case O_USINT: case O_VOID:
68: return 1;
69: case O_N:
1.2 noro 70: if ( NID((Num)obj) == N_Q || NID((Num)obj) == N_R )
1.1 noro 71: return 1;
72: else
73: return 0;
74: case O_LIST:
75: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
76: if ( !valid_as_cmo(BDY(m)) )
77: return 0;
78: return 1;
79: default:
80: return 0;
81: }
82: }
83:
84: write_cmo(s,obj)
85: FILE *s;
86: Obj obj;
87: {
88: int r;
89: char errmsg[BUFSIZ];
90:
91: if ( !obj ) {
92: r = CMO_NULL; write_int(s,&r);
93: return;
94: }
95: switch ( OID(obj) ) {
96: case O_N:
1.2 noro 97: switch ( NID((Num)obj) ) {
98: case N_Q:
99: write_cmo_q(s,(Q)obj);
100: break;
101: case N_R:
102: write_cmo_real(s,(Real)obj);
103: break;
104: default:
105: sprintf(errmsg, "write_cmo : number id=%d not implemented.",
106: NID((Num)obj));
107: error(errmsg);
108: break;
109: }
1.1 noro 110: break;
111: case O_P:
112: write_cmo_p(s,obj);
113: break;
114: case O_R:
115: write_cmo_r(s,obj);
116: break;
117: case O_DP:
118: write_cmo_dp(s,obj);
119: break;
120: case O_LIST:
121: write_cmo_list(s,obj);
122: break;
123: case O_STR:
124: write_cmo_string(s,obj);
125: break;
126: case O_USINT:
127: write_cmo_uint(s,obj);
128: break;
129: case O_MATHCAP:
130: write_cmo_mathcap(s,obj);
131: break;
132: case O_ERR:
133: write_cmo_error(s,obj);
134: break;
135: case O_VOID:
136: r = ((USINT)obj)->body; write_int(s,&r);
137: break;
138: default:
139: sprintf(errmsg, "write_cmo : id=%d not implemented.",OID(obj));
140: error(errmsg);
141: break;
142: }
143: }
144:
145: write_cmo_mathcap(s,mc)
146: FILE *s;
147: MATHCAP mc;
148: {
149: unsigned int r;
150:
151: r = CMO_MATHCAP; write_int(s,&r);
152: write_cmo(s,BDY(mc));
153: }
154:
155: write_cmo_uint(s,ui)
156: FILE *s;
157: USINT ui;
158: {
159: unsigned int r;
160:
161: r = CMO_INT32; write_int(s,&r);
162: r = ui->body; write_int(s,&r);
163: }
164:
165: write_cmo_q(s,q)
166: FILE *s;
167: Q q;
168: {
169: int r;
170:
171: if ( q && DN(q) ) {
172: r = CMO_QQ; write_int(s,&r);
173: write_cmo_zz(s,SGN(q),NM(q));
174: write_cmo_zz(s,1,DN(q));
175: } else {
176: r = CMO_ZZ; write_int(s,&r);
177: write_cmo_zz(s,SGN(q),NM(q));
178: }
179: }
180:
1.2 noro 181: write_cmo_real(s,real)
182: FILE *s;
183: Real real;
184: {
185: unsigned int r;
186: double dbl;
187:
188: r = CMO_IEEE_DOUBLE_FLOAT; write_int(s,&r);
189: dbl = real->body; write_double(s,&dbl);
190: }
191:
1.1 noro 192: write_cmo_zz(s,sgn,n)
193: FILE *s;
194: int sgn;
195: N n;
196: {
197: int i,l,bytes;
198: unsigned int t;
199: unsigned int *b;
200: unsigned char c;
201:
202: #if 1
203: l = PL(n);
204: bytes = sgn*l;
205: write_int(s,&bytes);
206: write_intarray(s,BD(n),l);
207: #else
208: l = PL(n); b = (unsigned int *)BD(n);
209: bytes = sgn*4*l;
210: write_int(s,&bytes);
211: for ( i = l-1; i >= 0; i-- ) {
212: t = b[i];
213: c = t>>24; write_char(s,&c);
214: c = (t>>16)&0xff; write_char(s,&c);
215: c = (t>>8)&0xff; write_char(s,&c);
216: c = t&0xff; write_char(s,&c);
217: }
218: #endif
219: }
220:
221: write_cmo_p(s,p)
222: FILE *s;
223: P p;
224: {
225: int r,i;
226: VL t,vl;
227: char *namestr;
228: STRING name;
229: NODE n0,n;
230:
231: r = CMO_RECURSIVE_POLYNOMIAL; write_int(s,&r);
232: get_vars((Obj)p,&vl);
233:
234: /* indeterminate list */
235: r = CMO_LIST; write_int(s,&r);
236: for ( t = vl, i = 0; t; t = NEXT(t), i++ );
237: write_int(s,&i);
238: r = CMO_INDETERMINATE;
239: for ( t = vl; t; t = NEXT(t) ) {
240: write_int(s,&r);
241: /* localname_to_cmoname(NAME(t->v),&namestr); */
242: namestr = NAME(t->v);
243: MKSTR(name,namestr);
244: write_cmo(s,name);
245: }
246:
247: /* body */
248: write_cmo_upoly(s,vl,p);
249: }
250:
251: write_cmo_upoly(s,vl,p)
252: FILE *s;
253: VL vl;
254: P p;
255: {
256: int r,i;
257: V v;
258: DCP dc,dct;
259: VL vlt;
260:
261: if ( NUM(p) )
262: write_cmo(s,p);
263: else {
264: r = CMO_UNIVARIATE_POLYNOMIAL; write_int(s,&r);
265: v = VR(p);
266: dc = DC(p);
267: for ( i = 0, dct = dc; dct; dct = NEXT(dct), i++ );
268: write_int(s,&i);
269: for ( i = 0, vlt = vl; vlt->v != v; vlt = NEXT(vlt), i++ );
270: write_int(s,&i);
271: for ( dct = dc; dct; dct = NEXT(dct) ) {
272: i = QTOS(DEG(dct)); write_int(s,&i);
273: write_cmo_upoly(s,vl,COEF(dct));
274: }
275: }
276: }
277:
278: write_cmo_r(s,f)
279: FILE *s;
280: R f;
281: {
282: int r;
283:
284: r = CMO_RATIONAL; write_int(s,&r);
285: write_cmo(s,NM(f));
286: write_cmo(s,DN(f));
287: }
288:
289: write_cmo_dp(s,dp)
290: FILE *s;
291: DP dp;
292: {
293: int i,n,nv,r;
294: MP m;
295:
296: for ( n = 0, m = BDY(dp); m; m = NEXT(m), n++ );
297: r = CMO_DISTRIBUTED_POLYNOMIAL; write_int(s,&r);
298: r = n; write_int(s,&r);
299: r = CMO_DMS_GENERIC; write_int(s,&r);
300: nv = dp->nv;
301: for ( i = 0, m = BDY(dp); i < n; i++, m = NEXT(m) )
302: write_cmo_monomial(s,m,nv);
303: }
304:
305: write_cmo_monomial(s,m,n)
306: FILE *s;
307: MP m;
308: int n;
309: {
310: int i,r;
311: int *p;
312:
313: r = CMO_MONOMIAL32; write_int(s,&r);
314: write_int(s,&n);
315: for ( i = 0, p = m->dl->d; i < n; i++ ) {
316: write_int(s,p++);
317: }
318: write_cmo_q(s,m->c);
319: }
320:
321: write_cmo_list(s,list)
322: FILE *s;
323: LIST list;
324: {
325: NODE m;
326: int i,n,r;
327:
328: for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
329: r = CMO_LIST; write_int(s,&r);
330: write_int(s,&n);
331: for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
332: write_cmo(s,BDY(m));
333: }
334:
335: write_cmo_string(s,str)
336: FILE *s;
337: STRING str;
338: {
339: int r;
340:
341: r = CMO_STRING; write_int(s,&r);
342: savestr(s,BDY(str));
343: }
344:
345: write_cmo_error(s,e)
346: FILE *s;
347: ERR e;
348: {
349: int r;
350:
351: r = CMO_ERROR2; write_int(s,&r);
352: write_cmo(s,BDY(e));
353: }
354:
355: read_cmo(s,rp)
356: FILE *s;
357: Obj *rp;
358: {
359: int id;
360: int n,sgn,dummy;
361: Q q,qnm,qdn;
362: N nm,dn;
363: P p,pnm,pdn;
364: R r;
1.2 noro 365: Real real;
366: double dbl;
1.1 noro 367: STRING str;
368: USINT t;
369: DP dp;
370: char *b;
371: Obj obj;
372: ERR e;
373: MATHCAP mc;
374:
375: read_int(s,&id);
376: switch ( id ) {
377: /* level 0 objects */
378: case CMO_NULL:
379: *rp = 0;
380: break;
381: case CMO_INT32:
382: read_cmo_uint(s,rp);
383: break;
384: case CMO_DATUM:
385: case CMO_STRING:
386: loadstring(s,&str); *rp = (Obj)str;
387: break;
388: case CMO_MATHCAP:
389: read_cmo(s,&obj); MKMATHCAP(mc,(LIST)obj);
390: *rp = (Obj)mc;
391: break;
392: case CMO_ERROR:
393: MKERR(e,0); *rp = (Obj)e;
394: break;
395: case CMO_ERROR2:
396: read_cmo(s,&obj); MKERR(e,obj); *rp = (Obj)e;
397: break;
398: /* level 1 objects */
399: case CMO_LIST:
400: read_cmo_list(s,rp);
401: break;
402: case CMO_MONOMIAL32:
403: read_cmo_monomial(s,rp);
404: break;
405: case CMO_ZZ:
406: read_cmo_zz(s,&sgn,&nm);
407: NTOQ(nm,sgn,q); *rp = (Obj)q;
408: break;
409: case CMO_QQ:
410: read_cmo_zz(s,&sgn,&nm);
411: read_cmo_zz(s,&dummy,&dn);
412: NDTOQ(nm,dn,sgn,q); *rp = (Obj)q;
1.2 noro 413: break;
414: case CMO_IEEE_DOUBLE_FLOAT:
415: read_double(s,&dbl); MKReal(dbl,real); *rp = (Obj)real;
1.1 noro 416: break;
417: case CMO_DISTRIBUTED_POLYNOMIAL:
418: read_cmo_dp(s,&dp); *rp = (Obj)dp;
419: break;
420: case CMO_RECURSIVE_POLYNOMIAL:
421: read_cmo_p(s,&p); *rp = (Obj)p;
422: break;
423: case CMO_UNIVARIATE_POLYNOMIAL:
424: read_cmo_upoly(s,&p); *rp = (Obj)p;
425: break;
426: case CMO_INDETERMINATE:
427: read_cmo(s,&str); *rp = (Obj)str;
428: break;
429: case CMO_RATIONAL:
430: read_cmo(s,&pnm); read_cmo(s,&pdn);
431: divr(CO,(Obj)pnm,(Obj)pdn,rp);
432: break;
433: case CMO_ZERO:
434: *rp = 0;
435: break;
436: case CMO_DMS_OF_N_VARIABLES:
437: read_cmo(s,rp);
438: break;
439: case CMO_RING_BY_NAME:
440: read_cmo(s,rp);
441: break;
442: default:
443: MKUSINT(t,id);
444: t->id = O_VOID;
445: *rp = (Obj)t;
446: break;
447: }
448: }
449:
450: read_cmo_uint(s,rp)
451: FILE *s;
452: USINT *rp;
453: {
454: unsigned int body;
455:
456: read_int(s,&body);
457: MKUSINT(*rp,body);
458: }
459:
460: read_cmo_zz(s,sgn,rp)
461: FILE *s;
462: int *sgn;
463: N *rp;
464: {
465: int l,i,words;
466: N n;
467: unsigned int *b;
468: unsigned int h;
469: unsigned char c;
470:
471: read_int(s,&l);
472: if ( l == 0 ) {
473: *sgn = 0;
474: *rp = 0;
475: return;
476: }
477: if ( l < 0 ) {
478: *sgn = -1; l = -l;
479: } else
480: *sgn = 1;
481: #if 1
482: *rp = n = NALLOC(l); PL(n) = l;
483: read_intarray(s,BD(n),l);
484: #else
485: words = (l+3)/4;
486: *rp = n = NALLOC(words); PL(n) = words; b = BD(n);
487: h = 0;
488: switch ( l % 4 ) {
489: case 0:
490: read_char(s,&c); h = c;
491: case 3:
492: read_char(s,&c); h = (h<<8)|c;
493: case 2:
494: read_char(s,&c); h = (h<<8)|c;
495: case 1:
496: read_char(s,&c); h = (h<<8)|c;
497: }
498: b[words-1] = h;
499: for ( i = words-2; i >= 0; i-- ) {
500: read_char(s,&c); h = c;
501: read_char(s,&c); h = (h<<8)|c;
502: read_char(s,&c); h = (h<<8)|c;
503: read_char(s,&c); h = (h<<8)|c;
504: b[i] = h;
505: }
506: #endif
507: }
508:
509: read_cmo_list(s,rp)
510: FILE *s;
511: Obj *rp;
512: {
513: int len;
514: Obj *w;
515: int i;
516: Obj r,r1;
517: NODE n0,n1;
518: LIST list;
519:
520: read_int(s,&len);
521: w = (Obj *)ALLOCA(len*sizeof(Obj));
522: for ( i = 0; i < len; i++ )
523: read_cmo(s,&w[i]);
524: for ( i = len-1, n0 = 0; i >= 0; i-- ) {
525: MKNODE(n1,w[i],n0); n0 = n1;
526: }
527: MKLIST(list,n0);
528: *rp = (Obj)list;
529: }
530:
531: read_cmo_dp(s,rp)
532: FILE *s;
533: DP *rp;
534: {
535: int len;
536: int i;
537: NODE n0,n1;
538: MP mp0,mp;
539: int nv,d;
540: DP dp;
541: Obj obj;
542:
543: read_int(s,&len);
544: /* skip the ring definition */
545: read_cmo(s,&obj);
546: for ( mp0 = 0, i = 0, d = 0; i < len; i++ ) {
547: read_cmo(s,&dp);
548: if ( !mp0 ) {
549: nv = dp->nv;
550: mp0 = dp->body;
551: mp = mp0;
552: } else {
553: NEXT(mp) = dp->body;
554: mp = NEXT(mp);
555: }
556: d = MAX(d,dp->sugar);
557: }
558: MKDP(nv,mp0,dp);
559: dp->sugar = d; *rp = dp;
560: }
561:
562: read_cmo_monomial(s,rp)
563: FILE *s;
564: DP *rp;
565: {
566: MP m;
567: DP dp;
568: int i,sugar,n;
569: DL dl;
570:
571: read_int(s,&n);
572: NEWMP(m); NEWDL(dl,n); m->dl = dl;
573: read_intarray(s,dl->d,n);
574: for ( sugar = 0, i = 0; i < n; i++ )
575: sugar += dl->d[i];
576: dl->td = sugar;
577: read_cmo(s,&m->c);
578: NEXT(m) = 0; MKDP(n,m,dp); dp->sugar = sugar; *rp = dp;
579: }
580:
581: static V *remote_vtab;
582:
583: read_cmo_p(s,rp)
584: FILE *s;
585: P *rp;
586: {
587: LIST vlist;
588: int nv,i;
589: V *vtab;
590: V v1,v2;
591: NODE t;
592: P v,p;
593: VL tvl,rvl;
594: char *name;
595:
596: read_cmo(s,&vlist);
597: nv = length(BDY(vlist));
598: vtab = (V *)ALLOCA(nv*sizeof(V));
599: for ( i = 0, t = BDY(vlist); i < nv; t = NEXT(t), i++ ) {
600: /* cmoname_to_localname(BDY((STRING)BDY(t)),&name); */
601: name = BDY((STRING)BDY(t));
602: makevar(name,&v); vtab[i] = VR(v);
603: }
604: remote_vtab = vtab;
605: read_cmo(s,&p);
606: for ( i = 0; i < nv-1; i++ ) {
607: v1 = vtab[i]; v2 = vtab[i+1];
608: for ( tvl = CO; tvl->v != v1 && tvl->v != v2; tvl = NEXT(tvl) );
609: if ( tvl->v == v2 )
610: break;
611: }
612: if ( i < nv-1 ) {
613: for ( i = nv-1, rvl = 0; i >= 0; i-- ) {
614: NEWVL(tvl); tvl->v = vtab[i]; NEXT(tvl) = rvl; rvl = tvl;
615: }
616: reorderp(CO,rvl,p,rp);
617: } else
618: *rp = p;
619: }
620:
621: read_cmo_upoly(s,rp)
622: FILE *s;
623: P *rp;
624: {
625: int n,ind,i,d;
626: P c;
627: Q q;
628: DCP dc0,dc;
629:
630: read_int(s,&n);
631: read_int(s,&ind);
632: for ( i = 0, dc0 = 0; i < n; i++ ) {
633: read_int(s,&d);
634: read_cmo(s,&c);
635: if ( c ) {
636: if ( OID(c) == O_USINT ) {
637: UTOQ(((USINT)c)->body,q); c = (P)q;
638: }
639: NEXTDC(dc0,dc);
640: STOQ(d,q);
641: dc->c = c; dc->d = q;
642: }
643: }
644: if ( dc0 )
645: NEXT(dc) = 0;
646: MKP(remote_vtab[ind],dc0,*rp);
647: }
648:
649: localname_to_cmoname(a,b)
650: char *a;
651: char **b;
652: {
653: int l;
654: char *t;
655:
656: l = strlen(a);
657: if ( l >= 2 && a[0] == '@' && isupper(a[1]) ) {
658: t = *b = (char *)MALLOC_ATOMIC(l);
659: strcpy(t,a+1);
660: } else {
661: t = *b = (char *)MALLOC_ATOMIC(l+1);
662: strcpy(t,a);
663: }
664: }
665:
666: cmoname_to_localname(a,b)
667: char *a;
668: char **b;
669: {
670: int l;
671: char *t;
672:
673: l = strlen(a);
674: if ( isupper(a[0]) ) {
675: t = *b = (char *)MALLOC_ATOMIC(l+2);
676: strcpy(t+1,a);
677: t[0] = '@';
678: } else {
679: t = *b = (char *)MALLOC_ATOMIC(l+1);
680: strcpy(t,a);
681: }
682: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>