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

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

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