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

1.55    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.54 2003/08/31 07:42:23 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))
                    188:
                    189: /* external functions */
                    190: void GC_gcollect();
                    191: NODE append_one(NODE,int);
1.1       noro      192:
1.40      noro      193: /* manipulation of coefficients */
1.20      noro      194: void nd_removecont(int mod,ND p);
1.21      noro      195: void nd_removecont2(ND p1,ND p2);
1.40      noro      196: void removecont_array(Q *c,int n);
                    197:
                    198: /* GeoBucket functions */
1.25      noro      199: ND normalize_pbucket(int mod,PGeoBucket g);
                    200: int head_pbucket(int mod,PGeoBucket g);
1.26      noro      201: int head_pbucket_q(PGeoBucket g);
1.31      noro      202: void add_pbucket(int mod,PGeoBucket g,ND d);
1.25      noro      203: void free_pbucket(PGeoBucket b);
1.26      noro      204: void mulq_pbucket(PGeoBucket g,Q c);
1.25      noro      205: PGeoBucket create_pbucket();
1.20      noro      206:
1.40      noro      207: /* manipulation of pairs and bases */
1.39      noro      208: int nd_newps(int mod,ND a,ND aq);
1.40      noro      209: ND_pairs nd_newpairs( NODE g, int t );
1.1       noro      210: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
                    211: NODE update_base(NODE nd,int ndp);
1.40      noro      212: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
                    213: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
                    214: ND_pairs crit_B( ND_pairs d, int s );
                    215: ND_pairs crit_M( ND_pairs d1 );
                    216: ND_pairs crit_F( ND_pairs d1 );
1.1       noro      217: int crit_2( int dp1, int dp2 );
1.40      noro      218:
                    219: /* top level functions */
                    220: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp);
1.52      noro      221: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
1.27      noro      222: NODE nd_gb(int m,int checkonly);
1.23      noro      223: NODE nd_gb_trace(int m);
1.40      noro      224:
                    225: /* ndl functions */
1.39      noro      226: int ndl_weight(unsigned int *d);
1.43      noro      227: int ndl_weight_mask(unsigned int *d,int i);
1.34      noro      228: void ndl_dehomogenize(unsigned int *p);
1.43      noro      229: void ndl_reconstruct(int obpe,EPOS oepos,unsigned int *d,unsigned int *r);
1.40      noro      230: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2);
                    231: INLINE int ndl_lex_compare(unsigned int *d1,unsigned int *d2);
1.43      noro      232: INLINE int ndl_block_compare(unsigned int *d1,unsigned int *d2);
1.40      noro      233: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2);
                    234: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2);
                    235: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d);
1.55    ! noro      236: INLINE void ndl_addto(unsigned int *d1,unsigned int *d2);
1.40      noro      237: INLINE void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d);
                    238: INLINE int ndl_hash_value(unsigned int *d);
1.45      noro      239:
                    240: /* normal forms */
1.40      noro      241: INLINE int nd_find_reducer(ND g);
                    242: INLINE int nd_find_reducer_direct(ND g,NDV *ps,int len);
1.53      noro      243: int nd_sp(int mod,int trace,ND_pairs p,ND *nf);
                    244: int nd_nf(int mod,ND g,NDV *ps,int full,ND *nf);
                    245: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *nf);
1.45      noro      246: int nd_nf_direct(int mod,ND g,BaseSet base,int full,ND *rp);
1.40      noro      247:
                    248: /* finalizers */
                    249: NODE nd_reducebase(NODE x);
1.23      noro      250: NODE nd_reduceall(int m,NODE f);
1.27      noro      251: int nd_gbcheck(int m,NODE f);
                    252: int nd_membercheck(int m,NODE f);
1.40      noro      253:
                    254: /* allocators */
                    255: void nd_free_private_storage();
                    256: void _NM_alloc();
                    257: void _ND_alloc();
1.1       noro      258: void nd_free(ND p);
1.40      noro      259: void nd_free_redlist();
                    260:
                    261: /* printing */
1.1       noro      262: void ndl_print(unsigned int *dl);
                    263: void nd_print(ND p);
1.16      noro      264: void nd_print_q(ND p);
1.1       noro      265: void ndp_print(ND_pairs d);
1.40      noro      266:
                    267:
                    268: /* setup, reconstruct */
                    269: void nd_init_ord(struct order_spec *spec);
                    270: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs ndp);
                    271: void nd_reconstruct_direct(int mod,NDV *ps,int len);
                    272: void nd_setup(int mod,int trace,NODE f);
                    273: void nd_setup_parameters();
1.43      noro      274: BlockMask nd_create_blockmask(struct order_spec *ord);
1.48      noro      275: int nd_get_exporigin(struct order_spec *ord);
1.40      noro      276:
                    277: /* ND functions */
                    278: int nd_check_candidate(NODE input,NODE cand);
                    279: void nd_mul_c(int mod,ND p,int mul);
                    280: void nd_mul_c_q(ND p,Q mul);
                    281: ND nd_remove_head(ND p);
1.1       noro      282: int nd_length(ND p);
1.34      noro      283: void nd_append_red(unsigned int *d,int i);
1.45      noro      284: unsigned int *ndv_compute_bound(NDV p);
1.5       noro      285: unsigned int *dp_compute_bound(DP p);
1.6       noro      286: ND nd_copy(ND p);
1.40      noro      287: ND nd_add(int mod,ND p1,ND p2);
                    288: ND nd_add_q(ND p1,ND p2);
1.41      noro      289: INLINE int nd_length(ND p);
1.4       noro      290:
1.40      noro      291: /* NDV functions */
1.55    ! noro      292: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p);
        !           293: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen);
        !           294: void weyl_mul_nm_nmv_q(int n,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:
1.34      noro     1767: /* XXX : check is necessary */
                   1768:
1.1       noro     1769: ND_pairs crit_M( ND_pairs d1 )
                   1770: {
                   1771:        ND_pairs e,d2,d3,dd,p;
                   1772:        unsigned int *id,*jd;
                   1773:
                   1774:        for ( dd = 0, e = d1; e; e = d3 ) {
                   1775:                if ( !(d2 = NEXT(e)) ) {
                   1776:                        NEXT(e) = dd;
                   1777:                        return e;
                   1778:                }
1.34      noro     1779:                id = LCM(e);
1.1       noro     1780:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     1781:                        p = NEXT(d2);
                   1782:                        jd = LCM(d2);
                   1783:                        if ( ndl_equal(jd,id) )
                   1784:                                ;
                   1785:                        else if ( TD(jd) > TD(id) )
1.1       noro     1786:                                if ( ndl_reducible(jd,id) ) continue;
                   1787:                                else ;
1.34      noro     1788:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     1789:                        NEXT(d2) = d3;
                   1790:                        d3 = d2;
                   1791:                }
                   1792:                NEXT(e) = dd;
                   1793:                dd = e;
                   1794:                continue;
                   1795:                /**/
                   1796:        delit:  NEXT(d2) = d3;
                   1797:                d3 = d2;
                   1798:                for ( ; p; p = d2 ) {
                   1799:                        d2 = NEXT(p);
                   1800:                        NEXT(p) = d3;
                   1801:                        d3 = p;
                   1802:                }
                   1803:                FREENDP(e);
                   1804:        }
                   1805:        return dd;
                   1806: }
                   1807:
                   1808: ND_pairs crit_F( ND_pairs d1 )
                   1809: {
                   1810:        ND_pairs rest, head,remove;
                   1811:        ND_pairs last, p, r, w;
                   1812:        int s;
                   1813:
                   1814:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   1815:                r = w = equivalent_pairs(p,&rest);
1.14      noro     1816:                s = SG(r);
1.1       noro     1817:                w = NEXT(w);
                   1818:                while ( w ) {
                   1819:                        if ( crit_2(w->i1,w->i2) ) {
                   1820:                                r = w;
                   1821:                                w = NEXT(w);
                   1822:                                while ( w ) {
                   1823:                                        remove = w;
                   1824:                                        w = NEXT(w);
                   1825:                                        FREENDP(remove);
                   1826:                                }
                   1827:                                break;
1.14      noro     1828:                        } else if ( SG(w) < s ) {
1.1       noro     1829:                                FREENDP(r);
                   1830:                                r = w;
1.14      noro     1831:                                s = SG(r);
1.1       noro     1832:                                w = NEXT(w);
                   1833:                        } else {
                   1834:                                remove = w;
                   1835:                                w = NEXT(w);
                   1836:                                FREENDP(remove);
                   1837:                        }
                   1838:                }
                   1839:                if ( last ) NEXT(last) = r;
                   1840:                else head = r;
                   1841:                NEXT(last = r) = 0;
                   1842:                p = rest;
                   1843:                if ( !p ) return head;
                   1844:        }
                   1845:        if ( !last ) return p;
                   1846:        NEXT(last) = p;
                   1847:        return head;
                   1848: }
                   1849:
                   1850: int crit_2( int dp1, int dp2 )
                   1851: {
1.20      noro     1852:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     1853: }
                   1854:
1.40      noro     1855: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     1856: {
                   1857:        ND_pairs w,p,r,s;
                   1858:        unsigned int *d;
                   1859:
                   1860:        w = d1;
1.34      noro     1861:        d = LCM(w);
1.1       noro     1862:        s = NEXT(w);
                   1863:        NEXT(w) = 0;
                   1864:        for ( r = 0; s; s = p ) {
                   1865:                p = NEXT(s);
1.34      noro     1866:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     1867:                        NEXT(s) = w; w = s;
1.1       noro     1868:                } else {
1.39      noro     1869:                        NEXT(s) = r; r = s;
1.1       noro     1870:                }
                   1871:        }
                   1872:        *prest = r;
                   1873:        return w;
                   1874: }
                   1875:
                   1876: NODE update_base(NODE nd,int ndp)
                   1877: {
                   1878:        unsigned int *dl, *dln;
                   1879:        NODE last, p, head;
                   1880:
1.20      noro     1881:        dl = DL(nd_psh[ndp]);
1.1       noro     1882:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     1883:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     1884:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     1885:                        p = NEXT(p);
                   1886:                        if ( last ) NEXT(last) = p;
                   1887:                } else {
                   1888:                        if ( !last ) head = p;
                   1889:                        p = NEXT(last = p);
                   1890:                }
                   1891:        }
                   1892:        head = append_one(head,ndp);
                   1893:        return head;
                   1894: }
                   1895:
                   1896: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   1897: {
                   1898:        ND_pairs m,ml,p,l;
                   1899:        unsigned int *lcm;
1.33      noro     1900:        int s,td,len,tlen,c,c1;
1.1       noro     1901:
                   1902:        if ( !(p = NEXT(m = d)) ) {
                   1903:                *prest = p;
                   1904:                NEXT(m) = 0;
                   1905:                return m;
                   1906:        }
1.14      noro     1907:        s = SG(m);
1.33      noro     1908:        for ( ml = 0, l = m; p; p = NEXT(l = p) )
1.34      noro     1909:                if ( (SG(p) < s)
                   1910:                        || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
1.39      noro     1911:                        ml = l; m = p; s = SG(m);
1.1       noro     1912:                }
                   1913:        if ( !ml ) *prest = NEXT(m);
                   1914:        else {
                   1915:                NEXT(ml) = NEXT(m);
                   1916:                *prest = d;
                   1917:        }
                   1918:        NEXT(m) = 0;
                   1919:        return m;
                   1920: }
                   1921:
1.39      noro     1922: int nd_newps(int mod,ND a,ND aq)
1.1       noro     1923: {
1.3       noro     1924:        int len;
1.13      noro     1925:        RHist r;
1.20      noro     1926:        NDV b;
1.3       noro     1927:
1.53      noro     1928:        if ( aq ) {
                   1929:                /* trace lifting */
                   1930:                if ( !rem(NM(HCQ(aq)),mod) )
                   1931:                        return -1;
                   1932:        }
1.1       noro     1933:        if ( nd_psn == nd_pslen ) {
                   1934:                nd_pslen *= 2;
1.11      noro     1935:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     1936:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     1937:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.1       noro     1938:                nd_bound = (unsigned int **)
                   1939:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
                   1940:        }
1.39      noro     1941:        NEWRHist(r); nd_psh[nd_psn] = r;
1.53      noro     1942:        nd_removecont(mod,a); nd_ps[nd_psn] = ndtondv(mod,a);
1.39      noro     1943:        if ( aq ) {
1.53      noro     1944:                nd_removecont(0,aq); nd_ps_trace[nd_psn] = ndtondv(0,aq);
                   1945:                nd_bound[nd_psn] = ndv_compute_bound(nd_ps_trace[nd_psn]);
1.39      noro     1946:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     1947:                nd_free(a); nd_free(aq);
                   1948:        } else {
                   1949:                nd_bound[nd_psn] = ndv_compute_bound(nd_ps[nd_psn]);
                   1950:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
                   1951:                nd_free(a);
1.39      noro     1952:        }
1.1       noro     1953:        return nd_psn++;
                   1954: }
                   1955:
1.39      noro     1956: void nd_setup(int mod,int trace,NODE f)
1.1       noro     1957: {
1.5       noro     1958:        int i,j,td,len,max;
1.1       noro     1959:        NODE s,s0,f0;
1.5       noro     1960:        unsigned int *d;
1.13      noro     1961:        RHist r;
1.20      noro     1962:        NDV a;
1.11      noro     1963:
                   1964:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     1965:
                   1966:        nd_psn = length(f); nd_pslen = 2*nd_psn;
1.11      noro     1967:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     1968:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     1969:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.1       noro     1970:        nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
1.5       noro     1971:        for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
                   1972:                nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
                   1973:                for ( j = 0; j < nd_nvar; j++ )
                   1974:                        max = MAX(d[j],max);
                   1975:        }
1.11      noro     1976:        if ( !nd_red )
1.13      noro     1977:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
                   1978:        bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.5       noro     1979:
1.34      noro     1980:        if ( max < 2 ) nd_bpe = 2;
                   1981:        else if ( max < 4 ) nd_bpe = 4;
                   1982:        else if ( max < 64 ) nd_bpe = 6;
                   1983:        else if ( max < 256 ) nd_bpe = 8;
                   1984:        else if ( max < 65536 ) nd_bpe = 16;
                   1985:        else nd_bpe = 32;
1.13      noro     1986:
1.1       noro     1987:        nd_setup_parameters();
                   1988:        nd_free_private_storage();
                   1989:        for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.20      noro     1990:                NEWRHist(r);
1.39      noro     1991:                a = dptondv(mod,(DP)BDY(f)); ndv_removecont(mod,a);
1.34      noro     1992:                SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.1       noro     1993:
1.53      noro     1994:                nd_ps[i] = a;
1.39      noro     1995:                if ( trace ) {
                   1996:                        a = dptondv(0,(DP)BDY(f)); ndv_removecont(0,a);
1.53      noro     1997:                        nd_ps_trace[i] = a;
1.39      noro     1998:                }
1.20      noro     1999:                nd_psh[i] = r;
                   2000:        }
                   2001: }
                   2002:
1.1       noro     2003: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp)
                   2004: {
                   2005:        struct order_spec ord1;
                   2006:        VL fv,vv,vc;
                   2007:        NODE fd,fd0,r,r0,t,x,s,xx;
                   2008:        DP a,b,c;
                   2009:
                   2010:        get_vars((Obj)f,&fv); pltovl(v,&vv);
                   2011:        nd_nvar = length(vv);
1.32      noro     2012:        nd_init_ord(ord);
                   2013:        /* XXX for DP */
1.1       noro     2014:        initd(ord);
                   2015:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2016:                ptod(CO,vv,(P)BDY(t),&b);
1.20      noro     2017:                NEXTNODE(fd0,fd); BDY(fd) = (pointer)b;
1.1       noro     2018:        }
                   2019:        if ( fd0 ) NEXT(fd) = 0;
1.39      noro     2020:        nd_setup(m,0,fd0);
1.27      noro     2021:        x = nd_gb(m,0);
1.25      noro     2022:        fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
                   2023:                nd_found,nd_notfirst,nd_create);
1.50      noro     2024:        x = nd_reducebase(x);
1.23      noro     2025:        x = nd_reduceall(m,x);
                   2026:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2027:                NEXTNODE(r0,r);
1.20      noro     2028:                if ( m ) {
1.23      noro     2029:                        a = ndvtodp(m,BDY(t));
1.16      noro     2030:                        _dtop_mod(CO,vv,a,(P *)&BDY(r));
1.20      noro     2031:                } else {
1.23      noro     2032:                        a = ndvtodp(m,BDY(t));
1.16      noro     2033:                        dtop(CO,vv,a,(P *)&BDY(r));
1.20      noro     2034:                }
                   2035:        }
                   2036:        if ( r0 ) NEXT(r) = 0;
                   2037:        MKLIST(*rp,r0);
                   2038: }
                   2039:
