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

1.64    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.63 2003/09/11 09:03:53 noro Exp $ */
1.2       noro        2:
1.1       noro        3: #include "ca.h"
                      4: #include "inline.h"
1.64    ! noro        5: #include <time.h>
1.1       noro        6:
                      7: #if defined(__GNUC__)
                      8: #define INLINE inline
                      9: #elif defined(VISUAL)
                     10: #define INLINE __inline
                     11: #else
                     12: #define INLINE
                     13: #endif
                     14:
1.61      noro       15: typedef unsigned int UINT;
                     16:
1.47      noro       17: #define USE_GEOBUCKET 1
1.28      noro       18:
1.1       noro       19: #define REDTAB_LEN 32003
                     20:
1.40      noro       21: /* GeoBucket for polynomial addition */
                     22:
1.1       noro       23: typedef struct oPGeoBucket {
                     24:        int m;
                     25:        struct oND *body[32];
                     26: } *PGeoBucket;
                     27:
1.40      noro       28: /* distributed polynomial; linked list rep. */
1.1       noro       29: typedef struct oND {
                     30:        struct oNM *body;
                     31:        int nv;
1.31      noro       32:        int len;
1.1       noro       33:        int sugar;
                     34: } *ND;
                     35:
1.40      noro       36: /* distributed polynomial; array rep. */
1.3       noro       37: typedef struct oNDV {
                     38:        struct oNMV *body;
                     39:        int nv;
1.31      noro       40:        int len;
1.3       noro       41:        int sugar;
                     42: } *NDV;
                     43:
1.40      noro       44: /* monomial; linked list rep. */
1.1       noro       45: typedef struct oNM {
                     46:        struct oNM *next;
1.14      noro       47:        union {
                     48:                int m;
                     49:                Q z;
1.61      noro       50:                P p;
1.14      noro       51:        } c;
1.61      noro       52:        UINT dl[1];
1.1       noro       53: } *NM;
                     54:
1.40      noro       55: /* monomial; array rep. */
1.3       noro       56: typedef struct oNMV {
1.14      noro       57:        union {
                     58:                int m;
                     59:                Q z;
1.61      noro       60:                P p;
1.14      noro       61:        } c;
1.61      noro       62:        UINT dl[1];
1.3       noro       63: } *NMV;
                     64:
1.40      noro       65: /* history of reducer */
1.13      noro       66: typedef struct oRHist {
                     67:        struct oRHist *next;
                     68:        int index;
1.34      noro       69:        int sugar;
1.61      noro       70:        UINT dl[1];
1.13      noro       71: } *RHist;
                     72:
1.40      noro       73: /* S-pair list */
1.1       noro       74: typedef struct oND_pairs {
                     75:        struct oND_pairs *next;
                     76:        int i1,i2;
1.34      noro       77:        int sugar;
1.61      noro       78:        UINT lcm[1];
1.1       noro       79: } *ND_pairs;
                     80:
1.42      noro       81: /* index and shift count for each exponent */
                     82: typedef struct oEPOS {
                     83:        int i; /* index */
                     84:        int s; /* shift */
                     85: } *EPOS;
                     86:
1.43      noro       87: typedef struct oBlockMask {
                     88:        int n;
                     89:        struct order_pair *order_pair;
1.61      noro       90:        UINT **mask;
1.43      noro       91: } *BlockMask;
                     92:
1.45      noro       93: typedef struct oBaseSet {
                     94:        int len;
                     95:        NDV *ps;
1.61      noro       96:        UINT **bound;
1.45      noro       97: } *BaseSet;
                     98:
1.63      noro       99: typedef struct oNM_ind_pair
                    100: {
                    101:        NM mul;
                    102:        int index;
                    103: } *NM_ind_pair;
                    104:
                    105:
1.61      noro      106: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.32      noro      107:
1.42      noro      108: static double nd_scale=2;
1.61      noro      109: static UINT **nd_bound;
1.42      noro      110: static struct order_spec *nd_ord;
                    111: static EPOS nd_epos;
1.43      noro      112: static BlockMask nd_blockmask;
1.42      noro      113: static int nd_nvar;
                    114: static int nd_isrlex;
                    115: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro      116: static UINT nd_mask[32];
                    117: static UINT nd_mask0,nd_mask1;
1.42      noro      118:
                    119: static NM _nm_free_list;
                    120: static ND _nd_free_list;
                    121: static ND_pairs _ndp_free_list;
1.20      noro      122:
                    123: static NDV *nd_ps;
1.53      noro      124: static NDV *nd_ps_trace;
1.42      noro      125: static RHist *nd_psh;
                    126: static int nd_psn,nd_pslen;
1.20      noro      127:
1.42      noro      128: static RHist *nd_red;
1.1       noro      129:
1.42      noro      130: static int nd_found,nd_create,nd_notfirst;
                    131: static int nm_adv;
                    132: static int nmv_adv;
                    133: static int nd_dcomp;
1.1       noro      134:
1.61      noro      135: extern VL CO;
1.55      noro      136: extern int Top,Reverse,dp_nelim,do_weyl;
1.58      noro      137: extern int *current_weyl_weight_vector;
1.1       noro      138:
1.40      noro      139: /* fundamental macros */
1.34      noro      140: #define TD(d) (d[0])
1.1       noro      141: #define HDL(d) ((d)->body->dl)
1.34      noro      142: #define HTD(d) (TD(HDL(d)))
1.14      noro      143: #define HCM(d) ((d)->body->c.m)
1.16      noro      144: #define HCQ(d) ((d)->body->c.z)
1.61      noro      145: #define HCP(d) ((d)->body->c.p)
1.14      noro      146: #define CM(a) ((a)->c.m)
1.16      noro      147: #define CQ(a) ((a)->c.z)
1.61      noro      148: #define CP(a) ((a)->c.p)
1.14      noro      149: #define DL(a) ((a)->dl)
                    150: #define SG(a) ((a)->sugar)
                    151: #define LEN(a) ((a)->len)
1.33      noro      152: #define LCM(a) ((a)->lcm)
1.42      noro      153: #define GET_EXP(d,a) (((d)[nd_epos[a].i]>>nd_epos[a].s)&nd_mask0)
1.60      noro      154: #define GET_EXP_MASK(d,a,m) ((((d)[nd_epos[a].i]&(m)[nd_epos[a].i])>>nd_epos[a].s)&nd_mask0)
1.42      noro      155: #define PUT_EXP(r,a,e) ((r)[nd_epos[a].i] |= ((e)<<nd_epos[a].s))
1.45      noro      156: #define XOR_EXP(r,a,e) ((r)[nd_epos[a].i] ^= ((e)<<nd_epos[a].s))
1.1       noro      157:
1.61      noro      158: #define GET_EXP_OLD(d,a) (((d)[oepos[a].i]>>oepos[a].s)&omask0)
                    159: #define PUT_EXP_OLD(r,a,e) ((r)[oepos[a].i] |= ((e)<<oepos[a].s))
                    160:
1.40      noro      161: /* macros for term comparison */
1.34      noro      162: #define TD_DL_COMPARE(d1,d2)\
1.41      noro      163: (TD(d1)>TD(d2)?1:(TD(d1)<TD(d2)?-1:ndl_lex_compare(d1,d2)))
1.43      noro      164: #if 0
1.34      noro      165: #define DL_COMPARE(d1,d2)\
1.43      noro      166: (nd_dcomp>0?TD_DL_COMPARE(d1,d2)\
                    167:          :(nd_dcomp==0?ndl_lex_compare(d1,d2)\
                    168:                      :(nd_blockmask?ndl_block_compare(d1,d2)\
1.45      noro      169:                                                                   :(*ndl_compare_function)(d1,d2))))
1.43      noro      170: #else
                    171: #define DL_COMPARE(d1,d2)\
1.45      noro      172: (nd_dcomp>0?TD_DL_COMPARE(d1,d2):(*ndl_compare_function)(d1,d2))
1.43      noro      173: #endif
1.34      noro      174:
1.40      noro      175: /* allocators */
1.15      noro      176: #define NEWRHist(r) \
1.61      noro      177: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(UINT)))
1.34      noro      178: #define NEWND_pairs(m) \
                    179: if(!_ndp_free_list)_NDP_alloc();\
                    180: (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
                    181: #define NEWNM(m)\
                    182: if(!_nm_free_list)_NM_alloc();\
                    183: (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
                    184: #define MKND(n,m,len,d)\
                    185: if(!_nd_free_list)_ND_alloc();\
                    186: (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list);\
                    187: NV(d)=(n); LEN(d)=(len); BDY(d)=(m)
1.40      noro      188: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
                    189: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.63      noro      190: #define NEWNM_ind_pair(p)\
                    191: ((p)=(NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair)))
1.1       noro      192:
1.40      noro      193: /* allocate and link a new object */
1.13      noro      194: #define NEXTRHist(r,c) \
                    195: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1       noro      196: #define NEXTNM(r,c) \
                    197: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
                    198: #define NEXTNM2(r,c,s) \
                    199: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
1.40      noro      200: #define NEXTND_pairs(r,c) \
                    201: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
1.63      noro      202: #define MKNM_ind_pair(p,m,i) (NEWNM_ind_pair(p),(p)->mul=(m),(p)->index=(i))
1.34      noro      203:
1.40      noro      204: /* deallocators */
1.1       noro      205: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
                    206: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
                    207: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
                    208:
1.40      noro      209: /* macro for increasing pointer to NMV */
                    210: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
1.61      noro      211: #define NMV_OADV(m) (m = (NMV)(((char *)m)+oadv))
                    212: #define NDV_NADV(m) (m = (NMV)(((char *)m)+newadv))
1.56      noro      213: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.61      noro      214: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
                    215:
1.40      noro      216: /* external functions */
                    217: void GC_gcollect();
                    218: NODE append_one(NODE,int);
1.1       noro      219:
1.40      noro      220: /* manipulation of coefficients */
1.20      noro      221: void nd_removecont(int mod,ND p);
1.21      noro      222: void nd_removecont2(ND p1,ND p2);
1.40      noro      223: void removecont_array(Q *c,int n);
                    224:
                    225: /* GeoBucket functions */
1.25      noro      226: ND normalize_pbucket(int mod,PGeoBucket g);
                    227: int head_pbucket(int mod,PGeoBucket g);
1.26      noro      228: int head_pbucket_q(PGeoBucket g);
1.63      noro      229: void add_pbucket_symbolic(PGeoBucket g,ND d);
1.31      noro      230: void add_pbucket(int mod,PGeoBucket g,ND d);
1.25      noro      231: void free_pbucket(PGeoBucket b);
1.26      noro      232: void mulq_pbucket(PGeoBucket g,Q c);
1.63      noro      233: NM remove_head_pbucket_symbolic(PGeoBucket g);
1.25      noro      234: PGeoBucket create_pbucket();
1.20      noro      235:
1.40      noro      236: /* manipulation of pairs and bases */
1.39      noro      237: int nd_newps(int mod,ND a,ND aq);
1.40      noro      238: ND_pairs nd_newpairs( NODE g, int t );
1.1       noro      239: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
1.63      noro      240: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest );
1.1       noro      241: NODE update_base(NODE nd,int ndp);
1.40      noro      242: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
                    243: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
                    244: ND_pairs crit_B( ND_pairs d, int s );
                    245: ND_pairs crit_M( ND_pairs d1 );
                    246: ND_pairs crit_F( ND_pairs d1 );
1.1       noro      247: int crit_2( int dp1, int dp2 );
1.63      noro      248: int ndv_newps(NDV a,NDV aq);
1.40      noro      249:
                    250: /* top level functions */
1.63      noro      251: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp);
1.52      noro      252: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
1.63      noro      253: NODE nd_f4(int m);
1.27      noro      254: NODE nd_gb(int m,int checkonly);
1.23      noro      255: NODE nd_gb_trace(int m);
1.40      noro      256:
                    257: /* ndl functions */
1.61      noro      258: int ndl_weight(UINT *d);
                    259: void ndl_weight_mask(UINT *d);
                    260: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight);
                    261: void ndl_dehomogenize(UINT *p);
                    262: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos);
                    263: INLINE int ndl_reducible(UINT *d1,UINT *d2);
                    264: INLINE int ndl_lex_compare(UINT *d1,UINT *d2);
                    265: INLINE int ndl_block_compare(UINT *d1,UINT *d2);
                    266: INLINE int ndl_equal(UINT *d1,UINT *d2);
                    267: INLINE void ndl_copy(UINT *d1,UINT *d2);
                    268: INLINE void ndl_zero(UINT *d);
                    269: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d);
                    270: INLINE void ndl_addto(UINT *d1,UINT *d2);
                    271: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d);
                    272: INLINE int ndl_hash_value(UINT *d);
1.45      noro      273:
                    274: /* normal forms */
1.63      noro      275: INLINE int ndl_find_reducer(UINT *g);
                    276: INLINE int ndl_find_reducer_direct(UINT *g,NDV *ps,int len);
1.53      noro      277: int nd_sp(int mod,int trace,ND_pairs p,ND *nf);
                    278: int nd_nf(int mod,ND g,NDV *ps,int full,ND *nf);
                    279: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *nf);
1.45      noro      280: int nd_nf_direct(int mod,ND g,BaseSet base,int full,ND *rp);
1.40      noro      281:
                    282: /* finalizers */
1.61      noro      283: NODE ndv_reducebase(NODE x);
                    284: NODE ndv_reduceall(int m,NODE f);
1.40      noro      285:
                    286: /* allocators */
                    287: void nd_free_private_storage();
                    288: void _NM_alloc();
                    289: void _ND_alloc();
1.1       noro      290: void nd_free(ND p);
1.40      noro      291: void nd_free_redlist();
                    292:
                    293: /* printing */
1.61      noro      294: void ndl_print(UINT *dl);
1.1       noro      295: void nd_print(ND p);
1.16      noro      296: void nd_print_q(ND p);
1.1       noro      297: void ndp_print(ND_pairs d);
1.40      noro      298:
                    299:
                    300: /* setup, reconstruct */
                    301: void nd_init_ord(struct order_spec *spec);
                    302: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs ndp);
                    303: void nd_reconstruct_direct(int mod,NDV *ps,int len);
1.61      noro      304: void ndv_setup(int mod,int trace,NODE f);
                    305: void nd_setup_parameters(int nvar,int max);
1.43      noro      306: BlockMask nd_create_blockmask(struct order_spec *ord);
1.57      noro      307: EPOS nd_create_epos(struct order_spec *ord);
1.48      noro      308: int nd_get_exporigin(struct order_spec *ord);
1.61      noro      309: void ndv_mod(int mod,NDV p);
                    310: NDV ndv_dup(int mod,NDV p);
1.63      noro      311: ND nd_dup(ND p);
1.40      noro      312:
                    313: /* ND functions */
1.61      noro      314: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand);
1.40      noro      315: void nd_mul_c(int mod,ND p,int mul);
                    316: void nd_mul_c_q(ND p,Q mul);
1.61      noro      317: void nd_mul_c_p(VL vl,ND p,P mul);
1.40      noro      318: ND nd_remove_head(ND p);
1.1       noro      319: int nd_length(ND p);
1.61      noro      320: void nd_append_red(UINT *d,int i);
                    321: UINT *ndv_compute_bound(NDV p);
1.6       noro      322: ND nd_copy(ND p);
1.63      noro      323: ND nd_merge(ND p1,ND p2);
1.40      noro      324: ND nd_add(int mod,ND p1,ND p2);
                    325: ND nd_add_q(ND p1,ND p2);
1.41      noro      326: INLINE int nd_length(ND p);
1.4       noro      327:
1.40      noro      328: /* NDV functions */
1.55      noro      329: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p);
                    330: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen);
1.19      noro      331: void ndv_mul_c(int mod,NDV p,int mul);
1.40      noro      332: void ndv_mul_c_q(NDV p,Q mul);
1.63      noro      333: ND ndv_mul_nm_symbolic(NM m0,NDV p);
1.61      noro      334: ND ndv_mul_nm(int mod,NM m0,NDV p);
1.43      noro      335: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos);
1.61      noro      336: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos);
                    337: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS eops);
1.45      noro      338: void ndv_dehomogenize(NDV p,struct order_spec *spec);
1.40      noro      339: void ndv_removecont(int mod,NDV p);
                    340: void ndv_print(NDV p);
                    341: void ndv_print_q(NDV p);
                    342: void ndv_free(NDV p);
                    343:
                    344: /* converters */
1.61      noro      345: ND ptond(VL vl,VL dvl,P p);
                    346: NDV ptondv(VL vl,VL dvl,P p);
                    347: P ndvtop(int mod,VL vl,VL dvl,NDV p);
1.16      noro      348: NDV ndtondv(int mod,ND p);
1.23      noro      349: ND ndvtond(int mod,NDV p);
1.63      noro      350: int nm_ind_pair_to_vect(int m,UINT *s0,int n,NM_ind_pair pair,UINT *r);
1.64    ! noro      351: void nm_ind_pair_to_vect_compress(int m,UINT *s0,int n,NM_ind_pair pair,UINT *r,int *ind);
1.63      noro      352: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r);
1.1       noro      353:
                    354: void nd_free_private_storage()
                    355: {
                    356:        _nd_free_list = 0;
                    357:        _nm_free_list = 0;
1.5       noro      358:        _ndp_free_list = 0;
1.13      noro      359:        bzero(nd_red,sizeof(REDTAB_LEN*sizeof(RHist)));
1.1       noro      360:        GC_gcollect();
                    361: }
                    362:
                    363: void _NM_alloc()
                    364: {
                    365:        NM p;
                    366:        int i;
                    367:
1.11      noro      368:        for ( i = 0; i < 1024; i++ ) {
1.61      noro      369:                p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro      370:                p->next = _nm_free_list; _nm_free_list = p;
                    371:        }
                    372: }
                    373:
                    374: void _ND_alloc()
                    375: {
                    376:        ND p;
                    377:        int i;
                    378:
                    379:        for ( i = 0; i < 1024; i++ ) {
                    380:                p = (ND)GC_malloc(sizeof(struct oND));
                    381:                p->body = (NM)_nd_free_list; _nd_free_list = p;
                    382:        }
                    383: }
                    384:
                    385: void _NDP_alloc()
                    386: {
                    387:        ND_pairs p;
                    388:        int i;
                    389:
1.11      noro      390:        for ( i = 0; i < 1024; i++ ) {
1.1       noro      391:                p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
1.61      noro      392:                        +(nd_wpd-1)*sizeof(UINT));
1.1       noro      393:                p->next = _ndp_free_list; _ndp_free_list = p;
                    394:        }
                    395: }
                    396:
1.30      noro      397: INLINE int nd_length(ND p)
1.1       noro      398: {
                    399:        NM m;
                    400:        int i;
                    401:
                    402:        if ( !p )
                    403:                return 0;
                    404:        else {
                    405:                for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    406:                return i;
                    407:        }
                    408: }
                    409:
