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

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

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