1.52      noro     2040: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20      noro     2041: {
                   2042:        struct order_spec ord1;
                   2043:        VL fv,vv,vc;
1.27      noro     2044:        NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.52      noro     2045:        int m,nocheck,nvar,mindex;
1.23      noro     2046:        DP a,b,c,h;
1.27      noro     2047:        P p;
1.20      noro     2048:
                   2049:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.52      noro     2050:        nvar = length(vv);
                   2051:        nocheck = 0;
                   2052:        mindex = 0;
                   2053:
                   2054:        /* setup modulus */
                   2055:        if ( trace < 0 ) {
                   2056:                trace = -trace;
                   2057:                nocheck = 1;
                   2058:        }
                   2059:        m = trace > 1 ? trace : get_lprime(mindex);
                   2060:
1.20      noro     2061:        initd(ord);
1.23      noro     2062:        if ( homo ) {
                   2063:                homogenize_order(ord,nd_nvar,&ord1);
1.27      noro     2064:                for ( fd0 = 0, in0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.23      noro     2065:                        ptod(CO,vv,(P)BDY(t),&c);
                   2066:                        if ( c ) {
                   2067:                                dp_homo(c,&h); NEXTNODE(fd0,fd); BDY(fd) = (pointer)h;
1.27      noro     2068:                                NEXTNODE(in0,in); BDY(in) = (pointer)c;
1.23      noro     2069:                        }
                   2070:                }
                   2071:                if ( fd0 ) NEXT(fd) = 0;
1.27      noro     2072:                if ( in0 ) NEXT(in) = 0;
1.25      noro     2073:        } else {
                   2074:                for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2075:                        ptod(CO,vv,(P)BDY(t),&c);
                   2076:                        if ( c ) {
                   2077:                                NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
1.23      noro     2078:                        }
                   2079:                }
1.25      noro     2080:                if ( fd0 ) NEXT(fd) = 0;
1.27      noro     2081:                in0 = fd0;
                   2082:        }
1.52      noro     2083:        while ( 1 ) {
                   2084:                if ( homo ) {
                   2085:                        nd_init_ord(&ord1);
                   2086:                        initd(&ord1);
                   2087:                        nd_nvar = nvar+1;
                   2088:                } else {
                   2089:                        nd_init_ord(ord);
                   2090:                        nd_nvar = nvar;
                   2091:                }
1.39      noro     2092:                nd_setup(m,1,fd0);
1.27      noro     2093:                cand = nd_gb_trace(m);
1.52      noro     2094:                if ( !cand ) {
                   2095:                        /* failure */
                   2096:                        if ( trace > 1 ) {
                   2097:                                *rp = 0; return;
                   2098:                        } else
                   2099:                                m = get_lprime(++mindex);
                   2100:                        continue;
                   2101:                }
                   2102:
1.27      noro     2103:                if ( homo ) {
                   2104:                        /* dehomogenization */
                   2105:                        for ( t = cand; t; t = NEXT(t) )
1.45      noro     2106:                                ndv_dehomogenize((NDV)BDY(t),ord);
1.52      noro     2107:                        nd_nvar = nvar;
1.45      noro     2108:                        initd(ord);
                   2109:                        nd_init_ord(ord);
1.27      noro     2110:                        nd_setup_parameters();
                   2111:                }
1.50      noro     2112:                cand = nd_reducebase(cand);
1.27      noro     2113:                fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
                   2114:                        nd_found,nd_notfirst,nd_create);
                   2115:                cand = nd_reduceall(0,cand);
                   2116:                initd(ord);
                   2117:                for ( r0 = 0; cand; cand = NEXT(cand) ) {
                   2118:                        NEXTNODE(r0,r);
                   2119:                        BDY(r) = (pointer)ndvtodp(0,(NDV)BDY(cand));
                   2120:                }
                   2121:                if ( r0 ) NEXT(r) = 0;
                   2122:                cand = r0;
1.52      noro     2123:                if ( nocheck || nd_check_candidate(in0,cand) )
                   2124:                        /* success */
                   2125:                        break;
                   2126:                else if ( trace > 1 ) {
                   2127:                        /* failure */
                   2128:                        *rp = 0; return;
                   2129:                } else
                   2130:                        /* try the next modulus */
                   2131:                        m = get_lprime(++mindex);
                   2132:        }
1.27      noro     2133:        /* dp->p */
                   2134:        for ( r = cand; r; r = NEXT(r) ) {
                   2135:                dtop(CO,vv,BDY(r),&p);
                   2136:                BDY(r) = (pointer)p;
1.23      noro     2137:        }
1.27      noro     2138:        MKLIST(*rp,cand);
1.1       noro     2139: }
                   2140:
                   2141: void dltondl(int n,DL dl,unsigned int *r)
                   2142: {
                   2143:        unsigned int *d;
1.43      noro     2144:        int i,j,l,s,ord_l,ord_o;
                   2145:        struct order_pair *op;
1.1       noro     2146:
                   2147:        d = dl->d;
1.41      noro     2148:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2149:        if ( nd_blockmask ) {
                   2150:                l = nd_blockmask->n;
                   2151:                op = nd_blockmask->order_pair;
                   2152:                for ( j = 0, s = 0; j < l; j++ ) {
                   2153:                        ord_o = op[j].order;
                   2154:                        ord_l = op[j].length;
                   2155:                        if ( !ord_o )
                   2156:                                for ( i = 0; i < ord_l; i++ )
                   2157:                                        PUT_EXP(r,s+ord_l-i-1,d[s+i]);
                   2158:                        else
                   2159:                                for ( i = 0; i < ord_l; i++ )
                   2160:                                        PUT_EXP(r,s+i,d[s+i]);
                   2161:                        s += ord_l;
                   2162:                }
                   2163:                TD(r) = ndl_weight(r);
                   2164:                for ( j = 0; j < l; j++ )
                   2165:                        r[j+1] = ndl_weight_mask(r,j);
                   2166:        } else {
                   2167:                if ( nd_isrlex )
                   2168:                        for ( i = 0; i < n; i++ ) PUT_EXP(r,n-1-i,d[i]);
                   2169:                else
                   2170:                        for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
                   2171:                TD(r) = ndl_weight(r);
                   2172:        }
1.1       noro     2173: }
                   2174:
1.34      noro     2175: DL ndltodl(int n,unsigned int *ndl)
1.1       noro     2176: {
                   2177:        DL dl;
                   2178:        int *d;
1.43      noro     2179:        int i,j,l,s,ord_l,ord_o;
                   2180:        struct order_pair *op;
1.1       noro     2181:
                   2182:        NEWDL(dl,n);
1.34      noro     2183:        dl->td = TD(ndl);
1.1       noro     2184:        d = dl->d;
1.43      noro     2185:        if ( nd_blockmask ) {
                   2186:                l = nd_blockmask->n;
                   2187:                op = nd_blockmask->order_pair;
                   2188:                for ( j = 0, s = 0; j < l; j++ ) {
                   2189:                        ord_o = op[j].order;
                   2190:                        ord_l = op[j].length;
                   2191:                        if ( !ord_o )
                   2192:                                for ( i = 0; i < ord_l; i++ )
                   2193:                                        d[s+i] = GET_EXP(ndl,s+ord_l-i-1);
                   2194:                        else
                   2195:                                for ( i = 0; i < ord_l; i++ )
                   2196:                                        d[s+i] = GET_EXP(ndl,s+i);
                   2197:                        s += ord_l;
                   2198:                }
                   2199:        } else {
                   2200:                if ( nd_isrlex )
                   2201:                        for ( i = 0; i < n; i++ )
                   2202:                                d[i] = GET_EXP(ndl,n-1-i);
                   2203:                else
                   2204:                        for ( i = 0; i < n; i++ )
                   2205:                                d[i] = GET_EXP(ndl,i);
                   2206:        }
1.1       noro     2207:        return dl;
                   2208: }
                   2209:
1.17      noro     2210: ND dptond(int mod,DP p)
1.1       noro     2211: {
                   2212:        ND d;
                   2213:        NM m0,m;
                   2214:        MP t;
1.31      noro     2215:        int n,l;
1.1       noro     2216:
                   2217:        if ( !p )
                   2218:                return 0;
                   2219:        n = NV(p);
                   2220:        m0 = 0;
1.31      noro     2221:        for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ ) {
1.1       noro     2222:                NEXTNM(m0,m);
1.34      noro     2223:                if ( mod ) CM(m) = ITOS(C(t));
                   2224:                else CQ(m) = (Q)C(t);
1.14      noro     2225:                dltondl(n,DL(t),DL(m));
1.1       noro     2226:        }
                   2227:        NEXT(m) = 0;
1.31      noro     2228:        MKND(n,m0,l,d);
1.14      noro     2229:        SG(d) = SG(p);
1.1       noro     2230:        return d;
                   2231: }
                   2232:
1.17      noro     2233: DP ndtodp(int mod,ND p)
1.1       noro     2234: {
                   2235:        DP d;
                   2236:        MP m0,m;
                   2237:        NM t;
                   2238:        int n;
                   2239:
                   2240:        if ( !p )
                   2241:                return 0;
                   2242:        n = NV(p);
                   2243:        m0 = 0;
                   2244:        for ( t = BDY(p); t; t = NEXT(t) ) {
                   2245:                NEXTMP(m0,m);
1.34      noro     2246:                if ( mod ) C(m) = STOI(CM(t));
                   2247:                else C(m) = (P)CQ(t);
                   2248:                DL(m) = ndltodl(n,DL(t));
1.1       noro     2249:        }
                   2250:        NEXT(m) = 0;
                   2251:        MKDP(n,m0,d);
1.14      noro     2252:        SG(d) = SG(p);
1.1       noro     2253:        return d;
                   2254: }
                   2255:
                   2256: void ndl_print(unsigned int *dl)
                   2257: {
                   2258:        int n;
1.43      noro     2259:        int i,j,l,ord_o,ord_l,s,s0;
                   2260:        struct order_pair *op;
1.1       noro     2261:
                   2262:        n = nd_nvar;
                   2263:        printf("<<");
1.43      noro     2264:        if ( nd_blockmask ) {
                   2265:                l = nd_blockmask->n;
                   2266:                op = nd_blockmask->order_pair;
                   2267:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2268:                        ord_o = op[j].order;
                   2269:                        ord_l = op[j].length;
                   2270:                        if ( !ord_o )
                   2271:                                for ( i = 0, s0 += ord_l; i < ord_l; i++, s++ )
                   2272:                                        printf(s==n-1?"%d":"%d,",GET_EXP(dl,s0-i-1));
                   2273:                        else
                   2274:                                for ( i = 0; i < ord_l; i++, s++ )
                   2275:                                        printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
                   2276:                }
                   2277:        } else {
                   2278:                if ( nd_isrlex )
                   2279:                        for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,n-1-i));
                   2280:                else
                   2281:                        for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
                   2282:        }
1.1       noro     2283:        printf(">>");
                   2284: }
                   2285:
                   2286: void nd_print(ND p)
                   2287: {
                   2288:        NM m;
                   2289:
                   2290:        if ( !p )
                   2291:                printf("0\n");
                   2292:        else {
                   2293:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     2294:                        printf("+%d*",CM(m));
                   2295:                        ndl_print(DL(m));
1.1       noro     2296:                }
                   2297:                printf("\n");
                   2298:        }
                   2299: }
                   2300:
1.16      noro     2301: void nd_print_q(ND p)
                   2302: {
                   2303:        NM m;
                   2304:
                   2305:        if ( !p )
                   2306:                printf("0\n");
                   2307:        else {
                   2308:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2309:                        printf("+");
                   2310:                        printexpr(CO,CQ(m));
                   2311:                        printf("*");
                   2312:                        ndl_print(DL(m));
                   2313:                }
                   2314:                printf("\n");
                   2315:        }
                   2316: }
                   2317:
1.1       noro     2318: void ndp_print(ND_pairs d)
                   2319: {
                   2320:        ND_pairs t;
                   2321:
1.34      noro     2322:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2323:        printf("\n");
                   2324: }
                   2325:
1.20      noro     2326: void nd_removecont(int mod,ND p)
1.16      noro     2327: {
                   2328:        int i,n;
                   2329:        Q *w;
                   2330:        Q dvr,t;
                   2331:        NM m;
1.21      noro     2332:        struct oVECT v;
                   2333:        N q,r;
1.16      noro     2334:
1.34      noro     2335:        if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2336:        else {
                   2337:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   2338:                w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2339:                v.len = n;
                   2340:                v.body = (pointer *)w;
1.34      noro     2341:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2342:                removecont_array(w,n);
                   2343:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16      noro     2344:        }
                   2345: }
                   2346:
1.21      noro     2347: void nd_removecont2(ND p1,ND p2)
                   2348: {
                   2349:        int i,n1,n2,n;
                   2350:        Q *w;
                   2351:        Q dvr,t;
                   2352:        NM m;
                   2353:        struct oVECT v;
                   2354:        N q,r;
                   2355:
                   2356:        if ( !p1 ) {
                   2357:                nd_removecont(0,p2); return;
                   2358:        } else if ( !p2 ) {
                   2359:                nd_removecont(0,p1); return;
                   2360:        }
                   2361:        n1 = nd_length(p1);
                   2362:        n2 = nd_length(p2);
                   2363:        n = n1+n2;
                   2364:        w = (Q *)ALLOCA(n*sizeof(Q));
                   2365:        v.len = n;
                   2366:        v.body = (pointer *)w;
1.34      noro     2367:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   2368:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2369:        removecont_array(w,n);
                   2370:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2371:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2372: }
                   2373:
1.20      noro     2374: void ndv_removecont(int mod,NDV p)
1.16      noro     2375: {
                   2376:        int i,len;
                   2377:        Q *w;
                   2378:        Q dvr,t;
                   2379:        NMV m;
                   2380:
1.20      noro     2381:        if ( mod )
                   2382:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2383:        else {
                   2384:                len = p->len;
                   2385:                w = (Q *)ALLOCA(len*sizeof(Q));
1.34      noro     2386:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
1.20      noro     2387:                sortbynm(w,len);
                   2388:                qltozl(w,len,&dvr);
                   2389:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   2390:                        divq(CQ(m),dvr,&t); CQ(m) = t;
                   2391:                }
1.16      noro     2392:        }
1.21      noro     2393: }
                   2394:
1.45      noro     2395: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2396: {
1.45      noro     2397:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.23      noro     2398:        Q *w;
                   2399:        Q dvr,t;
                   2400:        NMV m,r;
                   2401: #define NEWADV(m) (m = (NMV)(((char *)m)+newadv))
                   2402:
                   2403:        len = p->len;
                   2404:        newnvar = nd_nvar-1;
1.48      noro     2405:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2406:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2407:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2408:                ndl_dehomogenize(DL(m));
1.23      noro     2409:        if ( newwpd != nd_wpd ) {
1.41      noro     2410:                newadv = sizeof(struct oNMV)+(newwpd-1)*sizeof(unsigned int);
1.23      noro     2411:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NEWADV(r), i++ ) {
1.45      noro     2412:                        CQ(r) = CQ(m);
                   2413:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2414:                        adj = nd_exporigin-newexporigin;
                   2415:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2416:                }
                   2417:        }
                   2418:        NV(p)--;
                   2419: }
                   2420:
1.21      noro     2421: void removecont_array(Q *c,int n)
                   2422: {
                   2423:        struct oVECT v;
                   2424:        Q d0,d1,a,u,u1,gcd;
                   2425:        int i;
                   2426:        N qn,rn,gn;
                   2427:        Q *q,*r;
                   2428:
                   2429:        q = (Q *)ALLOCA(n*sizeof(Q));
                   2430:        r = (Q *)ALLOCA(n*sizeof(Q));
                   2431:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   2432:        igcdv_estimate(&v,&d0);
                   2433:        for ( i = 0; i < n; i++ ) {
                   2434:                divn(NM(c[i]),NM(d0),&qn,&rn);
                   2435:                NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   2436:                NTOQ(rn,SGN(c[i]),r[i]);
                   2437:        }
1.34      noro     2438:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2439:        if ( i < n ) {
                   2440:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   2441:                igcdv(&v,&d1);
                   2442:                gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   2443:                divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   2444:                for ( i = 0; i < n; i++ ) {
                   2445:                        mulq(a,q[i],&u);
                   2446:                        if ( r[i] ) {
                   2447:                                divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   2448:                                addq(u,u1,&q[i]);
                   2449:                        } else
                   2450:                                q[i] = u;
                   2451:                }
                   2452:        }
1.34      noro     2453:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2454: }
                   2455:
1.19      noro     2456: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2457: {
                   2458:        NM m;
                   2459:        int c,c1;
                   2460:
1.34      noro     2461:        if ( !p ) return;
1.1       noro     2462:        for ( m = BDY(p); m; m = NEXT(m) ) {
1.14      noro     2463:                c1 = CM(m);
1.19      noro     2464:                DMAR(c1,mul,0,mod,c);
1.14      noro     2465:                CM(m) = c;
1.1       noro     2466:        }
                   2467: }
                   2468:
1.16      noro     2469: void nd_mul_c_q(ND p,Q mul)
                   2470: {
                   2471:        NM m;
                   2472:        Q c;
                   2473:
1.34      noro     2474:        if ( !p ) return;
1.16      noro     2475:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2476:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   2477:        }
                   2478: }
                   2479:
1.1       noro     2480: void nd_free(ND p)
                   2481: {
                   2482:        NM t,s;
                   2483:
1.34      noro     2484:        if ( !p ) return;
1.1       noro     2485:        t = BDY(p);
                   2486:        while ( t ) {
                   2487:                s = NEXT(t);
                   2488:                FREENM(t);
                   2489:                t = s;
                   2490:        }
                   2491:        FREEND(p);
                   2492: }
                   2493:
1.23      noro     2494: void ndv_free(NDV p)
                   2495: {
                   2496:        GC_free(BDY(p));
                   2497: }
                   2498:
