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

1.90    ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.89 2004/02/03 23:31:57 noro Exp $ */
1.2       noro        2:
1.1       noro        3: #include "ca.h"
1.74      noro        4: #include "parse.h"
                      5: #include "ox.h"
1.87      noro        6: #include "base.h"
1.1       noro        7: #include "inline.h"
1.64      noro        8: #include <time.h>
1.1       noro        9:
                     10: #if defined(__GNUC__)
                     11: #define INLINE inline
                     12: #elif defined(VISUAL)
                     13: #define INLINE __inline
                     14: #else
                     15: #define INLINE
                     16: #endif
                     17:
1.61      noro       18: typedef unsigned int UINT;
                     19:
1.47      noro       20: #define USE_GEOBUCKET 1
1.65      noro       21: #define USE_UNROLL 1
1.28      noro       22:
1.1       noro       23: #define REDTAB_LEN 32003
                     24:
1.40      noro       25: /* GeoBucket for polynomial addition */
                     26:
1.1       noro       27: typedef struct oPGeoBucket {
                     28:        int m;
                     29:        struct oND *body[32];
                     30: } *PGeoBucket;
                     31:
1.40      noro       32: /* distributed polynomial; linked list rep. */
1.1       noro       33: typedef struct oND {
                     34:        struct oNM *body;
                     35:        int nv;
1.31      noro       36:        int len;
1.1       noro       37:        int sugar;
                     38: } *ND;
                     39:
1.40      noro       40: /* distributed polynomial; array rep. */
1.3       noro       41: typedef struct oNDV {
                     42:        struct oNMV *body;
                     43:        int nv;
1.31      noro       44:        int len;
1.3       noro       45:        int sugar;
                     46: } *NDV;
                     47:
1.69      noro       48: typedef union oNDC {
                     49:        int m;
                     50:        Q z;
                     51:        P p;
                     52: } *NDC;
                     53:
1.40      noro       54: /* monomial; linked list rep. */
1.1       noro       55: typedef struct oNM {
                     56:        struct oNM *next;
1.69      noro       57:        union oNDC c;
1.61      noro       58:        UINT dl[1];
1.1       noro       59: } *NM;
                     60:
1.40      noro       61: /* monomial; array rep. */
1.3       noro       62: typedef struct oNMV {
1.69      noro       63:        union oNDC c;
1.61      noro       64:        UINT dl[1];
1.3       noro       65: } *NMV;
                     66:
1.40      noro       67: /* history of reducer */
1.13      noro       68: typedef struct oRHist {
                     69:        struct oRHist *next;
                     70:        int index;
1.34      noro       71:        int sugar;
1.61      noro       72:        UINT dl[1];
1.13      noro       73: } *RHist;
                     74:
1.40      noro       75: /* S-pair list */
1.1       noro       76: typedef struct oND_pairs {
                     77:        struct oND_pairs *next;
                     78:        int i1,i2;
1.34      noro       79:        int sugar;
1.61      noro       80:        UINT lcm[1];
1.1       noro       81: } *ND_pairs;
                     82:
1.42      noro       83: /* index and shift count for each exponent */
                     84: typedef struct oEPOS {
                     85:        int i; /* index */
                     86:        int s; /* shift */
                     87: } *EPOS;
                     88:
1.43      noro       89: typedef struct oBlockMask {
                     90:        int n;
                     91:        struct order_pair *order_pair;
1.61      noro       92:        UINT **mask;
1.43      noro       93: } *BlockMask;
                     94:
1.45      noro       95: typedef struct oBaseSet {
                     96:        int len;
                     97:        NDV *ps;
1.61      noro       98:        UINT **bound;
1.45      noro       99: } *BaseSet;
                    100:
1.63      noro      101: typedef struct oNM_ind_pair
                    102: {
                    103:        NM mul;
1.76      noro      104:        int index,sugar;
1.63      noro      105: } *NM_ind_pair;
                    106:
1.67      noro      107: typedef struct oIndArray
                    108: {
                    109:        char width;
                    110:        int head;
                    111:        union {
                    112:                unsigned char *c;
                    113:                unsigned short *s;
                    114:                unsigned int *i;
                    115:        } index;
                    116: } *IndArray;
1.63      noro      117:
1.61      noro      118: int (*ndl_compare_function)(UINT *a1,UINT *a2);
1.32      noro      119:
1.74      noro      120: static int ndv_alloc;
1.87      noro      121: #if 1
1.69      noro      122: static int nd_f4_nsp=0x7fffffff;
1.87      noro      123: #else
                    124: static int nd_f4_nsp=50;
                    125: #endif
1.42      noro      126: static double nd_scale=2;
1.61      noro      127: static UINT **nd_bound;
1.42      noro      128: static struct order_spec *nd_ord;
                    129: static EPOS nd_epos;
1.43      noro      130: static BlockMask nd_blockmask;
1.42      noro      131: static int nd_nvar;
                    132: static int nd_isrlex;
                    133: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
1.61      noro      134: static UINT nd_mask[32];
                    135: static UINT nd_mask0,nd_mask1;
1.42      noro      136:
                    137: static NM _nm_free_list;
                    138: static ND _nd_free_list;
                    139: static ND_pairs _ndp_free_list;
1.20      noro      140:
                    141: static NDV *nd_ps;
1.53      noro      142: static NDV *nd_ps_trace;
1.42      noro      143: static RHist *nd_psh;
                    144: static int nd_psn,nd_pslen;
1.20      noro      145:
1.42      noro      146: static RHist *nd_red;
1.1       noro      147:
1.42      noro      148: static int nd_found,nd_create,nd_notfirst;
                    149: static int nmv_adv;
                    150: static int nd_dcomp;
1.77      noro      151: static int nd_demand;
1.1       noro      152:
1.89      noro      153: extern struct order_spec *dp_current_spec;
1.77      noro      154: extern char *Demand;
1.61      noro      155: extern VL CO;
1.77      noro      156: extern int Top,Reverse,DP_Print,dp_nelim,do_weyl,NoSugar;
1.58      noro      157: extern int *current_weyl_weight_vector;
1.1       noro      158:
1.40      noro      159: /* fundamental macros */
1.34      noro      160: #define TD(d) (d[0])
1.1       noro      161: #define HDL(d) ((d)->body->dl)
1.34      noro      162: #define HTD(d) (TD(HDL(d)))
1.14      noro      163: #define HCM(d) ((d)->body->c.m)
1.16      noro      164: #define HCQ(d) ((d)->body->c.z)
1.61      noro      165: #define HCP(d) ((d)->body->c.p)
1.14      noro      166: #define CM(a) ((a)->c.m)
1.16      noro      167: #define CQ(a) ((a)->c.z)
1.61      noro      168: #define CP(a) ((a)->c.p)
1.14      noro      169: #define DL(a) ((a)->dl)
                    170: #define SG(a) ((a)->sugar)
                    171: #define LEN(a) ((a)->len)
1.33      noro      172: #define LCM(a) ((a)->lcm)
1.42      noro      173: #define GET_EXP(d,a) (((d)[nd_epos[a].i]>>nd_epos[a].s)&nd_mask0)
1.60      noro      174: #define GET_EXP_MASK(d,a,m) ((((d)[nd_epos[a].i]&(m)[nd_epos[a].i])>>nd_epos[a].s)&nd_mask0)
1.42      noro      175: #define PUT_EXP(r,a,e) ((r)[nd_epos[a].i] |= ((e)<<nd_epos[a].s))
1.45      noro      176: #define XOR_EXP(r,a,e) ((r)[nd_epos[a].i] ^= ((e)<<nd_epos[a].s))
1.1       noro      177:
1.61      noro      178: #define GET_EXP_OLD(d,a) (((d)[oepos[a].i]>>oepos[a].s)&omask0)
                    179: #define PUT_EXP_OLD(r,a,e) ((r)[oepos[a].i] |= ((e)<<oepos[a].s))
                    180:
1.90    ! noro      181: #define ROUND_FOR_ALIGN(s) ((((s)+sizeof(void *)-1)/sizeof(void *))*sizeof(void *))
        !           182:
1.40      noro      183: /* macros for term comparison */
1.34      noro      184: #define TD_DL_COMPARE(d1,d2)\
1.41      noro      185: (TD(d1)>TD(d2)?1:(TD(d1)<TD(d2)?-1:ndl_lex_compare(d1,d2)))
1.43      noro      186: #if 0
1.34      noro      187: #define DL_COMPARE(d1,d2)\
1.43      noro      188: (nd_dcomp>0?TD_DL_COMPARE(d1,d2)\
                    189:          :(nd_dcomp==0?ndl_lex_compare(d1,d2)\
                    190:                      :(nd_blockmask?ndl_block_compare(d1,d2)\
1.45      noro      191:                                                                   :(*ndl_compare_function)(d1,d2))))
1.43      noro      192: #else
                    193: #define DL_COMPARE(d1,d2)\
1.45      noro      194: (nd_dcomp>0?TD_DL_COMPARE(d1,d2):(*ndl_compare_function)(d1,d2))
1.43      noro      195: #endif
1.34      noro      196:
1.40      noro      197: /* allocators */
1.15      noro      198: #define NEWRHist(r) \
1.61      noro      199: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(UINT)))
1.34      noro      200: #define NEWND_pairs(m) \
                    201: if(!_ndp_free_list)_NDP_alloc();\
                    202: (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
                    203: #define NEWNM(m)\
                    204: if(!_nm_free_list)_NM_alloc();\
                    205: (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
                    206: #define MKND(n,m,len,d)\
                    207: if(!_nd_free_list)_ND_alloc();\
                    208: (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list);\
                    209: NV(d)=(n); LEN(d)=(len); BDY(d)=(m)
1.40      noro      210: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
                    211: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.63      noro      212: #define NEWNM_ind_pair(p)\
                    213: ((p)=(NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair)))
1.1       noro      214:
1.40      noro      215: /* allocate and link a new object */
1.13      noro      216: #define NEXTRHist(r,c) \
                    217: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1       noro      218: #define NEXTNM(r,c) \
                    219: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
                    220: #define NEXTNM2(r,c,s) \
                    221: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
1.40      noro      222: #define NEXTND_pairs(r,c) \
                    223: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
1.76      noro      224: #define MKNM_ind_pair(p,m,i,s) (NEWNM_ind_pair(p),(p)->mul=(m),(p)->index=(i),(p)->sugar = (s))
1.34      noro      225:
1.40      noro      226: /* deallocators */
1.1       noro      227: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
                    228: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
                    229: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
                    230:
1.40      noro      231: /* macro for increasing pointer to NMV */
                    232: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
1.61      noro      233: #define NMV_OADV(m) (m = (NMV)(((char *)m)+oadv))
                    234: #define NDV_NADV(m) (m = (NMV)(((char *)m)+newadv))
1.56      noro      235: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.61      noro      236: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
                    237:
1.40      noro      238: /* external functions */
1.71      noro      239: #if 1
1.40      noro      240: void GC_gcollect();
1.71      noro      241: #endif
1.40      noro      242: NODE append_one(NODE,int);
1.1       noro      243:
1.40      noro      244: /* manipulation of coefficients */
1.20      noro      245: void nd_removecont(int mod,ND p);
1.21      noro      246: void nd_removecont2(ND p1,ND p2);
1.40      noro      247: void removecont_array(Q *c,int n);
                    248:
                    249: /* GeoBucket functions */
1.25      noro      250: ND normalize_pbucket(int mod,PGeoBucket g);
                    251: int head_pbucket(int mod,PGeoBucket g);
1.26      noro      252: int head_pbucket_q(PGeoBucket g);
1.63      noro      253: void add_pbucket_symbolic(PGeoBucket g,ND d);
1.31      noro      254: void add_pbucket(int mod,PGeoBucket g,ND d);
1.25      noro      255: void free_pbucket(PGeoBucket b);
1.26      noro      256: void mulq_pbucket(PGeoBucket g,Q c);
1.63      noro      257: NM remove_head_pbucket_symbolic(PGeoBucket g);
1.25      noro      258: PGeoBucket create_pbucket();
1.20      noro      259:
1.40      noro      260: /* manipulation of pairs and bases */
1.39      noro      261: int nd_newps(int mod,ND a,ND aq);
1.40      noro      262: ND_pairs nd_newpairs( NODE g, int t );
1.1       noro      263: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
1.63      noro      264: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest );
1.1       noro      265: NODE update_base(NODE nd,int ndp);
1.40      noro      266: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
                    267: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
                    268: ND_pairs crit_B( ND_pairs d, int s );
                    269: ND_pairs crit_M( ND_pairs d1 );
                    270: ND_pairs crit_F( ND_pairs d1 );
1.1       noro      271: int crit_2( int dp1, int dp2 );
1.77      noro      272: int ndv_newps(int m,NDV a,NDV aq);
1.40      noro      273:
                    274: /* top level functions */
1.63      noro      275: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp);
1.52      noro      276: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp);
1.63      noro      277: NODE nd_f4(int m);
1.77      noro      278: NODE nd_gb(int m,int ishomo,int checkonly);
1.82      noro      279: NODE nd_gb_trace(int m,int ishomo);
1.40      noro      280:
                    281: /* ndl functions */
1.61      noro      282: int ndl_weight(UINT *d);
                    283: void ndl_weight_mask(UINT *d);
                    284: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight);
                    285: void ndl_dehomogenize(UINT *p);
                    286: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos);
                    287: INLINE int ndl_reducible(UINT *d1,UINT *d2);
                    288: INLINE int ndl_lex_compare(UINT *d1,UINT *d2);
                    289: INLINE int ndl_block_compare(UINT *d1,UINT *d2);
                    290: INLINE int ndl_equal(UINT *d1,UINT *d2);
                    291: INLINE void ndl_copy(UINT *d1,UINT *d2);
                    292: INLINE void ndl_zero(UINT *d);
                    293: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d);
                    294: INLINE void ndl_addto(UINT *d1,UINT *d2);
                    295: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d);
                    296: INLINE int ndl_hash_value(UINT *d);
1.45      noro      297:
                    298: /* normal forms */
1.63      noro      299: INLINE int ndl_find_reducer(UINT *g);
1.53      noro      300: int nd_sp(int mod,int trace,ND_pairs p,ND *nf);
1.69      noro      301: int nd_nf(int mod,ND g,NDV *ps,int full,NDC dn,ND *nf);
1.53      noro      302: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *nf);
1.40      noro      303:
                    304: /* finalizers */
1.61      noro      305: NODE ndv_reducebase(NODE x);
                    306: NODE ndv_reduceall(int m,NODE f);
1.40      noro      307:
                    308: /* allocators */
                    309: void nd_free_private_storage();
                    310: void _NM_alloc();
                    311: void _ND_alloc();
1.1       noro      312: void nd_free(ND p);
1.40      noro      313: void nd_free_redlist();
                    314:
                    315: /* printing */
1.61      noro      316: void ndl_print(UINT *dl);
1.1       noro      317: void nd_print(ND p);
1.16      noro      318: void nd_print_q(ND p);
1.1       noro      319: void ndp_print(ND_pairs d);
1.40      noro      320:
                    321:
                    322: /* setup, reconstruct */
                    323: void nd_init_ord(struct order_spec *spec);
                    324: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs ndp);
1.61      noro      325: void ndv_setup(int mod,int trace,NODE f);
                    326: void nd_setup_parameters(int nvar,int max);
1.43      noro      327: BlockMask nd_create_blockmask(struct order_spec *ord);
1.57      noro      328: EPOS nd_create_epos(struct order_spec *ord);
1.48      noro      329: int nd_get_exporigin(struct order_spec *ord);
1.61      noro      330: void ndv_mod(int mod,NDV p);
                    331: NDV ndv_dup(int mod,NDV p);
1.63      noro      332: ND nd_dup(ND p);
1.40      noro      333:
                    334: /* ND functions */
1.61      noro      335: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand);
1.40      noro      336: void nd_mul_c(int mod,ND p,int mul);
                    337: void nd_mul_c_q(ND p,Q mul);
1.61      noro      338: void nd_mul_c_p(VL vl,ND p,P mul);
1.40      noro      339: ND nd_remove_head(ND p);
1.69      noro      340: ND nd_separate_head(ND p,ND *head);
1.1       noro      341: int nd_length(ND p);
1.61      noro      342: void nd_append_red(UINT *d,int i);
                    343: UINT *ndv_compute_bound(NDV p);
1.6       noro      344: ND nd_copy(ND p);
1.63      noro      345: ND nd_merge(ND p1,ND p2);
1.40      noro      346: ND nd_add(int mod,ND p1,ND p2);
                    347: ND nd_add_q(ND p1,ND p2);
1.71      noro      348: ND nd_add_sf(ND p1,ND p2);
1.41      noro      349: INLINE int nd_length(ND p);
1.74      noro      350: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0);
                    351: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0);
1.4       noro      352:
1.40      noro      353: /* NDV functions */
1.55      noro      354: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p);
                    355: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen);
1.19      noro      356: void ndv_mul_c(int mod,NDV p,int mul);
1.40      noro      357: void ndv_mul_c_q(NDV p,Q mul);
1.63      noro      358: ND ndv_mul_nm_symbolic(NM m0,NDV p);
1.61      noro      359: ND ndv_mul_nm(int mod,NM m0,NDV p);
1.43      noro      360: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos);
1.61      noro      361: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos);
                    362: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS eops);
1.45      noro      363: void ndv_dehomogenize(NDV p,struct order_spec *spec);
1.40      noro      364: void ndv_removecont(int mod,NDV p);
                    365: void ndv_print(NDV p);
                    366: void ndv_print_q(NDV p);
                    367: void ndv_free(NDV p);
1.77      noro      368: void ndv_save(NDV p,int index);
                    369: NDV ndv_load(int index);
1.40      noro      370:
                    371: /* converters */
1.61      noro      372: ND ptond(VL vl,VL dvl,P p);
                    373: NDV ptondv(VL vl,VL dvl,P p);
                    374: P ndvtop(int mod,VL vl,VL dvl,NDV p);
1.16      noro      375: NDV ndtondv(int mod,ND p);
1.23      noro      376: ND ndvtond(int mod,NDV p);
1.63      noro      377: int nm_ind_pair_to_vect(int m,UINT *s0,int n,NM_ind_pair pair,UINT *r);
1.67      noro      378: IndArray nm_ind_pair_to_vect_compress(int m,UINT *s0,int n,NM_ind_pair pair);
1.63      noro      379: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r);
1.1       noro      380:
1.76      noro      381: /* elimination */
                    382: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat);
                    383: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat);
                    384:
1.1       noro      385: void nd_free_private_storage()
                    386: {
                    387:        _nm_free_list = 0;
1.5       noro      388:        _ndp_free_list = 0;
1.71      noro      389: #if 0
1.1       noro      390:        GC_gcollect();
1.71      noro      391: #endif
1.1       noro      392: }
                    393:
                    394: void _NM_alloc()
                    395: {
                    396:        NM p;
                    397:        int i;
                    398:
1.11      noro      399:        for ( i = 0; i < 1024; i++ ) {
1.61      noro      400:                p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro      401:                p->next = _nm_free_list; _nm_free_list = p;
                    402:        }
                    403: }
                    404:
                    405: void _ND_alloc()
                    406: {
                    407:        ND p;
                    408:        int i;
                    409:
                    410:        for ( i = 0; i < 1024; i++ ) {
                    411:                p = (ND)GC_malloc(sizeof(struct oND));
                    412:                p->body = (NM)_nd_free_list; _nd_free_list = p;
                    413:        }
                    414: }
                    415:
                    416: void _NDP_alloc()
                    417: {
                    418:        ND_pairs p;
                    419:        int i;
                    420:
1.11      noro      421:        for ( i = 0; i < 1024; i++ ) {
1.1       noro      422:                p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
1.61      noro      423:                        +(nd_wpd-1)*sizeof(UINT));
1.1       noro      424:                p->next = _ndp_free_list; _ndp_free_list = p;
                    425:        }
                    426: }
                    427:
1.30      noro      428: INLINE int nd_length(ND p)
1.1       noro      429: {
                    430:        NM m;
                    431:        int i;
                    432:
                    433:        if ( !p )
                    434:                return 0;
                    435:        else {
                    436:                for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
                    437:                return i;
                    438:        }
                    439: }
                    440:
1.61      noro      441: INLINE int ndl_reducible(UINT *d1,UINT *d2)
1.1       noro      442: {
1.61      noro      443:        UINT u1,u2;
1.1       noro      444:        int i,j;
                    445:
1.34      noro      446:        if ( TD(d1) < TD(d2) ) return 0;
1.65      noro      447: #if USE_UNROLL
1.1       noro      448:        switch ( nd_bpe ) {
1.62      noro      449:                case 3:
                    450:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    451:                                u1 = d1[i]; u2 = d2[i];
                    452:                                if ( (u1&0x38000000) < (u2&0x38000000) ) return 0;
                    453:                                if ( (u1& 0x7000000) < (u2& 0x7000000) ) return 0;
                    454:                                if ( (u1&  0xe00000) < (u2&  0xe00000) ) return 0;
                    455:                                if ( (u1&  0x1c0000) < (u2&  0x1c0000) ) return 0;
                    456:                                if ( (u1&   0x38000) < (u2&   0x38000) ) return 0;
                    457:                                if ( (u1&    0x7000) < (u2&    0x7000) ) return 0;
                    458:                                if ( (u1&     0xe00) < (u2&     0xe00) ) return 0;
                    459:                                if ( (u1&     0x1c0) < (u2&     0x1c0) ) return 0;
                    460:                                if ( (u1&      0x38) < (u2&      0x38) ) return 0;
                    461:                                if ( (u1&       0x7) < (u2&       0x7) ) return 0;
                    462:                        }
                    463:                        return 1;
                    464:                        break;
1.1       noro      465:                case 4:
1.41      noro      466:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      467:                                u1 = d1[i]; u2 = d2[i];
                    468:                                if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
1.62      noro      469:                                if ( (u1& 0xf000000) < (u2& 0xf000000) ) return 0;
                    470:                                if ( (u1&  0xf00000) < (u2&  0xf00000) ) return 0;
                    471:                                if ( (u1&   0xf0000) < (u2&   0xf0000) ) return 0;
                    472:                                if ( (u1&    0xf000) < (u2&    0xf000) ) return 0;
                    473:                                if ( (u1&     0xf00) < (u2&     0xf00) ) return 0;
                    474:                                if ( (u1&      0xf0) < (u2&      0xf0) ) return 0;
                    475:                                if ( (u1&       0xf) < (u2&       0xf) ) return 0;
1.1       noro      476:                        }
                    477:                        return 1;
                    478:                        break;
                    479:                case 6:
1.41      noro      480:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      481:                                u1 = d1[i]; u2 = d2[i];
                    482:                                if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
1.62      noro      483:                                if ( (u1&  0xfc0000) < (u2&  0xfc0000) ) return 0;
                    484:                                if ( (u1&   0x3f000) < (u2&   0x3f000) ) return 0;
                    485:                                if ( (u1&     0xfc0) < (u2&     0xfc0) ) return 0;
                    486:                                if ( (u1&      0x3f) < (u2&      0x3f) ) return 0;
1.1       noro      487:                        }
                    488:                        return 1;
                    489:                        break;
                    490:                case 8:
1.41      noro      491:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      492:                                u1 = d1[i]; u2 = d2[i];
                    493:                                if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
1.62      noro      494:                                if ( (u1&  0xff0000) < (u2&  0xff0000) ) return 0;
                    495:                                if ( (u1&    0xff00) < (u2&    0xff00) ) return 0;
                    496:                                if ( (u1&      0xff) < (u2&      0xff) ) return 0;
1.1       noro      497:                        }
                    498:                        return 1;
                    499:                        break;
                    500:                case 16:
1.41      noro      501:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      502:                                u1 = d1[i]; u2 = d2[i];
                    503:                                if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
1.62      noro      504:                                if ( (u1&    0xffff) < (u2&    0xffff) ) return 0;
1.1       noro      505:                        }
                    506:                        return 1;
                    507:                        break;
                    508:                case 32:
1.41      noro      509:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      510:                                if ( d1[i] < d2[i] ) return 0;
                    511:                        return 1;
                    512:                        break;
                    513:                default:
1.41      noro      514:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      515:                                u1 = d1[i]; u2 = d2[i];
                    516:                                for ( j = 0; j < nd_epw; j++ )
                    517:                                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    518:                        }
                    519:                        return 1;
                    520:        }
1.65      noro      521: #else
                    522:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    523:                u1 = d1[i]; u2 = d2[i];
                    524:                for ( j = 0; j < nd_epw; j++ )
                    525:                        if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
                    526:        }
                    527:        return 1;
                    528: #endif
1.1       noro      529: }
                    530:
1.61      noro      531: /*
                    532:  * If the current order is a block order,
                    533:  * then the last block is length 1 and contains
                    534:  * the homo variable. Otherwise, the original
                    535:  * order is either 0 or 2.
                    536:  */
                    537:
                    538: void ndl_homogenize(UINT *d,UINT *r,int obpe,EPOS oepos,int weight)
