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