1.61      noro      410: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      411: {
1.61      noro      412:        UINT u1,u2;
1.1       noro      413:        int i,j;
                    414:
1.34      noro      415:        if ( TD(d1) < TD(d2) ) return 0;
1.1       noro      416:        switch ( nd_bpe ) {
1.63      noro      417: #if 0
1.62      noro      418:                case 3:
                    419:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    420:                                u1 = d1[i]; u2 = d2[i];
                    421:                                if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    422:                                if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    423:                                if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    424:                                if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    425:                                if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    426:                                if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    427:                                if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    428:                                if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    429:                                if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    430:                                if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    431:                        }
                    432:                        return 1;
                    433:                        break;
1.1       noro      434:                case 4:
1.41      noro      435:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      436:                                u1 = d1[i]; u2 = d2[i];
                    437:                                if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
1.62      noro      438:                                if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    439:                                if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    440:                                if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    441:                                if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    442:                                if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    443:                                if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    444:                                if ( (u1&       0xf) < (u2&       0xf) ) return 0;
1.1       noro      445:                        }
                    446:                        return 1;
                    447:                        break;
                    448:                case 6:
1.41      noro      449:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      450:                                u1 = d1[i]; u2 = d2[i];
                    451:                                if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
1.62      noro      452:                                if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    453:                                if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    454:                                if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    455:                                if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
1.1       noro      456:                        }
                    457:                        return 1;
                    458:                        break;
                    459:                case 8:
1.41      noro      460:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      461:                                u1 = d1[i]; u2 = d2[i];
                    462:                                if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
1.62      noro      463:                                if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    464:                                if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    465:                                if ( (u1&      0xff) < (u2&      0xff) ) return 0;
1.1       noro      466:                        }
                    467:                        return 1;
                    468:                        break;
                    469:                case 16:
1.41      noro      470:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      471:                                u1 = d1[i]; u2 = d2[i];
                    472:                                if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
1.62      noro      473:                                if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
1.1       noro      474:                        }
                    475:                        return 1;
                    476:                        break;
                    477:                case 32:
1.41      noro      478:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      479:                                if ( d1[i] < d2[i] ) return 0;
                    480:                        return 1;
                    481:                        break;
1.63      noro      482: #endif
1.1       noro      483:                default:
1.41      noro      484:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      485:                                u1 = d1[i]; u2 = d2[i];
                    486:                                for ( j = 0; j < nd_epw; j++ )
                    487:                                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    488:                        }
                    489:                        return 1;
                    490:        }
                    491: }
                    492:
1.61      noro      493: /*
                    494:  * If the current order is a block order,
                    495:  * then the last block is length 1 and contains
                    496:  * the homo variable. Otherwise, the original
                    497:  * order is either 0 or 2.
                    498:  */
                    499:
                    500: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight)
1.23      noro      501: {
1.61      noro      502:        int w,i,e,n,omask0;
                    503:
                    504:        omask0 = (1<<obpe)-1;
                    505:        n = nd_nvar-1;
                    506:        ndl_zero(r);
                    507:        for ( i = 0; i < n; i++ ) {
                    508:                e = GET_EXP_OLD(d,i);
                    509:                PUT_EXP(r,i,e);
                    510:        }
                    511:        w = TD(d);
                    512:        PUT_EXP(r,nd_nvar-1,weight-w);
                    513:        TD(r) = weight;
                    514:        if ( nd_blockmask ) ndl_weight_mask(r);
                    515: }
                    516:
                    517: void ndl_dehomogenize(UINT *d)
                    518: {
                    519:        UINT mask;
                    520:        UINT h;
1.31      noro      521:        int i,bits;
1.23      noro      522:
1.44      noro      523:        if ( nd_blockmask ) {
                    524:                h = GET_EXP(d,nd_nvar-1);
1.45      noro      525:                XOR_EXP(d,nd_nvar-1,h);
1.44      noro      526:                TD(d) -= h;
                    527:                d[nd_exporigin-1] -= h;
                    528:        } else {
                    529:                if ( nd_isrlex ) {
                    530:                        if ( nd_bpe == 32 ) {
                    531:                                h = d[nd_exporigin];
                    532:                                for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    533:                                        d[i-1] = d[i];
                    534:                                d[i-1] = 0;
                    535:                                TD(d) -= h;
                    536:                        } else {
                    537:                                bits = nd_epw*nd_bpe;
                    538:                                mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    539:                                h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    540:                                for ( i = nd_exporigin; i < nd_wpd; i++ )
                    541:                                        d[i] = ((d[i]<<nd_bpe)&mask)
                    542:                                                |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    543:                                TD(d) -= h;
                    544:                        }
1.45      noro      545:                } else {
                    546:                        h = GET_EXP(d,nd_nvar-1);
                    547:                        XOR_EXP(d,nd_nvar-1,h);
                    548:                        TD(d) -= h;
                    549:                }
1.44      noro      550:        }
1.23      noro      551: }
                    552:
1.61      noro      553: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      554: {
1.61      noro      555:        UINT t1,t2,u,u1,u2;
1.43      noro      556:        int i,j,l;
1.1       noro      557:
                    558:        switch ( nd_bpe ) {
1.63      noro      559: #if 0
1.62      noro      560:                case 3:
                    561:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    562:                                u1 = d1[i]; u2 = d2[i];
                    563:                                t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    564:                                t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    565:                                t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    566:                                t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    567:                                t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    568:                                t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    569:                                t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    570:                                t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    571:                                t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    572:                                t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    573:                                d[i] = u;
                    574:                        }
                    575:                        break;
1.1       noro      576:                case 4:
1.41      noro      577:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      578:                                u1 = d1[i]; u2 = d2[i];
                    579:                                t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
1.62      noro      580:                                t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    581:                                t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    582:                                t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    583:                                t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    584:                                t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    585:                                t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    586:                                t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
1.1       noro      587:                                d[i] = u;
                    588:                        }
                    589:                        break;
                    590:                case 6:
1.41      noro      591:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      592:                                u1 = d1[i]; u2 = d2[i];
                    593:                                t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
1.62      noro      594:                                t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    595:                                t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    596:                                t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    597:                                t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
1.1       noro      598:                                d[i] = u;
                    599:                        }
                    600:                        break;
                    601:                case 8:
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&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
1.62      noro      605:                                t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    606:                                t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    607:                                t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
1.1       noro      608:                                d[i] = u;
                    609:                        }
                    610:                        break;
                    611:                case 16:
1.41      noro      612:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      613:                                u1 = d1[i]; u2 = d2[i];
                    614:                                t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
1.62      noro      615:                                t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
1.1       noro      616:                                d[i] = u;
                    617:                        }
                    618:                        break;
                    619:                case 32:
1.41      noro      620:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      621:                                u1 = d1[i]; u2 = d2[i];
                    622:                                d[i] = u1>u2?u1:u2;
                    623:                        }
                    624:                        break;
1.63      noro      625: #endif
1.1       noro      626:                default:
1.41      noro      627:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      628:                                u1 = d1[i]; u2 = d2[i];
                    629:                                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    630:                                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    631:                                }
                    632:                                d[i] = u;
                    633:                        }
                    634:                        break;
                    635:        }
1.39      noro      636:        TD(d) = ndl_weight(d);
1.61      noro      637:        if ( nd_blockmask ) ndl_weight_mask(d);
1.57      noro      638: }
                    639:
1.61      noro      640: int ndl_weight(UINT *d)
1.1       noro      641: {
1.61      noro      642:        UINT t,u;
1.1       noro      643:        int i,j;
                    644:
1.60      noro      645:        if ( current_dl_weight_vector )
                    646:                for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    647:                        u = GET_EXP(d,i);
                    648:                        t += MUL_WEIGHT(u,i);
                    649:                }
                    650:        else
                    651:                for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    652:                        u = d[i];
                    653:                        for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    654:                                t += (u&nd_mask0);
                    655:                }
1.1       noro      656:        return t;
                    657: }
                    658:
1.61      noro      659: void ndl_weight_mask(UINT *d)
1.43      noro      660: {
1.61      noro      661:        UINT t,u;
                    662:        UINT *mask;
                    663:        int i,j,k,l;
1.43      noro      664:
1.61      noro      665:        l = nd_blockmask->n;
                    666:        for ( k = 0; k < l; k++ ) {
                    667:                mask = nd_blockmask->mask[k];
                    668:                if ( current_dl_weight_vector )
                    669:                        for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    670:                                u = GET_EXP_MASK(d,i,mask);
                    671:                                t += MUL_WEIGHT(u,i);
                    672:                        }
                    673:                else
                    674:                        for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    675:                                u = d[i]&mask[i];
                    676:                                for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    677:                                        t += (u&nd_mask0);
                    678:                        }
                    679:                d[k+1] = t;
                    680:        }
1.43      noro      681: }
                    682:
1.61      noro      683: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      684: {
                    685:        int i;
                    686:
1.41      noro      687:        d1 += nd_exporigin;
                    688:        d2 += nd_exporigin;
                    689:        for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
1.1       noro      690:                if ( *d1 > *d2 )
1.32      noro      691:                        return nd_isrlex ? -1 : 1;
1.1       noro      692:                else if ( *d1 < *d2 )
1.32      noro      693:                        return nd_isrlex ? 1 : -1;
1.1       noro      694:        return 0;
                    695: }
                    696:
1.61      noro      697: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      698: {
                    699:        int i,l,j,ord_o,ord_l;
                    700:        struct order_pair *op;
1.61      noro      701:        UINT t1,t2,m;
                    702:        UINT *mask;
1.43      noro      703:
                    704:        l = nd_blockmask->n;
                    705:        op = nd_blockmask->order_pair;
                    706:        for ( j = 0; j < l; j++ ) {
                    707:                mask = nd_blockmask->mask[j];
                    708:                ord_o = op[j].order;
                    709:                if ( ord_o < 2 )
1.44      noro      710:                        if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    711:                        else if ( t1 < t2 ) return -1;
1.43      noro      712:                for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.44      noro      713:                        m = mask[i];
                    714:                        t1 = d1[i]&m;
                    715:                        t2 = d2[i]&m;
1.43      noro      716:                        if ( t1 > t2 )
                    717:                                return !ord_o ? -1 : 1;
                    718:                        else if ( t1 < t2 )
                    719:                                return !ord_o ? 1 : -1;
                    720:                }
                    721:        }
                    722:        return 0;
                    723: }
                    724:
1.58      noro      725: /* TDH -> WW -> TD-> RL */
                    726:
1.61      noro      727: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      728: {
                    729:        int i,m,e1,e2;
                    730:
                    731:        if ( TD(d1) > TD(d2) ) return 1;
                    732:        else if ( TD(d1) < TD(d2) ) return -1;
                    733:        m = nd_nvar>>1;
                    734:        for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    735:                e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    736:                e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    737:        }
                    738:        if ( e1 > e2 ) return 1;
                    739:        else if ( e1 < e2 ) return -1;
                    740:        return ndl_lex_compare(d1,d2);
                    741: }
                    742:
1.61      noro      743: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      744: {
                    745:        int i;
                    746:
1.41      noro      747:        for ( i = 0; i < nd_wpd; i++ )
1.34      noro      748:                if ( *d1++ != *d2++ )
1.1       noro      749:                        return 0;
                    750:        return 1;
                    751: }
                    752:
1.61      noro      753: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      754: {
                    755:        int i;
                    756:
                    757:        switch ( nd_wpd ) {
1.41      noro      758:                case 2:
1.34      noro      759:                        TD(d2) = TD(d1);
                    760:                        d2[1] = d1[1];
1.6       noro      761:                        break;
1.41      noro      762:                case 3:
1.34      noro      763:                        TD(d2) = TD(d1);
1.6       noro      764:                        d2[1] = d1[1];
1.34      noro      765:                        d2[2] = d1[2];
1.6       noro      766:                        break;
                    767:                default:
1.41      noro      768:                        for ( i = 0; i < nd_wpd; i++ )
1.6       noro      769:                                d2[i] = d1[i];
                    770:                        break;
                    771:        }
                    772: }
                    773:
1.61      noro      774: INLINE void ndl_zero(UINT *d)
                    775: {
                    776:        int i;
                    777:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    778: }
                    779:
                    780: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      781: {
                    782:        int i;
                    783:
1.43      noro      784: #if 1
1.6       noro      785:        switch ( nd_wpd ) {
1.41      noro      786:                case 2:
                    787:                        TD(d) = TD(d1)+TD(d2);
1.34      noro      788:                        d[1] = d1[1]+d2[1];
1.6       noro      789:                        break;
1.41      noro      790:                case 3:
                    791:                        TD(d) = TD(d1)+TD(d2);
1.6       noro      792:                        d[1] = d1[1]+d2[1];
1.34      noro      793:                        d[2] = d1[2]+d2[2];
1.6       noro      794:                        break;
                    795:                default:
1.43      noro      796:                        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.6       noro      797:                        break;
                    798:        }
1.43      noro      799: #else
                    800:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    801: #endif
1.6       noro      802: }
                    803:
1.55      noro      804: /* d1 += d2 */
1.61      noro      805: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      806: {
                    807:        int i;
                    808:
                    809: #if 1
                    810:        switch ( nd_wpd ) {
                    811:                case 2:
                    812:                        TD(d1) += TD(d2);
                    813:                        d1[1] += d2[1];
                    814:                        break;
                    815:                case 3:
                    816:                        TD(d1) += TD(d2);
                    817:                        d1[1] += d2[1];
                    818:                        d1[2] += d2[2];
                    819:                        break;
                    820:                default:
                    821:                        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    822:                        break;
                    823:        }
                    824: #else
                    825:        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    826: #endif
                    827: }
                    828:
1.61      noro      829: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      830: {
                    831:        int i;
                    832:
1.43      noro      833:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      834: }
                    835:
1.61      noro      836: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      837: {
1.61      noro      838:        UINT t1,t2,u,u1,u2;
1.1       noro      839:        int i,j;
                    840:
                    841:        switch ( nd_bpe ) {
1.63      noro      842: #if 0
1.62      noro      843:                case 3:
                    844:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    845:                                u1 = d1[i]; u2 = d2[i];
                    846:                                t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    847:                                t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    848:                                t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    849:                                t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    850:                                t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    851:                                t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    852:                                t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    853:                                t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    854:                                t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    855:                                t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    856:                        }
                    857:                        return 1;
                    858:                        break;
1.1       noro      859:                case 4:
1.41      noro      860:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      861:                                u1 = d1[i]; u2 = d2[i];
                    862:                                t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1.62      noro      863:                                t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    864:                                t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    865:                                t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    866:                                t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    867:                                t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    868:                                t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    869:                                t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
1.1       noro      870:                        }
                    871:                        return 1;
                    872:                        break;
                    873:                case 6:
1.41      noro      874:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      875:                                u1 = d1[i]; u2 = d2[i];
                    876:                                t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1.62      noro      877:                                t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    878:                                t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    879:                                t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    880:                                t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
1.1       noro      881:                        }
                    882:                        return 1;
                    883:                        break;
                    884:                case 8:
1.41      noro      885:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      886:                                u1 = d1[i]; u2 = d2[i];
                    887:                                t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1.62      noro      888:                                t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    889:                                t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    890:                                t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
1.1       noro      891:                        }
                    892:                        return 1;
                    893:                        break;
                    894:                case 16:
1.41      noro      895:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      896:                                u1 = d1[i]; u2 = d2[i];
                    897:                                t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1.62      noro      898:                                t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
1.1       noro      899:                        }
                    900:                        return 1;
                    901:                        break;
                    902:                case 32:
1.41      noro      903:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      904:                                if ( d1[i] && d2[i] ) return 0;
                    905:                        return 1;
                    906:                        break;
1.63      noro      907: #endif
1.1       noro      908:                default:
1.41      noro      909:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      910:                                u1 = d1[i]; u2 = d2[i];
                    911:                                for ( j = 0; j < nd_epw; j++ ) {
                    912:                                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    913:                                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    914:                                }
                    915:                        }
                    916:                        return 1;
                    917:                        break;
                    918:        }
                    919: }
                    920:
1.61      noro      921: int ndl_check_bound2(int index,UINT *d2)
1.1       noro      922: {
1.61      noro      923:        UINT u2;
                    924:        UINT *d1;
1.5       noro      925:        int i,j,ind,k;
1.1       noro      926:
1.5       noro      927:        d1 = nd_bound[index];
                    928:        ind = 0;
                    929:        switch ( nd_bpe ) {
1.63      noro      930: #if 0
1.62      noro      931:                case 3:
                    932:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    933:                                u2 = d2[i];
                    934:                                if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                    935:                                if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                    936:                                if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                    937:                                if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                    938:                                if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                    939:                                if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                    940:                                if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                    941:                                if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                    942:                                if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                    943:                                if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                    944:                        }
                    945:                        return 0;
                    946:                        break;
1.5       noro      947:                case 4:
1.41      noro      948:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      949:                                u2 = d2[i];
                    950:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                    951:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                    952:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                    953:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                    954:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                    955:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                    956:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                    957:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                    958:                        }
                    959:                        return 0;
                    960:                        break;
                    961:                case 6:
1.41      noro      962:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      963:                                u2 = d2[i];
                    964:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                    965:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                    966:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                    967:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                    968:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                    969:                        }
                    970:                        return 0;
                    971:                        break;
                    972:                case 8:
1.41      noro      973:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      974:                                u2 = d2[i];
                    975:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                    976:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                    977:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                    978:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                    979:                        }
                    980:                        return 0;
                    981:                        break;
                    982:                case 16:
1.41      noro      983:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      984:                                u2 = d2[i];
                    985:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                    986:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                    987:                        }
                    988:                        return 0;
                    989:                        break;
                    990:                case 32:
1.41      noro      991:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.5       noro      992:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                    993:                        return 0;
                    994:                        break;
1.63      noro      995: #endif
1.5       noro      996:                default:
1.41      noro      997:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro      998:                                u2 = d2[i];
                    999:                                k = (nd_epw-1)*nd_bpe;
                   1000:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1001:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1002:                        }
                   1003:                        return 0;
                   1004:                        break;
                   1005:        }
1.1       noro     1006: }
                   1007:
1.61      noro     1008: int ndl_check_bound2_direct(UINT *d1,UINT *d2)
1.23      noro     1009: {
1.61      noro     1010:        UINT u2;
1.45      noro     1011:        int i,j,ind,k;
1.23      noro     1012:
1.45      noro     1013:        ind = 0;
1.23      noro     1014:        switch ( nd_bpe ) {
1.63      noro     1015: #if 0
1.62      noro     1016:                case 3:
                   1017:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1018:                                u2 = d2[i];
                   1019:                                if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1020:                                if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1021:                                if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1022:                                if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1023:                                if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1024:                                if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1025:                                if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1026:                                if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1027:                                if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1028:                                if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1029:                        }
                   1030:                        return 0;
                   1031:                        break;
1.23      noro     1032:                case 4:
1.41      noro     1033:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45      noro     1034:                                u2 = d2[i];
                   1035:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1036:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1037:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1038:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1039:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1040:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1041:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1042:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1.23      noro     1043:                        }
                   1044:                        return 0;
                   1045:                        break;
                   1046:                case 6:
1.41      noro     1047:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45      noro     1048:                                u2 = d2[i];
                   1049:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1050:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1051:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1052:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1053:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1.23      noro     1054:                        }
                   1055:                        return 0;
                   1056:                        break;
                   1057:                case 8:
1.41      noro     1058:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45      noro     1059:                                u2 = d2[i];
                   1060:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1061:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1062:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1063:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1.23      noro     1064:                        }
                   1065:                        return 0;
                   1066:                        break;
                   1067:                case 16:
1.41      noro     1068:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45      noro     1069:                                u2 = d2[i];
                   1070:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1071:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1.23      noro     1072:                        }
                   1073:                        return 0;
                   1074:                        break;
                   1075:                case 32:
1.41      noro     1076:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.23      noro     1077:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1078:                        return 0;
                   1079:                        break;
1.63      noro     1080: #endif
1.23      noro     1081:                default:
1.41      noro     1082:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45      noro     1083:                                u2 = d2[i];
1.23      noro     1084:                                k = (nd_epw-1)*nd_bpe;
                   1085:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1.45      noro     1086:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1.23      noro     1087:                        }
                   1088:                        return 0;
                   1089:                        break;
                   1090:        }
                   1091: }
                   1092:
1.61      noro     1093: INLINE int ndl_hash_value(UINT *d)
1.1       noro     1094: {
                   1095:        int i;
                   1096:        int r;
                   1097:
1.34      noro     1098:        r = 0;
1.41      noro     1099:        for ( i = 0; i < nd_wpd; i++ )
1.1       noro     1100:                r = ((r<<16)+d[i])%REDTAB_LEN;
                   1101:        return r;
                   1102: }
                   1103:
1.63      noro     1104: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1105: {
1.13      noro     1106:        RHist r;
1.6       noro     1107:        int d,k,i;
1.1       noro     1108:
1.63      noro     1109:        d = ndl_hash_value(dg);
1.13      noro     1110:        for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.34      noro     1111:                if ( ndl_equal(dg,DL(r)) ) {
1.1       noro     1112:                        if ( k > 0 ) nd_notfirst++;
                   1113:                        nd_found++;
1.13      noro     1114:                        return r->index;
1.1       noro     1115:                }
                   1116:        }
1.13      noro     1117:        if ( Reverse )
                   1118:                for ( i = nd_psn-1; i >= 0; i-- ) {
                   1119:                        r = nd_psh[i];
1.34      noro     1120:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro     1121:                                nd_create++;
1.34      noro     1122:                                nd_append_red(dg,i);
1.13      noro     1123:                                return i;
                   1124:                        }
                   1125:                }
                   1126:        else
                   1127:                for ( i = 0; i < nd_psn; i++ ) {
                   1128:                        r = nd_psh[i];
1.34      noro     1129:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro     1130:                                nd_create++;
1.34      noro     1131:                                nd_append_red(dg,i);
1.13      noro     1132:                                return i;
                   1133:                        }
1.1       noro     1134:                }
1.6       noro     1135:        return -1;
1.1       noro     1136: }
                   1137:
1.63      noro     1138: INLINE int ndl_find_reducer_direct(UINT *dg,NDV *ps,int len)
1.23      noro     1139: {
                   1140:        NDV r;
1.31      noro     1141:        RHist s;
                   1142:        int d,k,i;
1.23      noro     1143:
                   1144:        if ( Reverse )
                   1145:                for ( i = len-1; i >= 0; i-- ) {
                   1146:                        r = ps[i];
1.63      noro     1147:                        if ( ndl_reducible(dg,HDL(r)) )
1.23      noro     1148:                                return i;
                   1149:                }
                   1150:        else
                   1151:                for ( i = 0; i < len; i++ ) {
                   1152:                        r = ps[i];
1.63      noro     1153:                        if ( ndl_reducible(dg,HDL(r)) )
1.23      noro     1154:                                return i;
                   1155:                }
                   1156:        return -1;
                   1157: }
                   1158:
1.63      noro     1159: ND nd_merge(ND p1,ND p2)
                   1160: {
                   1161:        int n,c;
                   1162:        int t,can,td1,td2;
                   1163:        ND r;
                   1164:        NM m1,m2,mr0,mr,s;
                   1165:
                   1166:        if ( !p1 ) return p2;
                   1167:        else if ( !p2 ) return p1;
                   1168:        else {
                   1169:                can = 0;
                   1170:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1171:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1172:                        switch ( c ) {
                   1173:                                case 0:
                   1174:                                        s = m1; m1 = NEXT(m1);
                   1175:                                        can++; NEXTNM2(mr0,mr,s);
                   1176:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1177:                                        break;
                   1178:                                case 1:
                   1179:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1180:                                        break;
                   1181:                                case -1:
                   1182:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1183:                                        break;
                   1184:                        }
                   1185:                }
                   1186:                if ( !mr0 )
                   1187:                        if ( m1 ) mr0 = m1;
                   1188:                        else if ( m2 ) mr0 = m2;
                   1189:                        else return 0;
                   1190:                else if ( m1 ) NEXT(mr) = m1;
                   1191:                else if ( m2 ) NEXT(mr) = m2;
                   1192:                else NEXT(mr) = 0;
                   1193:                BDY(p1) = mr0;
                   1194:                SG(p1) = MAX(SG(p1),SG(p2));
                   1195:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1196:                FREEND(p2);
                   1197:                return p1;
                   1198:        }
                   1199: }
                   1200:
1.31      noro     1201: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1202: {
                   1203:        int n,c;
1.34      noro     1204:        int t,can,td1,td2;
1.1       noro     1205:        ND r;
                   1206:        NM m1,m2,mr0,mr,s;
                   1207:
1.34      noro     1208:        if ( !p1 ) return p2;
                   1209:        else if ( !p2 ) return p1;
                   1210:        else if ( !mod ) return nd_add_q(p1,p2);
1.1       noro     1211:        else {
1.30      noro     1212:                can = 0;
1.1       noro     1213:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro     1214:                        c = DL_COMPARE(DL(m1),DL(m2));
1.1       noro     1215:                        switch ( c ) {
                   1216:                                case 0:
1.19      noro     1217:                                        t = ((CM(m1))+(CM(m2))) - mod;
1.34      noro     1218:                                        if ( t < 0 ) t += mod;
1.1       noro     1219:                                        s = m1; m1 = NEXT(m1);
                   1220:                                        if ( t ) {
1.34      noro     1221:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1       noro     1222:                                        } else {
1.34      noro     1223:                                                can += 2; FREENM(s);
1.1       noro     1224:                                        }
                   1225:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1226:                                        break;
                   1227:                                case 1:
                   1228:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1229:                                        break;
                   1230:                                case -1:
                   1231:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1232:                                        break;
                   1233:                        }
                   1234:                }
                   1235:                if ( !mr0 )
1.34      noro     1236:                        if ( m1 ) mr0 = m1;
                   1237:                        else if ( m2 ) mr0 = m2;
                   1238:                        else return 0;
                   1239:                else if ( m1 ) NEXT(mr) = m1;
                   1240:                else if ( m2 ) NEXT(mr) = m2;
                   1241:                else NEXT(mr) = 0;
1.1       noro     1242:                BDY(p1) = mr0;
1.14      noro     1243:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1244:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.1       noro     1245:                FREEND(p2);
                   1246:                return p1;
                   1247:        }
                   1248: }
                   1249:
1.31      noro     1250: ND nd_add_q(ND p1,ND p2)
1.17      noro     1251: {
1.30      noro     1252:        int n,c,can;
1.17      noro     1253:        ND r;
                   1254:        NM m1,m2,mr0,mr,s;
                   1255:        Q t;
                   1256:
1.34      noro     1257:        if ( !p1 ) return p2;
                   1258:        else if ( !p2 ) return p1;
1.31      noro     1259:        else {
1.30      noro     1260:                can = 0;
1.17      noro     1261:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro     1262:                        c = DL_COMPARE(DL(m1),DL(m2));
1.17      noro     1263:                        switch ( c ) {
                   1264:                                case 0:
                   1265:                                        addq(CQ(m1),CQ(m2),&t);
                   1266:                                        s = m1; m1 = NEXT(m1);
                   1267:                                        if ( t ) {
1.34      noro     1268:                                                can++; NEXTNM2(mr0,mr,s); CQ(mr) = (t);
1.17      noro     1269:                                        } else {
1.34      noro     1270:                                                can += 2; FREENM(s);
1.17      noro     1271:                                        }
                   1272:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1273:                                        break;
                   1274:                                case 1:
                   1275:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1276:                                        break;
                   1277:                                case -1:
                   1278:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1279:                                        break;
                   1280:                        }
                   1281:                }
                   1282:                if ( !mr0 )
1.34      noro     1283:                        if ( m1 ) mr0 = m1;
                   1284:                        else if ( m2 ) mr0 = m2;
                   1285:                        else return 0;
                   1286:                else if ( m1 ) NEXT(mr) = m1;
                   1287:                else if ( m2 ) NEXT(mr) = m2;
                   1288:                else NEXT(mr) = 0;
1.17      noro     1289:                BDY(p1) = mr0;
                   1290:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1291:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.17      noro     1292:                FREEND(p2);
                   1293:                return p1;
                   1294:        }
                   1295: }
                   1296:
1.1       noro     1297: /* ret=1 : success, ret=0 : overflow */
1.53      noro     1298: int nd_nf(int mod,ND g,NDV *ps,int full,ND *rp)
1.1       noro     1299: {
1.11      noro     1300:        ND d;
1.1       noro     1301:        NM m,mrd,tail;
1.7       noro     1302:        NM mul;
1.10      noro     1303:        int n,sugar,psugar,sugar0,stat,index;
1.30      noro     1304:        int c,c1,c2,dummy;
1.17      noro     1305:        RHist h;
1.11      noro     1306:        NDV p,red;
1.16      noro     1307:        Q cg,cred,gcd;
1.21      noro     1308:        double hmag;
1.1       noro     1309:
                   1310:        if ( !g ) {
                   1311:                *rp = 0;
                   1312:                return 1;
                   1313:        }
1.34      noro     1314:        if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.21      noro     1315:
1.14      noro     1316:        sugar0 = sugar = SG(g);
1.1       noro     1317:        n = NV(g);
1.61      noro     1318:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro     1319:        for ( d = 0; g; ) {
1.63      noro     1320:                index = ndl_find_reducer(HDL(g));
1.6       noro     1321:                if ( index >= 0 ) {
1.17      noro     1322:                        h = nd_psh[index];
                   1323:                        ndl_sub(HDL(g),DL(h),DL(mul));
1.14      noro     1324:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.6       noro     1325:                                nd_free(g); nd_free(d);
                   1326:                                return 0;
                   1327:                        }
1.53      noro     1328:                        p = ps[index];
1.16      noro     1329:                        if ( mod ) {
1.19      noro     1330:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1331:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.16      noro     1332:                        } else {
1.17      noro     1333:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1.16      noro     1334:                                chsgnq(cg,&CQ(mul));
1.20      noro     1335:                                nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1.16      noro     1336:                        }
1.55      noro     1337:                        g = nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.34      noro     1338:                        sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.22      noro     1339:                        if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1.21      noro     1340:                                nd_removecont2(d,g);
                   1341:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1342:                        }
1.1       noro     1343:                } else if ( !full ) {
                   1344:                        *rp = g;
                   1345:                        return 1;
                   1346:                } else {
                   1347:                        m = BDY(g);
                   1348:                        if ( NEXT(m) ) {
1.34      noro     1349:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.1       noro     1350:                        } else {
                   1351:                                FREEND(g); g = 0;
                   1352:                        }
                   1353:                        if ( d ) {
1.34      noro     1354:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.1       noro     1355:                        } else {
1.34      noro     1356:                                MKND(n,m,1,d); tail = BDY(d);
1.1       noro     1357:                        }
                   1358:                }
                   1359:        }
1.34      noro     1360:        if ( d ) SG(d) = sugar;
1.1       noro     1361:        *rp = d;
                   1362:        return 1;
                   1363: }
1.28      noro     1364:
1.53      noro     1365: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1366: {
                   1367:        int hindex,index;
                   1368:        NDV p;
                   1369:        ND u,d,red;
                   1370:        NODE l;
1.31      noro     1371:        NM mul,m,mrd,tail;
1.25      noro     1372:        int sugar,psugar,n,h_reducible;
                   1373:        PGeoBucket bucket;
                   1374:        int c,c1,c2;
1.26      noro     1375:        Q cg,cred,gcd,zzz;
1.25      noro     1376:        RHist h;
1.28      noro     1377:        double hmag,gmag;
1.25      noro     1378:
                   1379:        if ( !g ) {
                   1380:                *rp = 0;
                   1381:                return 1;
                   1382:        }
                   1383:        sugar = SG(g);
                   1384:        n = NV(g);
1.34      noro     1385:        if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.25      noro     1386:        bucket = create_pbucket();
1.31      noro     1387:        add_pbucket(mod,bucket,g);
1.25      noro     1388:        d = 0;
1.61      noro     1389:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.25      noro     1390:        while ( 1 ) {
1.26      noro     1391:                hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.25      noro     1392:                if ( hindex < 0 ) {
1.34      noro     1393:                        if ( d ) SG(d) = sugar;
1.25      noro     1394:                        *rp = d;
                   1395:                        return 1;
                   1396:                }
                   1397:                g = bucket->body[hindex];
1.63      noro     1398:                index = ndl_find_reducer(HDL(g));
1.25      noro     1399:                if ( index >= 0 ) {
                   1400:                        h = nd_psh[index];
                   1401:                        ndl_sub(HDL(g),DL(h),DL(mul));
                   1402:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.26      noro     1403:                                nd_free(d);
1.25      noro     1404:                                free_pbucket(bucket);
                   1405:                                *rp = 0;
                   1406:                                return 0;
                   1407:                        }
1.53      noro     1408:                        p = ps[index];
1.25      noro     1409:                        if ( mod ) {
                   1410:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1411:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1412:                        } else {
                   1413:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1414:                                chsgnq(cg,&CQ(mul));
1.26      noro     1415:                                nd_mul_c_q(d,cred);
                   1416:                                mulq_pbucket(bucket,cred);
                   1417:                                g = bucket->body[hindex];
1.28      noro     1418:                                gmag = (double)p_mag((P)HCQ(g));
1.25      noro     1419:                        }
1.55      noro     1420:                        red = ndv_mul_nm(mod,mul,p);
1.25      noro     1421:                        bucket->body[hindex] = nd_remove_head(g);
                   1422:                        red = nd_remove_head(red);
1.31      noro     1423:                        add_pbucket(mod,bucket,red);
1.34      noro     1424:                        psugar = SG(p)+TD(DL(mul));
                   1425:                        sugar = MAX(sugar,psugar);
1.28      noro     1426:                        if ( !mod && hmag && (gmag > hmag) ) {
                   1427:                                g = normalize_pbucket(mod,bucket);
                   1428:                                if ( !g ) {
1.34      noro     1429:                                        if ( d ) SG(d) = sugar;
1.28      noro     1430:                                        *rp = d;
                   1431:                                        return 1;
                   1432:                                }
                   1433:                                nd_removecont2(d,g);
                   1434:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.31      noro     1435:                                add_pbucket(mod,bucket,g);
1.28      noro     1436:                        }
1.25      noro     1437:                } else if ( !full ) {
                   1438:                        g = normalize_pbucket(mod,bucket);
1.34      noro     1439:                        if ( g ) SG(g) = sugar;
1.25      noro     1440:                        *rp = g;
                   1441:                        return 1;
                   1442:                } else {
                   1443:                        m = BDY(g);
                   1444:                        if ( NEXT(m) ) {
1.34      noro     1445:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.25      noro     1446:                        } else {
                   1447:                                FREEND(g); g = 0;
                   1448:                        }
                   1449:                        bucket->body[hindex] = g;
                   1450:                        NEXT(m) = 0;
                   1451:                        if ( d ) {
1.34      noro     1452:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.25      noro     1453:                        } else {
1.34      noro     1454:                                MKND(n,m,1,d); tail = BDY(d);
1.25      noro     1455:                        }
                   1456:                }
                   1457:        }
                   1458: }
1.27      noro     1459:
1.45      noro     1460: int nd_nf_direct(int mod,ND g,BaseSet base,int full,ND *rp)
1.23      noro     1461: {
1.30      noro     1462:        ND d;
                   1463:        NM m,mrd,tail;
                   1464:        NM mul;
1.45      noro     1465:        NDV *ps;
                   1466:        int n,sugar,psugar,sugar0,stat,index,len;
1.31      noro     1467:        int c,c1,c2;
1.61      noro     1468:        UINT **bound;
1.30      noro     1469:        RHist h;
                   1470:        NDV p,red;
                   1471:        Q cg,cred,gcd;
                   1472:        double hmag;
                   1473:
                   1474:        if ( !g ) {
                   1475:                *rp = 0;
                   1476:                return 1;
                   1477:        }
                   1478: #if 0
                   1479:        if ( !mod )
                   1480:                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1481: #else
                   1482:        /* XXX */
                   1483:        hmag = 0;
                   1484: #endif
                   1485:
1.45      noro     1486:        ps = base->ps;
                   1487:        bound = base->bound;
                   1488:        len = base->len;
1.30      noro     1489:        sugar0 = sugar = SG(g);
                   1490:        n = NV(g);
1.61      noro     1491:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.30      noro     1492:        for ( d = 0; g; ) {
1.63      noro     1493:                index = ndl_find_reducer_direct(HDL(g),ps,len);
1.30      noro     1494:                if ( index >= 0 ) {
                   1495:                        p = ps[index];
                   1496:                        ndl_sub(HDL(g),HDL(p),DL(mul));
1.45      noro     1497:                        if ( ndl_check_bound2_direct(bound[index],DL(mul)) ) {
1.30      noro     1498:                                nd_free(g); nd_free(d);
                   1499:                                return 0;
                   1500:                        }
                   1501:                        if ( mod ) {
                   1502:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1503:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1504:                        } else {
                   1505:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1506:                                chsgnq(cg,&CQ(mul));
                   1507:                                nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
                   1508:                        }
1.55      noro     1509:                        g = nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.34      noro     1510:                        sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.30      noro     1511:                        if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
                   1512:                                nd_removecont2(d,g);
                   1513:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1514:                        }
                   1515:                } else if ( !full ) {
                   1516:                        *rp = g;
                   1517:                        return 1;
                   1518:                } else {
                   1519:                        m = BDY(g);
                   1520:                        if ( NEXT(m) ) {
1.34      noro     1521:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.30      noro     1522:                        } else {
                   1523:                                FREEND(g); g = 0;
                   1524:                        }
                   1525:                        if ( d ) {
1.34      noro     1526:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.30      noro     1527:                        } else {
1.34      noro     1528:                                MKND(n,m,1,d); tail = BDY(d);
1.30      noro     1529:                        }
                   1530:                }
                   1531:        }
1.34      noro     1532:        if ( d ) SG(d) = sugar;
1.30      noro     1533:        *rp = d;
                   1534:        return 1;
                   1535: }
                   1536:
