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

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

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