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

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

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