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

Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.19

1.19    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.18 2003/07/31 05:42:22 noro Exp $ */
1.2       noro        2:
1.1       noro        3: #include "ca.h"
                      4: #include "inline.h"
                      5:
                      6: #if defined(__GNUC__)
                      7: #define INLINE inline
                      8: #elif defined(VISUAL)
                      9: #define INLINE __inline
                     10: #else
                     11: #define INLINE
                     12: #endif
                     13:
                     14: #define REDTAB_LEN 32003
                     15:
                     16: typedef struct oPGeoBucket {
                     17:        int m;
                     18:        struct oND *body[32];
                     19: } *PGeoBucket;
                     20:
                     21: typedef struct oND {
                     22:        struct oNM *body;
                     23:        int nv;
                     24:        int sugar;
                     25: } *ND;
                     26:
1.3       noro       27: typedef struct oNDV {
                     28:        struct oNMV *body;
                     29:        int nv;
                     30:        int sugar;
                     31:        int len;
                     32: } *NDV;
                     33:
1.1       noro       34: typedef struct oNM {
                     35:        struct oNM *next;
1.14      noro       36:        union {
                     37:                int m;
                     38:                Q z;
                     39:        } c;
1.1       noro       40:        int td;
                     41:        unsigned int dl[1];
                     42: } *NM;
                     43:
1.3       noro       44: typedef struct oNMV {
1.14      noro       45:        union {
                     46:                int m;
                     47:                Q z;
                     48:        } c;
1.3       noro       49:        int td;
                     50:        unsigned int dl[1];
                     51: } *NMV;
                     52:
1.13      noro       53: typedef struct oRHist {
                     54:        struct oRHist *next;
                     55:        int index;
                     56:        int td;
                     57:        unsigned int dl[1];
                     58: } *RHist;
                     59:
1.1       noro       60: typedef struct oND_pairs {
                     61:        struct oND_pairs *next;
                     62:        int i1,i2;
                     63:        int td,sugar;
                     64:        unsigned int lcm[1];
                     65: } *ND_pairs;
                     66:
                     67: static unsigned int **nd_bound;
1.19    ! noro       68: int nd_nvar;
1.1       noro       69: int is_rlex;
                     70: int nd_epw,nd_bpe,nd_wpd;
                     71: unsigned int nd_mask[32];
                     72: unsigned int nd_mask0,nd_mask1;
                     73: NM _nm_free_list;
                     74: ND _nd_free_list;
                     75: ND_pairs _ndp_free_list;
1.13      noro       76: RHist *nd_red;
1.1       noro       77: int nd_red_len;
                     78:
                     79: extern int Top,Reverse;
                     80: int nd_psn,nd_pslen;
                     81: int nd_found,nd_create,nd_notfirst;
1.6       noro       82: int *nd_psl;
1.13      noro       83: RHist *nd_psh;
                     84: int nm_adv;
                     85: #define NM_ADV(m) (m = (NM)(((char *)m)+nm_adv))
1.1       noro       86:
                     87: void GC_gcollect();
                     88: NODE append_one(NODE,int);
                     89:
                     90: #define HTD(d) ((d)->body->td)
                     91: #define HDL(d) ((d)->body->dl)
1.14      noro       92: #define HCM(d) ((d)->body->c.m)
1.16      noro       93: #define HCQ(d) ((d)->body->c.z)
1.14      noro       94: #define CM(a) ((a)->c.m)
1.16      noro       95: #define CQ(a) ((a)->c.z)
1.14      noro       96: #define DL(a) ((a)->dl)
                     97: #define TD(a) ((a)->td)
                     98: #define SG(a) ((a)->sugar)
                     99: #define LEN(a) ((a)->len)
1.1       noro      100:
1.15      noro      101: #define NEWRHist(r) \
                    102: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(unsigned int)))
1.1       noro      103: #define NEWND_pairs(m) if(!_ndp_free_list)_NDP_alloc(); (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
                    104: #define NEWNM(m) if(!_nm_free_list)_NM_alloc(); (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
                    105: #define MKND(n,m,d) if(!_nd_free_list)_ND_alloc(); (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list); (d)->nv=(n); BDY(d)=(m)
                    106:
1.13      noro      107: #define NEXTRHist(r,c) \
                    108: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1       noro      109: #define NEXTNM(r,c) \
                    110: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
                    111: #define NEXTNM2(r,c,s) \
                    112: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
                    113: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
                    114: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
                    115: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
                    116:
                    117: #define NEXTND_pairs(r,c) \
                    118: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
                    119:
1.16      noro      120: void nd_removecont(ND p);
                    121: void ndv_removecont(NDV p);
                    122: void ndv_mul_c_q(NDV p,Q mul);
                    123: void nd_mul_c_q(ND p,Q mul);
                    124:
1.1       noro      125: ND_pairs crit_B( ND_pairs d, int s );
                    126: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp);
1.16      noro      127: NODE nd_setup(int mod,NODE f);
                    128: int nd_newps(int mod,ND a);
1.1       noro      129: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
                    130: NODE update_base(NODE nd,int ndp);
                    131: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
                    132: int crit_2( int dp1, int dp2 );
                    133: ND_pairs crit_F( ND_pairs d1 );
                    134: ND_pairs crit_M( ND_pairs d1 );
                    135: ND_pairs nd_newpairs( NODE g, int t );
                    136: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
1.16      noro      137: NODE nd_gb(int m,NODE f);
1.1       noro      138: void nd_free_private_storage();
                    139: void _NM_alloc();
                    140: void _ND_alloc();
                    141: int ndl_td(unsigned int *d);
1.19    ! noro      142: ND nd_add(int mod,ND p1,ND p2);
1.17      noro      143: ND nd_add_q(ND p1,ND p2);
                    144: ND nd_mul_nm(int mod,ND p,NM m0);
                    145: ND nd_mul_ind_nm(int mod,int index,NM m0);
1.16      noro      146: int nd_sp(int mod,ND_pairs p,ND *nf);
1.6       noro      147: int nd_find_reducer(ND g);
1.16      noro      148: int nd_nf(int mod,ND g,int full,ND *nf);
1.1       noro      149: ND nd_reduce(ND p1,ND p2);
                    150: ND nd_reduce_special(ND p1,ND p2);
                    151: void nd_free(ND p);
                    152: void ndl_print(unsigned int *dl);
                    153: void nd_print(ND p);
1.16      noro      154: void nd_print_q(ND p);
                    155: void ndv_print(NDV p);
                    156: void ndv_print_q(NDV p);
1.1       noro      157: void ndp_print(ND_pairs d);
                    158: int nd_length(ND p);
1.19    ! noro      159: void nd_monic(int mod,ND p);
        !           160: void nd_mul_c(int mod,ND p,int mul);
1.1       noro      161: void nd_free_redlist();
                    162: void nd_append_red(unsigned int *d,int td,int i);
                    163: unsigned int *nd_compute_bound(ND p);
1.5       noro      164: unsigned int *dp_compute_bound(DP p);
1.1       noro      165: ND_pairs nd_reconstruct(ND_pairs);
                    166: void nd_setup_parameters();
1.11      noro      167: void nd_realloc(ND p,int obpe);
1.6       noro      168: ND nd_copy(ND p);
1.1       noro      169: void ndl_dup(int obpe,unsigned int *d,unsigned int *r);
1.4       noro      170:
1.11      noro      171: static NDV *nd_ps;
                    172:
1.4       noro      173: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
                    174: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
1.14      noro      175: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.4       noro      176:
                    177: int nmv_adv;
                    178: int nmv_len;
                    179: NDV ndv_red;
                    180:
1.19    ! noro      181: void ndv_mul_c(int mod,NDV p,int mul);
        !           182: ND ndv_add(int mod,ND p1,NDV p2);
1.16      noro      183: ND ndv_add_q(ND p1,NDV p2);
                    184: NDV ndtondv(int mod,ND p);
                    185: void ndv_mul_nm(int mod,NDV pv,NM m,NDV r);
                    186: ND ndv_mul_nm_create(int mod,NDV p,NM m0);
1.11      noro      187: void ndv_realloc(NDV p,int obpe,int oadv);
1.16      noro      188: NDV dptondv(int,DP);
                    189: DP ndvtodp(int,NDV);
1.17      noro      190: ND dptond(int,DP);
                    191: DP ndtodp(int,ND);