1.23      noro      539: {
1.61      noro      540:        int w,i,e,n,omask0;
                    541:
                    542:        omask0 = (1<<obpe)-1;
                    543:        n = nd_nvar-1;
                    544:        ndl_zero(r);
                    545:        for ( i = 0; i < n; i++ ) {
                    546:                e = GET_EXP_OLD(d,i);
                    547:                PUT_EXP(r,i,e);
                    548:        }
                    549:        w = TD(d);
                    550:        PUT_EXP(r,nd_nvar-1,weight-w);
                    551:        TD(r) = weight;
                    552:        if ( nd_blockmask ) ndl_weight_mask(r);
                    553: }
                    554:
                    555: void ndl_dehomogenize(UINT *d)
                    556: {
                    557:        UINT mask;
                    558:        UINT h;
1.31      noro      559:        int i,bits;
1.23      noro      560:
1.44      noro      561:        if ( nd_blockmask ) {
                    562:                h = GET_EXP(d,nd_nvar-1);
1.45      noro      563:                XOR_EXP(d,nd_nvar-1,h);
1.44      noro      564:                TD(d) -= h;
                    565:                d[nd_exporigin-1] -= h;
                    566:        } else {
                    567:                if ( nd_isrlex ) {
                    568:                        if ( nd_bpe == 32 ) {
                    569:                                h = d[nd_exporigin];
                    570:                                for ( i = nd_exporigin+1; i < nd_wpd; i++ )
                    571:                                        d[i-1] = d[i];
                    572:                                d[i-1] = 0;
                    573:                                TD(d) -= h;
                    574:                        } else {
                    575:                                bits = nd_epw*nd_bpe;
                    576:                                mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
                    577:                                h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
                    578:                                for ( i = nd_exporigin; i < nd_wpd; i++ )
                    579:                                        d[i] = ((d[i]<<nd_bpe)&mask)
                    580:                                                |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
                    581:                                TD(d) -= h;
                    582:                        }
1.45      noro      583:                } else {
                    584:                        h = GET_EXP(d,nd_nvar-1);
                    585:                        XOR_EXP(d,nd_nvar-1,h);
                    586:                        TD(d) -= h;
                    587:                }
1.44      noro      588:        }
1.23      noro      589: }
                    590:
1.61      noro      591: void ndl_lcm(UINT *d1,unsigned *d2,UINT *d)
1.1       noro      592: {
1.61      noro      593:        UINT t1,t2,u,u1,u2;
1.43      noro      594:        int i,j,l;
1.1       noro      595:
1.65      noro      596: #if USE_UNROLL
1.1       noro      597:        switch ( nd_bpe ) {
1.62      noro      598:                case 3:
                    599:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    600:                                u1 = d1[i]; u2 = d2[i];
                    601:                                t1 = (u1&0x38000000); t2 = (u2&0x38000000); u = t1>t2?t1:t2;
                    602:                                t1 = (u1& 0x7000000); t2 = (u2& 0x7000000); u |= t1>t2?t1:t2;
                    603:                                t1 = (u1&  0xe00000); t2 = (u2&  0xe00000); u |= t1>t2?t1:t2;
                    604:                                t1 = (u1&  0x1c0000); t2 = (u2&  0x1c0000); u |= t1>t2?t1:t2;
                    605:                                t1 = (u1&   0x38000); t2 = (u2&   0x38000); u |= t1>t2?t1:t2;
                    606:                                t1 = (u1&    0x7000); t2 = (u2&    0x7000); u |= t1>t2?t1:t2;
                    607:                                t1 = (u1&     0xe00); t2 = (u2&     0xe00); u |= t1>t2?t1:t2;
                    608:                                t1 = (u1&     0x1c0); t2 = (u2&     0x1c0); u |= t1>t2?t1:t2;
                    609:                                t1 = (u1&      0x38); t2 = (u2&      0x38); u |= t1>t2?t1:t2;
                    610:                                t1 = (u1&       0x7); t2 = (u2&       0x7); u |= t1>t2?t1:t2;
                    611:                                d[i] = u;
                    612:                        }
                    613:                        break;
1.1       noro      614:                case 4:
1.41      noro      615:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      616:                                u1 = d1[i]; u2 = d2[i];
                    617:                                t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
1.62      noro      618:                                t1 = (u1& 0xf000000); t2 = (u2& 0xf000000); u |= t1>t2?t1:t2;
                    619:                                t1 = (u1&  0xf00000); t2 = (u2&  0xf00000); u |= t1>t2?t1:t2;
                    620:                                t1 = (u1&   0xf0000); t2 = (u2&   0xf0000); u |= t1>t2?t1:t2;
                    621:                                t1 = (u1&    0xf000); t2 = (u2&    0xf000); u |= t1>t2?t1:t2;
                    622:                                t1 = (u1&     0xf00); t2 = (u2&     0xf00); u |= t1>t2?t1:t2;
                    623:                                t1 = (u1&      0xf0); t2 = (u2&      0xf0); u |= t1>t2?t1:t2;
                    624:                                t1 = (u1&       0xf); t2 = (u2&       0xf); u |= t1>t2?t1:t2;
1.1       noro      625:                                d[i] = u;
                    626:                        }
                    627:                        break;
                    628:                case 6:
1.41      noro      629:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      630:                                u1 = d1[i]; u2 = d2[i];
                    631:                                t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
1.62      noro      632:                                t1 = (u1&  0xfc0000); t2 = (u2&  0xfc0000); u |= t1>t2?t1:t2;
                    633:                                t1 = (u1&   0x3f000); t2 = (u2&   0x3f000); u |= t1>t2?t1:t2;
                    634:                                t1 = (u1&     0xfc0); t2 = (u2&     0xfc0); u |= t1>t2?t1:t2;
                    635:                                t1 = (u1&      0x3f); t2 = (u2&      0x3f); u |= t1>t2?t1:t2;
1.1       noro      636:                                d[i] = u;
                    637:                        }
                    638:                        break;
                    639:                case 8:
1.41      noro      640:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      641:                                u1 = d1[i]; u2 = d2[i];
                    642:                                t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
1.62      noro      643:                                t1 = (u1&  0xff0000); t2 = (u2&  0xff0000); u |= t1>t2?t1:t2;
                    644:                                t1 = (u1&    0xff00); t2 = (u2&    0xff00); u |= t1>t2?t1:t2;
                    645:                                t1 = (u1&      0xff); t2 = (u2&      0xff); u |= t1>t2?t1:t2;
1.1       noro      646:                                d[i] = u;
                    647:                        }
                    648:                        break;
                    649:                case 16:
1.41      noro      650:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      651:                                u1 = d1[i]; u2 = d2[i];
                    652:                                t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
1.62      noro      653:                                t1 = (u1&    0xffff); t2 = (u2&    0xffff); u |= t1>t2?t1:t2;
1.1       noro      654:                                d[i] = u;
                    655:                        }
                    656:                        break;
                    657:                case 32:
1.41      noro      658:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      659:                                u1 = d1[i]; u2 = d2[i];
                    660:                                d[i] = u1>u2?u1:u2;
                    661:                        }
                    662:                        break;
                    663:                default:
1.41      noro      664:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      665:                                u1 = d1[i]; u2 = d2[i];
                    666:                                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    667:                                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    668:                                }
                    669:                                d[i] = u;
                    670:                        }
                    671:                        break;
                    672:        }
1.65      noro      673: #else
                    674:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    675:                u1 = d1[i]; u2 = d2[i];
                    676:                for ( j = 0, u = 0; j < nd_epw; j++ ) {
                    677:                        t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
                    678:                }
                    679:                d[i] = u;
                    680:        }
                    681: #endif
1.39      noro      682:        TD(d) = ndl_weight(d);
1.61      noro      683:        if ( nd_blockmask ) ndl_weight_mask(d);
1.57      noro      684: }
                    685:
1.61      noro      686: int ndl_weight(UINT *d)
1.1       noro      687: {
1.61      noro      688:        UINT t,u;
1.1       noro      689:        int i,j;
                    690:
1.60      noro      691:        if ( current_dl_weight_vector )
                    692:                for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    693:                        u = GET_EXP(d,i);
                    694:                        t += MUL_WEIGHT(u,i);
                    695:                }
                    696:        else
                    697:                for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    698:                        u = d[i];
                    699:                        for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    700:                                t += (u&nd_mask0);
                    701:                }
1.1       noro      702:        return t;
                    703: }
                    704:
1.61      noro      705: void ndl_weight_mask(UINT *d)
1.43      noro      706: {
1.61      noro      707:        UINT t,u;
                    708:        UINT *mask;
                    709:        int i,j,k,l;
1.43      noro      710:
1.61      noro      711:        l = nd_blockmask->n;
                    712:        for ( k = 0; k < l; k++ ) {
                    713:                mask = nd_blockmask->mask[k];
                    714:                if ( current_dl_weight_vector )
                    715:                        for ( i = 0, t = 0; i < nd_nvar; i++ ) {
                    716:                                u = GET_EXP_MASK(d,i,mask);
                    717:                                t += MUL_WEIGHT(u,i);
                    718:                        }
                    719:                else
                    720:                        for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
                    721:                                u = d[i]&mask[i];
                    722:                                for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
                    723:                                        t += (u&nd_mask0);
                    724:                        }
                    725:                d[k+1] = t;
                    726:        }
1.43      noro      727: }
                    728:
1.61      noro      729: int ndl_lex_compare(UINT *d1,UINT *d2)
1.1       noro      730: {
                    731:        int i;
                    732:
1.41      noro      733:        d1 += nd_exporigin;
                    734:        d2 += nd_exporigin;
                    735:        for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
1.1       noro      736:                if ( *d1 > *d2 )
1.32      noro      737:                        return nd_isrlex ? -1 : 1;
1.1       noro      738:                else if ( *d1 < *d2 )
1.32      noro      739:                        return nd_isrlex ? 1 : -1;
1.1       noro      740:        return 0;
                    741: }
                    742:
1.61      noro      743: int ndl_block_compare(UINT *d1,UINT *d2)
1.43      noro      744: {
                    745:        int i,l,j,ord_o,ord_l;
                    746:        struct order_pair *op;
1.61      noro      747:        UINT t1,t2,m;
                    748:        UINT *mask;
1.43      noro      749:
                    750:        l = nd_blockmask->n;
                    751:        op = nd_blockmask->order_pair;
                    752:        for ( j = 0; j < l; j++ ) {
                    753:                mask = nd_blockmask->mask[j];
                    754:                ord_o = op[j].order;
                    755:                if ( ord_o < 2 )
1.44      noro      756:                        if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
                    757:                        else if ( t1 < t2 ) return -1;
1.43      noro      758:                for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.44      noro      759:                        m = mask[i];
                    760:                        t1 = d1[i]&m;
                    761:                        t2 = d2[i]&m;
1.43      noro      762:                        if ( t1 > t2 )
                    763:                                return !ord_o ? -1 : 1;
                    764:                        else if ( t1 < t2 )
                    765:                                return !ord_o ? 1 : -1;
                    766:                }
                    767:        }
                    768:        return 0;
                    769: }
                    770:
1.58      noro      771: /* TDH -> WW -> TD-> RL */
                    772:
1.61      noro      773: int ndl_ww_lex_compare(UINT *d1,UINT *d2)
1.58      noro      774: {
                    775:        int i,m,e1,e2;
                    776:
                    777:        if ( TD(d1) > TD(d2) ) return 1;
                    778:        else if ( TD(d1) < TD(d2) ) return -1;
                    779:        m = nd_nvar>>1;
                    780:        for ( i = 0, e1 = e2 = 0; i < m; i++ ) {
                    781:                e1 += current_weyl_weight_vector[i]*(GET_EXP(d1,m+i)-GET_EXP(d1,i));
                    782:                e2 += current_weyl_weight_vector[i]*(GET_EXP(d2,m+i)-GET_EXP(d2,i));
                    783:        }
                    784:        if ( e1 > e2 ) return 1;
                    785:        else if ( e1 < e2 ) return -1;
                    786:        return ndl_lex_compare(d1,d2);
                    787: }
                    788:
1.61      noro      789: INLINE int ndl_equal(UINT *d1,UINT *d2)
1.1       noro      790: {
                    791:        int i;
                    792:
1.81      noro      793:        switch ( nd_wpd ) {
                    794:                case 2:
                    795:                        if ( TD(d2) != TD(d1) ) return 0;
                    796:                        if ( d2[1] != d1[1] ) return 0;
                    797:                        return 1;
                    798:                        break;
                    799:                case 3:
                    800:                        if ( TD(d2) != TD(d1) ) return 0;
                    801:                        if ( d2[1] != d1[1] ) return 0;
                    802:                        if ( d2[2] != d1[2] ) return 0;
                    803:                        return 1;
                    804:                        break;
                    805:                default:
                    806:                        for ( i = 0; i < nd_wpd; i++ )
                    807:                                if ( *d1++ != *d2++ ) return 0;
                    808:                        return 1;
                    809:                        break;
                    810:        }
1.1       noro      811: }
                    812:
1.61      noro      813: INLINE void ndl_copy(UINT *d1,UINT *d2)
1.6       noro      814: {
                    815:        int i;
                    816:
                    817:        switch ( nd_wpd ) {
1.41      noro      818:                case 2:
1.34      noro      819:                        TD(d2) = TD(d1);
                    820:                        d2[1] = d1[1];
1.6       noro      821:                        break;
1.41      noro      822:                case 3:
1.34      noro      823:                        TD(d2) = TD(d1);
1.6       noro      824:                        d2[1] = d1[1];
1.34      noro      825:                        d2[2] = d1[2];
1.6       noro      826:                        break;
                    827:                default:
1.41      noro      828:                        for ( i = 0; i < nd_wpd; i++ )
1.6       noro      829:                                d2[i] = d1[i];
                    830:                        break;
                    831:        }
                    832: }
                    833:
1.61      noro      834: INLINE void ndl_zero(UINT *d)
                    835: {
                    836:        int i;
                    837:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                    838: }
                    839:
                    840: INLINE void ndl_add(UINT *d1,UINT *d2,UINT *d)
1.1       noro      841: {
                    842:        int i;
                    843:
1.43      noro      844: #if 1
1.6       noro      845:        switch ( nd_wpd ) {
1.41      noro      846:                case 2:
                    847:                        TD(d) = TD(d1)+TD(d2);
1.34      noro      848:                        d[1] = d1[1]+d2[1];
1.6       noro      849:                        break;
1.41      noro      850:                case 3:
                    851:                        TD(d) = TD(d1)+TD(d2);
1.6       noro      852:                        d[1] = d1[1]+d2[1];
1.34      noro      853:                        d[2] = d1[2]+d2[2];
1.6       noro      854:                        break;
                    855:                default:
1.43      noro      856:                        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.6       noro      857:                        break;
                    858:        }
1.43      noro      859: #else
                    860:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
                    861: #endif
1.6       noro      862: }
                    863:
1.55      noro      864: /* d1 += d2 */
1.61      noro      865: INLINE void ndl_addto(UINT *d1,UINT *d2)
1.55      noro      866: {
                    867:        int i;
                    868:
                    869: #if 1
                    870:        switch ( nd_wpd ) {
                    871:                case 2:
                    872:                        TD(d1) += TD(d2);
                    873:                        d1[1] += d2[1];
                    874:                        break;
                    875:                case 3:
                    876:                        TD(d1) += TD(d2);
                    877:                        d1[1] += d2[1];
                    878:                        d1[2] += d2[2];
                    879:                        break;
                    880:                default:
                    881:                        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    882:                        break;
                    883:        }
                    884: #else
                    885:        for ( i = 0; i < nd_wpd; i++ ) d1[i] += d2[i];
                    886: #endif
                    887: }
                    888:
1.61      noro      889: INLINE void ndl_sub(UINT *d1,UINT *d2,UINT *d)
1.6       noro      890: {
                    891:        int i;
                    892:
1.43      noro      893:        for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1       noro      894: }
                    895:
1.61      noro      896: int ndl_disjoint(UINT *d1,UINT *d2)
1.1       noro      897: {
1.61      noro      898:        UINT t1,t2,u,u1,u2;
1.1       noro      899:        int i,j;
                    900:
1.65      noro      901: #if USE_UNROLL
1.1       noro      902:        switch ( nd_bpe ) {
1.62      noro      903:                case 3:
                    904:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    905:                                u1 = d1[i]; u2 = d2[i];
                    906:                                t1 = u1&0x38000000; t2 = u2&0x38000000; if ( t1&&t2 ) return 0;
                    907:                                t1 = u1& 0x7000000; t2 = u2& 0x7000000; if ( t1&&t2 ) return 0;
                    908:                                t1 = u1&  0xe00000; t2 = u2&  0xe00000; if ( t1&&t2 ) return 0;
                    909:                                t1 = u1&  0x1c0000; t2 = u2&  0x1c0000; if ( t1&&t2 ) return 0;
                    910:                                t1 = u1&   0x38000; t2 = u2&   0x38000; if ( t1&&t2 ) return 0;
                    911:                                t1 = u1&    0x7000; t2 = u2&    0x7000; if ( t1&&t2 ) return 0;
                    912:                                t1 = u1&     0xe00; t2 = u2&     0xe00; if ( t1&&t2 ) return 0;
                    913:                                t1 = u1&     0x1c0; t2 = u2&     0x1c0; if ( t1&&t2 ) return 0;
                    914:                                t1 = u1&      0x38; t2 = u2&      0x38; if ( t1&&t2 ) return 0;
                    915:                                t1 = u1&       0x7; t2 = u2&       0x7; if ( t1&&t2 ) return 0;
                    916:                        }
                    917:                        return 1;
                    918:                        break;
1.1       noro      919:                case 4:
1.41      noro      920:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      921:                                u1 = d1[i]; u2 = d2[i];
                    922:                                t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
1.62      noro      923:                                t1 = u1& 0xf000000; t2 = u2& 0xf000000; if ( t1&&t2 ) return 0;
                    924:                                t1 = u1&  0xf00000; t2 = u2&  0xf00000; if ( t1&&t2 ) return 0;
                    925:                                t1 = u1&   0xf0000; t2 = u2&   0xf0000; if ( t1&&t2 ) return 0;
                    926:                                t1 = u1&    0xf000; t2 = u2&    0xf000; if ( t1&&t2 ) return 0;
                    927:                                t1 = u1&     0xf00; t2 = u2&     0xf00; if ( t1&&t2 ) return 0;
                    928:                                t1 = u1&      0xf0; t2 = u2&      0xf0; if ( t1&&t2 ) return 0;
                    929:                                t1 = u1&       0xf; t2 = u2&       0xf; if ( t1&&t2 ) return 0;
1.1       noro      930:                        }
                    931:                        return 1;
                    932:                        break;
                    933:                case 6:
1.41      noro      934:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      935:                                u1 = d1[i]; u2 = d2[i];
                    936:                                t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
1.62      noro      937:                                t1 = u1&  0xfc0000; t2 = u2&  0xfc0000; if ( t1&&t2 ) return 0;
                    938:                                t1 = u1&   0x3f000; t2 = u2&   0x3f000; if ( t1&&t2 ) return 0;
                    939:                                t1 = u1&     0xfc0; t2 = u2&     0xfc0; if ( t1&&t2 ) return 0;
                    940:                                t1 = u1&      0x3f; t2 = u2&      0x3f; if ( t1&&t2 ) return 0;
1.1       noro      941:                        }
                    942:                        return 1;
                    943:                        break;
                    944:                case 8:
1.41      noro      945:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      946:                                u1 = d1[i]; u2 = d2[i];
                    947:                                t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
1.62      noro      948:                                t1 = u1&  0xff0000; t2 = u2&  0xff0000; if ( t1&&t2 ) return 0;
                    949:                                t1 = u1&    0xff00; t2 = u2&    0xff00; if ( t1&&t2 ) return 0;
                    950:                                t1 = u1&      0xff; t2 = u2&      0xff; if ( t1&&t2 ) return 0;
1.1       noro      951:                        }
                    952:                        return 1;
                    953:                        break;
                    954:                case 16:
1.41      noro      955:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      956:                                u1 = d1[i]; u2 = d2[i];
                    957:                                t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
1.62      noro      958:                                t1 = u1&    0xffff; t2 = u2&    0xffff; if ( t1&&t2 ) return 0;
1.1       noro      959:                        }
                    960:                        return 1;
                    961:                        break;
                    962:                case 32:
1.41      noro      963:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1       noro      964:                                if ( d1[i] && d2[i] ) return 0;
                    965:                        return 1;
                    966:                        break;
                    967:                default:
1.41      noro      968:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1       noro      969:                                u1 = d1[i]; u2 = d2[i];
                    970:                                for ( j = 0; j < nd_epw; j++ ) {
                    971:                                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    972:                                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    973:                                }
                    974:                        }
                    975:                        return 1;
                    976:                        break;
                    977:        }
1.65      noro      978: #else
                    979:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                    980:                u1 = d1[i]; u2 = d2[i];
                    981:                for ( j = 0; j < nd_epw; j++ ) {
                    982:                        if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
                    983:                        u1 >>= nd_bpe; u2 >>= nd_bpe;
                    984:                }
                    985:        }
                    986:        return 1;
                    987: #endif
1.1       noro      988: }
                    989:
1.61      noro      990: int ndl_check_bound2(int index,UINT *d2)
1.1       noro      991: {
1.61      noro      992:        UINT u2;
                    993:        UINT *d1;
1.5       noro      994:        int i,j,ind,k;
1.1       noro      995:
1.5       noro      996:        d1 = nd_bound[index];
                    997:        ind = 0;
1.65      noro      998: #if USE_UNROLL
1.5       noro      999:        switch ( nd_bpe ) {
1.62      noro     1000:                case 3:
                   1001:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1002:                                u2 = d2[i];
                   1003:                                if ( d1[ind++]+((u2>>27)&0x7) >= 0x8 ) return 1;
                   1004:                                if ( d1[ind++]+((u2>>24)&0x7) >= 0x8 ) return 1;
                   1005:                                if ( d1[ind++]+((u2>>21)&0x7) >= 0x8 ) return 1;
                   1006:                                if ( d1[ind++]+((u2>>18)&0x7) >= 0x8 ) return 1;
                   1007:                                if ( d1[ind++]+((u2>>15)&0x7) >= 0x8 ) return 1;
                   1008:                                if ( d1[ind++]+((u2>>12)&0x7) >= 0x8 ) return 1;
                   1009:                                if ( d1[ind++]+((u2>>9)&0x7) >= 0x8 ) return 1;
                   1010:                                if ( d1[ind++]+((u2>>6)&0x7) >= 0x8 ) return 1;
                   1011:                                if ( d1[ind++]+((u2>>3)&0x7) >= 0x8 ) return 1;
                   1012:                                if ( d1[ind++]+(u2&0x7) >= 0x8 ) return 1;
                   1013:                        }
                   1014:                        return 0;
                   1015:                        break;
1.5       noro     1016:                case 4:
1.41      noro     1017:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro     1018:                                u2 = d2[i];
                   1019:                                if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
                   1020:                                if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
                   1021:                                if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
                   1022:                                if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
                   1023:                                if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
                   1024:                                if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
                   1025:                                if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
                   1026:                                if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
                   1027:                        }
                   1028:                        return 0;
                   1029:                        break;
                   1030:                case 6:
1.41      noro     1031:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro     1032:                                u2 = d2[i];
                   1033:                                if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
                   1034:                                if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
                   1035:                                if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
                   1036:                                if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
                   1037:                                if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
                   1038:                        }
                   1039:                        return 0;
                   1040:                        break;
                   1041:                case 8:
1.41      noro     1042:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro     1043:                                u2 = d2[i];
                   1044:                                if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
                   1045:                                if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
                   1046:                                if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
                   1047:                                if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
                   1048:                        }
                   1049:                        return 0;
                   1050:                        break;
                   1051:                case 16:
1.41      noro     1052:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro     1053:                                u2 = d2[i];
                   1054:                                if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
                   1055:                                if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
                   1056:                        }
                   1057:                        return 0;
                   1058:                        break;
                   1059:                case 32:
1.41      noro     1060:                        for ( i = nd_exporigin; i < nd_wpd; i++ )
1.5       noro     1061:                                if ( d1[i]+d2[i]<d1[i] ) return 1;
                   1062:                        return 0;
                   1063:                        break;
                   1064:                default:
1.41      noro     1065:                        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5       noro     1066:                                u2 = d2[i];
                   1067:                                k = (nd_epw-1)*nd_bpe;
                   1068:                                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1069:                                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1070:                        }
                   1071:                        return 0;
                   1072:                        break;
                   1073:        }
1.65      noro     1074: #else
                   1075:        for ( i = nd_exporigin; i < nd_wpd; i++ ) {
                   1076:                u2 = d2[i];
                   1077:                k = (nd_epw-1)*nd_bpe;
                   1078:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
                   1079:                        if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
                   1080:        }
                   1081:        return 0;
                   1082: #endif
1.1       noro     1083: }
                   1084:
1.61      noro     1085: INLINE int ndl_hash_value(UINT *d)
1.1       noro     1086: {
                   1087:        int i;
                   1088:        int r;
                   1089:
1.34      noro     1090:        r = 0;
1.41      noro     1091:        for ( i = 0; i < nd_wpd; i++ )
1.1       noro     1092:                r = ((r<<16)+d[i])%REDTAB_LEN;
                   1093:        return r;
                   1094: }
                   1095:
1.63      noro     1096: INLINE int ndl_find_reducer(UINT *dg)
1.1       noro     1097: {
1.13      noro     1098:        RHist r;
1.6       noro     1099:        int d,k,i;
1.1       noro     1100:
1.63      noro     1101:        d = ndl_hash_value(dg);
1.13      noro     1102:        for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.34      noro     1103:                if ( ndl_equal(dg,DL(r)) ) {
1.1       noro     1104:                        if ( k > 0 ) nd_notfirst++;
                   1105:                        nd_found++;
1.13      noro     1106:                        return r->index;
1.1       noro     1107:                }
                   1108:        }
