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

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

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