1.1       noro      192:
                    193: void nd_free_private_storage()
                    194: {
                    195:        _nd_free_list = 0;
                    196:        _nm_free_list = 0;
1.5       noro      197:        _ndp_free_list = 0;
1.13      noro      198:        bzero(nd_red,sizeof(REDTAB_LEN*sizeof(RHist)));
1.1       noro      199:        GC_gcollect();
                    200: }
                    201:
                    202: void _NM_alloc()
                    203: {
                    204:        NM p;
                    205:        int i;
                    206:
1.11      noro      207:        for ( i = 0; i < 1024; i++ ) {
1.1       noro      208:                p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
                    209:                p->next = _nm_free_list; _nm_free_list = p;
                    210:        }
                    211: }
                    212:
                    213: void _ND_alloc()
                    214: {
                    215:        ND p;
                    216:        int i;
                    217:
                    218:        for ( i = 0; i < 1024; i++ ) {
                    219:                p = (ND)GC_malloc(sizeof(struct oND));
                    220:                p->body = (NM)_nd_free_list; _nd_free_list = p;
                    221:        }
                    222: }
                    223:
                    224: void _NDP_alloc()
                    225: {
                    226:        ND_pairs p;
                    227:        int i;
                    228:
1.11      noro      229:        for ( i = 0; i < 1024; i++ ) {
1.1       noro      230:                p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
                    231:                        +(nd_wpd-1)*sizeof(unsigned int));
                    232:                p->next = _ndp_free_list; _ndp_free_list = p;
                    233:        }
                    234: }
                    235:
                    236: INLINE nd_length(ND p)
                    237: {
                    238:        NM m;
                    239:        int i;
                    240:
                    241:        if ( !p )
                    242:                return 0;
                    243:        else {
                    244:                for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    245:                return i;
                    246:        }
                    247: }
                    248:
                    249: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2)
                    250: {
                    251:        unsigned int u1,u2;
                    252:        int i,j;
                    253:
                    254:        switch ( nd_bpe ) {
                    255:                case 4:
                    256:                        for ( i = 0; i < nd_wpd; i++ ) {
                    257:                                u1 = d1[i]; u2 = d2[i];
                    258:                                if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
                    259:                                if ( (u1&0xf000000) < (u2&0xf000000) ) return 0;
                    260:                                if ( (u1&0xf00000) < (u2&0xf00000) ) return 0;
                    261:                                if ( (u1&0xf0000) < (u2&0xf0000) ) return 0;
                    262:                                if ( (u1&0xf000) < (u2&0xf000) ) return 0;
                    263:                                if ( (u1&0xf00) < (u2&0xf00) ) return 0;
                    264:                                if ( (u1&0xf0) < (u2&0xf0) ) return 0;
                    265:                                if ( (u1&0xf) < (u2&0xf) ) return 0;
                    266:                        }
                    267:                        return 1;
                    268:                        break;
                    269:                case 6:
                    270:                        for ( i = 0; i < nd_wpd; i++ ) {
                    271:                                u1 = d1[i]; u2 = d2[i];
                    272:                                if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
                    273:                                if ( (u1&0xfc0000) < (u2&0xfc0000) ) return 0;
                    274:                                if ( (u1&0x3f000) < (u2&0x3f000) ) return 0;
                    275:                                if ( (u1&0xfc0) < (u2&0xfc0) ) return 0;
                    276:                                if ( (u1&0x3f) < (u2&0x3f) ) return 0;
                    277:                        }
                    278:                        return 1;
                    279:                        break;
                    280:                case 8:
                    281:                        for ( i = 0; i < nd_wpd; i++ ) {
                    282:                                u1 = d1[i]; u2 = d2[i];
                    283:                                if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
                    284:                                if ( (u1&0xff0000) < (u2&0xff0000) ) return 0;
                    285:                                if ( (u1&0xff00) < (u2&0xff00) ) return 0;
                    286:                                if ( (u1&0xff) < (u2&0xff) ) return 0;
                    287:                        }
                    288:                        return 1;
                    289:                        break;
                    290:                case 16:
                    291:                        for ( i = 0; i < nd_wpd; i++ ) {
                    292:                                u1 = d1[i]; u2 = d2[i];
                    293:                                if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
                    294:                                if ( (u1&0xffff) < (u2&0xffff) ) return 0;
                    295:                        }
                    296:                        return 1;
                    297:                        break;
                    298:                case 32:
                    299:                        for ( i = 0; i < nd_wpd; i++ )
                    300:                                if ( d1[i] < d2[i] ) return 0;
                    301:                        return 1;
                    302:                        break;
                    303:                default:
                    304:                        for ( i = 0; i < nd_wpd; i++ ) {
                    305:                                u1 = d1[i]; u2 = d2[i];
                    306:                                for ( j = 0; j < nd_epw; j++ )
                    307:                                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    308:                        }
                    309:                        return 1;
                    310:        }
                    311: }
                    312:
                    313: void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d)
                    314: {
                    315:        unsigned int t1,t2,u,u1,u2;
                    316:        int i,j;
                    317:
                    318:        switch ( nd_bpe ) {
                    319:                case 4:
                    320:                        for ( i = 0; i < nd_wpd; i++ ) {
                    321:                                u1 = d1[i]; u2 = d2[i];
                    322:                                t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
                    323:                                t1 = (u1&0xf000000); t2 = (u2&0xf000000); u |= t1>t2?t1:t2;
                    324:                                t1 = (u1&0xf00000); t2 = (u2&0xf00000); u |= t1>t2?t1:t2;
                    325:                                t1 = (u1&0xf0000); t2 = (u2&0xf0000); u |= t1>t2?t1:t2;
                    326:                                t1 = (u1&0xf000); t2 = (u2&0xf000); u |= t1>t2?t1:t2;
                    327:                                t1 = (u1&0xf00); t2 = (u2&0xf00); u |= t1>t2?t1:t2;
                    328:                                t1 = (u1&0xf0); t2 = (u2&0xf0); u |= t1>t2?t1:t2;
                    329:                                t1 = (u1&0xf); t2 = (u2&0xf); u |= t1>t2?t1:t2;
                    330:                                d[i] = u;
                    331:                        }
                    332:                        break;
                    333:                case 6:
                    334:                        for ( i = 0; i < nd_wpd; i++ ) {
                    335:                                u1 = d1[i]; u2 = d2[i];
                    336:                                t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
                    337:                                t1 = (u1&0xfc0000); t2 = (u2&0xfc0000); u |= t1>t2?t1:t2;
                    338:                                t1 = (u1&0x3f000); t2 = (u2&0x3f000); u |= t1>t2?t1:t2;
                    339:                                t1 = (u1&0xfc0); t2 = (u2&0xfc0); u |= t1>t2?t1:t2;
                    340:                                t1 = (u1&0x3f); t2 = (u2&0x3f); u |= t1>t2?t1:t2;
                    341:                                d[i] = u;
                    342:                        }
                    343:                        break;
                    344:                case 8:
                    345:                        for ( i = 0; i < nd_wpd; i++ ) {
                    346:                                u1 = d1[i]; u2 = d2[i];
                    347:                                t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
                    348:                                t1 = (u1&0xff0000); t2 = (u2&0xff0000); u |= t1>t2?t1:t2;
                    349:                                t1 = (u1&0xff00); t2 = (u2&0xff00); u |= t1>t2?t1:t2;
                    350:                                t1 = (u1&0xff); t2 = (u2&0xff); u |= t1>t2?t1:t2;
                    351:                                d[i] = u;
                    352:                        }
                    353:                        break;
                    354:                case 16:
                    355:                        for ( i = 0; i < nd_wpd; i++ ) {
                    356:                                u1 = d1[i]; u2 = d2[i];
                    357:                                t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
                    358:                                t1 = (u1&0xffff); t2 = (u2&0xffff); u |= t1>t2?t1:t2;
                    359:                                d[i] = u;
                    360:                        }
                    361:                        break;
                    362:                case 32:
                    363:                        for ( i = 0; i < nd_wpd; i++ ) {
                    364:                                u1 = d1[i]; u2 = d2[i];
                    365:                                d[i] = u1>u2?u1:u2;
                    366:                        }
                    367:                        break;
                    368:                default:
                    369:                        for ( i = 0; i < nd_wpd; i++ ) {
                    370:                                u1 = d1[i]; u2 = d2[i];
                    371:                                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    372:                                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    373:                                }
                    374:                                d[i] = u;
                    375:                        }
                    376:                        break;
                    377:        }
                    378: }
                    379:
                    380: int ndl_td(unsigned int *d)
                    381: {
                    382:        unsigned int t,u;
                    383:        int i,j;
                    384:
                    385:        for ( t = 0, i = 0; i < nd_wpd; i++ ) {
                    386:                u = d[i];
                    387:                for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    388:                        t += (u&nd_mask0);
                    389:        }
                    390:        return t;
                    391: }
                    392:
                    393: INLINE int ndl_compare(unsigned int *d1,unsigned int *d2)
                    394: {
                    395:        int i;
                    396:
                    397:        for ( i = 0; i < nd_wpd; i++, d1++, d2++ )
                    398:                if ( *d1 > *d2 )
                    399:                        return is_rlex ? -1 : 1;
                    400:                else if ( *d1 < *d2 )
                    401:                        return is_rlex ? 1 : -1;
                    402:        return 0;
                    403: }
                    404:
                    405: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2)
                    406: {
                    407:        int i;
                    408:
                    409:        for ( i = 0; i < nd_wpd; i++ )
                    410:                if ( d1[i] != d2[i] )
                    411:                        return 0;
                    412:        return 1;
                    413: }
                    414:
1.6       noro      415: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2)
                    416: {
                    417:        int i;
                    418:
                    419:        switch ( nd_wpd ) {
                    420:                case 1:
                    421:                        d2[0] = d1[0];
                    422:                        break;
                    423:                case 2:
                    424:                        d2[0] = d1[0];
                    425:                        d2[1] = d1[1];
                    426:                        break;
                    427:                default:
                    428:                        for ( i = 0; i < nd_wpd; i++ )
                    429:                                d2[i] = d1[i];
                    430:                        break;
                    431:        }
                    432: }
                    433:
1.1       noro      434: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d)
                    435: {
                    436:        int i;
                    437:
1.6       noro      438:        switch ( nd_wpd ) {
                    439:                case 1:
                    440:                        d[0] = d1[0]+d2[0];
                    441:                        break;
                    442:                case 2:
                    443:                        d[0] = d1[0]+d2[0];
                    444:                        d[1] = d1[1]+d2[1];
                    445:                        break;
                    446:                default:
                    447:                        for ( i = 0; i < nd_wpd; i++ )
                    448:                                d[i] = d1[i]+d2[i];
                    449:                        break;
                    450:        }
                    451: }
                    452:
                    453: INLINE void ndl_add2(unsigned int *d1,unsigned int *d2)
                    454: {
                    455:        int i;
                    456:
                    457:        switch ( nd_wpd ) {
                    458:                case 1:
                    459:                        d2[0] += d1[0];
                    460:                        break;
                    461:                case 2:
                    462:                        d2[0] += d1[0];
                    463:                        d2[1] += d1[1];
                    464:                        break;
                    465:                default:
                    466:                        for ( i = 0; i < nd_wpd; i++ )
                    467:                                d2[i] += d1[i];
                    468:                        break;
1.1       noro      469:        }
                    470: }
                    471:
                    472: void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d)
                    473: {
                    474:        int i;
                    475:
                    476:        for ( i = 0; i < nd_wpd; i++ )
                    477:                d[i] = d1[i]-d2[i];
                    478: }
                    479:
                    480: int ndl_disjoint(unsigned int *d1,unsigned int *d2)
                    481: {
                    482:        unsigned int t1,t2,u,u1,u2;
                    483:        int i,j;
                    484:
                    485:        switch ( nd_bpe ) {
                    486:                case 4:
                    487:                        for ( i = 0; i < nd_wpd; i++ ) {
                    488:                                u1 = d1[i]; u2 = d2[i];
                    489:                                t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
                    490:                                t1 = u1&0xf000000; t2 = u2&0xf000000; if ( t1&&t2 ) return 0;
                    491:                                t1 = u1&0xf00000; t2 = u2&0xf00000; if ( t1&&t2 ) return 0;
                    492:                                t1 = u1&0xf0000; t2 = u2&0xf0000; if ( t1&&t2 ) return 0;
                    493:                                t1 = u1&0xf000; t2 = u2&0xf000; if ( t1&&t2 ) return 0;
                    494:                                t1 = u1&0xf00; t2 = u2&0xf00; if ( t1&&t2 ) return 0;
                    495:                                t1 = u1&0xf0; t2 = u2&0xf0; if ( t1&&t2 ) return 0;
                    496:                                t1 = u1&0xf; t2 = u2&0xf; if ( t1&&t2 ) return 0;
                    497:                        }
                    498:                        return 1;
                    499:                        break;
                    500:                case 6:
                    501:                        for ( i = 0; i < nd_wpd; i++ ) {
                    502:                                u1 = d1[i]; u2 = d2[i];
                    503:                                t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
                    504:                                t1 = u1&0xfc0000; t2 = u2&0xfc0000; if ( t1&&t2 ) return 0;
                    505:                                t1 = u1&0x3f000; t2 = u2&0x3f000; if ( t1&&t2 ) return 0;
                    506:                                t1 = u1&0xfc0; t2 = u2&0xfc0; if ( t1&&t2 ) return 0;
                    507:                                t1 = u1&0x3f; t2 = u2&0x3f; if ( t1&&t2 ) return 0;
                    508:                        }
                    509:                        return 1;
                    510:                        break;
                    511:                case 8:
                    512:                        for ( i = 0; i < nd_wpd; i++ ) {
                    513:                                u1 = d1[i]; u2 = d2[i];
                    514:                                t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
                    515:                                t1 = u1&0xff0000; t2 = u2&0xff0000; if ( t1&&t2 ) return 0;
                    516:                                t1 = u1&0xff00; t2 = u2&0xff00; if ( t1&&t2 ) return 0;
                    517:                                t1 = u1&0xff; t2 = u2&0xff; if ( t1&&t2 ) return 0;
                    518:                        }
                    519:                        return 1;
                    520:                        break;
                    521:                case 16:
                    522:                        for ( i = 0; i < nd_wpd; i++ ) {
                    523:                                u1 = d1[i]; u2 = d2[i];
                    524:                                t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
                    525:                                t1 = u1&0xffff; t2 = u2&0xffff; if ( t1&&t2 ) return 0;
                    526:                        }
                    527:                        return 1;
                    528:                        break;
                    529:                case 32:
                    530:                        for ( i = 0; i < nd_wpd; i++ )
                    531:                                if ( d1[i] && d2[i] ) return 0;
                    532:                        return 1;
                    533:                        break;
                    534:                default:
                    535:                        for ( i = 0; i < nd_wpd; i++ ) {
                    536:                                u1 = d1[i]; u2 = d2[i];
                    537:                                for ( j = 0; j < nd_epw; j++ ) {
                    538:                                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    539:                                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    540:                                }
                    541:                        }
                    542:                        return 1;
                    543:                        break;
                    544:        }
                    545: }
                    546:
1.5       noro      547: int ndl_check_bound2(int index,unsigned int *d2)
1.1       noro      548: {
1.5       noro      549:        unsigned int u2;
                    550:        unsigned int *d1;
                    551:        int i,j,ind,k;
1.1       noro      552:
1.5       noro      553:        d1 = nd_bound[index];
                    554:        ind = 0;
                    555:        switch ( nd_bpe ) {
                    556:                case 4:
                    557:                        for ( i = 0; i < nd_wpd; i++ ) {
                    558:                                u2 = d2[i];
                    559:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    560:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    561:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    562:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    563:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    564:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    565:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    566:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    567:                        }
                    568:                        return 0;
                    569:                        break;
                    570:                case 6:
                    571:                        for ( i = 0; i < nd_wpd; i++ ) {
                    572:                                u2 = d2[i];
                    573:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    574:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    575:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    576:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    577:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    578:                        }
                    579:                        return 0;
                    580:                        break;
                    581:                case 8:
                    582:                        for ( i = 0; i < nd_wpd; i++ ) {
                    583:                                u2 = d2[i];
                    584:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    585:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    586:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    587:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    588:                        }
                    589:                        return 0;
                    590:                        break;
                    591:                case 16:
                    592:                        for ( i = 0; i < nd_wpd; i++ ) {
                    593:                                u2 = d2[i];
                    594:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    595:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    596:                        }
                    597:                        return 0;
                    598:                        break;
                    599:                case 32:
                    600:                        for ( i = 0; i < nd_wpd; i++ )
                    601:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                    602:                        return 0;
                    603:                        break;
                    604:                default:
                    605:                        for ( i = 0; i < nd_wpd; i++ ) {
                    606:                                u2 = d2[i];
                    607:                                k = (nd_epw-1)*nd_bpe;
                    608:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                    609:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                    610:                        }
                    611:                        return 0;
                    612:                        break;
                    613:        }
1.1       noro      614: }
                    615:
1.6       noro      616: INLINE int ndl_hash_value(int td,unsigned int *d)
1.1       noro      617: {
                    618:        int i;
                    619:        int r;
                    620:
                    621:        r = td;
                    622:        for ( i = 0; i < nd_wpd; i++ )
                    623:                r = ((r<<16)+d[i])%REDTAB_LEN;
                    624:        return r;
                    625: }
                    626:
1.9       noro      627: INLINE int nd_find_reducer(ND g)
1.1       noro      628: {
1.13      noro      629:        RHist r;
1.6       noro      630:        int d,k,i;
1.1       noro      631:
                    632:        d = ndl_hash_value(HTD(g),HDL(g));
1.13      noro      633:        for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.14      noro      634:                if ( HTD(g) == TD(r) && ndl_equal(HDL(g),DL(r)) ) {
1.1       noro      635:                        if ( k > 0 ) nd_notfirst++;
                    636:                        nd_found++;
1.13      noro      637:                        return r->index;
1.1       noro      638:                }
                    639:        }
                    640:
1.13      noro      641:        if ( Reverse )
                    642:                for ( i = nd_psn-1; i >= 0; i-- ) {
                    643:                        r = nd_psh[i];
1.14      noro      644:                        if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13      noro      645:                                nd_create++;
                    646:                                nd_append_red(HDL(g),HTD(g),i);
                    647:                                return i;
                    648:                        }
                    649:                }
                    650:        else
                    651:                for ( i = 0; i < nd_psn; i++ ) {
                    652:                        r = nd_psh[i];
1.14      noro      653:                        if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13      noro      654:                                nd_create++;
                    655:                                nd_append_red(HDL(g),HTD(g),i);
                    656:                                return i;
                    657:                        }
1.1       noro      658:                }
1.6       noro      659:        return -1;
1.1       noro      660: }
                    661:
1.19    ! noro      662: ND nd_add(int mod,ND p1,ND p2)
1.1       noro      663: {
                    664:        int n,c;
                    665:        int t;
                    666:        ND r;
                    667:        NM m1,m2,mr0,mr,s;
                    668:
                    669:        if ( !p1 )
                    670:                return p2;
                    671:        else if ( !p2 )
                    672:                return p1;
                    673:        else {
                    674:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.14      noro      675:                        if ( TD(m1) > TD(m2) )
1.1       noro      676:                                c = 1;
1.14      noro      677:                        else if ( TD(m1) < TD(m2) )
1.1       noro      678:                                c = -1;
                    679:                        else
1.14      noro      680:                                c = ndl_compare(DL(m1),DL(m2));
1.1       noro      681:                        switch ( c ) {
                    682:                                case 0:
1.19    ! noro      683:                                        t = ((CM(m1))+(CM(m2))) - mod;
1.1       noro      684:                                        if ( t < 0 )
1.19    ! noro      685:                                                t += mod;
1.1       noro      686:                                        s = m1; m1 = NEXT(m1);
                    687:                                        if ( t ) {
1.14      noro      688:                                                NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1       noro      689:                                        } else {
                    690:                                                FREENM(s);
                    691:                                        }
                    692:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                    693:                                        break;
                    694:                                case 1:
                    695:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    696:                                        break;
                    697:                                case -1:
                    698:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    699:                                        break;
                    700:                        }
                    701:                }
                    702:                if ( !mr0 )
                    703:                        if ( m1 )
                    704:                                mr0 = m1;
                    705:                        else if ( m2 )
                    706:                                mr0 = m2;
                    707:                        else
                    708:                                return 0;
                    709:                else if ( m1 )
                    710:                        NEXT(mr) = m1;
                    711:                else if ( m2 )
                    712:                        NEXT(mr) = m2;
                    713:                else
                    714:                        NEXT(mr) = 0;
                    715:                BDY(p1) = mr0;
1.14      noro      716:                SG(p1) = MAX(SG(p1),SG(p2));
1.1       noro      717:                FREEND(p2);
                    718:                return p1;
                    719:        }
                    720: }
                    721:
1.17      noro      722: ND nd_add_q(ND p1,ND p2)
                    723: {
                    724:        int n,c;
                    725:        ND r;
                    726:        NM m1,m2,mr0,mr,s;
                    727:        Q t;
                    728:
                    729:        if ( !p1 )
                    730:                return p2;
                    731:        else if ( !p2 )
                    732:                return p1;
                    733:        else {
                    734:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                    735:                        if ( TD(m1) > TD(m2) )
                    736:                                c = 1;
                    737:                        else if ( TD(m1) < TD(m2) )
                    738:                                c = -1;
                    739:                        else
                    740:                                c = ndl_compare(DL(m1),DL(m2));
                    741:                        switch ( c ) {
                    742:                                case 0:
                    743:                                        addq(CQ(m1),CQ(m2),&t);
                    744:                                        s = m1; m1 = NEXT(m1);
                    745:                                        if ( t ) {
                    746:                                                NEXTNM2(mr0,mr,s); CQ(mr) = (t);
                    747:                                        } else {
                    748:                                                FREENM(s);
                    749:                                        }
                    750:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                    751:                                        break;
                    752:                                case 1:
                    753:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                    754:                                        break;
                    755:                                case -1:
                    756:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                    757:                                        break;
                    758:                        }
                    759:                }
                    760:                if ( !mr0 )
                    761:                        if ( m1 )
                    762:                                mr0 = m1;
                    763:                        else if ( m2 )
                    764:                                mr0 = m2;
                    765:                        else
                    766:                                return 0;
                    767:                else if ( m1 )
                    768:                        NEXT(mr) = m1;
                    769:                else if ( m2 )
                    770:                        NEXT(mr) = m2;
                    771:                else
                    772:                        NEXT(mr) = 0;
                    773:                BDY(p1) = mr0;
                    774:                SG(p1) = MAX(SG(p1),SG(p2));
                    775:                FREEND(p2);
                    776:                return p1;
                    777:        }
                    778: }
                    779:
1.1       noro      780: #if 1
                    781: /* ret=1 : success, ret=0 : overflow */
1.16      noro      782: int nd_nf(int mod,ND g,int full,ND *rp)
1.1       noro      783: {
1.11      noro      784:        ND d;
1.1       noro      785:        NM m,mrd,tail;
1.7       noro      786:        NM mul;
1.10      noro      787:        int n,sugar,psugar,sugar0,stat,index;
1.6       noro      788:        int c,c1,c2;
1.17      noro      789:        RHist h;
1.11      noro      790:        NDV p,red;
1.16      noro      791:        Q cg,cred,gcd;
1.1       noro      792:
                    793:        if ( !g ) {
                    794:                *rp = 0;
                    795:                return 1;
                    796:        }
1.14      noro      797:        sugar0 = sugar = SG(g);
1.1       noro      798:        n = NV(g);
1.7       noro      799:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.1       noro      800:        for ( d = 0; g; ) {
1.6       noro      801:                index = nd_find_reducer(g);
                    802:                if ( index >= 0 ) {
1.17      noro      803:                        h = nd_psh[index];
                    804:                        ndl_sub(HDL(g),DL(h),DL(mul));
                    805:                        TD(mul) = HTD(g)-TD(h);
1.10      noro      806: #if 0
1.14      noro      807:                        if ( d && (SG(p)+TD(mul)) > sugar ) {
1.10      noro      808:                                goto afo;
                    809:                        }
                    810: #endif
1.14      noro      811:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.6       noro      812:                                nd_free(g); nd_free(d);
                    813:                                return 0;
                    814:                        }
1.16      noro      815:                        if ( mod ) {
1.17      noro      816:                                p = nd_ps[index];
1.19    ! noro      817:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
        !           818:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.17      noro      819:                                ndv_mul_nm(mod,p,mul,ndv_red);
1.19    ! noro      820:                                g = ndv_add(mod,g,ndv_red);
1.16      noro      821:                        } else {
1.17      noro      822:                                p = nd_ps[index];
                    823:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1.16      noro      824:                                chsgnq(cg,&CQ(mul));
                    825:                                nd_mul_c_q(d,cred);
                    826:                                nd_mul_c_q(g,cred);
1.17      noro      827:                                ndv_mul_nm(mod,p,mul,ndv_red);
1.16      noro      828:                                g = ndv_add_q(g,ndv_red);
                    829:                        }
1.14      noro      830:                        sugar = MAX(sugar,SG(ndv_red));
1.1       noro      831:                } else if ( !full ) {
                    832:                        *rp = g;
                    833:                        return 1;
                    834:                } else {
1.10      noro      835: afo:
1.1       noro      836:                        m = BDY(g);
                    837:                        if ( NEXT(m) ) {
                    838:                                BDY(g) = NEXT(m); NEXT(m) = 0;
                    839:                        } else {
                    840:                                FREEND(g); g = 0;
                    841:                        }
                    842:                        if ( d ) {
                    843:                                NEXT(tail)=m;
                    844:                                tail=m;
                    845:                        } else {
                    846:                                MKND(n,m,d);
                    847:                                tail = BDY(d);
                    848:                        }
                    849:                }
                    850:        }
                    851:        if ( d )
1.14      noro      852:                SG(d) = sugar;
1.1       noro      853:        *rp = d;
                    854:        return 1;
                    855: }
                    856: #else
                    857:
                    858: ND nd_remove_head(ND p)
                    859: {
                    860:        NM m;
                    861:
                    862:        m = BDY(p);
                    863:        if ( !NEXT(m) ) {
                    864:                FREEND(p);
                    865:                p = 0;
                    866:        } else
                    867:                BDY(p) = NEXT(m);
                    868:        FREENM(m);
                    869:        return p;
                    870: }
                    871:
                    872: PGeoBucket create_pbucket()
                    873: {
                    874:     PGeoBucket g;
                    875:
                    876:        g = CALLOC(1,sizeof(struct oPGeoBucket));
                    877:        g->m = -1;
                    878:        return g;
                    879: }
                    880:
1.19    ! noro      881: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro      882: {
                    883:        int l,k,m;
                    884:
                    885:        l = nd_length(d);
                    886:        for ( k = 0, m = 1; l > m; k++, m <<= 2 );
                    887:        /* 4^(k-1) < l <= 4^k */
1.19    ! noro      888:        d = nd_add(mod,g->body[k],d);
1.1       noro      889:        for ( ; d && nd_length(d) > 1<<(2*k); k++ ) {
                    890:                g->body[k] = 0;
1.19    ! noro      891:                d = nd_add(int mod,g->body[k+1],d);
1.1       noro      892:        }
                    893:        g->body[k] = d;
                    894:        g->m = MAX(g->m,k);
                    895: }
                    896:
1.19    ! noro      897: int head_pbucket(int mod,PGeoBucket g)
1.1       noro      898: {
                    899:        int j,i,c,k,nv,sum;
                    900:        unsigned int *di,*dj;
                    901:        ND gi,gj;
                    902:
                    903:        k = g->m;
                    904:        while ( 1 ) {
                    905:                j = -1;
                    906:                for ( i = 0; i <= k; i++ ) {
                    907:                        if ( !(gi = g->body[i]) )
                    908:                                continue;
                    909:                        if ( j < 0 ) {
                    910:                                j = i;
                    911:                                gj = g->body[j];
                    912:                                dj = HDL(gj);
1.14      noro      913:                                sum = HCM(gj);
1.1       noro      914:                        } else {
                    915:                                di = HDL(gi);
                    916:                                nv = NV(gi);
                    917:                                if ( HTD(gi) > HTD(gj) )
                    918:                                        c = 1;
                    919:                                else if ( HTD(gi) < HTD(gj) )
                    920:                                        c = -1;
                    921:                                else
                    922:                                        c = ndl_compare(di,dj);
                    923:                                if ( c > 0 ) {
                    924:                                        if ( sum )
1.14      noro      925:                                                HCM(gj) = sum;
1.1       noro      926:                                        else
                    927:                                                g->body[j] = nd_remove_head(gj);
                    928:                                        j = i;
                    929:                                        gj = g->body[j];
                    930:                                        dj = HDL(gj);
1.14      noro      931:                                        sum = HCM(gj);
1.1       noro      932:                                } else if ( c == 0 ) {
1.19    ! noro      933:                                        sum = sum+HCM(gi)-mod;
1.1       noro      934:                                        if ( sum < 0 )
1.19    ! noro      935:                                                sum += mod;
1.1       noro      936:                                        g->body[i] = nd_remove_head(gi);
                    937:                                }
                    938:                        }
                    939:                }
                    940:                if ( j < 0 )
                    941:                        return -1;
                    942:                else if ( sum ) {
1.14      noro      943:                        HCM(gj) = sum;
1.1       noro      944:                        return j;
                    945:                } else
                    946:                        g->body[j] = nd_remove_head(gj);
                    947:        }
                    948: }
                    949:
                    950: ND normalize_pbucket(PGeoBucket g)
                    951: {
                    952:        int i;
                    953:        ND r,t;
                    954:
                    955:        r = 0;
                    956:        for ( i = 0; i <= g->m; i++ )
1.19    ! noro      957:                r = nd_add(mod,r,g->body[i]);
1.1       noro      958:        return r;
                    959: }
                    960:
                    961: ND nd_nf(ND g,int full)
                    962: {
                    963:        ND u,p,d,red;
                    964:        NODE l;
                    965:        NM m,mrd;
                    966:        int sugar,psugar,n,h_reducible,h;
                    967:        PGeoBucket bucket;
                    968:
                    969:        if ( !g ) {
                    970:                return 0;
                    971:        }
1.14      noro      972:        sugar = SG(g);
                    973:        n = NV(g);
1.1       noro      974:        bucket = create_pbucket();
                    975:        add_pbucket(bucket,g);
                    976:        d = 0;
                    977:        while ( 1 ) {
                    978:                h = head_pbucket(bucket);
                    979:                if ( h < 0 ) {
                    980:                        if ( d )
1.14      noro      981:                                SG(d) = sugar;
1.1       noro      982:                        return d;
                    983:                }
                    984:                g = bucket->body[h];
                    985:                red = nd_find_reducer(g);
                    986:                if ( red ) {
                    987:                        bucket->body[h] = nd_remove_head(g);
                    988:                        red = nd_remove_head(red);
                    989:                        add_pbucket(bucket,red);
1.14      noro      990:                        sugar = MAX(sugar,SG(red));
1.1       noro      991:                } else if ( !full ) {
                    992:                        g = normalize_pbucket(bucket);
                    993:                        if ( g )
1.14      noro      994:                                SG(g) = sugar;
1.1       noro      995:                        return g;
                    996:                } else {
                    997:                        m = BDY(g);
                    998:                        if ( NEXT(m) ) {
                    999:                                BDY(g) = NEXT(m); NEXT(m) = 0;
                   1000:                        } else {
                   1001:                                FREEND(g); g = 0;
                   1002:                        }
                   1003:                        bucket->body[h] = g;
                   1004:                        NEXT(m) = 0;
                   1005:                        if ( d ) {
                   1006:                                for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
                   1007:                                NEXT(mrd) = m;
                   1008:                        } else {
                   1009:                                MKND(n,m,d);
                   1010:                        }
                   1011:                }
                   1012:        }
                   1013: }
                   1014: #endif
                   1015:
