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

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

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