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

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

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