1.16      noro     1016: NODE nd_gb(int m,NODE f)
1.1       noro     1017: {
                   1018:        int i,nh,sugar,stat;
                   1019:        NODE r,g,gall;
                   1020:        ND_pairs d;
                   1021:        ND_pairs l;
                   1022:        ND h,nf;
                   1023:
                   1024:        for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
                   1025:                i = (int)BDY(r);
                   1026:                d = update_pairs(d,g,i);
                   1027:                g = update_base(g,i);
                   1028:                gall = append_one(gall,i);
                   1029:        }
                   1030:        sugar = 0;
                   1031:        while ( d ) {
                   1032: again:
                   1033:                l = nd_minp(d,&d);
1.14      noro     1034:                if ( SG(l) != sugar ) {
                   1035:                        sugar = SG(l);
1.1       noro     1036:                        fprintf(asir_out,"%d",sugar);
                   1037:                }
1.16      noro     1038:                stat = nd_sp(m,l,&h);
1.1       noro     1039:                if ( !stat ) {
                   1040:                        NEXT(l) = d; d = l;
                   1041:                        d = nd_reconstruct(d);
                   1042:                        goto again;
                   1043:                }
1.16      noro     1044:                stat = nd_nf(m,h,!Top,&nf);
1.1       noro     1045:                if ( !stat ) {
                   1046:                        NEXT(l) = d; d = l;
                   1047:                        d = nd_reconstruct(d);
                   1048:                        goto again;
                   1049:                } else if ( nf ) {
                   1050:                        printf("+"); fflush(stdout);
1.16      noro     1051:                        nh = nd_newps(m,nf);
1.1       noro     1052:                        d = update_pairs(d,g,nh);
                   1053:                        g = update_base(g,nh);
                   1054:                        gall = append_one(gall,nh);
                   1055:                        FREENDP(l);
                   1056:                } else {
                   1057:                        printf("."); fflush(stdout);
                   1058:                        FREENDP(l);
                   1059:                }
                   1060:        }
                   1061:        return g;
                   1062: }
                   1063:
                   1064: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   1065: {
                   1066:        ND_pairs d1,nd,cur,head,prev,remove;
                   1067:
                   1068:        if ( !g ) return d;
                   1069:        d = crit_B(d,t);
                   1070:        d1 = nd_newpairs(g,t);
                   1071:        d1 = crit_M(d1);
                   1072:        d1 = crit_F(d1);
                   1073:        prev = 0; cur = head = d1;
                   1074:        while ( cur ) {
                   1075:                if ( crit_2( cur->i1,cur->i2 ) ) {
                   1076:                        remove = cur;
                   1077:                        if ( !prev ) {
                   1078:                                head = cur = NEXT(cur);
                   1079:                        } else {
                   1080:                                cur = NEXT(prev) = NEXT(cur);
                   1081:                        }
                   1082:                        FREENDP(remove);
                   1083:                } else {
                   1084:                        prev = cur;
                   1085:                        cur = NEXT(cur);
                   1086:                }
                   1087:        }
                   1088:        if ( !d )
                   1089:                return head;
                   1090:        else {
                   1091:                nd = d;
                   1092:                while ( NEXT(nd) )
                   1093:                        nd = NEXT(nd);
                   1094:                NEXT(nd) = head;
                   1095:                return d;
                   1096:        }
                   1097: }
                   1098:
                   1099: ND_pairs nd_newpairs( NODE g, int t )
                   1100: {
                   1101:        NODE h;
                   1102:        unsigned int *dl;
                   1103:        int td,ts,s;
                   1104:        ND_pairs r,r0;
                   1105:
                   1106:        dl = HDL(nd_ps[t]);
                   1107:        td = HTD(nd_ps[t]);
1.14      noro     1108:        ts = SG(nd_ps[t]) - td;
1.1       noro     1109:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   1110:                NEXTND_pairs(r0,r);
                   1111:                r->i1 = (int)BDY(h);
                   1112:                r->i2 = t;
                   1113:                ndl_lcm(HDL(nd_ps[r->i1]),dl,r->lcm);
1.14      noro     1114:                TD(r) = ndl_td(r->lcm);
                   1115:                s = SG(nd_ps[r->i1])-HTD(nd_ps[r->i1]);
                   1116:                SG(r) = MAX(s,ts) + TD(r);
1.1       noro     1117:        }
                   1118:        NEXT(r) = 0;
                   1119:        return r0;
                   1120: }
                   1121:
                   1122: ND_pairs crit_B( ND_pairs d, int s )
                   1123: {
                   1124:        ND_pairs cur,head,prev,remove;
                   1125:        unsigned int *t,*tl,*lcm;
                   1126:        int td,tdl;
                   1127:
                   1128:        if ( !d ) return 0;
                   1129:        t = HDL(nd_ps[s]);
                   1130:        prev = 0;
                   1131:        head = cur = d;
                   1132:        lcm = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
                   1133:        while ( cur ) {
                   1134:                tl = cur->lcm;
                   1135:                if ( ndl_reducible(tl,t)
                   1136:                        && (ndl_lcm(HDL(nd_ps[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   1137:                        && (ndl_lcm(HDL(nd_ps[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
                   1138:                        remove = cur;
                   1139:                        if ( !prev ) {
                   1140:                                head = cur = NEXT(cur);
                   1141:                        } else {
                   1142:                                cur = NEXT(prev) = NEXT(cur);
                   1143:                        }
                   1144:                        FREENDP(remove);
                   1145:                } else {
                   1146:                        prev = cur;
                   1147:                        cur = NEXT(cur);
                   1148:                }
                   1149:        }
                   1150:        return head;
                   1151: }
                   1152:
                   1153: ND_pairs crit_M( ND_pairs d1 )
                   1154: {
                   1155:        ND_pairs e,d2,d3,dd,p;
                   1156:        unsigned int *id,*jd;
                   1157:        int itd,jtd;
                   1158:
                   1159:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1160:                if ( !(d2 = NEXT(e)) ) {
                   1161:                        NEXT(e) = dd;
                   1162:                        return e;
                   1163:                }
                   1164:                id = e->lcm;
1.14      noro     1165:                itd = TD(e);
1.1       noro     1166:                for ( d3 = 0; d2; d2 = p ) {
                   1167:                        p = NEXT(d2),
                   1168:                        jd = d2->lcm;
1.14      noro     1169:                        jtd = TD(d2);
1.1       noro     1170:                        if ( jtd == itd  )
                   1171:                                if ( id == jd );
                   1172:                                else if ( ndl_reducible(jd,id) ) continue;
                   1173:                                else if ( ndl_reducible(id,jd) ) goto delit;
                   1174:                                else ;
                   1175:                        else if ( jtd > itd )
                   1176:                                if ( ndl_reducible(jd,id) ) continue;
                   1177:                                else ;
                   1178:                        else if ( ndl_reducible(id,jd ) ) goto delit;
                   1179:                        NEXT(d2) = d3;
                   1180:                        d3 = d2;
                   1181:                }
                   1182:                NEXT(e) = dd;
                   1183:                dd = e;
                   1184:                continue;
                   1185:                /**/
                   1186:        delit:  NEXT(d2) = d3;
                   1187:                d3 = d2;
                   1188:                for ( ; p; p = d2 ) {
                   1189:                        d2 = NEXT(p);
                   1190:                        NEXT(p) = d3;
                   1191:                        d3 = p;
                   1192:                }
                   1193:                FREENDP(e);
                   1194:        }
                   1195:        return dd;
                   1196: }
                   1197:
                   1198: ND_pairs crit_F( ND_pairs d1 )
                   1199: {
                   1200:        ND_pairs rest, head,remove;
                   1201:        ND_pairs last, p, r, w;
                   1202:        int s;
                   1203:
                   1204:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   1205:                r = w = equivalent_pairs(p,&rest);
1.14      noro     1206:                s = SG(r);
1.1       noro     1207:                w = NEXT(w);
                   1208:                while ( w ) {
                   1209:                        if ( crit_2(w->i1,w->i2) ) {
                   1210:                                r = w;
                   1211:                                w = NEXT(w);
                   1212:                                while ( w ) {
                   1213:                                        remove = w;
                   1214:                                        w = NEXT(w);
                   1215:                                        FREENDP(remove);
                   1216:                                }
                   1217:                                break;
1.14      noro     1218:                        } else if ( SG(w) < s ) {
1.1       noro     1219:                                FREENDP(r);
                   1220:                                r = w;
1.14      noro     1221:                                s = SG(r);
1.1       noro     1222:                                w = NEXT(w);
                   1223:                        } else {
                   1224:                                remove = w;
                   1225:                                w = NEXT(w);
                   1226:                                FREENDP(remove);
                   1227:                        }
                   1228:                }
                   1229:                if ( last ) NEXT(last) = r;
                   1230:                else head = r;
                   1231:                NEXT(last = r) = 0;
                   1232:                p = rest;
                   1233:                if ( !p ) return head;
                   1234:        }
                   1235:        if ( !last ) return p;
                   1236:        NEXT(last) = p;
                   1237:        return head;
                   1238: }
                   1239:
                   1240: int crit_2( int dp1, int dp2 )
                   1241: {
                   1242:        return ndl_disjoint(HDL(nd_ps[dp1]),HDL(nd_ps[dp2]));
                   1243: }
                   1244:
                   1245: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
                   1246: {
                   1247:        ND_pairs w,p,r,s;
                   1248:        unsigned int *d;
                   1249:        int td;
                   1250:
                   1251:        w = d1;
                   1252:        d = w->lcm;
1.14      noro     1253:        td = TD(w);
1.1       noro     1254:        s = NEXT(w);
                   1255:        NEXT(w) = 0;
                   1256:        for ( r = 0; s; s = p ) {
                   1257:                p = NEXT(s);
1.14      noro     1258:                if ( td == TD(s) && ndl_equal(d,s->lcm) ) {
1.1       noro     1259:                        NEXT(s) = w;
                   1260:                        w = s;
                   1261:                } else {
                   1262:                        NEXT(s) = r;
                   1263:                        r = s;
                   1264:                }
                   1265:        }
                   1266:        *prest = r;
                   1267:        return w;
                   1268: }
                   1269:
                   1270: NODE update_base(NODE nd,int ndp)
                   1271: {
                   1272:        unsigned int *dl, *dln;
                   1273:        NODE last, p, head;
                   1274:        int td,tdn;
                   1275:
                   1276:        dl = HDL(nd_ps[ndp]);
                   1277:        td = HTD(nd_ps[ndp]);
                   1278:        for ( head = last = 0, p = nd; p; ) {
                   1279:                dln = HDL(nd_ps[(int)BDY(p)]);
                   1280:                tdn = HTD(nd_ps[(int)BDY(p)]);
                   1281:                if ( tdn >= td && ndl_reducible( dln, dl ) ) {
                   1282:                        p = NEXT(p);
                   1283:                        if ( last ) NEXT(last) = p;
                   1284:                } else {
                   1285:                        if ( !last ) head = p;
                   1286:                        p = NEXT(last = p);
                   1287:                }
                   1288:        }
                   1289:        head = append_one(head,ndp);
                   1290:        return head;
                   1291: }
                   1292:
                   1293: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   1294: {
                   1295:        ND_pairs m,ml,p,l;
                   1296:        unsigned int *lcm;
                   1297:        int s,td,len,tlen,c;
                   1298:
                   1299:        if ( !(p = NEXT(m = d)) ) {
                   1300:                *prest = p;
                   1301:                NEXT(m) = 0;
                   1302:                return m;
                   1303:        }
                   1304:        lcm = m->lcm;
1.14      noro     1305:        s = SG(m);
                   1306:        td = TD(m);
1.6       noro     1307:        len = nd_psl[m->i1]+nd_psl[m->i2];
1.1       noro     1308:        for ( ml = 0, l = m; p; p = NEXT(l = p) ) {
1.14      noro     1309:                if (SG(p) < s)
1.1       noro     1310:                        goto find;
1.14      noro     1311:                else if ( SG(p) == s ) {
                   1312:                        if ( TD(p) < td )
1.1       noro     1313:                                goto find;
1.14      noro     1314:                        else if ( TD(p) == td ) {
1.1       noro     1315:                                c = ndl_compare(p->lcm,lcm);
                   1316:                                if ( c < 0 )
                   1317:                                        goto find;
1.10      noro     1318: #if 0
1.1       noro     1319:                                else if ( c == 0 ) {
1.6       noro     1320:                                        tlen = nd_psl[p->i1]+nd_psl[p->i2];
1.1       noro     1321:                                        if ( tlen < len )
                   1322:                                                goto find;
                   1323:                                }
1.10      noro     1324: #endif
1.1       noro     1325:                        }
                   1326:                }
                   1327:                continue;
                   1328: find:
                   1329:                ml = l;
                   1330:                m = p;
                   1331:                lcm = m->lcm;
1.14      noro     1332:                s = SG(m);
                   1333:                td = TD(m);
1.1       noro     1334:                len = tlen;
                   1335:        }
                   1336:        if ( !ml ) *prest = NEXT(m);
                   1337:        else {
                   1338:                NEXT(ml) = NEXT(m);
                   1339:                *prest = d;
                   1340:        }
                   1341:        NEXT(m) = 0;
                   1342:        return m;
                   1343: }
                   1344:
1.16      noro     1345: int nd_newps(int mod,ND a)
1.1       noro     1346: {
1.3       noro     1347:        int len;
1.13      noro     1348:        RHist r;
1.3       noro     1349:
1.1       noro     1350:        if ( nd_psn == nd_pslen ) {
                   1351:                nd_pslen *= 2;
1.6       noro     1352:                nd_psl = (int *)REALLOC((char *)nd_psl,nd_pslen*sizeof(int));
1.11      noro     1353:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.13      noro     1354:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.1       noro     1355:                nd_bound = (unsigned int **)
                   1356:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
                   1357:        }
1.16      noro     1358:        if ( mod )
1.19    ! noro     1359:                nd_monic(mod,a);
1.16      noro     1360:        else
                   1361:                nd_removecont(a);
1.1       noro     1362:        nd_bound[nd_psn] = nd_compute_bound(a);
1.14      noro     1363:        NEWRHist(r); TD(r) = HTD(a); ndl_copy(HDL(a),DL(r)); nd_psh[nd_psn] = r;
1.16      noro     1364:        nd_ps[nd_psn]= ndtondv(mod,a);
1.13      noro     1365:        nd_free(a);
1.14      noro     1366:        nd_psl[nd_psn] = len = LEN(nd_ps[nd_psn]);
1.3       noro     1367:        if ( len > nmv_len ) {
                   1368:                nmv_len = 2*len;
                   1369:                BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv);
                   1370:        }
1.1       noro     1371:        return nd_psn++;
                   1372: }
                   1373:
1.16      noro     1374: NODE nd_setup(int mod,NODE f)
1.1       noro     1375: {
1.5       noro     1376:        int i,j,td,len,max;
1.1       noro     1377:        NODE s,s0,f0;
1.5       noro     1378:        unsigned int *d;
1.13      noro     1379:        RHist r;
1.11      noro     1380:
                   1381:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     1382:
                   1383:        nd_psn = length(f); nd_pslen = 2*nd_psn;
1.6       noro     1384:        nd_psl = (int *)MALLOC(nd_pslen*sizeof(int));
1.11      noro     1385:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     1386:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.1       noro     1387:        nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
1.5       noro     1388:        for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
                   1389:                nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
                   1390:                for ( j = 0; j < nd_nvar; j++ )
                   1391:                        max = MAX(d[j],max);
                   1392:        }
1.11      noro     1393:        if ( !nd_red )
1.13      noro     1394:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   1395:        bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.5       noro     1396:
                   1397:        if ( max < 2 )
                   1398:                nd_bpe = 2;
                   1399:        else if ( max < 4 )
                   1400:                nd_bpe = 4;
                   1401:        else if ( max < 64 )
                   1402:                nd_bpe = 6;
                   1403:        else if ( max < 256 )
                   1404:                nd_bpe = 8;
                   1405:        else if ( max < 65536 )
                   1406:                nd_bpe = 16;
                   1407:        else
                   1408:                nd_bpe = 32;
1.13      noro     1409:
1.1       noro     1410:        nd_setup_parameters();
                   1411:        nd_free_private_storage();
1.3       noro     1412:        len = 0;
1.1       noro     1413:        for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.16      noro     1414:                nd_ps[i] = dptondv(mod,(DP)BDY(f));
                   1415:                if ( mod )
1.19    ! noro     1416:                        ndv_mul_c(mod,nd_ps[i],invm(HCM(nd_ps[i]),mod));
1.16      noro     1417:                else
                   1418:                        ndv_removecont(nd_ps[i]);
1.14      noro     1419:                len = MAX(len,LEN(nd_ps[i]));
                   1420:                NEWRHist(r); TD(r) = HTD(nd_ps[i]); ndl_copy(HDL(nd_ps[i]),DL(r));
1.13      noro     1421:                nd_psh[i] = r;
1.1       noro     1422:        }
1.3       noro     1423:        nmv_len = 16*len;
                   1424:        NEWNDV(ndv_red);
1.16      noro     1425:        if ( mod )
                   1426:                BDY(ndv_red) = (NMV)MALLOC_ATOMIC(nmv_len*nmv_adv);
                   1427:        else
                   1428:                BDY(ndv_red) = (NMV)MALLOC(nmv_len*nmv_adv);
1.1       noro     1429:        for ( s0 = 0, i = 0; i < nd_psn; i++ ) {
                   1430:                NEXTNODE(s0,s); BDY(s) = (pointer)i;
                   1431:        }
                   1432:        if ( s0 ) NEXT(s) = 0;
                   1433:        return s0;
                   1434: }
                   1435:
                   1436: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp)
                   1437: {
                   1438:        struct order_spec ord1;
                   1439:        VL fv,vv,vc;
                   1440:        NODE fd,fd0,r,r0,t,x,s,xx;
                   1441:        DP a,b,c;
                   1442:
                   1443:        get_vars((Obj)f,&fv); pltovl(v,&vv);
                   1444:        nd_nvar = length(vv);
                   1445:        if ( ord->id )
                   1446:                error("nd_gr : unsupported order");
                   1447:        switch ( ord->ord.simple ) {
                   1448:                case 0:
                   1449:                        is_rlex = 1;
                   1450:                        break;
                   1451:                case 1:
                   1452:                        is_rlex = 0;
                   1453:                        break;
                   1454:                default:
                   1455:                        error("nd_gr : unsupported order");
                   1456:        }
                   1457:        initd(ord);
                   1458:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   1459:                ptod(CO,vv,(P)BDY(t),&b);
1.16      noro     1460:                if ( m )
                   1461:                        _dp_mod(b,m,0,&c);
                   1462:                else
                   1463:                        c = b;
1.1       noro     1464:                if ( c ) {
                   1465:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
                   1466:                }
                   1467:        }
                   1468:        if ( fd0 ) NEXT(fd) = 0;
1.16      noro     1469:        s = nd_setup(m,fd0);
                   1470:        x = nd_gb(m,s);
1.1       noro     1471: #if 0
                   1472:        x = nd_reduceall(x,m);
                   1473: #endif
                   1474:        for ( r0 = 0; x; x = NEXT(x) ) {
                   1475:                NEXTNODE(r0,r);
1.16      noro     1476:                a = ndvtodp(m,nd_ps[(int)BDY(x)]);
                   1477:                if ( m )
                   1478:                        _dtop_mod(CO,vv,a,(P *)&BDY(r));
                   1479:                else
                   1480:                        dtop(CO,vv,a,(P *)&BDY(r));
1.1       noro     1481:        }
                   1482:        if ( r0 ) NEXT(r) = 0;
                   1483:        MKLIST(*rp,r0);
                   1484:        fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
                   1485:                nd_found,nd_notfirst,nd_create);
                   1486: }
                   1487:
                   1488: void dltondl(int n,DL dl,unsigned int *r)
                   1489: {
                   1490:        unsigned int *d;
                   1491:        int i;
                   1492:
                   1493:        d = dl->d;
                   1494:        bzero(r,nd_wpd*sizeof(unsigned int));
                   1495:        if ( is_rlex )
                   1496:                for ( i = 0; i < n; i++ )
                   1497:                        r[(n-1-i)/nd_epw] |= (d[i]<<((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe));
                   1498:        else
                   1499:                for ( i = 0; i < n; i++ )
                   1500:                        r[i/nd_epw] |= d[i]<<((nd_epw-(i%nd_epw)-1)*nd_bpe);
                   1501: }
                   1502:
                   1503: DL ndltodl(int n,int td,unsigned int *ndl)
                   1504: {
                   1505:        DL dl;
                   1506:        int *d;
                   1507:        int i;
                   1508:
                   1509:        NEWDL(dl,n);
1.14      noro     1510:        TD(dl) = td;
1.1       noro     1511:        d = dl->d;
                   1512:        if ( is_rlex )
                   1513:                for ( i = 0; i < n; i++ )
                   1514:                        d[i] = (ndl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
                   1515:                                &((1<<nd_bpe)-1);
                   1516:        else
                   1517:                for ( i = 0; i < n; i++ )
                   1518:                        d[i] = (ndl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
                   1519:                                &((1<<nd_bpe)-1);
                   1520:        return dl;
                   1521: }
                   1522:
1.17      noro     1523: ND dptond(int mod,DP p)
1.1       noro     1524: {
                   1525:        ND d;
                   1526:        NM m0,m;
                   1527:        MP t;
                   1528:        int n;
                   1529:
                   1530:        if ( !p )
                   1531:                return 0;
                   1532:        n = NV(p);
                   1533:        m0 = 0;
                   1534:        for ( t = BDY(p); t; t = NEXT(t) ) {
                   1535:                NEXTNM(m0,m);
1.17      noro     1536:                if ( mod )
                   1537:                        CM(m) = ITOS(C(t));
                   1538:                else
                   1539:                        CQ(m) = (Q)C(t);
1.14      noro     1540:                TD(m) = TD(DL(t));
                   1541:                dltondl(n,DL(t),DL(m));
1.1       noro     1542:        }
                   1543:        NEXT(m) = 0;
                   1544:        MKND(n,m0,d);
1.14      noro     1545:        NV(d) = n;
                   1546:        SG(d) = SG(p);
1.1       noro     1547:        return d;
                   1548: }
                   1549:
1.17      noro     1550: DP ndtodp(int mod,ND p)
1.1       noro     1551: {
                   1552:        DP d;
                   1553:        MP m0,m;
                   1554:        NM t;
                   1555:        int n;
                   1556:
                   1557:        if ( !p )
                   1558:                return 0;
                   1559:        n = NV(p);
                   1560:        m0 = 0;
                   1561:        for ( t = BDY(p); t; t = NEXT(t) ) {
                   1562:                NEXTMP(m0,m);
1.17      noro     1563:                if ( mod )
                   1564:                        C(m) = STOI(CM(t));
                   1565:                else
                   1566:                        C(m) = (P)CQ(t);
1.14      noro     1567:                DL(m) = ndltodl(n,TD(t),DL(t));
1.1       noro     1568:        }
                   1569:        NEXT(m) = 0;
                   1570:        MKDP(n,m0,d);
1.14      noro     1571:        SG(d) = SG(p);
1.1       noro     1572:        return d;
                   1573: }
                   1574:
                   1575: void ndl_print(unsigned int *dl)
                   1576: {
                   1577:        int n;
                   1578:        int i;
                   1579:
                   1580:        n = nd_nvar;
                   1581:        printf("<<");
                   1582:        if ( is_rlex )
                   1583:                for ( i = 0; i < n; i++ )
                   1584:                        printf(i==n-1?"%d":"%d,",
                   1585:                                (dl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
                   1586:                                        &((1<<nd_bpe)-1));
                   1587:        else
                   1588:                for ( i = 0; i < n; i++ )
                   1589:                        printf(i==n-1?"%d":"%d,",
                   1590:                                (dl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
                   1591:                                        &((1<<nd_bpe)-1));
                   1592:        printf(">>");
                   1593: }
                   1594:
                   1595: void nd_print(ND p)
                   1596: {
                   1597:        NM m;
                   1598:
                   1599:        if ( !p )
                   1600:                printf("0\n");
                   1601:        else {
                   1602:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     1603:                        printf("+%d*",CM(m));
                   1604:                        ndl_print(DL(m));
1.1       noro     1605:                }
                   1606:                printf("\n");
                   1607:        }
                   1608: }
                   1609:
1.16      noro     1610: void nd_print_q(ND p)
                   1611: {
                   1612:        NM m;
                   1613:
                   1614:        if ( !p )
                   1615:                printf("0\n");
                   1616:        else {
                   1617:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   1618:                        printf("+");
                   1619:                        printexpr(CO,CQ(m));
                   1620:                        printf("*");
                   1621:                        ndl_print(DL(m));
                   1622:                }
                   1623:                printf("\n");
                   1624:        }
                   1625: }
                   1626:
1.1       noro     1627: void ndp_print(ND_pairs d)
                   1628: {
                   1629:        ND_pairs t;
                   1630:
                   1631:        for ( t = d; t; t = NEXT(t) ) {
                   1632:                printf("%d,%d ",t->i1,t->i2);
                   1633:        }
                   1634:        printf("\n");
                   1635: }
                   1636:
1.16      noro     1637: void nd_removecont(ND p)
                   1638: {
                   1639:        int i,n;
                   1640:        Q *w;
                   1641:        Q dvr,t;
                   1642:        NM m;
                   1643:
                   1644:        for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   1645:        w = (Q *)ALLOCA(n*sizeof(Q));
                   1646:        for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ )
                   1647:                w[i] = CQ(m);
                   1648:        sortbynm(w,n);
                   1649:        qltozl(w,n,&dvr);
                   1650:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   1651:                divq(CQ(m),dvr,&t); CQ(m) = t;
                   1652:        }
                   1653: }
                   1654:
                   1655: void ndv_removecont(NDV p)
                   1656: {
                   1657:        int i,len;
                   1658:        Q *w;
                   1659:        Q dvr,t;
                   1660:        NMV m;
                   1661:
                   1662:        len = p->len;
                   1663:        w = (Q *)ALLOCA(len*sizeof(Q));
                   1664:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
                   1665:                w[i] = CQ(m);
                   1666:        sortbynm(w,len);
                   1667:        qltozl(w,len,&dvr);
                   1668:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   1669:                divq(CQ(m),dvr,&t); CQ(m) = t;
                   1670:        }
                   1671: }
                   1672:
1.19    ! noro     1673: void nd_monic(int mod,ND p)
1.1       noro     1674: {
                   1675:        if ( !p )
                   1676:                return;
                   1677:        else
1.19    ! noro     1678:                nd_mul_c(mod,p,invm(HCM(p),mod));
1.1       noro     1679: }
                   1680:
1.19    ! noro     1681: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     1682: {
                   1683:        NM m;
                   1684:        int c,c1;
                   1685:
                   1686:        if ( !p )
                   1687:                return;
                   1688:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     1689:                c1 = CM(m);
1.19    ! noro     1690:                DMAR(c1,mul,0,mod,c);
1.14      noro     1691:                CM(m) = c;
1.1       noro     1692:        }
                   1693: }
                   1694:
1.16      noro     1695: void nd_mul_c_q(ND p,Q mul)
                   1696: {
                   1697:        NM m;
                   1698:        Q c;
                   1699:
                   1700:        if ( !p )
                   1701:                return;
                   1702:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   1703:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   1704:        }
                   1705: }
                   1706:
1.1       noro     1707: void nd_free(ND p)
                   1708: {
                   1709:        NM t,s;
                   1710:
                   1711:        if ( !p )
                   1712:                return;
                   1713:        t = BDY(p);
                   1714:        while ( t ) {
                   1715:                s = NEXT(t);
                   1716:                FREENM(t);
                   1717:                t = s;
                   1718:        }
                   1719:        FREEND(p);
                   1720: }
                   1721:
                   1722: void nd_append_red(unsigned int *d,int td,int i)
                   1723: {
1.13      noro     1724:        RHist m,m0;
1.1       noro     1725:        int h;
                   1726:
1.13      noro     1727:        NEWRHist(m);
1.1       noro     1728:        h = ndl_hash_value(td,d);
1.13      noro     1729:        m->index = i;
1.14      noro     1730:        TD(m) = td;
                   1731:        ndl_copy(d,DL(m));
1.1       noro     1732:        NEXT(m) = nd_red[h];
                   1733:        nd_red[h] = m;
                   1734: }
                   1735:
1.5       noro     1736: unsigned int *dp_compute_bound(DP p)
                   1737: {
                   1738:        unsigned int *d,*d1,*d2,*t;
                   1739:        MP m;
1.7       noro     1740:        int i,l;
1.5       noro     1741:
                   1742:        if ( !p )
                   1743:                return 0;
                   1744:        d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
                   1745:        d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
                   1746:        m = BDY(p);
1.14      noro     1747:        bcopy(DL(m)->d,d1,nd_nvar*sizeof(unsigned int));
1.5       noro     1748:        for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14      noro     1749:                d = DL(m)->d;
1.5       noro     1750:                for ( i = 0; i < nd_nvar; i++ )
                   1751:                        d2[i] = d[i] > d1[i] ? d[i] : d1[i];
                   1752:                t = d1; d1 = d2; d2 = t;
                   1753:        }
1.13      noro     1754:        l = (nd_nvar+31);
1.7       noro     1755:        t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
                   1756:        bzero(t,l*sizeof(unsigned int));
1.5       noro     1757:        bcopy(d1,t,nd_nvar*sizeof(unsigned int));
                   1758:        return t;
                   1759: }
                   1760:
1.1       noro     1761: unsigned int *nd_compute_bound(ND p)
                   1762: {
                   1763:        unsigned int *d1,*d2,*t;
1.9       noro     1764:        int i,l;
1.1       noro     1765:        NM m;
                   1766:
                   1767:        if ( !p )
                   1768:                return 0;
                   1769:        d1 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
                   1770:        d2 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
                   1771:        bcopy(HDL(p),d1,nd_wpd*sizeof(unsigned int));
                   1772:        for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14      noro     1773:                ndl_lcm(DL(m),d1,d2);
1.1       noro     1774:                t = d1; d1 = d2; d2 = t;
                   1775:        }
1.12      noro     1776:        l = nd_nvar+31;
1.9       noro     1777:        t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
                   1778:        bzero(t,l*sizeof(unsigned int));
1.5       noro     1779:        for ( i = 0; i < nd_nvar; i++ )
                   1780:                t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0;
1.1       noro     1781:        return t;
                   1782: }
                   1783:
                   1784: void nd_setup_parameters() {
                   1785:        int i;
                   1786:
                   1787:        nd_epw = (sizeof(unsigned int)*8)/nd_bpe;
                   1788:        nd_wpd = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   1789:        if ( nd_bpe < 32 ) {
                   1790:                nd_mask0 = (1<<nd_bpe)-1;
                   1791:        } else {
                   1792:                nd_mask0 = 0xffffffff;
                   1793:        }
                   1794:        bzero(nd_mask,sizeof(nd_mask));
                   1795:        nd_mask1 = 0;
                   1796:        for ( i = 0; i < nd_epw; i++ ) {
                   1797:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   1798:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   1799:        }
1.13      noro     1800:        nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
1.3       noro     1801:        nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int);
1.1       noro     1802: }
                   1803:
                   1804: ND_pairs nd_reconstruct(ND_pairs d)
                   1805: {
1.11      noro     1806:        int i,obpe,oadv;
1.13      noro     1807:        NM prev_nm_free_list;
                   1808:        RHist mr0,mr;
                   1809:        RHist r;
1.1       noro     1810:        ND_pairs s0,s,t,prev_ndp_free_list;
1.15      noro     1811:
1.1       noro     1812:        obpe = nd_bpe;
1.11      noro     1813:        oadv = nmv_adv;
1.5       noro     1814:        if ( obpe < 4 )
                   1815:                nd_bpe = 4;
                   1816:        else if ( obpe < 6 )
                   1817:                nd_bpe = 6;
                   1818:        else if ( obpe < 8 )
                   1819:                nd_bpe = 8;
                   1820:        else if ( obpe < 16 )
                   1821:                nd_bpe = 16;
                   1822:        else if ( obpe < 32 )
                   1823:                nd_bpe = 32;
                   1824:        else
                   1825:                error("nd_reconstruct : exponent too large");
                   1826:
1.1       noro     1827:        nd_setup_parameters();
                   1828:        prev_nm_free_list = _nm_free_list;
                   1829:        prev_ndp_free_list = _ndp_free_list;
                   1830:        _nm_free_list = 0;
                   1831:        _ndp_free_list = 0;
1.13      noro     1832:        for ( i = nd_psn-1; i >= 0; i-- ) {
1.11      noro     1833:                ndv_realloc(nd_ps[i],obpe,oadv);
1.1       noro     1834:        }
                   1835:        s0 = 0;
                   1836:        for ( t = d; t; t = NEXT(t) ) {
                   1837:                NEXTND_pairs(s0,s);
                   1838:                s->i1 = t->i1;
                   1839:                s->i2 = t->i2;
1.14      noro     1840:                TD(s) = TD(t);
                   1841:                SG(s) = SG(t);
1.1       noro     1842:                ndl_dup(obpe,t->lcm,s->lcm);
                   1843:        }
1.6       noro     1844:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.13      noro     1845:                for ( mr0 = 0, r = nd_red[i]; r; r = NEXT(r) ) {
1.16      noro     1846:                        NEXTRHist(mr0,mr);
1.13      noro     1847:                        mr->index = r->index;
1.14      noro     1848:                        TD(mr) = TD(r);
                   1849:                        ndl_dup(obpe,DL(r),DL(mr));
1.6       noro     1850:                }
                   1851:                if ( mr0 ) NEXT(mr) = 0;
                   1852:                nd_red[i] = mr0;
                   1853:        }