1.13      noro     1109:        if ( Reverse )
                   1110:                for ( i = nd_psn-1; i >= 0; i-- ) {
                   1111:                        r = nd_psh[i];
1.34      noro     1112:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro     1113:                                nd_create++;
1.34      noro     1114:                                nd_append_red(dg,i);
1.13      noro     1115:                                return i;
                   1116:                        }
                   1117:                }
                   1118:        else
                   1119:                for ( i = 0; i < nd_psn; i++ ) {
                   1120:                        r = nd_psh[i];
1.34      noro     1121:                        if ( ndl_reducible(dg,DL(r)) ) {
1.13      noro     1122:                                nd_create++;
1.34      noro     1123:                                nd_append_red(dg,i);
1.13      noro     1124:                                return i;
                   1125:                        }
1.1       noro     1126:                }
1.6       noro     1127:        return -1;
1.1       noro     1128: }
                   1129:
1.63      noro     1130: ND nd_merge(ND p1,ND p2)
                   1131: {
                   1132:        int n,c;
                   1133:        int t,can,td1,td2;
                   1134:        ND r;
                   1135:        NM m1,m2,mr0,mr,s;
                   1136:
                   1137:        if ( !p1 ) return p2;
                   1138:        else if ( !p2 ) return p1;
                   1139:        else {
                   1140:                can = 0;
                   1141:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1142:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1143:                        switch ( c ) {
                   1144:                                case 0:
                   1145:                                        s = m1; m1 = NEXT(m1);
                   1146:                                        can++; NEXTNM2(mr0,mr,s);
                   1147:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1148:                                        break;
                   1149:                                case 1:
                   1150:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1151:                                        break;
                   1152:                                case -1:
                   1153:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1154:                                        break;
                   1155:                        }
                   1156:                }
                   1157:                if ( !mr0 )
                   1158:                        if ( m1 ) mr0 = m1;
                   1159:                        else if ( m2 ) mr0 = m2;
                   1160:                        else return 0;
                   1161:                else if ( m1 ) NEXT(mr) = m1;
                   1162:                else if ( m2 ) NEXT(mr) = m2;
                   1163:                else NEXT(mr) = 0;
                   1164:                BDY(p1) = mr0;
                   1165:                SG(p1) = MAX(SG(p1),SG(p2));
                   1166:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1167:                FREEND(p2);
                   1168:                return p1;
                   1169:        }
                   1170: }
                   1171:
1.31      noro     1172: ND nd_add(int mod,ND p1,ND p2)
1.1       noro     1173: {
                   1174:        int n,c;
1.34      noro     1175:        int t,can,td1,td2;
1.1       noro     1176:        ND r;
                   1177:        NM m1,m2,mr0,mr,s;
                   1178:
1.34      noro     1179:        if ( !p1 ) return p2;
                   1180:        else if ( !p2 ) return p1;
1.71      noro     1181:        else if ( mod == -1 ) return nd_add_sf(p1,p2);
1.34      noro     1182:        else if ( !mod ) return nd_add_q(p1,p2);
1.1       noro     1183:        else {
1.30      noro     1184:                can = 0;
1.1       noro     1185:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro     1186:                        c = DL_COMPARE(DL(m1),DL(m2));
1.1       noro     1187:                        switch ( c ) {
                   1188:                                case 0:
1.19      noro     1189:                                        t = ((CM(m1))+(CM(m2))) - mod;
1.34      noro     1190:                                        if ( t < 0 ) t += mod;
1.1       noro     1191:                                        s = m1; m1 = NEXT(m1);
                   1192:                                        if ( t ) {
1.34      noro     1193:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1       noro     1194:                                        } else {
1.34      noro     1195:                                                can += 2; FREENM(s);
1.1       noro     1196:                                        }
                   1197:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1198:                                        break;
                   1199:                                case 1:
                   1200:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1201:                                        break;
                   1202:                                case -1:
                   1203:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1204:                                        break;
                   1205:                        }
                   1206:                }
                   1207:                if ( !mr0 )
1.34      noro     1208:                        if ( m1 ) mr0 = m1;
                   1209:                        else if ( m2 ) mr0 = m2;
                   1210:                        else return 0;
                   1211:                else if ( m1 ) NEXT(mr) = m1;
                   1212:                else if ( m2 ) NEXT(mr) = m2;
                   1213:                else NEXT(mr) = 0;
1.1       noro     1214:                BDY(p1) = mr0;
1.14      noro     1215:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1216:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.1       noro     1217:                FREEND(p2);
                   1218:                return p1;
                   1219:        }
                   1220: }
                   1221:
1.31      noro     1222: ND nd_add_q(ND p1,ND p2)
1.17      noro     1223: {
1.30      noro     1224:        int n,c,can;
1.17      noro     1225:        ND r;
                   1226:        NM m1,m2,mr0,mr,s;
                   1227:        Q t;
                   1228:
1.34      noro     1229:        if ( !p1 ) return p2;
                   1230:        else if ( !p2 ) return p1;
1.31      noro     1231:        else {
1.30      noro     1232:                can = 0;
1.17      noro     1233:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34      noro     1234:                        c = DL_COMPARE(DL(m1),DL(m2));
1.17      noro     1235:                        switch ( c ) {
                   1236:                                case 0:
                   1237:                                        addq(CQ(m1),CQ(m2),&t);
                   1238:                                        s = m1; m1 = NEXT(m1);
                   1239:                                        if ( t ) {
1.34      noro     1240:                                                can++; NEXTNM2(mr0,mr,s); CQ(mr) = (t);
1.17      noro     1241:                                        } else {
1.34      noro     1242:                                                can += 2; FREENM(s);
1.17      noro     1243:                                        }
                   1244:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1245:                                        break;
                   1246:                                case 1:
                   1247:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1248:                                        break;
                   1249:                                case -1:
                   1250:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1251:                                        break;
                   1252:                        }
                   1253:                }
                   1254:                if ( !mr0 )
1.34      noro     1255:                        if ( m1 ) mr0 = m1;
                   1256:                        else if ( m2 ) mr0 = m2;
                   1257:                        else return 0;
                   1258:                else if ( m1 ) NEXT(mr) = m1;
                   1259:                else if ( m2 ) NEXT(mr) = m2;
                   1260:                else NEXT(mr) = 0;
1.17      noro     1261:                BDY(p1) = mr0;
                   1262:                SG(p1) = MAX(SG(p1),SG(p2));
1.31      noro     1263:                LEN(p1) = LEN(p1)+LEN(p2)-can;
1.17      noro     1264:                FREEND(p2);
                   1265:                return p1;
                   1266:        }
                   1267: }
                   1268:
1.71      noro     1269: ND nd_add_sf(ND p1,ND p2)
                   1270: {
                   1271:        int n,c,can;
                   1272:        ND r;
                   1273:        NM m1,m2,mr0,mr,s;
                   1274:        int t;
                   1275:
                   1276:        if ( !p1 ) return p2;
                   1277:        else if ( !p2 ) return p1;
                   1278:        else {
                   1279:                can = 0;
                   1280:                for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
                   1281:                        c = DL_COMPARE(DL(m1),DL(m2));
                   1282:                        switch ( c ) {
                   1283:                                case 0:
                   1284:                                        t = _addsf(CM(m1),CM(m2));
                   1285:                                        s = m1; m1 = NEXT(m1);
                   1286:                                        if ( t ) {
                   1287:                                                can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
                   1288:                                        } else {
                   1289:                                                can += 2; FREENM(s);
                   1290:                                        }
                   1291:                                        s = m2; m2 = NEXT(m2); FREENM(s);
                   1292:                                        break;
                   1293:                                case 1:
                   1294:                                        s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
                   1295:                                        break;
                   1296:                                case -1:
                   1297:                                        s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
                   1298:                                        break;
                   1299:                        }
                   1300:                }
                   1301:                if ( !mr0 )
                   1302:                        if ( m1 ) mr0 = m1;
                   1303:                        else if ( m2 ) mr0 = m2;
                   1304:                        else return 0;
                   1305:                else if ( m1 ) NEXT(mr) = m1;
                   1306:                else if ( m2 ) NEXT(mr) = m2;
                   1307:                else NEXT(mr) = 0;
                   1308:                BDY(p1) = mr0;
                   1309:                SG(p1) = MAX(SG(p1),SG(p2));
                   1310:                LEN(p1) = LEN(p1)+LEN(p2)-can;
                   1311:                FREEND(p2);
                   1312:                return p1;
                   1313:        }
                   1314: }
                   1315:
1.1       noro     1316: /* ret=1 : success, ret=0 : overflow */
1.69      noro     1317: int nd_nf(int mod,ND g,NDV *ps,int full,NDC dn,ND *rp)
1.1       noro     1318: {
1.11      noro     1319:        ND d;
1.1       noro     1320:        NM m,mrd,tail;
1.7       noro     1321:        NM mul;
1.10      noro     1322:        int n,sugar,psugar,sugar0,stat,index;
1.30      noro     1323:        int c,c1,c2,dummy;
1.17      noro     1324:        RHist h;
1.11      noro     1325:        NDV p,red;
1.69      noro     1326:        Q cg,cred,gcd,tq,qq;
1.21      noro     1327:        double hmag;
1.1       noro     1328:
1.69      noro     1329:        if ( dn ) {
                   1330:                if ( mod )
                   1331:                        dn->m = 1;
                   1332:                else
                   1333:                        dn->z = ONE;
                   1334:        }
1.1       noro     1335:        if ( !g ) {
                   1336:                *rp = 0;
                   1337:                return 1;
                   1338:        }
1.34      noro     1339:        if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.21      noro     1340:
1.14      noro     1341:        sugar0 = sugar = SG(g);
1.1       noro     1342:        n = NV(g);
1.61      noro     1343:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.1       noro     1344:        for ( d = 0; g; ) {
1.63      noro     1345:                index = ndl_find_reducer(HDL(g));
1.6       noro     1346:                if ( index >= 0 ) {
1.17      noro     1347:                        h = nd_psh[index];
                   1348:                        ndl_sub(HDL(g),DL(h),DL(mul));
1.14      noro     1349:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.6       noro     1350:                                nd_free(g); nd_free(d);
                   1351:                                return 0;
                   1352:                        }
1.77      noro     1353:                        if ( nd_demand )
                   1354:                                p = ndv_load(index);
                   1355:                        else
                   1356:                                p = ps[index];
1.71      noro     1357:                        if ( mod == -1 )
                   1358:                                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1359:                        else if ( mod ) {
1.19      noro     1360:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1361:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.16      noro     1362:                        } else {
1.17      noro     1363:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1.16      noro     1364:                                chsgnq(cg,&CQ(mul));
1.20      noro     1365:                                nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1.69      noro     1366:                                if ( dn ) {
                   1367:                                        mulq(dn->z,cred,&tq); dn->z = tq;
                   1368:                                }
1.16      noro     1369:                        }
1.55      noro     1370:                        g = nd_add(mod,g,ndv_mul_nm(mod,mul,p));
1.34      noro     1371:                        sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.22      noro     1372:                        if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1.69      noro     1373:                                tq = HCQ(g);
1.21      noro     1374:                                nd_removecont2(d,g);
1.69      noro     1375:                                if ( dn ) {
                   1376:                                        divq(tq,HCQ(g),&qq); divq(dn->z,qq,&tq); dn->z = tq;
                   1377:                                }
1.21      noro     1378:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
                   1379:                        }
1.1       noro     1380:                } else if ( !full ) {
                   1381:                        *rp = g;
                   1382:                        return 1;
                   1383:                } else {
                   1384:                        m = BDY(g);
                   1385:                        if ( NEXT(m) ) {
1.34      noro     1386:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.1       noro     1387:                        } else {
                   1388:                                FREEND(g); g = 0;
                   1389:                        }
                   1390:                        if ( d ) {
1.34      noro     1391:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.1       noro     1392:                        } else {
1.34      noro     1393:                                MKND(n,m,1,d); tail = BDY(d);
1.1       noro     1394:                        }
                   1395:                }
                   1396:        }
1.34      noro     1397:        if ( d ) SG(d) = sugar;
1.1       noro     1398:        *rp = d;
                   1399:        return 1;
                   1400: }
1.28      noro     1401:
1.53      noro     1402: int nd_nf_pbucket(int mod,ND g,NDV *ps,int full,ND *rp)
1.25      noro     1403: {
                   1404:        int hindex,index;
                   1405:        NDV p;
                   1406:        ND u,d,red;
                   1407:        NODE l;
1.31      noro     1408:        NM mul,m,mrd,tail;
1.25      noro     1409:        int sugar,psugar,n,h_reducible;
                   1410:        PGeoBucket bucket;
                   1411:        int c,c1,c2;
1.26      noro     1412:        Q cg,cred,gcd,zzz;
1.25      noro     1413:        RHist h;
1.28      noro     1414:        double hmag,gmag;
1.77      noro     1415:        int count = 0;
                   1416:        int hcount = 0;
1.25      noro     1417:
                   1418:        if ( !g ) {
                   1419:                *rp = 0;
                   1420:                return 1;
                   1421:        }
                   1422:        sugar = SG(g);
                   1423:        n = NV(g);
1.34      noro     1424:        if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.25      noro     1425:        bucket = create_pbucket();
1.31      noro     1426:        add_pbucket(mod,bucket,g);
1.25      noro     1427:        d = 0;
1.61      noro     1428:        mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
1.25      noro     1429:        while ( 1 ) {
1.26      noro     1430:                hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.25      noro     1431:                if ( hindex < 0 ) {
1.77      noro     1432:                        if ( DP_Print > 3 ) printf("(%d %d)",count,hcount);
1.34      noro     1433:                        if ( d ) SG(d) = sugar;
1.25      noro     1434:                        *rp = d;
                   1435:                        return 1;
                   1436:                }
                   1437:                g = bucket->body[hindex];
1.63      noro     1438:                index = ndl_find_reducer(HDL(g));
1.25      noro     1439:                if ( index >= 0 ) {
1.77      noro     1440:                        count++;
                   1441:                        if ( !d ) hcount++;
1.25      noro     1442:                        h = nd_psh[index];
                   1443:                        ndl_sub(HDL(g),DL(h),DL(mul));
                   1444:                        if ( ndl_check_bound2(index,DL(mul)) ) {
1.26      noro     1445:                                nd_free(d);
1.25      noro     1446:                                free_pbucket(bucket);
                   1447:                                *rp = 0;
                   1448:                                return 0;
                   1449:                        }
1.53      noro     1450:                        p = ps[index];
1.71      noro     1451:                        if ( mod == -1 )
                   1452:                                CM(mul) = _mulsf(_invsf(HCM(p)),_chsgnsf(HCM(g)));
                   1453:                        else if ( mod ) {
1.25      noro     1454:                                c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
                   1455:                                DMAR(c1,c2,0,mod,c); CM(mul) = c;
                   1456:                        } else {
                   1457:                                igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
                   1458:                                chsgnq(cg,&CQ(mul));
1.26      noro     1459:                                nd_mul_c_q(d,cred);
                   1460:                                mulq_pbucket(bucket,cred);
                   1461:                                g = bucket->body[hindex];
1.28      noro     1462:                                gmag = (double)p_mag((P)HCQ(g));
1.25      noro     1463:                        }
1.55      noro     1464:                        red = ndv_mul_nm(mod,mul,p);
1.25      noro     1465:                        bucket->body[hindex] = nd_remove_head(g);
                   1466:                        red = nd_remove_head(red);
1.31      noro     1467:                        add_pbucket(mod,bucket,red);
1.34      noro     1468:                        psugar = SG(p)+TD(DL(mul));
                   1469:                        sugar = MAX(sugar,psugar);
1.28      noro     1470:                        if ( !mod && hmag && (gmag > hmag) ) {
                   1471:                                g = normalize_pbucket(mod,bucket);
                   1472:                                if ( !g ) {
1.34      noro     1473:                                        if ( d ) SG(d) = sugar;
1.28      noro     1474:                                        *rp = d;
                   1475:                                        return 1;
                   1476:                                }
                   1477:                                nd_removecont2(d,g);
                   1478:                                hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.31      noro     1479:                                add_pbucket(mod,bucket,g);
1.28      noro     1480:                        }
1.25      noro     1481:                } else if ( !full ) {
                   1482:                        g = normalize_pbucket(mod,bucket);
1.34      noro     1483:                        if ( g ) SG(g) = sugar;
1.25      noro     1484:                        *rp = g;
                   1485:                        return 1;
                   1486:                } else {
                   1487:                        m = BDY(g);
                   1488:                        if ( NEXT(m) ) {
1.34      noro     1489:                                BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.25      noro     1490:                        } else {
                   1491:                                FREEND(g); g = 0;
                   1492:                        }
                   1493:                        bucket->body[hindex] = g;
                   1494:                        NEXT(m) = 0;
                   1495:                        if ( d ) {
1.34      noro     1496:                                NEXT(tail)=m; tail=m; LEN(d)++;
1.25      noro     1497:                        } else {
1.34      noro     1498:                                MKND(n,m,1,d); tail = BDY(d);
1.25      noro     1499:                        }
                   1500:                }
                   1501:        }
                   1502: }
1.27      noro     1503:
1.61      noro     1504: /* input : list of NDV, cand : list of NDV */
1.28      noro     1505:
1.61      noro     1506: int ndv_check_candidate(NODE input,int obpe,int oadv,EPOS oepos,NODE cand)
1.28      noro     1507: {
                   1508:        int n,i,stat;
                   1509:        ND nf,d;
1.61      noro     1510:        NDV r;
1.45      noro     1511:        NODE t,s;
1.69      noro     1512:        union oNDC dn;
1.45      noro     1513:
1.61      noro     1514:        ndv_setup(0,0,cand);
1.31      noro     1515:        n = length(cand);
1.28      noro     1516:
                   1517:        /* membercheck : list is a subset of Id(cand) ? */
                   1518:        for ( t = input; t; t = NEXT(t) ) {
1.45      noro     1519: again:
1.61      noro     1520:                if ( nd_bpe > obpe )
                   1521:                        r = ndv_dup_realloc((NDV)BDY(t),obpe,oadv,oepos);
                   1522:                else
                   1523:                        r = (NDV)BDY(t);
                   1524:                d = ndvtond(0,r);
1.69      noro     1525:                stat = nd_nf(0,d,nd_ps,0,0,&nf);
1.45      noro     1526:                if ( !stat ) {
                   1527:                        nd_reconstruct(0,0,0);
                   1528:                        goto again;
                   1529:                } else if ( nf ) return 0;
1.71      noro     1530:                if ( DP_Print ) { printf("."); fflush(stdout); }
1.28      noro     1531:        }
1.71      noro     1532:        if ( DP_Print ) { printf("\n"); }
1.28      noro     1533:        /* gbcheck : cand is a GB of Id(cand) ? */
1.77      noro     1534:        if ( !nd_gb(0,0,1) ) return 0;
1.28      noro     1535:        /* XXX */
1.23      noro     1536:        return 1;
                   1537: }
1.1       noro     1538:
                   1539: ND nd_remove_head(ND p)
                   1540: {
                   1541:        NM m;
                   1542:
                   1543:        m = BDY(p);
                   1544:        if ( !NEXT(m) ) {
1.34      noro     1545:                FREEND(p); p = 0;
1.31      noro     1546:        } else {
1.34      noro     1547:                BDY(p) = NEXT(m); LEN(p)--;
1.31      noro     1548:        }
1.1       noro     1549:        FREENM(m);
                   1550:        return p;
                   1551: }
                   1552:
1.69      noro     1553: ND nd_separate_head(ND p,ND *head)
                   1554: {
                   1555:        NM m,m0;
                   1556:        ND r;
                   1557:
                   1558:        m = BDY(p);
                   1559:        if ( !NEXT(m) ) {
                   1560:                *head = p; p = 0;
                   1561:        } else {
                   1562:                m0 = m;
                   1563:                BDY(p) = NEXT(m); LEN(p)--;
                   1564:                NEXT(m0) = 0;
                   1565:                MKND(NV(p),m0,1,r);
                   1566:                *head = r;
                   1567:        }
                   1568:        return p;
                   1569: }
                   1570:
1.1       noro     1571: PGeoBucket create_pbucket()
                   1572: {
                   1573:     PGeoBucket g;
                   1574:
                   1575:        g = CALLOC(1,sizeof(struct oPGeoBucket));
                   1576:        g->m = -1;
                   1577:        return g;
                   1578: }
                   1579:
1.25      noro     1580: void free_pbucket(PGeoBucket b) {
                   1581:        int i;
                   1582:
1.26      noro     1583:        for ( i = 0; i <= b->m; i++ )
1.25      noro     1584:                if ( b->body[i] ) {
                   1585:                        nd_free(b->body[i]);
                   1586:                        b->body[i] = 0;
                   1587:                }
                   1588:        GC_free(b);
                   1589: }
                   1590:
1.63      noro     1591: void add_pbucket_symbolic(PGeoBucket g,ND d)
                   1592: {
                   1593:        int l,i,k,m;
                   1594:
                   1595:        if ( !d )
                   1596:                return;
                   1597:        l = LEN(d);
                   1598:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1599:        /* 2^(k-1) < l <= 2^k (=m) */
                   1600:        d = nd_merge(g->body[k],d);
                   1601:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
                   1602:                g->body[k] = 0;
                   1603:                d = nd_merge(g->body[k+1],d);
                   1604:        }
                   1605:        g->body[k] = d;
                   1606:        g->m = MAX(g->m,k);
                   1607: }
                   1608:
1.31      noro     1609: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1       noro     1610: {
1.31      noro     1611:        int l,i,k,m;
1.1       noro     1612:
1.31      noro     1613:        if ( !d )
                   1614:                return;
                   1615:        l = LEN(d);
1.29      noro     1616:        for ( k = 0, m = 1; l > m; k++, m <<= 1 );
                   1617:        /* 2^(k-1) < l <= 2^k (=m) */
1.31      noro     1618:        d = nd_add(mod,g->body[k],d);
                   1619:        for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1.1       noro     1620:                g->body[k] = 0;
1.31      noro     1621:                d = nd_add(mod,g->body[k+1],d);
1.1       noro     1622:        }
                   1623:        g->body[k] = d;
                   1624:        g->m = MAX(g->m,k);
                   1625: }
                   1626:
1.26      noro     1627: void mulq_pbucket(PGeoBucket g,Q c)
                   1628: {
                   1629:        int k;
                   1630:
                   1631:        for ( k = 0; k <= g->m; k++ )
                   1632:                nd_mul_c_q(g->body[k],c);
                   1633: }
                   1634:
1.63      noro     1635: NM remove_head_pbucket_symbolic(PGeoBucket g)
                   1636: {
                   1637:        int j,i,k,c;
                   1638:        NM head;
                   1639:
                   1640:        k = g->m;
                   1641:        j = -1;
                   1642:        for ( i = 0; i <= k; i++ ) {
                   1643:                if ( !g->body[i] ) continue;
                   1644:                if ( j < 0 ) j = i;
                   1645:                else {
                   1646:                        c = DL_COMPARE(HDL(g->body[i]),HDL(g->body[j]));
                   1647:                        if ( c > 0 )
                   1648:                                j = i;
                   1649:                        else if ( c == 0 )
                   1650:                                g->body[i] = nd_remove_head(g->body[i]);
                   1651:                }
                   1652:        }
                   1653:        if ( j < 0 ) return 0;
                   1654:        else {
                   1655:                head = BDY(g->body[j]);
                   1656:                if ( !NEXT(head) ) {
                   1657:                        FREEND(g->body[j]);
                   1658:                        g->body[j] = 0;
                   1659:                } else {
                   1660:                        BDY(g->body[j]) = NEXT(head);
                   1661:                        LEN(g->body[j])--;
                   1662:                }
                   1663:                return head;
                   1664:        }
                   1665: }
                   1666:
1.19      noro     1667: int head_pbucket(int mod,PGeoBucket g)
1.1       noro     1668: {
                   1669:        int j,i,c,k,nv,sum;
1.61      noro     1670:        UINT *di,*dj;
1.1       noro     1671:        ND gi,gj;
                   1672:
                   1673:        k = g->m;
                   1674:        while ( 1 ) {
                   1675:                j = -1;
                   1676:                for ( i = 0; i <= k; i++ ) {
                   1677:                        if ( !(gi = g->body[i]) )
                   1678:                                continue;
                   1679:                        if ( j < 0 ) {
                   1680:                                j = i;
                   1681:                                gj = g->body[j];
                   1682:                                dj = HDL(gj);
1.14      noro     1683:                                sum = HCM(gj);
1.1       noro     1684:                        } else {
1.34      noro     1685:                                c = DL_COMPARE(HDL(gi),dj);
1.1       noro     1686:                                if ( c > 0 ) {
1.34      noro     1687:                                        if ( sum ) HCM(gj) = sum;
                   1688:                                        else g->body[j] = nd_remove_head(gj);
1.1       noro     1689:                                        j = i;
                   1690:                                        gj = g->body[j];
                   1691:                                        dj = HDL(gj);
1.14      noro     1692:                                        sum = HCM(gj);
1.1       noro     1693:                                } else if ( c == 0 ) {
1.71      noro     1694:                                        if ( mod == -1 )
                   1695:                                                sum = _addsf(sum,HCM(gi));
                   1696:                                        else {
                   1697:                                                sum = sum+HCM(gi)-mod;
                   1698:                                                if ( sum < 0 ) sum += mod;
                   1699:                                        }
1.1       noro     1700:                                        g->body[i] = nd_remove_head(gi);
                   1701:                                }
                   1702:                        }
                   1703:                }
1.34      noro     1704:                if ( j < 0 ) return -1;
1.1       noro     1705:                else if ( sum ) {
1.14      noro     1706:                        HCM(gj) = sum;
1.26      noro     1707:                        return j;
1.31      noro     1708:                } else
1.26      noro     1709:                        g->body[j] = nd_remove_head(gj);
                   1710:        }
                   1711: }
                   1712:
                   1713: int head_pbucket_q(PGeoBucket g)
                   1714: {
                   1715:        int j,i,c,k,nv;
                   1716:        Q sum,t;
                   1717:        ND gi,gj;
                   1718:
                   1719:        k = g->m;
                   1720:        while ( 1 ) {
                   1721:                j = -1;
                   1722:                for ( i = 0; i <= k; i++ ) {
1.34      noro     1723:                        if ( !(gi = g->body[i]) ) continue;
1.26      noro     1724:                        if ( j < 0 ) {
                   1725:                                j = i;
                   1726:                                gj = g->body[j];
                   1727:                                sum = HCQ(gj);
                   1728:                        } else {
                   1729:                                nv = NV(gi);
1.34      noro     1730:                                c = DL_COMPARE(HDL(gi),HDL(gj));
1.26      noro     1731:                                if ( c > 0 ) {
1.34      noro     1732:                                        if ( sum ) HCQ(gj) = sum;
                   1733:                                        else g->body[j] = nd_remove_head(gj);
1.26      noro     1734:                                        j = i;
                   1735:                                        gj = g->body[j];
                   1736:                                        sum = HCQ(gj);
                   1737:                                } else if ( c == 0 ) {
                   1738:                                        addq(sum,HCQ(gi),&t);
                   1739:                                        sum = t;
                   1740:                                        g->body[i] = nd_remove_head(gi);
                   1741:                                }
                   1742:                        }
                   1743:                }
1.34      noro     1744:                if ( j < 0 ) return -1;
1.26      noro     1745:                else if ( sum ) {
                   1746:                        HCQ(gj) = sum;
1.1       noro     1747:                        return j;
1.31      noro     1748:                } else
1.1       noro     1749:                        g->body[j] = nd_remove_head(gj);
                   1750:        }
                   1751: }
                   1752:
1.25      noro     1753: ND normalize_pbucket(int mod,PGeoBucket g)
1.1       noro     1754: {
1.31      noro     1755:        int i;
1.1       noro     1756:        ND r,t;
                   1757:
                   1758:        r = 0;
1.28      noro     1759:        for ( i = 0; i <= g->m; i++ ) {
1.31      noro     1760:                r = nd_add(mod,r,g->body[i]);
1.28      noro     1761:                g->body[i] = 0;
                   1762:        }
                   1763:        g->m = -1;
1.1       noro     1764:        return r;
                   1765: }
                   1766:
1.27      noro     1767: /* return value = 0 => input is not a GB */
                   1768:
1.77      noro     1769: NODE nd_gb(int m,int ishomo,int checkonly)
1.1       noro     1770: {
                   1771:        int i,nh,sugar,stat;
1.23      noro     1772:        NODE r,g,t;
1.1       noro     1773:        ND_pairs d;
                   1774:        ND_pairs l;
1.77      noro     1775:        ND h,nf,s,head;
1.63      noro     1776:        NDV nfv;
1.85      noro     1777:        Q q,num,den;
1.69      noro     1778:        union oNDC dn;
1.1       noro     1779:
1.23      noro     1780:        g = 0; d = 0;
                   1781:        for ( i = 0; i < nd_psn; i++ ) {
1.1       noro     1782:                d = update_pairs(d,g,i);
                   1783:                g = update_base(g,i);
                   1784:        }
                   1785:        sugar = 0;
                   1786:        while ( d ) {
                   1787: again:
                   1788:                l = nd_minp(d,&d);
1.14      noro     1789:                if ( SG(l) != sugar ) {
1.77      noro     1790:                        if ( ishomo ) {
1.85      noro     1791:                                for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.82      noro     1792:                                        if ( nd_demand )
                   1793:                                                nfv = ndv_load(i);
                   1794:                                        else
                   1795:                                                nfv = nd_ps[i];
                   1796:                                        s = ndvtond(m,nfv);
1.77      noro     1797:                                        s = nd_separate_head(s,&head);
                   1798:                                        nd_nf(m,s,nd_ps,1,&dn,&nf);
1.85      noro     1799:                                        if ( !m ) {
                   1800:                                                NTOQ(NM(dn.z),SGN(dn.z),num);
                   1801:                                                mulq(HCQ(head),num,&q); HCQ(head) = q;
                   1802:                                                if ( DN(dn.z) ) {
                   1803:                                                        NTOQ(DN(dn.z),1,den);
                   1804:                                                        nd_mul_c_q(nf,den);
                   1805:                                                }
                   1806:                                        }
1.77      noro     1807:                                        nf = nd_add(m,head,nf);
1.82      noro     1808:                                        ndv_free(nfv);
1.77      noro     1809:                                        nd_removecont(m,nf);
1.82      noro     1810:                                        nfv = ndtondv(m,nf);
                   1811:                                        nd_free(nf);
                   1812:                                        nd_bound[i] = ndv_compute_bound(nfv);
                   1813:                                        if ( nd_demand ) {
                   1814:                                                ndv_save(nfv,i);
                   1815:                                                ndv_free(nfv);
                   1816:                                        } else
                   1817:                                                nd_ps[i] = nfv;
1.77      noro     1818:                                }
                   1819:                        }
1.14      noro     1820:                        sugar = SG(l);
1.71      noro     1821:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.1       noro     1822:                }
1.53      noro     1823:                stat = nd_sp(m,0,l,&h);
1.1       noro     1824:                if ( !stat ) {
                   1825:                        NEXT(l) = d; d = l;
1.20      noro     1826:                        d = nd_reconstruct(m,0,d);
1.1       noro     1827:                        goto again;
                   1828:                }
1.41      noro     1829: #if USE_GEOBUCKET
1.69      noro     1830:                stat = m?nd_nf_pbucket(m,h,nd_ps,!Top,&nf):nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1831: #else
1.69      noro     1832:                stat = nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1833: #endif
1.1       noro     1834:                if ( !stat ) {
                   1835:                        NEXT(l) = d; d = l;
1.20      noro     1836:                        d = nd_reconstruct(m,0,d);
1.1       noro     1837:                        goto again;
                   1838:                } else if ( nf ) {
1.27      noro     1839:                        if ( checkonly ) return 0;
1.71      noro     1840:                        if ( DP_Print ) { printf("+"); fflush(stdout); }
1.63      noro     1841:                        nd_removecont(m,nf);
                   1842:                        nfv = ndtondv(m,nf); nd_free(nf);
1.77      noro     1843:                        nh = ndv_newps(m,nfv,0);
1.1       noro     1844:                        d = update_pairs(d,g,nh);
                   1845:                        g = update_base(g,nh);
                   1846:                        FREENDP(l);
                   1847:                } else {
1.71      noro     1848:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.1       noro     1849:                        FREENDP(l);
                   1850:                }
                   1851:        }
1.77      noro     1852:        if ( nd_demand )
                   1853:                for ( t = g; t; t = NEXT(t) )
                   1854:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1855:        else
                   1856:                for ( t = g; t; t = NEXT(t) )
                   1857:                        BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.1       noro     1858:        return g;
                   1859: }
                   1860:
1.82      noro     1861: NODE nd_gb_trace(int m,int ishomo)
1.20      noro     1862: {
                   1863:        int i,nh,sugar,stat;
1.23      noro     1864:        NODE r,g,t;
1.20      noro     1865:        ND_pairs d;
                   1866:        ND_pairs l;
1.82      noro     1867:        ND h,nf,nfq,s,head;
1.63      noro     1868:        NDV nfv,nfqv;
1.85      noro     1869:        Q q,den,num;
1.69      noro     1870:        union oNDC dn;
1.20      noro     1871:
1.23      noro     1872:        g = 0; d = 0;
                   1873:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     1874:                d = update_pairs(d,g,i);
                   1875:                g = update_base(g,i);
                   1876:        }
                   1877:        sugar = 0;
                   1878:        while ( d ) {
                   1879: again:
                   1880:                l = nd_minp(d,&d);
                   1881:                if ( SG(l) != sugar ) {
1.82      noro     1882:                        if ( ishomo ) {
1.85      noro     1883:                                for ( i = nd_psn-1; i >= 0 && SG(nd_psh[i]) == sugar; i-- ) {
1.82      noro     1884:                                        /* for nd_ps */
                   1885:                                        s = ndvtond(m,nd_ps[i]);
                   1886:                                        s = nd_separate_head(s,&head);
                   1887:                                        nd_nf_pbucket(m,s,nd_ps,1,&nf);
                   1888:                                        nf = nd_add(m,head,nf);
                   1889:                                        ndv_free(nd_ps[i]);
                   1890:                                        nd_ps[i] = ndtondv(m,nf);
                   1891:                                        nd_free(nf);
                   1892:
                   1893:                                        /* for nd_ps_trace */
                   1894:                                        if ( nd_demand )
                   1895:                                                nfv = ndv_load(i);
                   1896:                                        else
                   1897:                                                nfv = nd_ps_trace[i];
                   1898:                                        s = ndvtond(0,nfv);
                   1899:                                        s = nd_separate_head(s,&head);
                   1900:                                        nd_nf(0,s,nd_ps_trace,1,&dn,&nf);
1.85      noro     1901:                                        NTOQ(NM(dn.z),SGN(dn.z),num);
                   1902:                                        mulq(HCQ(head),num,&q); HCQ(head) = q;
                   1903:                                        if ( DN(dn.z) ) {
                   1904:                                                NTOQ(DN(dn.z),1,den);
                   1905:                                                nd_mul_c_q(nf,den);
                   1906:                                        }
1.82      noro     1907:                                        nf = nd_add(0,head,nf);
                   1908:                                        ndv_free(nfv);
                   1909:                                        nd_removecont(0,nf);
                   1910:                                        nfv = ndtondv(0,nf);
                   1911:                                        nd_free(nf);
                   1912:                                        nd_bound[i] = ndv_compute_bound(nfv);
                   1913:                                        if ( nd_demand ) {
                   1914:                                                ndv_save(nfv,i);
                   1915:                                                ndv_free(nfv);
                   1916:                                        } else
                   1917:                                                nd_ps_trace[i] = nfv;
                   1918:                                }
                   1919:                        }
1.20      noro     1920:                        sugar = SG(l);
1.71      noro     1921:                        if ( DP_Print ) fprintf(asir_out,"%d",sugar);
1.20      noro     1922:                }
1.53      noro     1923:                stat = nd_sp(m,0,l,&h);
1.20      noro     1924:                if ( !stat ) {
                   1925:                        NEXT(l) = d; d = l;
                   1926:                        d = nd_reconstruct(m,1,d);
                   1927:                        goto again;
                   1928:                }
1.41      noro     1929: #if USE_GEOBUCKET
1.53      noro     1930:                stat = nd_nf_pbucket(m,h,nd_ps,!Top,&nf);
1.41      noro     1931: #else
1.69      noro     1932:                stat = nd_nf(m,h,nd_ps,!Top,0,&nf);
1.41      noro     1933: #endif
1.20      noro     1934:                if ( !stat ) {
                   1935:                        NEXT(l) = d; d = l;
                   1936:                        d = nd_reconstruct(m,1,d);
                   1937:                        goto again;
                   1938:                } else if ( nf ) {
1.77      noro     1939:                        if ( nd_demand ) {
                   1940:                                nfqv = ndv_load(nd_psn);
                   1941:                                nfq = ndvtond(0,nfqv);
                   1942:                        } else
                   1943:                                nfq = 0;
                   1944:                        if ( !nfq ) {
                   1945:                                if ( !nd_sp(0,1,l,&h) || !nd_nf(0,h,nd_ps_trace,!Top,0,&nfq) ) {
                   1946:                                        NEXT(l) = d; d = l;
                   1947:                                        d = nd_reconstruct(m,1,d);
                   1948:                                        goto again;
                   1949:                                }
1.70      noro     1950:                        }
1.20      noro     1951:                        if ( nfq ) {
1.70      noro     1952:                                /* m|HC(nfq) => failure */
1.63      noro     1953:                                if ( !rem(NM(HCQ(nfq)),m) ) return 0;
                   1954:
1.71      noro     1955:                                if ( DP_Print ) { printf("+"); fflush(stdout); }
1.63      noro     1956:                                nd_removecont(m,nf); nfv = ndtondv(m,nf); nd_free(nf);
                   1957:                                nd_removecont(0,nfq); nfqv = ndtondv(0,nfq); nd_free(nfq);
1.77      noro     1958:                                nh = ndv_newps(0,nfv,nfqv);
1.20      noro     1959:                                d = update_pairs(d,g,nh);
                   1960:                                g = update_base(g,nh);
                   1961:                        } else {
1.71      noro     1962:                                if ( DP_Print ) { printf("*"); fflush(stdout); }
1.20      noro     1963:                        }
                   1964:                } else {
1.71      noro     1965:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.20      noro     1966:                }
                   1967:                FREENDP(l);
                   1968:        }
1.77      noro     1969:        if ( nd_demand )
                   1970:                for ( t = g; t; t = NEXT(t) )
                   1971:                        BDY(t) = (pointer)ndv_load((int)BDY(t));
                   1972:        else
                   1973:                for ( t = g; t; t = NEXT(t) )
                   1974:                        BDY(t) = (pointer)nd_ps_trace[(int)BDY(t)];
1.20      noro     1975:        return g;
                   1976: }
                   1977:
1.23      noro     1978: int ndv_compare(NDV *p1,NDV *p2)
                   1979: {
1.34      noro     1980:        return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1981: }
                   1982:
                   1983: int ndv_compare_rev(NDV *p1,NDV *p2)
                   1984: {
1.34      noro     1985:        return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23      noro     1986: }
                   1987:
1.61      noro     1988: NODE ndv_reduceall(int m,NODE f)
1.23      noro     1989: {
1.69      noro     1990:        int i,n,stat;
                   1991:        ND nf,g,head;
1.23      noro     1992:        NODE t,a0,a;
1.69      noro     1993:        union oNDC dn;
                   1994:        NDV *w;
1.85      noro     1995:        Q q,num,den;
1.23      noro     1996:
1.69      noro     1997:        n = length(f);
                   1998: #if 0
                   1999:        w = (NDV *)ALLOCA(n*sizeof(NDV));
                   2000:        for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) w[i] = (NDV)BDY(t);
                   2001:        qsort(w,n,sizeof(NDV),
                   2002:                (int (*)(const void *,const void *))ndv_compare);
                   2003:        for ( t = f, i = 0; t; i++, t = NEXT(t) ) BDY(t) = (pointer)w[i];
                   2004: #endif
                   2005:        ndv_setup(m,0,f);
                   2006:        for ( i = 0; i < n; ) {
                   2007:                g = ndvtond(m,nd_ps[i]);
                   2008:                g = nd_separate_head(g,&head);
                   2009:                stat = nd_nf(m,g,nd_ps,1,&dn,&nf);
1.50      noro     2010:                if ( !stat )
1.69      noro     2011:                        nd_reconstruct(m,0,0);
                   2012:                else {
1.71      noro     2013:                        if ( DP_Print ) { printf("."); fflush(stdout); }
1.85      noro     2014:                        if ( !m ) {
                   2015:                                NTOQ(NM(dn.z),SGN(dn.z),num);
                   2016:                                mulq(HCQ(head),num,&q); HCQ(head) = q;
                   2017:                                if ( DN(dn.z) ) {
                   2018:                                        NTOQ(DN(dn.z),1,den);
                   2019:                                        nd_mul_c_q(nf,den);
                   2020:                                }
                   2021:                        }
1.69      noro     2022:                        nf = nd_add(m,head,nf);
                   2023:                        ndv_free(nd_ps[i]);
1.24      noro     2024:                        nd_removecont(m,nf);
1.69      noro     2025:                        nd_ps[i] = ndtondv(m,nf); nd_free(nf);
                   2026:                        nd_bound[i] = ndv_compute_bound(nd_ps[i]);
1.50      noro     2027:                        i++;
1.23      noro     2028:                }
                   2029:        }
1.71      noro     2030:        if ( DP_Print ) { printf("\n"); }
1.23      noro     2031:        for ( a0 = 0, i = 0; i < n; i++ ) {
                   2032:                NEXTNODE(a0,a);
1.69      noro     2033:                BDY(a) = (pointer)nd_ps[i];
1.23      noro     2034:        }
                   2035:        NEXT(a) = 0;
                   2036:        return a0;
                   2037: }
                   2038:
1.1       noro     2039: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
                   2040: {
                   2041:        ND_pairs d1,nd,cur,head,prev,remove;
                   2042:
                   2043:        if ( !g ) return d;
                   2044:        d = crit_B(d,t);
                   2045:        d1 = nd_newpairs(g,t);
                   2046:        d1 = crit_M(d1);
                   2047:        d1 = crit_F(d1);
1.55      noro     2048:        if ( do_weyl )
                   2049:                head = d1;
                   2050:        else {
                   2051:                prev = 0; cur = head = d1;
                   2052:                while ( cur ) {
                   2053:                        if ( crit_2( cur->i1,cur->i2 ) ) {
                   2054:                                remove = cur;
                   2055:                                if ( !prev ) head = cur = NEXT(cur);
                   2056:                                else cur = NEXT(prev) = NEXT(cur);
                   2057:                                FREENDP(remove);
                   2058:                        } else {
                   2059:                                prev = cur; cur = NEXT(cur);
                   2060:                        }
1.1       noro     2061:                }
                   2062:        }
                   2063:        if ( !d )
                   2064:                return head;
                   2065:        else {
                   2066:                nd = d;
1.34      noro     2067:                while ( NEXT(nd) ) nd = NEXT(nd);
1.1       noro     2068:                NEXT(nd) = head;
                   2069:                return d;
                   2070:        }
                   2071: }
                   2072:
                   2073: ND_pairs nd_newpairs( NODE g, int t )
                   2074: {
                   2075:        NODE h;
1.61      noro     2076:        UINT *dl;
1.34      noro     2077:        int ts,s;
1.1       noro     2078:        ND_pairs r,r0;
                   2079:
1.20      noro     2080:        dl = DL(nd_psh[t]);
1.34      noro     2081:        ts = SG(nd_psh[t]) - TD(dl);
1.1       noro     2082:        for ( r0 = 0, h = g; h; h = NEXT(h) ) {
                   2083:                NEXTND_pairs(r0,r);
                   2084:                r->i1 = (int)BDY(h);
                   2085:                r->i2 = t;
1.20      noro     2086:                ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34      noro     2087:                s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
                   2088:                SG(r) = MAX(s,ts) + TD(LCM(r));
1.1       noro     2089:        }
                   2090:        NEXT(r) = 0;
                   2091:        return r0;
                   2092: }
                   2093:
                   2094: ND_pairs crit_B( ND_pairs d, int s )
                   2095: {
                   2096:        ND_pairs cur,head,prev,remove;
1.61      noro     2097:        UINT *t,*tl,*lcm;
1.1       noro     2098:        int td,tdl;
                   2099:
                   2100:        if ( !d ) return 0;
1.20      noro     2101:        t = DL(nd_psh[s]);
1.1       noro     2102:        prev = 0;
                   2103:        head = cur = d;
1.61      noro     2104:        lcm = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.1       noro     2105:        while ( cur ) {
                   2106:                tl = cur->lcm;
                   2107:                if ( ndl_reducible(tl,t)
1.20      noro     2108:                        && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
                   2109:                        && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1       noro     2110:                        remove = cur;
                   2111:                        if ( !prev ) {
                   2112:                                head = cur = NEXT(cur);
                   2113:                        } else {
                   2114:                                cur = NEXT(prev) = NEXT(cur);
                   2115:                        }
                   2116:                        FREENDP(remove);
                   2117:                } else {
1.34      noro     2118:                        prev = cur; cur = NEXT(cur);
1.1       noro     2119:                }
                   2120:        }
                   2121:        return head;
                   2122: }
                   2123:
                   2124: ND_pairs crit_M( ND_pairs d1 )
                   2125: {
                   2126:        ND_pairs e,d2,d3,dd,p;
1.61      noro     2127:        UINT *id,*jd;
1.1       noro     2128:
                   2129:        for ( dd = 0, e = d1; e; e = d3 ) {
                   2130:                if ( !(d2 = NEXT(e)) ) {
                   2131:                        NEXT(e) = dd;
                   2132:                        return e;
                   2133:                }
1.34      noro     2134:                id = LCM(e);
1.1       noro     2135:                for ( d3 = 0; d2; d2 = p ) {
1.34      noro     2136:                        p = NEXT(d2);
                   2137:                        jd = LCM(d2);
                   2138:                        if ( ndl_equal(jd,id) )
                   2139:                                ;
                   2140:                        else if ( TD(jd) > TD(id) )
1.1       noro     2141:                                if ( ndl_reducible(jd,id) ) continue;
                   2142:                                else ;
1.34      noro     2143:                        else if ( ndl_reducible(id,jd) ) goto delit;
1.1       noro     2144:                        NEXT(d2) = d3;
                   2145:                        d3 = d2;
                   2146:                }
                   2147:                NEXT(e) = dd;
                   2148:                dd = e;
                   2149:                continue;
                   2150:                /**/
                   2151:        delit:  NEXT(d2) = d3;
                   2152:                d3 = d2;
                   2153:                for ( ; p; p = d2 ) {
                   2154:                        d2 = NEXT(p);
                   2155:                        NEXT(p) = d3;
                   2156:                        d3 = p;
                   2157:                }
                   2158:                FREENDP(e);
                   2159:        }
                   2160:        return dd;
                   2161: }
                   2162:
                   2163: ND_pairs crit_F( ND_pairs d1 )
                   2164: {
                   2165:        ND_pairs rest, head,remove;
                   2166:        ND_pairs last, p, r, w;
                   2167:        int s;
                   2168:
                   2169:        for ( head = last = 0, p = d1; NEXT(p); ) {
                   2170:                r = w = equivalent_pairs(p,&rest);
1.14      noro     2171:                s = SG(r);
1.1       noro     2172:                w = NEXT(w);
                   2173:                while ( w ) {
                   2174:                        if ( crit_2(w->i1,w->i2) ) {
                   2175:                                r = w;
                   2176:                                w = NEXT(w);
                   2177:                                while ( w ) {
                   2178:                                        remove = w;
                   2179:                                        w = NEXT(w);
                   2180:                                        FREENDP(remove);
                   2181:                                }
                   2182:                                break;
1.14      noro     2183:                        } else if ( SG(w) < s ) {
1.1       noro     2184:                                FREENDP(r);
                   2185:                                r = w;
1.14      noro     2186:                                s = SG(r);
1.1       noro     2187:                                w = NEXT(w);
                   2188:                        } else {
                   2189:                                remove = w;
                   2190:                                w = NEXT(w);
                   2191:                                FREENDP(remove);
                   2192:                        }
                   2193:                }
                   2194:                if ( last ) NEXT(last) = r;
                   2195:                else head = r;
                   2196:                NEXT(last = r) = 0;
                   2197:                p = rest;
                   2198:                if ( !p ) return head;
                   2199:        }
                   2200:        if ( !last ) return p;
                   2201:        NEXT(last) = p;
                   2202:        return head;
                   2203: }
                   2204:
                   2205: int crit_2( int dp1, int dp2 )
                   2206: {
1.20      noro     2207:        return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1       noro     2208: }
                   2209:
1.40      noro     2210: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1       noro     2211: {
                   2212:        ND_pairs w,p,r,s;
1.61      noro     2213:        UINT *d;
1.1       noro     2214:
                   2215:        w = d1;
1.34      noro     2216:        d = LCM(w);
1.1       noro     2217:        s = NEXT(w);
                   2218:        NEXT(w) = 0;
                   2219:        for ( r = 0; s; s = p ) {
                   2220:                p = NEXT(s);
1.34      noro     2221:                if ( ndl_equal(d,LCM(s)) ) {
1.39      noro     2222:                        NEXT(s) = w; w = s;
1.1       noro     2223:                } else {
1.39      noro     2224:                        NEXT(s) = r; r = s;
1.1       noro     2225:                }
                   2226:        }
                   2227:        *prest = r;
                   2228:        return w;
                   2229: }
                   2230:
                   2231: NODE update_base(NODE nd,int ndp)
                   2232: {
1.61      noro     2233:        UINT *dl, *dln;
1.1       noro     2234:        NODE last, p, head;
                   2235:
1.20      noro     2236:        dl = DL(nd_psh[ndp]);
1.1       noro     2237:        for ( head = last = 0, p = nd; p; ) {
1.20      noro     2238:                dln = DL(nd_psh[(int)BDY(p)]);
1.34      noro     2239:                if ( ndl_reducible( dln, dl ) ) {
1.1       noro     2240:                        p = NEXT(p);
                   2241:                        if ( last ) NEXT(last) = p;
                   2242:                } else {
                   2243:                        if ( !last ) head = p;
                   2244:                        p = NEXT(last = p);
                   2245:                }
                   2246:        }
                   2247:        head = append_one(head,ndp);
                   2248:        return head;
                   2249: }
                   2250:
                   2251: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
                   2252: {
                   2253:        ND_pairs m,ml,p,l;
1.61      noro     2254:        UINT *lcm;
1.33      noro     2255:        int s,td,len,tlen,c,c1;
1.1       noro     2256:
                   2257:        if ( !(p = NEXT(m = d)) ) {
                   2258:                *prest = p;
                   2259:                NEXT(m) = 0;
                   2260:                return m;
                   2261:        }
1.14      noro     2262:        s = SG(m);
1.77      noro     2263:        if ( !NoSugar ) {
                   2264:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2265:                        if ( (SG(p) < s)
                   2266:                                || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
                   2267:                                ml = l; m = p; s = SG(m);
                   2268:                        }
                   2269:        } else {
                   2270:                for ( ml = 0, l = m; p; p = NEXT(l = p) )
                   2271:                        if ( DL_COMPARE(LCM(p),LCM(m)) < 0 ) {
                   2272:                                ml = l; m = p; s = SG(m);
                   2273:                        }
                   2274:        }
1.1       noro     2275:        if ( !ml ) *prest = NEXT(m);
                   2276:        else {
                   2277:                NEXT(ml) = NEXT(m);
                   2278:                *prest = d;
                   2279:        }
                   2280:        NEXT(m) = 0;
                   2281:        return m;
                   2282: }
                   2283:
