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

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

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