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

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

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