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