Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.14
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
1.4 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 noro 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.14 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cio.c,v 1.13 2015/08/04 06:20:45 noro Exp $
1.3 noro 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
51: #include "ox.h"
1.12 ohara 52: #if !defined(VISUAL)
53: #include <ctype.h>
54: #endif
1.1 noro 55:
56: #define ISIZ sizeof(int)
57:
1.11 noro 58: int valid_as_cmo(Obj obj)
1.1 noro 59: {
1.14 ! noro 60: NODE m;
1.13 noro 61: int nid;
1.1 noro 62:
1.14 ! noro 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_BYTEARRAY: case O_VOID:
! 68: return 1;
! 69: case O_N:
1.13 noro 70: nid = NID((Num)obj);
1.14 ! noro 71: if ( nid == N_Q || nid == N_R || nid == N_B )
! 72: return 1;
! 73: else
! 74: return 0;
! 75: case O_LIST:
! 76: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
! 77: if ( !valid_as_cmo(BDY(m)) )
! 78: return 0;
! 79: return 1;
! 80: case O_QUOTE:
! 81: return 1;
! 82: default:
! 83: return 0;
! 84: }
1.1 noro 85: }
86:
1.11 noro 87: void write_cmo(FILE *s,Obj obj)
1.1 noro 88: {
1.14 ! noro 89: int r;
! 90: char errmsg[BUFSIZ];
! 91: LIST l;
! 92:
! 93: if ( !obj ) {
! 94: r = CMO_NULL; write_int(s,&r);
! 95: return;
! 96: }
! 97: switch ( OID(obj) ) {
! 98: case O_N:
! 99: switch ( NID((Num)obj) ) {
! 100: case N_Q:
! 101: write_cmo_q(s,(Q)obj);
! 102: break;
! 103: case N_R:
! 104: write_cmo_real(s,(Real)obj);
! 105: break;
1.13 noro 106: case N_B:
1.14 ! noro 107: write_cmo_bf(s,(BF)obj);
! 108: break;
! 109: default:
! 110: sprintf(errmsg, "write_cmo : number id=%d not implemented.",
! 111: NID((Num)obj));
! 112: error(errmsg);
! 113: break;
! 114: }
! 115: break;
! 116: case O_P:
! 117: write_cmo_p(s,(P)obj);
! 118: break;
! 119: case O_R:
! 120: write_cmo_r(s,(R)obj);
! 121: break;
! 122: case O_DP:
! 123: write_cmo_dp(s,(DP)obj);
! 124: break;
! 125: case O_LIST:
! 126: write_cmo_list(s,(LIST)obj);
! 127: break;
! 128: case O_STR:
! 129: write_cmo_string(s,(STRING)obj);
! 130: break;
! 131: case O_USINT:
! 132: write_cmo_uint(s,(USINT)obj);
! 133: break;
! 134: case O_MATHCAP:
! 135: write_cmo_mathcap(s,(MATHCAP)obj);
! 136: break;
! 137: case O_ERR:
! 138: write_cmo_error(s,(ERR)obj);
! 139: break;
! 140: case O_BYTEARRAY:
! 141: write_cmo_bytearray(s,(BYTEARRAY)obj);
! 142: break;
! 143: case O_VOID:
! 144: r = ((USINT)obj)->body; write_int(s,&r);
! 145: break;
! 146: case O_QUOTE:
! 147: fnodetotree(BDY((QUOTE)obj),&l);
! 148: write_cmo_tree(s,l);
! 149: break;
! 150: default:
! 151: sprintf(errmsg, "write_cmo : id=%d not implemented.",OID(obj));
! 152: error(errmsg);
! 153: break;
! 154: }
1.5 noro 155: }
156:
1.11 noro 157: int cmo_tag(Obj obj,int *tag)
1.5 noro 158: {
1.14 ! noro 159: if ( !valid_as_cmo(obj) )
! 160: return 0;
! 161: if ( !obj ) {
! 162: *tag = CMO_NULL;
! 163: return 1;
! 164: }
! 165: switch ( OID(obj) ) {
! 166: case O_N:
! 167: switch ( NID((Num)obj) ) {
! 168: case N_Q:
! 169: *tag = DN((Q)obj) ? CMO_QQ : CMO_ZZ; break;
! 170: case N_R:
! 171: *tag = CMO_IEEE_DOUBLE_FLOAT; break;
1.13 noro 172: case N_B:
1.14 ! noro 173: *tag = CMO_BIGFLOAT; break;
! 174: default:
! 175: return 0;
! 176: }
! 177: break;
! 178: case O_P:
! 179: *tag = CMO_RECURSIVE_POLYNOMIAL; break;
! 180: case O_R:
! 181: *tag = CMO_RATIONAL; break;
! 182: case O_DP:
! 183: *tag = CMO_DISTRIBUTED_POLYNOMIAL; break;
! 184: case O_LIST:
! 185: *tag = CMO_LIST; break;
! 186: case O_STR:
! 187: *tag = CMO_STRING; break;
! 188: case O_USINT:
! 189: *tag = CMO_INT32; break;
! 190: case O_MATHCAP:
! 191: *tag = CMO_MATHCAP; break;
! 192: case O_ERR:
! 193: *tag = CMO_ERROR2; break;
! 194: case O_QUOTE:
! 195: *tag = CMO_TREE; break; break;
! 196: default:
! 197: return 0;
! 198: }
! 199: return 1;
1.1 noro 200: }
201:
1.11 noro 202: void write_cmo_mathcap(FILE *s,MATHCAP mc)
1.1 noro 203: {
1.14 ! noro 204: unsigned int r;
1.1 noro 205:
1.14 ! noro 206: r = CMO_MATHCAP; write_int(s,&r);
! 207: write_cmo(s,(Obj)BDY(mc));
1.1 noro 208: }
209:
1.11 noro 210: void write_cmo_uint(FILE *s,USINT ui)
1.1 noro 211: {
1.14 ! noro 212: unsigned int r;
1.1 noro 213:
1.14 ! noro 214: r = CMO_INT32; write_int(s,&r);
! 215: r = ui->body; write_int(s,&r);
1.1 noro 216: }
217:
1.11 noro 218: void write_cmo_q(FILE *s,Q q)
1.1 noro 219: {
1.14 ! noro 220: int r;
1.1 noro 221:
1.14 ! noro 222: if ( q && DN(q) ) {
! 223: r = CMO_QQ; write_int(s,&r);
! 224: write_cmo_zz(s,SGN(q),NM(q));
! 225: write_cmo_zz(s,1,DN(q));
! 226: } else {
! 227: r = CMO_ZZ; write_int(s,&r);
! 228: write_cmo_zz(s,SGN(q),NM(q));
! 229: }
1.1 noro 230: }
231:
1.11 noro 232: void write_cmo_real(FILE *s,Real real)
1.2 noro 233: {
1.14 ! noro 234: unsigned int r;
! 235: double dbl;
1.2 noro 236:
1.14 ! noro 237: r = CMO_IEEE_DOUBLE_FLOAT; write_int(s,&r);
! 238: dbl = real->body; write_double(s,&dbl);
1.2 noro 239: }
240:
1.13 noro 241: void write_cmo_bf(FILE *s,BF bf)
242: {
1.14 ! noro 243: unsigned int r,u,l;
! 244: int len,t,i;
! 245: L exp;
! 246: UL *ptr;
1.13 noro 247:
1.14 ! noro 248: r = CMO_BIGFLOAT; write_int(s,&r);
1.13 noro 249: write_int(s,&MPFR_SIGN(bf->body));
250: write_int(s,&MPFR_PREC(bf->body));
251: exp = MPFR_EXP(bf->body);
252: write_int64(s,&exp);
1.14 ! noro 253: len = MPFR_LIMB_SIZE(bf->body);
! 254: #if SIZEOF_LONG == 4
1.13 noro 255: write_int(s,&len);
1.14 ! noro 256: write_intarray(s,MPFR_MANT(bf->body),len);
1.13 noro 257: #else /* SIZEOF_LONG == 8 */
1.14 ! noro 258: t = 2*len;
! 259: write_int(s,&t);
! 260: ptr = (UL *)MPFR_MANT(bf->body);
! 261: for ( i = 0; i < len; i++ ) {
! 262: u = ptr[i]>>32;
! 263: l = ptr[i]&0xffffffff;
! 264: write_int(s,&u);
! 265: write_int(s,&l);
! 266: }
1.13 noro 267: #endif
268: }
269:
1.11 noro 270: void write_cmo_zz(FILE *s,int sgn,N n)
1.1 noro 271: {
1.14 ! noro 272: int l,bytes;
1.1 noro 273:
274: #if 1
1.14 ! noro 275: l = PL(n);
! 276: bytes = sgn*l;
! 277: write_int(s,&bytes);
! 278: write_intarray(s,BD(n),l);
1.1 noro 279: #else
1.14 ! noro 280: l = PL(n); b = (unsigned int *)BD(n);
! 281: bytes = sgn*4*l;
! 282: write_int(s,&bytes);
! 283: for ( i = l-1; i >= 0; i-- ) {
! 284: t = b[i];
! 285: c = t>>24; write_char(s,&c);
! 286: c = (t>>16)&0xff; write_char(s,&c);
! 287: c = (t>>8)&0xff; write_char(s,&c);
! 288: c = t&0xff; write_char(s,&c);
! 289: }
1.1 noro 290: #endif
291: }
292:
1.11 noro 293: void write_cmo_p(FILE *s,P p)
1.1 noro 294: {
1.14 ! noro 295: int r,i;
! 296: VL t,vl;
! 297: char *namestr;
! 298: STRING name;
! 299:
! 300: r = CMO_RECURSIVE_POLYNOMIAL; write_int(s,&r);
! 301: get_vars((Obj)p,&vl);
! 302:
! 303: /* indeterminate list */
! 304: r = CMO_LIST; write_int(s,&r);
! 305: for ( t = vl, i = 0; t; t = NEXT(t), i++ );
! 306: write_int(s,&i);
! 307: r = CMO_INDETERMINATE;
! 308: for ( t = vl; t; t = NEXT(t) ) {
! 309: write_int(s,&r);
! 310: /* localname_to_cmoname(NAME(t->v),&namestr); */
! 311: namestr = NAME(t->v);
! 312: MKSTR(name,namestr);
! 313: write_cmo(s,(Obj)name);
! 314: }
1.1 noro 315:
1.14 ! noro 316: /* body */
! 317: write_cmo_upoly(s,vl,p);
1.1 noro 318: }
319:
1.11 noro 320: void write_cmo_upoly(FILE *s,VL vl,P p)
1.1 noro 321: {
1.14 ! noro 322: int r,i;
! 323: V v;
! 324: DCP dc,dct;
! 325: VL vlt;
! 326:
! 327: if ( NUM(p) )
! 328: write_cmo(s,(Obj)p);
! 329: else {
! 330: r = CMO_UNIVARIATE_POLYNOMIAL; write_int(s,&r);
! 331: v = VR(p);
! 332: dc = DC(p);
! 333: for ( i = 0, dct = dc; dct; dct = NEXT(dct), i++ );
! 334: write_int(s,&i);
! 335: for ( i = 0, vlt = vl; vlt->v != v; vlt = NEXT(vlt), i++ );
! 336: write_int(s,&i);
! 337: for ( dct = dc; dct; dct = NEXT(dct) ) {
! 338: i = QTOS(DEG(dct)); write_int(s,&i);
! 339: write_cmo_upoly(s,vl,COEF(dct));
! 340: }
! 341: }
1.1 noro 342: }
343:
1.11 noro 344: void write_cmo_r(FILE *s,R f)
1.1 noro 345: {
1.14 ! noro 346: int r;
1.1 noro 347:
1.14 ! noro 348: r = CMO_RATIONAL; write_int(s,&r);
! 349: write_cmo(s,(Obj)NM(f));
! 350: write_cmo(s,(Obj)DN(f));
1.1 noro 351: }
352:
1.11 noro 353: void write_cmo_dp(FILE *s,DP dp)
1.1 noro 354: {
1.14 ! noro 355: int i,n,nv,r;
! 356: MP m;
1.1 noro 357:
1.14 ! noro 358: for ( n = 0, m = BDY(dp); m; m = NEXT(m), n++ );
! 359: r = CMO_DISTRIBUTED_POLYNOMIAL; write_int(s,&r);
! 360: r = n; write_int(s,&r);
! 361: r = CMO_DMS_GENERIC; write_int(s,&r);
! 362: nv = dp->nv;
! 363: for ( i = 0, m = BDY(dp); i < n; i++, m = NEXT(m) )
! 364: write_cmo_monomial(s,m,nv);
1.1 noro 365: }
366:
1.11 noro 367: void write_cmo_monomial(FILE *s,MP m,int n)
1.1 noro 368: {
1.14 ! noro 369: int i,r;
! 370: int *p;
1.1 noro 371:
1.14 ! noro 372: r = CMO_MONOMIAL32; write_int(s,&r);
! 373: write_int(s,&n);
! 374: for ( i = 0, p = m->dl->d; i < n; i++ ) {
! 375: write_int(s,p++);
! 376: }
! 377: write_cmo_q(s,(Q)m->c);
1.1 noro 378: }
379:
1.11 noro 380: void write_cmo_list(FILE *s,LIST list)
1.1 noro 381: {
1.14 ! noro 382: NODE m;
! 383: int i,n,r;
1.1 noro 384:
1.14 ! noro 385: for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
! 386: r = CMO_LIST; write_int(s,&r);
! 387: write_int(s,&n);
! 388: for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
! 389: write_cmo(s,BDY(m));
1.1 noro 390: }
391:
1.11 noro 392: void write_cmo_string(FILE *s,STRING str)
1.1 noro 393: {
1.14 ! noro 394: int r;
1.1 noro 395:
1.14 ! noro 396: r = CMO_STRING; write_int(s,&r);
! 397: savestr(s,BDY(str));
1.1 noro 398: }
399:
1.11 noro 400: void write_cmo_bytearray(FILE *s,BYTEARRAY array)
1.6 noro 401: {
1.14 ! noro 402: int r;
1.6 noro 403:
1.14 ! noro 404: r = CMO_DATUM; write_int(s,&r);
! 405: write_int(s,&array->len);
! 406: write_string(s,array->body,array->len);
1.6 noro 407: }
408:
1.11 noro 409: void write_cmo_error(FILE *s,ERR e)
1.1 noro 410: {
1.14 ! noro 411: int r;
1.1 noro 412:
1.14 ! noro 413: r = CMO_ERROR2; write_int(s,&r);
! 414: write_cmo(s,BDY(e));
1.1 noro 415: }
416:
1.7 noro 417: /* XXX */
418:
1.10 noro 419: /*
420: * BDY(l) = treenode
421: * treenode = [property,(name,)arglist]
422: * arglist = list of treenode
423: */
424:
1.11 noro 425: void write_cmo_tree(FILE *s,LIST l)
1.7 noro 426: {
1.14 ! noro 427: NODE n;
! 428: int r;
! 429: STRING prop,name,key;
! 430:
! 431: /* (CMO_TREE (CMO_LIST,n,key1,attr1,...,keyn,attn),(CMO_LIST,m,arg1,...,argm)) */
! 432: n = BDY(l);
! 433: prop = (STRING)BDY(n); n = NEXT(n);
! 434: if ( !strcmp(BDY(prop),"internal") ) {
! 435: write_cmo(s,(Obj)BDY(n));
! 436: } else {
! 437: if ( strcmp(BDY(prop),"list") ) {
! 438: r = CMO_TREE; write_int(s,&r);
! 439: name = (STRING)BDY(n);
! 440: n = NEXT(n);
! 441: /* function name */
! 442: write_cmo(s,(Obj)name);
! 443:
! 444: /* attribute list */
! 445: r = CMO_LIST; write_int(s,&r);
! 446: r = 2; write_int(s,&r);
! 447: MKSTR(key,"asir");
! 448: write_cmo(s,(Obj)key);
! 449: write_cmo(s,(Obj)prop);
! 450: }
! 451:
! 452: /* argument list */
! 453: r = CMO_LIST; write_int(s,&r);
! 454: /* len = number of arguments */
! 455: r = length(n); write_int(s,&r);
! 456: while ( n ) {
! 457: write_cmo_tree(s,BDY(n));
! 458: n = NEXT(n);
! 459: }
! 460: }
1.7 noro 461: }
462:
1.11 noro 463: void read_cmo(FILE *s,Obj *rp)
1.1 noro 464: {
1.14 ! noro 465: int id;
! 466: int sgn,dummy;
! 467: Q q;
! 468: N nm,dn;
! 469: P p,pnm,pdn;
! 470: Real real;
! 471: double dbl;
! 472: STRING str;
! 473: USINT t;
! 474: DP dp;
! 475: Obj obj;
! 476: ERR e;
1.13 noro 477: BF bf;
1.14 ! noro 478: MATHCAP mc;
! 479: BYTEARRAY array;
! 480: LIST list;
! 481:
! 482: read_int(s,&id);
! 483: switch ( id ) {
! 484: /* level 0 objects */
! 485: case CMO_NULL:
! 486: *rp = 0;
! 487: break;
! 488: case CMO_INT32:
! 489: read_cmo_uint(s,&t); *rp = (Obj)t;
! 490: break;
! 491: case CMO_DATUM:
! 492: loadbytearray(s,&array); *rp = (Obj)array;
! 493: break;
! 494: case CMO_STRING:
! 495: loadstring(s,&str); *rp = (Obj)str;
! 496: break;
! 497: case CMO_MATHCAP:
! 498: read_cmo(s,&obj); MKMATHCAP(mc,(LIST)obj);
! 499: *rp = (Obj)mc;
! 500: break;
! 501: case CMO_ERROR:
! 502: MKERR(e,0); *rp = (Obj)e;
! 503: break;
! 504: case CMO_ERROR2:
! 505: read_cmo(s,&obj); MKERR(e,obj); *rp = (Obj)e;
! 506: break;
! 507: /* level 1 objects */
! 508: case CMO_LIST:
! 509: read_cmo_list(s,rp);
! 510: break;
! 511: case CMO_MONOMIAL32:
! 512: read_cmo_monomial(s,&dp); *rp = (Obj)dp;
! 513: break;
! 514: case CMO_ZZ:
! 515: read_cmo_zz(s,&sgn,&nm);
! 516: NTOQ(nm,sgn,q); *rp = (Obj)q;
! 517: break;
! 518: case CMO_QQ:
! 519: read_cmo_zz(s,&sgn,&nm);
! 520: read_cmo_zz(s,&dummy,&dn);
! 521: NDTOQ(nm,dn,sgn,q); *rp = (Obj)q;
! 522: break;
! 523: case CMO_IEEE_DOUBLE_FLOAT:
! 524: read_double(s,&dbl); MKReal(dbl,real); *rp = (Obj)real;
! 525: break;
! 526: case CMO_BIGFLOAT:
! 527: read_cmo_bf(s,&bf); *rp = (Obj)bf;
! 528: break;
! 529: case CMO_DISTRIBUTED_POLYNOMIAL:
! 530: read_cmo_dp(s,&dp); *rp = (Obj)dp;
! 531: break;
! 532: case CMO_RECURSIVE_POLYNOMIAL:
! 533: read_cmo_p(s,&p); *rp = (Obj)p;
! 534: break;
! 535: case CMO_UNIVARIATE_POLYNOMIAL:
! 536: read_cmo_upoly(s,&p); *rp = (Obj)p;
! 537: break;
! 538: case CMO_INDETERMINATE:
! 539: read_cmo(s,rp);
! 540: break;
! 541: case CMO_RATIONAL:
! 542: read_cmo(s,&obj); pnm = (P)obj;
! 543: read_cmo(s,&obj); pdn = (P)obj;
! 544: divr(CO,(Obj)pnm,(Obj)pdn,rp);
! 545: break;
! 546: case CMO_ZERO:
! 547: *rp = 0;
! 548: break;
! 549: case CMO_DMS_OF_N_VARIABLES:
! 550: read_cmo(s,rp);
! 551: break;
! 552: case CMO_RING_BY_NAME:
! 553: read_cmo(s,rp);
! 554: break;
! 555: case CMO_TREE:
! 556: read_cmo_tree_as_list(s,&list);
1.10 noro 557: #if 0
1.14 ! noro 558: treetofnode(list,&fn);
! 559: MKQUOTE(quote,fn);
! 560: *rp = (Obj)quote;
1.10 noro 561: #else
1.14 ! noro 562: *rp = (Obj)list;
1.10 noro 563: #endif
1.14 ! noro 564: break;
! 565: default:
! 566: MKUSINT(t,id);
! 567: t->id = O_VOID;
! 568: *rp = (Obj)t;
! 569: break;
! 570: }
1.1 noro 571: }
572:
1.11 noro 573: void read_cmo_uint(FILE *s,USINT *rp)
1.1 noro 574: {
1.14 ! noro 575: unsigned int body;
1.1 noro 576:
1.14 ! noro 577: read_int(s,&body);
! 578: MKUSINT(*rp,body);
1.1 noro 579: }
580:
1.11 noro 581: void read_cmo_zz(FILE *s,int *sgn,N *rp)
1.1 noro 582: {
1.14 ! noro 583: int l;
! 584: N n;
1.1 noro 585:
1.14 ! noro 586: read_int(s,&l);
! 587: if ( l == 0 ) {
! 588: *sgn = 0;
! 589: *rp = 0;
! 590: return;
! 591: }
! 592: if ( l < 0 ) {
! 593: *sgn = -1; l = -l;
! 594: } else
! 595: *sgn = 1;
1.1 noro 596: #if 1
1.14 ! noro 597: *rp = n = NALLOC(l); PL(n) = l;
! 598: read_intarray(s,BD(n),l);
1.1 noro 599: #else
1.14 ! noro 600: words = (l+3)/4;
! 601: *rp = n = NALLOC(words); PL(n) = words; b = BD(n);
! 602: h = 0;
! 603: switch ( l % 4 ) {
! 604: case 0:
! 605: read_char(s,&c); h = c;
! 606: case 3:
! 607: read_char(s,&c); h = (h<<8)|c;
! 608: case 2:
! 609: read_char(s,&c); h = (h<<8)|c;
! 610: case 1:
! 611: read_char(s,&c); h = (h<<8)|c;
! 612: }
! 613: b[words-1] = h;
! 614: for ( i = words-2; i >= 0; i-- ) {
! 615: read_char(s,&c); h = c;
! 616: read_char(s,&c); h = (h<<8)|c;
! 617: read_char(s,&c); h = (h<<8)|c;
! 618: read_char(s,&c); h = (h<<8)|c;
! 619: b[i] = h;
! 620: }
1.1 noro 621: #endif
622: }
623:
1.13 noro 624: void read_cmo_bf(FILE *s,BF *bf)
625: {
1.14 ! noro 626: BF r;
! 627: int sgn,prec,len,i;
! 628: unsigned int u,l;
! 629: UL *ptr;
! 630: L exp;
1.13 noro 631:
632: NEWBF(r);
633: read_int(s,&sgn);
634: read_int(s,&prec);
635: read_int64(s,&exp);
636: read_int(s,&len);
637: mpfr_init2(r->body,prec);
638: MPFR_SIGN(r->body) = sgn;
639: MPFR_EXP(r->body) = exp;
640: #if SIZEOF_LONG == 4
1.14 ! noro 641: read_intarray(s,MPFR_MANT(r->body),len);
1.13 noro 642: #else /* SIZEOF_LONG == 8 */
1.14 ! noro 643: len >>= 1;
! 644: ptr = (UL *)MPFR_MANT(r->body);
! 645: for ( i = 0; i < len; i++ ) {
! 646: read_int(s,&u);
! 647: read_int(s,&l);
! 648: ptr[i] = ((UL)u)<<32|((UL)l)&0xffffffff;
! 649: }
1.13 noro 650: #endif
651: *bf = r;
652: }
653:
1.11 noro 654: void read_cmo_list(FILE *s,Obj *rp)
1.1 noro 655: {
1.14 ! noro 656: int len;
! 657: Obj *w;
! 658: int i;
! 659: NODE n0,n1;
! 660: LIST list;
! 661:
! 662: read_int(s,&len);
! 663: w = (Obj *)ALLOCA(len*sizeof(Obj));
! 664: for ( i = 0; i < len; i++ )
! 665: read_cmo(s,&w[i]);
! 666: for ( i = len-1, n0 = 0; i >= 0; i-- ) {
! 667: MKNODE(n1,w[i],n0); n0 = n1;
! 668: }
! 669: MKLIST(list,n0);
! 670: *rp = (Obj)list;
1.1 noro 671: }
672:
1.11 noro 673: void read_cmo_dp(FILE *s,DP *rp)
1.1 noro 674: {
1.14 ! noro 675: int len;
! 676: int i;
! 677: MP mp0,mp;
! 678: int nv,d;
! 679: DP dp;
! 680: Obj obj;
! 681:
! 682: read_int(s,&len);
! 683: /* skip the ring definition */
! 684: read_cmo(s,&obj);
! 685: for ( mp0 = 0, i = 0, d = 0; i < len; i++ ) {
! 686: read_cmo(s,&obj); dp = (DP)obj;
! 687: if ( !mp0 ) {
! 688: nv = dp->nv;
! 689: mp0 = dp->body;
! 690: mp = mp0;
! 691: } else {
! 692: NEXT(mp) = dp->body;
! 693: mp = NEXT(mp);
! 694: }
! 695: d = MAX(d,dp->sugar);
! 696: }
! 697: MKDP(nv,mp0,dp);
! 698: dp->sugar = d; *rp = dp;
1.1 noro 699: }
700:
1.11 noro 701: void read_cmo_monomial(FILE *s,DP *rp)
1.1 noro 702: {
1.14 ! noro 703: Obj obj;
! 704: MP m;
! 705: DP dp;
! 706: int i,sugar,n;
! 707: DL dl;
! 708:
! 709: read_int(s,&n);
! 710: NEWMP(m); NEWDL(dl,n); m->dl = dl;
! 711: read_intarray(s,dl->d,n);
! 712: for ( sugar = 0, i = 0; i < n; i++ )
! 713: sugar += dl->d[i];
! 714: dl->td = sugar;
! 715: read_cmo(s,&obj); m->c = (P)obj;
! 716: NEXT(m) = 0; MKDP(n,m,dp); dp->sugar = sugar; *rp = dp;
1.1 noro 717: }
718:
719: static V *remote_vtab;
720:
1.11 noro 721: void read_cmo_p(FILE *s,P *rp)
1.1 noro 722: {
1.14 ! noro 723: Obj obj;
! 724: LIST vlist;
! 725: int nv,i;
! 726: V *vtab;
! 727: V v1,v2;
! 728: NODE t;
! 729: P v,p;
! 730: VL tvl,rvl;
! 731: char *name;
! 732:
! 733: read_cmo(s,&obj); vlist = (LIST)obj;
! 734: nv = length(BDY(vlist));
! 735: vtab = (V *)ALLOCA(nv*sizeof(V));
! 736: for ( i = 0, t = BDY(vlist); i < nv; t = NEXT(t), i++ ) {
! 737: /* cmoname_to_localname(BDY((STRING)BDY(t)),&name); */
! 738: name = BDY((STRING)BDY(t));
! 739: makevar(name,&v); vtab[i] = VR(v);
! 740: }
! 741: remote_vtab = vtab;
! 742: read_cmo(s,&obj); p = (P)obj;
! 743: for ( i = 0; i < nv-1; i++ ) {
! 744: v1 = vtab[i]; v2 = vtab[i+1];
! 745: for ( tvl = CO; tvl->v != v1 && tvl->v != v2; tvl = NEXT(tvl) );
! 746: if ( tvl->v == v2 )
! 747: break;
! 748: }
! 749: if ( i < nv-1 ) {
! 750: for ( i = nv-1, rvl = 0; i >= 0; i-- ) {
! 751: NEWVL(tvl); tvl->v = vtab[i]; NEXT(tvl) = rvl; rvl = tvl;
! 752: }
! 753: reorderp(CO,rvl,p,rp);
! 754: } else
! 755: *rp = p;
1.1 noro 756: }
757:
1.11 noro 758: void read_cmo_upoly(FILE *s,P *rp)
1.1 noro 759: {
1.14 ! noro 760: int n,ind,i,d;
! 761: Obj obj;
! 762: P c;
! 763: Q q;
! 764: DCP dc0,dc;
! 765:
! 766: read_int(s,&n);
! 767: read_int(s,&ind);
! 768: for ( i = 0, dc0 = 0; i < n; i++ ) {
! 769: read_int(s,&d);
! 770: read_cmo(s,&obj); c = (P)obj;
! 771: if ( c ) {
! 772: if ( OID(c) == O_USINT ) {
! 773: UTOQ(((USINT)c)->body,q); c = (P)q;
! 774: }
! 775: NEXTDC(dc0,dc);
! 776: STOQ(d,q);
! 777: dc->c = c; dc->d = q;
! 778: }
! 779: }
! 780: if ( dc0 )
! 781: NEXT(dc) = 0;
! 782: MKP(remote_vtab[ind],dc0,*rp);
1.7 noro 783: }
784:
785: /* XXX */
786:
787: extern struct oARF arf[];
788:
789: struct operator_tab {
1.14 ! noro 790: char *name;
! 791: fid id;
! 792: ARF arf;
! 793: cid cid;
1.7 noro 794: };
795:
796: static struct operator_tab optab[] = {
1.14 ! noro 797: {"+",I_BOP,&arf[0],0}, /* XXX */
! 798: {"-",I_BOP,&arf[1],0},
! 799: {"*",I_BOP,&arf[2],0},
! 800: {"/",I_BOP,&arf[3],0},
! 801: {"%",I_BOP,&arf[4],0},
! 802: {"^",I_BOP,&arf[5],0},
! 803: {"==",I_COP,0,C_EQ},
! 804: {"!=",I_COP,0,C_NE},
! 805: {"<",I_COP,0,C_LT},
! 806: {"<=",I_COP,0,C_LE},
! 807: {">",I_COP,0,C_GT},
! 808: {">=",I_COP,0,C_GE},
! 809: {"&&",I_AND,0,0},
! 810: {"||",I_OR,0,0},
! 811: {"!",I_NOT,0,0},
1.7 noro 812: };
813:
814: static int optab_len = sizeof(optab)/sizeof(struct operator_tab);
815:
1.10 noro 816: #if 0
817: /* old code */
1.11 noro 818: void read_cmo_tree(s,rp)
1.7 noro 819: FILE *s;
1.8 noro 820: FNODE *rp;
1.7 noro 821: {
1.14 ! noro 822: int r,i,n;
! 823: char *opname;
! 824: STRING name,cd;
! 825: int op;
! 826: pointer *arg;
! 827: QUOTE quote;
! 828: FNODE fn;
! 829: NODE t,t1;
! 830: fid id;
! 831: Obj expr;
! 832: FUNC func;
! 833:
! 834: read_cmo(s,&name);
! 835: read_cmo(s,&attr);
! 836: for ( i = 0; i < optab_len; i++ )
! 837: if ( !strcmp(optab[i].name,BDY(name)) )
! 838: break;
! 839: if ( i == optab_len ) {
! 840: /* may be a function name */
! 841: n = read_cmo_tree_arg(s,&arg);
! 842: for ( i = n-1, t = 0; i >= 0; i-- ) {
! 843: MKNODE(t1,arg[i],t); t = t1;
! 844: }
! 845: searchf(sysf,BDY(name),&func);
! 846: if ( !func )
! 847: searchf(ubinf,BDY(name),&func);
! 848: if ( !func )
! 849: searchpf(BDY(name),&func);
! 850: if ( !func )
! 851: searchf(usrf,BDY(name),&func);
! 852: if ( !func )
! 853: appenduf(BDY(name),&func);
! 854: *rp = mkfnode(2,I_FUNC,func,mkfnode(1,I_LIST,t));
! 855: } else {
! 856: opname = optab[i].name;
! 857: id = optab[i].id;
! 858: switch ( id ) {
! 859: case I_BOP:
! 860: read_cmo_tree_arg(s,&arg);
! 861: *rp = mkfnode(3,I_BOP,optab[i].arf,arg[0],arg[1]);
! 862: return;
! 863: case I_COP:
! 864: read_cmo_tree_arg(s,&arg);
! 865: *rp = mkfnode(3,I_COP,optab[i].cid,arg[0],arg[0]);
! 866: return;
! 867: case I_AND:
! 868: read_cmo_tree_arg(s,&arg);
! 869: *rp = mkfnode(2,I_AND,arg[0],arg[1]);
! 870: return;
! 871: case I_OR:
! 872: read_cmo_tree_arg(s,&arg);
! 873: *rp = mkfnode(2,I_OR,arg[0],arg[1]);
! 874: return;
! 875: case I_NOT:
! 876: read_cmo_tree_arg(s,&arg);
! 877: *rp = mkfnode(1,I_OR,arg[0]);
! 878: return;
! 879: }
! 880: }
1.8 noro 881: }
882:
883: int read_cmo_tree_arg(s,argp)
884: FILE *s;
885: pointer **argp;
886: {
1.14 ! noro 887: int id,n,i;
! 888: pointer *ap;
! 889: Obj t;
! 890:
! 891: read_int(s,&id); /* id = CMO_LIST */
! 892: read_int(s,&n); /* n = the number of args */
! 893: *argp = ap = (pointer *) MALLOC(n*sizeof(pointer));
! 894: for ( i = 0; i < n; i++ ) {
! 895: read_cmo(s,&t);
! 896: if ( !t || (OID(t) != O_QUOTE) )
! 897: ap[i] = mkfnode(1,I_FORMULA,t);
! 898: else
! 899: ap[i] = BDY((QUOTE)t);
! 900: }
! 901: return n;
1.1 noro 902: }
1.10 noro 903: #else
1.11 noro 904: void read_cmo_tree_as_list(FILE *s,LIST *rp)
1.10 noro 905: {
1.14 ! noro 906: Obj obj;
! 907: STRING name;
! 908: LIST attr,args;
! 909: NODE t0,t1;
! 910:
! 911: read_cmo(s,&obj); name = (STRING)obj;
! 912: read_cmo(s,&obj); attr = (LIST)obj;
! 913: read_cmo(s,&obj); args = (LIST)obj;
! 914: MKNODE(t1,name,BDY(args));
! 915: MKNODE(t0,attr,t1);
! 916: MKLIST(*rp,t0);
1.10 noro 917: }
918: #endif
1.1 noro 919:
1.11 noro 920: void localname_to_cmoname(char *a,char **b)
1.1 noro 921: {
1.14 ! noro 922: int l;
! 923: char *t;
1.1 noro 924:
1.14 ! noro 925: l = strlen(a);
! 926: if ( l >= 2 && a[0] == '@' && isupper(a[1]) ) {
! 927: t = *b = (char *)MALLOC_ATOMIC(l);
! 928: strcpy(t,a+1);
! 929: } else {
! 930: t = *b = (char *)MALLOC_ATOMIC(l+1);
! 931: strcpy(t,a);
! 932: }
1.1 noro 933: }
934:
1.11 noro 935: void cmoname_to_localname(char *a,char **b)
1.1 noro 936: {
1.14 ! noro 937: int l;
! 938: char *t;
1.1 noro 939:
1.14 ! noro 940: l = strlen(a);
! 941: if ( isupper(a[0]) ) {
! 942: t = *b = (char *)MALLOC_ATOMIC(l+2);
! 943: strcpy(t+1,a);
! 944: t[0] = '@';
! 945: } else {
! 946: t = *b = (char *)MALLOC_ATOMIC(l+1);
! 947: strcpy(t,a);
! 948: }
1.1 noro 949: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>