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

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

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