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

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

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