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

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

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