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

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

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