Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.89
1.89 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.88 2003/12/24 02:20:19 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.89 ! 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.88 noro 2409: switch ( ord->id ) {
2410: case 1:
2411: if ( ord->nv != nvar )
2412: error("nd_{gr,f4} : invalid order specification");
2413: break;
2414: default:
2415: break;
2416: }
1.32 noro 2417: nd_init_ord(ord);
1.61 noro 2418: for ( t = BDY(f), max = 0; t; t = NEXT(t) )
2419: for ( tv = vv; tv; tv = NEXT(tv) ) {
2420: e = getdeg(tv->v,(P)BDY(t));
2421: max = MAX(e,max);
2422: }
2423: nd_setup_parameters(nvar,max);
1.77 noro 2424: ishomo = 1;
1.1 noro 2425: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.61 noro 2426: b = (pointer)ptondv(CO,vv,(P)BDY(t));
1.77 noro 2427: if ( ishomo )
2428: ishomo = ishomo && ndv_ishomo(b);
1.69 noro 2429: if ( m ) ndv_mod(m,b);
1.61 noro 2430: if ( b ) { NEXTNODE(fd0,fd); BDY(fd) = (pointer)b; }
1.1 noro 2431: }
2432: if ( fd0 ) NEXT(fd) = 0;
1.61 noro 2433: ndv_setup(m,0,fd0);
1.77 noro 2434: x = f4?nd_f4(m):nd_gb(m,ishomo,0);
1.82 noro 2435: nd_demand = 0;
1.61 noro 2436: x = ndv_reducebase(x);
2437: x = ndv_reduceall(m,x);
1.23 noro 2438: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1 noro 2439: NEXTNODE(r0,r);
1.61 noro 2440: BDY(r) = ndvtop(m,CO,vv,BDY(t));
1.20 noro 2441: }
2442: if ( r0 ) NEXT(r) = 0;
2443: MKLIST(*rp,r0);
1.74 noro 2444: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.20 noro 2445: }
2446:
1.52 noro 2447: void nd_gr_trace(LIST f,LIST v,int trace,int homo,struct order_spec *ord,LIST *rp)
1.20 noro 2448: {
1.89 ! noro 2449: struct order_spec *ord1;
1.61 noro 2450: VL tv,fv,vv,vc;
1.27 noro 2451: NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.61 noro 2452: int m,nocheck,nvar,mindex,e,max;
2453: NDV c;
2454: NMV a;
1.27 noro 2455: P p;
1.61 noro 2456: EPOS oepos;
1.82 noro 2457: int obpe,oadv,wmax,i,len,cbpe,ishomo;
1.20 noro 2458:
2459: get_vars((Obj)f,&fv); pltovl(v,&vv);
1.74 noro 2460: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.88 noro 2461: switch ( ord->id ) {
2462: case 1:
2463: if ( ord->nv != nvar )
2464: error("nd_gr_trace : invalid order specification");
2465: break;
2466: default:
2467: break;
2468: }
1.52 noro 2469: nocheck = 0;
2470: mindex = 0;
2471:
1.78 noro 2472: if ( Demand ) nd_demand = 1;
2473: else nd_demand = 0;
2474:
1.52 noro 2475: /* setup modulus */
2476: if ( trace < 0 ) {
2477: trace = -trace;
2478: nocheck = 1;
2479: }
2480: m = trace > 1 ? trace : get_lprime(mindex);
1.61 noro 2481: for ( t = BDY(f), max = 0; t; t = NEXT(t) )
2482: for ( tv = vv; tv; tv = NEXT(tv) ) {
2483: e = getdeg(tv->v,(P)BDY(t));
2484: max = MAX(e,max);
1.23 noro 2485: }
1.61 noro 2486: nd_init_ord(ord);
2487: nd_setup_parameters(nvar,max);
2488: obpe = nd_bpe; oadv = nmv_adv; oepos = nd_epos;
1.82 noro 2489: ishomo = 1;
1.61 noro 2490: for ( in0 = 0, fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
2491: c = ptondv(CO,vv,(P)BDY(t));
1.82 noro 2492: if ( ishomo )
2493: ishomo = ishomo && ndv_ishomo(c);
1.61 noro 2494: if ( c ) {
2495: NEXTNODE(in0,in); BDY(in) = (pointer)c;
2496: NEXTNODE(fd0,fd); BDY(fd) = (pointer)ndv_dup(0,c);
1.23 noro 2497: }
1.61 noro 2498: }
2499: if ( in0 ) NEXT(in) = 0;
2500: if ( fd0 ) NEXT(fd) = 0;
1.82 noro 2501: if ( !ishomo && homo ) {
1.61 noro 2502: for ( t = in0, wmax = 0; t; t = NEXT(t) ) {
2503: c = (NDV)BDY(t); len = LEN(c);
2504: for ( a = BDY(c), i = 0; i < len; i++, NMV_ADV(a) )
2505: wmax = MAX(TD(DL(a)),wmax);
2506: }
2507: homogenize_order(ord,nvar,&ord1);
1.89 ! noro 2508: nd_init_ord(ord1);
1.61 noro 2509: nd_setup_parameters(nvar+1,wmax);
2510: for ( t = fd0; t; t = NEXT(t) )
2511: ndv_homogenize((NDV)BDY(t),obpe,oadv,oepos);
1.27 noro 2512: }
1.52 noro 2513: while ( 1 ) {
1.77 noro 2514: if ( Demand )
2515: nd_demand = 1;
1.61 noro 2516: ndv_setup(m,1,fd0);
1.82 noro 2517: cand = nd_gb_trace(m,ishomo || homo);
1.52 noro 2518: if ( !cand ) {
2519: /* failure */
1.61 noro 2520: if ( trace > 1 ) { *rp = 0; return; }
2521: else m = get_lprime(++mindex);
1.52 noro 2522: continue;
2523: }
1.82 noro 2524: if ( !ishomo && homo ) {
1.27 noro 2525: /* dehomogenization */
1.61 noro 2526: for ( t = cand; t; t = NEXT(t) ) ndv_dehomogenize((NDV)BDY(t),ord);
1.45 noro 2527: nd_init_ord(ord);
1.61 noro 2528: nd_setup_parameters(nvar,0);
1.27 noro 2529: }
1.77 noro 2530: nd_demand = 0;
1.61 noro 2531: cand = ndv_reducebase(cand);
2532: cand = ndv_reduceall(0,cand);
1.78 noro 2533: cbpe = nd_bpe;
1.61 noro 2534: if ( nocheck )
2535: break;
2536: if ( ndv_check_candidate(in0,obpe,oadv,oepos,cand) )
1.52 noro 2537: /* success */
2538: break;
2539: else if ( trace > 1 ) {
2540: /* failure */
2541: *rp = 0; return;
1.61 noro 2542: } else {
1.52 noro 2543: /* try the next modulus */
2544: m = get_lprime(++mindex);
1.61 noro 2545: /* reset the parameters */
1.82 noro 2546: if ( !ishomo && homo ) {
1.89 ! noro 2547: nd_init_ord(ord1);
1.61 noro 2548: nd_setup_parameters(nvar+1,wmax);
2549: } else {
2550: nd_init_ord(ord);
2551: nd_setup_parameters(nvar,max);
2552: }
2553: }
1.52 noro 2554: }
1.27 noro 2555: /* dp->p */
1.61 noro 2556: nd_bpe = cbpe;
1.73 noro 2557: nd_setup_parameters(nd_nvar,0);
1.61 noro 2558: for ( r = cand; r; r = NEXT(r) ) BDY(r) = (pointer)ndvtop(0,CO,vv,BDY(r));
1.27 noro 2559: MKLIST(*rp,cand);
1.1 noro 2560: }
2561:
1.61 noro 2562: void dltondl(int n,DL dl,UINT *r)
1.1 noro 2563: {
1.61 noro 2564: UINT *d;
1.57 noro 2565: int i,j,l,s,ord_l;
1.43 noro 2566: struct order_pair *op;
1.1 noro 2567:
2568: d = dl->d;
1.41 noro 2569: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 2570: if ( nd_blockmask ) {
2571: l = nd_blockmask->n;
2572: op = nd_blockmask->order_pair;
2573: for ( j = 0, s = 0; j < l; j++ ) {
2574: ord_l = op[j].length;
1.57 noro 2575: for ( i = 0; i < ord_l; i++, s++ ) PUT_EXP(r,s,d[s]);
1.43 noro 2576: }
2577: TD(r) = ndl_weight(r);
1.61 noro 2578: ndl_weight_mask(r);
1.43 noro 2579: } else {
1.56 noro 2580: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
1.43 noro 2581: TD(r) = ndl_weight(r);
2582: }
1.1 noro 2583: }
2584:
1.61 noro 2585: DL ndltodl(int n,UINT *ndl)
1.1 noro 2586: {
2587: DL dl;
2588: int *d;
1.57 noro 2589: int i,j,l,s,ord_l;
1.43 noro 2590: struct order_pair *op;
1.1 noro 2591:
2592: NEWDL(dl,n);
1.34 noro 2593: dl->td = TD(ndl);
1.1 noro 2594: d = dl->d;
1.43 noro 2595: if ( nd_blockmask ) {
2596: l = nd_blockmask->n;
2597: op = nd_blockmask->order_pair;
2598: for ( j = 0, s = 0; j < l; j++ ) {
2599: ord_l = op[j].length;
1.57 noro 2600: for ( i = 0; i < ord_l; i++, s++ ) d[s] = GET_EXP(ndl,s);
1.43 noro 2601: }
2602: } else {
1.56 noro 2603: for ( i = 0; i < n; i++ ) d[i] = GET_EXP(ndl,i);
1.43 noro 2604: }
1.1 noro 2605: return dl;
2606: }
2607:
1.61 noro 2608: void ndl_print(UINT *dl)
1.1 noro 2609: {
2610: int n;
1.57 noro 2611: int i,j,l,ord_l,s,s0;
1.43 noro 2612: struct order_pair *op;
1.1 noro 2613:
2614: n = nd_nvar;
2615: printf("<<");
1.43 noro 2616: if ( nd_blockmask ) {
2617: l = nd_blockmask->n;
2618: op = nd_blockmask->order_pair;
2619: for ( j = 0, s = s0 = 0; j < l; j++ ) {
2620: ord_l = op[j].length;
1.57 noro 2621: for ( i = 0; i < ord_l; i++, s++ )
2622: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
1.43 noro 2623: }
2624: } else {
1.56 noro 2625: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
1.43 noro 2626: }
1.1 noro 2627: printf(">>");
2628: }
2629:
2630: void nd_print(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) ) {
1.71 noro 2638: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
2639: else printf("+%d*",CM(m));
1.14 noro 2640: ndl_print(DL(m));
1.1 noro 2641: }
2642: printf("\n");
2643: }
2644: }
2645:
1.16 noro 2646: void nd_print_q(ND p)
2647: {
2648: NM m;
2649:
2650: if ( !p )
2651: printf("0\n");
2652: else {
2653: for ( m = BDY(p); m; m = NEXT(m) ) {
2654: printf("+");
1.74 noro 2655: printexpr(CO,(Obj)CQ(m));
1.16 noro 2656: printf("*");
2657: ndl_print(DL(m));
2658: }
2659: printf("\n");
2660: }
2661: }
2662:
1.1 noro 2663: void ndp_print(ND_pairs d)
2664: {
2665: ND_pairs t;
2666:
1.34 noro 2667: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1 noro 2668: printf("\n");
2669: }
2670:
1.20 noro 2671: void nd_removecont(int mod,ND p)
1.16 noro 2672: {
2673: int i,n;
2674: Q *w;
2675: Q dvr,t;
2676: NM m;
1.21 noro 2677: struct oVECT v;
2678: N q,r;
1.16 noro 2679:
1.71 noro 2680: if ( mod == -1 ) nd_mul_c(mod,p,_invsf(HCM(p)));
2681: else if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20 noro 2682: else {
2683: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
2684: w = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2685: v.len = n;
2686: v.body = (pointer *)w;
1.34 noro 2687: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2688: removecont_array(w,n);
2689: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16 noro 2690: }
2691: }
2692:
1.21 noro 2693: void nd_removecont2(ND p1,ND p2)
2694: {
2695: int i,n1,n2,n;
2696: Q *w;
2697: Q dvr,t;
2698: NM m;
2699: struct oVECT v;
2700: N q,r;
2701:
2702: if ( !p1 ) {
2703: nd_removecont(0,p2); return;
2704: } else if ( !p2 ) {
2705: nd_removecont(0,p1); return;
2706: }
2707: n1 = nd_length(p1);
2708: n2 = nd_length(p2);
2709: n = n1+n2;
2710: w = (Q *)ALLOCA(n*sizeof(Q));
2711: v.len = n;
2712: v.body = (pointer *)w;
1.34 noro 2713: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
2714: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2715: removecont_array(w,n);
2716: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
2717: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
2718: }
2719:
1.20 noro 2720: void ndv_removecont(int mod,NDV p)
1.16 noro 2721: {
2722: int i,len;
2723: Q *w;
2724: Q dvr,t;
2725: NMV m;
2726:
1.71 noro 2727: if ( mod == -1 )
2728: ndv_mul_c(mod,p,_invsf(HCM(p)));
2729: else if ( mod )
1.20 noro 2730: ndv_mul_c(mod,p,invm(HCM(p),mod));
2731: else {
2732: len = p->len;
2733: w = (Q *)ALLOCA(len*sizeof(Q));
1.34 noro 2734: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
1.20 noro 2735: sortbynm(w,len);
2736: qltozl(w,len,&dvr);
2737: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
2738: divq(CQ(m),dvr,&t); CQ(m) = t;
2739: }
1.16 noro 2740: }
1.21 noro 2741: }
2742:
1.61 noro 2743: void ndv_homogenize(NDV p,int obpe,int oadv,EPOS oepos)
2744: {
2745: int len,i,max;
2746: NMV m,mr0,mr,t;
2747:
2748: len = p->len;
2749: for ( m = BDY(p), i = 0, max = 0; i < len; NMV_OADV(m), i++ )
2750: max = MAX(max,TD(DL(m)));
2751: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
2752: m = (NMV)((char *)mr0+(len-1)*oadv);
2753: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
2754: t = (NMV)ALLOCA(nmv_adv);
2755: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
2756: ndl_homogenize(DL(m),DL(t),obpe,oepos,max);
2757: CQ(mr) = CQ(m);
2758: ndl_copy(DL(t),DL(mr));
2759: }
2760: NV(p)++;
2761: BDY(p) = mr0;
2762: }
2763:
1.45 noro 2764: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23 noro 2765: {
1.45 noro 2766: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.23 noro 2767: Q *w;
2768: Q dvr,t;
2769: NMV m,r;
2770:
2771: len = p->len;
2772: newnvar = nd_nvar-1;
1.48 noro 2773: newexporigin = nd_get_exporigin(ord);
1.45 noro 2774: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23 noro 2775: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34 noro 2776: ndl_dehomogenize(DL(m));
1.23 noro 2777: if ( newwpd != nd_wpd ) {
1.61 noro 2778: newadv = sizeof(struct oNMV)+(newwpd-1)*sizeof(UINT);
2779: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NDV_NADV(r), i++ ) {
1.45 noro 2780: CQ(r) = CQ(m);
2781: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
2782: adj = nd_exporigin-newexporigin;
2783: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23 noro 2784: }
2785: }
2786: NV(p)--;
2787: }
2788:
1.21 noro 2789: void removecont_array(Q *c,int n)
2790: {
2791: struct oVECT v;
2792: Q d0,d1,a,u,u1,gcd;
2793: int i;
2794: N qn,rn,gn;
2795: Q *q,*r;
2796:
2797: q = (Q *)ALLOCA(n*sizeof(Q));
2798: r = (Q *)ALLOCA(n*sizeof(Q));
2799: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
2800: igcdv_estimate(&v,&d0);
2801: for ( i = 0; i < n; i++ ) {
2802: divn(NM(c[i]),NM(d0),&qn,&rn);
2803: NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
2804: NTOQ(rn,SGN(c[i]),r[i]);
2805: }
1.34 noro 2806: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21 noro 2807: if ( i < n ) {
2808: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
2809: igcdv(&v,&d1);
2810: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
2811: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
2812: for ( i = 0; i < n; i++ ) {
2813: mulq(a,q[i],&u);
2814: if ( r[i] ) {
2815: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
2816: addq(u,u1,&q[i]);
2817: } else
2818: q[i] = u;
2819: }
2820: }
1.34 noro 2821: for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16 noro 2822: }
2823:
1.19 noro 2824: void nd_mul_c(int mod,ND p,int mul)
1.1 noro 2825: {
2826: NM m;
2827: int c,c1;
2828:
1.34 noro 2829: if ( !p ) return;
1.71 noro 2830: if ( mod == -1 )
2831: for ( m = BDY(p); m; m = NEXT(m) )
2832: CM(m) = _mulsf(CM(m),mul);
2833: else
2834: for ( m = BDY(p); m; m = NEXT(m) ) {
2835: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
2836: }
1.1 noro 2837: }
2838:
1.16 noro 2839: void nd_mul_c_q(ND p,Q mul)
2840: {
2841: NM m;
2842: Q c;
2843:
1.34 noro 2844: if ( !p ) return;
1.16 noro 2845: for ( m = BDY(p); m; m = NEXT(m) ) {
2846: mulq(CQ(m),mul,&c); CQ(m) = c;
2847: }
2848: }
2849:
1.61 noro 2850: void nd_mul_c_p(VL vl,ND p,P mul)
2851: {
2852: NM m;
2853: P c;
2854:
2855: if ( !p ) return;
2856: for ( m = BDY(p); m; m = NEXT(m) ) {
2857: mulp(vl,CP(m),mul,&c); CP(m) = c;
2858: }
2859: }
2860:
1.1 noro 2861: void nd_free(ND p)
2862: {
2863: NM t,s;
2864:
1.34 noro 2865: if ( !p ) return;
1.1 noro 2866: t = BDY(p);
2867: while ( t ) {
2868: s = NEXT(t);
2869: FREENM(t);
2870: t = s;
2871: }
2872: FREEND(p);
2873: }
2874:
1.23 noro 2875: void ndv_free(NDV p)
2876: {
2877: GC_free(BDY(p));
2878: }
2879:
1.61 noro 2880: void nd_append_red(UINT *d,int i)
1.1 noro 2881: {
1.13 noro 2882: RHist m,m0;
1.1 noro 2883: int h;
2884:
1.13 noro 2885: NEWRHist(m);
1.34 noro 2886: h = ndl_hash_value(d);
1.13 noro 2887: m->index = i;
1.14 noro 2888: ndl_copy(d,DL(m));
1.1 noro 2889: NEXT(m) = nd_red[h];
2890: nd_red[h] = m;
2891: }
2892:
1.61 noro 2893: UINT *ndv_compute_bound(NDV p)
1.1 noro 2894: {
1.61 noro 2895: UINT *d1,*d2,*t;
2896: UINT u;
1.57 noro 2897: int i,j,k,l,len,ind;
1.45 noro 2898: NMV m;
1.1 noro 2899:
2900: if ( !p )
2901: return 0;
1.61 noro 2902: d1 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
2903: d2 = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.45 noro 2904: len = LEN(p);
2905: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
2906: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14 noro 2907: ndl_lcm(DL(m),d1,d2);
1.1 noro 2908: t = d1; d1 = d2; d2 = t;
2909: }
1.12 noro 2910: l = nd_nvar+31;
1.61 noro 2911: t = (UINT *)MALLOC_ATOMIC(l*sizeof(UINT));
1.57 noro 2912: for ( i = nd_exporigin, ind = 0; i < nd_wpd; i++ ) {
2913: u = d1[i];
2914: k = (nd_epw-1)*nd_bpe;
2915: for ( j = 0; j < nd_epw; j++, k -= nd_bpe, ind++ )
2916: t[ind] = (u>>k)&nd_mask0;
2917: }
2918: for ( ; ind < l; ind++ ) t[ind] = 0;
1.1 noro 2919: return t;
2920: }
2921:
1.48 noro 2922: int nd_get_exporigin(struct order_spec *ord)
2923: {
1.51 noro 2924: switch ( ord->id ) {
1.41 noro 2925: case 0:
1.48 noro 2926: return 1;
1.41 noro 2927: case 1:
2928: /* block order */
1.43 noro 2929: /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
1.48 noro 2930: return ord->ord.block.length+1;
1.41 noro 2931: case 2:
1.52 noro 2932: error("nd_get_exporigin : matrix order is not supported yet.");
1.41 noro 2933: }
1.48 noro 2934: }
2935:
1.61 noro 2936: void nd_setup_parameters(int nvar,int max) {
1.79 noro 2937: int i,j,n,elen,ord_o,ord_l,l,s,wpd;
1.57 noro 2938: struct order_pair *op;
1.48 noro 2939:
1.73 noro 2940: nd_nvar = nvar;
1.79 noro 2941: if ( max ) {
1.83 noro 2942: /* XXX */
2943: if ( do_weyl ) nd_bpe = 32;
2944: else if ( max < 2 ) nd_bpe = 1;
1.79 noro 2945: else if ( max < 4 ) nd_bpe = 2;
2946: else if ( max < 8 ) nd_bpe = 3;
2947: else if ( max < 16 ) nd_bpe = 4;
2948: else if ( max < 32 ) nd_bpe = 5;
2949: else if ( max < 64 ) nd_bpe = 6;
2950: else if ( max < 256 ) nd_bpe = 8;
2951: else if ( max < 1024 ) nd_bpe = 10;
2952: else if ( max < 65536 ) nd_bpe = 16;
2953: else nd_bpe = 32;
2954: }
1.61 noro 2955: nd_epw = (sizeof(UINT)*8)/nd_bpe;
1.48 noro 2956: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
2957: nd_exporigin = nd_get_exporigin(nd_ord);
1.79 noro 2958: wpd = nd_exporigin+elen;
2959: if ( wpd != nd_wpd ) {
2960: nd_free_private_storage();
2961: nd_wpd = wpd;
2962: }
1.1 noro 2963: if ( nd_bpe < 32 ) {
2964: nd_mask0 = (1<<nd_bpe)-1;
2965: } else {
2966: nd_mask0 = 0xffffffff;
2967: }
2968: bzero(nd_mask,sizeof(nd_mask));
2969: nd_mask1 = 0;
2970: for ( i = 0; i < nd_epw; i++ ) {
2971: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
2972: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
2973: }
1.61 noro 2974: nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT);
2975: nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(UINT);
1.57 noro 2976: nd_epos = nd_create_epos(nd_ord);
1.43 noro 2977: nd_blockmask = nd_create_blockmask(nd_ord);
1.1 noro 2978: }
2979:
1.20 noro 2980: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1 noro 2981: {
1.37 noro 2982: int i,obpe,oadv,h;
1.71 noro 2983: static NM prev_nm_free_list;
2984: static ND_pairs prev_ndp_free_list;
1.13 noro 2985: RHist mr0,mr;
2986: RHist r;
1.37 noro 2987: RHist *old_red;
1.71 noro 2988: ND_pairs s0,s,t;
1.43 noro 2989: EPOS oepos;
1.15 noro 2990:
1.1 noro 2991: obpe = nd_bpe;
1.11 noro 2992: oadv = nmv_adv;
1.43 noro 2993: oepos = nd_epos;
1.63 noro 2994: if ( obpe < 2 ) nd_bpe = 2;
2995: else if ( obpe < 3 ) nd_bpe = 3;
1.62 noro 2996: else if ( obpe < 4 ) nd_bpe = 4;
1.63 noro 2997: else if ( obpe < 5 ) nd_bpe = 5;
1.34 noro 2998: else if ( obpe < 6 ) nd_bpe = 6;
2999: else if ( obpe < 8 ) nd_bpe = 8;
1.63 noro 3000: else if ( obpe < 10 ) nd_bpe = 10;
1.34 noro 3001: else if ( obpe < 16 ) nd_bpe = 16;
3002: else if ( obpe < 32 ) nd_bpe = 32;
3003: else error("nd_reconstruct : exponent too large");
1.5 noro 3004:
1.73 noro 3005: nd_setup_parameters(nd_nvar,0);
1.1 noro 3006: prev_nm_free_list = _nm_free_list;
3007: prev_ndp_free_list = _ndp_free_list;
3008: _nm_free_list = 0;
3009: _ndp_free_list = 0;
1.53 noro 3010: for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
3011: if ( trace )
3012: for ( i = nd_psn-1; i >= 0; i-- )
3013: ndv_realloc(nd_ps_trace[i],obpe,oadv,oepos);
1.1 noro 3014: s0 = 0;
3015: for ( t = d; t; t = NEXT(t) ) {
3016: NEXTND_pairs(s0,s);
3017: s->i1 = t->i1;
3018: s->i2 = t->i2;
1.14 noro 3019: SG(s) = SG(t);
1.61 noro 3020: ndl_reconstruct(LCM(t),LCM(s),obpe,oepos);
1.1 noro 3021: }
1.37 noro 3022:
3023: old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6 noro 3024: for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37 noro 3025: old_red[i] = nd_red[i];
3026: nd_red[i] = 0;
3027: }
3028: for ( i = 0; i < REDTAB_LEN; i++ )
3029: for ( r = old_red[i]; r; r = NEXT(r) ) {
3030: NEWRHist(mr);
1.13 noro 3031: mr->index = r->index;
1.20 noro 3032: SG(mr) = SG(r);
1.61 noro 3033: ndl_reconstruct(DL(r),DL(mr),obpe,oepos);
1.37 noro 3034: h = ndl_hash_value(DL(mr));
3035: NEXT(mr) = nd_red[h];
3036: nd_red[h] = mr;
1.6 noro 3037: }
1.37 noro 3038: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
3039: old_red = 0;
1.11 noro 3040: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 3041: NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.61 noro 3042: ndl_reconstruct(DL(nd_psh[i]),DL(r),obpe,oepos);
1.13 noro 3043: nd_psh[i] = r;
1.11 noro 3044: }
1.1 noro 3045: if ( s0 ) NEXT(s) = 0;
3046: prev_nm_free_list = 0;
3047: prev_ndp_free_list = 0;
1.71 noro 3048: #if 0
1.1 noro 3049: GC_gcollect();
1.71 noro 3050: #endif
1.1 noro 3051: return s0;
3052: }
3053:
1.61 noro 3054: void ndl_reconstruct(UINT *d,UINT *r,int obpe,EPOS oepos)
1.1 noro 3055: {
1.57 noro 3056: int n,i,ei,oepw,omask0,j,s,ord_l,l;
1.43 noro 3057: struct order_pair *op;
1.1 noro 3058:
3059: n = nd_nvar;
1.61 noro 3060: oepw = (sizeof(UINT)*8)/obpe;
1.43 noro 3061: omask0 = (1<<obpe)-1;
1.34 noro 3062: TD(r) = TD(d);
1.41 noro 3063: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 3064: if ( nd_blockmask ) {
3065: l = nd_blockmask->n;
3066: op = nd_blockmask->order_pair;
3067: for ( i = 1; i < nd_exporigin; i++ )
3068: r[i] = d[i];
3069: for ( j = 0, s = 0; j < l; j++ ) {
3070: ord_l = op[j].length;
1.57 noro 3071: for ( i = 0; i < ord_l; i++, s++ ) {
3072: ei = GET_EXP_OLD(d,s);
3073: PUT_EXP(r,s,ei);
3074: }
1.1 noro 3075: }
1.43 noro 3076: } else {
1.56 noro 3077: for ( i = 0; i < n; i++ ) {
3078: ei = GET_EXP_OLD(d,i);
3079: PUT_EXP(r,i,ei);
3080: }
1.1 noro 3081: }
3082: }
1.3 noro 3083:
1.6 noro 3084: ND nd_copy(ND p)
3085: {
3086: NM m,mr,mr0;
1.41 noro 3087: int c,n;
1.6 noro 3088: ND r;
3089:
3090: if ( !p )
3091: return 0;
3092: else {
3093: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
3094: NEXTNM(mr0,mr);
1.14 noro 3095: CM(mr) = CM(m);
3096: ndl_copy(DL(m),DL(mr));
1.6 noro 3097: }
3098: NEXT(mr) = 0;
1.31 noro 3099: MKND(NV(p),mr0,LEN(p),r);
1.14 noro 3100: SG(r) = SG(p);
1.6 noro 3101: return r;
3102: }
3103: }
3104:
1.53 noro 3105: int nd_sp(int mod,int trace,ND_pairs p,ND *rp)
1.11 noro 3106: {
3107: NM m;
3108: NDV p1,p2;
3109: ND t1,t2;
1.61 noro 3110: UINT *lcm;
1.31 noro 3111: int td;
1.11 noro 3112:
1.77 noro 3113: if ( !mod && nd_demand ) {
3114: p1 = ndv_load(p->i1); p2 = ndv_load(p->i2);
1.53 noro 3115: } else {
1.77 noro 3116: if ( trace ) {
3117: p1 = nd_ps_trace[p->i1]; p2 = nd_ps_trace[p->i2];
3118: } else {
3119: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
3120: }
1.20 noro 3121: }
1.34 noro 3122: lcm = LCM(p);
1.11 noro 3123: NEWNM(m);
1.20 noro 3124: CQ(m) = HCQ(p2);
1.34 noro 3125: ndl_sub(lcm,HDL(p1),DL(m));
1.56 noro 3126: if ( ndl_check_bound2(p->i1,DL(m)) )
3127: return 0;
1.55 noro 3128: t1 = ndv_mul_nm(mod,m,p1);
1.71 noro 3129: if ( mod == -1 ) CM(m) = _chsgnsf(HCM(p1));
3130: else if ( mod ) CM(m) = mod-HCM(p1);
1.34 noro 3131: else chsgnq(HCQ(p1),&CQ(m));
3132: ndl_sub(lcm,HDL(p2),DL(m));
1.14 noro 3133: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 3134: nd_free(t1);
3135: return 0;
3136: }
1.55 noro 3137: t2 = ndv_mul_nm(mod,m,p2);
1.31 noro 3138: *rp = nd_add(mod,t1,t2);
1.11 noro 3139: FREENM(m);
3140: return 1;
3141: }
3142:
1.19 noro 3143: void ndv_mul_c(int mod,NDV p,int mul)
1.11 noro 3144: {
3145: NMV m;
3146: int c,c1,len,i;
3147:
1.34 noro 3148: if ( !p ) return;
1.14 noro 3149: len = LEN(p);
1.71 noro 3150: if ( mod == -1 )
3151: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) )
3152: CM(m) = _mulsf(CM(m),mul);
3153: else
3154: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3155: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
3156: }
1.11 noro 3157: }
3158:
1.16 noro 3159: void ndv_mul_c_q(NDV p,Q mul)
3160: {
3161: NMV m;
3162: Q c;
3163: int len,i;
3164:
1.34 noro 3165: if ( !p ) return;
1.16 noro 3166: len = LEN(p);
3167: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3168: mulq(CQ(m),mul,&c); CQ(m) = c;
3169: }
3170: }
3171:
1.55 noro 3172: ND weyl_ndv_mul_nm(int mod,NM m0,NDV p) {
3173: int n2,i,j,l,n,tlen;
1.61 noro 3174: UINT *d0;
1.55 noro 3175: NM *tab,*psum;
3176: ND s,r;
3177: NM t;
3178: NMV m1;
3179:
3180: if ( !p ) return 0;
3181: n = NV(p); n2 = n>>1;
3182: d0 = DL(m0);
3183: l = LEN(p);
3184: for ( i = 0, tlen = 1; i < n2; i++ ) tlen *= (GET_EXP(d0,n2+i)+1);
3185: tab = (NM *)ALLOCA(tlen*sizeof(NM));
3186: psum = (NM *)ALLOCA(tlen*sizeof(NM));
3187: for ( i = 0; i < tlen; i++ ) psum[i] = 0;
1.56 noro 3188: m1 = (NMV)(((char *)BDY(p))+nmv_adv*(l-1));
3189: for ( i = l-1; i >= 0; i--, NMV_PREV(m1) ) {
1.55 noro 3190: /* m0(NM) * m1(NMV) => tab(NM) */
1.56 noro 3191: weyl_mul_nm_nmv(n,mod,m0,m1,tab,tlen);
1.55 noro 3192: for ( j = 0; j < tlen; j++ ) {
3193: if ( tab[j] ) {
3194: NEXT(tab[j]) = psum[j]; psum[j] = tab[j];
3195: }
3196: }
3197: }
3198: for ( i = tlen-1, r = 0; i >= 0; i-- )
3199: if ( psum[i] ) {
3200: for ( j = 0, t = psum[i]; t; t = NEXT(t), j++ );
3201: MKND(n,psum[i],j,s);
3202: r = nd_add(mod,r,s);
3203: }
1.56 noro 3204: if ( r ) SG(r) = SG(p)+TD(d0);
3205: return r;
1.55 noro 3206: }
3207:
1.56 noro 3208: /* product of monomials */
3209: /* XXX block order is not handled correctly */
3210:
1.55 noro 3211: void weyl_mul_nm_nmv(int n,int mod,NM m0,NMV m1,NM *tab,int tlen)
3212: {
1.56 noro 3213: int i,n2,j,s,curlen,homo,h,a,b,k,l,u,min;
1.61 noro 3214: UINT *d0,*d1,*d,*dt,*ctab;
1.56 noro 3215: Q *ctab_q;
3216: Q q,q1;
1.61 noro 3217: UINT c0,c1,c;
1.55 noro 3218: NM *p;
3219: NM m,t;
3220:
3221: for ( i = 0; i < tlen; i++ ) tab[i] = 0;
3222: if ( !m0 || !m1 ) return;
3223: d0 = DL(m0); d1 = DL(m1); n2 = n>>1;
3224: NEWNM(m); d = DL(m);
1.56 noro 3225: if ( mod ) {
3226: c0 = CM(m0); c1 = CM(m1); DMAR(c0,c1,0,mod,c); CM(m) = c;
3227: } else
3228: mulq(CQ(m0),CQ(m1),&CQ(m));
1.55 noro 3229: for ( i = 0; i < nd_wpd; i++ ) d[i] = 0;
3230: homo = n&1 ? 1 : 0;
3231: if ( homo ) {
3232: /* offset of h-degree */
3233: h = GET_EXP(d0,n-1)+GET_EXP(d1,n-1);
3234: PUT_EXP(DL(m),n-1,h);
3235: TD(DL(m)) = h;
1.61 noro 3236: if ( nd_blockmask ) ndl_weight_mask(DL(m));
1.55 noro 3237: }
3238: tab[0] = m;
3239: NEWNM(m); d = DL(m);
1.57 noro 3240: for ( i = 0, curlen = 1; i < n2; i++ ) {
1.55 noro 3241: a = GET_EXP(d0,i); b = GET_EXP(d1,n2+i);
3242: k = GET_EXP(d0,n2+i); l = GET_EXP(d1,i);
3243: /* xi^a*(Di^k*xi^l)*Di^b */
3244: a += l; b += k;
1.56 noro 3245: s = MUL_WEIGHT(a,i)+MUL_WEIGHT(b,n2+i);
1.55 noro 3246: if ( !k || !l ) {
3247: for ( j = 0; j < curlen; j++ )
1.56 noro 3248: if ( t = tab[j] ) {
3249: dt = DL(t);
3250: PUT_EXP(dt,i,a); PUT_EXP(dt,n2+i,b); TD(dt) += s;
1.61 noro 3251: if ( nd_blockmask ) ndl_weight_mask(dt);
1.55 noro 3252: }
3253: curlen *= k+1;
3254: continue;
3255: }
3256: min = MIN(k,l);
1.56 noro 3257: if ( mod ) {
1.61 noro 3258: ctab = (UINT *)ALLOCA((min+1)*sizeof(UINT));
1.56 noro 3259: mkwcm(k,l,mod,ctab);
3260: } else {
3261: ctab_q = (Q *)ALLOCA((min+1)*sizeof(Q));
3262: mkwc(k,l,ctab_q);
3263: }
1.57 noro 3264: for ( j = min; j >= 0; j-- ) {
1.56 noro 3265: for ( u = 0; u < nd_wpd; u++ ) d[u] = 0;
1.55 noro 3266: PUT_EXP(d,i,a-j); PUT_EXP(d,n2+i,b-j);
1.56 noro 3267: h = MUL_WEIGHT(a-j,i)+MUL_WEIGHT(b-j,n2+i);
1.55 noro 3268: if ( homo ) {
3269: TD(d) = s;
1.56 noro 3270: PUT_EXP(d,n-1,s-h);
1.55 noro 3271: } else TD(d) = h;
1.61 noro 3272: if ( nd_blockmask ) ndl_weight_mask(d);
1.56 noro 3273: if ( mod ) c = ctab[j];
3274: else q = ctab_q[j];
1.57 noro 3275: p = tab+curlen*j;
3276: if ( j == 0 ) {
3277: for ( u = 0; u < curlen; u++, p++ ) {
3278: if ( tab[u] ) {
3279: ndl_addto(DL(tab[u]),d);
3280: if ( mod ) {
3281: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(tab[u]) = c1;
3282: } else {
3283: mulq(CQ(tab[u]),q,&q1); CQ(tab[u]) = q1;
3284: }
3285: }
1.56 noro 3286: }
1.57 noro 3287: } else {
3288: for ( u = 0; u < curlen; u++, p++ ) {
3289: if ( tab[u] ) {
3290: NEWNM(t);
3291: ndl_add(DL(tab[u]),d,DL(t));
3292: if ( mod ) {
3293: c0 = CM(tab[u]); DMAR(c0,c,0,mod,c1); CM(t) = c1;
3294: } else
3295: mulq(CQ(tab[u]),q,&CQ(t));
3296: *p = t;
3297: }
1.55 noro 3298: }
3299: }
3300: }
3301: curlen *= k+1;
3302: }
3303: FREENM(m);
3304: }
3305:
1.63 noro 3306: ND ndv_mul_nm_symbolic(NM m0,NDV p)
3307: {
3308: NM mr,mr0;
3309: NMV m;
3310: UINT *d,*dt,*dm;
3311: int c,n,td,i,c1,c2,len;
3312: Q q;
3313: ND r;
3314:
3315: if ( !p ) return 0;
3316: else {
3317: n = NV(p); m = BDY(p);
3318: d = DL(m0);
3319: len = LEN(p);
3320: mr0 = 0;
3321: td = TD(d);
3322: c = CM(m0);
3323: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3324: NEXTNM(mr0,mr);
3325: CM(mr) = 1;
3326: ndl_add(DL(m),d,DL(mr));
3327: }
3328: NEXT(mr) = 0;
3329: MKND(NV(p),mr0,len,r);
3330: SG(r) = SG(p) + TD(d);
3331: return r;
3332: }
3333: }
3334:
1.55 noro 3335: ND ndv_mul_nm(int mod,NM m0,NDV p)
1.9 noro 3336: {
3337: NM mr,mr0;
3338: NMV m;
1.61 noro 3339: UINT *d,*dt,*dm;
1.9 noro 3340: int c,n,td,i,c1,c2,len;
1.16 noro 3341: Q q;
1.9 noro 3342: ND r;
3343:
1.34 noro 3344: if ( !p ) return 0;
1.55 noro 3345: else if ( do_weyl )
1.71 noro 3346: if ( mod == -1 )
3347: error("ndv_mul_nm : not implemented (weyl)");
3348: else
3349: return weyl_ndv_mul_nm(mod,m0,p);
1.9 noro 3350: else {
3351: n = NV(p); m = BDY(p);
1.34 noro 3352: d = DL(m0);
1.14 noro 3353: len = LEN(p);
1.9 noro 3354: mr0 = 0;
1.34 noro 3355: td = TD(d);
1.71 noro 3356: if ( mod == -1 ) {
3357: c = CM(m0);
3358: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3359: NEXTNM(mr0,mr);
3360: CM(mr) = _mulsf(CM(m),c);
3361: ndl_add(DL(m),d,DL(mr));
3362: }
3363: } else if ( mod ) {
1.16 noro 3364: c = CM(m0);
3365: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3366: NEXTNM(mr0,mr);
3367: c1 = CM(m);
1.19 noro 3368: DMAR(c1,c,0,mod,c2);
1.16 noro 3369: CM(mr) = c2;
3370: ndl_add(DL(m),d,DL(mr));
3371: }
3372: } else {
3373: q = CQ(m0);
3374: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
3375: NEXTNM(mr0,mr);
3376: mulq(CQ(m),q,&CQ(mr));
3377: ndl_add(DL(m),d,DL(mr));
3378: }
1.4 noro 3379: }
1.9 noro 3380: NEXT(mr) = 0;
1.31 noro 3381: MKND(NV(p),mr0,len,r);
1.34 noro 3382: SG(r) = SG(p) + TD(d);
1.9 noro 3383: return r;
1.4 noro 3384: }
3385: }
3386:
1.43 noro 3387: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11 noro 3388: {
1.13 noro 3389: NMV m,mr,mr0,t;
3390: int len,i,k;
1.11 noro 3391:
1.61 noro 3392: if ( !p ) return;
3393: m = BDY(p); len = LEN(p);
3394: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
3395: m = (NMV)((char *)mr0+(len-1)*oadv);
3396: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
3397: t = (NMV)ALLOCA(nmv_adv);
3398: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
3399: CQ(t) = CQ(m);
3400: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
3401: ndl_reconstruct(DL(m),DL(t),obpe,oepos);
3402: CQ(mr) = CQ(t);
3403: ndl_copy(DL(t),DL(mr));
3404: }
3405: BDY(p) = mr0;
3406: }
3407:
3408: NDV ndv_dup_realloc(NDV p,int obpe,int oadv,EPOS oepos)
3409: {
3410: NMV m,mr,mr0;
3411: int len,i;
3412: NDV r;
1.11 noro 3413:
1.61 noro 3414: if ( !p ) return 0;
3415: m = BDY(p); len = LEN(p);
3416: mr0 = mr = (NMV)MALLOC(len*nmv_adv);
3417: for ( i = 0; i < len; i++, NMV_OADV(m), NMV_ADV(mr) ) {
3418: ndl_zero(DL(mr));
3419: ndl_reconstruct(DL(m),DL(mr),obpe,oepos);
3420: CQ(mr) = CQ(m);
1.11 noro 3421: }
1.61 noro 3422: MKNDV(NV(p),mr0,len,r);
3423: SG(r) = SG(p);
3424: return r;
1.11 noro 3425: }
3426:
1.61 noro 3427: /* duplicate p */
3428:
3429: NDV ndv_dup(int mod,NDV p)
1.3 noro 3430: {
3431: NDV d;
1.61 noro 3432: NMV t,m,m0;
1.3 noro 3433: int i,len;
3434:
1.34 noro 3435: if ( !p ) return 0;
1.31 noro 3436: len = LEN(p);
1.34 noro 3437: m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.61 noro 3438: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t), NMV_ADV(m) ) {
1.14 noro 3439: ndl_copy(DL(t),DL(m));
1.16 noro 3440: CQ(m) = CQ(t);
1.3 noro 3441: }
3442: MKNDV(NV(p),m0,len,d);
1.23 noro 3443: SG(d) = SG(p);
3444: return d;
3445: }
3446:
1.63 noro 3447: ND nd_dup(ND p)
3448: {
3449: ND d;
3450: NM t,m,m0;
3451:
3452: if ( !p ) return 0;
3453: for ( m0 = 0, t = BDY(p); t; t = NEXT(t) ) {
3454: NEXTNM(m0,m);
3455: ndl_copy(DL(t),DL(m));
3456: CQ(m) = CQ(t);
3457: }
3458: if ( m0 ) NEXT(m) = 0;
3459: MKND(NV(p),m0,LEN(p),d);
3460: SG(d) = SG(p);
3461: return d;
3462: }
3463:
1.61 noro 3464: /* XXX if p->len == 0 then it represents 0 */
3465:
3466: void ndv_mod(int mod,NDV p)
3467: {
3468: NMV t,d;
3469: int r;
3470: int i,len,dlen;
1.71 noro 3471: Obj gfs;
1.61 noro 3472:
3473: if ( !p ) return;
3474: len = LEN(p);
3475: dlen = 0;
1.71 noro 3476: if ( mod == -1 )
3477: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
3478: simp_ff((Obj)CP(t),&gfs);
3479: r = FTOIF(CONT((GFS)gfs));
1.61 noro 3480: CM(d) = r;
3481: ndl_copy(DL(t),DL(d));
3482: NMV_ADV(d);
3483: dlen++;
3484: }
1.71 noro 3485: else
3486: for ( t = d = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
3487: r = rem(NM(CQ(t)),mod);
3488: if ( r ) {
3489: if ( SGN(CQ(t)) < 0 )
3490: r = mod-r;
3491: CM(d) = r;
3492: ndl_copy(DL(t),DL(d));
3493: NMV_ADV(d);
3494: dlen++;
3495: }
3496: }
1.61 noro 3497: LEN(p) = dlen;
3498: }
3499:
3500: NDV ptondv(VL vl,VL dvl,P p)
3501: {
3502: ND nd;
3503:
3504: nd = ptond(vl,dvl,p);
3505: return ndtondv(0,nd);
3506: }
3507:
3508: ND ptond(VL vl,VL dvl,P p)
1.23 noro 3509: {
1.61 noro 3510: int n,i,j,k,e;
3511: VL tvl;
3512: V v;
3513: DCP dc;
3514: DCP *w;
3515: ND r,s,t,u;
3516: P x;
3517: int c;
3518: UINT *d;
1.23 noro 3519: NM m,m0;
1.61 noro 3520:
3521: if ( !p )
3522: return 0;
3523: else if ( NUM(p) ) {
3524: NEWNM(m);
3525: ndl_zero(DL(m));
3526: CQ(m) = (Q)p;
3527: NEXT(m) = 0;
3528: MKND(nd_nvar,m,1,r);
3529: SG(r) = 0;
3530: return r;
3531: } else {
3532: for ( dc = DC(p), k = 0; dc; dc = NEXT(dc), k++ );
3533: w = (DCP *)ALLOCA(k*sizeof(DCP));
3534: for ( dc = DC(p), j = 0; j < k; dc = NEXT(dc), j++ ) w[j] = dc;
3535: for ( i = 0, tvl = dvl, v = VR(p);
3536: vl && tvl->v != v; tvl = NEXT(tvl), i++ );
3537: if ( !tvl ) {
3538: for ( j = k-1, s = 0, MKV(v,x); j >= 0; j-- ) {
3539: t = ptond(vl,dvl,COEF(w[j]));
3540: pwrp(vl,x,DEG(w[j]),&p);
3541: nd_mul_c_p(CO,t,p); s = nd_add(0,s,t);
3542: }
3543: return s;
3544: } else {
3545: NEWNM(m0); d = DL(m0);
3546: for ( j = k-1, s = 0; j >= 0; j-- ) {
3547: ndl_zero(d); e = QTOS(DEG(w[j])); PUT_EXP(d,i,e);
3548: TD(d) = MUL_WEIGHT(e,i);
3549: if ( nd_blockmask) ndl_weight_mask(d);
3550: t = ptond(vl,dvl,COEF(w[j]));
3551: for ( m = BDY(t); m; m = NEXT(m) )
3552: ndl_addto(DL(m),d);
3553: SG(t) += TD(d);
3554: s = nd_add(0,s,t);
3555: }
3556: FREENM(m0);
3557: return s;
3558: }
3559: }
3560: }
3561:
3562: P ndvtop(int mod,VL vl,VL dvl,NDV p)
3563: {
3564: VL tvl;
3565: int len,n,j,i,e;
3566: NMV m;
3567: Q q;
3568: P c;
3569: UINT *d;
3570: P s,r,u,t,w;
1.71 noro 3571: GFS gfs;
1.23 noro 3572:
1.34 noro 3573: if ( !p ) return 0;
1.61 noro 3574: else {
3575: len = LEN(p);
3576: n = NV(p);
3577: m = (NMV)(((char *)BDY(p))+nmv_adv*(len-1));
3578: for ( j = len-1, s = 0; j >= 0; j--, NMV_PREV(m) ) {
1.71 noro 3579: if ( mod == -1 ) {
3580: e = IFTOF(CM(m)); MKGFS(e,gfs); c = (P)gfs;
3581: } else if ( mod ) {
1.61 noro 3582: STOQ(CM(m),q); c = (P)q;
3583: } else
3584: c = CP(m);
3585: d = DL(m);
3586: for ( i = 0, t = c, tvl = dvl; i < n; tvl = NEXT(tvl), i++ ) {
3587: MKV(tvl->v,r); e = GET_EXP(d,i); STOQ(e,q);
3588: pwrp(vl,r,q,&u); mulp(vl,t,u,&w); t = w;
3589: }
3590: addp(vl,s,t,&u); s = u;
3591: }
3592: return s;
1.23 noro 3593: }
1.3 noro 3594: }
3595:
1.61 noro 3596: NDV ndtondv(int mod,ND p)
1.11 noro 3597: {
3598: NDV d;
1.61 noro 3599: NMV m,m0;
3600: NM t;
3601: int i,len;
1.11 noro 3602:
1.34 noro 3603: if ( !p ) return 0;
1.61 noro 3604: len = LEN(p);
1.74 noro 3605: if ( mod )
3606: m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(len*nmv_adv);
3607: else
3608: m0 = m = MALLOC(len*nmv_adv);
3609: ndv_alloc += nmv_adv*len;
1.61 noro 3610: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
3611: ndl_copy(DL(t),DL(m));
3612: CQ(m) = CQ(t);
1.11 noro 3613: }
1.61 noro 3614: MKNDV(NV(p),m0,len,d);
1.14 noro 3615: SG(d) = SG(p);
1.11 noro 3616: return d;
3617: }
3618:
1.61 noro 3619: ND ndvtond(int mod,NDV p)
1.11 noro 3620: {
1.61 noro 3621: ND d;
3622: NM m,m0;
1.11 noro 3623: NMV t;
1.61 noro 3624: int i,len;
1.11 noro 3625:
1.34 noro 3626: if ( !p ) return 0;
1.11 noro 3627: m0 = 0;
1.61 noro 3628: len = p->len;
3629: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
3630: NEXTNM(m0,m);
3631: ndl_copy(DL(t),DL(m));
3632: CQ(m) = CQ(t);
1.11 noro 3633: }
3634: NEXT(m) = 0;
1.61 noro 3635: MKND(NV(p),m0,len,d);
1.14 noro 3636: SG(d) = SG(p);
1.11 noro 3637: return d;
3638: }
3639:
1.3 noro 3640: void ndv_print(NDV p)
3641: {
3642: NMV m;
3643: int i,len;
3644:
1.34 noro 3645: if ( !p ) printf("0\n");
1.3 noro 3646: else {
1.14 noro 3647: len = LEN(p);
1.3 noro 3648: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.71 noro 3649: if ( CM(m) & 0x80000000 ) printf("+@_%d*",IFTOF(CM(m)));
3650: else printf("+%d*",CM(m));
1.16 noro 3651: ndl_print(DL(m));
3652: }
3653: printf("\n");
3654: }
3655: }
3656:
3657: void ndv_print_q(NDV p)
3658: {
3659: NMV m;
3660: int i,len;
3661:
1.34 noro 3662: if ( !p ) printf("0\n");
1.16 noro 3663: else {
3664: len = LEN(p);
3665: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
3666: printf("+");
1.74 noro 3667: printexpr(CO,(Obj)CQ(m));
1.16 noro 3668: printf("*");
1.14 noro 3669: ndl_print(DL(m));
1.3 noro 3670: }
3671: printf("\n");
3672: }
1.25 noro 3673: }
3674:
1.61 noro 3675: NODE ndv_reducebase(NODE x)
1.27 noro 3676: {
3677: int len,i,j;
3678: NDV *w;
3679: NODE t,t0;
3680:
3681: len = length(x);
3682: w = (NDV *)ALLOCA(len*sizeof(NDV));
3683: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
3684: for ( i = 0; i < len; i++ ) {
3685: for ( j = 0; j < i; j++ ) {
3686: if ( w[i] && w[j] )
3687: if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
3688: else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
3689: }
3690: }
3691: for ( i = len-1, t0 = 0; i >= 0; i-- ) {
3692: if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
3693: }
3694: NEXT(t) = 0; x = t0;
3695: return x;
1.11 noro 3696: }
1.32 noro 3697:
1.43 noro 3698: /* XXX incomplete */
3699:
1.32 noro 3700: void nd_init_ord(struct order_spec *ord)
3701: {
1.43 noro 3702: switch ( ord->id ) {
1.32 noro 3703: case 0:
1.43 noro 3704: switch ( ord->ord.simple ) {
3705: case 0:
3706: nd_dcomp = 1;
3707: nd_isrlex = 1;
3708: break;
3709: case 1:
3710: nd_dcomp = 1;
3711: nd_isrlex = 0;
3712: break;
3713: case 2:
3714: nd_dcomp = 0;
3715: nd_isrlex = 0;
1.45 noro 3716: ndl_compare_function = ndl_lex_compare;
1.58 noro 3717: break;
3718: case 11:
3719: /* XXX */
3720: nd_dcomp = 0;
3721: nd_isrlex = 1;
3722: ndl_compare_function = ndl_ww_lex_compare;
1.43 noro 3723: break;
3724: default:
3725: error("nd_gr : unsupported order");
3726: }
1.32 noro 3727: break;
3728: case 1:
1.43 noro 3729: /* XXX */
3730: nd_dcomp = -1;
1.32 noro 3731: nd_isrlex = 0;
1.45 noro 3732: ndl_compare_function = ndl_block_compare;
1.34 noro 3733: break;
1.43 noro 3734: case 2:
3735: error("nd_init_ord : matrix order is not supported yet.");
1.32 noro 3736: break;
3737: }
1.41 noro 3738: nd_ord = ord;
1.32 noro 3739: }
3740:
1.43 noro 3741: BlockMask nd_create_blockmask(struct order_spec *ord)
3742: {
3743: int n,i,j,s,l;
1.61 noro 3744: UINT *t;
1.43 noro 3745: BlockMask bm;
3746:
3747: if ( !ord->id )
3748: return 0;
3749: n = ord->ord.block.length;
3750: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
3751: bm->n = n;
3752: bm->order_pair = ord->ord.block.order_pair;
1.61 noro 3753: bm->mask = (UINT **)MALLOC(n*sizeof(UINT *));
1.43 noro 3754: for ( i = 0, s = 0; i < n; i++ ) {
1.61 noro 3755: bm->mask[i] = t = (UINT *)MALLOC_ATOMIC(nd_wpd*sizeof(UINT));
1.43 noro 3756: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
3757: l = bm->order_pair[i].length;
3758: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
3759: }
3760: return bm;
1.57 noro 3761: }
3762:
3763: EPOS nd_create_epos(struct order_spec *ord)
3764: {
3765: int i,j,l,s,ord_l,ord_o;
3766: EPOS epos;
3767: struct order_pair *op;
3768:
3769: epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
3770: switch ( ord->id ) {
3771: case 0:
3772: if ( nd_isrlex ) {
3773: for ( i = 0; i < nd_nvar; i++ ) {
3774: epos[i].i = nd_exporigin + (nd_nvar-1-i)/nd_epw;
3775: epos[i].s = (nd_epw-((nd_nvar-1-i)%nd_epw)-1)*nd_bpe;
3776: }
3777: } else {
3778: for ( i = 0; i < nd_nvar; i++ ) {
3779: epos[i].i = nd_exporigin + i/nd_epw;
3780: epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
3781: }
3782: }
3783: break;
3784: case 1:
3785: /* block order */
3786: l = ord->ord.block.length;
3787: op = ord->ord.block.order_pair;
3788: for ( j = 0, s = 0; j < l; j++ ) {
3789: ord_o = op[j].order;
3790: ord_l = op[j].length;
3791: if ( !ord_o )
3792: for ( i = 0; i < ord_l; i++ ) {
3793: epos[s+i].i = nd_exporigin + (s+ord_l-i-1)/nd_epw;
3794: epos[s+i].s = (nd_epw-((s+ord_l-i-1)%nd_epw)-1)*nd_bpe;
3795: }
3796: else
3797: for ( i = 0; i < ord_l; i++ ) {
3798: epos[s+i].i = nd_exporigin + (s+i)/nd_epw;
3799: epos[s+i].s = (nd_epw-((s+i)%nd_epw)-1)*nd_bpe;
3800: }
3801: s += ord_l;
3802: }
3803: break;
3804: case 2:
3805: error("nd_create_epos : matrix order is not supported yet.");
3806: }
3807: return epos;
1.43 noro 3808: }
1.59 noro 3809:
3810: /* external interface */
3811:
3812: void nd_nf_p(P f,LIST g,LIST v,int m,struct order_spec *ord,P *rp)
3813: {
1.61 noro 3814: NODE t,in0,in;
1.59 noro 3815: ND nd,nf;
1.61 noro 3816: NDV ndv;
3817: VL vv,tv;
3818: int stat,nvar,max,e;
1.69 noro 3819: union oNDC dn;
1.59 noro 3820:
3821: pltovl(v,&vv);
1.74 noro 3822: for ( nvar = 0, tv = vv; tv; tv = NEXT(tv), nvar++ );
1.61 noro 3823:
3824: /* get the degree bound */
3825: for ( t = BDY(g), max = 0; t; t = NEXT(t) )
3826: for ( tv = vv; tv; tv = NEXT(tv) ) {
3827: e = getdeg(tv->v,(P)BDY(t));
3828: max = MAX(e,max);
3829: }
3830: for ( tv = vv; tv; tv = NEXT(tv) ) {
3831: e = getdeg(tv->v,f);
3832: max = MAX(e,max);
3833: }
3834:
1.59 noro 3835: nd_init_ord(ord);
1.61 noro 3836: nd_setup_parameters(nvar,max);
3837:
3838: /* conversion to ndv */
3839: for ( in0 = 0, t = BDY(g); t; t = NEXT(t) ) {
3840: NEXTNODE(in0,in);
3841: BDY(in) = (pointer)ptondv(CO,vv,(P)BDY(t));
1.69 noro 3842: if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61 noro 3843: }
3844: NEXTNODE(in0,in);
3845: BDY(in) = (pointer)ptondv(CO,vv,f);
1.69 noro 3846: if ( m ) ndv_mod(m,(NDV)BDY(in));
1.61 noro 3847: NEXT(in) = 0;
3848:
3849: ndv_setup(m,0,in0);
1.59 noro 3850: nd_psn--;
3851: nd_scale=2;
3852: while ( 1 ) {
3853: nd = (pointer)ndvtond(m,nd_ps[nd_psn]);
1.69 noro 3854: stat = nd_nf(m,nd,nd_ps,1,0,&nf);
1.59 noro 3855: if ( !stat ) {
3856: nd_psn++;
3857: nd_reconstruct(m,0,0);
3858: nd_psn--;
3859: } else
3860: break;
3861: }
1.61 noro 3862: *rp = ndvtop(m,CO,vv,ndtondv(m,nf));
1.63 noro 3863: }
3864:
3865: int nd_to_vect(int mod,UINT *s0,int n,ND d,UINT *r)
3866: {
3867: NM m;
3868: UINT *t,*s;
3869: int i;
3870:
3871: for ( i = 0; i < n; i++ ) r[i] = 0;
3872: for ( i = 0, s = s0, m = BDY(d); m; m = NEXT(m) ) {
3873: t = DL(m);
3874: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
3875: r[i] = CM(m);
3876: }
3877: for ( i = 0; !r[i]; i++ );
3878: return i;
3879: }
3880:
1.74 noro 3881: int ndv_to_vect(int mod,UINT *s0,int n,NDV d,UINT *r)
3882: {
3883: NMV m;
3884: UINT *t,*s;
3885: int i,j,len;
3886:
3887: for ( i = 0; i < n; i++ ) r[i] = 0;
3888: m = BDY(d);
3889: len = LEN(d);
3890: for ( i = j = 0, s = s0; j < len; j++, NMV_ADV(m)) {
3891: t = DL(m);
3892: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
3893: r[i] = CM(m);
3894: }
3895: for ( i = 0; !r[i]; i++ );
3896: return i;
3897: }
3898:
1.63 noro 3899: int nm_ind_pair_to_vect(int mod,UINT *s0,int n,NM_ind_pair pair,UINT *r)
3900: {
3901: NM m;
3902: NMV mr;
3903: UINT *d,*t,*s;
3904: NDV p;
3905: int i,j,len;
3906:
3907: m = pair->mul;
3908: d = DL(m);
3909: p = nd_ps[pair->index];
3910: t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
3911: for ( i = 0; i < n; i++ ) r[i] = 0;
3912: len = LEN(p);
3913: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
3914: ndl_add(d,DL(mr),t);
3915: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
3916: r[i] = CM(mr);
3917: }
3918: for ( i = 0; !r[i]; i++ );
3919: return i;
3920: }
3921:
1.67 noro 3922: IndArray nm_ind_pair_to_vect_compress(int mod,UINT *s0,int n,NM_ind_pair pair)
1.64 noro 3923: {
3924: NM m;
3925: NMV mr;
3926: UINT *d,*t,*s;
3927: NDV p;
1.67 noro 3928: unsigned char *ivc;
3929: unsigned short *ivs;
1.81 noro 3930: UINT *v,*ivi,*s0v;
1.67 noro 3931: int i,j,len,prev,diff,cdiff;
3932: IndArray r;
1.64 noro 3933:
3934: m = pair->mul;
3935: d = DL(m);
3936: p = nd_ps[pair->index];
3937: len = LEN(p);
3938: t = (UINT *)ALLOCA(nd_wpd*sizeof(UINT));
1.67 noro 3939: v = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
1.64 noro 3940: for ( i = j = 0, s = s0, mr = BDY(p); j < len; j++, NMV_ADV(mr) ) {
3941: ndl_add(d,DL(mr),t);
3942: for ( ; !ndl_equal(t,s); s += nd_wpd, i++ );
1.67 noro 3943: v[j] = i;
3944: }
1.81 noro 3945: r = (IndArray)MALLOC(sizeof(struct oIndArray));
1.67 noro 3946: r->head = v[0];
3947: diff = 0;
3948: for ( i = 1; i < len; i++ ) {
3949: cdiff = v[i]-v[i-1]; diff = MAX(cdiff,diff);
3950: }
3951: if ( diff < 256 ) {
3952: r->width = 1;
3953: ivc = (unsigned char *)MALLOC_ATOMIC(len*sizeof(unsigned char));
3954: r->index.c = ivc;
3955: for ( i = 1, ivc[0] = 0; i < len; i++ ) ivc[i] = v[i]-v[i-1];
3956: } else if ( diff < 65536 ) {
3957: r->width = 2;
3958: ivs = (unsigned short *)MALLOC_ATOMIC(len*sizeof(unsigned short));
3959: r->index.s = ivs;
3960: for ( i = 1, ivs[0] = 0; i < len; i++ ) ivs[i] = v[i]-v[i-1];
3961: } else {
3962: r->width = 4;
3963: ivi = (unsigned int *)MALLOC_ATOMIC(len*sizeof(unsigned int));
3964: r->index.i = ivi;
3965: for ( i = 1, ivi[0] = 0; i < len; i++ ) ivi[i] = v[i]-v[i-1];
1.64 noro 3966: }
1.67 noro 3967: return r;
1.64 noro 3968: }
3969:
1.65 noro 3970:
1.76 noro 3971: int ndv_reduce_vect(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.65 noro 3972: {
1.67 noro 3973: int i,j,k,len,pos,prev;
1.66 noro 3974: UINT c,c1,c2,c3,up,lo,dmy;
1.67 noro 3975: IndArray ivect;
3976: unsigned char *ivc;
3977: unsigned short *ivs;
3978: unsigned int *ivi;
1.65 noro 3979: NDV redv;
1.67 noro 3980: NMV mr;
1.65 noro 3981: NODE rp;
1.76 noro 3982: int maxrs;
1.65 noro 3983:
1.76 noro 3984: maxrs = 0;
1.74 noro 3985: for ( i = 0; i < nred; i++ ) {
1.65 noro 3986: ivect = imat[i];
1.67 noro 3987: k = ivect->head; svect[k] %= m;
1.65 noro 3988: if ( c = svect[k] ) {
1.76 noro 3989: maxrs = MAX(maxrs,rp0[i]->sugar);
1.74 noro 3990: c = m-c; redv = nd_ps[rp0[i]->index];
1.67 noro 3991: len = LEN(redv); mr = BDY(redv);
3992: svect[k] = 0; prev = k;
3993: switch ( ivect->width ) {
3994: case 1:
3995: ivc = ivect->index.c;
3996: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
3997: pos = prev+ivc[j]; c1 = CM(mr); c2 = svect[pos];
3998: prev = pos;
3999: DMA(c1,c,c2,up,lo);
4000: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4001: } else svect[pos] = lo;
4002: }
4003: break;
4004: case 2:
4005: ivs = ivect->index.s;
4006: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4007: pos = prev+ivs[j]; c1 = CM(mr); c2 = svect[pos];
4008: prev = pos;
4009: DMA(c1,c,c2,up,lo);
4010: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4011: } else svect[pos] = lo;
4012: }
4013: break;
4014: case 4:
4015: ivi = ivect->index.i;
4016: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4017: pos = prev+ivi[j]; c1 = CM(mr); c2 = svect[pos];
4018: prev = pos;
4019: DMA(c1,c,c2,up,lo);
4020: if ( up ) { DSAB(m,up,lo,dmy,c3); svect[pos] = c3;
4021: } else svect[pos] = lo;
4022: }
4023: break;
1.65 noro 4024: }
4025: }
4026: }
1.66 noro 4027: for ( i = 0; i < col; i++ )
4028: if ( svect[i] >= (UINT)m ) svect[i] %= m;
1.76 noro 4029: return maxrs;
1.65 noro 4030: }
4031:
1.76 noro 4032: int ndv_reduce_vect_sf(int m,UINT *svect,int col,IndArray *imat,NM_ind_pair *rp0,int nred)
1.72 noro 4033: {
4034: int i,j,k,len,pos,prev;
4035: UINT c,c1,c2,c3,up,lo,dmy;
4036: IndArray ivect;
4037: unsigned char *ivc;
4038: unsigned short *ivs;
4039: unsigned int *ivi;
4040: NDV redv;
4041: NMV mr;
4042: NODE rp;
1.76 noro 4043: int maxrs;
1.72 noro 4044:
1.76 noro 4045: maxrs = 0;
1.74 noro 4046: for ( i = 0; i < nred; i++ ) {
1.72 noro 4047: ivect = imat[i];
4048: k = ivect->head; svect[k] %= m;
4049: if ( c = svect[k] ) {
1.76 noro 4050: maxrs = MAX(maxrs,rp0[i]->sugar);
1.74 noro 4051: c = _chsgnsf(c); redv = nd_ps[rp0[i]->index];
1.72 noro 4052: len = LEN(redv); mr = BDY(redv);
4053: svect[k] = 0; prev = k;
4054: switch ( ivect->width ) {
4055: case 1:
4056: ivc = ivect->index.c;
4057: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4058: pos = prev+ivc[j]; prev = pos;
4059: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4060: }
4061: break;
4062: case 2:
4063: ivs = ivect->index.s;
4064: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4065: pos = prev+ivs[j]; prev = pos;
4066: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4067: }
4068: break;
4069: case 4:
4070: ivi = ivect->index.i;
4071: for ( j = 1, NMV_ADV(mr); j < len; j++, NMV_ADV(mr) ) {
4072: pos = prev+ivi[j]; prev = pos;
4073: svect[pos] = _addsf(_mulsf(CM(mr),c),svect[pos]);
4074: }
4075: break;
4076: }
4077: }
4078: }
1.76 noro 4079: return maxrs;
1.72 noro 4080: }
4081:
1.65 noro 4082: NDV vect_to_ndv(UINT *vect,int spcol,int col,int *rhead,UINT *s0vect)
4083: {
4084: int j,k,len;
4085: UINT *p;
4086: UINT c;
4087: NDV r;
4088: NMV mr0,mr;
4089:
4090: for ( j = 0, len = 0; j < spcol; j++ ) if ( vect[j] ) len++;
4091: if ( !len ) return 0;
4092: else {
1.74 noro 4093: mr0 = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
4094: ndv_alloc += nmv_adv*len;
1.65 noro 4095: mr = mr0;
4096: p = s0vect;
4097: for ( j = k = 0; j < col; j++, p += nd_wpd )
4098: if ( !rhead[j] ) {
4099: if ( c = vect[k++] ) {
4100: ndl_copy(p,DL(mr)); CM(mr) = c; NMV_ADV(mr);
4101: }
4102: }
4103: MKNDV(nd_nvar,mr0,len,r);
4104: return r;
4105: }
4106: }
4107:
1.74 noro 4108: int nd_sp_f4(int m,ND_pairs l,PGeoBucket bucket)
1.65 noro 4109: {
4110: ND_pairs t;
4111: NODE sp0,sp;
4112: int stat;
4113: ND spol;
4114:
4115: for ( t = l; t; t = NEXT(t) ) {
4116: stat = nd_sp(m,0,t,&spol);
4117: if ( !stat ) return 0;
4118: if ( spol ) {
4119: add_pbucket_symbolic(bucket,spol);
4120: }
4121: }
1.68 noro 4122: return 1;
1.65 noro 4123: }
4124:
4125: int nd_symbolic_preproc(PGeoBucket bucket,UINT **s0vect,NODE *r)
4126: {
4127: NODE rp0,rp;
4128: NM mul,head,s0,s;
1.76 noro 4129: int index,col,i,sugar;
1.65 noro 4130: RHist h;
4131: UINT *s0v,*p;
4132: NM_ind_pair pair;
4133: ND red;
4134:
4135: s0 = 0; rp0 = 0; col = 0;
4136: while ( 1 ) {
4137: head = remove_head_pbucket_symbolic(bucket);
4138: if ( !head ) break;
4139: if ( !s0 ) s0 = head;
4140: else NEXT(s) = head;
4141: s = head;
4142: index = ndl_find_reducer(DL(head));
4143: if ( index >= 0 ) {
4144: h = nd_psh[index];
4145: NEWNM(mul);
4146: ndl_sub(DL(head),DL(h),DL(mul));
4147: if ( ndl_check_bound2(index,DL(mul)) ) return 0;
1.76 noro 4148: sugar = TD(DL(mul))+SG(nd_ps[index]);
4149: MKNM_ind_pair(pair,mul,index,sugar);
1.65 noro 4150: red = ndv_mul_nm_symbolic(mul,nd_ps[index]);
4151: add_pbucket_symbolic(bucket,nd_remove_head(red));
4152: NEXTNODE(rp0,rp); BDY(rp) = (pointer)pair;
4153: }
4154: col++;
4155: }
1.72 noro 4156: if ( rp0 ) NEXT(rp) = 0;
4157: NEXT(s) = 0;
1.65 noro 4158: s0v = (UINT *)MALLOC_ATOMIC(col*nd_wpd*sizeof(UINT));
4159: for ( i = 0, p = s0v, s = s0; i < col;
4160: i++, p += nd_wpd, s = NEXT(s) ) ndl_copy(DL(s),p);
4161: *s0vect = s0v;
4162: *r = rp0;
4163: return col;
4164: }
4165:
1.69 noro 4166: NODE nd_f4(int m)
4167: {
4168: int i,nh,stat,index;
4169: NODE r,g;
4170: ND_pairs d,l,t;
4171: ND spol,red;
4172: NDV nf,redv;
4173: NM s0,s;
1.84 noro 4174: NODE rp0,srp0,nflist;
1.69 noro 4175: int nsp,nred,col,rank,len,k,j,a;
4176: UINT c;
1.74 noro 4177: UINT **spmat;
1.69 noro 4178: UINT *s0vect,*svect,*p,*v;
4179: int *colstat;
4180: IndArray *imat;
4181: int *rhead;
4182: int spcol,sprow;
4183: int sugar;
4184: PGeoBucket bucket;
4185: struct oEGT eg0,eg1,eg_f4;
4186:
4187: if ( !m )
4188: error("nd_f4 : not implemented");
1.74 noro 4189: ndv_alloc = 0;
1.69 noro 4190: g = 0; d = 0;
4191: for ( i = 0; i < nd_psn; i++ ) {
4192: d = update_pairs(d,g,i);
4193: g = update_base(g,i);
4194: }
4195: while ( d ) {
4196: get_eg(&eg0);
4197: l = nd_minsugarp(d,&d);
4198: sugar = SG(l);
4199: bucket = create_pbucket();
1.74 noro 4200: stat = nd_sp_f4(m,l,bucket);
1.69 noro 4201: if ( !stat ) {
4202: for ( t = l; NEXT(t); t = NEXT(t) );
4203: NEXT(t) = d; d = l;
4204: d = nd_reconstruct(m,0,d);
4205: continue;
4206: }
1.86 noro 4207: if ( bucket->m < 0 ) continue;
1.69 noro 4208: col = nd_symbolic_preproc(bucket,&s0vect,&rp0);
4209: if ( !col ) {
4210: for ( t = l; NEXT(t); t = NEXT(t) );
4211: NEXT(t) = d; d = l;
4212: d = nd_reconstruct(m,0,d);
4213: continue;
4214: }
4215: get_eg(&eg1); init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg1);
1.74 noro 4216: if ( DP_Print )
4217: fprintf(asir_out,"sugar=%d,symb=%fsec,",
4218: sugar,eg_f4.exectime+eg_f4.gctime);
4219: if ( 1 )
4220: nflist = nd_f4_red(m,l,s0vect,col,rp0);
4221: else
4222: nflist = nd_f4_red_dist(m,l,s0vect,col,rp0);
1.69 noro 4223: /* adding new bases */
1.74 noro 4224: for ( r = nflist; r; r = NEXT(r) ) {
4225: nf = (NDV)BDY(r);
1.69 noro 4226: ndv_removecont(m,nf);
1.77 noro 4227: nh = ndv_newps(m,nf,0);
1.69 noro 4228: d = update_pairs(d,g,nh);
4229: g = update_base(g,nh);
4230: }
4231: }
4232: for ( r = g; r; r = NEXT(r) ) BDY(r) = (pointer)nd_ps[(int)BDY(r)];
1.74 noro 4233: fprintf(asir_out,"ndv_alloc=%d\n",ndv_alloc);
1.69 noro 4234: return g;
4235: }
1.74 noro 4236:
4237: NODE nd_f4_red(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
1.63 noro 4238: {
1.67 noro 4239: IndArray *imat;
1.74 noro 4240: int nsp,nred,spcol,sprow,a;
1.65 noro 4241: int *rhead;
1.74 noro 4242: int i,j,k,l,rank;
4243: NODE rp,r0,r;
4244: ND_pairs sp;
4245: ND spol;
4246: int **spmat;
4247: UINT *svect,*v;
4248: int *colstat;
1.80 noro 4249: struct oEGT eg0,eg1,eg2,eg_f4,eg_f4_1,eg_f4_2;
1.74 noro 4250: NM_ind_pair *rvect;
1.76 noro 4251: int maxrs;
4252: int *spsugar;
1.63 noro 4253:
1.74 noro 4254: get_eg(&eg0);
4255: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
4256: nred = length(rp0); spcol = col-nred;
4257: imat = (IndArray *)ALLOCA(nred*sizeof(IndArray));
4258: rhead = (int *)ALLOCA(col*sizeof(int));
4259: for ( i = 0; i < col; i++ ) rhead[i] = 0;
1.63 noro 4260:
1.74 noro 4261: /* construction of index arrays */
4262: rvect = (NM_ind_pair *)ALLOCA(nred*sizeof(NM_ind_pair));
4263: for ( rp = rp0, i = 0; rp; i++, rp = NEXT(rp) ) {
4264: rvect[i] = (NM_ind_pair)BDY(rp);
4265: imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rvect[i]);
4266: rhead[imat[i]->head] = 1;
4267: }
4268:
4269: /* elimination (1st step) */
4270: spmat = (int **)ALLOCA(nsp*sizeof(UINT *));
4271: svect = (UINT *)ALLOCA(col*sizeof(UINT));
1.76 noro 4272: spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74 noro 4273: for ( a = sprow = 0, sp = sp0; a < nsp; a++, sp = NEXT(sp) ) {
4274: nd_sp(m,0,sp,&spol);
1.75 noro 4275: if ( !spol ) continue;
1.74 noro 4276: nd_to_vect(m,s0vect,col,spol,svect);
1.76 noro 4277: if ( m == -1 )
4278: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rvect,nred);
4279: else
4280: maxrs = ndv_reduce_vect(m,svect,col,imat,rvect,nred);
1.74 noro 4281: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
4282: if ( i < col ) {
4283: spmat[sprow] = v = (UINT *)MALLOC_ATOMIC(spcol*sizeof(UINT));
4284: for ( j = k = 0; j < col; j++ )
4285: if ( !rhead[j] ) v[k++] = svect[j];
1.76 noro 4286: spsugar[sprow] = MAX(maxrs,SG(spol));
1.74 noro 4287: sprow++;
4288: }
1.76 noro 4289: nd_free(spol);
1.74 noro 4290: }
1.80 noro 4291: get_eg(&eg1); init_eg(&eg_f4_1); add_eg(&eg_f4_1,&eg0,&eg1);
4292: if ( DP_Print ) {
4293: fprintf(asir_out,"elim1=%fsec,",eg_f4_1.exectime+eg_f4_1.gctime);
4294: fflush(asir_out);
4295: }
1.74 noro 4296: /* free index arrays */
4297: for ( i = 0; i < nred; i++ ) GC_free(imat[i]->index.c);
4298:
4299: /* elimination (2nd step) */
4300: colstat = (int *)ALLOCA(spcol*sizeof(int));
4301: if ( m == -1 )
1.76 noro 4302: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4303: else
1.76 noro 4304: rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4305: r0 = 0;
4306: for ( i = 0; i < rank; i++ ) {
4307: NEXTNODE(r0,r); BDY(r) =
4308: (pointer)vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
1.76 noro 4309: SG((NDV)BDY(r)) = spsugar[i];
1.74 noro 4310: GC_free(spmat[i]);
4311: }
4312: for ( ; i < sprow; i++ ) GC_free(spmat[i]);
1.80 noro 4313: get_eg(&eg2); init_eg(&eg_f4_2); add_eg(&eg_f4_2,&eg1,&eg2);
4314: init_eg(&eg_f4); add_eg(&eg_f4,&eg0,&eg2);
1.74 noro 4315: if ( DP_Print ) {
1.80 noro 4316: fprintf(asir_out,"elim2=%fsec\n",eg_f4_2.exectime+eg_f4_2.gctime);
1.74 noro 4317: fprintf(asir_out,"nsp=%d,nred=%d,spmat=(%d,%d),rank=%d ",
4318: nsp,nred,sprow,spcol,rank);
4319: fprintf(asir_out,"%fsec\n",eg_f4.exectime+eg_f4.gctime);
1.63 noro 4320: }
1.74 noro 4321: return r0;
4322: }
4323:
4324: FILE *nd_write,*nd_read;
4325:
4326: void nd_send_int(int a) {
4327: write_int(nd_write,&a);
4328: }
4329:
4330: void nd_send_intarray(int *p,int len) {
4331: write_intarray(nd_write,p,len);
4332: }
4333:
4334: int nd_recv_int() {
4335: int a;
4336:
4337: read_int(nd_read,&a);
4338: return a;
4339: }
4340:
4341: void nd_recv_intarray(int *p,int len) {
4342: read_intarray(nd_read,p,len);
4343: }
4344:
4345: void nd_send_ndv(NDV p) {
4346: int len,i;
4347: NMV m;
4348:
4349: if ( !p ) nd_send_int(0);
4350: else {
4351: len = LEN(p);
4352: nd_send_int(len);
4353: m = BDY(p);
4354: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4355: nd_send_int(CM(m));
4356: nd_send_intarray(DL(m),nd_wpd);
1.68 noro 4357: }
1.74 noro 4358: }
4359: }
4360:
4361: void nd_send_nd(ND p) {
4362: int len,i;
4363: NM m;
4364:
4365: if ( !p ) nd_send_int(0);
4366: else {
4367: len = LEN(p);
4368: nd_send_int(len);
4369: m = BDY(p);
4370: for ( i = 0; i < len; i++, m = NEXT(m) ) {
4371: nd_send_int(CM(m));
4372: nd_send_intarray(DL(m),nd_wpd);
1.65 noro 4373: }
1.74 noro 4374: }
4375: }
1.65 noro 4376:
1.74 noro 4377: NDV nd_recv_ndv()
4378: {
4379: int len,i;
4380: NMV m,m0;
4381: NDV r;
1.65 noro 4382:
1.74 noro 4383: len = nd_recv_int();
4384: if ( !len ) return 0;
4385: else {
4386: m0 = m = (NMV)GC_malloc_atomic_ignore_off_page(nmv_adv*len);
4387: ndv_alloc += len*nmv_adv;
4388: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4389: CM(m) = nd_recv_int();
4390: nd_recv_intarray(DL(m),nd_wpd);
1.71 noro 4391: }
1.74 noro 4392: MKNDV(nd_nvar,m0,len,r);
4393: return r;
4394: }
4395: }
1.65 noro 4396:
1.74 noro 4397: int ox_exec_f4_red(Q proc)
4398: {
4399: Obj obj;
4400: STRING fname;
4401: NODE arg;
4402: int s;
4403: extern int ox_need_conv,ox_file_io;
4404:
4405: MKSTR(fname,"nd_exec_f4_red");
4406: arg = mknode(2,proc,fname);
4407: Pox_cmo_rpc(arg,&obj);
4408: s = get_ox_server_id(QTOS(proc));
4409: nd_write = iofp[s].out;
4410: nd_read = iofp[s].in;
4411: ox_need_conv = ox_file_io = 0;
4412: return s;
4413: }
4414:
4415: NODE nd_f4_red_dist(int m,ND_pairs sp0,UINT *s0vect,int col,NODE rp0)
4416: {
4417: int nsp,nred;
4418: int i,rank,s;
4419: NODE rp,r0,r;
4420: ND_pairs sp;
4421: NM_ind_pair pair;
4422: NMV nmv;
4423: NM nm;
4424: NDV nf;
4425: Obj proc,dmy;
4426:
4427: ox_launch_main(0,0,&proc);
4428: s = ox_exec_f4_red((Q)proc);
4429:
4430: nd_send_int(m);
4431: nd_send_int(nd_nvar);
4432: nd_send_int(nd_bpe);
4433: nd_send_int(nd_wpd);
4434: nd_send_int(nmv_adv);
4435:
1.89 ! noro 4436: saveobj(nd_write,dp_current_spec->obj); fflush(nd_write);
1.74 noro 4437:
4438: nd_send_int(nd_psn);
4439: for ( i = 0; i < nd_psn; i++ ) nd_send_ndv(nd_ps[i]);
4440:
4441: for ( sp = sp0, nsp = 0; sp; sp = NEXT(sp), nsp++ );
4442: nd_send_int(nsp);
4443: for ( i = 0, sp = sp0; i < nsp; i++, sp = NEXT(sp) ) {
4444: nd_send_int(sp->i1); nd_send_int(sp->i2);
4445: }
4446:
4447: nd_send_int(col); nd_send_intarray(s0vect,col*nd_wpd);
4448:
4449: nred = length(rp0); nd_send_int(nred);
4450: for ( i = 0, rp = rp0; i < nred; i++, rp = NEXT(rp) ) {
4451: pair = (NM_ind_pair)BDY(rp);
4452: nd_send_int(pair->index);
4453: nd_send_intarray(pair->mul->dl,nd_wpd);
4454: }
4455: fflush(nd_write);
4456: rank = nd_recv_int();
4457: fprintf(asir_out,"rank=%d\n",rank);
4458: r0 = 0;
4459: for ( i = 0; i < rank; i++ ) {
4460: nf = nd_recv_ndv();
4461: NEXTNODE(r0,r); BDY(r) = (pointer)nf;
4462: }
4463: Pox_shutdown(mknode(1,proc),&dmy);
4464: return r0;
4465: }
4466:
4467: /* server side */
4468:
4469: void nd_exec_f4_red_dist()
4470: {
4471: int m,i,nsp,col,s0size,nred,spcol,j,k;
4472: NM_ind_pair *rp0;
4473: NDV nf;
4474: UINT *s0vect;
4475: IndArray *imat;
4476: int *rhead;
4477: int **spmat;
4478: UINT *svect,*v;
4479: ND_pairs *sp0;
4480: int *colstat;
4481: int a,sprow,rank;
1.89 ! noro 4482: struct order_spec *ord;
1.74 noro 4483: Obj ordspec;
4484: ND spol;
1.76 noro 4485: int maxrs;
4486: int *spsugar;
1.74 noro 4487:
4488: nd_read = iofp[0].in;
4489: nd_write = iofp[0].out;
4490: m = nd_recv_int();
4491: nd_nvar = nd_recv_int();
4492: nd_bpe = nd_recv_int();
4493: nd_wpd = nd_recv_int();
4494: nmv_adv = nd_recv_int();
4495:
4496: loadobj(nd_read,&ordspec);
1.89 ! noro 4497: create_order_spec(0,ordspec,&ord);
! 4498: nd_init_ord(ord);
1.74 noro 4499: nd_setup_parameters(nd_nvar,0);
4500:
4501: nd_psn = nd_recv_int();
4502: nd_ps = (NDV *)MALLOC(nd_psn*sizeof(NDV));
4503: nd_bound = (UINT **)MALLOC(nd_psn*sizeof(UINT *));
4504: for ( i = 0; i < nd_psn; i++ ) {
4505: nd_ps[i] = nd_recv_ndv();
4506: nd_bound[i] = ndv_compute_bound(nd_ps[i]);
4507: }
4508:
4509: nsp = nd_recv_int();
4510: sp0 = (ND_pairs *)MALLOC(nsp*sizeof(ND_pairs));
4511: for ( i = 0; i < nsp; i++ ) {
4512: NEWND_pairs(sp0[i]);
4513: sp0[i]->i1 = nd_recv_int(); sp0[i]->i2 = nd_recv_int();
4514: ndl_lcm(HDL(nd_ps[sp0[i]->i1]),HDL(nd_ps[sp0[i]->i2]),LCM(sp0[i]));
4515: }
4516:
4517: col = nd_recv_int();
4518: s0size = col*nd_wpd;
4519: s0vect = (UINT *)MALLOC(s0size*sizeof(UINT));
4520: nd_recv_intarray(s0vect,s0size);
4521:
4522: nred = nd_recv_int();
4523: rp0 = (NM_ind_pair *)MALLOC(nred*sizeof(NM_ind_pair));
4524: for ( i = 0; i < nred; i++ ) {
4525: rp0[i] = (NM_ind_pair)MALLOC(sizeof(struct oNM_ind_pair));
4526: rp0[i]->index = nd_recv_int();
4527: rp0[i]->mul = (NM)MALLOC(sizeof(struct oNM)+(nd_wpd-1)*sizeof(UINT));
4528: nd_recv_intarray(rp0[i]->mul->dl,nd_wpd);
4529: }
4530:
4531: spcol = col-nred;
4532: imat = (IndArray *)MALLOC(nred*sizeof(IndArray));
4533: rhead = (int *)MALLOC(col*sizeof(int));
4534: for ( i = 0; i < col; i++ ) rhead[i] = 0;
4535:
4536: /* construction of index arrays */
4537: for ( i = 0; i < nred; i++ ) {
4538: imat[i] = nm_ind_pair_to_vect_compress(m,s0vect,col,rp0[i]);
4539: rhead[imat[i]->head] = 1;
4540: }
4541:
4542: /* elimination (1st step) */
4543: spmat = (int **)MALLOC(nsp*sizeof(UINT *));
4544: svect = (UINT *)MALLOC(col*sizeof(UINT));
1.76 noro 4545: spsugar = (int *)ALLOCA(nsp*sizeof(UINT));
1.74 noro 4546: for ( a = sprow = 0; a < nsp; a++ ) {
4547: nd_sp(m,0,sp0[a],&spol);
1.75 noro 4548: if ( !spol ) continue;
1.74 noro 4549: nd_to_vect(m,s0vect,col,spol,svect);
1.76 noro 4550: if ( m == -1 )
4551: maxrs = ndv_reduce_vect_sf(m,svect,col,imat,rp0,nred);
4552: else
4553: maxrs = ndv_reduce_vect(m,svect,col,imat,rp0,nred);
1.74 noro 4554: for ( i = 0; i < col; i++ ) if ( svect[i] ) break;
4555: if ( i < col ) {
4556: spmat[sprow] = v = (UINT *)MALLOC(spcol*sizeof(UINT));
4557: for ( j = k = 0; j < col; j++ )
4558: if ( !rhead[j] ) v[k++] = svect[j];
1.76 noro 4559: spsugar[sprow] = MAX(maxrs,SG(spol));
1.74 noro 4560: sprow++;
4561: }
1.76 noro 4562: nd_free(spol);
1.74 noro 4563: }
4564: /* elimination (2nd step) */
4565: colstat = (int *)ALLOCA(spcol*sizeof(int));
4566: if ( m == -1 )
1.76 noro 4567: rank = nd_gauss_elim_sf(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4568: else
1.76 noro 4569: rank = nd_gauss_elim_mod(spmat,spsugar,sprow,spcol,m,colstat);
1.74 noro 4570: nd_send_int(rank);
4571: for ( i = 0; i < rank; i++ ) {
4572: nf = vect_to_ndv(spmat[i],spcol,col,rhead,s0vect);
4573: nd_send_ndv(nf);
1.63 noro 4574: }
1.74 noro 4575: fflush(nd_write);
1.76 noro 4576: }
4577:
4578: int nd_gauss_elim_mod(int **mat0,int *sugar,int row,int col,int md,int *colstat)
4579: {
4580: int i,j,k,l,inv,a,rank,s;
4581: unsigned int *t,*pivot,*pk;
4582: unsigned int **mat;
4583:
4584: mat = (unsigned int **)mat0;
4585: for ( rank = 0, j = 0; j < col; j++ ) {
4586: for ( i = rank; i < row; i++ )
4587: mat[i][j] %= md;
4588: for ( i = rank; i < row; i++ )
4589: if ( mat[i][j] )
4590: break;
4591: if ( i == row ) {
4592: colstat[j] = 0;
4593: continue;
4594: } else
4595: colstat[j] = 1;
4596: if ( i != rank ) {
4597: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
4598: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
4599: }
4600: pivot = mat[rank];
4601: s = sugar[rank];
4602: inv = invm(pivot[j],md);
4603: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
4604: if ( *pk ) {
4605: if ( *pk >= (unsigned int)md )
4606: *pk %= md;
4607: DMAR(*pk,inv,0,md,*pk)
4608: }
4609: for ( i = rank+1; i < row; i++ ) {
4610: t = mat[i];
4611: if ( a = t[j] ) {
4612: sugar[i] = MAX(sugar[i],s);
4613: red_by_vect(md,t+j,pivot+j,md-a,col-j);
4614: }
4615: }
4616: rank++;
4617: }
4618: for ( j = col-1, l = rank-1; j >= 0; j-- )
4619: if ( colstat[j] ) {
4620: pivot = mat[l];
4621: s = sugar[l];
4622: for ( i = 0; i < l; i++ ) {
4623: t = mat[i];
4624: t[j] %= md;
4625: if ( a = t[j] ) {
4626: sugar[i] = MAX(sugar[i],s);
4627: red_by_vect(md,t+j,pivot+j,md-a,col-j);
4628: }
4629: }
4630: l--;
4631: }
4632: for ( j = 0, l = 0; l < rank; j++ )
4633: if ( colstat[j] ) {
4634: t = mat[l];
4635: for ( k = j; k < col; k++ )
4636: if ( t[k] >= (unsigned int)md )
4637: t[k] %= md;
4638: l++;
4639: }
4640: return rank;
4641: }
4642:
4643: int nd_gauss_elim_sf(int **mat0,int *sugar,int row,int col,int md,int *colstat)
4644: {
4645: int i,j,k,l,inv,a,rank,s;
4646: unsigned int *t,*pivot,*pk;
4647: unsigned int **mat;
4648:
4649: mat = (unsigned int **)mat0;
4650: for ( rank = 0, j = 0; j < col; j++ ) {
4651: for ( i = rank; i < row; i++ )
4652: if ( mat[i][j] )
4653: break;
4654: if ( i == row ) {
4655: colstat[j] = 0;
4656: continue;
4657: } else
4658: colstat[j] = 1;
4659: if ( i != rank ) {
4660: t = mat[i]; mat[i] = mat[rank]; mat[rank] = t;
4661: s = sugar[i]; sugar[i] = sugar[rank]; sugar[rank] = s;
4662: }
4663: pivot = mat[rank];
4664: s = sugar[rank];
4665: inv = _invsf(pivot[j]);
4666: for ( k = j, pk = pivot+k; k < col; k++, pk++ )
4667: if ( *pk )
4668: *pk = _mulsf(*pk,inv);
4669: for ( i = rank+1; i < row; i++ ) {
4670: t = mat[i];
4671: if ( a = t[j] ) {
4672: sugar[i] = MAX(sugar[i],s);
4673: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
4674: }
4675: }
4676: rank++;
4677: }
4678: for ( j = col-1, l = rank-1; j >= 0; j-- )
4679: if ( colstat[j] ) {
4680: pivot = mat[l];
4681: s = sugar[l];
4682: for ( i = 0; i < l; i++ ) {
4683: t = mat[i];
4684: if ( a = t[j] ) {
4685: sugar[i] = MAX(sugar[i],s);
4686: red_by_vect_sf(md,t+j,pivot+j,_chsgnsf(a),col-j);
4687: }
4688: }
4689: l--;
4690: }
4691: return rank;
1.77 noro 4692: }
4693:
4694: int ndv_ishomo(NDV p)
4695: {
4696: NMV m;
4697: int len,h;
4698:
4699: if ( !p ) return 1;
4700: len = LEN(p);
4701: m = BDY(p);
4702: h = TD(DL(m));
4703: NMV_ADV(m);
4704: for ( len--; len; len--, NMV_ADV(m) )
4705: if ( TD(DL(m)) != h ) return 0;
4706: return 1;
4707: }
4708:
4709: void ndv_save(NDV p,int index)
4710: {
4711: FILE *s;
4712: char name[BUFSIZ];
4713: short id;
4714: int nv,sugar,len,n,i,td,e,j;
4715: NMV m;
4716: unsigned int *dl;
4717:
4718: sprintf(name,"%s/%d",Demand,index);
4719: s = fopen(name,"w");
4720: savevl(s,0);
4721: if ( !p ) {
4722: saveobj(s,0);
4723: return;
4724: }
4725: id = O_DP;
4726: nv = NV(p);
4727: sugar = SG(p);
4728: len = LEN(p);
4729: write_short(s,&id); write_int(s,&nv); write_int(s,&sugar);
4730: write_int(s,&len);
4731:
4732: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
4733: saveobj(s,(Obj)CQ(m));
4734: dl = DL(m);
4735: td = TD(dl);
4736: write_int(s,&td);
4737: for ( j = 0; j < nv; j++ ) {
4738: e = GET_EXP(dl,j);
4739: write_int(s,&e);
4740: }
4741: }
4742: fclose(s);
4743: }
4744:
4745: NDV ndv_load(int index)
4746: {
4747: FILE *s;
4748: char name[BUFSIZ];
4749: short id;
4750: int nv,sugar,len,n,i,td,e,j;
4751: NDV d;
4752: NMV m0,m;
4753: unsigned int *dl;
4754: Obj obj;
4755:
4756: sprintf(name,"%s/%d",Demand,index);
4757: s = fopen(name,"r");
4758: if ( !s ) return 0;
4759:
4760: skipvl(s);
4761: read_short(s,&id);
4762: if ( !id ) return 0;
4763: read_int(s,&nv);
4764: read_int(s,&sugar);
4765: read_int(s,&len);
4766:
4767: m0 = m = MALLOC(len*nmv_adv);
4768: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
4769: loadobj(s,&obj); CQ(m) = (Q)obj;
4770: dl = DL(m);
4771: ndl_zero(dl);
4772: read_int(s,&td); TD(dl) = td;
4773: for ( j = 0; j < nv; j++ ) {
4774: read_int(s,&e);
4775: PUT_EXP(dl,j,e);
4776: }
4777: if ( nd_blockmask ) ndl_weight_mask(dl);
4778: }
4779: fclose(s);
4780: MKNDV(nv,m0,len,d);
4781: SG(d) = sugar;
4782: return d;
1.59 noro 4783: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>