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

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

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