1.63      noro     2284: ND_pairs nd_minsugarp( ND_pairs d, ND_pairs *prest )
                   2285: {
1.69      noro     2286:        int msugar,i;
1.63      noro     2287:        ND_pairs t,dm0,dm,dr0,dr;
                   2288:
                   2289:        for ( msugar = SG(d), t = NEXT(d); t; t = NEXT(t) )
                   2290:                if ( SG(t) < msugar ) msugar = SG(t);
                   2291:        dm0 = 0; dr0 = 0;
1.69      noro     2292:        for ( i = 0, t = d; t; t = NEXT(t) )
                   2293:                if ( i < nd_f4_nsp && SG(t) == msugar ) {
1.63      noro     2294:                        if ( dm0 ) NEXT(dm) = t;
                   2295:                        else dm0 = t;
                   2296:                        dm = t;
1.69      noro     2297:                        i++;
1.63      noro     2298:                } else {
                   2299:                        if ( dr0 ) NEXT(dr) = t;
                   2300:                        else dr0 = t;
                   2301:                        dr = t;
                   2302:                }
                   2303:        NEXT(dm) = 0;
                   2304:        if ( dr0 ) NEXT(dr) = 0;
                   2305:        *prest = dr0;
                   2306:        return dm0;
                   2307: }
                   2308:
1.77      noro     2309: int ndv_newps(int m,NDV a,NDV aq)
1.1       noro     2310: {
1.3       noro     2311:        int len;
1.13      noro     2312:        RHist r;
1.20      noro     2313:        NDV b;
1.3       noro     2314:
1.1       noro     2315:        if ( nd_psn == nd_pslen ) {
                   2316:                nd_pslen *= 2;
1.11      noro     2317:                nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.53      noro     2318:                nd_ps_trace = (NDV *)REALLOC((char *)nd_ps_trace,nd_pslen*sizeof(NDV));
1.13      noro     2319:                nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.61      noro     2320:                nd_bound = (UINT **)
                   2321:                        REALLOC((char *)nd_bound,nd_pslen*sizeof(UINT *));
1.1       noro     2322:        }
1.39      noro     2323:        NEWRHist(r); nd_psh[nd_psn] = r;
1.63      noro     2324:        nd_ps[nd_psn] = a;
1.39      noro     2325:        if ( aq ) {
1.63      noro     2326:                nd_ps_trace[nd_psn] = aq;
                   2327:                nd_bound[nd_psn] = ndv_compute_bound(aq);
1.39      noro     2328:                SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1.53      noro     2329:        } else {
1.63      noro     2330:                nd_bound[nd_psn] = ndv_compute_bound(a);
1.53      noro     2331:                SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1.39      noro     2332:        }
1.77      noro     2333:        if ( nd_demand ) {
                   2334:                if ( aq ) {
                   2335:                        ndv_save(nd_ps_trace[nd_psn],nd_psn);
                   2336:                        nd_ps_trace[nd_psn] = 0;
                   2337:                } else {
                   2338:                        ndv_save(nd_ps[nd_psn],nd_psn);
                   2339:                        nd_ps[nd_psn] = 0;
                   2340:                }
                   2341:        }
1.1       noro     2342:        return nd_psn++;
                   2343: }
                   2344:
1.61      noro     2345: void ndv_setup(int mod,int trace,NODE f)
1.1       noro     2346: {
1.5       noro     2347:        int i,j,td,len,max;
1.1       noro     2348:        NODE s,s0,f0;
1.61      noro     2349:        UINT *d;
1.13      noro     2350:        RHist r;
1.69      noro     2351:        NDV *w;
1.61      noro     2352:        NDV a,am;
1.11      noro     2353:
                   2354:        nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1       noro     2355:
1.69      noro     2356:        for ( nd_psn = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) nd_psn++;
                   2357:        w = (NDV *)ALLOCA(nd_psn*sizeof(NDV));
                   2358:        for ( i = 0, s = f; s; s = NEXT(s) ) if ( BDY(s) ) w[i++] = BDY(s);
                   2359:        qsort(w,nd_psn,sizeof(NDV),
                   2360:                (int (*)(const void *,const void *))ndv_compare);
                   2361:        nd_pslen = 2*nd_psn;
1.11      noro     2362:        nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.53      noro     2363:        nd_ps_trace = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13      noro     2364:        nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.61      noro     2365:        nd_bound = (UINT **)MALLOC(nd_pslen*sizeof(UINT *));
1.57      noro     2366:
1.11      noro     2367:        if ( !nd_red )
1.13      noro     2368:                nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1.71      noro     2369:        for ( i = 0; i < REDTAB_LEN; i++ ) nd_red[i] = 0;
1.69      noro     2370:        for ( i = 0; i < nd_psn; i++ ) {
1.39      noro     2371:                if ( trace ) {
1.69      noro     2372:                        a = nd_ps_trace[i] = ndv_dup(0,w[i]);
1.61      noro     2373:                        ndv_removecont(0,a);
                   2374:                        am = nd_ps[i] = ndv_dup(mod,a);
                   2375:                        ndv_mod(mod,am);
                   2376:                        ndv_removecont(mod,am);
                   2377:                } else {
1.69      noro     2378:                        a = nd_ps[i] = ndv_dup(mod,w[i]);
1.61      noro     2379:                        ndv_removecont(mod,a);
1.39      noro     2380:                }
1.61      noro     2381:                NEWRHist(r); SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.57      noro     2382:                nd_bound[i] = ndv_compute_bound(a);
1.20      noro     2383:                nd_psh[i] = r;
1.77      noro     2384:                if ( nd_demand ) {
                   2385:                        if ( trace ) {
                   2386:                                ndv_save(nd_ps_trace[i],i);
                   2387:                                nd_ps_trace[i] = 0;
                   2388:                        } else {
                   2389:                                ndv_save(nd_ps[i],i);
                   2390:                                nd_ps[i] = 0;
                   2391:                        }
                   2392:                }
1.20      noro     2393:        }
                   2394: }
                   2395:
1.63      noro     2396: void nd_gr(LIST f,LIST v,int m,int f4,struct order_spec *ord,LIST *rp)
1.1       noro     2397: {
1.61      noro     2398:        VL tv,fv,vv,vc;
1.1       noro     2399:        NODE fd,fd0,r,r0,t,x,s,xx;
1.61      noro     2400:        int e,max,nvar;
1.69      noro     2401:        NDV b;
1.77      noro     2402:        int ishomo;
1.1       noro     2403:
1.78      noro     2404:        if ( !m && Demand ) nd_demand = 1;
                   2405:        else nd_demand = 0;
                   2406:
1.74      noro     2407:        ndv_alloc = 0;
1.1       noro     2408:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2409:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2410:        switch ( ord->id ) {
                   2411:                case 1:
                   2412:                        if ( ord->nv != nvar )
                   2413:                                error("nd_{gr,f4} : invalid order specification");
                   2414:                        break;
                   2415:                default:
                   2416:                        break;
                   2417:        }
1.32      noro     2418:        nd_init_ord(ord);
1.61      noro     2419:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2420:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2421:                        e = getdeg(tv->v,(P)BDY(t));
                   2422:                        max = MAX(e,max);
                   2423:                }
                   2424:        nd_setup_parameters(nvar,max);
1.77      noro     2425:        ishomo = 1;
1.1       noro     2426:        for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61      noro     2427:                b = (pointer)ptondv(CO,vv,(P)BDY(t));
1.77      noro     2428:                if ( ishomo )
                   2429:                        ishomo = ishomo && ndv_ishomo(b);
1.69      noro     2430:                if ( m ) ndv_mod(m,b);
1.61      noro     2431:                if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1       noro     2432:        }
                   2433:        if ( fd0 ) NEXT(fd) = 0;
1.61      noro     2434:        ndv_setup(m,0,fd0);
1.77      noro     2435:        x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82      noro     2436:        nd_demand = 0;
1.61      noro     2437:        x = ndv_reducebase(x);
                   2438:        x = ndv_reduceall(m,x);
1.23      noro     2439:        for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1       noro     2440:                NEXTNODE(r0,r);
1.61      noro     2441:                BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20      noro     2442:        }
                   2443:        if ( r0 ) NEXT(r) = 0;
                   2444:        MKLIST(*rp,r0);
1.74      noro     2445:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.20      noro     2446: }
                   2447:
1.52      noro     2448: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20      noro     2449: {
1.89      noro     2450:        struct order_spec *ord1;
1.61      noro     2451:        VL tv,fv,vv,vc;
1.27      noro     2452:        NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.61      noro     2453:        int m,nocheck,nvar,mindex,e,max;
                   2454:        NDV c;
                   2455:        NMV a;
1.27      noro     2456:        P p;
1.61      noro     2457:        EPOS oepos;
1.82      noro     2458:        int obpe,oadv,wmax,i,len,cbpe,ishomo;
1.20      noro     2459:
                   2460:        get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74      noro     2461:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88      noro     2462:        switch ( ord->id ) {
                   2463:                case 1:
                   2464:                        if ( ord->nv != nvar )
                   2465:                                error("nd_gr_trace : invalid order specification");
                   2466:                        break;
                   2467:                default:
                   2468:                        break;
                   2469:        }
1.52      noro     2470:        nocheck = 0;
                   2471:        mindex = 0;
                   2472:
1.78      noro     2473:        if ( Demand ) nd_demand = 1;
                   2474:        else nd_demand = 0;
                   2475:
1.52      noro     2476:        /* setup modulus */
                   2477:        if ( trace < 0 ) {
                   2478:                trace = -trace;
                   2479:                nocheck = 1;
                   2480:        }
                   2481:        m = trace > 1 ? trace : get_lprime(mindex);
1.61      noro     2482:        for ( t = BDY(f), max = 0; t; t = NEXT(t) )
                   2483:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   2484:                        e = getdeg(tv->v,(P)BDY(t));
                   2485:                        max = MAX(e,max);
1.23      noro     2486:                }
1.61      noro     2487:        nd_init_ord(ord);
                   2488:        nd_setup_parameters(nvar,max);
                   2489:        obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82      noro     2490:        ishomo = 1;
1.61      noro     2491:        for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
                   2492:                c = ptondv(CO,vv,(P)BDY(t));
1.82      noro     2493:                if ( ishomo )
                   2494:                        ishomo = ishomo && ndv_ishomo(c);
1.61      noro     2495:                if ( c ) {
                   2496:                        NEXTNODE(in0,in); BDY(in) = (pointer)c;
                   2497:                        NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23      noro     2498:                }
1.61      noro     2499:        }
                   2500:        if ( in0 ) NEXT(in) = 0;
                   2501:        if ( fd0 ) NEXT(fd) = 0;
1.82      noro     2502:        if ( !ishomo && homo ) {
1.61      noro     2503:                for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
                   2504:                        c = (NDV)BDY(t); len = LEN(c);
                   2505:                        for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
                   2506:                                wmax = MAX(TD(DL(a)),wmax);
                   2507:                }
                   2508:                homogenize_order(ord,nvar,&ord1);
1.89      noro     2509:                nd_init_ord(ord1);
1.61      noro     2510:                nd_setup_parameters(nvar+1,wmax);
                   2511:                for ( t = fd0; t; t = NEXT(t) )
                   2512:                        ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27      noro     2513:        }
1.52      noro     2514:        while ( 1 ) {
1.77      noro     2515:                if ( Demand )
                   2516:                        nd_demand = 1;
1.61      noro     2517:                ndv_setup(m,1,fd0);
1.82      noro     2518:                cand = nd_gb_trace(m,ishomo || homo);
1.52      noro     2519:                if ( !cand ) {
                   2520:                        /* failure */
1.61      noro     2521:                        if ( trace > 1 ) { *rp = 0; return; }
                   2522:                        else m = get_lprime(++mindex);
1.52      noro     2523:                        continue;
                   2524:                }
1.82      noro     2525:                if ( !ishomo && homo ) {
1.27      noro     2526:                        /* dehomogenization */
1.61      noro     2527:                        for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45      noro     2528:                        nd_init_ord(ord);
1.61      noro     2529:                        nd_setup_parameters(nvar,0);
1.27      noro     2530:                }
1.77      noro     2531:                nd_demand = 0;
1.61      noro     2532:                cand = ndv_reducebase(cand);
                   2533:                cand = ndv_reduceall(0,cand);
1.78      noro     2534:                cbpe = nd_bpe;
1.61      noro     2535:                if ( nocheck )
                   2536:                        break;
                   2537:                if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52      noro     2538:                        /* success */
                   2539:                        break;
                   2540:                else if ( trace > 1 ) {
                   2541:                        /* failure */
                   2542:                        *rp = 0; return;
1.61      noro     2543:                } else {
1.52      noro     2544:                        /* try the next modulus */
                   2545:                        m = get_lprime(++mindex);
1.61      noro     2546:                        /* reset the parameters */
1.82      noro     2547:                        if ( !ishomo && homo ) {
1.89      noro     2548:                                nd_init_ord(ord1);
1.61      noro     2549:                                nd_setup_parameters(nvar+1,wmax);
                   2550:                        } else {
                   2551:                                nd_init_ord(ord);
                   2552:                                nd_setup_parameters(nvar,max);
                   2553:                        }
                   2554:                }
1.52      noro     2555:        }
1.27      noro     2556:        /* dp->p */
1.61      noro     2557:        nd_bpe = cbpe;
1.73      noro     2558:        nd_setup_parameters(nd_nvar,0);
1.61      noro     2559:        for ( r = cand; r; r = NEXT(r) ) BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.27      noro     2560:        MKLIST(*rp,cand);
1.1       noro     2561: }
                   2562:
1.61      noro     2563: void dltondl(int n,DL dl,UINT *r)
1.1       noro     2564: {
1.61      noro     2565:        UINT *d;
1.57      noro     2566:        int i,j,l,s,ord_l;
1.43      noro     2567:        struct order_pair *op;
1.1       noro     2568:
                   2569:        d = dl->d;
1.41      noro     2570:        for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     2571:        if ( nd_blockmask ) {
                   2572:                l = nd_blockmask->n;
                   2573:                op = nd_blockmask->order_pair;
                   2574:                for ( j = 0, s = 0; j < l; j++ ) {
                   2575:                        ord_l = op[j].length;
1.57      noro     2576:                        for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43      noro     2577:                }
                   2578:                TD(r) = ndl_weight(r);
1.61      noro     2579:                ndl_weight_mask(r);
1.43      noro     2580:        } else {
1.56      noro     2581:                for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43      noro     2582:                TD(r) = ndl_weight(r);
                   2583:        }
1.1       noro     2584: }
                   2585:
1.61      noro     2586: DL ndltodl(int n,UINT *ndl)
1.1       noro     2587: {
                   2588:        DL dl;
                   2589:        int *d;
1.57      noro     2590:        int i,j,l,s,ord_l;
1.43      noro     2591:        struct order_pair *op;
1.1       noro     2592:
                   2593:        NEWDL(dl,n);
1.34      noro     2594:        dl->td = TD(ndl);
1.1       noro     2595:        d = dl->d;
1.43      noro     2596:        if ( nd_blockmask ) {
                   2597:                l = nd_blockmask->n;
                   2598:                op = nd_blockmask->order_pair;
                   2599:                for ( j = 0, s = 0; j < l; j++ ) {
                   2600:                        ord_l = op[j].length;
1.57      noro     2601:                        for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43      noro     2602:                }
                   2603:        } else {
1.56      noro     2604:                for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43      noro     2605:        }
1.1       noro     2606:        return dl;
                   2607: }
                   2608:
1.61      noro     2609: void ndl_print(UINT *dl)
1.1       noro     2610: {
                   2611:        int n;
1.57      noro     2612:        int i,j,l,ord_l,s,s0;
1.43      noro     2613:        struct order_pair *op;
1.1       noro     2614:
                   2615:        n = nd_nvar;
                   2616:        printf("<<");
1.43      noro     2617:        if ( nd_blockmask ) {
                   2618:                l = nd_blockmask->n;
                   2619:                op = nd_blockmask->order_pair;
                   2620:                for ( j = 0, s = s0 = 0; j < l; j++ ) {
                   2621:                        ord_l = op[j].length;
1.57      noro     2622:                        for ( i = 0; i < ord_l; i++, s++ )
                   2623:                                printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43      noro     2624:                }
                   2625:        } else {
1.56      noro     2626:                for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43      noro     2627:        }
1.1       noro     2628:        printf(">>");
                   2629: }
                   2630:
                   2631: void nd_print(ND p)
                   2632: {
                   2633:        NM m;
                   2634:
                   2635:        if ( !p )
                   2636:                printf("0\n");
                   2637:        else {
                   2638:                for ( m = BDY(p); m; m = NEXT(m) ) {
1.71      noro     2639:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   2640:                        else printf("+%d*",CM(m));
1.14      noro     2641:                        ndl_print(DL(m));
1.1       noro     2642:                }
                   2643:                printf("\n");
                   2644:        }
                   2645: }
                   2646:
1.16      noro     2647: void nd_print_q(ND p)
                   2648: {
                   2649:        NM m;
                   2650:
                   2651:        if ( !p )
                   2652:                printf("0\n");
                   2653:        else {
                   2654:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2655:                        printf("+");
1.74      noro     2656:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     2657:                        printf("*");
                   2658:                        ndl_print(DL(m));
                   2659:                }
                   2660:                printf("\n");
                   2661:        }
                   2662: }
                   2663:
1.1       noro     2664: void ndp_print(ND_pairs d)
                   2665: {
                   2666:        ND_pairs t;
                   2667:
1.34      noro     2668:        for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1       noro     2669:        printf("\n");
                   2670: }
                   2671:
1.20      noro     2672: void nd_removecont(int mod,ND p)
1.16      noro     2673: {
                   2674:        int i,n;
                   2675:        Q *w;
                   2676:        Q dvr,t;
                   2677:        NM m;
1.21      noro     2678:        struct oVECT v;
                   2679:        N q,r;
1.16      noro     2680:
1.71      noro     2681:        if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
                   2682:        else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20      noro     2683:        else {
                   2684:                for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
                   2685:                w = (Q *)ALLOCA(n*sizeof(Q));
1.21      noro     2686:                v.len = n;
                   2687:                v.body = (pointer *)w;
1.34      noro     2688:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2689:                removecont_array(w,n);
                   2690:                for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16      noro     2691:        }
                   2692: }
                   2693:
1.21      noro     2694: void nd_removecont2(ND p1,ND p2)
                   2695: {
                   2696:        int i,n1,n2,n;
                   2697:        Q *w;
                   2698:        Q dvr,t;
                   2699:        NM m;
                   2700:        struct oVECT v;
                   2701:        N q,r;
                   2702:
                   2703:        if ( !p1 ) {
                   2704:                nd_removecont(0,p2); return;
                   2705:        } else if ( !p2 ) {
                   2706:                nd_removecont(0,p1); return;
                   2707:        }
                   2708:        n1 = nd_length(p1);
                   2709:        n2 = nd_length(p2);
                   2710:        n = n1+n2;
                   2711:        w = (Q *)ALLOCA(n*sizeof(Q));
                   2712:        v.len = n;
                   2713:        v.body = (pointer *)w;
1.34      noro     2714:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
                   2715:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21      noro     2716:        removecont_array(w,n);
                   2717:        for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2718:        for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
                   2719: }
                   2720:
1.20      noro     2721: void ndv_removecont(int mod,NDV p)
1.16      noro     2722: {
                   2723:        int i,len;
                   2724:        Q *w;
                   2725:        Q dvr,t;
                   2726:        NMV m;
                   2727:
1.71      noro     2728:        if ( mod == -1 )
                   2729:                ndv_mul_c(mod,p,_invsf(HCM(p)));
                   2730:        else if ( mod )
1.20      noro     2731:                ndv_mul_c(mod,p,invm(HCM(p),mod));
                   2732:        else {
                   2733:                len = p->len;
                   2734:                w = (Q *)ALLOCA(len*sizeof(Q));
1.34      noro     2735:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
1.20      noro     2736:                sortbynm(w,len);
                   2737:                qltozl(w,len,&dvr);
                   2738:                for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
                   2739:                        divq(CQ(m),dvr,&t); CQ(m) = t;
                   2740:                }
1.16      noro     2741:        }
1.21      noro     2742: }
                   2743:
1.61      noro     2744: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
                   2745: {
                   2746:        int len,i,max;
                   2747:        NMV m,mr0,mr,t;
                   2748:
                   2749:        len = p->len;
                   2750:        for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
                   2751:                max = MAX(max,TD(DL(m)));
                   2752:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   2753:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   2754:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   2755:        t = (NMV)ALLOCA(nmv_adv);
                   2756:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   2757:                ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
                   2758:                CQ(mr) = CQ(m);
                   2759:                ndl_copy(DL(t),DL(mr));
                   2760:        }
                   2761:        NV(p)++;
                   2762:        BDY(p) = mr0;
                   2763: }
                   2764:
1.45      noro     2765: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23      noro     2766: {
1.45      noro     2767:        int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.23      noro     2768:        Q *w;
                   2769:        Q dvr,t;
                   2770:        NMV m,r;
                   2771:
                   2772:        len = p->len;
                   2773:        newnvar = nd_nvar-1;
1.48      noro     2774:        newexporigin = nd_get_exporigin(ord);
1.45      noro     2775:        newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23      noro     2776:        for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34      noro     2777:                ndl_dehomogenize(DL(m));
1.23      noro     2778:        if ( newwpd != nd_wpd ) {
1.90    ! noro     2779:                newadv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT));
1.61      noro     2780:                for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.45      noro     2781:                        CQ(r) = CQ(m);
                   2782:                        for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
                   2783:                        adj = nd_exporigin-newexporigin;
                   2784:                        for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23      noro     2785:                }
                   2786:        }
                   2787:        NV(p)--;
                   2788: }
                   2789:
1.21      noro     2790: void removecont_array(Q *c,int n)
                   2791: {
                   2792:        struct oVECT v;
                   2793:        Q d0,d1,a,u,u1,gcd;
                   2794:        int i;
                   2795:        N qn,rn,gn;
                   2796:        Q *q,*r;
                   2797:
                   2798:        q = (Q *)ALLOCA(n*sizeof(Q));
                   2799:        r = (Q *)ALLOCA(n*sizeof(Q));
                   2800:        v.id = O_VECT; v.len = n; v.body = (pointer *)c;
                   2801:        igcdv_estimate(&v,&d0);
                   2802:        for ( i = 0; i < n; i++ ) {
                   2803:                divn(NM(c[i]),NM(d0),&qn,&rn);
                   2804:                NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
                   2805:                NTOQ(rn,SGN(c[i]),r[i]);
                   2806:        }
1.34      noro     2807:        for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21      noro     2808:        if ( i < n ) {
                   2809:                v.id = O_VECT; v.len = n; v.body = (pointer *)r;
                   2810:                igcdv(&v,&d1);
                   2811:                gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
                   2812:                divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
                   2813:                for ( i = 0; i < n; i++ ) {
                   2814:                        mulq(a,q[i],&u);
                   2815:                        if ( r[i] ) {
                   2816:                                divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
                   2817:                                addq(u,u1,&q[i]);
                   2818:                        } else
                   2819:                                q[i] = u;
                   2820:                }
                   2821:        }
1.34      noro     2822:        for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16      noro     2823: }
                   2824:
1.19      noro     2825: void nd_mul_c(int mod,ND p,int mul)
1.1       noro     2826: {
                   2827:        NM m;
                   2828:        int c,c1;
                   2829:
1.34      noro     2830:        if ( !p ) return;
1.71      noro     2831:        if ( mod == -1 )
                   2832:                for ( m = BDY(p); m; m = NEXT(m) )
                   2833:                        CM(m) = _mulsf(CM(m),mul);
                   2834:        else
                   2835:                for ( m = BDY(p); m; m = NEXT(m) ) {
                   2836:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   2837:                }
1.1       noro     2838: }
                   2839:
1.16      noro     2840: void nd_mul_c_q(ND p,Q mul)
                   2841: {
                   2842:        NM m;
                   2843:        Q c;
                   2844:
1.34      noro     2845:        if ( !p ) return;
1.16      noro     2846:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2847:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   2848:        }
                   2849: }
                   2850:
1.61      noro     2851: void nd_mul_c_p(VL vl,ND p,P mul)
                   2852: {
                   2853:        NM m;
                   2854:        P c;
                   2855:
                   2856:        if ( !p ) return;
                   2857:        for ( m = BDY(p); m; m = NEXT(m) ) {
                   2858:                mulp(vl,CP(m),mul,&c); CP(m) = c;
                   2859:        }
                   2860: }
                   2861:
1.1       noro     2862: void nd_free(ND p)
                   2863: {
                   2864:        NM t,s;
                   2865:
1.34      noro     2866:        if ( !p ) return;
1.1       noro     2867:        t = BDY(p);
                   2868:        while ( t ) {
                   2869:                s = NEXT(t);
                   2870:                FREENM(t);
                   2871:                t = s;
                   2872:        }
                   2873:        FREEND(p);
                   2874: }
                   2875:
1.23      noro     2876: void ndv_free(NDV p)
                   2877: {
                   2878:        GC_free(BDY(p));
                   2879: }
                   2880:
1.61      noro     2881: void nd_append_red(UINT *d,int i)
1.1       noro     2882: {
1.13      noro     2883:        RHist m,m0;
1.1       noro     2884:        int h;
                   2885:
1.13      noro     2886:        NEWRHist(m);
1.34      noro     2887:        h = ndl_hash_value(d);
1.13      noro     2888:        m->index = i;
1.14      noro     2889:        ndl_copy(d,DL(m));
1.1       noro     2890:        NEXT(m) = nd_red[h];
                   2891:        nd_red[h] = m;
                   2892: }
                   2893:
1.61      noro     2894: UINT *ndv_compute_bound(NDV p)
1.1       noro     2895: {
1.61      noro     2896:        UINT *d1,*d2,*t;
                   2897:        UINT u;
1.57      noro     2898:        int i,j,k,l,len,ind;
1.45      noro     2899:        NMV m;
1.1       noro     2900:
                   2901:        if ( !p )
                   2902:                return 0;
1.61      noro     2903:        d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   2904:        d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45      noro     2905:        len = LEN(p);
                   2906:        m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
                   2907:        for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14      noro     2908:                ndl_lcm(DL(m),d1,d2);
1.1       noro     2909:                t = d1; d1 = d2; d2 = t;
                   2910:        }
1.12      noro     2911:        l = nd_nvar+31;
1.61      noro     2912:        t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57      noro     2913:        for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
                   2914:                u = d1[i];
                   2915:                k = (nd_epw-1)*nd_bpe;
                   2916:                for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
                   2917:                        t[ind] = (u>>k)&nd_mask0;
                   2918:        }
                   2919:        for ( ; ind < l; ind++ ) t[ind] = 0;
1.1       noro     2920:        return t;
                   2921: }
                   2922:
1.48      noro     2923: int nd_get_exporigin(struct order_spec *ord)
                   2924: {
1.51      noro     2925:        switch ( ord->id ) {
1.41      noro     2926:                case 0:
1.48      noro     2927:                        return 1;
1.41      noro     2928:                case 1:
                   2929:                        /* block order */
1.43      noro     2930:                        /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48      noro     2931:                        return ord->ord.block.length+1;
1.41      noro     2932:                case 2:
1.52      noro     2933:                        error("nd_get_exporigin : matrix order is not supported yet.");
1.41      noro     2934:        }
1.48      noro     2935: }
                   2936:
1.61      noro     2937: void nd_setup_parameters(int nvar,int max) {
1.79      noro     2938:        int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57      noro     2939:        struct order_pair *op;
1.48      noro     2940:
1.73      noro     2941:        nd_nvar = nvar;
1.79      noro     2942:        if ( max ) {
1.83      noro     2943:                /* XXX */
                   2944:                if ( do_weyl ) nd_bpe = 32;
                   2945:                else if ( max < 2 ) nd_bpe = 1;
1.79      noro     2946:                else if ( max < 4 ) nd_bpe = 2;
                   2947:                else if ( max < 8 ) nd_bpe = 3;
                   2948:                else if ( max < 16 ) nd_bpe = 4;
                   2949:                else if ( max < 32 ) nd_bpe = 5;
                   2950:                else if ( max < 64 ) nd_bpe = 6;
                   2951:                else if ( max < 256 ) nd_bpe = 8;
                   2952:                else if ( max < 1024 ) nd_bpe = 10;
                   2953:                else if ( max < 65536 ) nd_bpe = 16;
                   2954:                else nd_bpe = 32;
                   2955:        }
1.61      noro     2956:        nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48      noro     2957:        elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
                   2958:        nd_exporigin = nd_get_exporigin(nd_ord);
1.79      noro     2959:        wpd = nd_exporigin+elen;
                   2960:        if ( wpd != nd_wpd ) {
                   2961:                nd_free_private_storage();
                   2962:                nd_wpd = wpd;
                   2963:        }
1.1       noro     2964:        if ( nd_bpe < 32 ) {
                   2965:                nd_mask0 = (1<<nd_bpe)-1;
                   2966:        } else {
                   2967:                nd_mask0 = 0xffffffff;
                   2968:        }
                   2969:        bzero(nd_mask,sizeof(nd_mask));
                   2970:        nd_mask1 = 0;
                   2971:        for ( i = 0; i < nd_epw; i++ ) {
                   2972:                nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
                   2973:                nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
                   2974:        }
1.90    ! noro     2975:        nmv_adv = ROUND_FOR_ALIGN(sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT));
1.57      noro     2976:        nd_epos = nd_create_epos(nd_ord);
1.43      noro     2977:        nd_blockmask = nd_create_blockmask(nd_ord);
1.1       noro     2978: }
                   2979:
1.20      noro     2980: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1       noro     2981: {
1.37      noro     2982:        int i,obpe,oadv,h;
1.71      noro     2983:        static NM prev_nm_free_list;
                   2984:        static ND_pairs prev_ndp_free_list;
1.13      noro     2985:        RHist mr0,mr;
                   2986:        RHist r;
1.37      noro     2987:        RHist *old_red;
1.71      noro     2988:        ND_pairs s0,s,t;
1.43      noro     2989:        EPOS oepos;
1.15      noro     2990:
1.1       noro     2991:        obpe = nd_bpe;
1.11      noro     2992:        oadv = nmv_adv;
1.43      noro     2993:        oepos = nd_epos;
1.63      noro     2994:        if ( obpe < 2 ) nd_bpe = 2;
                   2995:        else if ( obpe < 3 ) nd_bpe = 3;
1.62      noro     2996:        else if ( obpe < 4 ) nd_bpe = 4;
1.63      noro     2997:        else if ( obpe < 5 ) nd_bpe = 5;
1.34      noro     2998:        else if ( obpe < 6 ) nd_bpe = 6;
                   2999:        else if ( obpe < 8 ) nd_bpe = 8;
1.63      noro     3000:        else if ( obpe < 10 ) nd_bpe = 10;
1.34      noro     3001:        else if ( obpe < 16 ) nd_bpe = 16;
                   3002:        else if ( obpe < 32 ) nd_bpe = 32;
                   3003:        else error("nd_reconstruct : exponent too large");
1.5       noro     3004:
1.73      noro     3005:        nd_setup_parameters(nd_nvar,0);
1.1       noro     3006:        prev_nm_free_list = _nm_free_list;
                   3007:        prev_ndp_free_list = _ndp_free_list;
                   3008:        _nm_free_list = 0;
                   3009:        _ndp_free_list = 0;
1.53      noro     3010:        for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
                   3011:        if ( trace )
                   3012:                for ( i = nd_psn-1; i >= 0; i-- )
                   3013:                        ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1       noro     3014:        s0 = 0;
                   3015:        for ( t = d; t; t = NEXT(t) ) {
                   3016:                NEXTND_pairs(s0,s);
                   3017:                s->i1 = t->i1;
                   3018:                s->i2 = t->i2;
1.14      noro     3019:                SG(s) = SG(t);
1.61      noro     3020:                ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1       noro     3021:        }
1.37      noro     3022:
                   3023:        old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6       noro     3024:        for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37      noro     3025:                old_red[i] = nd_red[i];
                   3026:                nd_red[i] = 0;
                   3027:        }
                   3028:        for ( i = 0; i < REDTAB_LEN; i++ )
                   3029:                for ( r = old_red[i]; r; r = NEXT(r) ) {
                   3030:                        NEWRHist(mr);
1.13      noro     3031:                        mr->index = r->index;
1.20      noro     3032:                        SG(mr) = SG(r);
1.61      noro     3033:                        ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37      noro     3034:                        h = ndl_hash_value(DL(mr));
                   3035:                        NEXT(mr) = nd_red[h];
                   3036:                        nd_red[h] = mr;
1.6       noro     3037:                }
1.37      noro     3038:        for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
                   3039:        old_red = 0;
1.11      noro     3040:        for ( i = 0; i < nd_psn; i++ ) {
1.20      noro     3041:                NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61      noro     3042:                ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13      noro     3043:                nd_psh[i] = r;
1.11      noro     3044:        }
1.1       noro     3045:        if ( s0 ) NEXT(s) = 0;
                   3046:        prev_nm_free_list = 0;
                   3047:        prev_ndp_free_list = 0;
1.71      noro     3048: #if 0
1.1       noro     3049:        GC_gcollect();
1.71      noro     3050: #endif
1.1       noro     3051:        return s0;
                   3052: }
                   3053:
1.61      noro     3054: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1       noro     3055: {
1.57      noro     3056:        int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43      noro     3057:        struct order_pair *op;
1.1       noro     3058:
                   3059:        n = nd_nvar;
1.61      noro     3060:        oepw = (sizeof(UINT)*8)/obpe;
1.43      noro     3061:        omask0 = (1<<obpe)-1;
1.34      noro     3062:        TD(r) = TD(d);
1.41      noro     3063:        for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43      noro     3064:        if ( nd_blockmask ) {
                   3065:                l = nd_blockmask->n;
                   3066:                op = nd_blockmask->order_pair;
                   3067:                for ( i = 1; i < nd_exporigin; i++ )
                   3068:                        r[i] = d[i];
                   3069:                for ( j = 0, s = 0; j < l; j++ ) {
                   3070:                        ord_l = op[j].length;
1.57      noro     3071:                        for ( i = 0; i < ord_l; i++, s++ ) {
                   3072:                                ei =  GET_EXP_OLD(d,s);
                   3073:                                PUT_EXP(r,s,ei);
                   3074:                        }
1.1       noro     3075:                }
1.43      noro     3076:        } else {
1.56      noro     3077:                for ( i = 0; i < n; i++ ) {
                   3078:                        ei = GET_EXP_OLD(d,i);
                   3079:                        PUT_EXP(r,i,ei);
                   3080:                }
1.1       noro     3081:        }
                   3082: }
1.3       noro     3083:
1.6       noro     3084: ND nd_copy(ND p)
                   3085: {
                   3086:        NM m,mr,mr0;
1.41      noro     3087:        int c,n;
1.6       noro     3088:        ND r;
                   3089:
                   3090:        if ( !p )
                   3091:                return 0;
                   3092:        else {
                   3093:                for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
                   3094:                        NEXTNM(mr0,mr);
1.14      noro     3095:                        CM(mr) = CM(m);
                   3096:                        ndl_copy(DL(m),DL(mr));
1.6       noro     3097:                }
                   3098:                NEXT(mr) = 0;
1.31      noro     3099:                MKND(NV(p),mr0,LEN(p),r);
1.14      noro     3100:                SG(r) = SG(p);
1.6       noro     3101:                return r;
                   3102:        }
                   3103: }
                   3104:
1.53      noro     3105: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11      noro     3106: {
                   3107:        NM m;
                   3108:        NDV p1,p2;
                   3109:        ND t1,t2;
1.61      noro     3110:        UINT *lcm;
1.31      noro     3111:        int td;
1.11      noro     3112:
1.77      noro     3113:        if ( !mod && nd_demand ) {
                   3114:                p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53      noro     3115:        } else {
1.77      noro     3116:                if ( trace ) {
                   3117:                        p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
                   3118:                } else {
                   3119:                        p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
                   3120:                }
1.20      noro     3121:        }
1.34      noro     3122:        lcm = LCM(p);
1.11      noro     3123:        NEWNM(m);
1.20      noro     3124:        CQ(m) = HCQ(p2);
1.34      noro     3125:        ndl_sub(lcm,HDL(p1),DL(m));
1.56      noro     3126:        if ( ndl_check_bound2(p->i1,DL(m)) )
                   3127:                return 0;
1.55      noro     3128:        t1 = ndv_mul_nm(mod,m,p1);
1.71      noro     3129:        if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
                   3130:        else if ( mod ) CM(m) = mod-HCM(p1);
1.34      noro     3131:        else chsgnq(HCQ(p1),&CQ(m));
                   3132:        ndl_sub(lcm,HDL(p2),DL(m));
1.14      noro     3133:        if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11      noro     3134:                nd_free(t1);
                   3135:                return 0;
                   3136:        }
1.55      noro     3137:        t2 = ndv_mul_nm(mod,m,p2);
1.31      noro     3138:        *rp = nd_add(mod,t1,t2);
1.11      noro     3139:        FREENM(m);
                   3140:        return 1;
                   3141: }
                   3142:
1.19      noro     3143: void ndv_mul_c(int mod,NDV p,int mul)
1.11      noro     3144: {
                   3145:        NMV m;
                   3146:        int c,c1,len,i;
                   3147:
1.34      noro     3148:        if ( !p ) return;
1.14      noro     3149:        len = LEN(p);
1.71      noro     3150:        if ( mod == -1 )
                   3151:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
                   3152:                        CM(m) = _mulsf(CM(m),mul);
                   3153:        else
                   3154:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3155:                        c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
                   3156:                }
1.11      noro     3157: }
                   3158:
1.16      noro     3159: void ndv_mul_c_q(NDV p,Q mul)
                   3160: {
                   3161:        NMV m;
                   3162:        Q c;
                   3163:        int len,i;
                   3164:
1.34      noro     3165:        if ( !p ) return;
1.16      noro     3166:        len = LEN(p);
                   3167:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3168:                mulq(CQ(m),mul,&c); CQ(m) = c;
                   3169:        }
                   3170: }
                   3171:
1.55      noro     3172: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
                   3173:        int n2,i,j,l,n,tlen;
1.61      noro     3174:        UINT *d0;
1.55      noro     3175:        NM *tab,*psum;
                   3176:        ND s,r;
                   3177:        NM t;
                   3178:        NMV m1;
                   3179:
                   3180:        if ( !p ) return 0;
                   3181:        n = NV(p); n2 = n>>1;
                   3182:        d0 = DL(m0);
                   3183:        l = LEN(p);
                   3184:        for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
                   3185:        tab = (NM *)ALLOCA(tlen*sizeof(NM));
                   3186:        psum = (NM *)ALLOCA(tlen*sizeof(NM));
                   3187:        for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56      noro     3188:        m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
                   3189:        for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55      noro     3190:                /* m0(NM) * m1(NMV) => tab(NM) */
1.56      noro     3191:                weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55      noro     3192:                for ( j = 0; j < tlen; j++ ) {
                   3193:                        if ( tab[j] ) {
                   3194:                                NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
                   3195:                        }
                   3196:                }
                   3197:        }
                   3198:        for ( i = tlen-1, r = 0; i >= 0; i-- )
                   3199:                if ( psum[i] ) {
                   3200:                        for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
                   3201:                        MKND(n,psum[i],j,s);
                   3202:                        r = nd_add(mod,r,s);
                   3203:                }
1.56      noro     3204:        if ( r ) SG(r) = SG(p)+TD(d0);
                   3205:        return r;
1.55      noro     3206: }
                   3207:
1.56      noro     3208: /* product of monomials */
                   3209: /* XXX block order is not handled correctly */
                   3210:
1.55      noro     3211: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
                   3212: {
1.56      noro     3213:        int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61      noro     3214:        UINT *d0,*d1,*d,*dt,*ctab;
1.56      noro     3215:        Q *ctab_q;
                   3216:        Q q,q1;
1.61      noro     3217:        UINT c0,c1,c;
1.55      noro     3218:        NM *p;
                   3219:        NM m,t;
                   3220:
                   3221:        for ( i = 0; i < tlen; i++ ) tab[i] = 0;
                   3222:        if ( !m0 || !m1 ) return;
                   3223:        d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
                   3224:        NEWNM(m); d = DL(m);
1.56      noro     3225:        if ( mod ) {
                   3226:                c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
                   3227:        } else
                   3228:                mulq(CQ(m0),CQ(m1),&CQ(m));
1.55      noro     3229:        for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
                   3230:        homo = n&1 ? 1 : 0;
                   3231:        if ( homo ) {
                   3232:                /* offset of h-degree */
                   3233:                h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
                   3234:                PUT_EXP(DL(m),n-1,h);
                   3235:                TD(DL(m)) = h;
1.61      noro     3236:                if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55      noro     3237:        }
                   3238:        tab[0] = m;
                   3239:        NEWNM(m); d = DL(m);
1.57      noro     3240:        for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55      noro     3241:                a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
                   3242:                k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
                   3243:                /* xi^a*(Di^k*xi^l)*Di^b */
                   3244:                a += l; b += k;
1.56      noro     3245:                s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55      noro     3246:                if ( !k || !l ) {
                   3247:                        for ( j = 0; j < curlen; j++ )
1.56      noro     3248:                                if ( t = tab[j] ) {
                   3249:                                        dt = DL(t);
                   3250:                                        PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61      noro     3251:                                        if ( nd_blockmask ) ndl_weight_mask(dt);
1.55      noro     3252:                                }
                   3253:                        curlen *= k+1;
                   3254:                        continue;
                   3255:                }
                   3256:                min = MIN(k,l);
1.56      noro     3257:                if ( mod ) {
1.61      noro     3258:                        ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56      noro     3259:                        mkwcm(k,l,mod,ctab);
                   3260:                } else {
                   3261:                        ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
                   3262:                        mkwc(k,l,ctab_q);
                   3263:                }
1.57      noro     3264:                for ( j = min; j >= 0; j-- ) {
1.56      noro     3265:                        for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55      noro     3266:                        PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56      noro     3267:                        h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55      noro     3268:                        if ( homo ) {
                   3269:                                TD(d) = s;
1.56      noro     3270:                                PUT_EXP(d,n-1,s-h);
1.55      noro     3271:                        } else TD(d) = h;
1.61      noro     3272:                        if ( nd_blockmask ) ndl_weight_mask(d);
1.56      noro     3273:                        if ( mod ) c = ctab[j];
                   3274:                        else q = ctab_q[j];
1.57      noro     3275:                        p = tab+curlen*j;
                   3276:                        if ( j == 0 ) {
                   3277:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3278:                                        if ( tab[u] ) {
                   3279:                                                ndl_addto(DL(tab[u]),d);
                   3280:                                                if ( mod ) {
                   3281:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
                   3282:                                                } else {
                   3283:                                                        mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
                   3284:                                                }
                   3285:                                        }
1.56      noro     3286:                                }
1.57      noro     3287:                        } else {
                   3288:                                for ( u = 0; u < curlen; u++, p++ ) {
                   3289:                                        if ( tab[u] ) {
                   3290:                                                NEWNM(t);
                   3291:                                                ndl_add(DL(tab[u]),d,DL(t));
                   3292:                                                if ( mod ) {
                   3293:                                                        c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
                   3294:                                                } else
                   3295:                                                        mulq(CQ(tab[u]),q,&CQ(t));
                   3296:                                                *p = t;
                   3297:                                        }
1.55      noro     3298:                                }
                   3299:                        }
                   3300:                }
                   3301:                curlen *= k+1;
                   3302:        }
                   3303:        FREENM(m);
                   3304: }
                   3305:
1.63      noro     3306: ND ndv_mul_nm_symbolic(NM m0,NDV p)
                   3307: {
                   3308:        NM mr,mr0;
                   3309:        NMV m;
                   3310:        UINT *d,*dt,*dm;
                   3311:        int c,n,td,i,c1,c2,len;
                   3312:        Q q;
                   3313:        ND r;
                   3314:
                   3315:        if ( !p ) return 0;
                   3316:        else {
                   3317:                n = NV(p); m = BDY(p);
                   3318:                d = DL(m0);
                   3319:                len = LEN(p);
                   3320:                mr0 = 0;
                   3321:                td = TD(d);
                   3322:                c = CM(m0);
                   3323:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3324:                        NEXTNM(mr0,mr);
                   3325:                        CM(mr) = 1;
                   3326:                        ndl_add(DL(m),d,DL(mr));
                   3327:                }
                   3328:                NEXT(mr) = 0;
                   3329:                MKND(NV(p),mr0,len,r);
                   3330:                SG(r) = SG(p) + TD(d);
                   3331:                return r;
                   3332:        }
                   3333: }
                   3334:
1.55      noro     3335: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9       noro     3336: {
                   3337:        NM mr,mr0;
                   3338:        NMV m;
1.61      noro     3339:        UINT *d,*dt,*dm;
1.9       noro     3340:        int c,n,td,i,c1,c2,len;
1.16      noro     3341:        Q q;
1.9       noro     3342:        ND r;
                   3343:
1.34      noro     3344:        if ( !p ) return 0;
1.55      noro     3345:        else if ( do_weyl )
1.71      noro     3346:                if ( mod == -1 )
                   3347:                        error("ndv_mul_nm : not implemented (weyl)");
                   3348:                else
                   3349:                        return weyl_ndv_mul_nm(mod,m0,p);
1.9       noro     3350:        else {
                   3351:                n = NV(p); m = BDY(p);
1.34      noro     3352:                d = DL(m0);
1.14      noro     3353:                len = LEN(p);
1.9       noro     3354:                mr0 = 0;
1.34      noro     3355:                td = TD(d);
1.71      noro     3356:                if ( mod == -1 ) {
                   3357:                        c = CM(m0);
                   3358:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3359:                                NEXTNM(mr0,mr);
                   3360:                                CM(mr) = _mulsf(CM(m),c);
                   3361:                                ndl_add(DL(m),d,DL(mr));
                   3362:                        }
                   3363:                } else if ( mod ) {
1.16      noro     3364:                        c = CM(m0);
                   3365:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3366:                                NEXTNM(mr0,mr);
                   3367:                                c1 = CM(m);
1.19      noro     3368:                                DMAR(c1,c,0,mod,c2);
1.16      noro     3369:                                CM(mr) = c2;
                   3370:                                ndl_add(DL(m),d,DL(mr));
                   3371:                        }
                   3372:                } else {
                   3373:                        q = CQ(m0);
                   3374:                        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   3375:                                NEXTNM(mr0,mr);
                   3376:                                mulq(CQ(m),q,&CQ(mr));
                   3377:                                ndl_add(DL(m),d,DL(mr));
                   3378:                        }
1.4       noro     3379:                }
1.9       noro     3380:                NEXT(mr) = 0;
1.31      noro     3381:                MKND(NV(p),mr0,len,r);
1.34      noro     3382:                SG(r) = SG(p) + TD(d);
1.9       noro     3383:                return r;
1.4       noro     3384:        }
                   3385: }
                   3386:
1.43      noro     3387: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11      noro     3388: {
1.13      noro     3389:        NMV m,mr,mr0,t;
                   3390:        int len,i,k;
1.11      noro     3391:
1.61      noro     3392:        if ( !p ) return;
                   3393:        m = BDY(p); len = LEN(p);
                   3394:        mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
                   3395:        m = (NMV)((char *)mr0+(len-1)*oadv);
                   3396:        mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
                   3397:        t = (NMV)ALLOCA(nmv_adv);
                   3398:        for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
                   3399:                CQ(t) = CQ(m);
                   3400:                for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
                   3401:                ndl_reconstruct(DL(m),DL(t),obpe,oepos);
                   3402:                CQ(mr) = CQ(t);
                   3403:                ndl_copy(DL(t),DL(mr));
                   3404:        }
                   3405:        BDY(p) = mr0;
                   3406: }
                   3407:
                   3408: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
                   3409: {
                   3410:        NMV m,mr,mr0;
                   3411:        int len,i;
                   3412:        NDV r;
1.11      noro     3413:
1.61      noro     3414:        if ( !p ) return 0;
                   3415:        m = BDY(p); len = LEN(p);
                   3416:        mr0 = mr = (NMV)MALLOC(len*nmv_adv);
                   3417:        for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
                   3418:                ndl_zero(DL(mr));
                   3419:                ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
                   3420:                CQ(mr) = CQ(m);
1.11      noro     3421:        }
1.61      noro     3422:        MKNDV(NV(p),mr0,len,r);
                   3423:        SG(r) = SG(p);
                   3424:        return r;
1.11      noro     3425: }
                   3426:
1.61      noro     3427: /* duplicate p */
                   3428:
                   3429: NDV ndv_dup(int mod,NDV p)
1.3       noro     3430: {
                   3431:        NDV d;
1.61      noro     3432:        NMV t,m,m0;
1.3       noro     3433:        int i,len;
                   3434:
1.34      noro     3435:        if ( !p ) return 0;
1.31      noro     3436:        len = LEN(p);
1.34      noro     3437:        m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61      noro     3438:        for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14      noro     3439:                ndl_copy(DL(t),DL(m));
1.16      noro     3440:                CQ(m) = CQ(t);
1.3       noro     3441:        }
                   3442:        MKNDV(NV(p),m0,len,d);
1.23      noro     3443:        SG(d) = SG(p);
                   3444:        return d;
                   3445: }
                   3446:
1.63      noro     3447: ND nd_dup(ND p)
                   3448: {
                   3449:        ND d;
                   3450:        NM t,m,m0;
                   3451:
                   3452:        if ( !p ) return 0;
                   3453:        for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
                   3454:                NEXTNM(m0,m);
                   3455:                ndl_copy(DL(t),DL(m));
                   3456:                CQ(m) = CQ(t);
                   3457:        }
                   3458:        if ( m0 ) NEXT(m) = 0;
                   3459:        MKND(NV(p),m0,LEN(p),d);
                   3460:        SG(d) = SG(p);
                   3461:        return d;
                   3462: }
                   3463:
1.61      noro     3464: /* XXX if p->len == 0 then it represents 0 */
                   3465:
                   3466: void ndv_mod(int mod,NDV p)
                   3467: {
                   3468:        NMV t,d;
                   3469:        int r;
                   3470:        int i,len,dlen;
1.71      noro     3471:        Obj gfs;
1.61      noro     3472:
                   3473:        if ( !p ) return;
                   3474:        len = LEN(p);
                   3475:        dlen = 0;
1.71      noro     3476:        if ( mod == -1 )
                   3477:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3478:                        simp_ff((Obj)CP(t),&gfs);
                   3479:                        r = FTOIF(CONT((GFS)gfs));
1.61      noro     3480:                        CM(d) = r;
                   3481:                        ndl_copy(DL(t),DL(d));
                   3482:                        NMV_ADV(d);
                   3483:                        dlen++;
                   3484:                }
1.71      noro     3485:        else
                   3486:                for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
                   3487:                        r = rem(NM(CQ(t)),mod);
                   3488:                        if ( r ) {
                   3489:                                if ( SGN(CQ(t)) < 0 )
                   3490:                                        r = mod-r;
                   3491:                                CM(d) = r;
                   3492:                                ndl_copy(DL(t),DL(d));
                   3493:                                NMV_ADV(d);
                   3494:                                dlen++;
                   3495:                        }
                   3496:                }
1.61      noro     3497:        LEN(p) = dlen;
                   3498: }
                   3499:
                   3500: NDV ptondv(VL vl,VL dvl,P p)
                   3501: {
                   3502:        ND nd;
                   3503:
                   3504:        nd = ptond(vl,dvl,p);
                   3505:        return ndtondv(0,nd);
                   3506: }
                   3507:
                   3508: ND ptond(VL vl,VL dvl,P p)
1.23      noro     3509: {
1.61      noro     3510:        int n,i,j,k,e;
                   3511:        VL tvl;
                   3512:        V v;
                   3513:        DCP dc;
                   3514:        DCP *w;
                   3515:        ND r,s,t,u;
                   3516:        P x;
                   3517:        int c;
                   3518:        UINT *d;
1.23      noro     3519:        NM m,m0;
1.61      noro     3520:
                   3521:        if ( !p )
                   3522:                return 0;
                   3523:        else if ( NUM(p) ) {
                   3524:                NEWNM(m);
                   3525:                ndl_zero(DL(m));
                   3526:                CQ(m) = (Q)p;
                   3527:                NEXT(m) = 0;
                   3528:                MKND(nd_nvar,m,1,r);
                   3529:                SG(r) = 0;
                   3530:                return r;
                   3531:        } else {
                   3532:                for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
                   3533:                w = (DCP *)ALLOCA(k*sizeof(DCP));
                   3534:                for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
                   3535:                for ( i = 0, tvl = dvl, v = VR(p);
                   3536:                        vl && tvl->v != v; tvl = NEXT(tvl), i++ );
                   3537:                if ( !tvl ) {
                   3538:                        for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
                   3539:                                t = ptond(vl,dvl,COEF(w[j]));
                   3540:                                pwrp(vl,x,DEG(w[j]),&p);
                   3541:                                nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
                   3542:                        }
                   3543:                        return s;
                   3544:                } else {
                   3545:                        NEWNM(m0); d = DL(m0);
                   3546:                        for ( j = k-1, s = 0; j >= 0; j-- ) {
                   3547:                                ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
                   3548:                                TD(d) = MUL_WEIGHT(e,i);
                   3549:                                if ( nd_blockmask) ndl_weight_mask(d);
                   3550:                                t = ptond(vl,dvl,COEF(w[j]));
                   3551:                                for ( m = BDY(t); m; m = NEXT(m) )
                   3552:                                        ndl_addto(DL(m),d);
                   3553:                                SG(t) += TD(d);
                   3554:                                s = nd_add(0,s,t);
                   3555:                        }
                   3556:                        FREENM(m0);
                   3557:                        return s;
                   3558:                }
                   3559:        }
                   3560: }
                   3561:
                   3562: P ndvtop(int mod,VL vl,VL dvl,NDV p)
                   3563: {
                   3564:        VL tvl;
                   3565:        int len,n,j,i,e;
                   3566:        NMV m;
                   3567:        Q q;
                   3568:        P c;
                   3569:        UINT *d;
                   3570:        P s,r,u,t,w;
1.71      noro     3571:        GFS gfs;
1.23      noro     3572:
1.34      noro     3573:        if ( !p ) return 0;
1.61      noro     3574:        else {
                   3575:                len = LEN(p);
                   3576:                n = NV(p);
                   3577:                m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
                   3578:                for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71      noro     3579:                        if ( mod == -1 ) {
                   3580:                                e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
                   3581:                        } else if ( mod ) {
1.61      noro     3582:                                STOQ(CM(m),q); c = (P)q;
                   3583:                        } else
                   3584:                                c = CP(m);
                   3585:                        d = DL(m);
                   3586:                        for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
                   3587:                                MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
                   3588:                                pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
                   3589:                        }
                   3590:                        addp(vl,s,t,&u); s = u;
                   3591:                }
                   3592:                return s;
1.23      noro     3593:        }
1.3       noro     3594: }
                   3595:
1.61      noro     3596: NDV ndtondv(int mod,ND p)
1.11      noro     3597: {
                   3598:        NDV d;
1.61      noro     3599:        NMV m,m0;
                   3600:        NM t;
                   3601:        int i,len;
1.11      noro     3602:
1.34      noro     3603:        if ( !p ) return 0;
1.61      noro     3604:        len = LEN(p);
1.74      noro     3605:        if ( mod )
                   3606:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
                   3607:        else
                   3608:                m0 = m = MALLOC(len*nmv_adv);
                   3609:        ndv_alloc += nmv_adv*len;
1.61      noro     3610:        for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
                   3611:                ndl_copy(DL(t),DL(m));
                   3612:                CQ(m) = CQ(t);
1.11      noro     3613:        }
1.61      noro     3614:        MKNDV(NV(p),m0,len,d);
1.14      noro     3615:        SG(d) = SG(p);
1.11      noro     3616:        return d;
                   3617: }
                   3618:
1.61      noro     3619: ND ndvtond(int mod,NDV p)
1.11      noro     3620: {
1.61      noro     3621:        ND d;
                   3622:        NM m,m0;
1.11      noro     3623:        NMV t;
1.61      noro     3624:        int i,len;
1.11      noro     3625:
1.34      noro     3626:        if ( !p ) return 0;
1.11      noro     3627:        m0 = 0;
1.61      noro     3628:        len = p->len;
                   3629:        for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
                   3630:                NEXTNM(m0,m);
                   3631:                ndl_copy(DL(t),DL(m));
                   3632:                CQ(m) = CQ(t);
1.11      noro     3633:        }
                   3634:        NEXT(m) = 0;
1.61      noro     3635:        MKND(NV(p),m0,len,d);
1.14      noro     3636:        SG(d) = SG(p);
1.11      noro     3637:        return d;
                   3638: }
                   3639:
1.3       noro     3640: void ndv_print(NDV p)
                   3641: {
                   3642:        NMV m;
                   3643:        int i,len;
                   3644:
1.34      noro     3645:        if ( !p ) printf("0\n");
1.3       noro     3646:        else {
1.14      noro     3647:                len = LEN(p);
1.3       noro     3648:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71      noro     3649:                        if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
                   3650:                        else printf("+%d*",CM(m));
1.16      noro     3651:                        ndl_print(DL(m));
                   3652:                }
                   3653:                printf("\n");
                   3654:        }
                   3655: }
                   3656:
                   3657: void ndv_print_q(NDV p)
                   3658: {
                   3659:        NMV m;
                   3660:        int i,len;
                   3661:
1.34      noro     3662:        if ( !p ) printf("0\n");
1.16      noro     3663:        else {
                   3664:                len = LEN(p);
                   3665:                for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   3666:                        printf("+");
1.74      noro     3667:                        printexpr(CO,(Obj)CQ(m));
1.16      noro     3668:                        printf("*");
1.14      noro     3669:                        ndl_print(DL(m));
1.3       noro     3670:                }
                   3671:                printf("\n");
                   3672:        }
1.25      noro     3673: }
                   3674:
1.61      noro     3675: NODE ndv_reducebase(NODE x)
1.27      noro     3676: {
                   3677:        int len,i,j;
                   3678:        NDV *w;
                   3679:        NODE t,t0;
                   3680:
                   3681:        len = length(x);
                   3682:        w = (NDV *)ALLOCA(len*sizeof(NDV));
                   3683:        for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
                   3684:        for ( i = 0; i < len; i++ ) {
                   3685:                for ( j = 0; j < i; j++ ) {
                   3686:                        if ( w[i] && w[j] )
                   3687:                                if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
                   3688:                                else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
                   3689:                }
                   3690:        }
                   3691:        for ( i = len-1, t0 = 0; i >= 0; i-- ) {
                   3692:                if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
                   3693:        }
                   3694:        NEXT(t) = 0; x = t0;
                   3695:        return x;
1.11      noro     3696: }
1.32      noro     3697:
1.43      noro     3698: /* XXX incomplete */
                   3699:
1.32      noro     3700: void nd_init_ord(struct order_spec *ord)
                   3701: {
1.43      noro     3702:        switch ( ord->id ) {
1.32      noro     3703:                case 0:
1.43      noro     3704:                        switch ( ord->ord.simple ) {
                   3705:                                case 0:
                   3706:                                        nd_dcomp = 1;
                   3707:                                        nd_isrlex = 1;
                   3708:                                        break;
                   3709:                                case 1:
                   3710:                                        nd_dcomp = 1;
                   3711:                                        nd_isrlex = 0;
                   3712:                                        break;
                   3713:                                case 2:
                   3714:                                        nd_dcomp = 0;
                   3715:                                        nd_isrlex = 0;
1.45      noro     3716:                                        ndl_compare_function = ndl_lex_compare;
1.58      noro     3717:                                        break;
                   3718:                                case 11:
                   3719:                                        /* XXX */
                   3720:                                        nd_dcomp = 0;
                   3721:                                        nd_isrlex = 1;
                   3722:                                        ndl_compare_function = ndl_ww_lex_compare;
1.43      noro     3723:                                        break;
                   3724:                                default:
                   3725:                                        error("nd_gr : unsupported order");
                   3726:                        }
1.32      noro     3727:                        break;
                   3728:                case 1:
1.43      noro     3729:                        /* XXX */
                   3730:                        nd_dcomp = -1;
1.32      noro     3731:                        nd_isrlex = 0;
1.45      noro     3732:                        ndl_compare_function = ndl_block_compare;
1.34      noro     3733:                        break;
1.43      noro     3734:                case 2:
                   3735:                        error("nd_init_ord : matrix order is not supported yet.");
1.32      noro     3736:                        break;
                   3737:        }
1.41      noro     3738:        nd_ord = ord;
1.32      noro     3739: }
                   3740:
1.43      noro     3741: BlockMask nd_create_blockmask(struct order_spec *ord)
                   3742: {
                   3743:        int n,i,j,s,l;
1.61      noro     3744:        UINT *t;
1.43      noro     3745:        BlockMask bm;
                   3746:
                   3747:        if ( !ord->id )
                   3748:                return 0;
                   3749:        n = ord->ord.block.length;
                   3750:        bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
                   3751:        bm->n = n;
                   3752:        bm->order_pair = ord->ord.block.order_pair;
1.61      noro     3753:        bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43      noro     3754:        for ( i = 0, s = 0; i < n; i++ ) {
1.61      noro     3755:                bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43      noro     3756:                for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
                   3757:                l = bm->order_pair[i].length;
                   3758:                for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
                   3759:        }
                   3760:        return bm;
1.57      noro     3761: }
                   3762:
                   3763: EPOS nd_create_epos(struct order_spec *ord)
                   3764: {
                   3765:        int i,j,l,s,ord_l,ord_o;
                   3766:        EPOS epos;
                   3767:        struct order_pair *op;
                   3768:
                   3769:        epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
                   3770:        switch ( ord->id ) {
                   3771:                case 0:
                   3772:                        if ( nd_isrlex ) {
                   3773:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3774:                                        epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
                   3775:                                        epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
                   3776:                                }
                   3777:                        } else {
                   3778:                                for ( i = 0; i < nd_nvar; i++ ) {
                   3779:                                        epos[i].i = nd_exporigin + i/nd_epw;
                   3780:                                        epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
                   3781:                                }
                   3782:                        }
                   3783:                        break;
                   3784:                case 1:
                   3785:                        /* block order */
                   3786:                        l = ord->ord.block.length;
                   3787:                        op = ord->ord.block.order_pair;
                   3788:                        for ( j = 0, s = 0; j < l; j++ ) {
                   3789:                                ord_o = op[j].order;
                   3790:                                ord_l = op[j].length;
                   3791:                                if ( !ord_o )
                   3792:                                        for ( i = 0; i < ord_l; i++ ) {
                   3793:                                                epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
                   3794:                                                epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
                   3795:                                        }
                   3796:                                else
                   3797:                                        for ( i = 0; i < ord_l; i++ ) {
                   3798:                                                epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
                   3799:                                                epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
                   3800:                                        }
                   3801:                                s += ord_l;
                   3802:                        }
                   3803:                        break;
                   3804:                case 2:
                   3805:                        error("nd_create_epos : matrix order is not supported yet.");
                   3806:        }
                   3807:        return epos;
1.43      noro     3808: }
1.59      noro     3809:
                   3810: /* external interface */
                   3811:
                   3812: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
                   3813: {
1.61      noro     3814:        NODE t,in0,in;
1.59      noro     3815:        ND nd,nf;
1.61      noro     3816:        NDV ndv;
                   3817:        VL vv,tv;
                   3818:        int stat,nvar,max,e;
1.69      noro     3819:        union oNDC dn;
1.59      noro     3820:
                   3821:        pltovl(v,&vv);
1.74      noro     3822:        for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61      noro     3823:
                   3824:        /* get the degree bound */
                   3825:        for ( t = BDY(g), max = 0; t; t = NEXT(t) )
                   3826:                for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3827:                        e = getdeg(tv->v,(P)BDY(t));
                   3828:                        max = MAX(e,max);
                   3829:                }
                   3830:        for ( tv = vv; tv; tv = NEXT(tv) ) {
                   3831:                e = getdeg(tv->v,f);
                   3832:                max = MAX(e,max);
                   3833:        }
                   3834:
1.59      noro     3835:        nd_init_ord(ord);
1.61      noro     3836:        nd_setup_parameters(nvar,max);
                   3837:
                   3838:        /* conversion to ndv */
                   3839:        for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
                   3840:                NEXTNODE(in0,in);
                   3841:                BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
1.69      noro     3842:                if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3843:        }
                   3844:        NEXTNODE(in0,in);
                   3845:        BDY(in) = (pointer)ptondv(CO,vv,f);
1.69      noro     3846:        if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61      noro     3847:        NEXT(in) = 0;
                   3848:
                   3849:        ndv_setup(m,0,in0);
1.59      noro     3850:        nd_psn--;
                   3851:        nd_scale=2;
                   3852:        while ( 1 ) {
                   3853:                nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.69      noro     3854:                stat = nd_nf(m,nd,nd_ps,1,0,&nf);
1.59      noro     3855:                if ( !stat ) {
                   3856:                        nd_psn++;
                   3857:                        nd_reconstruct(m,0,0);
                   3858:                        nd_psn--;
                   3859:                } else
                   3860:                        break;
                   3861:        }
1.61      noro     3862:        *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63      noro     3863: }
                   3864:
                   3865: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
                   3866: {
                   3867:        NM m;
                   3868:        UINT *t,*s;
                   3869:        int i;
                   3870:
                   3871:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3872:        for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
                   3873:                t = DL(m);
                   3874:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3875:                r[i] = CM(m);
                   3876:        }
                   3877:        for ( i = 0; !r[i]; i++ );
                   3878:        return i;
                   3879: }
                   3880:
1.74      noro     3881: int ndv_to_vect(int mod,UINT *s0,int n,NDV d,UINT *r)
                   3882: {
                   3883:        NMV m;
                   3884:        UINT *t,*s;
                   3885:        int i,j,len;
                   3886:
                   3887:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3888:        m = BDY(d);
                   3889:        len = LEN(d);
                   3890:        for ( i = j = 0, s = s0; j < len; j++, NMV_ADV(m)) {
                   3891:                t = DL(m);
                   3892:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3893:                r[i] = CM(m);
                   3894:        }
                   3895:        for ( i = 0; !r[i]; i++ );
                   3896:        return i;
                   3897: }
                   3898:
1.63      noro     3899: int nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair,UINT *r)
                   3900: {
                   3901:        NM m;
                   3902:        NMV mr;
                   3903:        UINT *d,*t,*s;
                   3904:        NDV p;
                   3905:        int i,j,len;
                   3906:
                   3907:        m = pair->mul;
                   3908:        d = DL(m);
                   3909:        p = nd_ps[pair->index];
                   3910:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
                   3911:        for ( i = 0; i < n; i++ ) r[i] = 0;
                   3912:        len = LEN(p);
                   3913:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   3914:                ndl_add(d,DL(mr),t);
                   3915:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
                   3916:                r[i] = CM(mr);
                   3917:        }
                   3918:        for ( i = 0; !r[i]; i++ );
                   3919:        return i;
                   3920: }
                   3921:
1.67      noro     3922: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64      noro     3923: {
                   3924:        NM m;
                   3925:        NMV mr;
                   3926:        UINT *d,*t,*s;
                   3927:        NDV p;
1.67      noro     3928:        unsigned char *ivc;
                   3929:        unsigned short *ivs;
1.81      noro     3930:        UINT *v,*ivi,*s0v;
1.67      noro     3931:        int i,j,len,prev,diff,cdiff;
                   3932:        IndArray r;
1.64      noro     3933:
                   3934:        m = pair->mul;
                   3935:        d = DL(m);
                   3936:        p = nd_ps[pair->index];
                   3937:        len = LEN(p);
                   3938:        t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67      noro     3939:        v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64      noro     3940:        for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
                   3941:                ndl_add(d,DL(mr),t);
                   3942:                for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67      noro     3943:                v[j] = i;
                   3944:        }
1.81      noro     3945:        r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67      noro     3946:        r->head = v[0];
                   3947:        diff = 0;
                   3948:        for ( i = 1; i < len; i++ ) {
                   3949:                cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
                   3950:        }
                   3951:        if ( diff < 256 ) {
                   3952:                r->width = 1;
                   3953:                ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
                   3954:                r->index.c = ivc;
                   3955:                for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
                   3956:        } else if ( diff < 65536 ) {
                   3957:                r->width = 2;
                   3958:                ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
                   3959:                r->index.s = ivs;
                   3960:                for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
                   3961:        } else {
                   3962:                r->width = 4;
                   3963:                ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
                   3964:                r->index.i = ivi;
                   3965:                for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64      noro     3966:        }
1.67      noro     3967:        return r;
1.64      noro     3968: }
                   3969:
1.65      noro     3970:
1.76      noro     3971: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65      noro     3972: {
1.67      noro     3973:        int i,j,k,len,pos,prev;
1.66      noro     3974:        UINT c,c1,c2,c3,up,lo,dmy;
1.67      noro     3975:        IndArray ivect;
                   3976:        unsigned char *ivc;
                   3977:        unsigned short *ivs;
                   3978:        unsigned int *ivi;
1.65      noro     3979:        NDV redv;
1.67      noro     3980:        NMV mr;
1.65      noro     3981:        NODE rp;
1.76      noro     3982:        int maxrs;
1.65      noro     3983:
1.76      noro     3984:        maxrs = 0;
1.74      noro     3985:        for ( i = 0; i < nred; i++ ) {
1.65      noro     3986:                ivect = imat[i];
1.67      noro     3987:                k = ivect->head; svect[k] %= m;
1.65      noro     3988:                if ( c = svect[k] ) {
1.76      noro     3989:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     3990:                        c = m-c; redv = nd_ps[rp0[i]->index];
1.67      noro     3991:                        len = LEN(redv); mr = BDY(redv);
                   3992:                        svect[k] = 0; prev = k;
                   3993:                        switch ( ivect->width ) {
                   3994:                                case 1:
                   3995:                                        ivc = ivect->index.c;
                   3996:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   3997:                                                pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
                   3998:                                                prev = pos;
                   3999:                                                DMA(c1,c,c2,up,lo);
                   4000:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4001:                                                } else svect[pos] = lo;
                   4002:                                        }
                   4003:                                        break;
                   4004:                                case 2:
                   4005:                                        ivs = ivect->index.s;
                   4006:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4007:                                                pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
                   4008:                                                prev = pos;
                   4009:                                                DMA(c1,c,c2,up,lo);
                   4010:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4011:                                                } else svect[pos] = lo;
                   4012:                                        }
                   4013:                                        break;
                   4014:                                case 4:
                   4015:                                        ivi = ivect->index.i;
                   4016:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4017:                                                pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
                   4018:                                                prev = pos;
                   4019:                                                DMA(c1,c,c2,up,lo);
                   4020:                                                if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
                   4021:                                                } else svect[pos] = lo;
                   4022:                                        }
                   4023:                                        break;
1.65      noro     4024:                        }
                   4025:                }
                   4026:        }
1.66      noro     4027:        for ( i = 0; i < col; i++ )
                   4028:                if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76      noro     4029:        return maxrs;
1.65      noro     4030: }
                   4031:
1.76      noro     4032: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72      noro     4033: {
                   4034:        int i,j,k,len,pos,prev;
                   4035:        UINT c,c1,c2,c3,up,lo,dmy;
                   4036:        IndArray ivect;
                   4037:        unsigned char *ivc;
                   4038:        unsigned short *ivs;
                   4039:        unsigned int *ivi;
                   4040:        NDV redv;
                   4041:        NMV mr;
                   4042:        NODE rp;
1.76      noro     4043:        int maxrs;
1.72      noro     4044:
1.76      noro     4045:        maxrs = 0;
1.74      noro     4046:        for ( i = 0; i < nred; i++ ) {
1.72      noro     4047:                ivect = imat[i];
                   4048:                k = ivect->head; svect[k] %= m;
                   4049:                if ( c = svect[k] ) {
1.76      noro     4050:                        maxrs = MAX(maxrs,rp0[i]->sugar);
1.74      noro     4051:                        c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72      noro     4052:                        len = LEN(redv); mr = BDY(redv);
                   4053:                        svect[k] = 0; prev = k;
                   4054:                        switch ( ivect->width ) {
                   4055:                                case 1:
                   4056:                                        ivc = ivect->index.c;
                   4057:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4058:                                                pos = prev+ivc[j]; prev = pos;
                   4059:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4060:                                        }
                   4061:                                        break;
                   4062:                                case 2:
                   4063:                                        ivs = ivect->index.s;
                   4064:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4065:                                                pos = prev+ivs[j]; prev = pos;
                   4066:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4067:                                        }
                   4068:                                        break;
                   4069:                                case 4:
                   4070:                                        ivi = ivect->index.i;
                   4071:                                        for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
                   4072:                                                pos = prev+ivi[j]; prev = pos;
                   4073:                                                svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
                   4074:                                        }
                   4075:                                        break;
                   4076:                        }
                   4077:                }
                   4078:        }
1.76      noro     4079:        return maxrs;
1.72      noro     4080: }
                   4081:
1.65      noro     4082: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
                   4083: {
                   4084:        int j,k,len;
                   4085:        UINT *p;
                   4086:        UINT c;
                   4087:        NDV r;
                   4088:        NMV mr0,mr;
                   4089:
                   4090:        for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
                   4091:        if ( !len ) return 0;
                   4092:        else {
1.74      noro     4093:                mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
                   4094:                ndv_alloc += nmv_adv*len;
1.65      noro     4095:                mr = mr0;
                   4096:                p = s0vect;
                   4097:                for ( j = k = 0; j < col; j++, p += nd_wpd )
                   4098:                        if ( !rhead[j] ) {
                   4099:                                if ( c = vect[k++] ) {
                   4100:                                        ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
                   4101:                                }
                   4102:                        }
                   4103:                MKNDV(nd_nvar,mr0,len,r);
                   4104:                return r;
                   4105:        }
                   4106: }
                   4107:
