[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.15

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

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