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