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

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

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