1.34      noro     2499: void nd_append_red(unsigned int *d,int i)
1.1       noro     2500: {
1.13      noro     2501:        RHist m,m0;
1.1       noro     2502:        int h;
                   2503:
1.13      noro     2504:        NEWRHist(m);
1.34      noro     2505:        h = ndl_hash_value(d);
1.13      noro     2506:        m->index = i;
1.14      noro     2507:        ndl_copy(d,DL(m));
1.1       noro     2508:        NEXT(m) = nd_red[h];
                   2509:        nd_red[h] = m;
                   2510: }
                   2511:
1.5       noro     2512: unsigned int *dp_compute_bound(DP p)
                   2513: {
                   2514:        unsigned int *d,*d1,*d2,*t;
                   2515:        MP m;
1.7       noro     2516:        int i,l;
1.5       noro     2517:
                   2518:        if ( !p )
                   2519:                return 0;
                   2520:        d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
                   2521:        d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
                   2522:        m = BDY(p);
1.35      noro     2523:        d = DL(m)->d;
                   2524:        for ( i = 0; i < nd_nvar; i++ ) d1[i] = d[i];
1.5       noro     2525:        for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14      noro     2526:                d = DL(m)->d;
1.5       noro     2527:                for ( i = 0; i < nd_nvar; i++ )
                   2528:                        d2[i] = d[i] > d1[i] ? d[i] : d1[i];
                   2529:                t = d1; d1 = d2; d2 = t;
                   2530:        }
1.13      noro     2531:        l = (nd_nvar+31);
1.7       noro     2532:        t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1.35      noro     2533:        for ( i = 0; i < nd_nvar; i++ ) t[i] = d1[i];
                   2534:        for ( ; i < l; i++ ) t[i] = 0;
1.5       noro     2535:        return t;
                   2536: }
                   2537:
1.45      noro     2538: unsigned int *ndv_compute_bound(NDV p)
1.1       noro     2539: {
                   2540:        unsigned int *d1,*d2,*t;
1.45      noro     2541:        int i,l,len;
                   2542:        NMV m;
1.1       noro     2543:
                   2544:        if ( !p )
                   2545:                return 0;
                   2546:        d1 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
                   2547:        d2 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1.45      noro     2548:        len = LEN(p);
                   2549:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   2550:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2551:                ndl_lcm(DL(m),d1,d2);
1.1       noro     2552:                t = d1; d1 = d2; d2 = t;
                   2553:        }
1.12      noro     2554:        l = nd_nvar+31;
1.9       noro     2555:        t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1.42      noro     2556:        for ( i = 0; i < nd_nvar; i++ ) t[i] = GET_EXP(d1,i);
1.35      noro     2557:        for ( ; i < l; i++ ) t[i] = 0;
1.1       noro     2558:        return t;
                   2559: }
                   2560:
1.48      noro     2561: int nd_get_exporigin(struct order_spec *ord)
                   2562: {
1.51      noro     2563:        switch ( ord->id ) {
1.41      noro     2564:                case 0:
1.48      noro     2565:                        return 1;
1.41      noro     2566:                case 1:
                   2567:                        /* block order */
1.43      noro     2568:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     2569:                        return ord->ord.block.length+1;
1.41      noro     2570:                case 2:
1.52      noro     2571:                        error("nd_get_exporigin : matrix order is not supported yet.");
1.41      noro     2572:        }
1.48      noro     2573: }
                   2574:
                   2575: void nd_setup_parameters() {
                   2576:        int i,n,elen;
                   2577:
                   2578:        nd_epw = (sizeof(unsigned int)*8)/nd_bpe;
                   2579:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   2580:
                   2581:        nd_exporigin = nd_get_exporigin(nd_ord);
1.43      noro     2582:        nd_wpd = nd_exporigin+elen;
                   2583:        nd_epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   2584:        for ( i = 0; i < nd_nvar; i++ ) {
                   2585:                nd_epos[i].i = nd_exporigin + i/nd_epw;
                   2586:                nd_epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   2587:        }
1.1       noro     2588:        if ( nd_bpe < 32 ) {
                   2589:                nd_mask0 = (1<<nd_bpe)-1;
                   2590:        } else {
                   2591:                nd_mask0 = 0xffffffff;
                   2592:        }
                   2593:        bzero(nd_mask,sizeof(nd_mask));
                   2594:        nd_mask1 = 0;
                   2595:        for ( i = 0; i < nd_epw; i++ ) {
                   2596:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   2597:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   2598:        }
1.41      noro     2599:        nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
                   2600:        nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int);
1.43      noro     2601:        nd_blockmask = nd_create_blockmask(nd_ord);
1.1       noro     2602: }
                   2603:
1.20      noro     2604: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1       noro     2605: {
1.37      noro     2606:        int i,obpe,oadv,h;
1.13      noro     2607:        NM prev_nm_free_list;
                   2608:        RHist mr0,mr;
                   2609:        RHist r;
1.37      noro     2610:        RHist *old_red;
1.1       noro     2611:        ND_pairs s0,s,t,prev_ndp_free_list;
1.43      noro     2612:        EPOS oepos;
1.15      noro     2613:
1.1       noro     2614:        obpe = nd_bpe;
1.11      noro     2615:        oadv = nmv_adv;
1.43      noro     2616:        oepos = nd_epos;
1.34      noro     2617:        if ( obpe < 4 ) nd_bpe = 4;
                   2618:        else if ( obpe < 6 ) nd_bpe = 6;
                   2619:        else if ( obpe < 8 ) nd_bpe = 8;
                   2620:        else if ( obpe < 16 ) nd_bpe = 16;
                   2621:        else if ( obpe < 32 ) nd_bpe = 32;
                   2622:        else error("nd_reconstruct : exponent too large");
1.5       noro     2623:
1.1       noro     2624:        nd_setup_parameters();
                   2625:        prev_nm_free_list = _nm_free_list;
                   2626:        prev_ndp_free_list = _ndp_free_list;
                   2627:        _nm_free_list = 0;
                   2628:        _ndp_free_list = 0;
1.53      noro     2629:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   2630:        if ( trace )
                   2631:                for ( i = nd_psn-1; i >= 0; i-- )
                   2632:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     2633:        s0 = 0;
                   2634:        for ( t = d; t; t = NEXT(t) ) {
                   2635:                NEXTND_pairs(s0,s);
                   2636:                s->i1 = t->i1;
                   2637:                s->i2 = t->i2;
1.14      noro     2638:                SG(s) = SG(t);
1.43      noro     2639:                ndl_reconstruct(obpe,oepos,LCM(t),LCM(s));
1.1       noro     2640:        }
1.37      noro     2641:
                   2642:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     2643:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     2644:                old_red[i] = nd_red[i];
                   2645:                nd_red[i] = 0;
                   2646:        }
                   2647:        for ( i = 0; i < REDTAB_LEN; i++ )
                   2648:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   2649:                        NEWRHist(mr);
1.13      noro     2650:                        mr->index = r->index;
1.20      noro     2651:                        SG(mr) = SG(r);
1.43      noro     2652:                        ndl_reconstruct(obpe,oepos,DL(r),DL(mr));
1.37      noro     2653:                        h = ndl_hash_value(DL(mr));
                   2654:                        NEXT(mr) = nd_red[h];
                   2655:                        nd_red[h] = mr;
1.6       noro     2656:                }
1.37      noro     2657:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   2658:        old_red = 0;
1.11      noro     2659:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     2660:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.43      noro     2661:                ndl_reconstruct(obpe,oepos,DL(nd_psh[i]),DL(r));
1.13      noro     2662:                nd_psh[i] = r;
1.11      noro     2663:        }
1.1       noro     2664:        if ( s0 ) NEXT(s) = 0;
                   2665:        prev_nm_free_list = 0;
                   2666:        prev_ndp_free_list = 0;
                   2667:        GC_gcollect();
                   2668:        return s0;
                   2669: }
                   2670:
1.23      noro     2671: void nd_reconstruct_direct(int mod,NDV *ps,int len)
                   2672: {
1.37      noro     2673:        int i,obpe,oadv,h;
1.45      noro     2674:        unsigned int **bound;
1.23      noro     2675:        NM prev_nm_free_list;
                   2676:        RHist mr0,mr;
                   2677:        RHist r;
1.37      noro     2678:        RHist *old_red;
1.23      noro     2679:        ND_pairs s0,s,t,prev_ndp_free_list;
1.43      noro     2680:        EPOS oepos;
1.23      noro     2681:
                   2682:        obpe = nd_bpe;
                   2683:        oadv = nmv_adv;
1.43      noro     2684:        oepos = nd_epos;
1.34      noro     2685:        if ( obpe < 4 ) nd_bpe = 4;
                   2686:        else if ( obpe < 6 ) nd_bpe = 6;
                   2687:        else if ( obpe < 8 ) nd_bpe = 8;
                   2688:        else if ( obpe < 16 ) nd_bpe = 16;
                   2689:        else if ( obpe < 32 ) nd_bpe = 32;
                   2690:        else error("nd_reconstruct_direct : exponent too large");
1.23      noro     2691:
                   2692:        nd_setup_parameters();
                   2693:        prev_nm_free_list = _nm_free_list;
                   2694:        prev_ndp_free_list = _ndp_free_list;
1.34      noro     2695:        _nm_free_list = 0; _ndp_free_list = 0;
1.43      noro     2696:        for ( i = len-1; i >= 0; i-- ) ndv_realloc(ps[i],obpe,oadv,oepos);
1.23      noro     2697:        prev_nm_free_list = 0;
                   2698:        prev_ndp_free_list = 0;
                   2699:        GC_gcollect();
                   2700: }
                   2701:
1.43      noro     2702: void ndl_reconstruct(int obpe,EPOS oepos,unsigned int *d,unsigned int *r)
1.1       noro     2703: {
1.43      noro     2704:        int n,i,ei,oepw,omask0,j,s,ord_l,ord_o,l;
                   2705:        struct order_pair *op;
                   2706: #define GET_EXP_OLD(d,a) (((d)[oepos[a].i]>>oepos[a].s)&omask0)
                   2707: #define PUT_EXP_OLD(r,a,e) ((r)[oepos[a].i] |= ((e)<<oepos[a].s))
1.1       noro     2708:
                   2709:        n = nd_nvar;
                   2710:        oepw = (sizeof(unsigned int)*8)/obpe;
1.43      noro     2711:        omask0 = (1<<obpe)-1;
1.34      noro     2712:        TD(r) = TD(d);
1.41      noro     2713:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2714:        if ( nd_blockmask ) {
                   2715:                l = nd_blockmask->n;
                   2716:                op = nd_blockmask->order_pair;
                   2717:                for ( i = 1; i < nd_exporigin; i++ )
                   2718:                        r[i] = d[i];
                   2719:                for ( j = 0, s = 0; j < l; j++ ) {
                   2720:                        ord_o = op[j].order;
                   2721:                        ord_l = op[j].length;
                   2722:                        if ( !ord_o )
                   2723:                                for ( i = 0; i < ord_l; i++ ) {
                   2724:                                        ei =  GET_EXP_OLD(d,s+ord_l-i-1);
                   2725:                                        PUT_EXP(r,s+ord_l-i-1,ei);
                   2726:                                }
                   2727:                        else
                   2728:                                for ( i = 0; i < ord_l; i++ ) {
                   2729:                                        ei =  GET_EXP_OLD(d,s+i);
                   2730:                                        PUT_EXP(r,s+i,ei);
                   2731:                                }
                   2732:                        s += ord_l;
1.1       noro     2733:                }
1.43      noro     2734:        } else {
                   2735:                if ( nd_isrlex )
                   2736:                        for ( i = 0; i < n; i++ ) {
                   2737:                                ei = GET_EXP_OLD(d,n-1-i);
                   2738:                                PUT_EXP(r,n-1-i,ei);
                   2739:                        }
                   2740:                else
                   2741:                        for ( i = 0; i < n; i++ ) {
                   2742:                                ei = GET_EXP_OLD(d,i);
                   2743:                                PUT_EXP(r,i,ei);
                   2744:                        }
1.1       noro     2745:        }
                   2746: }
1.3       noro     2747:
1.6       noro     2748: ND nd_copy(ND p)
                   2749: {
                   2750:        NM m,mr,mr0;
1.41      noro     2751:        int c,n;
1.6       noro     2752:        ND r;
                   2753:
                   2754:        if ( !p )
                   2755:                return 0;
                   2756:        else {
                   2757:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   2758:                        NEXTNM(mr0,mr);
1.14      noro     2759:                        CM(mr) = CM(m);
                   2760:                        ndl_copy(DL(m),DL(mr));
1.6       noro     2761:                }
                   2762:                NEXT(mr) = 0;
1.31      noro     2763:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     2764:                SG(r) = SG(p);
1.6       noro     2765:                return r;
                   2766:        }
                   2767: }
                   2768:
1.53      noro     2769: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     2770: {
                   2771:        NM m;
                   2772:        NDV p1,p2;
                   2773:        ND t1,t2;
                   2774:        unsigned int *lcm;
1.31      noro     2775:        int td;
1.11      noro     2776:
1.53      noro     2777:        if ( trace ) {
                   2778:                p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   2779:        } else {
1.20      noro     2780:                p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   2781:        }
1.34      noro     2782:        lcm = LCM(p);
1.11      noro     2783:        NEWNM(m);
1.20      noro     2784:        CQ(m) = HCQ(p2);
1.34      noro     2785:        ndl_sub(lcm,HDL(p1),DL(m));
                   2786:        if ( ndl_check_bound2(p->i1,DL(m)) ) return 0;
1.55    ! noro     2787:        t1 = ndv_mul_nm(mod,m,p1);
1.34      noro     2788:        if ( mod ) CM(m) = mod-HCM(p1);
                   2789:        else chsgnq(HCQ(p1),&CQ(m));
                   2790:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     2791:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     2792:                nd_free(t1);
                   2793:                return 0;
                   2794:        }
1.55    ! noro     2795:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     2796:        *rp = nd_add(mod,t1,t2);
1.11      noro     2797:        FREENM(m);
                   2798:        return 1;
                   2799: }
                   2800:
1.19      noro     2801: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     2802: {
                   2803:        NMV m;
                   2804:        int c,c1,len,i;
                   2805:
1.34      noro     2806:        if ( !p ) return;
1.14      noro     2807:        len = LEN(p);
1.11      noro     2808:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.34      noro     2809:                c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
1.11      noro     2810:        }
                   2811: }
                   2812:
1.16      noro     2813: void ndv_mul_c_q(NDV p,Q mul)
                   2814: {
                   2815:        NMV m;
                   2816:        Q c;
                   2817:        int len,i;
                   2818:
1.34      noro     2819:        if ( !p ) return;
1.16      noro     2820:        len = LEN(p);
                   2821:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   2822:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   2823:        }
                   2824: }
                   2825:
