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

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

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