[BACK]Return to cio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.17

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>