1.11      noro     1854:        for ( i = 0; i < nd_psn; i++ ) {
1.14      noro     1855:                NEWRHist(r); TD(r) = TD(nd_psh[i]); ndl_dup(obpe,DL(nd_psh[i]),DL(r));
1.13      noro     1856:                nd_psh[i] = r;
1.11      noro     1857:        }
1.1       noro     1858:        if ( s0 ) NEXT(s) = 0;
                   1859:        prev_nm_free_list = 0;
                   1860:        prev_ndp_free_list = 0;
1.3       noro     1861:        BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv);
1.1       noro     1862:        GC_gcollect();
                   1863:        return s0;
                   1864: }
                   1865:
                   1866: void ndl_dup(int obpe,unsigned int *d,unsigned int *r)
                   1867: {
                   1868:        int n,i,ei,oepw,cepw,cbpe;
                   1869:
                   1870:        n = nd_nvar;
                   1871:        oepw = (sizeof(unsigned int)*8)/obpe;
                   1872:        cepw = nd_epw;
                   1873:        cbpe = nd_bpe;
1.15      noro     1874:        for ( i = 0; i < nd_wpd; i++ )
                   1875:                r[i] = 0;
1.1       noro     1876:        if ( is_rlex )
                   1877:                for ( i = 0; i < n; i++ ) {
                   1878:                        ei = (d[(n-1-i)/oepw]>>((oepw-((n-1-i)%oepw)-1)*obpe))
                   1879:                                &((1<<obpe)-1);
                   1880:                        r[(n-1-i)/cepw] |= (ei<<((cepw-((n-1-i)%cepw)-1)*cbpe));
                   1881:                }
                   1882:        else
                   1883:                for ( i = 0; i < n; i++ ) {
                   1884:                        ei = (d[i/oepw]>>((oepw-(i%oepw)-1)*obpe))
                   1885:                                &((1<<obpe)-1);
                   1886:                        r[i/cepw] |= (ei<<((cepw-(i%cepw)-1)*cbpe));
                   1887:                }
                   1888: }
                   1889:
1.11      noro     1890: void nd_realloc(ND p,int obpe)
1.1       noro     1891: {
                   1892:        NM m,mr,mr0;
                   1893:
1.11      noro     1894:        if ( p ) {
                   1895:                m = BDY(p);
1.1       noro     1896:                for ( mr0 = 0; m; m = NEXT(m) ) {
                   1897:                        NEXTNM(mr0,mr);
1.14      noro     1898:                        CM(mr) = CM(m);
                   1899:                        TD(mr) = TD(m);
                   1900:                        ndl_dup(obpe,DL(m),DL(mr));
1.1       noro     1901:                }
                   1902:                NEXT(mr) = 0;
1.11      noro     1903:                BDY(p) = mr0;
1.1       noro     1904:        }
                   1905: }
1.3       noro     1906:
1.6       noro     1907: ND nd_copy(ND p)
                   1908: {
                   1909:        NM m,mr,mr0;
                   1910:        int c,n,s;
                   1911:        ND r;
                   1912:
                   1913:        if ( !p )
                   1914:                return 0;
                   1915:        else {
                   1916:                s = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
                   1917:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   1918:                        NEXTNM(mr0,mr);
1.14      noro     1919:                        CM(mr) = CM(m);
                   1920:                        TD(mr) = TD(m);
                   1921:                        ndl_copy(DL(m),DL(mr));
1.6       noro     1922:                }
                   1923:                NEXT(mr) = 0;
                   1924:                MKND(NV(p),mr0,r);
1.14      noro     1925:                SG(r) = SG(p);
1.6       noro     1926:                return r;
                   1927:        }
                   1928: }
                   1929:
1.16      noro     1930: int nd_sp(int mod,ND_pairs p,ND *rp)
1.11      noro     1931: {
                   1932:        NM m;
                   1933:        NDV p1,p2;
                   1934:        ND t1,t2;
                   1935:        unsigned int *lcm;
                   1936:        int td;
                   1937:
                   1938:        p1 = nd_ps[p->i1];
                   1939:        p2 = nd_ps[p->i2];
                   1940:        lcm = p->lcm;
1.14      noro     1941:        td = TD(p);
1.11      noro     1942:        NEWNM(m);
1.16      noro     1943:        if ( mod ) CM(m) = HCM(p2);
                   1944:        else CQ(m) = HCQ(p2);
                   1945:        TD(m) = td-HTD(p1); ndl_sub(lcm,HDL(p1),DL(m));
1.14      noro     1946:        if ( ndl_check_bound2(p->i1,DL(m)) )
1.11      noro     1947:                return 0;
1.16      noro     1948:        t1 = ndv_mul_nm_create(mod,p1,m);
                   1949:        if ( mod )
                   1950:                CM(m) = mod-HCM(p1);
                   1951:        else
                   1952:                chsgnq(HCQ(p1),&CQ(m));
                   1953:        TD(m) = td-HTD(p2); ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     1954:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     1955:                nd_free(t1);
                   1956:                return 0;
                   1957:        }
1.16      noro     1958:        ndv_mul_nm(mod,p2,m,ndv_red);
1.11      noro     1959:        FREENM(m);
1.16      noro     1960:        if ( mod )
1.19    ! noro     1961:                *rp = ndv_add(mod,t1,ndv_red);
1.16      noro     1962:        else
                   1963:                *rp = ndv_add_q(t1,ndv_red);
1.11      noro     1964:        return 1;
                   1965: }
                   1966:
1.19    ! noro     1967: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     1968: {
                   1969:        NMV m;
                   1970:        int c,c1,len,i;
                   1971:
                   1972:        if ( !p )
                   1973:                return;
1.14      noro     1974:        len = LEN(p);
1.11      noro     1975:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14      noro     1976:                c1 = CM(m);
1.19    ! noro     1977:                DMAR(c1,mul,0,mod,c);
1.14      noro     1978:                CM(m) = c;
1.11      noro     1979:        }
                   1980: }
                   1981:
1.16      noro     1982: void ndv_mul_c_q(NDV p,Q mul)
                   1983: {
                   1984:        NMV m;
                   1985:        Q c;
                   1986:        int len,i;
                   1987:
                   1988:        if ( !p )
                   1989:                return;
                   1990:        len = LEN(p);
                   1991:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   1992:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   1993:        }
                   1994: }
                   1995:
                   1996: void ndv_mul_nm(int mod,NDV p,NM m0,NDV r)
1.4       noro     1997: {
                   1998:        NMV m,mr,mr0;
                   1999:        unsigned int *d,*dt,*dm;
                   2000:        int c,n,td,i,c1,c2,len;
1.16      noro     2001:        Q q;
1.4       noro     2002:
                   2003:        if ( !p )
                   2004:                /* XXX */
1.14      noro     2005:                LEN(r) = 0;
1.4       noro     2006:        else {
1.14      noro     2007:                n = NV(p); m = BDY(p); len = LEN(p);
1.16      noro     2008:                d = DL(m0); td = TD(m0);
1.4       noro     2009:                mr = BDY(r);
1.16      noro     2010:                if ( mod ) {
                   2011:                        c = CM(m0);
                   2012:                        for ( ; len > 0; len--, NMV_ADV(m), NMV_ADV(mr) ) {
1.19    ! noro     2013:                                c1 = CM(m); DMAR(c1,c,0,mod,c2); CM(mr) = c2;
1.16      noro     2014:                                TD(mr) = TD(m)+td; ndl_add(DL(m),d,DL(mr));
                   2015:                        }
                   2016:                } else {
                   2017:                        q = CQ(m0);
                   2018:                        for ( ; len > 0; len--, NMV_ADV(m), NMV_ADV(mr) ) {
                   2019:                                mulq(CQ(m),q,&CQ(mr));
                   2020:                                TD(mr) = TD(m)+td; ndl_add(DL(m),d,DL(mr));
                   2021:                        }
1.9       noro     2022:                }
                   2023:                NV(r) = NV(p);
1.14      noro     2024:                LEN(r) = LEN(p);
                   2025:                SG(r) = SG(p) + td;
1.9       noro     2026:        }
                   2027: }
                   2028:
1.16      noro     2029: ND ndv_mul_nm_create(int mod,NDV p,NM m0)
1.9       noro     2030: {
                   2031:        NM mr,mr0;
                   2032:        NMV m;
                   2033:        unsigned int *d,*dt,*dm;
                   2034:        int c,n,td,i,c1,c2,len;
1.16      noro     2035:        Q q;
1.9       noro     2036:        ND r;
                   2037:
                   2038:        if ( !p )
                   2039:                return 0;
                   2040:        else {
                   2041:                n = NV(p); m = BDY(p);
1.16      noro     2042:                d = DL(m0); td = TD(m0);
1.14      noro     2043:                len = LEN(p);
1.9       noro     2044:                mr0 = 0;
1.16      noro     2045:                if ( mod ) {
                   2046:                        c = CM(m0);
                   2047:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   2048:                                NEXTNM(mr0,mr);
                   2049:                                c1 = CM(m);
1.19    ! noro     2050:                                DMAR(c1,c,0,mod,c2);
1.16      noro     2051:                                CM(mr) = c2;
                   2052:                                TD(mr) = TD(m)+td;
                   2053:                                ndl_add(DL(m),d,DL(mr));
                   2054:                        }
                   2055:                } else {
                   2056:                        q = CQ(m0);
                   2057:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   2058:                                NEXTNM(mr0,mr);
                   2059:                                mulq(CQ(m),q,&CQ(mr));
                   2060:                                TD(mr) = TD(m)+td;
                   2061:                                ndl_add(DL(m),d,DL(mr));
                   2062:                        }
1.4       noro     2063:                }
1.9       noro     2064:                NEXT(mr) = 0;
                   2065:                MKND(NV(p),mr0,r);
1.14      noro     2066:                SG(r) = SG(p) + td;
1.9       noro     2067:                return r;
1.4       noro     2068:        }
                   2069: }
                   2070:
