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

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

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