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

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

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