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

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

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