1.55    ! noro     2826: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
        !          2827:        int n2,i,j,l,n,tlen;
        !          2828:        unsigned int *d0;
        !          2829:        NM *tab,*psum;
        !          2830:        ND s,r;
        !          2831:        NM t;
        !          2832:        NMV m1;
        !          2833:
        !          2834:        if ( !p ) return 0;
        !          2835:        n = NV(p); n2 = n>>1;
        !          2836:        d0 = DL(m0);
        !          2837:        l = LEN(p);
        !          2838:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
        !          2839:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
        !          2840:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
        !          2841:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
        !          2842:        for ( i = l-1, m1 = BDY(p)+nmv_adv*(l-1); i >= 0; i--, m1 -= nmv_adv ) {
        !          2843:                /* m0(NM) * m1(NMV) => tab(NM) */
        !          2844:                if ( mod )
        !          2845:                        weyl_mul_nm_nmv(mod,n,m0,m1,tab,tlen);
        !          2846:                else
        !          2847:                        weyl_mul_nm_nmv_q(n,m0,m1,tab,tlen);
        !          2848:                for ( j = 0; j < tlen; j++ ) {
        !          2849:                        if ( tab[j] ) {
        !          2850:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
        !          2851:                        }
        !          2852:                }
        !          2853:        }
        !          2854:        for ( i = tlen-1, r = 0; i >= 0; i-- )
        !          2855:                if ( psum[i] ) {
        !          2856:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
        !          2857:                        MKND(n,psum[i],j,s);
        !          2858:                        r = nd_add(mod,r,s);
        !          2859:                }
        !          2860:        if ( s ) SG(s) = SG(p)+TD(d0);
        !          2861:        return s;
        !          2862: }
        !          2863:
        !          2864: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
        !          2865: {
        !          2866:        int i,n2,j,s,curlen,homo,h,a,b,k,l,min;
        !          2867:        unsigned int *d0,*d1,*d,*ctab;
        !          2868:        unsigned int c0,c1,c;
        !          2869:        NM *p;
        !          2870:        NM m,t;
        !          2871:
        !          2872:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
        !          2873:        if ( !m0 || !m1 ) return;
        !          2874:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
        !          2875:        NEWNM(m); d = DL(m);
        !          2876:        c0 = CM(m0); c1 = CM(m1); DMAR(c1,c,0,mod,c); CM(m) = c;
        !          2877:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
        !          2878:        homo = n&1 ? 1 : 0;
        !          2879:        if ( homo ) {
        !          2880:                /* offset of h-degree */
        !          2881:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
        !          2882:                PUT_EXP(DL(m),n-1,h);
        !          2883:                TD(DL(m)) = h;
        !          2884:                /* XXX other weights */
        !          2885:        }
        !          2886:        tab[0] = m;
        !          2887:        curlen = 1;
        !          2888:        s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
        !          2889:        NEWNM(m); d = DL(m);
        !          2890:        for ( i = 0; i < n2; i++ ) {
        !          2891:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
        !          2892:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
        !          2893:                /* xi^a*(Di^k*xi^l)*Di^b */
        !          2894:                a += l; b += k;
        !          2895:                if ( !k || !l ) {
        !          2896:                        for ( j = 0; j < curlen; j++ )
        !          2897:                                if ( m = tab[j] ) {
        !          2898:                                        d = DL(m);
        !          2899:                                        PUT_EXP(d,i,a); PUT_EXP(d,n2+i,b); TD(d) += s;
        !          2900:                                        /* XXX other weights */
        !          2901:                                }
        !          2902:                        curlen *= k+1;
        !          2903:                        continue;
        !          2904:                }
        !          2905:                min = MIN(k,l);
        !          2906:                ctab = (unsigned int *)ALLOCA((min+1)*sizeof(unsigned int));
        !          2907:                mkwcm(k,l,mod,ctab);
        !          2908:                for ( j = 0; j < nd_wpd; i++ ) d[j] = 0;
        !          2909:                p = tab+curlen;
        !          2910:                for ( j = 1; j <= min; j++ ) {
        !          2911:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
        !          2912:                        h = s-(MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i));
        !          2913:                        if ( homo ) {
        !          2914:                                TD(d) = s;
        !          2915:                                PUT_EXP(d,n-1,h);
        !          2916:                        } else TD(d) = h;
        !          2917:                        /* XXX other weights */
        !          2918:                        c = ctab[j];
        !          2919:                        for ( k = 0; k < curlen; k++, p++ ) {
        !          2920:                                NEWNM(t);
        !          2921:                                ndl_add(DL(tab[k]),d,DL(t));
        !          2922:                                c0 = CM(tab[k]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
        !          2923:                                *p = t;
        !          2924:                        }
        !          2925:                }
        !          2926:                /* destructive for j = 0 */
        !          2927:                PUT_EXP(d,i,a); PUT_EXP(d,n2+i,b);
        !          2928:                h = s-(MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i));
        !          2929:                if ( homo ) {
        !          2930:                        TD(d) = s;
        !          2931:                        PUT_EXP(d,n-1,h);
        !          2932:                } else TD(d) = h;
        !          2933:                /* XXX other weights */
        !          2934:                c = ctab[0];
        !          2935:                p = tab;
        !          2936:                for ( k = 0; k < curlen; k++, p++ ) {
        !          2937:                        ndl_addto(DL(tab[k]),d);
        !          2938:                        c0 = CM(tab[k]); DMAR(c0,c,0,mod,c1); CM(tab[k]) = c1;
        !          2939:                }
        !          2940:                curlen *= k+1;
        !          2941:        }
        !          2942:        FREENM(m);
        !          2943: }
        !          2944:
        !          2945: void weyl_mul_nm_nmv_q(int n,NM m0,NMV m1,NM *tab,int tlen)
        !          2946: {
        !          2947:        int i,n2,j,s,curlen,homo,h,a,b,k,l,min;
        !          2948:        unsigned int *d0,*d1,*d;
        !          2949:        Q *ctab;
        !          2950:        Q c0,c1,c;
        !          2951:        NM *p;
        !          2952:        NM m,t;
        !          2953:
        !          2954:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
        !          2955:        if ( !m0 || !m1 ) return;
        !          2956:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
        !          2957:        NEWNM(m); d = DL(m);
        !          2958:        mulq(CQ(m0),CQ(m1),&CQ(m));
        !          2959:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
        !          2960:        homo = n&1 ? 1 : 0;
        !          2961:        if ( homo ) {
        !          2962:                /* offset of h-degree */
        !          2963:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
        !          2964:                PUT_EXP(DL(m),n-1,h);
        !          2965:                TD(DL(m)) = h;
        !          2966:                /* XXX other weights */
        !          2967:        }
        !          2968:        tab[0] = m;
        !          2969:        curlen = 1;
        !          2970:        s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
        !          2971:        NEWNM(m); d = DL(m);
        !          2972:        for ( i = 0; i < n2; i++ ) {
        !          2973:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
        !          2974:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
        !          2975:                /* xi^a*(Di^k*xi^l)*Di^b */
        !          2976:                a += l; b += k;
        !          2977:                if ( !k || !l ) {
        !          2978:                        for ( j = 0; j < curlen; j++ )
        !          2979:                                if ( m = tab[j] ) {
        !          2980:                                        d = DL(m);
        !          2981:                                        PUT_EXP(d,i,a); PUT_EXP(d,n2+i,b); TD(d) += s;
        !          2982:                                        /* XXX other weights */
        !          2983:                                }
        !          2984:                        curlen *= k+1;
        !          2985:                        continue;
        !          2986:                }
        !          2987:                min = MIN(k,l);
        !          2988:                ctab = (Q *)ALLOCA((min+1)*sizeof(Q));
        !          2989:                mkwc(k,l,ctab);
        !          2990:                for ( j = 0; j < nd_wpd; i++ ) d[j] = 0;
        !          2991:                p = tab+curlen;
        !          2992:                for ( j = 1; j <= min; j++ ) {
        !          2993:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
        !          2994:                        h = s-(MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i));
        !          2995:                        if ( homo ) {
        !          2996:                                TD(d) = s;
        !          2997:                                PUT_EXP(d,n-1,h);
        !          2998:                        } else TD(d) = h;
        !          2999:                        /* XXX other weights */
        !          3000:                        c = ctab[j];
        !          3001:                        for ( k = 0; k < curlen; k++, p++ ) {
        !          3002:                                NEWNM(t);
        !          3003:                                ndl_add(DL(tab[k]),d,DL(t));
        !          3004:                                mulq(CQ(tab[k]),c,&CQ(t));
        !          3005:                                *p = t;
        !          3006:                        }
        !          3007:                }
        !          3008:                /* destructive for j = 0 */
        !          3009:                PUT_EXP(d,i,a); PUT_EXP(d,n2+i,b);
        !          3010:                h = s-(MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i));
        !          3011:                if ( homo ) {
        !          3012:                        TD(d) = s;
        !          3013:                        PUT_EXP(d,n-1,h);
        !          3014:                } else TD(d) = h;
        !          3015:                /* XXX other weights */
        !          3016:                c = ctab[0];
        !          3017:                p = tab;
        !          3018:                for ( k = 0; k < curlen; k++, p++ ) {
        !          3019:                        ndl_addto(DL(tab[k]),d);
        !          3020:                        mulq(CQ(tab[k]),c,&CQ(tab[k]));
        !          3021:                }
        !          3022:                curlen *= k+1;
        !          3023:        }
        !          3024:        FREENM(m);
        !          3025: }
        !          3026:
        !          3027: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3028: {
                   3029:        NM mr,mr0;
                   3030:        NMV m;
                   3031:        unsigned int *d,*dt,*dm;
                   3032:        int c,n,td,i,c1,c2,len;
1.16      noro     3033:        Q q;
1.9       noro     3034:        ND r;
                   3035:
1.34      noro     3036:        if ( !p ) return 0;
1.55    ! noro     3037:        else if ( do_weyl )
        !          3038:                return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3039:        else {
                   3040:                n = NV(p); m = BDY(p);
1.34      noro     3041:                d = DL(m0);
1.14      noro     3042:                len = LEN(p);
1.9       noro     3043:                mr0 = 0;
1.34      noro     3044:                td = TD(d);
1.16      noro     3045:                if ( mod ) {
                   3046:                        c = CM(m0);
                   3047:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3048:                                NEXTNM(mr0,mr);
                   3049:                                c1 = CM(m);
1.19      noro     3050:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3051:                                CM(mr) = c2;
                   3052:                                ndl_add(DL(m),d,DL(mr));
                   3053:                        }
                   3054:                } else {
                   3055:                        q = CQ(m0);
                   3056:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3057:                                NEXTNM(mr0,mr);
                   3058:                                mulq(CQ(m),q,&CQ(mr));
                   3059:                                ndl_add(DL(m),d,DL(mr));
                   3060:                        }
1.4       noro     3061:                }
1.9       noro     3062:                NEXT(mr) = 0;
1.31      noro     3063:                MKND(NV(p),mr0,len,r);
1.34      noro     3064:                SG(r) = SG(p) + TD(d);
1.9       noro     3065:                return r;
1.4       noro     3066:        }
                   3067: }
                   3068:
1.43      noro     3069: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3070: {
1.13      noro     3071:        NMV m,mr,mr0,t;
                   3072:        int len,i,k;
1.11      noro     3073:
1.13      noro     3074: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
                   3075: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.11      noro     3076:
                   3077:        if ( p ) {
1.14      noro     3078:                m = BDY(p); len = LEN(p);
1.34      noro     3079:                mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
1.13      noro     3080:                m = (NMV)((char *)mr0+(len-1)*oadv);
                   3081:                mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3082:                t = (NMV)ALLOCA(nmv_adv);
                   3083:                for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.16      noro     3084:                        CQ(t) = CQ(m);
1.14      noro     3085:                        for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
1.43      noro     3086:                        ndl_reconstruct(obpe,oepos,DL(m),DL(t));
1.16      noro     3087:                        CQ(mr) = CQ(t);
1.14      noro     3088:                        ndl_copy(DL(t),DL(mr));
1.11      noro     3089:                }
                   3090:                BDY(p) = mr0;
                   3091:        }
                   3092: }
                   3093:
1.16      noro     3094: NDV ndtondv(int mod,ND p)
1.3       noro     3095: {
                   3096:        NDV d;
                   3097:        NMV m,m0;
                   3098:        NM t;
                   3099:        int i,len;
                   3100:
1.34      noro     3101:        if ( !p ) return 0;
1.31      noro     3102:        len = LEN(p);
1.34      noro     3103:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.3       noro     3104:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
1.14      noro     3105:                ndl_copy(DL(t),DL(m));
1.16      noro     3106:                CQ(m) = CQ(t);
1.3       noro     3107:        }
                   3108:        MKNDV(NV(p),m0,len,d);
1.23      noro     3109:        SG(d) = SG(p);
                   3110:        return d;
                   3111: }
                   3112:
                   3113: ND ndvtond(int mod,NDV p)
                   3114: {
                   3115:        ND d;
                   3116:        NM m,m0;
                   3117:        NMV t;
                   3118:        int i,len;
                   3119:
1.34      noro     3120:        if ( !p ) return 0;
1.23      noro     3121:        m0 = 0;
                   3122:        len = p->len;
                   3123:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3124:                NEXTNM(m0,m);
                   3125:                ndl_copy(DL(t),DL(m));
                   3126:                CQ(m) = CQ(t);
                   3127:        }
                   3128:        NEXT(m) = 0;
1.31      noro     3129:        MKND(NV(p),m0,len,d);
1.14      noro     3130:        SG(d) = SG(p);
1.3       noro     3131:        return d;
                   3132: }
                   3133:
1.16      noro     3134: NDV dptondv(int mod,DP p)
1.11      noro     3135: {
                   3136:        NDV d;
                   3137:        NMV m0,m;
                   3138:        MP t;
1.20      noro     3139:        DP q;
1.11      noro     3140:        int l,i,n;
                   3141:
1.46      noro     3142:        if ( mod ) {
                   3143:                _dp_mod(p,mod,0,&q); p = q;
                   3144:        }
1.34      noro     3145:        if ( !p ) return 0;
1.11      noro     3146:        for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ );
1.46      noro     3147:        if ( mod )
1.16      noro     3148:                m0 = m = (NMV)MALLOC_ATOMIC(l*nmv_adv);
1.46      noro     3149:        else
1.16      noro     3150:                m0 = m = (NMV)MALLOC(l*nmv_adv);
1.11      noro     3151:        n = NV(p);
1.46      noro     3152:        for ( t = BDY(p); t; t = NEXT(t), NMV_ADV(m) ) {
1.34      noro     3153:                if ( mod ) CM(m) = ITOS(C(t));
                   3154:                else CQ(m) = (Q)C(t);
1.17      noro     3155:                dltondl(n,DL(t),DL(m));
1.11      noro     3156:        }
                   3157:        MKNDV(n,m0,l,d);
1.14      noro     3158:        SG(d) = SG(p);
1.11      noro     3159:        return d;
                   3160: }
                   3161:
1.16      noro     3162: DP ndvtodp(int mod,NDV p)
1.11      noro     3163: {
                   3164:        DP d;
                   3165:        MP m0,m;
                   3166:        NMV t;
                   3167:        int len,i,n;
                   3168:
1.34      noro     3169:        if ( !p ) return 0;
1.11      noro     3170:        m0 = 0;
1.14      noro     3171:        len = LEN(p);
1.11      noro     3172:        n = NV(p);
1.17      noro     3173:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3174:                NEXTMP(m0,m);
1.34      noro     3175:                if ( mod ) C(m) = STOI(CM(t));
                   3176:                else C(m) = (P)CQ(t);
                   3177:                DL(m) = ndltodl(n,DL(t));
1.11      noro     3178:        }
                   3179:        NEXT(m) = 0;
                   3180:        MKDP(NV(p),m0,d);
1.14      noro     3181:        SG(d) = SG(p);
1.11      noro     3182:        return d;
                   3183: }
                   3184:
1.3       noro     3185: void ndv_print(NDV p)
                   3186: {
                   3187:        NMV m;
                   3188:        int i,len;
                   3189:
1.34      noro     3190:        if ( !p ) printf("0\n");
1.3       noro     3191:        else {
1.14      noro     3192:                len = LEN(p);
1.3       noro     3193:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14      noro     3194:                        printf("+%d*",CM(m));
1.16      noro     3195:                        ndl_print(DL(m));
                   3196:                }
                   3197:                printf("\n");
                   3198:        }
                   3199: }
                   3200:
                   3201: void ndv_print_q(NDV p)
                   3202: {
                   3203:        NMV m;
                   3204:        int i,len;
                   3205:
1.34      noro     3206:        if ( !p ) printf("0\n");
1.16      noro     3207:        else {
                   3208:                len = LEN(p);
                   3209:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3210:                        printf("+");
                   3211:                        printexpr(CO,CQ(m));
                   3212:                        printf("*");
1.14      noro     3213:                        ndl_print(DL(m));
1.3       noro     3214:                }
                   3215:                printf("\n");
                   3216:        }
1.25      noro     3217: }
                   3218:
1.27      noro     3219: NODE nd_reducebase(NODE x)
                   3220: {
                   3221:        int len,i,j;
                   3222:        NDV *w;
                   3223:        NODE t,t0;
                   3224:
                   3225:        len = length(x);
                   3226:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3227:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3228:        for ( i = 0; i < len; i++ ) {
                   3229:                for ( j = 0; j < i; j++ ) {
                   3230:                        if ( w[i] && w[j] )
                   3231:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3232:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3233:                }
                   3234:        }
                   3235:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3236:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3237:        }
                   3238:        NEXT(t) = 0; x = t0;
                   3239:        return x;
1.11      noro     3240: }
1.32      noro     3241:
1.43      noro     3242: /* XXX incomplete */
                   3243:
1.32      noro     3244: void nd_init_ord(struct order_spec *ord)
                   3245: {
1.43      noro     3246:        switch ( ord->id ) {
1.32      noro     3247:                case 0:
1.43      noro     3248:                        switch ( ord->ord.simple ) {
                   3249:                                case 0:
                   3250:                                        nd_dcomp = 1;
                   3251:                                        nd_isrlex = 1;
                   3252:                                        break;
                   3253:                                case 1:
                   3254:                                        nd_dcomp = 1;
                   3255:                                        nd_isrlex = 0;
                   3256:                                        break;
                   3257:                                case 2:
                   3258:                                        nd_dcomp = 0;
                   3259:                                        nd_isrlex = 0;
1.45      noro     3260:                                        ndl_compare_function = ndl_lex_compare;
1.43      noro     3261:                                        break;
                   3262:                                default:
                   3263:                                        error("nd_gr : unsupported order");
                   3264:                        }
1.32      noro     3265:                        break;
                   3266:                case 1:
1.43      noro     3267:                        /* XXX */
                   3268:                        nd_dcomp = -1;
1.32      noro     3269:                        nd_isrlex = 0;
1.45      noro     3270:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3271:                        break;
1.43      noro     3272:                case 2:
                   3273:                        error("nd_init_ord : matrix order is not supported yet.");
1.32      noro     3274:                        break;
                   3275:        }
1.41      noro     3276:        nd_ord = ord;
1.32      noro     3277: }
                   3278:
1.43      noro     3279: BlockMask nd_create_blockmask(struct order_spec *ord)
                   3280: {
                   3281:        int n,i,j,s,l;
                   3282:        unsigned int *t;
                   3283:        BlockMask bm;
                   3284:
                   3285:        if ( !ord->id )
                   3286:                return 0;
                   3287:        n = ord->ord.block.length;
                   3288:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   3289:        bm->n = n;
                   3290:        bm->order_pair = ord->ord.block.order_pair;
                   3291:        bm->mask = (unsigned int **)MALLOC(n*sizeof(unsigned int *));
                   3292:        for ( i = 0, s = 0; i < n; i++ ) {
                   3293:                bm->mask[i] = t
                   3294:                        = (unsigned int *)MALLOC_ATOMIC(nd_wpd*sizeof(unsigned int));
                   3295:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   3296:                l = bm->order_pair[i].length;
                   3297:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   3298:        }
                   3299:        return bm;
                   3300: }

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