1.19    ! noro     2071: ND ndv_add(int mod,ND p1,NDV p2)
1.4       noro     2072: {
1.9       noro     2073:        register NM prev,cur,new;
1.4       noro     2074:        int c,c1,c2,t,td,td2,mul,len,i;
1.9       noro     2075:        NM head;
1.4       noro     2076:        unsigned int *d;
                   2077:        NMV m2;
1.16      noro     2078:        Q q;
1.4       noro     2079:
                   2080:        if ( !p1 )
                   2081:                return 0;
                   2082:        else {
                   2083:                prev = 0; head = cur = BDY(p1);
1.14      noro     2084:                NEWNM(new); len = LEN(p2);
1.9       noro     2085:                for ( m2 = BDY(p2), i = 0; cur && i < len; ) {
1.14      noro     2086:                        td2 = TD(new) = TD(m2);
                   2087:                        if ( TD(cur) > td2 ) {
1.13      noro     2088:                                prev = cur; cur = NEXT(cur);
                   2089:                                continue;
1.14      noro     2090:                        } else if ( TD(cur) < td2 ) c = -1;
1.13      noro     2091:                        else if ( nd_wpd == 1 ) {
1.14      noro     2092:                                if ( DL(cur)[0] > DL(m2)[0] ) c = is_rlex ? -1 : 1;
                   2093:                                else if ( DL(cur)[0] < DL(m2)[0] ) c = is_rlex ? 1 : -1;
1.13      noro     2094:                                else c = 0;
                   2095:                        }
1.14      noro     2096:                        else c = ndl_compare(DL(cur),DL(m2));
1.4       noro     2097:                        switch ( c ) {
                   2098:                                case 0:
1.19    ! noro     2099:                                        t = CM(m2)+CM(cur)-mod;
        !          2100:                                        if ( t < 0 ) t += mod;
1.14      noro     2101:                                        if ( t ) CM(cur) = t;
1.4       noro     2102:                                        else if ( !prev ) {
1.9       noro     2103:                                                head = NEXT(cur); FREENM(cur); cur = head;
1.4       noro     2104:                                        } else {
1.9       noro     2105:                                                NEXT(prev) = NEXT(cur); FREENM(cur); cur = NEXT(prev);
1.4       noro     2106:                                        }
                   2107:                                        NMV_ADV(m2); i++;
                   2108:                                        break;
                   2109:                                case 1:
1.9       noro     2110:                                        prev = cur; cur = NEXT(cur);
1.4       noro     2111:                                        break;
                   2112:                                case -1:
1.14      noro     2113:                                        ndl_copy(DL(m2),DL(new));
1.16      noro     2114:                                        CQ(new) = CQ(m2);
                   2115:                                        if ( !prev ) {
                   2116:                                                /* cur = head */
                   2117:                                                prev = new; NEXT(prev) = head; head = prev;
                   2118:                                        } else {
                   2119:                                                NEXT(prev) = new; NEXT(new) = cur; prev = new;
                   2120:                                        }
                   2121:                                        NEWNM(new); NMV_ADV(m2); i++;
                   2122:                                        break;
                   2123:                        }
                   2124:                }
                   2125:                for ( ; i < len; i++, NMV_ADV(m2) ) {
                   2126:                        td2 = TD(new) = TD(m2); CQ(new) = CQ(m2); ndl_copy(DL(m2),DL(new));
                   2127:                        if ( !prev ) {
                   2128:                                prev = new; NEXT(prev) = 0; head = prev;
                   2129:                        } else {
                   2130:                                NEXT(prev) = new; NEXT(new) = 0; prev = new;
                   2131:                        }
                   2132:                        NEWNM(new);
                   2133:                }
                   2134:                FREENM(new);
                   2135:                if ( head ) {
                   2136:                        BDY(p1) = head; SG(p1) = MAX(SG(p1),SG(p2));
                   2137:                        return p1;
                   2138:                } else {
                   2139:                        FREEND(p1);
                   2140:                        return 0;
                   2141:                }
                   2142:
                   2143:        }
                   2144: }
                   2145:
                   2146: ND ndv_add_q(ND p1,NDV p2)
                   2147: {
                   2148:        register NM prev,cur,new;
                   2149:        int c,c1,c2,t,td,td2,mul,len,i;
                   2150:        NM head;
                   2151:        unsigned int *d;
                   2152:        NMV m2;
                   2153:        Q q;
                   2154:
                   2155:        if ( !p1 )
                   2156:                return 0;
                   2157:        else {
                   2158:                prev = 0; head = cur = BDY(p1);
                   2159:                NEWNM(new); len = LEN(p2);
                   2160:                for ( m2 = BDY(p2), i = 0; cur && i < len; ) {
                   2161:                        td2 = TD(new) = TD(m2);
                   2162:                        if ( TD(cur) > td2 ) {
                   2163:                                prev = cur; cur = NEXT(cur);
                   2164:                                continue;
                   2165:                        } else if ( TD(cur) < td2 ) c = -1;
                   2166:                        else if ( nd_wpd == 1 ) {
                   2167:                                if ( DL(cur)[0] > DL(m2)[0] ) c = is_rlex ? -1 : 1;
                   2168:                                else if ( DL(cur)[0] < DL(m2)[0] ) c = is_rlex ? 1 : -1;
                   2169:                                else c = 0;
                   2170:                        }
                   2171:                        else c = ndl_compare(DL(cur),DL(m2));
                   2172:                        switch ( c ) {
                   2173:                                case 0:
                   2174:                                        addq(CQ(cur),CQ(m2),&q);
                   2175:                                        if ( q )
                   2176:                                                CQ(cur) = q;
                   2177:                                        else if ( !prev ) {
                   2178:                                                head = NEXT(cur); FREENM(cur); cur = head;
                   2179:                                        } else {
                   2180:                                                NEXT(prev) = NEXT(cur); FREENM(cur); cur = NEXT(prev);
                   2181:                                        }
                   2182:                                        NMV_ADV(m2); i++;
                   2183:                                        break;
                   2184:                                case 1:
                   2185:                                        prev = cur; cur = NEXT(cur);
                   2186:                                        break;
                   2187:                                case -1:
                   2188:                                        ndl_copy(DL(m2),DL(new));
                   2189:                                        CQ(new) = CQ(m2);
1.4       noro     2190:                                        if ( !prev ) {
                   2191:                                                /* cur = head */
1.9       noro     2192:                                                prev = new; NEXT(prev) = head; head = prev;
1.4       noro     2193:                                        } else {
1.9       noro     2194:                                                NEXT(prev) = new; NEXT(new) = cur; prev = new;
1.4       noro     2195:                                        }
1.9       noro     2196:                                        NEWNM(new); NMV_ADV(m2); i++;
1.4       noro     2197:                                        break;
                   2198:                        }
                   2199:                }
1.9       noro     2200:                for ( ; i < len; i++, NMV_ADV(m2) ) {
1.16      noro     2201:                        td2 = TD(new) = TD(m2); CQ(new) = CQ(m2); ndl_copy(DL(m2),DL(new));
1.9       noro     2202:                        if ( !prev ) {
                   2203:                                prev = new; NEXT(prev) = 0; head = prev;
                   2204:                        } else {
                   2205:                                NEXT(prev) = new; NEXT(new) = 0; prev = new;
                   2206:                        }
                   2207:                        NEWNM(new);
                   2208:                }
1.4       noro     2209:                FREENM(new);
                   2210:                if ( head ) {
1.14      noro     2211:                        BDY(p1) = head; SG(p1) = MAX(SG(p1),SG(p2));
1.4       noro     2212:                        return p1;
                   2213:                } else {
                   2214:                        FREEND(p1);
                   2215:                        return 0;
                   2216:                }
                   2217:
                   2218:        }
                   2219: }
                   2220:
1.11      noro     2221: void ndv_realloc(NDV p,int obpe,int oadv)
                   2222: {
1.13      noro     2223:        NMV m,mr,mr0,t;
                   2224:        int len,i,k;
1.11      noro     2225:
1.13      noro     2226: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
                   2227: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.11      noro     2228:
                   2229:        if ( p ) {
1.14      noro     2230:                m = BDY(p); len = LEN(p);
1.15      noro     2231:                if ( nmv_adv > oadv )
                   2232:                        mr0 = (NMV)REALLOC(BDY(p),len*nmv_adv);
                   2233:                else
                   2234:                        mr0 = BDY(p);
1.13      noro     2235:                m = (NMV)((char *)mr0+(len-1)*oadv);
                   2236:                mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2237:                t = (NMV)ALLOCA(nmv_adv);
                   2238:                for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.16      noro     2239:                        CQ(t) = CQ(m);
1.14      noro     2240:                        TD(t) = TD(m);
                   2241:                        for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   2242:                        ndl_dup(obpe,DL(m),DL(t));
1.16      noro     2243:                        CQ(mr) = CQ(t);
1.14      noro     2244:                        TD(mr) = TD(t);
                   2245:                        ndl_copy(DL(t),DL(mr));
1.11      noro     2246:                }
                   2247:                BDY(p) = mr0;
                   2248:        }
                   2249: }
                   2250:
1.16      noro     2251: NDV ndtondv(int mod,ND p)
1.3       noro     2252: {
                   2253:        NDV d;
                   2254:        NMV m,m0;
                   2255:        NM t;
                   2256:        int i,len;
                   2257:
                   2258:        if ( !p )
                   2259:                return 0;
                   2260:        len = nd_length(p);
1.16      noro     2261:        if ( mod )
                   2262:                m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv);
                   2263:        else
                   2264:                m0 = m = (NMV)MALLOC(len*nmv_adv);
1.3       noro     2265:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
1.14      noro     2266:                TD(m) = TD(t);
                   2267:                ndl_copy(DL(t),DL(m));
1.16      noro     2268:                CQ(m) = CQ(t);
1.3       noro     2269:        }
                   2270:        MKNDV(NV(p),m0,len,d);
1.14      noro     2271:        SG(d) = SG(p);
1.3       noro     2272:        return d;
                   2273: }
                   2274:
1.16      noro     2275: NDV dptondv(int mod,DP p)
1.11      noro     2276: {
                   2277:        NDV d;
                   2278:        NMV m0,m;
                   2279:        MP t;
                   2280:        int l,i,n;
                   2281:
                   2282:        if ( !p )
                   2283:                return 0;
                   2284:        for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ );
1.16      noro     2285:        if ( mod )
                   2286:                m0 = m = (NMV)MALLOC_ATOMIC(l*nmv_adv);
                   2287:        else
                   2288:                m0 = m = (NMV)MALLOC(l*nmv_adv);
1.11      noro     2289:        n = NV(p);
1.17      noro     2290:        for ( t = BDY(p), i = 0; i < l; i++, t = NEXT(t), NMV_ADV(m) ) {
                   2291:                if ( mod )
1.16      noro     2292:                        CM(m) = ITOS(C(t));
1.17      noro     2293:                else
1.16      noro     2294:                        CQ(m) = (Q)C(t);
1.17      noro     2295:                TD(m) = TD(DL(t));
                   2296:                dltondl(n,DL(t),DL(m));
1.11      noro     2297:        }
                   2298:        MKNDV(n,m0,l,d);
1.14      noro     2299:        SG(d) = SG(p);
1.11      noro     2300:        return d;
                   2301: }
                   2302:
1.16      noro     2303: DP ndvtodp(int mod,NDV p)
1.11      noro     2304: {
                   2305:        DP d;
                   2306:        MP m0,m;
                   2307:        NMV t;
                   2308:        int len,i,n;
                   2309:
                   2310:        if ( !p )
                   2311:                return 0;
                   2312:        m0 = 0;
1.14      noro     2313:        len = LEN(p);
1.11      noro     2314:        n = NV(p);
1.17      noro     2315:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   2316:                NEXTMP(m0,m);
                   2317:                if ( mod )
1.16      noro     2318:                        C(m) = STOI(CM(t));
1.17      noro     2319:                else
1.16      noro     2320:                        C(m) = (P)CQ(t);
1.17      noro     2321:                DL(m) = ndltodl(n,TD(t),DL(t));
1.11      noro     2322:        }
                   2323:        NEXT(m) = 0;
                   2324:        MKDP(NV(p),m0,d);
1.14      noro     2325:        SG(d) = SG(p);
1.11      noro     2326:        return d;
                   2327: }
                   2328:
1.3       noro     2329: void ndv_print(NDV p)
                   2330: {
                   2331:        NMV m;
                   2332:        int i,len;
                   2333:
                   2334:        if ( !p )
                   2335:                printf("0\n");
                   2336:        else {
1.14      noro     2337:                len = LEN(p);
1.3       noro     2338:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2339:                        printf("+%d*",CM(m));
1.16      noro     2340:                        ndl_print(DL(m));
                   2341:                }
                   2342:                printf("\n");
                   2343:        }
                   2344: }
                   2345:
                   2346: void ndv_print_q(NDV p)
                   2347: {
                   2348:        NMV m;
                   2349:        int i,len;
                   2350:
                   2351:        if ( !p )
                   2352:                printf("0\n");
                   2353:        else {
                   2354:                len = LEN(p);
                   2355:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   2356:                        printf("+");
                   2357:                        printexpr(CO,CQ(m));
                   2358:                        printf("*");
1.14      noro     2359:                        ndl_print(DL(m));
1.3       noro     2360:                }
                   2361:                printf("\n");
                   2362:        }
1.11      noro     2363: }

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