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

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

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