1.61      noro     1537: /* input : list of NDV, cand : list of NDV */
1.28      noro     1538:
1.61      noro     1539: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1540: {
                   1541:        int n,i,stat;
                   1542:        ND nf,d;
1.61      noro     1543:        NDV r;
1.45      noro     1544:        NODE t,s;
                   1545:
1.61      noro     1546:        ndv_setup(0,0,cand);
1.31      noro     1547:        n = length(cand);
1.28      noro     1548:
                   1549:        /* membercheck : list is a subset of Id(cand) ? */
                   1550:        for ( t = input; t; t = NEXT(t) ) {
1.45      noro     1551: again:
1.61      noro     1552:                if ( nd_bpe > obpe )
                   1553:                        r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1554:                else
                   1555:                        r = (NDV)BDY(t);
                   1556:                d = ndvtond(0,r);
1.53      noro     1557:                stat = nd_nf(0,d,nd_ps,0,&nf);
1.45      noro     1558:                if ( !stat ) {
                   1559:                        nd_reconstruct(0,0,0);
                   1560:                        goto again;
                   1561:                } else if ( nf ) return 0;
                   1562:                printf("."); fflush(stdout);
1.28      noro     1563:        }
1.45      noro     1564:        printf("\n");
1.28      noro     1565:        /* gbcheck : cand is a GB of Id(cand) ? */
1.34      noro     1566:        if ( !nd_gb(0,1) ) return 0;
1.28      noro     1567:        /* XXX */
1.23      noro     1568:        return 1;
                   1569: }
1.1       noro     1570:
                   1571: ND nd_remove_head(ND p)
                   1572: {
                   1573:        NM m;
                   1574:
                   1575:        m = BDY(p);
                   1576:        if ( !NEXT(m) ) {
1.34      noro     1577:                FREEND(p); p = 0;
1.31      noro     1578:        } else {
1.34      noro     1579:                BDY(p) = NEXT(m); LEN(p)--;
1.31      noro     1580:        }
1.1       noro     1581:        FREENM(m);
                   1582:        return p;
                   1583: }
                   1584:
                   1585: PGeoBucket create_pbucket()
                   1586: {
                   1587:     PGeoBucket g;
                   1588:
                   1589:        g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1590:        g->m = -1;
                   1591:        return g;
                   1592: }
                   1593:
1.25      noro     1594: void free_pbucket(PGeoBucket b) {
                   1595:        int i;
                   1596:
1.26      noro     1597:        for ( i = 0; i <= b->m; i++ )
1.25      noro     1598:                if ( b->body[i] ) {
                   1599:                        nd_free(b->body[i]);
                   1600:                        b->body[i] = 0;
                   1601:                }
                   1602:        GC_free(b);
                   1603: }
                   1604:
1.63      noro     1605: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1606: {
                   1607:        int l,i,k,m;
                   1608:
                   1609:        if ( !d )
                   1610:                return;
                   1611:        l = LEN(d);
                   1612:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1613:        /* 2^(k-1) < l <= 2^k (=m) */
                   1614:        d = nd_merge(g->body[k],d);
                   1615:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1616:                g->body[k] = 0;
                   1617:                d = nd_merge(g->body[k+1],d);
                   1618:        }
                   1619:        g->body[k] = d;
                   1620:        g->m = MAX(g->m,k);
                   1621: }
                   1622:
1.31      noro     1623: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1624: {
1.31      noro     1625:        int l,i,k,m;
1.1       noro     1626:
1.31      noro     1627:        if ( !d )
                   1628:                return;
                   1629:        l = LEN(d);
1.29      noro     1630:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1631:        /* 2^(k-1) < l <= 2^k (=m) */
1.31      noro     1632:        d = nd_add(mod,g->body[k],d);
                   1633:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1.1       noro     1634:                g->body[k] = 0;
1.31      noro     1635:                d = nd_add(mod,g->body[k+1],d);
1.1       noro     1636:        }
                   1637:        g->body[k] = d;
                   1638:        g->m = MAX(g->m,k);
                   1639: }
                   1640:
1.26      noro     1641: void mulq_pbucket(PGeoBucket g,Q c)
                   1642: {
                   1643:        int k;
                   1644:
                   1645:        for ( k = 0; k <= g->m; k++ )
                   1646:                nd_mul_c_q(g->body[k],c);
                   1647: }
                   1648:
1.63      noro     1649: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1650: {
                   1651:        int j,i,k,c;
                   1652:        NM head;
                   1653:
                   1654:        k = g->m;
                   1655:        j = -1;
                   1656:        for ( i = 0; i <= k; i++ ) {
                   1657:                if ( !g->body[i] ) continue;
                   1658:                if ( j < 0 ) j = i;
                   1659:                else {
                   1660:                        c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1661:                        if ( c > 0 )
                   1662:                                j = i;
                   1663:                        else if ( c == 0 )
                   1664:                                g->body[i] = nd_remove_head(g->body[i]);
                   1665:                }
                   1666:        }
                   1667:        if ( j < 0 ) return 0;
                   1668:        else {
                   1669:                head = BDY(g->body[j]);
                   1670:                if ( !NEXT(head) ) {
                   1671:                        FREEND(g->body[j]);
                   1672:                        g->body[j] = 0;
                   1673:                } else {
                   1674:                        BDY(g->body[j]) = NEXT(head);
                   1675:                        LEN(g->body[j])--;
                   1676:                }
                   1677:                return head;
                   1678:        }
                   1679: }
                   1680:
1.19      noro     1681: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1682: {
                   1683:        int j,i,c,k,nv,sum;
1.61      noro     1684:        UINT *di,*dj;
1.1       noro     1685:        ND gi,gj;
                   1686:
                   1687:        k = g->m;
                   1688:        while ( 1 ) {
                   1689:                j = -1;
                   1690:                for ( i = 0; i <= k; i++ ) {
                   1691:                        if ( !(gi = g->body[i]) )
                   1692:                                continue;
                   1693:                        if ( j < 0 ) {
                   1694:                                j = i;
                   1695:                                gj = g->body[j];
                   1696:                                dj = HDL(gj);
1.14      noro     1697:                                sum = HCM(gj);
1.1       noro     1698:                        } else {
1.34      noro     1699:                                c = DL_COMPARE(HDL(gi),dj);
1.1       noro     1700:                                if ( c > 0 ) {
1.34      noro     1701:                                        if ( sum ) HCM(gj) = sum;
                   1702:                                        else g->body[j] = nd_remove_head(gj);
1.1       noro     1703:                                        j = i;
                   1704:                                        gj = g->body[j];
                   1705:                                        dj = HDL(gj);
1.14      noro     1706:                                        sum = HCM(gj);
1.1       noro     1707:                                } else if ( c == 0 ) {
1.19      noro     1708:                                        sum = sum+HCM(gi)-mod;
1.34      noro     1709:                                        if ( sum < 0 ) sum += mod;
1.1       noro     1710:                                        g->body[i] = nd_remove_head(gi);
                   1711:                                }
                   1712:                        }
                   1713:                }
1.34      noro     1714:                if ( j < 0 ) return -1;
1.1       noro     1715:                else if ( sum ) {
1.14      noro     1716:                        HCM(gj) = sum;
1.26      noro     1717:                        return j;
1.31      noro     1718:                } else
1.26      noro     1719:                        g->body[j] = nd_remove_head(gj);
                   1720:        }
                   1721: }
                   1722:
                   1723: int head_pbucket_q(PGeoBucket g)
                   1724: {
                   1725:        int j,i,c,k,nv;
                   1726:        Q sum,t;
                   1727:        ND gi,gj;
                   1728:
                   1729:        k = g->m;
                   1730:        while ( 1 ) {
                   1731:                j = -1;
                   1732:                for ( i = 0; i <= k; i++ ) {
1.34      noro     1733:                        if ( !(gi = g->body[i]) ) continue;
1.26      noro     1734:                        if ( j < 0 ) {
                   1735:                                j = i;
                   1736:                                gj = g->body[j];
                   1737:                                sum = HCQ(gj);
                   1738:                        } else {
                   1739:                                nv = NV(gi);
1.34      noro     1740:                                c = DL_COMPARE(HDL(gi),HDL(gj));
1.26      noro     1741:                                if ( c > 0 ) {
1.34      noro     1742:                                        if ( sum ) HCQ(gj) = sum;
                   1743:                                        else g->body[j] = nd_remove_head(gj);
1.26      noro     1744:                                        j = i;
                   1745:                                        gj = g->body[j];
                   1746:                                        sum = HCQ(gj);
                   1747:                                } else if ( c == 0 ) {
                   1748:                                        addq(sum,HCQ(gi),&t);
                   1749:                                        sum = t;
                   1750:                                        g->body[i] = nd_remove_head(gi);
                   1751:                                }
                   1752:                        }
                   1753:                }
1.34      noro     1754:                if ( j < 0 ) return -1;
1.26      noro     1755:                else if ( sum ) {
                   1756:                        HCQ(gj) = sum;
1.1       noro     1757:                        return j;
1.31      noro     1758:                } else
1.1       noro     1759:                        g->body[j] = nd_remove_head(gj);
                   1760:        }
                   1761: }
                   1762:
1.25      noro     1763: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1764: {
1.31      noro     1765:        int i;
1.1       noro     1766:        ND r,t;
                   1767:
                   1768:        r = 0;
1.28      noro     1769:        for ( i = 0; i <= g->m; i++ ) {
1.31      noro     1770:                r = nd_add(mod,r,g->body[i]);
1.28      noro     1771:                g->body[i] = 0;
                   1772:        }
                   1773:        g->m = -1;
1.1       noro     1774:        return r;
                   1775: }
                   1776:
1.27      noro     1777: /* return value = 0 => input is not a GB */
                   1778:
                   1779: NODE nd_gb(int m,int checkonly)
1.1       noro     1780: {
                   1781:        int i,nh,sugar,stat;
1.23      noro     1782:        NODE r,g,t;
1.1       noro     1783:        ND_pairs d;
                   1784:        ND_pairs l;
                   1785:        ND h,nf;
1.63      noro     1786:        NDV nfv;
1.1       noro     1787:
1.23      noro     1788:        g = 0; d = 0;
                   1789:        for ( i = 0; i < nd_psn; i++ ) {
1.1       noro     1790:                d = update_pairs(d,g,i);
                   1791:                g = update_base(g,i);
                   1792:        }
                   1793:        sugar = 0;
                   1794:        while ( d ) {
                   1795: again:
                   1796:                l = nd_minp(d,&d);
1.14      noro     1797:                if ( SG(l) != sugar ) {
                   1798:                        sugar = SG(l);
1.1       noro     1799:                        fprintf(asir_out,"%d",sugar);
                   1800:                }
1.53      noro     1801:                stat = nd_sp(m,0,l,&h);
1.1       noro     1802:                if ( !stat ) {
                   1803:                        NEXT(l) = d; d = l;
1.20      noro     1804:                        d = nd_reconstruct(m,0,d);
1.1       noro     1805:                        goto again;
                   1806:                }
1.41      noro     1807: #if USE_GEOBUCKET
1.53      noro     1808:                stat = m?nd_nf_pbucket(m,h,nd_ps,!Top,&nf):nd_nf(m,h,nd_ps,!Top,&nf);
1.41      noro     1809: #else
1.53      noro     1810:                stat = nd_nf(m,h,nd_ps,!Top,&nf);
1.41      noro     1811: #endif
1.1       noro     1812:                if ( !stat ) {
                   1813:                        NEXT(l) = d; d = l;
1.20      noro     1814:                        d = nd_reconstruct(m,0,d);
1.1       noro     1815:                        goto again;
                   1816:                } else if ( nf ) {
1.27      noro     1817:                        if ( checkonly ) return 0;
1.1       noro     1818:                        printf("+"); fflush(stdout);
1.63      noro     1819:                        nd_removecont(m,nf);
                   1820:                        nfv = ndtondv(m,nf); nd_free(nf);
                   1821:                        nh = ndv_newps(nfv,0);
1.1       noro     1822:                        d = update_pairs(d,g,nh);
                   1823:                        g = update_base(g,nh);
                   1824:                        FREENDP(l);
                   1825:                } else {
                   1826:                        printf("."); fflush(stdout);
                   1827:                        FREENDP(l);
                   1828:                }
                   1829:        }
1.53      noro     1830:        for ( t = g; t; t = NEXT(t) ) BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.1       noro     1831:        return g;
                   1832: }
                   1833:
1.23      noro     1834: NODE nd_gb_trace(int m)
1.20      noro     1835: {
                   1836:        int i,nh,sugar,stat;
1.23      noro     1837:        NODE r,g,t;
1.20      noro     1838:        ND_pairs d;
                   1839:        ND_pairs l;
                   1840:        ND h,nf,nfq;
1.63      noro     1841:        NDV nfv,nfqv;
1.20      noro     1842:
1.23      noro     1843:        g = 0; d = 0;
                   1844:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     1845:                d = update_pairs(d,g,i);
                   1846:                g = update_base(g,i);
                   1847:        }
                   1848:        sugar = 0;
                   1849:        while ( d ) {
                   1850: again:
                   1851:                l = nd_minp(d,&d);
                   1852:                if ( SG(l) != sugar ) {
                   1853:                        sugar = SG(l);
                   1854:                        fprintf(asir_out,"%d",sugar);
                   1855:                }
1.53      noro     1856:                stat = nd_sp(m,0,l,&h);
1.20      noro     1857:                if ( !stat ) {
                   1858:                        NEXT(l) = d; d = l;
                   1859:                        d = nd_reconstruct(m,1,d);
                   1860:                        goto again;
                   1861:                }
1.41      noro     1862: #if USE_GEOBUCKET
1.53      noro     1863:                stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     1864: #else
1.53      noro     1865:                stat = nd_nf(m,h,nd_ps,!Top,&nf);
1.41      noro     1866: #endif
1.20      noro     1867:                if ( !stat ) {
                   1868:                        NEXT(l) = d; d = l;
                   1869:                        d = nd_reconstruct(m,1,d);
                   1870:                        goto again;
                   1871:                } else if ( nf ) {
                   1872:                        /* overflow does not occur */
1.53      noro     1873:                        nd_sp(0,1,l,&h);
                   1874:                        nd_nf(0,h,nd_ps_trace,!Top,&nfq);
1.20      noro     1875:                        if ( nfq ) {
1.63      noro     1876:                                /* failure; m|HC(nfq) */
                   1877:                                if ( !rem(NM(HCQ(nfq)),m) ) return 0;
                   1878:
1.20      noro     1879:                                printf("+"); fflush(stdout);
1.63      noro     1880:                                nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   1881:                                nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
                   1882:                                nh = ndv_newps(nfv,nfqv);
1.20      noro     1883:                                d = update_pairs(d,g,nh);
                   1884:                                g = update_base(g,nh);
                   1885:                        } else {
                   1886:                                printf("*"); fflush(stdout);
                   1887:                        }
                   1888:                } else {
                   1889:                        printf("."); fflush(stdout);
                   1890:                }
                   1891:                FREENDP(l);
                   1892:        }
1.23      noro     1893:        for ( t = g; t; t = NEXT(t) )
1.53      noro     1894:                BDY(t) = (pointer)nd_ps_trace[(int)BDY(t)];
1.20      noro     1895:        return g;
                   1896: }
                   1897:
1.23      noro     1898: int ndv_compare(NDV *p1,NDV *p2)
                   1899: {
1.34      noro     1900:        return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1901: }
                   1902:
                   1903: int ndv_compare_rev(NDV *p1,NDV *p2)
                   1904: {
1.34      noro     1905:        return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1906: }
                   1907:
1.61      noro     1908: NODE ndv_reduceall(int m,NODE f)
1.23      noro     1909: {
                   1910:        int i,j,n,stat;
                   1911:        NDV *w,*ps;
                   1912:        ND nf,g;
                   1913:        NODE t,a0,a;
1.45      noro     1914:        struct oBaseSet base;
1.61      noro     1915:        UINT **bound;
1.23      noro     1916:
                   1917:        for ( n = 0, t = f; t; t = NEXT(t), n++ );
                   1918:        ps = (NDV *)ALLOCA(n*sizeof(NDV));
1.61      noro     1919:        bound = (UINT **)ALLOCA(n*sizeof(UINT *));
1.54      noro     1920:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) ps[i] = (NDV)BDY(t);
1.23      noro     1921:        qsort(ps,n,sizeof(NDV),(int (*)(const void *,const void *))ndv_compare);
1.54      noro     1922:        for ( i = 0; i < n; i++ ) bound[i] = ndv_compute_bound(ps[i]);
1.45      noro     1923:        base.ps = (NDV *)ALLOCA((n-1)*sizeof(NDV));
1.61      noro     1924:        base.bound = (UINT **)ALLOCA((n-1)*sizeof(UINT *));
1.45      noro     1925:        base.len = n-1;
1.50      noro     1926:        i = 0;
                   1927:        while ( i < n ) {
1.45      noro     1928:                for ( j = 0; j < i; j++ ) {
                   1929:                        base.ps[j] = ps[j]; base.bound[j] = bound[j];
                   1930:                }
                   1931:                for ( j = i+1; j < n; j++ ) {
                   1932:                        base.ps[j-1] = ps[j]; base.bound[j-1] = bound[j];
                   1933:                }
1.23      noro     1934:                g = ndvtond(m,ps[i]);
1.45      noro     1935:                stat = nd_nf_direct(m,g,&base,1,&nf);
1.50      noro     1936:                if ( !stat )
1.23      noro     1937:                        nd_reconstruct_direct(m,ps,n);
                   1938:                else if ( !nf ) {
                   1939:                        printf("."); fflush(stdout);
                   1940:                        ndv_free(ps[i]);
1.50      noro     1941:                        for ( j = i+1; j < n; j++ ) {
                   1942:                                ps[j-1] = ps[j]; bound[j-1] = bound[j];
                   1943:                        }
1.23      noro     1944:                        n--;
1.50      noro     1945:                        base.len = n-1;
1.23      noro     1946:                } else {
                   1947:                        printf("."); fflush(stdout);
                   1948:                        ndv_free(ps[i]);
1.24      noro     1949:                        nd_removecont(m,nf);
1.23      noro     1950:                        ps[i] = ndtondv(m,nf);
1.45      noro     1951:                        bound[i] = ndv_compute_bound(ps[i]);
1.23      noro     1952:                        nd_free(nf);
1.50      noro     1953:                        i++;
1.23      noro     1954:                }
                   1955:        }
1.45      noro     1956:        printf("\n");
1.23      noro     1957:        for ( a0 = 0, i = 0; i < n; i++ ) {
                   1958:                NEXTNODE(a0,a);
                   1959:                BDY(a) = (pointer)ps[i];
                   1960:        }
                   1961:        NEXT(a) = 0;
                   1962:        return a0;
                   1963: }
                   1964:
1.1       noro     1965: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   1966: {
                   1967:        ND_pairs d1,nd,cur,head,prev,remove;
                   1968:
                   1969:        if ( !g ) return d;
                   1970:        d = crit_B(d,t);
                   1971:        d1 = nd_newpairs(g,t);
                   1972:        d1 = crit_M(d1);
                   1973:        d1 = crit_F(d1);
1.55      noro     1974:        if ( do_weyl )
                   1975:                head = d1;
                   1976:        else {
                   1977:                prev = 0; cur = head = d1;
                   1978:                while ( cur ) {
                   1979:                        if ( crit_2( cur->i1,cur->i2 ) ) {
                   1980:                                remove = cur;
                   1981:                                if ( !prev ) head = cur = NEXT(cur);
                   1982:                                else cur = NEXT(prev) = NEXT(cur);
                   1983:                                FREENDP(remove);
                   1984:                        } else {
                   1985:                                prev = cur; cur = NEXT(cur);
                   1986:                        }
1.1       noro     1987:                }
                   1988:        }
                   1989:        if ( !d )
                   1990:                return head;
                   1991:        else {
                   1992:                nd = d;
1.34      noro     1993:                while ( NEXT(nd) ) nd = NEXT(nd);
1.1       noro     1994:                NEXT(nd) = head;
                   1995:                return d;
                   1996:        }
                   1997: }
                   1998:
                   1999: ND_pairs nd_newpairs( NODE g, int t )
                   2000: {
                   2001:        NODE h;
1.61      noro     2002:        UINT *dl;
1.34      noro     2003:        int ts,s;
1.1       noro     2004:        ND_pairs r,r0;
                   2005:
1.20      noro     2006:        dl = DL(nd_psh[t]);
1.34      noro     2007:        ts = SG(nd_psh[t]) - TD(dl);
1.1       noro     2008:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   2009:                NEXTND_pairs(r0,r);
                   2010:                r->i1 = (int)BDY(h);
                   2011:                r->i2 = t;
1.20      noro     2012:                ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34      noro     2013:                s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2014:                SG(r) = MAX(s,ts) + TD(LCM(r));
1.1       noro     2015:        }
                   2016:        NEXT(r) = 0;
                   2017:        return r0;
                   2018: }
                   2019:
                   2020: ND_pairs crit_B( ND_pairs d, int s )
                   2021: {
                   2022:        ND_pairs cur,head,prev,remove;
1.61      noro     2023:        UINT *t,*tl,*lcm;
1.1       noro     2024:        int td,tdl;
                   2025:
                   2026:        if ( !d ) return 0;
1.20      noro     2027:        t = DL(nd_psh[s]);
1.1       noro     2028:        prev = 0;
                   2029:        head = cur = d;
1.61      noro     2030:        lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.1       noro     2031:        while ( cur ) {
                   2032:                tl = cur->lcm;
                   2033:                if ( ndl_reducible(tl,t)
1.20      noro     2034:                        && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   2035:                        && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1       noro     2036:                        remove = cur;
                   2037:                        if ( !prev ) {
                   2038:                                head = cur = NEXT(cur);
                   2039:                        } else {
                   2040:                                cur = NEXT(prev) = NEXT(cur);
                   2041:                        }
                   2042:                        FREENDP(remove);
                   2043:                } else {
1.34      noro     2044:                        prev = cur; cur = NEXT(cur);
1.1       noro     2045:                }
                   2046:        }
                   2047:        return head;
                   2048: }
                   2049:
                   2050: ND_pairs crit_M( ND_pairs d1 )
                   2051: {
                   2052:        ND_pairs e,d2,d3,dd,p;
1.61      noro     2053:        UINT *id,*jd;
1.1       noro     2054:
                   2055:        for ( dd = 0, e = d1; e; e = d3 ) {
                   2056:                if ( !(d2 = NEXT(e)) ) {
                   2057:                        NEXT(e) = dd;
                   2058:                        return e;
                   2059:                }
1.34      noro     2060:                id = LCM(e);
1.1       noro     2061:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     2062:                        p = NEXT(d2);
                   2063:                        jd = LCM(d2);
                   2064:                        if ( ndl_equal(jd,id) )
                   2065:                                ;
                   2066:                        else if ( TD(jd) > TD(id) )
1.1       noro     2067:                                if ( ndl_reducible(jd,id) ) continue;
                   2068:                                else ;
1.34      noro     2069:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     2070:                        NEXT(d2) = d3;
                   2071:                        d3 = d2;
                   2072:                }
                   2073:                NEXT(e) = dd;
                   2074:                dd = e;
                   2075:                continue;
                   2076:                /**/
                   2077:        delit:  NEXT(d2) = d3;
                   2078:                d3 = d2;
                   2079:                for ( ; p; p = d2 ) {
                   2080:                        d2 = NEXT(p);
                   2081:                        NEXT(p) = d3;
                   2082:                        d3 = p;
                   2083:                }
                   2084:                FREENDP(e);
                   2085:        }
                   2086:        return dd;
                   2087: }
                   2088:
                   2089: ND_pairs crit_F( ND_pairs d1 )
                   2090: {
                   2091:        ND_pairs rest, head,remove;
                   2092:        ND_pairs last, p, r, w;
                   2093:        int s;
                   2094:
                   2095:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   2096:                r = w = equivalent_pairs(p,&rest);
1.14      noro     2097:                s = SG(r);
1.1       noro     2098:                w = NEXT(w);
                   2099:                while ( w ) {
                   2100:                        if ( crit_2(w->i1,w->i2) ) {
                   2101:                                r = w;
                   2102:                                w = NEXT(w);
                   2103:                                while ( w ) {
                   2104:                                        remove = w;
                   2105:                                        w = NEXT(w);
                   2106:                                        FREENDP(remove);
                   2107:                                }
                   2108:                                break;
1.14      noro     2109:                        } else if ( SG(w) < s ) {
1.1       noro     2110:                                FREENDP(r);
                   2111:                                r = w;
1.14      noro     2112:                                s = SG(r);
1.1       noro     2113:                                w = NEXT(w);
                   2114:                        } else {
                   2115:                                remove = w;
                   2116:                                w = NEXT(w);
                   2117:                                FREENDP(remove);
                   2118:                        }
                   2119:                }
                   2120:                if ( last ) NEXT(last) = r;
                   2121:                else head = r;
                   2122:                NEXT(last = r) = 0;
                   2123:                p = rest;
                   2124:                if ( !p ) return head;
                   2125:        }
                   2126:        if ( !last ) return p;
                   2127:        NEXT(last) = p;
                   2128:        return head;
                   2129: }
                   2130:
                   2131: int crit_2( int dp1, int dp2 )
                   2132: {
1.20      noro     2133:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2134: }
                   2135:
1.40      noro     2136: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2137: {
                   2138:        ND_pairs w,p,r,s;
1.61      noro     2139:        UINT *d;
1.1       noro     2140:
                   2141:        w = d1;
1.34      noro     2142:        d = LCM(w);
1.1       noro     2143:        s = NEXT(w);
                   2144:        NEXT(w) = 0;
                   2145:        for ( r = 0; s; s = p ) {
                   2146:                p = NEXT(s);
1.34      noro     2147:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     2148:                        NEXT(s) = w; w = s;
1.1       noro     2149:                } else {
1.39      noro     2150:                        NEXT(s) = r; r = s;
1.1       noro     2151:                }
                   2152:        }
                   2153:        *prest = r;
                   2154:        return w;
                   2155: }
                   2156:
                   2157: NODE update_base(NODE nd,int ndp)
                   2158: {
1.61      noro     2159:        UINT *dl, *dln;
1.1       noro     2160:        NODE last, p, head;
                   2161:
1.20      noro     2162:        dl = DL(nd_psh[ndp]);
1.1       noro     2163:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     2164:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     2165:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     2166:                        p = NEXT(p);
                   2167:                        if ( last ) NEXT(last) = p;
                   2168:                } else {
                   2169:                        if ( !last ) head = p;
                   2170:                        p = NEXT(last = p);
                   2171:                }
                   2172:        }
                   2173:        head = append_one(head,ndp);
                   2174:        return head;
                   2175: }
                   2176:
                   2177: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2178: {
                   2179:        ND_pairs m,ml,p,l;
1.61      noro     2180:        UINT *lcm;
1.33      noro     2181:        int s,td,len,tlen,c,c1;
1.1       noro     2182:
                   2183:        if ( !(p = NEXT(m = d)) ) {
                   2184:                *prest = p;
                   2185:                NEXT(m) = 0;
                   2186:                return m;
                   2187:        }
1.14      noro     2188:        s = SG(m);
1.33      noro     2189:        for ( ml = 0, l = m; p; p = NEXT(l = p) )
1.34      noro     2190:                if ( (SG(p) < s)
                   2191:                        || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
1.39      noro     2192:                        ml = l; m = p; s = SG(m);
1.1       noro     2193:                }
                   2194:        if ( !ml ) *prest = NEXT(m);
                   2195:        else {
                   2196:                NEXT(ml) = NEXT(m);
                   2197:                *prest = d;
                   2198:        }
                   2199:        NEXT(m) = 0;
                   2200:        return m;
                   2201: }
                   2202:
1.63      noro     2203: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2204: {
                   2205:        int msugar;
                   2206:        ND_pairs t,dm0,dm,dr0,dr;
                   2207:
                   2208:        for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2209:                if ( SG(t) < msugar ) msugar = SG(t);
                   2210:        dm0 = 0; dr0 = 0;
                   2211:        for ( t = d; t; t = NEXT(t) )
                   2212:                if ( SG(t) == msugar ) {
                   2213:                        if ( dm0 ) NEXT(dm) = t;
                   2214:                        else dm0 = t;
                   2215:                        dm = t;
                   2216:                } else {
                   2217:                        if ( dr0 ) NEXT(dr) = t;
                   2218:                        else dr0 = t;
                   2219:                        dr = t;
                   2220:                }
                   2221:        NEXT(dm) = 0;
                   2222:        if ( dr0 ) NEXT(dr) = 0;
                   2223:        *prest = dr0;
                   2224:        return dm0;
                   2225: }
                   2226:
                   2227: int ndv_newps(NDV a,NDV aq)
1.1       noro     2228: {
1.3       noro     2229:        int len;
1.13      noro     2230:        RHist r;
1.20      noro     2231:        NDV b;
1.3       noro     2232:
1.1       noro     2233:        if ( nd_psn == nd_pslen ) {
                   2234:                nd_pslen *= 2;
1.11      noro     2235:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     2236:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     2237:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.61      noro     2238:                nd_bound = (UINT **)
                   2239:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.1       noro     2240:        }
1.39      noro     2241:        NEWRHist(r); nd_psh[nd_psn] = r;
1.63      noro     2242:        nd_ps[nd_psn] = a;
1.39      noro     2243:        if ( aq ) {
1.63      noro     2244:                nd_ps_trace[nd_psn] = aq;
                   2245:                nd_bound[nd_psn] = ndv_compute_bound(aq);
1.39      noro     2246:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     2247:        } else {
1.63      noro     2248:                nd_bound[nd_psn] = ndv_compute_bound(a);
1.53      noro     2249:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.39      noro     2250:        }
1.1       noro     2251:        return nd_psn++;
                   2252: }
                   2253:
1.61      noro     2254: void ndv_setup(int mod,int trace,NODE f)
1.1       noro     2255: {
1.5       noro     2256:        int i,j,td,len,max;
1.1       noro     2257:        NODE s,s0,f0;
1.61      noro     2258:        UINT *d;
1.13      noro     2259:        RHist r;
1.61      noro     2260:        NDV a,am;
1.11      noro     2261:
                   2262:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     2263:
                   2264:        nd_psn = length(f); nd_pslen = 2*nd_psn;
1.11      noro     2265:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     2266:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     2267:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.61      noro     2268:        nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
1.57      noro     2269:
1.11      noro     2270:        if ( !nd_red )
1.13      noro     2271:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   2272:        bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.1       noro     2273:        nd_free_private_storage();
                   2274:        for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.39      noro     2275:                if ( trace ) {
1.61      noro     2276:                        a = nd_ps_trace[i] = ndv_dup(0,(NDV)BDY(f));
                   2277:                        ndv_removecont(0,a);
                   2278:                        am = nd_ps[i] = ndv_dup(mod,a);
                   2279:                        ndv_mod(mod,am);
                   2280:                        ndv_removecont(mod,am);
                   2281:                } else {
                   2282:                        a = nd_ps[i] = ndv_dup(mod,(NDV)BDY(f));
                   2283:                        if ( mod ) ndv_mod(mod,a);
                   2284:                        ndv_removecont(mod,a);
1.39      noro     2285:                }
1.61      noro     2286:                NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.57      noro     2287:                nd_bound[i] = ndv_compute_bound(a);
1.20      noro     2288:                nd_psh[i] = r;
                   2289:        }
                   2290: }
                   2291:
1.63      noro     2292: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2293: {
1.61      noro     2294:        VL tv,fv,vv,vc;
1.1       noro     2295:        NODE fd,fd0,r,r0,t,x,s,xx;
1.61      noro     2296:        int e,max,nvar;
                   2297:        ND b;
1.1       noro     2298:
                   2299:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.61      noro     2300:        nvar = length(vv);
1.32      noro     2301:        nd_init_ord(ord);
1.61      noro     2302:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2303:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2304:                        e = getdeg(tv->v,(P)BDY(t));
                   2305:                        max = MAX(e,max);
                   2306:                }
                   2307:        nd_setup_parameters(nvar,max);
1.1       noro     2308:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61      noro     2309:                b = (pointer)ptondv(CO,vv,(P)BDY(t));
                   2310:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1       noro     2311:        }
                   2312:        if ( fd0 ) NEXT(fd) = 0;
1.61      noro     2313:        ndv_setup(m,0,fd0);
1.63      noro     2314:        x = f4?nd_f4(m):nd_gb(m,0);
1.61      noro     2315:        x = ndv_reducebase(x);
                   2316:        x = ndv_reduceall(m,x);
1.23      noro     2317:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2318:                NEXTNODE(r0,r);
1.61      noro     2319:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20      noro     2320:        }
                   2321:        if ( r0 ) NEXT(r) = 0;
                   2322:        MKLIST(*rp,r0);
                   2323: }
                   2324:
1.52      noro     2325: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20      noro     2326: {
                   2327:        struct order_spec ord1;
1.61      noro     2328:        VL tv,fv,vv,vc;
1.27      noro     2329:        NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.61      noro     2330:        int m,nocheck,nvar,mindex,e,max;
                   2331:        NDV c;
                   2332:        NMV a;
1.27      noro     2333:        P p;
1.61      noro     2334:        EPOS oepos;
                   2335:        int obpe,oadv,wmax,i,len,cbpe;
1.20      noro     2336:
                   2337:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.52      noro     2338:        nvar = length(vv);
                   2339:        nocheck = 0;
                   2340:        mindex = 0;
                   2341:
                   2342:        /* setup modulus */
                   2343:        if ( trace < 0 ) {
                   2344:                trace = -trace;
                   2345:                nocheck = 1;
                   2346:        }
                   2347:        m = trace > 1 ? trace : get_lprime(mindex);
1.61      noro     2348:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2349:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2350:                        e = getdeg(tv->v,(P)BDY(t));
                   2351:                        max = MAX(e,max);
1.23      noro     2352:                }
1.61      noro     2353:        nd_init_ord(ord);
                   2354:        nd_setup_parameters(nvar,max);
                   2355:        obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
                   2356:        for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2357:                c = ptondv(CO,vv,(P)BDY(t));
                   2358:                if ( c ) {
                   2359:                        NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   2360:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23      noro     2361:                }
1.61      noro     2362:        }
                   2363:        if ( in0 ) NEXT(in) = 0;
                   2364:        if ( fd0 ) NEXT(fd) = 0;
                   2365:        if ( homo ) {
                   2366:                for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
                   2367:                        c = (NDV)BDY(t); len = LEN(c);
                   2368:                        for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2369:                                wmax = MAX(TD(DL(a)),wmax);
                   2370:                }
                   2371:                homogenize_order(ord,nvar,&ord1);
                   2372:                nd_init_ord(&ord1);
                   2373:                nd_setup_parameters(nvar+1,wmax);
                   2374:                for ( t = fd0; t; t = NEXT(t) )
                   2375:                        ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27      noro     2376:        }
1.52      noro     2377:        while ( 1 ) {
1.61      noro     2378:                ndv_setup(m,1,fd0);
1.27      noro     2379:                cand = nd_gb_trace(m);
1.52      noro     2380:                if ( !cand ) {
                   2381:                        /* failure */
1.61      noro     2382:                        if ( trace > 1 ) { *rp = 0; return; }
                   2383:                        else m = get_lprime(++mindex);
1.52      noro     2384:                        continue;
                   2385:                }
1.27      noro     2386:                if ( homo ) {
                   2387:                        /* dehomogenization */
1.61      noro     2388:                        for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45      noro     2389:                        nd_init_ord(ord);
1.61      noro     2390:                        nd_setup_parameters(nvar,0);
1.27      noro     2391:                }
1.61      noro     2392:                cand = ndv_reducebase(cand);
                   2393:                cand = ndv_reduceall(0,cand);
                   2394:                if ( nocheck )
                   2395:                        break;
                   2396:                cbpe = nd_bpe;
                   2397:                if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52      noro     2398:                        /* success */
                   2399:                        break;
                   2400:                else if ( trace > 1 ) {
                   2401:                        /* failure */
                   2402:                        *rp = 0; return;
1.61      noro     2403:                } else {
1.52      noro     2404:                        /* try the next modulus */
                   2405:                        m = get_lprime(++mindex);
1.61      noro     2406:                        /* reset the parameters */
                   2407:                        if ( homo ) {
                   2408:                                nd_init_ord(&ord1);
                   2409:                                nd_setup_parameters(nvar+1,wmax);
                   2410:                        } else {
                   2411:                                nd_init_ord(ord);
                   2412:                                nd_setup_parameters(nvar,max);
                   2413:                        }
                   2414:                }
1.52      noro     2415:        }
1.27      noro     2416:        /* dp->p */
1.61      noro     2417:        nd_bpe = cbpe;
                   2418:        nd_setup_parameters(0,0);
                   2419:        for ( r = cand; r; r = NEXT(r) ) BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.27      noro     2420:        MKLIST(*rp,cand);
1.1       noro     2421: }
                   2422:
1.61      noro     2423: void dltondl(int n,DL dl,UINT *r)
1.1       noro     2424: {
1.61      noro     2425:        UINT *d;
1.57      noro     2426:        int i,j,l,s,ord_l;
1.43      noro     2427:        struct order_pair *op;
1.1       noro     2428:
                   2429:        d = dl->d;
1.41      noro     2430:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2431:        if ( nd_blockmask ) {
                   2432:                l = nd_blockmask->n;
                   2433:                op = nd_blockmask->order_pair;
                   2434:                for ( j = 0, s = 0; j < l; j++ ) {
                   2435:                        ord_l = op[j].length;
1.57      noro     2436:                        for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43      noro     2437:                }
                   2438:                TD(r) = ndl_weight(r);
1.61      noro     2439:                ndl_weight_mask(r);
1.43      noro     2440:        } else {
1.56      noro     2441:                for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43      noro     2442:                TD(r) = ndl_weight(r);
                   2443:        }
1.1       noro     2444: }
                   2445:
1.61      noro     2446: DL ndltodl(int n,UINT *ndl)
1.1       noro     2447: {
                   2448:        DL dl;
                   2449:        int *d;
1.57      noro     2450:        int i,j,l,s,ord_l;
1.43      noro     2451:        struct order_pair *op;
1.1       noro     2452:
                   2453:        NEWDL(dl,n);
1.34      noro     2454:        dl->td = TD(ndl);
1.1       noro     2455:        d = dl->d;
1.43      noro     2456:        if ( nd_blockmask ) {
                   2457:                l = nd_blockmask->n;
                   2458:                op = nd_blockmask->order_pair;
                   2459:                for ( j = 0, s = 0; j < l; j++ ) {
                   2460:                        ord_l = op[j].length;
1.57      noro     2461:                        for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43      noro     2462:                }
                   2463:        } else {
1.56      noro     2464:                for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43      noro     2465:        }
1.1       noro     2466:        return dl;
                   2467: }
                   2468:
1.61      noro     2469: void ndl_print(UINT *dl)
1.1       noro     2470: {
                   2471:        int n;
1.57      noro     2472:        int i,j,l,ord_l,s,s0;
1.43      noro     2473:        struct order_pair *op;
1.1       noro     2474:
                   2475:        n = nd_nvar;
                   2476:        printf("<<");
1.43      noro     2477:        if ( nd_blockmask ) {
                   2478:                l = nd_blockmask->n;
                   2479:                op = nd_blockmask->order_pair;
                   2480:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2481:                        ord_l = op[j].length;
1.57      noro     2482:                        for ( i = 0; i < ord_l; i++, s++ )
                   2483:                                printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43      noro     2484:                }
                   2485:        } else {
1.56      noro     2486:                for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43      noro     2487:        }
1.1       noro     2488:        printf(">>");
                   2489: }
                   2490:
                   2491: void nd_print(ND p)
                   2492: {
                   2493:        NM m;
                   2494:
                   2495:        if ( !p )
                   2496:                printf("0\n");
                   2497:        else {
                   2498:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     2499:                        printf("+%d*",CM(m));
                   2500:                        ndl_print(DL(m));
1.1       noro     2501:                }
                   2502:                printf("\n");
                   2503:        }
                   2504: }
                   2505:
1.16      noro     2506: void nd_print_q(ND p)
                   2507: {
                   2508:        NM m;
                   2509:
                   2510:        if ( !p )
                   2511:                printf("0\n");
                   2512:        else {
                   2513:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2514:                        printf("+");
                   2515:                        printexpr(CO,CQ(m));
                   2516:                        printf("*");
                   2517:                        ndl_print(DL(m));
                   2518:                }
                   2519:                printf("\n");
                   2520:        }
                   2521: }
                   2522:
1.1       noro     2523: void ndp_print(ND_pairs d)
                   2524: {
                   2525:        ND_pairs t;
                   2526:
1.34      noro     2527:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2528:        printf("\n");
                   2529: }
                   2530:
1.20      noro     2531: void nd_removecont(int mod,ND p)
1.16      noro     2532: {
                   2533:        int i,n;
                   2534:        Q *w;
                   2535:        Q dvr,t;
                   2536:        NM m;
1.21      noro     2537:        struct oVECT v;
                   2538:        N q,r;
1.16      noro     2539:
1.34      noro     2540:        if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2541:        else {
                   2542:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   2543:                w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2544:                v.len = n;
                   2545:                v.body = (pointer *)w;
1.34      noro     2546:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2547:                removecont_array(w,n);
                   2548:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16      noro     2549:        }
                   2550: }
                   2551:
1.21      noro     2552: void nd_removecont2(ND p1,ND p2)
                   2553: {
                   2554:        int i,n1,n2,n;
                   2555:        Q *w;
                   2556:        Q dvr,t;
                   2557:        NM m;
                   2558:        struct oVECT v;
                   2559:        N q,r;
                   2560:
                   2561:        if ( !p1 ) {
                   2562:                nd_removecont(0,p2); return;
                   2563:        } else if ( !p2 ) {
                   2564:                nd_removecont(0,p1); return;
                   2565:        }
                   2566:        n1 = nd_length(p1);
                   2567:        n2 = nd_length(p2);
                   2568:        n = n1+n2;
                   2569:        w = (Q *)ALLOCA(n*sizeof(Q));
                   2570:        v.len = n;
                   2571:        v.body = (pointer *)w;
1.34      noro     2572:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   2573:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2574:        removecont_array(w,n);
                   2575:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2576:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2577: }
                   2578:
1.20      noro     2579: void ndv_removecont(int mod,NDV p)
1.16      noro     2580: {
                   2581:        int i,len;
                   2582:        Q *w;
                   2583:        Q dvr,t;
                   2584:        NMV m;
                   2585:
1.20      noro     2586:        if ( mod )
                   2587:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2588:        else {
                   2589:                len = p->len;
                   2590:                w = (Q *)ALLOCA(len*sizeof(Q));
1.34      noro     2591:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
1.20      noro     2592:                sortbynm(w,len);
                   2593:                qltozl(w,len,&dvr);
                   2594:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   2595:                        divq(CQ(m),dvr,&t); CQ(m) = t;
                   2596:                }
1.16      noro     2597:        }
1.21      noro     2598: }
                   2599:
1.61      noro     2600: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
                   2601: {
                   2602:        int len,i,max;
                   2603:        NMV m,mr0,mr,t;
                   2604:
                   2605:        len = p->len;
                   2606:        for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   2607:                max = MAX(max,TD(DL(m)));
                   2608:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   2609:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   2610:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2611:        t = (NMV)ALLOCA(nmv_adv);
                   2612:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   2613:                ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
                   2614:                CQ(mr) = CQ(m);
                   2615:                ndl_copy(DL(t),DL(mr));
                   2616:        }
                   2617:        NV(p)++;
                   2618:        BDY(p) = mr0;
                   2619: }
                   2620:
1.45      noro     2621: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2622: {
1.45      noro     2623:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.23      noro     2624:        Q *w;
                   2625:        Q dvr,t;
                   2626:        NMV m,r;
                   2627:
                   2628:        len = p->len;
                   2629:        newnvar = nd_nvar-1;
1.48      noro     2630:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2631:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2632:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2633:                ndl_dehomogenize(DL(m));
1.23      noro     2634:        if ( newwpd != nd_wpd ) {
1.61      noro     2635:                newadv = sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT);
                   2636:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.45      noro     2637:                        CQ(r) = CQ(m);
                   2638:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2639:                        adj = nd_exporigin-newexporigin;
                   2640:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2641:                }
                   2642:        }
                   2643:        NV(p)--;
                   2644: }
                   2645:
1.21      noro     2646: void removecont_array(Q *c,int n)
                   2647: {
                   2648:        struct oVECT v;
                   2649:        Q d0,d1,a,u,u1,gcd;
                   2650:        int i;
                   2651:        N qn,rn,gn;
                   2652:        Q *q,*r;
                   2653:
                   2654:        q = (Q *)ALLOCA(n*sizeof(Q));
                   2655:        r = (Q *)ALLOCA(n*sizeof(Q));
                   2656:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   2657:        igcdv_estimate(&v,&d0);
                   2658:        for ( i = 0; i < n; i++ ) {
                   2659:                divn(NM(c[i]),NM(d0),&qn,&rn);
                   2660:                NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   2661:                NTOQ(rn,SGN(c[i]),r[i]);
                   2662:        }
1.34      noro     2663:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2664:        if ( i < n ) {
                   2665:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   2666:                igcdv(&v,&d1);
                   2667:                gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   2668:                divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   2669:                for ( i = 0; i < n; i++ ) {
                   2670:                        mulq(a,q[i],&u);
                   2671:                        if ( r[i] ) {
                   2672:                                divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   2673:                                addq(u,u1,&q[i]);
                   2674:                        } else
                   2675:                                q[i] = u;
                   2676:                }
                   2677:        }
1.34      noro     2678:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2679: }
                   2680:
1.19      noro     2681: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2682: {
                   2683:        NM m;
                   2684:        int c,c1;
                   2685:
1.34      noro     2686:        if ( !p ) return;
1.1       noro     2687:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     2688:                c1 = CM(m);
1.19      noro     2689:                DMAR(c1,mul,0,mod,c);
1.14      noro     2690:                CM(m) = c;
1.1       noro     2691:        }
                   2692: }
                   2693:
1.16      noro     2694: void nd_mul_c_q(ND p,Q mul)
                   2695: {
                   2696:        NM m;
                   2697:        Q c;
                   2698:
1.34      noro     2699:        if ( !p ) return;
1.16      noro     2700:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2701:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   2702:        }
                   2703: }
                   2704:
1.61      noro     2705: void nd_mul_c_p(VL vl,ND p,P mul)
                   2706: {
                   2707:        NM m;
                   2708:        P c;
                   2709:
                   2710:        if ( !p ) return;
                   2711:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2712:                mulp(vl,CP(m),mul,&c); CP(m) = c;
                   2713:        }
                   2714: }
                   2715:
1.1       noro     2716: void nd_free(ND p)
                   2717: {
                   2718:        NM t,s;
                   2719:
1.34      noro     2720:        if ( !p ) return;
1.1       noro     2721:        t = BDY(p);
                   2722:        while ( t ) {
                   2723:                s = NEXT(t);
                   2724:                FREENM(t);
                   2725:                t = s;
                   2726:        }
                   2727:        FREEND(p);
                   2728: }
                   2729:
1.23      noro     2730: void ndv_free(NDV p)
                   2731: {
                   2732:        GC_free(BDY(p));
                   2733: }
                   2734:
1.61      noro     2735: void nd_append_red(UINT *d,int i)
1.1       noro     2736: {
1.13      noro     2737:        RHist m,m0;
1.1       noro     2738:        int h;
                   2739:
1.13      noro     2740:        NEWRHist(m);
1.34      noro     2741:        h = ndl_hash_value(d);
1.13      noro     2742:        m->index = i;
1.14      noro     2743:        ndl_copy(d,DL(m));
1.1       noro     2744:        NEXT(m) = nd_red[h];
                   2745:        nd_red[h] = m;
                   2746: }
                   2747:
1.61      noro     2748: UINT *ndv_compute_bound(NDV p)
1.1       noro     2749: {
1.61      noro     2750:        UINT *d1,*d2,*t;
                   2751:        UINT u;
1.57      noro     2752:        int i,j,k,l,len,ind;
1.45      noro     2753:        NMV m;
1.1       noro     2754:
                   2755:        if ( !p )
                   2756:                return 0;
1.61      noro     2757:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2758:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45      noro     2759:        len = LEN(p);
                   2760:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   2761:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2762:                ndl_lcm(DL(m),d1,d2);
1.1       noro     2763:                t = d1; d1 = d2; d2 = t;
                   2764:        }
1.12      noro     2765:        l = nd_nvar+31;
1.61      noro     2766:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57      noro     2767:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2768:                u = d1[i];
                   2769:                k = (nd_epw-1)*nd_bpe;
                   2770:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2771:                        t[ind] = (u>>k)&nd_mask0;
                   2772:        }
                   2773:        for ( ; ind < l; ind++ ) t[ind] = 0;
1.1       noro     2774:        return t;
                   2775: }
                   2776:
1.48      noro     2777: int nd_get_exporigin(struct order_spec *ord)
                   2778: {
1.51      noro     2779:        switch ( ord->id ) {
1.41      noro     2780:                case 0:
1.48      noro     2781:                        return 1;
1.41      noro     2782:                case 1:
                   2783:                        /* block order */
1.43      noro     2784:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     2785:                        return ord->ord.block.length+1;
1.41      noro     2786:                case 2:
1.52      noro     2787:                        error("nd_get_exporigin : matrix order is not supported yet.");
1.41      noro     2788:        }
1.48      noro     2789: }
                   2790:
1.61      noro     2791: void nd_setup_parameters(int nvar,int max) {
1.57      noro     2792:        int i,j,n,elen,ord_o,ord_l,l,s;
                   2793:        struct order_pair *op;
1.48      noro     2794:
1.61      noro     2795:        /* if max == 0, don't touch nd_bpe */
                   2796:        if ( max > 0 ) {
1.63      noro     2797:                if ( max < 2 ) nd_bpe = 1;
1.62      noro     2798:                if ( max < 4 ) nd_bpe = 2;
                   2799:                else if ( max < 8 ) nd_bpe = 3;
                   2800:                else if ( max < 16 ) nd_bpe = 4;
1.63      noro     2801:                else if ( max < 32 ) nd_bpe = 5;
1.61      noro     2802:                else if ( max < 64 ) nd_bpe = 6;
                   2803:                else if ( max < 256 ) nd_bpe = 8;
1.63      noro     2804:                else if ( max < 1024 ) nd_bpe = 10;
1.61      noro     2805:                else if ( max < 65536 ) nd_bpe = 16;
                   2806:                else nd_bpe = 32;
                   2807:        }
                   2808:        /* nvar == 0, don't touch nd_nvar */
                   2809:        if ( nvar > 0 ) nd_nvar = nvar;
                   2810:
                   2811:        nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48      noro     2812:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   2813:
                   2814:        nd_exporigin = nd_get_exporigin(nd_ord);
1.43      noro     2815:        nd_wpd = nd_exporigin+elen;
1.57      noro     2816:
1.1       noro     2817:        if ( nd_bpe < 32 ) {
                   2818:                nd_mask0 = (1<<nd_bpe)-1;
                   2819:        } else {
                   2820:                nd_mask0 = 0xffffffff;
                   2821:        }
                   2822:        bzero(nd_mask,sizeof(nd_mask));
                   2823:        nd_mask1 = 0;
                   2824:        for ( i = 0; i < nd_epw; i++ ) {
                   2825:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   2826:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   2827:        }
1.61      noro     2828:        nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT);
                   2829:        nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT);
1.57      noro     2830:        nd_epos = nd_create_epos(nd_ord);
1.43      noro     2831:        nd_blockmask = nd_create_blockmask(nd_ord);
1.1       noro     2832: }
                   2833:
1.20      noro     2834: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1       noro     2835: {
1.37      noro     2836:        int i,obpe,oadv,h;
1.13      noro     2837:        NM prev_nm_free_list;
                   2838:        RHist mr0,mr;
                   2839:        RHist r;
1.37      noro     2840:        RHist *old_red;
1.1       noro     2841:        ND_pairs s0,s,t,prev_ndp_free_list;
1.43      noro     2842:        EPOS oepos;
1.15      noro     2843:
1.1       noro     2844:        obpe = nd_bpe;
1.11      noro     2845:        oadv = nmv_adv;
1.43      noro     2846:        oepos = nd_epos;
1.63      noro     2847:        if ( obpe < 2 ) nd_bpe = 2;
                   2848:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     2849:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     2850:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     2851:        else if ( obpe < 6 ) nd_bpe = 6;
                   2852:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     2853:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     2854:        else if ( obpe < 16 ) nd_bpe = 16;
                   2855:        else if ( obpe < 32 ) nd_bpe = 32;
                   2856:        else error("nd_reconstruct : exponent too large");
1.5       noro     2857:
1.61      noro     2858:        nd_setup_parameters(0,0);
1.1       noro     2859:        prev_nm_free_list = _nm_free_list;
                   2860:        prev_ndp_free_list = _ndp_free_list;
                   2861:        _nm_free_list = 0;
                   2862:        _ndp_free_list = 0;
1.53      noro     2863:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   2864:        if ( trace )
                   2865:                for ( i = nd_psn-1; i >= 0; i-- )
                   2866:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     2867:        s0 = 0;
                   2868:        for ( t = d; t; t = NEXT(t) ) {
                   2869:                NEXTND_pairs(s0,s);
                   2870:                s->i1 = t->i1;
                   2871:                s->i2 = t->i2;
1.14      noro     2872:                SG(s) = SG(t);
1.61      noro     2873:                ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1       noro     2874:        }
1.37      noro     2875:
                   2876:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     2877:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     2878:                old_red[i] = nd_red[i];
                   2879:                nd_red[i] = 0;
                   2880:        }
                   2881:        for ( i = 0; i < REDTAB_LEN; i++ )
                   2882:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   2883:                        NEWRHist(mr);
1.13      noro     2884:                        mr->index = r->index;
1.20      noro     2885:                        SG(mr) = SG(r);
1.61      noro     2886:                        ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37      noro     2887:                        h = ndl_hash_value(DL(mr));
                   2888:                        NEXT(mr) = nd_red[h];
                   2889:                        nd_red[h] = mr;
1.6       noro     2890:                }
1.37      noro     2891:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   2892:        old_red = 0;
1.11      noro     2893:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     2894:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61      noro     2895:                ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13      noro     2896:                nd_psh[i] = r;
1.11      noro     2897:        }
1.1       noro     2898:        if ( s0 ) NEXT(s) = 0;
                   2899:        prev_nm_free_list = 0;
                   2900:        prev_ndp_free_list = 0;
                   2901:        GC_gcollect();
                   2902:        return s0;
                   2903: }
                   2904:
1.23      noro     2905: void nd_reconstruct_direct(int mod,NDV *ps,int len)
                   2906: {
1.37      noro     2907:        int i,obpe,oadv,h;
1.61      noro     2908:        UINT **bound;
1.23      noro     2909:        NM prev_nm_free_list;
                   2910:        RHist mr0,mr;
                   2911:        RHist r;
1.37      noro     2912:        RHist *old_red;
1.23      noro     2913:        ND_pairs s0,s,t,prev_ndp_free_list;
1.43      noro     2914:        EPOS oepos;
1.23      noro     2915:
                   2916:        obpe = nd_bpe;
                   2917:        oadv = nmv_adv;
1.43      noro     2918:        oepos = nd_epos;
1.63      noro     2919:        if ( obpe < 2 ) nd_bpe = 2;
                   2920:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     2921:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     2922:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     2923:        else if ( obpe < 6 ) nd_bpe = 6;
                   2924:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     2925:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     2926:        else if ( obpe < 16 ) nd_bpe = 16;
                   2927:        else if ( obpe < 32 ) nd_bpe = 32;
                   2928:        else error("nd_reconstruct_direct : exponent too large");
1.23      noro     2929:
1.61      noro     2930:        nd_setup_parameters(0,0);
1.23      noro     2931:        prev_nm_free_list = _nm_free_list;
                   2932:        prev_ndp_free_list = _ndp_free_list;
1.34      noro     2933:        _nm_free_list = 0; _ndp_free_list = 0;
1.43      noro     2934:        for ( i = len-1; i >= 0; i-- ) ndv_realloc(ps[i],obpe,oadv,oepos);
1.23      noro     2935:        prev_nm_free_list = 0;
                   2936:        prev_ndp_free_list = 0;
                   2937:        GC_gcollect();
                   2938: }
                   2939:
