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

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

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