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

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

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