1.61      noro     2940: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     2941: {
1.57      noro     2942:        int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43      noro     2943:        struct order_pair *op;
1.1       noro     2944:
                   2945:        n = nd_nvar;
1.61      noro     2946:        oepw = (sizeof(UINT)*8)/obpe;
1.43      noro     2947:        omask0 = (1<<obpe)-1;
1.34      noro     2948:        TD(r) = TD(d);
1.41      noro     2949:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2950:        if ( nd_blockmask ) {
                   2951:                l = nd_blockmask->n;
                   2952:                op = nd_blockmask->order_pair;
                   2953:                for ( i = 1; i < nd_exporigin; i++ )
                   2954:                        r[i] = d[i];
                   2955:                for ( j = 0, s = 0; j < l; j++ ) {
                   2956:                        ord_l = op[j].length;
1.57      noro     2957:                        for ( i = 0; i < ord_l; i++, s++ ) {
                   2958:                                ei =  GET_EXP_OLD(d,s);
                   2959:                                PUT_EXP(r,s,ei);
                   2960:                        }
1.1       noro     2961:                }
1.43      noro     2962:        } else {
1.56      noro     2963:                for ( i = 0; i < n; i++ ) {
                   2964:                        ei = GET_EXP_OLD(d,i);
                   2965:                        PUT_EXP(r,i,ei);
                   2966:                }
1.1       noro     2967:        }
                   2968: }
1.3       noro     2969:
1.6       noro     2970: ND nd_copy(ND p)
                   2971: {
                   2972:        NM m,mr,mr0;
1.41      noro     2973:        int c,n;
1.6       noro     2974:        ND r;
                   2975:
                   2976:        if ( !p )
                   2977:                return 0;
                   2978:        else {
                   2979:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2980:                        NEXTNM(mr0,mr);
1.14      noro     2981:                        CM(mr) = CM(m);
                   2982:                        ndl_copy(DL(m),DL(mr));
1.6       noro     2983:                }
                   2984:                NEXT(mr) = 0;
1.31      noro     2985:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     2986:                SG(r) = SG(p);
1.6       noro     2987:                return r;
                   2988:        }
                   2989: }
                   2990:
1.53      noro     2991: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     2992: {
                   2993:        NM m;
                   2994:        NDV p1,p2;
                   2995:        ND t1,t2;
1.61      noro     2996:        UINT *lcm;
1.31      noro     2997:        int td;
1.11      noro     2998:
1.53      noro     2999:        if ( trace ) {
                   3000:                p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3001:        } else {
1.20      noro     3002:                p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3003:        }
1.34      noro     3004:        lcm = LCM(p);
1.11      noro     3005:        NEWNM(m);
1.20      noro     3006:        CQ(m) = HCQ(p2);
1.34      noro     3007:        ndl_sub(lcm,HDL(p1),DL(m));
1.56      noro     3008:        if ( ndl_check_bound2(p->i1,DL(m)) )
                   3009:                return 0;
1.55      noro     3010:        t1 = ndv_mul_nm(mod,m,p1);
1.34      noro     3011:        if ( mod ) CM(m) = mod-HCM(p1);
                   3012:        else chsgnq(HCQ(p1),&CQ(m));
                   3013:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     3014:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     3015:                nd_free(t1);
                   3016:                return 0;
                   3017:        }
1.55      noro     3018:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     3019:        *rp = nd_add(mod,t1,t2);
1.11      noro     3020:        FREENM(m);
                   3021:        return 1;
                   3022: }
                   3023:
1.19      noro     3024: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     3025: {
                   3026:        NMV m;
                   3027:        int c,c1,len,i;
                   3028:
1.34      noro     3029:        if ( !p ) return;
1.14      noro     3030:        len = LEN(p);
1.11      noro     3031:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.34      noro     3032:                c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
1.11      noro     3033:        }
                   3034: }
                   3035:
1.16      noro     3036: void ndv_mul_c_q(NDV p,Q mul)
                   3037: {
                   3038:        NMV m;
                   3039:        Q c;
                   3040:        int len,i;
                   3041:
1.34      noro     3042:        if ( !p ) return;
1.16      noro     3043:        len = LEN(p);
                   3044:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3045:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   3046:        }
                   3047: }
                   3048:
1.55      noro     3049: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   3050:        int n2,i,j,l,n,tlen;
1.61      noro     3051:        UINT *d0;
1.55      noro     3052:        NM *tab,*psum;
                   3053:        ND s,r;
                   3054:        NM t;
                   3055:        NMV m1;
                   3056:
                   3057:        if ( !p ) return 0;
                   3058:        n = NV(p); n2 = n>>1;
                   3059:        d0 = DL(m0);
                   3060:        l = LEN(p);
                   3061:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3062:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3063:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3064:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56      noro     3065:        m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3066:        for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55      noro     3067:                /* m0(NM) * m1(NMV) => tab(NM) */
1.56      noro     3068:                weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55      noro     3069:                for ( j = 0; j < tlen; j++ ) {
                   3070:                        if ( tab[j] ) {
                   3071:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
                   3072:                        }
                   3073:                }
                   3074:        }
                   3075:        for ( i = tlen-1, r = 0; i >= 0; i-- )
                   3076:                if ( psum[i] ) {
                   3077:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   3078:                        MKND(n,psum[i],j,s);
                   3079:                        r = nd_add(mod,r,s);
                   3080:                }
1.56      noro     3081:        if ( r ) SG(r) = SG(p)+TD(d0);
                   3082:        return r;
1.55      noro     3083: }
                   3084:
1.56      noro     3085: /* product of monomials */
                   3086: /* XXX block order is not handled correctly */
                   3087:
1.55      noro     3088: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   3089: {
1.56      noro     3090:        int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61      noro     3091:        UINT *d0,*d1,*d,*dt,*ctab;
1.56      noro     3092:        Q *ctab_q;
                   3093:        Q q,q1;
1.61      noro     3094:        UINT c0,c1,c;
1.55      noro     3095:        NM *p;
                   3096:        NM m,t;
                   3097:
                   3098:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   3099:        if ( !m0 || !m1 ) return;
                   3100:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   3101:        NEWNM(m); d = DL(m);
1.56      noro     3102:        if ( mod ) {
                   3103:                c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   3104:        } else
                   3105:                mulq(CQ(m0),CQ(m1),&CQ(m));
1.55      noro     3106:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   3107:        homo = n&1 ? 1 : 0;
                   3108:        if ( homo ) {
                   3109:                /* offset of h-degree */
                   3110:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   3111:                PUT_EXP(DL(m),n-1,h);
                   3112:                TD(DL(m)) = h;
1.61      noro     3113:                if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55      noro     3114:        }
                   3115:        tab[0] = m;
                   3116:        NEWNM(m); d = DL(m);
1.57      noro     3117:        for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55      noro     3118:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   3119:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   3120:                /* xi^a*(Di^k*xi^l)*Di^b */
                   3121:                a += l; b += k;
1.56      noro     3122:                s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55      noro     3123:                if ( !k || !l ) {
                   3124:                        for ( j = 0; j < curlen; j++ )
1.56      noro     3125:                                if ( t = tab[j] ) {
                   3126:                                        dt = DL(t);
                   3127:                                        PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61      noro     3128:                                        if ( nd_blockmask ) ndl_weight_mask(dt);
1.55      noro     3129:                                }
                   3130:                        curlen *= k+1;
                   3131:                        continue;
                   3132:                }
                   3133:                min = MIN(k,l);
1.56      noro     3134:                if ( mod ) {
1.61      noro     3135:                        ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56      noro     3136:                        mkwcm(k,l,mod,ctab);
                   3137:                } else {
                   3138:                        ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   3139:                        mkwc(k,l,ctab_q);
                   3140:                }
1.57      noro     3141:                for ( j = min; j >= 0; j-- ) {
1.56      noro     3142:                        for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55      noro     3143:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56      noro     3144:                        h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55      noro     3145:                        if ( homo ) {
                   3146:                                TD(d) = s;
1.56      noro     3147:                                PUT_EXP(d,n-1,s-h);
1.55      noro     3148:                        } else TD(d) = h;
1.61      noro     3149:                        if ( nd_blockmask ) ndl_weight_mask(d);
1.56      noro     3150:                        if ( mod ) c = ctab[j];
                   3151:                        else q = ctab_q[j];
1.57      noro     3152:                        p = tab+curlen*j;
                   3153:                        if ( j == 0 ) {
                   3154:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3155:                                        if ( tab[u] ) {
                   3156:                                                ndl_addto(DL(tab[u]),d);
                   3157:                                                if ( mod ) {
                   3158:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   3159:                                                } else {
                   3160:                                                        mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   3161:                                                }
                   3162:                                        }
1.56      noro     3163:                                }
1.57      noro     3164:                        } else {
                   3165:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3166:                                        if ( tab[u] ) {
                   3167:                                                NEWNM(t);
                   3168:                                                ndl_add(DL(tab[u]),d,DL(t));
                   3169:                                                if ( mod ) {
                   3170:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   3171:                                                } else
                   3172:                                                        mulq(CQ(tab[u]),q,&CQ(t));
                   3173:                                                *p = t;
                   3174:                                        }
1.55      noro     3175:                                }
                   3176:                        }
                   3177:                }
                   3178:                curlen *= k+1;
                   3179:        }
                   3180:        FREENM(m);
                   3181: }
                   3182:
1.63      noro     3183: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   3184: {
                   3185:        NM mr,mr0;
                   3186:        NMV m;
                   3187:        UINT *d,*dt,*dm;
                   3188:        int c,n,td,i,c1,c2,len;
                   3189:        Q q;
                   3190:        ND r;
                   3191:
                   3192:        if ( !p ) return 0;
                   3193:        else {
                   3194:                n = NV(p); m = BDY(p);
                   3195:                d = DL(m0);
                   3196:                len = LEN(p);
                   3197:                mr0 = 0;
                   3198:                td = TD(d);
                   3199:                c = CM(m0);
                   3200:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3201:                        NEXTNM(mr0,mr);
                   3202:                        CM(mr) = 1;
                   3203:                        ndl_add(DL(m),d,DL(mr));
                   3204:                }
                   3205:                NEXT(mr) = 0;
                   3206:                MKND(NV(p),mr0,len,r);
                   3207:                SG(r) = SG(p) + TD(d);
                   3208:                return r;
                   3209:        }
                   3210: }
                   3211:
1.55      noro     3212: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3213: {
                   3214:        NM mr,mr0;
                   3215:        NMV m;
1.61      noro     3216:        UINT *d,*dt,*dm;
1.9       noro     3217:        int c,n,td,i,c1,c2,len;
1.16      noro     3218:        Q q;
1.9       noro     3219:        ND r;
                   3220:
1.34      noro     3221:        if ( !p ) return 0;
1.55      noro     3222:        else if ( do_weyl )
                   3223:                return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3224:        else {
                   3225:                n = NV(p); m = BDY(p);
1.34      noro     3226:                d = DL(m0);
1.14      noro     3227:                len = LEN(p);
1.9       noro     3228:                mr0 = 0;
1.34      noro     3229:                td = TD(d);
1.16      noro     3230:                if ( mod ) {
                   3231:                        c = CM(m0);
                   3232:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3233:                                NEXTNM(mr0,mr);
                   3234:                                c1 = CM(m);
1.19      noro     3235:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3236:                                CM(mr) = c2;
                   3237:                                ndl_add(DL(m),d,DL(mr));
                   3238:                        }
                   3239:                } else {
                   3240:                        q = CQ(m0);
                   3241:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3242:                                NEXTNM(mr0,mr);
                   3243:                                mulq(CQ(m),q,&CQ(mr));
                   3244:                                ndl_add(DL(m),d,DL(mr));
                   3245:                        }
1.4       noro     3246:                }
1.9       noro     3247:                NEXT(mr) = 0;
1.31      noro     3248:                MKND(NV(p),mr0,len,r);
1.34      noro     3249:                SG(r) = SG(p) + TD(d);
1.9       noro     3250:                return r;
1.4       noro     3251:        }
                   3252: }
                   3253:
1.43      noro     3254: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3255: {
1.13      noro     3256:        NMV m,mr,mr0,t;
                   3257:        int len,i,k;
1.11      noro     3258:
1.61      noro     3259:        if ( !p ) return;
                   3260:        m = BDY(p); len = LEN(p);
                   3261:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3262:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   3263:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3264:        t = (NMV)ALLOCA(nmv_adv);
                   3265:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   3266:                CQ(t) = CQ(m);
                   3267:                for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   3268:                ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   3269:                CQ(mr) = CQ(t);
                   3270:                ndl_copy(DL(t),DL(mr));
                   3271:        }
                   3272:        BDY(p) = mr0;
                   3273: }
                   3274:
                   3275: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   3276: {
                   3277:        NMV m,mr,mr0;
                   3278:        int len,i;
                   3279:        NDV r;
1.11      noro     3280:
1.61      noro     3281:        if ( !p ) return 0;
                   3282:        m = BDY(p); len = LEN(p);
                   3283:        mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   3284:        for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   3285:                ndl_zero(DL(mr));
                   3286:                ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   3287:                CQ(mr) = CQ(m);
1.11      noro     3288:        }
1.61      noro     3289:        MKNDV(NV(p),mr0,len,r);
                   3290:        SG(r) = SG(p);
                   3291:        return r;
1.11      noro     3292: }
                   3293:
1.61      noro     3294: /* duplicate p */
                   3295:
                   3296: NDV ndv_dup(int mod,NDV p)
1.3       noro     3297: {
                   3298:        NDV d;
1.61      noro     3299:        NMV t,m,m0;
1.3       noro     3300:        int i,len;
                   3301:
1.34      noro     3302:        if ( !p ) return 0;
1.31      noro     3303:        len = LEN(p);
1.34      noro     3304:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61      noro     3305:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14      noro     3306:                ndl_copy(DL(t),DL(m));
1.16      noro     3307:                CQ(m) = CQ(t);
1.3       noro     3308:        }
                   3309:        MKNDV(NV(p),m0,len,d);
1.23      noro     3310:        SG(d) = SG(p);
                   3311:        return d;
                   3312: }
                   3313:
1.63      noro     3314: ND nd_dup(ND p)
                   3315: {
                   3316:        ND d;
                   3317:        NM t,m,m0;
                   3318:
                   3319:        if ( !p ) return 0;
                   3320:        for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   3321:                NEXTNM(m0,m);
                   3322:                ndl_copy(DL(t),DL(m));
                   3323:                CQ(m) = CQ(t);
                   3324:        }
                   3325:        if ( m0 ) NEXT(m) = 0;
                   3326:        MKND(NV(p),m0,LEN(p),d);
                   3327:        SG(d) = SG(p);
                   3328:        return d;
                   3329: }
                   3330:
1.61      noro     3331: /* XXX if p->len == 0 then it represents 0 */
                   3332:
                   3333: void ndv_mod(int mod,NDV p)
                   3334: {
                   3335:        NMV t,d;
                   3336:        int r;
                   3337:        int i,len,dlen;
                   3338:
                   3339:        if ( !p ) return;
                   3340:        len = LEN(p);
                   3341:        dlen = 0;
                   3342:        for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3343:                r = rem(NM(CQ(t)),mod);
                   3344:                if ( r ) {
                   3345:                        if ( SGN(CQ(t)) < 0 )
                   3346:                                r = mod-r;
                   3347:                        CM(d) = r;
                   3348:                        ndl_copy(DL(t),DL(d));
                   3349:                        NMV_ADV(d);
                   3350:                        dlen++;
                   3351:                }
                   3352:        }
                   3353:        LEN(p) = dlen;
                   3354: }
                   3355:
                   3356: NDV ptondv(VL vl,VL dvl,P p)
                   3357: {
                   3358:        ND nd;
                   3359:
                   3360:        nd = ptond(vl,dvl,p);
                   3361:        return ndtondv(0,nd);
                   3362: }
                   3363:
                   3364: ND ptond(VL vl,VL dvl,P p)
1.23      noro     3365: {
1.61      noro     3366:        int n,i,j,k,e;
                   3367:        VL tvl;
                   3368:        V v;
                   3369:        DCP dc;
                   3370:        DCP *w;
                   3371:        ND r,s,t,u;
                   3372:        P x;
                   3373:        int c;
                   3374:        UINT *d;
1.23      noro     3375:        NM m,m0;
1.61      noro     3376:
                   3377:        if ( !p )
                   3378:                return 0;
                   3379:        else if ( NUM(p) ) {
                   3380:                NEWNM(m);
                   3381:                ndl_zero(DL(m));
                   3382:                CQ(m) = (Q)p;
                   3383:                NEXT(m) = 0;
                   3384:                MKND(nd_nvar,m,1,r);
                   3385:                SG(r) = 0;
                   3386:                return r;
                   3387:        } else {
                   3388:                for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   3389:                w = (DCP *)ALLOCA(k*sizeof(DCP));
                   3390:                for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   3391:                for ( i = 0, tvl = dvl, v = VR(p);
                   3392:                        vl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   3393:                if ( !tvl ) {
                   3394:                        for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   3395:                                t = ptond(vl,dvl,COEF(w[j]));
                   3396:                                pwrp(vl,x,DEG(w[j]),&p);
                   3397:                                nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   3398:                        }
                   3399:                        return s;
                   3400:                } else {
                   3401:                        NEWNM(m0); d = DL(m0);
                   3402:                        for ( j = k-1, s = 0; j >= 0; j-- ) {
                   3403:                                ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   3404:                                TD(d) = MUL_WEIGHT(e,i);
                   3405:                                if ( nd_blockmask) ndl_weight_mask(d);
                   3406:                                t = ptond(vl,dvl,COEF(w[j]));
                   3407:                                for ( m = BDY(t); m; m = NEXT(m) )
                   3408:                                        ndl_addto(DL(m),d);
                   3409:                                SG(t) += TD(d);
                   3410:                                s = nd_add(0,s,t);
                   3411:                        }
                   3412:                        FREENM(m0);
                   3413:                        return s;
                   3414:                }
                   3415:        }
                   3416: }
                   3417:
                   3418: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   3419: {
                   3420:        VL tvl;
                   3421:        int len,n,j,i,e;
                   3422:        NMV m;
                   3423:        Q q;
                   3424:        P c;
                   3425:        UINT *d;
                   3426:        P s,r,u,t,w;
1.23      noro     3427:
1.34      noro     3428:        if ( !p ) return 0;
1.61      noro     3429:        else {
                   3430:                len = LEN(p);
                   3431:                n = NV(p);
                   3432:                m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   3433:                for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
                   3434:                        if ( mod ) {
                   3435:                                STOQ(CM(m),q); c = (P)q;
                   3436:                        } else
                   3437:                                c = CP(m);
                   3438:                        d = DL(m);
                   3439:                        for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   3440:                                MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   3441:                                pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   3442:                        }
                   3443:                        addp(vl,s,t,&u); s = u;
                   3444:                }
                   3445:                return s;
1.23      noro     3446:        }
1.3       noro     3447: }
                   3448:
1.61      noro     3449: NDV ndtondv(int mod,ND p)
1.11      noro     3450: {
                   3451:        NDV d;
1.61      noro     3452:        NMV m,m0;
                   3453:        NM t;
                   3454:        int i,len;
1.11      noro     3455:
1.34      noro     3456:        if ( !p ) return 0;
1.61      noro     3457:        len = LEN(p);
                   3458:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
                   3459:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   3460:                ndl_copy(DL(t),DL(m));
                   3461:                CQ(m) = CQ(t);
1.11      noro     3462:        }
1.61      noro     3463:        MKNDV(NV(p),m0,len,d);
1.14      noro     3464:        SG(d) = SG(p);
1.11      noro     3465:        return d;
                   3466: }
                   3467:
1.61      noro     3468: ND ndvtond(int mod,NDV p)
1.11      noro     3469: {
1.61      noro     3470:        ND d;
                   3471:        NM m,m0;
1.11      noro     3472:        NMV t;
1.61      noro     3473:        int i,len;
1.11      noro     3474:
1.34      noro     3475:        if ( !p ) return 0;
1.11      noro     3476:        m0 = 0;
1.61      noro     3477:        len = p->len;
                   3478:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3479:                NEXTNM(m0,m);
                   3480:                ndl_copy(DL(t),DL(m));
                   3481:                CQ(m) = CQ(t);
1.11      noro     3482:        }
                   3483:        NEXT(m) = 0;
1.61      noro     3484:        MKND(NV(p),m0,len,d);
1.14      noro     3485:        SG(d) = SG(p);
1.11      noro     3486:        return d;
                   3487: }
                   3488:
1.3       noro     3489: void ndv_print(NDV p)
                   3490: {
                   3491:        NMV m;
                   3492:        int i,len;
                   3493:
1.34      noro     3494:        if ( !p ) printf("0\n");
1.3       noro     3495:        else {
1.14      noro     3496:                len = LEN(p);
1.3       noro     3497:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14      noro     3498:                        printf("+%d*",CM(m));
1.16      noro     3499:                        ndl_print(DL(m));
                   3500:                }
                   3501:                printf("\n");
                   3502:        }
                   3503: }
                   3504:
                   3505: void ndv_print_q(NDV p)
                   3506: {
                   3507:        NMV m;
                   3508:        int i,len;
                   3509:
1.34      noro     3510:        if ( !p ) printf("0\n");
1.16      noro     3511:        else {
                   3512:                len = LEN(p);
                   3513:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3514:                        printf("+");
                   3515:                        printexpr(CO,CQ(m));
                   3516:                        printf("*");
1.14      noro     3517:                        ndl_print(DL(m));
1.3       noro     3518:                }
                   3519:                printf("\n");
                   3520:        }
1.25      noro     3521: }
                   3522:
1.61      noro     3523: NODE ndv_reducebase(NODE x)
1.27      noro     3524: {
                   3525:        int len,i,j;
                   3526:        NDV *w;
                   3527:        NODE t,t0;
                   3528:
                   3529:        len = length(x);
                   3530:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3531:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3532:        for ( i = 0; i < len; i++ ) {
                   3533:                for ( j = 0; j < i; j++ ) {
                   3534:                        if ( w[i] && w[j] )
                   3535:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3536:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3537:                }
                   3538:        }
                   3539:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3540:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3541:        }
                   3542:        NEXT(t) = 0; x = t0;
                   3543:        return x;
1.11      noro     3544: }
1.32      noro     3545:
1.43      noro     3546: /* XXX incomplete */
                   3547:
1.32      noro     3548: void nd_init_ord(struct order_spec *ord)
                   3549: {
1.43      noro     3550:        switch ( ord->id ) {
1.32      noro     3551:                case 0:
1.43      noro     3552:                        switch ( ord->ord.simple ) {
                   3553:                                case 0:
                   3554:                                        nd_dcomp = 1;
                   3555:                                        nd_isrlex = 1;
                   3556:                                        break;
                   3557:                                case 1:
                   3558:                                        nd_dcomp = 1;
                   3559:                                        nd_isrlex = 0;
                   3560:                                        break;
                   3561:                                case 2:
                   3562:                                        nd_dcomp = 0;
                   3563:                                        nd_isrlex = 0;
1.45      noro     3564:                                        ndl_compare_function = ndl_lex_compare;
1.58      noro     3565:                                        break;
                   3566:                                case 11:
                   3567:                                        /* XXX */
                   3568:                                        nd_dcomp = 0;
                   3569:                                        nd_isrlex = 1;
                   3570:                                        ndl_compare_function = ndl_ww_lex_compare;
1.43      noro     3571:                                        break;
                   3572:                                default:
                   3573:                                        error("nd_gr : unsupported order");
                   3574:                        }
1.32      noro     3575:                        break;
                   3576:                case 1:
1.43      noro     3577:                        /* XXX */
                   3578:                        nd_dcomp = -1;
1.32      noro     3579:                        nd_isrlex = 0;
1.45      noro     3580:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3581:                        break;
1.43      noro     3582:                case 2:
                   3583:                        error("nd_init_ord : matrix order is not supported yet.");
1.32      noro     3584:                        break;
                   3585:        }
1.41      noro     3586:        nd_ord = ord;
1.32      noro     3587: }
                   3588:
1.43      noro     3589: BlockMask nd_create_blockmask(struct order_spec *ord)
                   3590: {
                   3591:        int n,i,j,s,l;
1.61      noro     3592:        UINT *t;
1.43      noro     3593:        BlockMask bm;
                   3594:
                   3595:        if ( !ord->id )
                   3596:                return 0;
                   3597:        n = ord->ord.block.length;
                   3598:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   3599:        bm->n = n;
                   3600:        bm->order_pair = ord->ord.block.order_pair;
1.61      noro     3601:        bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43      noro     3602:        for ( i = 0, s = 0; i < n; i++ ) {
1.61      noro     3603:                bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43      noro     3604:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   3605:                l = bm->order_pair[i].length;
                   3606:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   3607:        }
                   3608:        return bm;
1.57      noro     3609: }
                   3610:
                   3611: EPOS nd_create_epos(struct order_spec *ord)
                   3612: {
                   3613:        int i,j,l,s,ord_l,ord_o;
                   3614:        EPOS epos;
                   3615:        struct order_pair *op;
                   3616:
                   3617:        epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   3618:        switch ( ord->id ) {
                   3619:                case 0:
                   3620:                        if ( nd_isrlex ) {
                   3621:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3622:                                        epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   3623:                                        epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   3624:                                }
                   3625:                        } else {
                   3626:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3627:                                        epos[i].i = nd_exporigin + i/nd_epw;
                   3628:                                        epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3629:                                }
                   3630:                        }
                   3631:                        break;
                   3632:                case 1:
                   3633:                        /* block order */
                   3634:                        l = ord->ord.block.length;
                   3635:                        op = ord->ord.block.order_pair;
                   3636:                        for ( j = 0, s = 0; j < l; j++ ) {
                   3637:                                ord_o = op[j].order;
                   3638:                                ord_l = op[j].length;
                   3639:                                if ( !ord_o )
                   3640:                                        for ( i = 0; i < ord_l; i++ ) {
                   3641:                                                epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   3642:                                                epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   3643:                                        }
                   3644:                                else
                   3645:                                        for ( i = 0; i < ord_l; i++ ) {
                   3646:                                                epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   3647:                                                epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   3648:                                        }
                   3649:                                s += ord_l;
                   3650:                        }
                   3651:                        break;
                   3652:                case 2:
                   3653:                        error("nd_create_epos : matrix order is not supported yet.");
                   3654:        }
                   3655:        return epos;
1.43      noro     3656: }
1.59      noro     3657:
                   3658: /* external interface */
                   3659:
                   3660: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   3661: {
1.61      noro     3662:        NODE t,in0,in;
1.59      noro     3663:        ND nd,nf;
1.61      noro     3664:        NDV ndv;
                   3665:        VL vv,tv;
                   3666:        int stat,nvar,max,e;
1.59      noro     3667:
                   3668:        pltovl(v,&vv);
1.61      noro     3669:        nvar = length(vv);
                   3670:
                   3671:        /* get the degree bound */
                   3672:        for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   3673:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3674:                        e = getdeg(tv->v,(P)BDY(t));
                   3675:                        max = MAX(e,max);
                   3676:                }
                   3677:        for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3678:                e = getdeg(tv->v,f);
                   3679:                max = MAX(e,max);
                   3680:        }
                   3681:
1.59      noro     3682:        nd_init_ord(ord);
1.61      noro     3683:        nd_setup_parameters(nvar,max);
                   3684:
                   3685:        /* conversion to ndv */
                   3686:        for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   3687:                NEXTNODE(in0,in);
                   3688:                BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
                   3689:        }
                   3690:        NEXTNODE(in0,in);
                   3691:        BDY(in) = (pointer)ptondv(CO,vv,f);
                   3692:        NEXT(in) = 0;
                   3693:
                   3694:        ndv_setup(m,0,in0);
1.59      noro     3695:        nd_psn--;
                   3696:        nd_scale=2;
                   3697:        while ( 1 ) {
                   3698:                nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
                   3699:                stat = nd_nf(m,nd,nd_ps,1,&nf);
                   3700:                if ( !stat ) {
                   3701:                        nd_psn++;
                   3702:                        nd_reconstruct(m,0,0);
                   3703:                        nd_psn--;
                   3704:                } else
                   3705:                        break;
                   3706:        }
1.61      noro     3707:        *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     3708: }
                   3709:
                   3710: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   3711: {
                   3712:        NM m;
                   3713:        UINT *t,*s;
                   3714:        int i;
                   3715:
                   3716:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3717:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   3718:                t = DL(m);
                   3719:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3720:                r[i] = CM(m);
                   3721:        }
                   3722:        for ( i = 0; !r[i]; i++ );
                   3723:        return i;
                   3724: }
                   3725:
                   3726: int nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair,UINT *r)
                   3727: {
                   3728:        NM m;
                   3729:        NMV mr;
                   3730:        UINT *d,*t,*s;
                   3731:        NDV p;
                   3732:        int i,j,len;
                   3733:
                   3734:        m = pair->mul;
                   3735:        d = DL(m);
                   3736:        p = nd_ps[pair->index];
                   3737:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3738:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3739:        len = LEN(p);
                   3740:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   3741:                ndl_add(d,DL(mr),t);
                   3742:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3743:                r[i] = CM(mr);
                   3744:        }
                   3745:        for ( i = 0; !r[i]; i++ );
                   3746:        return i;
                   3747: }
                   3748:
1.64    ! noro     3749: void nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair,UINT *r,int *ind)
        !          3750: {
        !          3751:        NM m;
        !          3752:        NMV mr;
        !          3753:        UINT *d,*t,*s;
        !          3754:        NDV p;
        !          3755:        int i,j,len;
        !          3756:
        !          3757:        m = pair->mul;
        !          3758:        d = DL(m);
        !          3759:        p = nd_ps[pair->index];
        !          3760:        len = LEN(p);
        !          3761:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
        !          3762:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
        !          3763:                ndl_add(d,DL(mr),t);
        !          3764:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
        !          3765:                r[j] = CM(mr);
        !          3766:                ind[j] = i;
        !          3767:        }
        !          3768: }
        !          3769:
1.63      noro     3770: NODE nd_f4(int m)
                   3771: {
                   3772:        int i,nh,stat,index;
                   3773:        NODE r,g;
                   3774:        ND_pairs d,l,t;
                   3775:        ND spol,red;
                   3776:        NDV nf;
                   3777:        NM_ind_pair pair,pair1,pair2;
                   3778:        NM s0,s;
                   3779:        NODE sp0,sp,rp0,rp;
                   3780:        RHist h;
                   3781:        int nsp,nred,col,rank,len,k,j;
                   3782:        NMV mr0,mr;
                   3783:        UINT c,c1,c2,c3;
                   3784:        NM mul,head;
                   3785:        UINT **spmat;
1.64    ! noro     3786:        UINT *s0vect,*rvect,*svect,*p,*ivect;
1.63      noro     3787:        int *colstat;
1.64    ! noro     3788:        int sugar,pos;
1.63      noro     3789:        PGeoBucket bucket;
1.64    ! noro     3790:        int t_0,t_1,t_2,t_3,t_4,t_5,t_c,t_e,t_20,t_21;
1.63      noro     3791:
                   3792:        if ( !m )
                   3793:                error("nd_f4 : not implemented");
                   3794:
                   3795:        g = 0; d = 0;
                   3796:        for ( i = 0; i < nd_psn; i++ ) {
                   3797:                d = update_pairs(d,g,i);
                   3798:                g = update_base(g,i);
                   3799:        }
                   3800:        while ( d ) {
                   3801:                l = nd_minsugarp(d,&d);
                   3802:                sugar = SG(l);
                   3803:                sp0 = 0;
                   3804:                bucket = create_pbucket();
1.64    ! noro     3805:                t_0 = clock();
1.63      noro     3806:                for ( t = l, nsp = 0; t; t = NEXT(t) ) {
                   3807:                        stat = nd_sp(m,0,t,&spol);
                   3808:                        if ( !stat ) goto again;
                   3809:                        if ( spol ) {
                   3810:                                NEXTNODE(sp0,sp); BDY(sp) = (pointer)nd_dup(spol);
                   3811:                                add_pbucket_symbolic(bucket,spol);
                   3812:                                nsp++;
                   3813:                        }
                   3814:                }
                   3815:                if ( sp0 ) NEXT(sp) = 0;
                   3816:                s0 = 0;
                   3817:                rp0 = 0;
                   3818:                while ( 1 ) {
                   3819:                        head = remove_head_pbucket_symbolic(bucket);
                   3820:                        if ( !head ) break;
                   3821:                        if ( !s0 ) s0 = head;
                   3822:                        else NEXT(s) = head;
                   3823:                        s = head;
                   3824:                        index = ndl_find_reducer(DL(head));
                   3825:                        if ( index >= 0 ) {
                   3826:                                h = nd_psh[index];
                   3827:                                NEWNM(mul);
                   3828:                                ndl_sub(DL(head),DL(h),DL(mul));
                   3829:                                if ( ndl_check_bound2(index,DL(mul)) ) goto again;
                   3830:                                MKNM_ind_pair(pair,mul,index);
                   3831:                                red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
                   3832:                                add_pbucket_symbolic(bucket,nd_remove_head(red));
                   3833:                                NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   3834:                                nred++;
                   3835:                        }
                   3836:                }
1.64    ! noro     3837:                t_1 = clock();
1.63      noro     3838:                if ( s0 ) NEXT(s) = 0;
                   3839:                for ( i = 0, s = s0; s; s = NEXT(s), i++ );
                   3840:                col = i;
                   3841:                s0vect = (UINT *)MALLOC(col*nd_wpd*sizeof(UINT));
                   3842:                for ( i = 0, p = s0vect, s = s0; i < col;
                   3843:                        i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   3844:                spmat = (UINT **)MALLOC(nsp*sizeof(UINT *));
                   3845:                for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   3846:                        spmat[i] = (UINT *)MALLOC(col*sizeof(UINT));
                   3847:                        nd_to_vect(m,s0vect,col,BDY(sp),spmat[i]);
                   3848:                }
1.64    ! noro     3849:                t_2 = clock();
        !          3850:                t_c = 0;
        !          3851:                t_e = 0;
1.63      noro     3852:                rvect = (UINT *)ALLOCA(col*sizeof(UINT));
1.64    ! noro     3853:                ivect = (int *)ALLOCA(col*sizeof(int));
1.63      noro     3854:                for ( rp = rp0; rp; rp = NEXT(rp) ) {
1.64    ! noro     3855:                        t_20 = clock();
        !          3856:                        nm_ind_pair_to_vect_compress(m,s0vect,col,(NM_ind_pair)BDY(rp),rvect,ivect);
        !          3857:                        t_21 = clock();
        !          3858:                        t_c += t_21-t_20;
        !          3859:                        k = ivect[0];
        !          3860:                        len = LEN(nd_ps[((NM_ind_pair)BDY(rp))->index]);
1.63      noro     3861:                        for ( i = 0; i < nsp; i++ ) {
                   3862:                                svect = spmat[i];
                   3863:                                if ( c = svect[k] )
1.64    ! noro     3864:                                        for ( j = 0; j < len; j++ ) {
        !          3865:                                                pos = ivect[j];
        !          3866:                                                c1 = m-rvect[j];
        !          3867:                                                c2 = svect[pos]; DMAR(c1,c,c2,m,c3);
        !          3868:                                                svect[pos] = c3;
1.63      noro     3869:                                        }
                   3870:                        }
1.64    ! noro     3871:                        t_e +=  clock()-t_21;
1.63      noro     3872:                }
1.64    ! noro     3873:                t_4 = clock();
1.63      noro     3874:                colstat = (int *)ALLOCA(col*sizeof(int));
                   3875:                rank = generic_gauss_elim_mod(spmat,nsp,col,m,colstat);
1.64    ! noro     3876:                t_5 = clock();
        !          3877:                fprintf(asir_out,"sugar=%d,rank=%d ",sugar,rank);
        !          3878:                fprintf(asir_out,"symb=%f,conv1=%f,conv2=%f,elim1=%f,elim2=%f\n",
        !          3879:                (t_1-t_0)/(double)CLOCKS_PER_SEC,
        !          3880:                (t_2-t_1)/(double)CLOCKS_PER_SEC,
        !          3881:                (t_c)/(double)CLOCKS_PER_SEC,
        !          3882:                (t_e)/(double)CLOCKS_PER_SEC,
        !          3883:                (t_5-t_4)/(double)CLOCKS_PER_SEC);
1.63      noro     3884:                if ( rank )
                   3885:                        for ( j = 0, i = 0; j < col; j++ ) {
                   3886:                                if ( colstat[j] ) {
                   3887:                                        for ( k = j, len = 0; k < col; k++ )
                   3888:                                                if ( spmat[i][k] ) len++;
                   3889:                                        mr0 = (NMV)MALLOC_ATOMIC(nmv_adv*len);
                   3890:                                        mr = mr0;
                   3891:                                        p = s0vect+nd_wpd*j;
                   3892:                                        for ( k = j; k < col; k++, p += nd_wpd )
                   3893:                                                if ( spmat[i][k] ) {
                   3894:                                                        ndl_copy(p,DL(mr));
                   3895:                                                        CM(mr) = spmat[i][k];
                   3896:                                                        NMV_ADV(mr);
                   3897:                                                }
                   3898:                                        MKNDV(nd_nvar,mr0,len,nf);
                   3899:                                        SG(nf) = sugar;
                   3900:                                        ndv_removecont(m,nf);
                   3901:                                        nh = ndv_newps(nf,0);
                   3902:                                        d = update_pairs(d,g,nh);
                   3903:                                        g = update_base(g,nh);
                   3904:                                        i++;
                   3905:                                }
                   3906:                        }
                   3907:                continue;
                   3908:
                   3909: again:
                   3910:                for ( t = l; NEXT(t); t = NEXT(t) );
                   3911:                NEXT(t) = d; d = l;
                   3912:                d = nd_reconstruct(m,0,d);
                   3913:                NEWNM(mul);
                   3914:        }
                   3915:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
                   3916:        return g;
1.59      noro     3917: }

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