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

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

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