1.74      noro     4108: int nd_sp_f4(int m,ND_pairs l,PGeoBucket bucket)
1.65      noro     4109: {
                   4110:        ND_pairs t;
                   4111:        NODE sp0,sp;
                   4112:        int stat;
                   4113:        ND spol;
                   4114:
                   4115:        for ( t = l; t; t = NEXT(t) ) {
                   4116:                stat = nd_sp(m,0,t,&spol);
                   4117:                if ( !stat ) return 0;
                   4118:                if ( spol ) {
                   4119:                        add_pbucket_symbolic(bucket,spol);
                   4120:                }
                   4121:        }
1.68      noro     4122:        return 1;
1.65      noro     4123: }
                   4124:
                   4125: int nd_symbolic_preproc(PGeoBucket bucket,UINT **s0vect,NODE *r)
                   4126: {
                   4127:        NODE rp0,rp;
                   4128:        NM mul,head,s0,s;
1.76      noro     4129:        int index,col,i,sugar;
1.65      noro     4130:        RHist h;
                   4131:        UINT *s0v,*p;
                   4132:        NM_ind_pair pair;
                   4133:        ND red;
                   4134:
                   4135:        s0 = 0; rp0 = 0; col = 0;
                   4136:        while ( 1 ) {
                   4137:                head = remove_head_pbucket_symbolic(bucket);
                   4138:                if ( !head ) break;
                   4139:                if ( !s0 ) s0 = head;
                   4140:                else NEXT(s) = head;
                   4141:                s = head;
                   4142:                index = ndl_find_reducer(DL(head));
                   4143:                if ( index >= 0 ) {
                   4144:                        h = nd_psh[index];
                   4145:                        NEWNM(mul);
                   4146:                        ndl_sub(DL(head),DL(h),DL(mul));
                   4147:                        if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.76      noro     4148:                        sugar = TD(DL(mul))+SG(nd_ps[index]);
                   4149:                        MKNM_ind_pair(pair,mul,index,sugar);
1.65      noro     4150:                        red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
                   4151:                        add_pbucket_symbolic(bucket,nd_remove_head(red));
                   4152:                        NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
                   4153:                }
                   4154:                col++;
                   4155:        }
1.72      noro     4156:        if ( rp0 ) NEXT(rp) = 0;
                   4157:        NEXT(s) = 0;
1.65      noro     4158:        s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
                   4159:        for ( i = 0, p = s0v, s = s0; i < col;
                   4160:                i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
                   4161:        *s0vect = s0v;
                   4162:        *r = rp0;
                   4163:        return col;
                   4164: }
                   4165:
1.69      noro     4166: NODE nd_f4(int m)
                   4167: {
                   4168:        int i,nh,stat,index;
                   4169:        NODE r,g;
                   4170:        ND_pairs d,l,t;
                   4171:        ND spol,red;
                   4172:        NDV nf,redv;
                   4173:        NM s0,s;
1.84      noro     4174:        NODE rp0,srp0,nflist;
1.69      noro     4175:        int nsp,nred,col,rank,len,k,j,a;
                   4176:        UINT c;
1.74      noro     4177:        UINT **spmat;
1.69      noro     4178:        UINT *s0vect,*svect,*p,*v;
                   4179:        int *colstat;
                   4180:        IndArray *imat;
                   4181:        int *rhead;
                   4182:        int spcol,sprow;
                   4183:        int sugar;
                   4184:        PGeoBucket bucket;
                   4185:        struct oEGT eg0,eg1,eg_f4;
                   4186:
                   4187:        if ( !m )
                   4188:                error("nd_f4 : not implemented");
1.74      noro     4189:        ndv_alloc = 0;
1.69      noro     4190:        g = 0; d = 0;
                   4191:        for ( i = 0; i < nd_psn; i++ ) {
                   4192:                d = update_pairs(d,g,i);
                   4193:                g = update_base(g,i);
                   4194:        }
                   4195:        while ( d ) {
                   4196:                get_eg(&eg0);
                   4197:                l = nd_minsugarp(d,&d);
                   4198:                sugar = SG(l);
                   4199:                bucket = create_pbucket();
1.74      noro     4200:                stat = nd_sp_f4(m,l,bucket);
1.69      noro     4201:                if ( !stat ) {
                   4202:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4203:                        NEXT(t) = d; d = l;
                   4204:                        d = nd_reconstruct(m,0,d);
                   4205:                        continue;
                   4206:                }
1.86      noro     4207:                if ( bucket->m < 0 ) continue;
1.69      noro     4208:                col = nd_symbolic_preproc(bucket,&s0vect,&rp0);
                   4209:                if ( !col ) {
                   4210:                        for ( t = l; NEXT(t); t = NEXT(t) );
                   4211:                        NEXT(t) = d; d = l;
                   4212:                        d = nd_reconstruct(m,0,d);
                   4213:                        continue;
                   4214:                }
                   4215:                get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74      noro     4216:                if ( DP_Print )
                   4217:                        fprintf(asir_out,"sugar=%d,symb=%fsec,",
                   4218:                                sugar,eg_f4.exectime+eg_f4.gctime);
                   4219:                if ( 1 )
                   4220:                        nflist = nd_f4_red(m,l,s0vect,col,rp0);
                   4221:                else
                   4222:                        nflist = nd_f4_red_dist(m,l,s0vect,col,rp0);
1.69      noro     4223:                /* adding new bases */
1.74      noro     4224:                for ( r = nflist; r; r = NEXT(r) ) {
                   4225:                        nf = (NDV)BDY(r);
1.69      noro     4226:                        ndv_removecont(m,nf);
1.77      noro     4227:                        nh = ndv_newps(m,nf,0);
1.69      noro     4228:                        d = update_pairs(d,g,nh);
                   4229:                        g = update_base(g,nh);
                   4230:                }
                   4231:        }
                   4232:        for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.74      noro     4233:        fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.69      noro     4234:        return g;
                   4235: }
1.74      noro     4236:
                   4237: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
1.63      noro     4238: {
1.67      noro     4239:        IndArray *imat;
1.74      noro     4240:        int nsp,nred,spcol,sprow,a;
1.65      noro     4241:        int *rhead;
1.74      noro     4242:        int i,j,k,l,rank;
                   4243:        NODE rp,r0,r;
                   4244:        ND_pairs sp;
                   4245:        ND spol;
                   4246:        int **spmat;
                   4247:        UINT *svect,*v;
                   4248:        int *colstat;
1.80      noro     4249:        struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
1.74      noro     4250:        NM_ind_pair *rvect;
1.76      noro     4251:        int maxrs;
                   4252:        int *spsugar;
1.63      noro     4253:
1.74      noro     4254:        get_eg(&eg0);
                   4255:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   4256:        nred = length(rp0); spcol = col-nred;
                   4257:        imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
                   4258:        rhead = (int *)ALLOCA(col*sizeof(int));
                   4259:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63      noro     4260:
1.74      noro     4261:        /* construction of index arrays */
                   4262:        rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
                   4263:        for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
                   4264:                rvect[i] = (NM_ind_pair)BDY(rp);
                   4265:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
                   4266:                rhead[imat[i]->head] = 1;
                   4267:        }
                   4268:
                   4269:        /* elimination (1st step) */
                   4270:        spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
                   4271:        svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76      noro     4272:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4273:        for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
                   4274:                nd_sp(m,0,sp,&spol);
1.75      noro     4275:                if ( !spol ) continue;
1.74      noro     4276:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4277:                if ( m == -1 )
                   4278:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
                   4279:                else
                   4280:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74      noro     4281:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4282:                if ( i < col ) {
                   4283:                        spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
                   4284:                        for ( j = k = 0; j < col; j++ )
                   4285:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4286:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4287:                        sprow++;
                   4288:                }
1.76      noro     4289:                nd_free(spol);
1.74      noro     4290:        }
1.80      noro     4291:        get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
                   4292:        if ( DP_Print ) {
                   4293:                fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
                   4294:                fflush(asir_out);
                   4295:        }
1.74      noro     4296:        /* free index arrays */
                   4297:        for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
                   4298:
                   4299:        /* elimination (2nd step) */
                   4300:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4301:        if ( m == -1 )
1.76      noro     4302:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4303:        else
1.76      noro     4304:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4305:        r0 = 0;
                   4306:        for ( i = 0; i < rank; i++ ) {
                   4307:                NEXTNODE(r0,r); BDY(r) =
                   4308:                        (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76      noro     4309:                SG((NDV)BDY(r)) = spsugar[i];
1.74      noro     4310:                GC_free(spmat[i]);
                   4311:        }
                   4312:        for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80      noro     4313:        get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
                   4314:        init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74      noro     4315:        if ( DP_Print ) {
1.80      noro     4316:                fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74      noro     4317:                fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d  ",
                   4318:                        nsp,nred,sprow,spcol,rank);
                   4319:                fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63      noro     4320:        }
1.74      noro     4321:        return r0;
                   4322: }
                   4323:
                   4324: FILE *nd_write,*nd_read;
                   4325:
                   4326: void nd_send_int(int a) {
                   4327:        write_int(nd_write,&a);
                   4328: }
                   4329:
                   4330: void nd_send_intarray(int *p,int len) {
                   4331:        write_intarray(nd_write,p,len);
                   4332: }
                   4333:
                   4334: int nd_recv_int() {
                   4335:        int a;
                   4336:
                   4337:        read_int(nd_read,&a);
                   4338:        return a;
                   4339: }
                   4340:
                   4341: void nd_recv_intarray(int *p,int len) {
                   4342:        read_intarray(nd_read,p,len);
                   4343: }
                   4344:
                   4345: void nd_send_ndv(NDV p) {
                   4346:        int len,i;
                   4347:        NMV m;
                   4348:
                   4349:        if ( !p ) nd_send_int(0);
                   4350:        else {
                   4351:                len = LEN(p);
                   4352:                nd_send_int(len);
                   4353:                m = BDY(p);
                   4354:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4355:                        nd_send_int(CM(m));
                   4356:                        nd_send_intarray(DL(m),nd_wpd);
1.68      noro     4357:                }
1.74      noro     4358:        }
                   4359: }
                   4360:
                   4361: void nd_send_nd(ND p) {
                   4362:        int len,i;
                   4363:        NM m;
                   4364:
                   4365:        if ( !p ) nd_send_int(0);
                   4366:        else {
                   4367:                len = LEN(p);
                   4368:                nd_send_int(len);
                   4369:                m = BDY(p);
                   4370:                for ( i = 0; i < len; i++, m = NEXT(m) ) {
                   4371:                        nd_send_int(CM(m));
                   4372:                        nd_send_intarray(DL(m),nd_wpd);
1.65      noro     4373:                }
1.74      noro     4374:        }
                   4375: }
1.65      noro     4376:
1.74      noro     4377: NDV nd_recv_ndv()
                   4378: {
                   4379:        int len,i;
                   4380:        NMV m,m0;
                   4381:        NDV r;
1.65      noro     4382:
1.74      noro     4383:        len = nd_recv_int();
                   4384:        if ( !len ) return 0;
                   4385:        else {
                   4386:                m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
                   4387:                ndv_alloc += len*nmv_adv;
                   4388:                for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4389:                        CM(m) = nd_recv_int();
                   4390:                        nd_recv_intarray(DL(m),nd_wpd);
1.71      noro     4391:                }
1.74      noro     4392:                MKNDV(nd_nvar,m0,len,r);
                   4393:                return r;
                   4394:        }
                   4395: }
1.65      noro     4396:
1.74      noro     4397: int ox_exec_f4_red(Q proc)
                   4398: {
                   4399:        Obj obj;
                   4400:        STRING fname;
                   4401:        NODE arg;
                   4402:        int s;
                   4403:        extern int ox_need_conv,ox_file_io;
                   4404:
                   4405:        MKSTR(fname,"nd_exec_f4_red");
                   4406:        arg = mknode(2,proc,fname);
                   4407:        Pox_cmo_rpc(arg,&obj);
                   4408:        s = get_ox_server_id(QTOS(proc));
                   4409:        nd_write = iofp[s].out;
                   4410:        nd_read = iofp[s].in;
                   4411:        ox_need_conv = ox_file_io = 0;
                   4412:        return s;
                   4413: }
                   4414:
                   4415: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
                   4416: {
                   4417:        int nsp,nred;
                   4418:        int i,rank,s;
                   4419:        NODE rp,r0,r;
                   4420:        ND_pairs sp;
                   4421:        NM_ind_pair pair;
                   4422:        NMV nmv;
                   4423:        NM nm;
                   4424:        NDV nf;
                   4425:        Obj proc,dmy;
                   4426:
                   4427:        ox_launch_main(0,0,&proc);
                   4428:        s = ox_exec_f4_red((Q)proc);
                   4429:
                   4430:        nd_send_int(m);
                   4431:        nd_send_int(nd_nvar);
                   4432:        nd_send_int(nd_bpe);
                   4433:        nd_send_int(nd_wpd);
                   4434:        nd_send_int(nmv_adv);
                   4435:
1.89      noro     4436:        saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74      noro     4437:
                   4438:        nd_send_int(nd_psn);
                   4439:        for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
                   4440:
                   4441:        for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
                   4442:        nd_send_int(nsp);
                   4443:        for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
                   4444:                nd_send_int(sp->i1); nd_send_int(sp->i2);
                   4445:        }
                   4446:
                   4447:        nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
                   4448:
                   4449:        nred = length(rp0); nd_send_int(nred);
                   4450:        for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
                   4451:                pair = (NM_ind_pair)BDY(rp);
                   4452:                nd_send_int(pair->index);
                   4453:                nd_send_intarray(pair->mul->dl,nd_wpd);
                   4454:        }
                   4455:        fflush(nd_write);
                   4456:        rank = nd_recv_int();
                   4457:        fprintf(asir_out,"rank=%d\n",rank);
                   4458:        r0 = 0;
                   4459:        for ( i = 0; i < rank; i++ ) {
                   4460:                nf = nd_recv_ndv();
                   4461:                NEXTNODE(r0,r); BDY(r) = (pointer)nf;
                   4462:        }
                   4463:        Pox_shutdown(mknode(1,proc),&dmy);
                   4464:        return r0;
                   4465: }
                   4466:
                   4467: /* server side */
                   4468:
                   4469: void nd_exec_f4_red_dist()
                   4470: {
                   4471:        int m,i,nsp,col,s0size,nred,spcol,j,k;
                   4472:        NM_ind_pair *rp0;
                   4473:        NDV nf;
                   4474:        UINT *s0vect;
                   4475:        IndArray *imat;
                   4476:        int *rhead;
                   4477:        int **spmat;
                   4478:        UINT *svect,*v;
                   4479:        ND_pairs *sp0;
                   4480:        int *colstat;
                   4481:        int a,sprow,rank;
1.89      noro     4482:        struct order_spec *ord;
1.74      noro     4483:        Obj ordspec;
                   4484:        ND spol;
1.76      noro     4485:        int maxrs;
                   4486:        int *spsugar;
1.74      noro     4487:
                   4488:        nd_read = iofp[0].in;
                   4489:        nd_write = iofp[0].out;
                   4490:        m = nd_recv_int();
                   4491:        nd_nvar = nd_recv_int();
                   4492:        nd_bpe = nd_recv_int();
                   4493:        nd_wpd = nd_recv_int();
                   4494:        nmv_adv = nd_recv_int();
                   4495:
                   4496:        loadobj(nd_read,&ordspec);
1.89      noro     4497:        create_order_spec(0,ordspec,&ord);
                   4498:        nd_init_ord(ord);
1.74      noro     4499:        nd_setup_parameters(nd_nvar,0);
                   4500:
                   4501:        nd_psn = nd_recv_int();
                   4502:        nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
                   4503:        nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
                   4504:        for ( i = 0; i < nd_psn; i++ ) {
                   4505:                nd_ps[i] = nd_recv_ndv();
                   4506:                nd_bound[i] = ndv_compute_bound(nd_ps[i]);
                   4507:        }
                   4508:
                   4509:        nsp = nd_recv_int();
                   4510:        sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
                   4511:        for ( i = 0; i < nsp; i++ ) {
                   4512:                NEWND_pairs(sp0[i]);
                   4513:                sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
                   4514:                ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
                   4515:        }
                   4516:
                   4517:        col = nd_recv_int();
                   4518:        s0size = col*nd_wpd;
                   4519:        s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
                   4520:        nd_recv_intarray(s0vect,s0size);
                   4521:
                   4522:        nred = nd_recv_int();
                   4523:        rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
                   4524:        for ( i = 0; i < nred; i++ ) {
                   4525:                rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
                   4526:                rp0[i]->index = nd_recv_int();
                   4527:                rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
                   4528:                nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
                   4529:        }
                   4530:
                   4531:        spcol = col-nred;
                   4532:        imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
                   4533:        rhead = (int *)MALLOC(col*sizeof(int));
                   4534:        for ( i = 0; i < col; i++ ) rhead[i] = 0;
                   4535:
                   4536:        /* construction of index arrays */
                   4537:        for ( i = 0; i < nred; i++ ) {
                   4538:                imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
                   4539:                rhead[imat[i]->head] = 1;
                   4540:        }
                   4541:
                   4542:        /* elimination (1st step) */
                   4543:        spmat = (int **)MALLOC(nsp*sizeof(UINT *));
                   4544:        svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76      noro     4545:        spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74      noro     4546:        for ( a = sprow = 0; a < nsp; a++ ) {
                   4547:                nd_sp(m,0,sp0[a],&spol);
1.75      noro     4548:                if ( !spol ) continue;
1.74      noro     4549:                nd_to_vect(m,s0vect,col,spol,svect);
1.76      noro     4550:                if ( m == -1 )
                   4551:                        maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
                   4552:                else
                   4553:                        maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74      noro     4554:                for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
                   4555:                if ( i < col ) {
                   4556:                        spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
                   4557:                        for ( j = k = 0; j < col; j++ )
                   4558:                                if ( !rhead[j] ) v[k++] = svect[j];
1.76      noro     4559:                        spsugar[sprow] = MAX(maxrs,SG(spol));
1.74      noro     4560:                        sprow++;
                   4561:                }
1.76      noro     4562:                nd_free(spol);
1.74      noro     4563:        }
                   4564:        /* elimination (2nd step) */
                   4565:        colstat = (int *)ALLOCA(spcol*sizeof(int));
                   4566:        if ( m == -1 )
1.76      noro     4567:                rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4568:        else
1.76      noro     4569:                rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74      noro     4570:        nd_send_int(rank);
                   4571:        for ( i = 0; i < rank; i++ ) {
                   4572:                nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
                   4573:                nd_send_ndv(nf);
1.63      noro     4574:        }
1.74      noro     4575:        fflush(nd_write);
1.76      noro     4576: }
                   4577:
                   4578: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4579: {
                   4580:        int i,j,k,l,inv,a,rank,s;
                   4581:        unsigned int *t,*pivot,*pk;
                   4582:        unsigned int **mat;
                   4583:
                   4584:        mat = (unsigned int **)mat0;
                   4585:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4586:                for ( i = rank; i < row; i++ )
                   4587:                        mat[i][j] %= md;
                   4588:                for ( i = rank; i < row; i++ )
                   4589:                        if ( mat[i][j] )
                   4590:                                break;
                   4591:                if ( i == row ) {
                   4592:                        colstat[j] = 0;
                   4593:                        continue;
                   4594:                } else
                   4595:                        colstat[j] = 1;
                   4596:                if ( i != rank ) {
                   4597:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4598:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4599:                }
                   4600:                pivot = mat[rank];
                   4601:                s = sugar[rank];
                   4602:                inv = invm(pivot[j],md);
                   4603:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4604:                        if ( *pk ) {
                   4605:                                if ( *pk >= (unsigned int)md )
                   4606:                                        *pk %= md;
                   4607:                                DMAR(*pk,inv,0,md,*pk)
                   4608:                        }
                   4609:                for ( i = rank+1; i < row; i++ ) {
                   4610:                        t = mat[i];
                   4611:                        if ( a = t[j] ) {
                   4612:                                sugar[i] = MAX(sugar[i],s);
                   4613:                                red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4614:                        }
                   4615:                }
                   4616:                rank++;
                   4617:        }
                   4618:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4619:                if ( colstat[j] ) {
                   4620:                        pivot = mat[l];
                   4621:                        s = sugar[l];
                   4622:                        for ( i = 0; i < l; i++ ) {
                   4623:                                t = mat[i];
                   4624:                                t[j] %= md;
                   4625:                                if ( a = t[j] ) {
                   4626:                                        sugar[i] = MAX(sugar[i],s);
                   4627:                                        red_by_vect(md,t+j,pivot+j,md-a,col-j);
                   4628:                                }
                   4629:                        }
                   4630:                        l--;
                   4631:                }
                   4632:        for ( j = 0, l = 0; l < rank; j++ )
                   4633:                if ( colstat[j] ) {
                   4634:                        t = mat[l];
                   4635:                        for ( k = j; k < col; k++ )
                   4636:                                if ( t[k] >= (unsigned int)md )
                   4637:                                        t[k] %= md;
                   4638:                        l++;
                   4639:                }
                   4640:        return rank;
                   4641: }
                   4642:
                   4643: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
                   4644: {
                   4645:        int i,j,k,l,inv,a,rank,s;
                   4646:        unsigned int *t,*pivot,*pk;
                   4647:        unsigned int **mat;
                   4648:
                   4649:        mat = (unsigned int **)mat0;
                   4650:        for ( rank = 0, j = 0; j < col; j++ ) {
                   4651:                for ( i = rank; i < row; i++ )
                   4652:                        if ( mat[i][j] )
                   4653:                                break;
                   4654:                if ( i == row ) {
                   4655:                        colstat[j] = 0;
                   4656:                        continue;
                   4657:                } else
                   4658:                        colstat[j] = 1;
                   4659:                if ( i != rank ) {
                   4660:                        t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
                   4661:                        s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
                   4662:                }
                   4663:                pivot = mat[rank];
                   4664:                s = sugar[rank];
                   4665:                inv = _invsf(pivot[j]);
                   4666:                for ( k = j, pk = pivot+k; k < col; k++, pk++ )
                   4667:                        if ( *pk )
                   4668:                                *pk = _mulsf(*pk,inv);
                   4669:                for ( i = rank+1; i < row; i++ ) {
                   4670:                        t = mat[i];
                   4671:                        if ( a = t[j] ) {
                   4672:                                sugar[i] = MAX(sugar[i],s);
                   4673:                                red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4674:                        }
                   4675:                }
                   4676:                rank++;
                   4677:        }
                   4678:        for ( j = col-1, l = rank-1; j >= 0; j-- )
                   4679:                if ( colstat[j] ) {
                   4680:                        pivot = mat[l];
                   4681:                        s = sugar[l];
                   4682:                        for ( i = 0; i < l; i++ ) {
                   4683:                                t = mat[i];
                   4684:                                if ( a = t[j] ) {
                   4685:                                        sugar[i] = MAX(sugar[i],s);
                   4686:                                        red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
                   4687:                                }
                   4688:                        }
                   4689:                        l--;
                   4690:                }
                   4691:        return rank;
1.77      noro     4692: }
                   4693:
                   4694: int ndv_ishomo(NDV p)
                   4695: {
                   4696:        NMV m;
                   4697:        int len,h;
                   4698:
                   4699:        if ( !p ) return 1;
                   4700:        len = LEN(p);
                   4701:        m = BDY(p);
                   4702:        h = TD(DL(m));
                   4703:        NMV_ADV(m);
                   4704:        for ( len--; len; len--, NMV_ADV(m) )
                   4705:                if ( TD(DL(m)) != h ) return 0;
                   4706:        return 1;
                   4707: }
                   4708:
                   4709: void ndv_save(NDV p,int index)
                   4710: {
                   4711:        FILE *s;
                   4712:        char name[BUFSIZ];
                   4713:        short id;
                   4714:        int nv,sugar,len,n,i,td,e,j;
                   4715:        NMV m;
                   4716:        unsigned int *dl;
                   4717:
                   4718:        sprintf(name,"%s/%d",Demand,index);
                   4719:        s = fopen(name,"w");
                   4720:        savevl(s,0);
                   4721:        if ( !p ) {
                   4722:                saveobj(s,0);
                   4723:                return;
                   4724:        }
                   4725:        id = O_DP;
                   4726:        nv = NV(p);
                   4727:        sugar = SG(p);
                   4728:        len = LEN(p);
                   4729:        write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
                   4730:        write_int(s,&len);
                   4731:
                   4732:        for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
                   4733:                saveobj(s,(Obj)CQ(m));
                   4734:                dl = DL(m);
                   4735:                td = TD(dl);
                   4736:                write_int(s,&td);
                   4737:                for ( j = 0; j < nv; j++ ) {
                   4738:                        e = GET_EXP(dl,j);
                   4739:                        write_int(s,&e);
                   4740:                }
                   4741:        }
                   4742:        fclose(s);
                   4743: }
                   4744:
                   4745: NDV ndv_load(int index)
                   4746: {
                   4747:        FILE *s;
                   4748:        char name[BUFSIZ];
                   4749:        short id;
                   4750:        int nv,sugar,len,n,i,td,e,j;
                   4751:        NDV d;
                   4752:        NMV m0,m;
                   4753:        unsigned int *dl;
                   4754:        Obj obj;
                   4755:
                   4756:        sprintf(name,"%s/%d",Demand,index);
                   4757:        s = fopen(name,"r");
                   4758:        if ( !s ) return 0;
                   4759:
                   4760:        skipvl(s);
                   4761:        read_short(s,&id);
                   4762:        if ( !id ) return 0;
                   4763:        read_int(s,&nv);
                   4764:        read_int(s,&sugar);
                   4765:        read_int(s,&len);
                   4766:
                   4767:        m0 = m = MALLOC(len*nmv_adv);
                   4768:        for ( i = 0; i < len; i++, NMV_ADV(m) ) {
                   4769:                loadobj(s,&obj); CQ(m) = (Q)obj;
                   4770:                dl = DL(m);
                   4771:                ndl_zero(dl);
                   4772:                read_int(s,&td); TD(dl) = td;
                   4773:                for ( j = 0; j < nv; j++ ) {
                   4774:                        read_int(s,&e);
                   4775:                        PUT_EXP(dl,j,e);
                   4776:                }
                   4777:                if ( nd_blockmask ) ndl_weight_mask(dl);
                   4778:        }
                   4779:        fclose(s);
                   4780:        MKNDV(nv,m0,len,d);
                   4781:        SG(d) = sugar;
                   4782:        return d;
1.59      noro     4783: }

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