Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.46
1.46 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.45 2003/08/25 08:57:31 noro Exp $ */
1.2 noro 2:
1.1 noro 3: #include "ca.h"
4: #include "inline.h"
5:
6: #if defined(__GNUC__)
7: #define INLINE inline
8: #elif defined(VISUAL)
9: #define INLINE __inline
10: #else
11: #define INLINE
12: #endif
13:
1.46 ! noro 14: #define USE_GEOBUCKET 0
1.28 noro 15:
1.1 noro 16: #define REDTAB_LEN 32003
17:
1.40 noro 18: /* GeoBucket for polynomial addition */
19:
1.1 noro 20: typedef struct oPGeoBucket {
21: int m;
22: struct oND *body[32];
23: } *PGeoBucket;
24:
1.40 noro 25: /* distributed polynomial; linked list rep. */
1.1 noro 26: typedef struct oND {
27: struct oNM *body;
28: int nv;
1.31 noro 29: int len;
1.1 noro 30: int sugar;
31: } *ND;
32:
1.40 noro 33: /* distributed polynomial; array rep. */
1.3 noro 34: typedef struct oNDV {
35: struct oNMV *body;
36: int nv;
1.31 noro 37: int len;
1.3 noro 38: int sugar;
39: } *NDV;
40:
1.40 noro 41: /* monomial; linked list rep. */
1.1 noro 42: typedef struct oNM {
43: struct oNM *next;
1.14 noro 44: union {
45: int m;
46: Q z;
47: } c;
1.1 noro 48: unsigned int dl[1];
49: } *NM;
50:
1.40 noro 51: /* monomial; array rep. */
1.3 noro 52: typedef struct oNMV {
1.14 noro 53: union {
54: int m;
55: Q z;
56: } c;
1.3 noro 57: unsigned int dl[1];
58: } *NMV;
59:
1.40 noro 60: /* history of reducer */
1.13 noro 61: typedef struct oRHist {
62: struct oRHist *next;
63: int index;
1.34 noro 64: int sugar;
1.13 noro 65: unsigned int dl[1];
66: } *RHist;
67:
1.40 noro 68: /* S-pair list */
1.1 noro 69: typedef struct oND_pairs {
70: struct oND_pairs *next;
71: int i1,i2;
1.34 noro 72: int sugar;
1.1 noro 73: unsigned int lcm[1];
74: } *ND_pairs;
75:
1.42 noro 76: /* index and shift count for each exponent */
77: typedef struct oEPOS {
78: int i; /* index */
79: int s; /* shift */
80: } *EPOS;
81:
1.43 noro 82: typedef struct oBlockMask {
83: int n;
84: struct order_pair *order_pair;
85: unsigned int **mask;
86: } *BlockMask;
87:
1.45 noro 88: typedef struct oBaseSet {
89: int len;
90: NDV *ps;
91: unsigned int **bound;
92: } *BaseSet;
93:
94: int (*ndl_compare_function)(unsigned int *a1,unsigned int *a2);
1.32 noro 95:
1.42 noro 96: static double nd_scale=2;
1.1 noro 97: static unsigned int **nd_bound;
1.42 noro 98: static struct order_spec *nd_ord;
99: static EPOS nd_epos;
1.43 noro 100: static BlockMask nd_blockmask;
1.42 noro 101: static int nd_nvar;
102: static int nd_isrlex;
103: static int nd_epw,nd_bpe,nd_wpd,nd_exporigin;
104: static unsigned int nd_mask[32];
105: static unsigned int nd_mask0,nd_mask1;
106:
107: static NM _nm_free_list;
108: static ND _nd_free_list;
109: static ND_pairs _ndp_free_list;
1.20 noro 110:
111: static NDV *nd_ps;
112: static NDV *nd_psq;
1.42 noro 113: static RHist *nd_psh;
114: static int nd_psn,nd_pslen;
1.20 noro 115:
1.42 noro 116: static RHist *nd_red;
1.1 noro 117:
1.42 noro 118: static int nd_found,nd_create,nd_notfirst;
119: static int nm_adv;
120: static int nmv_adv;
121: static int nd_dcomp;
1.1 noro 122:
1.34 noro 123: extern int Top,Reverse,dp_nelim;
1.1 noro 124:
1.40 noro 125: /* fundamental macros */
1.34 noro 126: #define TD(d) (d[0])
1.1 noro 127: #define HDL(d) ((d)->body->dl)
1.34 noro 128: #define HTD(d) (TD(HDL(d)))
1.14 noro 129: #define HCM(d) ((d)->body->c.m)
1.16 noro 130: #define HCQ(d) ((d)->body->c.z)
1.14 noro 131: #define CM(a) ((a)->c.m)
1.16 noro 132: #define CQ(a) ((a)->c.z)
1.14 noro 133: #define DL(a) ((a)->dl)
134: #define SG(a) ((a)->sugar)
135: #define LEN(a) ((a)->len)
1.33 noro 136: #define LCM(a) ((a)->lcm)
1.42 noro 137: #define GET_EXP(d,a) (((d)[nd_epos[a].i]>>nd_epos[a].s)&nd_mask0)
138: #define PUT_EXP(r,a,e) ((r)[nd_epos[a].i] |= ((e)<<nd_epos[a].s))
1.45 noro 139: #define XOR_EXP(r,a,e) ((r)[nd_epos[a].i] ^= ((e)<<nd_epos[a].s))
1.1 noro 140:
1.40 noro 141: /* macros for term comparison */
1.34 noro 142: #define TD_DL_COMPARE(d1,d2)\
1.41 noro 143: (TD(d1)>TD(d2)?1:(TD(d1)<TD(d2)?-1:ndl_lex_compare(d1,d2)))
1.43 noro 144: #if 0
1.34 noro 145: #define DL_COMPARE(d1,d2)\
1.43 noro 146: (nd_dcomp>0?TD_DL_COMPARE(d1,d2)\
147: :(nd_dcomp==0?ndl_lex_compare(d1,d2)\
148: :(nd_blockmask?ndl_block_compare(d1,d2)\
1.45 noro 149: :(*ndl_compare_function)(d1,d2))))
1.43 noro 150: #else
151: #define DL_COMPARE(d1,d2)\
1.45 noro 152: (nd_dcomp>0?TD_DL_COMPARE(d1,d2):(*ndl_compare_function)(d1,d2))
1.43 noro 153: #endif
1.34 noro 154:
1.40 noro 155: /* allocators */
1.15 noro 156: #define NEWRHist(r) \
1.41 noro 157: ((r)=(RHist)MALLOC(sizeof(struct oRHist)+(nd_wpd-1)*sizeof(unsigned int)))
1.34 noro 158: #define NEWND_pairs(m) \
159: if(!_ndp_free_list)_NDP_alloc();\
160: (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
161: #define NEWNM(m)\
162: if(!_nm_free_list)_NM_alloc();\
163: (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
164: #define MKND(n,m,len,d)\
165: if(!_nd_free_list)_ND_alloc();\
166: (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list);\
167: NV(d)=(n); LEN(d)=(len); BDY(d)=(m)
1.40 noro 168: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
169: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.1 noro 170:
1.40 noro 171: /* allocate and link a new object */
1.13 noro 172: #define NEXTRHist(r,c) \
173: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1 noro 174: #define NEXTNM(r,c) \
175: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
176: #define NEXTNM2(r,c,s) \
177: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
1.40 noro 178: #define NEXTND_pairs(r,c) \
179: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
1.34 noro 180:
1.40 noro 181: /* deallocators */
1.1 noro 182: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
183: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
184: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
185:
1.40 noro 186: /* macro for increasing pointer to NMV */
187: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
188:
189: /* external functions */
190: void GC_gcollect();
191: NODE append_one(NODE,int);
1.1 noro 192:
1.40 noro 193: /* manipulation of coefficients */
1.20 noro 194: void nd_removecont(int mod,ND p);
1.21 noro 195: void nd_removecont2(ND p1,ND p2);
1.40 noro 196: void removecont_array(Q *c,int n);
197:
198: /* GeoBucket functions */
1.25 noro 199: ND normalize_pbucket(int mod,PGeoBucket g);
200: int head_pbucket(int mod,PGeoBucket g);
1.26 noro 201: int head_pbucket_q(PGeoBucket g);
1.31 noro 202: void add_pbucket(int mod,PGeoBucket g,ND d);
1.25 noro 203: void free_pbucket(PGeoBucket b);
1.26 noro 204: void mulq_pbucket(PGeoBucket g,Q c);
1.25 noro 205: PGeoBucket create_pbucket();
1.20 noro 206:
1.40 noro 207: /* manipulation of pairs and bases */
1.39 noro 208: int nd_newps(int mod,ND a,ND aq);
1.40 noro 209: ND_pairs nd_newpairs( NODE g, int t );
1.1 noro 210: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
211: NODE update_base(NODE nd,int ndp);
1.40 noro 212: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
213: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
214: ND_pairs crit_B( ND_pairs d, int s );
215: ND_pairs crit_M( ND_pairs d1 );
216: ND_pairs crit_F( ND_pairs d1 );
1.1 noro 217: int crit_2( int dp1, int dp2 );
1.40 noro 218:
219: /* top level functions */
220: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp);
221: void nd_gr_trace(LIST f,LIST v,int m,int homo,struct order_spec *ord,LIST *rp);
1.27 noro 222: NODE nd_gb(int m,int checkonly);
1.23 noro 223: NODE nd_gb_trace(int m);
1.40 noro 224:
225: /* ndl functions */
1.39 noro 226: int ndl_weight(unsigned int *d);
1.43 noro 227: int ndl_weight_mask(unsigned int *d,int i);
1.34 noro 228: void ndl_dehomogenize(unsigned int *p);
1.43 noro 229: void ndl_reconstruct(int obpe,EPOS oepos,unsigned int *d,unsigned int *r);
1.40 noro 230: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2);
231: INLINE int ndl_lex_compare(unsigned int *d1,unsigned int *d2);
1.43 noro 232: INLINE int ndl_block_compare(unsigned int *d1,unsigned int *d2);
1.40 noro 233: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2);
234: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2);
235: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d);
236: INLINE void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d);
237: INLINE int ndl_hash_value(unsigned int *d);
1.45 noro 238:
239: /* normal forms */
1.40 noro 240: INLINE int nd_find_reducer(ND g);
241: INLINE int nd_find_reducer_direct(ND g,NDV *ps,int len);
1.16 noro 242: int nd_sp(int mod,ND_pairs p,ND *nf);
243: int nd_nf(int mod,ND g,int full,ND *nf);
1.30 noro 244: int nd_nf_pbucket(int mod,ND g,int full,ND *nf);
1.45 noro 245: int nd_nf_direct(int mod,ND g,BaseSet base,int full,ND *rp);
1.40 noro 246:
247: /* finalizers */
248: NODE nd_reducebase(NODE x);
1.23 noro 249: NODE nd_reduceall(int m,NODE f);
1.27 noro 250: int nd_gbcheck(int m,NODE f);
251: int nd_membercheck(int m,NODE f);
1.40 noro 252:
253: /* allocators */
254: void nd_free_private_storage();
255: void _NM_alloc();
256: void _ND_alloc();
1.1 noro 257: void nd_free(ND p);
1.40 noro 258: void nd_free_redlist();
259:
260: /* printing */
1.1 noro 261: void ndl_print(unsigned int *dl);
262: void nd_print(ND p);
1.16 noro 263: void nd_print_q(ND p);
1.1 noro 264: void ndp_print(ND_pairs d);
1.40 noro 265:
266:
267: /* setup, reconstruct */
268: void nd_init_ord(struct order_spec *spec);
269: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs ndp);
270: void nd_reconstruct_direct(int mod,NDV *ps,int len);
271: void nd_setup(int mod,int trace,NODE f);
272: void nd_setup_parameters();
1.43 noro 273: BlockMask nd_create_blockmask(struct order_spec *ord);
1.40 noro 274:
275: /* ND functions */
276: int nd_check_candidate(NODE input,NODE cand);
277: void nd_mul_c(int mod,ND p,int mul);
278: void nd_mul_c_q(ND p,Q mul);
279: ND nd_remove_head(ND p);
1.1 noro 280: int nd_length(ND p);
1.34 noro 281: void nd_append_red(unsigned int *d,int i);
1.45 noro 282: unsigned int *ndv_compute_bound(NDV p);
1.5 noro 283: unsigned int *dp_compute_bound(DP p);
1.6 noro 284: ND nd_copy(ND p);
1.40 noro 285: ND nd_add(int mod,ND p1,ND p2);
286: ND nd_add_q(ND p1,ND p2);
1.41 noro 287: INLINE int nd_length(ND p);
1.4 noro 288:
1.40 noro 289: /* NDV functions */
1.19 noro 290: void ndv_mul_c(int mod,NDV p,int mul);
1.40 noro 291: void ndv_mul_c_q(NDV p,Q mul);
1.43 noro 292: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos);
1.40 noro 293: ND ndv_mul_nm(int mod,NDV p,NM m0);
1.45 noro 294: void ndv_dehomogenize(NDV p,struct order_spec *spec);
1.40 noro 295: void ndv_removecont(int mod,NDV p);
296: void ndv_print(NDV p);
297: void ndv_print_q(NDV p);
298: void ndv_free(NDV p);
299:
300: /* converters */
1.16 noro 301: NDV ndtondv(int mod,ND p);
1.23 noro 302: ND ndvtond(int mod,NDV p);
1.16 noro 303: NDV dptondv(int,DP);
304: DP ndvtodp(int,NDV);
1.17 noro 305: ND dptond(int,DP);
306: DP ndtodp(int,ND);
1.1 noro 307:
308: void nd_free_private_storage()
309: {
310: _nd_free_list = 0;
311: _nm_free_list = 0;
1.5 noro 312: _ndp_free_list = 0;
1.13 noro 313: bzero(nd_red,sizeof(REDTAB_LEN*sizeof(RHist)));
1.1 noro 314: GC_gcollect();
315: }
316:
317: void _NM_alloc()
318: {
319: NM p;
320: int i;
321:
1.11 noro 322: for ( i = 0; i < 1024; i++ ) {
1.41 noro 323: p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.1 noro 324: p->next = _nm_free_list; _nm_free_list = p;
325: }
326: }
327:
328: void _ND_alloc()
329: {
330: ND p;
331: int i;
332:
333: for ( i = 0; i < 1024; i++ ) {
334: p = (ND)GC_malloc(sizeof(struct oND));
335: p->body = (NM)_nd_free_list; _nd_free_list = p;
336: }
337: }
338:
339: void _NDP_alloc()
340: {
341: ND_pairs p;
342: int i;
343:
1.11 noro 344: for ( i = 0; i < 1024; i++ ) {
1.1 noro 345: p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
1.41 noro 346: +(nd_wpd-1)*sizeof(unsigned int));
1.1 noro 347: p->next = _ndp_free_list; _ndp_free_list = p;
348: }
349: }
350:
1.30 noro 351: INLINE int nd_length(ND p)
1.1 noro 352: {
353: NM m;
354: int i;
355:
356: if ( !p )
357: return 0;
358: else {
359: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
360: return i;
361: }
362: }
363:
1.35 noro 364: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2)
1.1 noro 365: {
366: unsigned int u1,u2;
367: int i,j;
368:
1.34 noro 369: if ( TD(d1) < TD(d2) ) return 0;
1.1 noro 370: switch ( nd_bpe ) {
371: case 4:
1.41 noro 372: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 373: u1 = d1[i]; u2 = d2[i];
374: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
375: if ( (u1&0xf000000) < (u2&0xf000000) ) return 0;
376: if ( (u1&0xf00000) < (u2&0xf00000) ) return 0;
377: if ( (u1&0xf0000) < (u2&0xf0000) ) return 0;
378: if ( (u1&0xf000) < (u2&0xf000) ) return 0;
379: if ( (u1&0xf00) < (u2&0xf00) ) return 0;
380: if ( (u1&0xf0) < (u2&0xf0) ) return 0;
381: if ( (u1&0xf) < (u2&0xf) ) return 0;
382: }
383: return 1;
384: break;
385: case 6:
1.41 noro 386: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 387: u1 = d1[i]; u2 = d2[i];
388: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
389: if ( (u1&0xfc0000) < (u2&0xfc0000) ) return 0;
390: if ( (u1&0x3f000) < (u2&0x3f000) ) return 0;
391: if ( (u1&0xfc0) < (u2&0xfc0) ) return 0;
392: if ( (u1&0x3f) < (u2&0x3f) ) return 0;
393: }
394: return 1;
395: break;
396: case 8:
1.41 noro 397: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 398: u1 = d1[i]; u2 = d2[i];
399: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
400: if ( (u1&0xff0000) < (u2&0xff0000) ) return 0;
401: if ( (u1&0xff00) < (u2&0xff00) ) return 0;
402: if ( (u1&0xff) < (u2&0xff) ) return 0;
403: }
404: return 1;
405: break;
406: case 16:
1.41 noro 407: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 408: u1 = d1[i]; u2 = d2[i];
409: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
410: if ( (u1&0xffff) < (u2&0xffff) ) return 0;
411: }
412: return 1;
413: break;
414: case 32:
1.41 noro 415: for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1 noro 416: if ( d1[i] < d2[i] ) return 0;
417: return 1;
418: break;
419: default:
1.41 noro 420: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 421: u1 = d1[i]; u2 = d2[i];
422: for ( j = 0; j < nd_epw; j++ )
423: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
424: }
425: return 1;
426: }
427: }
428:
1.34 noro 429: void ndl_dehomogenize(unsigned int *d)
1.23 noro 430: {
431: unsigned int mask;
432: unsigned int h;
1.31 noro 433: int i,bits;
1.23 noro 434:
1.44 noro 435: if ( nd_blockmask ) {
436: h = GET_EXP(d,nd_nvar-1);
1.45 noro 437: XOR_EXP(d,nd_nvar-1,h);
1.44 noro 438: TD(d) -= h;
439: d[nd_exporigin-1] -= h;
440: } else {
441: if ( nd_isrlex ) {
442: if ( nd_bpe == 32 ) {
443: h = d[nd_exporigin];
444: for ( i = nd_exporigin+1; i < nd_wpd; i++ )
445: d[i-1] = d[i];
446: d[i-1] = 0;
447: TD(d) -= h;
448: } else {
449: bits = nd_epw*nd_bpe;
450: mask = bits==32?0xffffffff:((1<<(nd_epw*nd_bpe))-1);
451: h = (d[nd_exporigin]>>((nd_epw-1)*nd_bpe))&nd_mask0;
452: for ( i = nd_exporigin; i < nd_wpd; i++ )
453: d[i] = ((d[i]<<nd_bpe)&mask)
454: |(i+1<nd_wpd?((d[i+1]>>((nd_epw-1)*nd_bpe))&nd_mask0):0);
455: TD(d) -= h;
456: }
1.45 noro 457: } else {
458: h = GET_EXP(d,nd_nvar-1);
459: XOR_EXP(d,nd_nvar-1,h);
460: TD(d) -= h;
461: }
1.44 noro 462: }
1.23 noro 463: }
464:
1.1 noro 465: void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d)
466: {
467: unsigned int t1,t2,u,u1,u2;
1.43 noro 468: int i,j,l;
1.1 noro 469:
470: switch ( nd_bpe ) {
471: case 4:
1.41 noro 472: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 473: u1 = d1[i]; u2 = d2[i];
474: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
475: t1 = (u1&0xf000000); t2 = (u2&0xf000000); u |= t1>t2?t1:t2;
476: t1 = (u1&0xf00000); t2 = (u2&0xf00000); u |= t1>t2?t1:t2;
477: t1 = (u1&0xf0000); t2 = (u2&0xf0000); u |= t1>t2?t1:t2;
478: t1 = (u1&0xf000); t2 = (u2&0xf000); u |= t1>t2?t1:t2;
479: t1 = (u1&0xf00); t2 = (u2&0xf00); u |= t1>t2?t1:t2;
480: t1 = (u1&0xf0); t2 = (u2&0xf0); u |= t1>t2?t1:t2;
481: t1 = (u1&0xf); t2 = (u2&0xf); u |= t1>t2?t1:t2;
482: d[i] = u;
483: }
484: break;
485: case 6:
1.41 noro 486: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 487: u1 = d1[i]; u2 = d2[i];
488: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
489: t1 = (u1&0xfc0000); t2 = (u2&0xfc0000); u |= t1>t2?t1:t2;
490: t1 = (u1&0x3f000); t2 = (u2&0x3f000); u |= t1>t2?t1:t2;
491: t1 = (u1&0xfc0); t2 = (u2&0xfc0); u |= t1>t2?t1:t2;
492: t1 = (u1&0x3f); t2 = (u2&0x3f); u |= t1>t2?t1:t2;
493: d[i] = u;
494: }
495: break;
496: case 8:
1.41 noro 497: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 498: u1 = d1[i]; u2 = d2[i];
499: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
500: t1 = (u1&0xff0000); t2 = (u2&0xff0000); u |= t1>t2?t1:t2;
501: t1 = (u1&0xff00); t2 = (u2&0xff00); u |= t1>t2?t1:t2;
502: t1 = (u1&0xff); t2 = (u2&0xff); u |= t1>t2?t1:t2;
503: d[i] = u;
504: }
505: break;
506: case 16:
1.41 noro 507: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 508: u1 = d1[i]; u2 = d2[i];
509: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
510: t1 = (u1&0xffff); t2 = (u2&0xffff); u |= t1>t2?t1:t2;
511: d[i] = u;
512: }
513: break;
514: case 32:
1.41 noro 515: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 516: u1 = d1[i]; u2 = d2[i];
517: d[i] = u1>u2?u1:u2;
518: }
519: break;
520: default:
1.41 noro 521: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 522: u1 = d1[i]; u2 = d2[i];
523: for ( j = 0, u = 0; j < nd_epw; j++ ) {
524: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
525: }
526: d[i] = u;
527: }
528: break;
529: }
1.39 noro 530: TD(d) = ndl_weight(d);
1.43 noro 531: if ( nd_blockmask ) {
532: l = nd_blockmask->n;
533: for ( j = 0; j < l; j++ )
534: d[j+1] = ndl_weight_mask(d,j);
535: }
1.1 noro 536: }
537:
1.39 noro 538: int ndl_weight(unsigned int *d)
1.1 noro 539: {
540: unsigned int t,u;
541: int i,j;
542:
1.41 noro 543: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 544: u = d[i];
545: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
546: t += (u&nd_mask0);
547: }
548: return t;
549: }
550:
1.43 noro 551: int ndl_weight_mask(unsigned int *d,int index)
552: {
553: unsigned int t,u;
554: unsigned int *mask;
555: int i,j;
556:
557: mask = nd_blockmask->mask[index];
558: for ( t = 0, i = nd_exporigin; i < nd_wpd; i++ ) {
559: u = d[i]&mask[i];
560: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
561: t += (u&nd_mask0);
562: }
563: return t;
564: }
565:
566: int ndl_lex_compare(unsigned int *d1,unsigned int *d2)
1.1 noro 567: {
568: int i;
569:
1.41 noro 570: d1 += nd_exporigin;
571: d2 += nd_exporigin;
572: for ( i = nd_exporigin; i < nd_wpd; i++, d1++, d2++ )
1.1 noro 573: if ( *d1 > *d2 )
1.32 noro 574: return nd_isrlex ? -1 : 1;
1.1 noro 575: else if ( *d1 < *d2 )
1.32 noro 576: return nd_isrlex ? 1 : -1;
1.1 noro 577: return 0;
578: }
579:
1.43 noro 580: int ndl_block_compare(unsigned int *d1,unsigned int *d2)
581: {
582: int i,l,j,ord_o,ord_l;
583: struct order_pair *op;
1.44 noro 584: unsigned int t1,t2,m;
1.43 noro 585: unsigned int *mask;
586:
587: l = nd_blockmask->n;
588: op = nd_blockmask->order_pair;
589: for ( j = 0; j < l; j++ ) {
590: mask = nd_blockmask->mask[j];
591: ord_o = op[j].order;
592: if ( ord_o < 2 )
1.44 noro 593: if ( (t1=d1[j+1]) > (t2=d2[j+1]) ) return 1;
594: else if ( t1 < t2 ) return -1;
1.43 noro 595: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.44 noro 596: m = mask[i];
597: t1 = d1[i]&m;
598: t2 = d2[i]&m;
1.43 noro 599: if ( t1 > t2 )
600: return !ord_o ? -1 : 1;
601: else if ( t1 < t2 )
602: return !ord_o ? 1 : -1;
603: }
604: }
605: return 0;
606: }
607:
1.1 noro 608: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2)
609: {
610: int i;
611:
1.41 noro 612: for ( i = 0; i < nd_wpd; i++ )
1.34 noro 613: if ( *d1++ != *d2++ )
1.1 noro 614: return 0;
615: return 1;
616: }
617:
1.6 noro 618: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2)
619: {
620: int i;
621:
622: switch ( nd_wpd ) {
1.41 noro 623: case 2:
1.34 noro 624: TD(d2) = TD(d1);
625: d2[1] = d1[1];
1.6 noro 626: break;
1.41 noro 627: case 3:
1.34 noro 628: TD(d2) = TD(d1);
1.6 noro 629: d2[1] = d1[1];
1.34 noro 630: d2[2] = d1[2];
1.6 noro 631: break;
632: default:
1.41 noro 633: for ( i = 0; i < nd_wpd; i++ )
1.6 noro 634: d2[i] = d1[i];
635: break;
636: }
637: }
638:
1.1 noro 639: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d)
640: {
641: int i;
642:
1.43 noro 643: #if 1
1.6 noro 644: switch ( nd_wpd ) {
1.41 noro 645: case 2:
646: TD(d) = TD(d1)+TD(d2);
1.34 noro 647: d[1] = d1[1]+d2[1];
1.6 noro 648: break;
1.41 noro 649: case 3:
650: TD(d) = TD(d1)+TD(d2);
1.6 noro 651: d[1] = d1[1]+d2[1];
1.34 noro 652: d[2] = d1[2]+d2[2];
1.6 noro 653: break;
654: default:
1.43 noro 655: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
1.6 noro 656: break;
657: }
1.43 noro 658: #else
659: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]+d2[i];
660: #endif
1.6 noro 661: }
662:
1.34 noro 663: INLINE void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d)
1.6 noro 664: {
665: int i;
666:
1.43 noro 667: for ( i = 0; i < nd_wpd; i++ ) d[i] = d1[i]-d2[i];
1.1 noro 668: }
669:
670: int ndl_disjoint(unsigned int *d1,unsigned int *d2)
671: {
672: unsigned int t1,t2,u,u1,u2;
673: int i,j;
674:
675: switch ( nd_bpe ) {
676: case 4:
1.41 noro 677: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 678: u1 = d1[i]; u2 = d2[i];
679: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
680: t1 = u1&0xf000000; t2 = u2&0xf000000; if ( t1&&t2 ) return 0;
681: t1 = u1&0xf00000; t2 = u2&0xf00000; if ( t1&&t2 ) return 0;
682: t1 = u1&0xf0000; t2 = u2&0xf0000; if ( t1&&t2 ) return 0;
683: t1 = u1&0xf000; t2 = u2&0xf000; if ( t1&&t2 ) return 0;
684: t1 = u1&0xf00; t2 = u2&0xf00; if ( t1&&t2 ) return 0;
685: t1 = u1&0xf0; t2 = u2&0xf0; if ( t1&&t2 ) return 0;
686: t1 = u1&0xf; t2 = u2&0xf; if ( t1&&t2 ) return 0;
687: }
688: return 1;
689: break;
690: case 6:
1.41 noro 691: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 692: u1 = d1[i]; u2 = d2[i];
693: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
694: t1 = u1&0xfc0000; t2 = u2&0xfc0000; if ( t1&&t2 ) return 0;
695: t1 = u1&0x3f000; t2 = u2&0x3f000; if ( t1&&t2 ) return 0;
696: t1 = u1&0xfc0; t2 = u2&0xfc0; if ( t1&&t2 ) return 0;
697: t1 = u1&0x3f; t2 = u2&0x3f; if ( t1&&t2 ) return 0;
698: }
699: return 1;
700: break;
701: case 8:
1.41 noro 702: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 703: u1 = d1[i]; u2 = d2[i];
704: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
705: t1 = u1&0xff0000; t2 = u2&0xff0000; if ( t1&&t2 ) return 0;
706: t1 = u1&0xff00; t2 = u2&0xff00; if ( t1&&t2 ) return 0;
707: t1 = u1&0xff; t2 = u2&0xff; if ( t1&&t2 ) return 0;
708: }
709: return 1;
710: break;
711: case 16:
1.41 noro 712: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 713: u1 = d1[i]; u2 = d2[i];
714: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
715: t1 = u1&0xffff; t2 = u2&0xffff; if ( t1&&t2 ) return 0;
716: }
717: return 1;
718: break;
719: case 32:
1.41 noro 720: for ( i = nd_exporigin; i < nd_wpd; i++ )
1.1 noro 721: if ( d1[i] && d2[i] ) return 0;
722: return 1;
723: break;
724: default:
1.41 noro 725: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.1 noro 726: u1 = d1[i]; u2 = d2[i];
727: for ( j = 0; j < nd_epw; j++ ) {
728: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
729: u1 >>= nd_bpe; u2 >>= nd_bpe;
730: }
731: }
732: return 1;
733: break;
734: }
735: }
736:
1.5 noro 737: int ndl_check_bound2(int index,unsigned int *d2)
1.1 noro 738: {
1.5 noro 739: unsigned int u2;
740: unsigned int *d1;
741: int i,j,ind,k;
1.1 noro 742:
1.5 noro 743: d1 = nd_bound[index];
744: ind = 0;
745: switch ( nd_bpe ) {
746: case 4:
1.41 noro 747: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5 noro 748: u2 = d2[i];
749: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
750: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
751: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
752: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
753: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
754: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
755: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
756: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
757: }
758: return 0;
759: break;
760: case 6:
1.41 noro 761: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5 noro 762: u2 = d2[i];
763: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
764: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
765: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
766: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
767: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
768: }
769: return 0;
770: break;
771: case 8:
1.41 noro 772: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5 noro 773: u2 = d2[i];
774: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
775: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
776: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
777: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
778: }
779: return 0;
780: break;
781: case 16:
1.41 noro 782: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5 noro 783: u2 = d2[i];
784: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
785: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
786: }
787: return 0;
788: break;
789: case 32:
1.41 noro 790: for ( i = nd_exporigin; i < nd_wpd; i++ )
1.5 noro 791: if ( d1[i]+d2[i]<d1[i] ) return 1;
792: return 0;
793: break;
794: default:
1.41 noro 795: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.5 noro 796: u2 = d2[i];
797: k = (nd_epw-1)*nd_bpe;
798: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
799: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
800: }
801: return 0;
802: break;
803: }
1.1 noro 804: }
805:
1.23 noro 806: int ndl_check_bound2_direct(unsigned int *d1,unsigned int *d2)
807: {
1.45 noro 808: unsigned int u2;
809: int i,j,ind,k;
1.23 noro 810:
1.45 noro 811: ind = 0;
1.23 noro 812: switch ( nd_bpe ) {
813: case 4:
1.41 noro 814: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45 noro 815: u2 = d2[i];
816: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
817: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
818: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
819: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
820: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
821: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
822: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
823: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
1.23 noro 824: }
825: return 0;
826: break;
827: case 6:
1.41 noro 828: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45 noro 829: u2 = d2[i];
830: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
831: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
832: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
833: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
834: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
1.23 noro 835: }
836: return 0;
837: break;
838: case 8:
1.41 noro 839: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45 noro 840: u2 = d2[i];
841: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
842: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
843: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
844: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
1.23 noro 845: }
846: return 0;
847: break;
848: case 16:
1.41 noro 849: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45 noro 850: u2 = d2[i];
851: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
852: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
1.23 noro 853: }
854: return 0;
855: break;
856: case 32:
1.41 noro 857: for ( i = nd_exporigin; i < nd_wpd; i++ )
1.23 noro 858: if ( d1[i]+d2[i]<d1[i] ) return 1;
859: return 0;
860: break;
861: default:
1.41 noro 862: for ( i = nd_exporigin; i < nd_wpd; i++ ) {
1.45 noro 863: u2 = d2[i];
1.23 noro 864: k = (nd_epw-1)*nd_bpe;
865: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
1.45 noro 866: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
1.23 noro 867: }
868: return 0;
869: break;
870: }
871: }
872:
1.34 noro 873: INLINE int ndl_hash_value(unsigned int *d)
1.1 noro 874: {
875: int i;
876: int r;
877:
1.34 noro 878: r = 0;
1.41 noro 879: for ( i = 0; i < nd_wpd; i++ )
1.1 noro 880: r = ((r<<16)+d[i])%REDTAB_LEN;
881: return r;
882: }
883:
1.9 noro 884: INLINE int nd_find_reducer(ND g)
1.1 noro 885: {
1.13 noro 886: RHist r;
1.34 noro 887: unsigned int *dg;
1.6 noro 888: int d,k,i;
1.1 noro 889:
1.34 noro 890: dg = HDL(g);
1.39 noro 891: #if 1
1.34 noro 892: d = ndl_hash_value(HDL(g));
1.13 noro 893: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.34 noro 894: if ( ndl_equal(dg,DL(r)) ) {
1.1 noro 895: if ( k > 0 ) nd_notfirst++;
896: nd_found++;
1.13 noro 897: return r->index;
1.1 noro 898: }
899: }
1.39 noro 900: #endif
1.13 noro 901: if ( Reverse )
902: for ( i = nd_psn-1; i >= 0; i-- ) {
903: r = nd_psh[i];
1.34 noro 904: if ( ndl_reducible(dg,DL(r)) ) {
1.13 noro 905: nd_create++;
1.34 noro 906: nd_append_red(dg,i);
1.13 noro 907: return i;
908: }
909: }
910: else
911: for ( i = 0; i < nd_psn; i++ ) {
912: r = nd_psh[i];
1.34 noro 913: if ( ndl_reducible(dg,DL(r)) ) {
1.13 noro 914: nd_create++;
1.34 noro 915: nd_append_red(dg,i);
1.13 noro 916: return i;
917: }
1.1 noro 918: }
1.6 noro 919: return -1;
1.1 noro 920: }
921:
1.23 noro 922: INLINE int nd_find_reducer_direct(ND g,NDV *ps,int len)
923: {
924: NDV r;
1.31 noro 925: RHist s;
926: int d,k,i;
1.23 noro 927:
928: if ( Reverse )
929: for ( i = len-1; i >= 0; i-- ) {
930: r = ps[i];
1.45 noro 931: if ( ndl_reducible(HDL(g),HDL(r)) )
1.23 noro 932: return i;
933: }
934: else
935: for ( i = 0; i < len; i++ ) {
936: r = ps[i];
1.45 noro 937: if ( ndl_reducible(HDL(g),HDL(r)) )
1.23 noro 938: return i;
939: }
940: return -1;
941: }
942:
1.31 noro 943: ND nd_add(int mod,ND p1,ND p2)
1.1 noro 944: {
945: int n,c;
1.34 noro 946: int t,can,td1,td2;
1.1 noro 947: ND r;
948: NM m1,m2,mr0,mr,s;
949:
1.34 noro 950: if ( !p1 ) return p2;
951: else if ( !p2 ) return p1;
952: else if ( !mod ) return nd_add_q(p1,p2);
1.1 noro 953: else {
1.30 noro 954: can = 0;
1.1 noro 955: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34 noro 956: c = DL_COMPARE(DL(m1),DL(m2));
1.1 noro 957: switch ( c ) {
958: case 0:
1.19 noro 959: t = ((CM(m1))+(CM(m2))) - mod;
1.34 noro 960: if ( t < 0 ) t += mod;
1.1 noro 961: s = m1; m1 = NEXT(m1);
962: if ( t ) {
1.34 noro 963: can++; NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1 noro 964: } else {
1.34 noro 965: can += 2; FREENM(s);
1.1 noro 966: }
967: s = m2; m2 = NEXT(m2); FREENM(s);
968: break;
969: case 1:
970: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
971: break;
972: case -1:
973: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
974: break;
975: }
976: }
977: if ( !mr0 )
1.34 noro 978: if ( m1 ) mr0 = m1;
979: else if ( m2 ) mr0 = m2;
980: else return 0;
981: else if ( m1 ) NEXT(mr) = m1;
982: else if ( m2 ) NEXT(mr) = m2;
983: else NEXT(mr) = 0;
1.1 noro 984: BDY(p1) = mr0;
1.14 noro 985: SG(p1) = MAX(SG(p1),SG(p2));
1.31 noro 986: LEN(p1) = LEN(p1)+LEN(p2)-can;
1.1 noro 987: FREEND(p2);
988: return p1;
989: }
990: }
991:
1.31 noro 992: ND nd_add_q(ND p1,ND p2)
1.17 noro 993: {
1.30 noro 994: int n,c,can;
1.17 noro 995: ND r;
996: NM m1,m2,mr0,mr,s;
997: Q t;
998:
1.34 noro 999: if ( !p1 ) return p2;
1000: else if ( !p2 ) return p1;
1.31 noro 1001: else {
1.30 noro 1002: can = 0;
1.17 noro 1003: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.34 noro 1004: c = DL_COMPARE(DL(m1),DL(m2));
1.17 noro 1005: switch ( c ) {
1006: case 0:
1007: addq(CQ(m1),CQ(m2),&t);
1008: s = m1; m1 = NEXT(m1);
1009: if ( t ) {
1.34 noro 1010: can++; NEXTNM2(mr0,mr,s); CQ(mr) = (t);
1.17 noro 1011: } else {
1.34 noro 1012: can += 2; FREENM(s);
1.17 noro 1013: }
1014: s = m2; m2 = NEXT(m2); FREENM(s);
1015: break;
1016: case 1:
1017: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
1018: break;
1019: case -1:
1020: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
1021: break;
1022: }
1023: }
1024: if ( !mr0 )
1.34 noro 1025: if ( m1 ) mr0 = m1;
1026: else if ( m2 ) mr0 = m2;
1027: else return 0;
1028: else if ( m1 ) NEXT(mr) = m1;
1029: else if ( m2 ) NEXT(mr) = m2;
1030: else NEXT(mr) = 0;
1.17 noro 1031: BDY(p1) = mr0;
1032: SG(p1) = MAX(SG(p1),SG(p2));
1.31 noro 1033: LEN(p1) = LEN(p1)+LEN(p2)-can;
1.17 noro 1034: FREEND(p2);
1035: return p1;
1036: }
1037: }
1038:
1.1 noro 1039: /* ret=1 : success, ret=0 : overflow */
1.16 noro 1040: int nd_nf(int mod,ND g,int full,ND *rp)
1.1 noro 1041: {
1.11 noro 1042: ND d;
1.1 noro 1043: NM m,mrd,tail;
1.7 noro 1044: NM mul;
1.10 noro 1045: int n,sugar,psugar,sugar0,stat,index;
1.30 noro 1046: int c,c1,c2,dummy;
1.17 noro 1047: RHist h;
1.11 noro 1048: NDV p,red;
1.16 noro 1049: Q cg,cred,gcd;
1.21 noro 1050: double hmag;
1.1 noro 1051:
1052: if ( !g ) {
1053: *rp = 0;
1054: return 1;
1055: }
1.34 noro 1056: if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.21 noro 1057:
1.14 noro 1058: sugar0 = sugar = SG(g);
1.1 noro 1059: n = NV(g);
1.41 noro 1060: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.1 noro 1061: for ( d = 0; g; ) {
1.6 noro 1062: index = nd_find_reducer(g);
1063: if ( index >= 0 ) {
1.17 noro 1064: h = nd_psh[index];
1065: ndl_sub(HDL(g),DL(h),DL(mul));
1.14 noro 1066: if ( ndl_check_bound2(index,DL(mul)) ) {
1.6 noro 1067: nd_free(g); nd_free(d);
1068: return 0;
1069: }
1.16 noro 1070: if ( mod ) {
1.17 noro 1071: p = nd_ps[index];
1.19 noro 1072: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1073: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1.16 noro 1074: } else {
1.20 noro 1075: p = nd_psq[index];
1.17 noro 1076: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1.16 noro 1077: chsgnq(cg,&CQ(mul));
1.20 noro 1078: nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1.16 noro 1079: }
1.31 noro 1080: g = nd_add(mod,g,ndv_mul_nm(mod,p,mul));
1.34 noro 1081: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.22 noro 1082: if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1.21 noro 1083: nd_removecont2(d,g);
1084: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1085: }
1.1 noro 1086: } else if ( !full ) {
1087: *rp = g;
1088: return 1;
1089: } else {
1090: m = BDY(g);
1091: if ( NEXT(m) ) {
1.34 noro 1092: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.1 noro 1093: } else {
1094: FREEND(g); g = 0;
1095: }
1096: if ( d ) {
1.34 noro 1097: NEXT(tail)=m; tail=m; LEN(d)++;
1.1 noro 1098: } else {
1.34 noro 1099: MKND(n,m,1,d); tail = BDY(d);
1.1 noro 1100: }
1101: }
1102: }
1.34 noro 1103: if ( d ) SG(d) = sugar;
1.1 noro 1104: *rp = d;
1105: return 1;
1106: }
1.28 noro 1107:
1.30 noro 1108: int nd_nf_pbucket(int mod,ND g,int full,ND *rp)
1.25 noro 1109: {
1110: int hindex,index;
1111: NDV p;
1112: ND u,d,red;
1113: NODE l;
1.31 noro 1114: NM mul,m,mrd,tail;
1.25 noro 1115: int sugar,psugar,n,h_reducible;
1116: PGeoBucket bucket;
1117: int c,c1,c2;
1.26 noro 1118: Q cg,cred,gcd,zzz;
1.25 noro 1119: RHist h;
1.28 noro 1120: double hmag,gmag;
1.25 noro 1121:
1122: if ( !g ) {
1123: *rp = 0;
1124: return 1;
1125: }
1126: sugar = SG(g);
1127: n = NV(g);
1.34 noro 1128: if ( !mod ) hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.25 noro 1129: bucket = create_pbucket();
1.31 noro 1130: add_pbucket(mod,bucket,g);
1.25 noro 1131: d = 0;
1.41 noro 1132: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.25 noro 1133: while ( 1 ) {
1.26 noro 1134: hindex = mod?head_pbucket(mod,bucket):head_pbucket_q(bucket);
1.25 noro 1135: if ( hindex < 0 ) {
1.34 noro 1136: if ( d ) SG(d) = sugar;
1.25 noro 1137: *rp = d;
1138: return 1;
1139: }
1140: g = bucket->body[hindex];
1141: index = nd_find_reducer(g);
1142: if ( index >= 0 ) {
1143: h = nd_psh[index];
1144: ndl_sub(HDL(g),DL(h),DL(mul));
1145: if ( ndl_check_bound2(index,DL(mul)) ) {
1.26 noro 1146: nd_free(d);
1.25 noro 1147: free_pbucket(bucket);
1148: *rp = 0;
1149: return 0;
1150: }
1151: if ( mod ) {
1152: p = nd_ps[index];
1153: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1154: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1155: } else {
1156: p = nd_psq[index];
1157: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1158: chsgnq(cg,&CQ(mul));
1.26 noro 1159: nd_mul_c_q(d,cred);
1160: mulq_pbucket(bucket,cred);
1161: g = bucket->body[hindex];
1.28 noro 1162: gmag = (double)p_mag((P)HCQ(g));
1.25 noro 1163: }
1164: red = ndv_mul_nm(mod,p,mul);
1165: bucket->body[hindex] = nd_remove_head(g);
1166: red = nd_remove_head(red);
1.31 noro 1167: add_pbucket(mod,bucket,red);
1.34 noro 1168: psugar = SG(p)+TD(DL(mul));
1169: sugar = MAX(sugar,psugar);
1.28 noro 1170: if ( !mod && hmag && (gmag > hmag) ) {
1171: g = normalize_pbucket(mod,bucket);
1172: if ( !g ) {
1.34 noro 1173: if ( d ) SG(d) = sugar;
1.28 noro 1174: *rp = d;
1175: return 1;
1176: }
1177: nd_removecont2(d,g);
1178: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1.31 noro 1179: add_pbucket(mod,bucket,g);
1.28 noro 1180: }
1.25 noro 1181: } else if ( !full ) {
1182: g = normalize_pbucket(mod,bucket);
1.34 noro 1183: if ( g ) SG(g) = sugar;
1.25 noro 1184: *rp = g;
1185: return 1;
1186: } else {
1187: m = BDY(g);
1188: if ( NEXT(m) ) {
1.34 noro 1189: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.25 noro 1190: } else {
1191: FREEND(g); g = 0;
1192: }
1193: bucket->body[hindex] = g;
1194: NEXT(m) = 0;
1195: if ( d ) {
1.34 noro 1196: NEXT(tail)=m; tail=m; LEN(d)++;
1.25 noro 1197: } else {
1.34 noro 1198: MKND(n,m,1,d); tail = BDY(d);
1.25 noro 1199: }
1200: }
1201: }
1202: }
1.27 noro 1203:
1.45 noro 1204: int nd_nf_direct(int mod,ND g,BaseSet base,int full,ND *rp)
1.23 noro 1205: {
1.30 noro 1206: ND d;
1207: NM m,mrd,tail;
1208: NM mul;
1.45 noro 1209: NDV *ps;
1210: int n,sugar,psugar,sugar0,stat,index,len;
1.31 noro 1211: int c,c1,c2;
1.45 noro 1212: unsigned int **bound;
1.30 noro 1213: RHist h;
1214: NDV p,red;
1215: Q cg,cred,gcd;
1216: double hmag;
1217:
1218: if ( !g ) {
1219: *rp = 0;
1220: return 1;
1221: }
1222: #if 0
1223: if ( !mod )
1224: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1225: #else
1226: /* XXX */
1227: hmag = 0;
1228: #endif
1229:
1.45 noro 1230: ps = base->ps;
1231: bound = base->bound;
1232: len = base->len;
1.30 noro 1233: sugar0 = sugar = SG(g);
1234: n = NV(g);
1.41 noro 1235: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.30 noro 1236: for ( d = 0; g; ) {
1237: index = nd_find_reducer_direct(g,ps,len);
1238: if ( index >= 0 ) {
1239: p = ps[index];
1240: ndl_sub(HDL(g),HDL(p),DL(mul));
1.45 noro 1241: if ( ndl_check_bound2_direct(bound[index],DL(mul)) ) {
1.30 noro 1242: nd_free(g); nd_free(d);
1243: return 0;
1244: }
1245: if ( mod ) {
1246: c1 = invm(HCM(p),mod); c2 = mod-HCM(g);
1247: DMAR(c1,c2,0,mod,c); CM(mul) = c;
1248: } else {
1249: igcd_cofactor(HCQ(g),HCQ(p),&gcd,&cg,&cred);
1250: chsgnq(cg,&CQ(mul));
1251: nd_mul_c_q(d,cred); nd_mul_c_q(g,cred);
1252: }
1.31 noro 1253: g = nd_add(mod,g,ndv_mul_nm(mod,p,mul));
1.34 noro 1254: sugar = MAX(sugar,SG(p)+TD(DL(mul)));
1.30 noro 1255: if ( !mod && hmag && g && ((double)(p_mag((P)HCQ(g))) > hmag) ) {
1256: nd_removecont2(d,g);
1257: hmag = ((double)p_mag((P)HCQ(g)))*nd_scale;
1258: }
1259: } else if ( !full ) {
1260: *rp = g;
1261: return 1;
1262: } else {
1263: m = BDY(g);
1264: if ( NEXT(m) ) {
1.34 noro 1265: BDY(g) = NEXT(m); NEXT(m) = 0; LEN(g)--;
1.30 noro 1266: } else {
1267: FREEND(g); g = 0;
1268: }
1269: if ( d ) {
1.34 noro 1270: NEXT(tail)=m; tail=m; LEN(d)++;
1.30 noro 1271: } else {
1.34 noro 1272: MKND(n,m,1,d); tail = BDY(d);
1.30 noro 1273: }
1274: }
1275: }
1.34 noro 1276: if ( d ) SG(d) = sugar;
1.30 noro 1277: *rp = d;
1278: return 1;
1279: }
1280:
1.28 noro 1281: /* input : list of DP, cand : list of DP */
1282:
1283: int nd_check_candidate(NODE input,NODE cand)
1284: {
1285: int n,i,stat;
1286: ND nf,d;
1.45 noro 1287: NODE t,s;
1288:
1289: #if 0
1290: for ( t = 0; cand; cand = NEXT(cand) ) {
1291: MKNODE(s,BDY(cand),t); t = s;
1292: }
1293: cand = t;
1294: #endif
1.28 noro 1295:
1.39 noro 1296: nd_setup(0,0,cand);
1.31 noro 1297: n = length(cand);
1.28 noro 1298:
1299: /* membercheck : list is a subset of Id(cand) ? */
1300: for ( t = input; t; t = NEXT(t) ) {
1.45 noro 1301: again:
1.28 noro 1302: d = dptond(0,(DP)BDY(t));
1.45 noro 1303: stat = nd_nf(0,d,0,&nf);
1304: if ( !stat ) {
1305: nd_reconstruct(0,0,0);
1306: goto again;
1307: } else if ( nf ) return 0;
1308: printf("."); fflush(stdout);
1.28 noro 1309: }
1.45 noro 1310: printf("\n");
1.28 noro 1311: /* gbcheck : cand is a GB of Id(cand) ? */
1.34 noro 1312: if ( !nd_gb(0,1) ) return 0;
1.28 noro 1313: /* XXX */
1.23 noro 1314: return 1;
1315: }
1.1 noro 1316:
1317: ND nd_remove_head(ND p)
1318: {
1319: NM m;
1320:
1321: m = BDY(p);
1322: if ( !NEXT(m) ) {
1.34 noro 1323: FREEND(p); p = 0;
1.31 noro 1324: } else {
1.34 noro 1325: BDY(p) = NEXT(m); LEN(p)--;
1.31 noro 1326: }
1.1 noro 1327: FREENM(m);
1328: return p;
1329: }
1330:
1331: PGeoBucket create_pbucket()
1332: {
1333: PGeoBucket g;
1334:
1335: g = CALLOC(1,sizeof(struct oPGeoBucket));
1336: g->m = -1;
1337: return g;
1338: }
1339:
1.25 noro 1340: void free_pbucket(PGeoBucket b) {
1341: int i;
1342:
1.26 noro 1343: for ( i = 0; i <= b->m; i++ )
1.25 noro 1344: if ( b->body[i] ) {
1345: nd_free(b->body[i]);
1346: b->body[i] = 0;
1347: }
1348: GC_free(b);
1349: }
1350:
1.31 noro 1351: void add_pbucket(int mod,PGeoBucket g,ND d)
1.1 noro 1352: {
1.31 noro 1353: int l,i,k,m;
1.1 noro 1354:
1.31 noro 1355: if ( !d )
1356: return;
1357: l = LEN(d);
1.29 noro 1358: for ( k = 0, m = 1; l > m; k++, m <<= 1 );
1359: /* 2^(k-1) < l <= 2^k (=m) */
1.31 noro 1360: d = nd_add(mod,g->body[k],d);
1361: for ( ; d && LEN(d) > m; k++, m <<= 1 ) {
1.1 noro 1362: g->body[k] = 0;
1.31 noro 1363: d = nd_add(mod,g->body[k+1],d);
1.1 noro 1364: }
1365: g->body[k] = d;
1366: g->m = MAX(g->m,k);
1367: }
1368:
1.26 noro 1369: void mulq_pbucket(PGeoBucket g,Q c)
1370: {
1371: int k;
1372:
1373: for ( k = 0; k <= g->m; k++ )
1374: nd_mul_c_q(g->body[k],c);
1375: }
1376:
1.19 noro 1377: int head_pbucket(int mod,PGeoBucket g)
1.1 noro 1378: {
1379: int j,i,c,k,nv,sum;
1380: unsigned int *di,*dj;
1381: ND gi,gj;
1382:
1383: k = g->m;
1384: while ( 1 ) {
1385: j = -1;
1386: for ( i = 0; i <= k; i++ ) {
1387: if ( !(gi = g->body[i]) )
1388: continue;
1389: if ( j < 0 ) {
1390: j = i;
1391: gj = g->body[j];
1392: dj = HDL(gj);
1.14 noro 1393: sum = HCM(gj);
1.1 noro 1394: } else {
1.34 noro 1395: c = DL_COMPARE(HDL(gi),dj);
1.1 noro 1396: if ( c > 0 ) {
1.34 noro 1397: if ( sum ) HCM(gj) = sum;
1398: else g->body[j] = nd_remove_head(gj);
1.1 noro 1399: j = i;
1400: gj = g->body[j];
1401: dj = HDL(gj);
1.14 noro 1402: sum = HCM(gj);
1.1 noro 1403: } else if ( c == 0 ) {
1.19 noro 1404: sum = sum+HCM(gi)-mod;
1.34 noro 1405: if ( sum < 0 ) sum += mod;
1.1 noro 1406: g->body[i] = nd_remove_head(gi);
1407: }
1408: }
1409: }
1.34 noro 1410: if ( j < 0 ) return -1;
1.1 noro 1411: else if ( sum ) {
1.14 noro 1412: HCM(gj) = sum;
1.26 noro 1413: return j;
1.31 noro 1414: } else
1.26 noro 1415: g->body[j] = nd_remove_head(gj);
1416: }
1417: }
1418:
1419: int head_pbucket_q(PGeoBucket g)
1420: {
1421: int j,i,c,k,nv;
1422: Q sum,t;
1423: ND gi,gj;
1424:
1425: k = g->m;
1426: while ( 1 ) {
1427: j = -1;
1428: for ( i = 0; i <= k; i++ ) {
1.34 noro 1429: if ( !(gi = g->body[i]) ) continue;
1.26 noro 1430: if ( j < 0 ) {
1431: j = i;
1432: gj = g->body[j];
1433: sum = HCQ(gj);
1434: } else {
1435: nv = NV(gi);
1.34 noro 1436: c = DL_COMPARE(HDL(gi),HDL(gj));
1.26 noro 1437: if ( c > 0 ) {
1.34 noro 1438: if ( sum ) HCQ(gj) = sum;
1439: else g->body[j] = nd_remove_head(gj);
1.26 noro 1440: j = i;
1441: gj = g->body[j];
1442: sum = HCQ(gj);
1443: } else if ( c == 0 ) {
1444: addq(sum,HCQ(gi),&t);
1445: sum = t;
1446: g->body[i] = nd_remove_head(gi);
1447: }
1448: }
1449: }
1.34 noro 1450: if ( j < 0 ) return -1;
1.26 noro 1451: else if ( sum ) {
1452: HCQ(gj) = sum;
1.1 noro 1453: return j;
1.31 noro 1454: } else
1.1 noro 1455: g->body[j] = nd_remove_head(gj);
1456: }
1457: }
1458:
1.25 noro 1459: ND normalize_pbucket(int mod,PGeoBucket g)
1.1 noro 1460: {
1.31 noro 1461: int i;
1.1 noro 1462: ND r,t;
1463:
1464: r = 0;
1.28 noro 1465: for ( i = 0; i <= g->m; i++ ) {
1.31 noro 1466: r = nd_add(mod,r,g->body[i]);
1.28 noro 1467: g->body[i] = 0;
1468: }
1469: g->m = -1;
1.1 noro 1470: return r;
1471: }
1472:
1.27 noro 1473: /* return value = 0 => input is not a GB */
1474:
1475: NODE nd_gb(int m,int checkonly)
1.1 noro 1476: {
1477: int i,nh,sugar,stat;
1.23 noro 1478: NODE r,g,t;
1.1 noro 1479: ND_pairs d;
1480: ND_pairs l;
1481: ND h,nf;
1482:
1.23 noro 1483: g = 0; d = 0;
1484: for ( i = 0; i < nd_psn; i++ ) {
1.1 noro 1485: d = update_pairs(d,g,i);
1486: g = update_base(g,i);
1487: }
1488: sugar = 0;
1489: while ( d ) {
1490: again:
1491: l = nd_minp(d,&d);
1.14 noro 1492: if ( SG(l) != sugar ) {
1493: sugar = SG(l);
1.1 noro 1494: fprintf(asir_out,"%d",sugar);
1495: }
1.16 noro 1496: stat = nd_sp(m,l,&h);
1.1 noro 1497: if ( !stat ) {
1498: NEXT(l) = d; d = l;
1.20 noro 1499: d = nd_reconstruct(m,0,d);
1.1 noro 1500: goto again;
1501: }
1.41 noro 1502: #if USE_GEOBUCKET
1.30 noro 1503: stat = m?nd_nf_pbucket(m,h,!Top,&nf):nd_nf(m,h,!Top,&nf);
1.41 noro 1504: #else
1505: stat = nd_nf(m,h,!Top,&nf);
1506: #endif
1.1 noro 1507: if ( !stat ) {
1508: NEXT(l) = d; d = l;
1.20 noro 1509: d = nd_reconstruct(m,0,d);
1.1 noro 1510: goto again;
1511: } else if ( nf ) {
1.27 noro 1512: if ( checkonly ) return 0;
1.1 noro 1513: printf("+"); fflush(stdout);
1.39 noro 1514: nh = m?nd_newps(m,nf,0):nd_newps(m,0,nf);
1.1 noro 1515: d = update_pairs(d,g,nh);
1516: g = update_base(g,nh);
1517: FREENDP(l);
1518: } else {
1519: printf("."); fflush(stdout);
1520: FREENDP(l);
1521: }
1522: }
1.23 noro 1523: if ( m )
1.34 noro 1524: for ( t = g; t; t = NEXT(t) ) BDY(t) = (pointer)nd_ps[(int)BDY(t)];
1.23 noro 1525: else
1.34 noro 1526: for ( t = g; t; t = NEXT(t) ) BDY(t) = (pointer)nd_psq[(int)BDY(t)];
1.1 noro 1527: return g;
1528: }
1529:
1.23 noro 1530: NODE nd_gb_trace(int m)
1.20 noro 1531: {
1532: int i,nh,sugar,stat;
1.23 noro 1533: NODE r,g,t;
1.20 noro 1534: ND_pairs d;
1535: ND_pairs l;
1536: ND h,nf,nfq;
1537:
1.23 noro 1538: g = 0; d = 0;
1539: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 1540: d = update_pairs(d,g,i);
1541: g = update_base(g,i);
1542: }
1543: sugar = 0;
1544: while ( d ) {
1545: again:
1546: l = nd_minp(d,&d);
1547: if ( SG(l) != sugar ) {
1548: sugar = SG(l);
1549: fprintf(asir_out,"%d",sugar);
1550: }
1551: stat = nd_sp(m,l,&h);
1552: if ( !stat ) {
1553: NEXT(l) = d; d = l;
1554: d = nd_reconstruct(m,1,d);
1555: goto again;
1556: }
1.41 noro 1557: #if USE_GEOBUCKET
1.30 noro 1558: stat = nd_nf_pbucket(m,h,!Top,&nf);
1.41 noro 1559: #else
1560: stat = nd_nf(m,h,!Top,&nf);
1561: #endif
1.20 noro 1562: if ( !stat ) {
1563: NEXT(l) = d; d = l;
1564: d = nd_reconstruct(m,1,d);
1565: goto again;
1566: } else if ( nf ) {
1567: /* overflow does not occur */
1568: nd_sp(0,l,&h);
1569: nd_nf(0,h,!Top,&nfq);
1570: if ( nfq ) {
1571: printf("+"); fflush(stdout);
1.39 noro 1572: nh = nd_newps(m,nf,nfq);
1.27 noro 1573: /* failure; m|HC(nfq) */
1.38 noro 1574: if ( nh < 0 ) return 0;
1.20 noro 1575: d = update_pairs(d,g,nh);
1576: g = update_base(g,nh);
1577: } else {
1578: printf("*"); fflush(stdout);
1579: }
1580: } else {
1581: printf("."); fflush(stdout);
1582: }
1583: FREENDP(l);
1584: }
1.23 noro 1585: for ( t = g; t; t = NEXT(t) )
1586: BDY(t) = (pointer)nd_psq[(int)BDY(t)];
1.20 noro 1587: return g;
1588: }
1589:
1.23 noro 1590: int ndv_compare(NDV *p1,NDV *p2)
1591: {
1.34 noro 1592: return DL_COMPARE(HDL(*p1),HDL(*p2));
1.23 noro 1593: }
1594:
1595: int ndv_compare_rev(NDV *p1,NDV *p2)
1596: {
1.34 noro 1597: return -DL_COMPARE(HDL(*p1),HDL(*p2));
1.23 noro 1598: }
1599:
1600: NODE nd_reduceall(int m,NODE f)
1601: {
1602: int i,j,n,stat;
1603: NDV *w,*ps;
1604: ND nf,g;
1605: NODE t,a0,a;
1.45 noro 1606: struct oBaseSet base;
1607: unsigned int **bound;
1.23 noro 1608:
1609: for ( n = 0, t = f; t; t = NEXT(t), n++ );
1610: ps = (NDV *)ALLOCA(n*sizeof(NDV));
1.45 noro 1611: bound = (unsigned int **)ALLOCA(n*sizeof(unsigned int *));
1612: for ( i = 0, t = f; i < n; i++, t = NEXT(t) ) {
1.23 noro 1613: ps[i] = (NDV)BDY(t);
1.45 noro 1614: bound[i] = ndv_compute_bound(ps[i]);
1615: }
1.23 noro 1616: qsort(ps,n,sizeof(NDV),(int (*)(const void *,const void *))ndv_compare);
1.45 noro 1617: base.ps = (NDV *)ALLOCA((n-1)*sizeof(NDV));
1618: base.bound = (unsigned int **)ALLOCA((n-1)*sizeof(unsigned int *));
1619: base.len = n-1;
1.23 noro 1620: for ( i = 0; i < n; i++ ) {
1.45 noro 1621: again:
1622: for ( j = 0; j < i; j++ ) {
1623: base.ps[j] = ps[j]; base.bound[j] = bound[j];
1624: }
1625: for ( j = i+1; j < n; j++ ) {
1626: base.ps[j-1] = ps[j]; base.bound[j-1] = bound[j];
1627: }
1.23 noro 1628: g = ndvtond(m,ps[i]);
1.45 noro 1629: stat = nd_nf_direct(m,g,&base,1,&nf);
1630: if ( !stat ) {
1.23 noro 1631: nd_reconstruct_direct(m,ps,n);
1.45 noro 1632: goto again;
1633: }
1.23 noro 1634: else if ( !nf ) {
1635: printf("."); fflush(stdout);
1636: ndv_free(ps[i]);
1.34 noro 1637: for ( j = i+1; j < n; j++ ) ps[j-1] = ps[j];
1.23 noro 1638: n--;
1639: } else {
1640: printf("."); fflush(stdout);
1641: ndv_free(ps[i]);
1.24 noro 1642: nd_removecont(m,nf);
1.23 noro 1643: ps[i] = ndtondv(m,nf);
1.45 noro 1644: bound[i] = ndv_compute_bound(ps[i]);
1.23 noro 1645: nd_free(nf);
1646: }
1647: }
1.45 noro 1648: printf("\n");
1.23 noro 1649: for ( a0 = 0, i = 0; i < n; i++ ) {
1650: NEXTNODE(a0,a);
1651: BDY(a) = (pointer)ps[i];
1652: }
1653: NEXT(a) = 0;
1654: return a0;
1655: }
1656:
1.1 noro 1657: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
1658: {
1659: ND_pairs d1,nd,cur,head,prev,remove;
1660:
1661: if ( !g ) return d;
1662: d = crit_B(d,t);
1663: d1 = nd_newpairs(g,t);
1664: d1 = crit_M(d1);
1665: d1 = crit_F(d1);
1666: prev = 0; cur = head = d1;
1667: while ( cur ) {
1668: if ( crit_2( cur->i1,cur->i2 ) ) {
1669: remove = cur;
1.34 noro 1670: if ( !prev ) head = cur = NEXT(cur);
1671: else cur = NEXT(prev) = NEXT(cur);
1.1 noro 1672: FREENDP(remove);
1673: } else {
1.34 noro 1674: prev = cur; cur = NEXT(cur);
1.1 noro 1675: }
1676: }
1677: if ( !d )
1678: return head;
1679: else {
1680: nd = d;
1.34 noro 1681: while ( NEXT(nd) ) nd = NEXT(nd);
1.1 noro 1682: NEXT(nd) = head;
1683: return d;
1684: }
1685: }
1686:
1687: ND_pairs nd_newpairs( NODE g, int t )
1688: {
1689: NODE h;
1690: unsigned int *dl;
1.34 noro 1691: int ts,s;
1.1 noro 1692: ND_pairs r,r0;
1693:
1.20 noro 1694: dl = DL(nd_psh[t]);
1.34 noro 1695: ts = SG(nd_psh[t]) - TD(dl);
1.1 noro 1696: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1697: NEXTND_pairs(r0,r);
1698: r->i1 = (int)BDY(h);
1699: r->i2 = t;
1.20 noro 1700: ndl_lcm(DL(nd_psh[r->i1]),dl,r->lcm);
1.34 noro 1701: s = SG(nd_psh[r->i1])-TD(DL(nd_psh[r->i1]));
1702: SG(r) = MAX(s,ts) + TD(LCM(r));
1.1 noro 1703: }
1704: NEXT(r) = 0;
1705: return r0;
1706: }
1707:
1708: ND_pairs crit_B( ND_pairs d, int s )
1709: {
1710: ND_pairs cur,head,prev,remove;
1711: unsigned int *t,*tl,*lcm;
1712: int td,tdl;
1713:
1714: if ( !d ) return 0;
1.20 noro 1715: t = DL(nd_psh[s]);
1.1 noro 1716: prev = 0;
1717: head = cur = d;
1.41 noro 1718: lcm = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1.1 noro 1719: while ( cur ) {
1720: tl = cur->lcm;
1721: if ( ndl_reducible(tl,t)
1.20 noro 1722: && (ndl_lcm(DL(nd_psh[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
1723: && (ndl_lcm(DL(nd_psh[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1.1 noro 1724: remove = cur;
1725: if ( !prev ) {
1726: head = cur = NEXT(cur);
1727: } else {
1728: cur = NEXT(prev) = NEXT(cur);
1729: }
1730: FREENDP(remove);
1731: } else {
1.34 noro 1732: prev = cur; cur = NEXT(cur);
1.1 noro 1733: }
1734: }
1735: return head;
1736: }
1737:
1.34 noro 1738: /* XXX : check is necessary */
1739:
1.1 noro 1740: ND_pairs crit_M( ND_pairs d1 )
1741: {
1742: ND_pairs e,d2,d3,dd,p;
1743: unsigned int *id,*jd;
1744:
1745: for ( dd = 0, e = d1; e; e = d3 ) {
1746: if ( !(d2 = NEXT(e)) ) {
1747: NEXT(e) = dd;
1748: return e;
1749: }
1.34 noro 1750: id = LCM(e);
1.1 noro 1751: for ( d3 = 0; d2; d2 = p ) {
1.34 noro 1752: p = NEXT(d2);
1753: jd = LCM(d2);
1754: if ( ndl_equal(jd,id) )
1755: ;
1756: else if ( TD(jd) > TD(id) )
1.1 noro 1757: if ( ndl_reducible(jd,id) ) continue;
1758: else ;
1.34 noro 1759: else if ( ndl_reducible(id,jd) ) goto delit;
1.1 noro 1760: NEXT(d2) = d3;
1761: d3 = d2;
1762: }
1763: NEXT(e) = dd;
1764: dd = e;
1765: continue;
1766: /**/
1767: delit: NEXT(d2) = d3;
1768: d3 = d2;
1769: for ( ; p; p = d2 ) {
1770: d2 = NEXT(p);
1771: NEXT(p) = d3;
1772: d3 = p;
1773: }
1774: FREENDP(e);
1775: }
1776: return dd;
1777: }
1778:
1779: ND_pairs crit_F( ND_pairs d1 )
1780: {
1781: ND_pairs rest, head,remove;
1782: ND_pairs last, p, r, w;
1783: int s;
1784:
1785: for ( head = last = 0, p = d1; NEXT(p); ) {
1786: r = w = equivalent_pairs(p,&rest);
1.14 noro 1787: s = SG(r);
1.1 noro 1788: w = NEXT(w);
1789: while ( w ) {
1790: if ( crit_2(w->i1,w->i2) ) {
1791: r = w;
1792: w = NEXT(w);
1793: while ( w ) {
1794: remove = w;
1795: w = NEXT(w);
1796: FREENDP(remove);
1797: }
1798: break;
1.14 noro 1799: } else if ( SG(w) < s ) {
1.1 noro 1800: FREENDP(r);
1801: r = w;
1.14 noro 1802: s = SG(r);
1.1 noro 1803: w = NEXT(w);
1804: } else {
1805: remove = w;
1806: w = NEXT(w);
1807: FREENDP(remove);
1808: }
1809: }
1810: if ( last ) NEXT(last) = r;
1811: else head = r;
1812: NEXT(last = r) = 0;
1813: p = rest;
1814: if ( !p ) return head;
1815: }
1816: if ( !last ) return p;
1817: NEXT(last) = p;
1818: return head;
1819: }
1820:
1821: int crit_2( int dp1, int dp2 )
1822: {
1.20 noro 1823: return ndl_disjoint(DL(nd_psh[dp1]),DL(nd_psh[dp2]));
1.1 noro 1824: }
1825:
1.40 noro 1826: ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1.1 noro 1827: {
1828: ND_pairs w,p,r,s;
1829: unsigned int *d;
1830:
1831: w = d1;
1.34 noro 1832: d = LCM(w);
1.1 noro 1833: s = NEXT(w);
1834: NEXT(w) = 0;
1835: for ( r = 0; s; s = p ) {
1836: p = NEXT(s);
1.34 noro 1837: if ( ndl_equal(d,LCM(s)) ) {
1.39 noro 1838: NEXT(s) = w; w = s;
1.1 noro 1839: } else {
1.39 noro 1840: NEXT(s) = r; r = s;
1.1 noro 1841: }
1842: }
1843: *prest = r;
1844: return w;
1845: }
1846:
1847: NODE update_base(NODE nd,int ndp)
1848: {
1849: unsigned int *dl, *dln;
1850: NODE last, p, head;
1851:
1.20 noro 1852: dl = DL(nd_psh[ndp]);
1.1 noro 1853: for ( head = last = 0, p = nd; p; ) {
1.20 noro 1854: dln = DL(nd_psh[(int)BDY(p)]);
1.34 noro 1855: if ( ndl_reducible( dln, dl ) ) {
1.1 noro 1856: p = NEXT(p);
1857: if ( last ) NEXT(last) = p;
1858: } else {
1859: if ( !last ) head = p;
1860: p = NEXT(last = p);
1861: }
1862: }
1863: head = append_one(head,ndp);
1864: return head;
1865: }
1866:
1867: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
1868: {
1869: ND_pairs m,ml,p,l;
1870: unsigned int *lcm;
1.33 noro 1871: int s,td,len,tlen,c,c1;
1.1 noro 1872:
1873: if ( !(p = NEXT(m = d)) ) {
1874: *prest = p;
1875: NEXT(m) = 0;
1876: return m;
1877: }
1.14 noro 1878: s = SG(m);
1.33 noro 1879: for ( ml = 0, l = m; p; p = NEXT(l = p) )
1.34 noro 1880: if ( (SG(p) < s)
1881: || ((SG(p) == s) && (DL_COMPARE(LCM(p),LCM(m)) < 0)) ) {
1.39 noro 1882: ml = l; m = p; s = SG(m);
1.1 noro 1883: }
1884: if ( !ml ) *prest = NEXT(m);
1885: else {
1886: NEXT(ml) = NEXT(m);
1887: *prest = d;
1888: }
1889: NEXT(m) = 0;
1890: return m;
1891: }
1892:
1.39 noro 1893: int nd_newps(int mod,ND a,ND aq)
1.1 noro 1894: {
1.3 noro 1895: int len;
1.13 noro 1896: RHist r;
1.20 noro 1897: NDV b;
1.3 noro 1898:
1.1 noro 1899: if ( nd_psn == nd_pslen ) {
1900: nd_pslen *= 2;
1.11 noro 1901: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1.20 noro 1902: nd_psq = (NDV *)REALLOC((char *)nd_psq,nd_pslen*sizeof(NDV));
1.13 noro 1903: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.1 noro 1904: nd_bound = (unsigned int **)
1905: REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
1906: }
1.39 noro 1907: if ( a && aq ) {
1908: /* trace lifting */
1.46 ! noro 1909: if ( !rem(NM(HCQ(aq)),mod) )
! 1910: return -1;
1.39 noro 1911: }
1912: NEWRHist(r); nd_psh[nd_psn] = r;
1913: if ( aq ) {
1914: nd_removecont(0,aq);
1915: nd_psq[nd_psn] = ndtondv(0,aq);
1.45 noro 1916: nd_bound[nd_psn] = ndv_compute_bound(nd_psq[nd_psn]);
1.39 noro 1917: SG(r) = SG(aq); ndl_copy(HDL(aq),DL(r));
1918: }
1919: if ( a ) {
1920: nd_removecont(mod,a);
1921: nd_ps[nd_psn] = ndtondv(mod,a);
1922: if ( !aq ) {
1.45 noro 1923: nd_bound[nd_psn] = ndv_compute_bound(nd_ps[nd_psn]);
1.39 noro 1924: SG(r) = SG(a); ndl_copy(HDL(a),DL(r));
1925: }
1.20 noro 1926: }
1.39 noro 1927: nd_free(a); nd_free(aq);
1.1 noro 1928: return nd_psn++;
1929: }
1930:
1.39 noro 1931: void nd_setup(int mod,int trace,NODE f)
1.1 noro 1932: {
1.5 noro 1933: int i,j,td,len,max;
1.1 noro 1934: NODE s,s0,f0;
1.5 noro 1935: unsigned int *d;
1.13 noro 1936: RHist r;
1.20 noro 1937: NDV a;
1.11 noro 1938:
1939: nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1 noro 1940:
1941: nd_psn = length(f); nd_pslen = 2*nd_psn;
1.11 noro 1942: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.20 noro 1943: nd_psq = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1.13 noro 1944: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.1 noro 1945: nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
1.5 noro 1946: for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
1947: nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
1948: for ( j = 0; j < nd_nvar; j++ )
1949: max = MAX(d[j],max);
1950: }
1.11 noro 1951: if ( !nd_red )
1.13 noro 1952: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1953: bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.5 noro 1954:
1.34 noro 1955: if ( max < 2 ) nd_bpe = 2;
1956: else if ( max < 4 ) nd_bpe = 4;
1957: else if ( max < 64 ) nd_bpe = 6;
1958: else if ( max < 256 ) nd_bpe = 8;
1959: else if ( max < 65536 ) nd_bpe = 16;
1960: else nd_bpe = 32;
1.13 noro 1961:
1.1 noro 1962: nd_setup_parameters();
1963: nd_free_private_storage();
1964: for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.20 noro 1965: NEWRHist(r);
1.39 noro 1966: a = dptondv(mod,(DP)BDY(f)); ndv_removecont(mod,a);
1.34 noro 1967: SG(r) = HTD(a); ndl_copy(HDL(a),DL(r));
1.1 noro 1968:
1.39 noro 1969: if ( trace ) {
1970: nd_ps[i] = a;
1971: a = dptondv(0,(DP)BDY(f)); ndv_removecont(0,a);
1972: nd_psq[i] = a;
1973: } else {
1974: if ( mod ) nd_ps[i] = a;
1975: else nd_psq[i] = a;
1976: }
1.20 noro 1977: nd_psh[i] = r;
1978: }
1979: }
1980:
1.1 noro 1981: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp)
1982: {
1983: struct order_spec ord1;
1984: VL fv,vv,vc;
1985: NODE fd,fd0,r,r0,t,x,s,xx;
1986: DP a,b,c;
1987:
1988: get_vars((Obj)f,&fv); pltovl(v,&vv);
1989: nd_nvar = length(vv);
1.32 noro 1990: nd_init_ord(ord);
1991: /* XXX for DP */
1.1 noro 1992: initd(ord);
1993: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1994: ptod(CO,vv,(P)BDY(t),&b);
1.20 noro 1995: NEXTNODE(fd0,fd); BDY(fd) = (pointer)b;
1.1 noro 1996: }
1997: if ( fd0 ) NEXT(fd) = 0;
1.39 noro 1998: nd_setup(m,0,fd0);
1.27 noro 1999: x = nd_gb(m,0);
1.25 noro 2000: fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
2001: nd_found,nd_notfirst,nd_create);
1.23 noro 2002: x = nd_reduceall(m,x);
2003: for ( r0 = 0, t = x; t; t = NEXT(t) ) {
1.1 noro 2004: NEXTNODE(r0,r);
1.20 noro 2005: if ( m ) {
1.23 noro 2006: a = ndvtodp(m,BDY(t));
1.16 noro 2007: _dtop_mod(CO,vv,a,(P *)&BDY(r));
1.20 noro 2008: } else {
1.23 noro 2009: a = ndvtodp(m,BDY(t));
1.16 noro 2010: dtop(CO,vv,a,(P *)&BDY(r));
1.20 noro 2011: }
2012: }
2013: if ( r0 ) NEXT(r) = 0;
2014: MKLIST(*rp,r0);
2015: }
2016:
1.23 noro 2017: void nd_gr_trace(LIST f,LIST v,int m,int homo,struct order_spec *ord,LIST *rp)
1.20 noro 2018: {
2019: struct order_spec ord1;
2020: VL fv,vv,vc;
1.27 noro 2021: NODE fd,fd0,in0,in,r,r0,t,s,cand;
1.23 noro 2022: DP a,b,c,h;
1.27 noro 2023: P p;
1.20 noro 2024:
2025: get_vars((Obj)f,&fv); pltovl(v,&vv);
2026: nd_nvar = length(vv);
2027: initd(ord);
1.23 noro 2028: if ( homo ) {
2029: homogenize_order(ord,nd_nvar,&ord1);
1.27 noro 2030: for ( fd0 = 0, in0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1.23 noro 2031: ptod(CO,vv,(P)BDY(t),&c);
2032: if ( c ) {
2033: dp_homo(c,&h); NEXTNODE(fd0,fd); BDY(fd) = (pointer)h;
1.27 noro 2034: NEXTNODE(in0,in); BDY(in) = (pointer)c;
1.23 noro 2035: }
2036: }
2037: if ( fd0 ) NEXT(fd) = 0;
1.27 noro 2038: if ( in0 ) NEXT(in) = 0;
1.32 noro 2039: nd_init_ord(&ord1);
1.23 noro 2040: initd(&ord1);
2041: nd_nvar++;
1.25 noro 2042: } else {
2043: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
2044: ptod(CO,vv,(P)BDY(t),&c);
2045: if ( c ) {
2046: NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
1.23 noro 2047: }
2048: }
1.25 noro 2049: if ( fd0 ) NEXT(fd) = 0;
1.27 noro 2050: in0 = fd0;
1.33 noro 2051: nd_init_ord(ord);
1.27 noro 2052: }
2053: do {
1.39 noro 2054: nd_setup(m,1,fd0);
1.27 noro 2055: cand = nd_gb_trace(m);
2056: if ( !cand ) continue;
2057: if ( homo ) {
2058: /* dehomogenization */
2059: for ( t = cand; t; t = NEXT(t) )
1.45 noro 2060: ndv_dehomogenize((NDV)BDY(t),ord);
1.27 noro 2061: nd_nvar--;
1.45 noro 2062: initd(ord);
2063: nd_init_ord(ord);
1.27 noro 2064: nd_setup_parameters();
2065: cand = nd_reducebase(cand);
2066: }
2067: fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
2068: nd_found,nd_notfirst,nd_create);
2069: cand = nd_reduceall(0,cand);
2070: initd(ord);
2071: for ( r0 = 0; cand; cand = NEXT(cand) ) {
2072: NEXTNODE(r0,r);
2073: BDY(r) = (pointer)ndvtodp(0,(NDV)BDY(cand));
2074: }
2075: if ( r0 ) NEXT(r) = 0;
2076: cand = r0;
2077: } while ( !nd_check_candidate(in0,cand) );
2078: /* dp->p */
2079: for ( r = cand; r; r = NEXT(r) ) {
2080: dtop(CO,vv,BDY(r),&p);
2081: BDY(r) = (pointer)p;
1.23 noro 2082: }
1.27 noro 2083: MKLIST(*rp,cand);
1.1 noro 2084: }
2085:
2086: void dltondl(int n,DL dl,unsigned int *r)
2087: {
2088: unsigned int *d;
1.43 noro 2089: int i,j,l,s,ord_l,ord_o;
2090: struct order_pair *op;
1.1 noro 2091:
2092: d = dl->d;
1.41 noro 2093: for ( i = 0; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 2094: if ( nd_blockmask ) {
2095: l = nd_blockmask->n;
2096: op = nd_blockmask->order_pair;
2097: for ( j = 0, s = 0; j < l; j++ ) {
2098: ord_o = op[j].order;
2099: ord_l = op[j].length;
2100: if ( !ord_o )
2101: for ( i = 0; i < ord_l; i++ )
2102: PUT_EXP(r,s+ord_l-i-1,d[s+i]);
2103: else
2104: for ( i = 0; i < ord_l; i++ )
2105: PUT_EXP(r,s+i,d[s+i]);
2106: s += ord_l;
2107: }
2108: TD(r) = ndl_weight(r);
2109: for ( j = 0; j < l; j++ )
2110: r[j+1] = ndl_weight_mask(r,j);
2111: } else {
2112: if ( nd_isrlex )
2113: for ( i = 0; i < n; i++ ) PUT_EXP(r,n-1-i,d[i]);
2114: else
2115: for ( i = 0; i < n; i++ ) PUT_EXP(r,i,d[i]);
2116: TD(r) = ndl_weight(r);
2117: }
1.1 noro 2118: }
2119:
1.34 noro 2120: DL ndltodl(int n,unsigned int *ndl)
1.1 noro 2121: {
2122: DL dl;
2123: int *d;
1.43 noro 2124: int i,j,l,s,ord_l,ord_o;
2125: struct order_pair *op;
1.1 noro 2126:
2127: NEWDL(dl,n);
1.34 noro 2128: dl->td = TD(ndl);
1.1 noro 2129: d = dl->d;
1.43 noro 2130: if ( nd_blockmask ) {
2131: l = nd_blockmask->n;
2132: op = nd_blockmask->order_pair;
2133: for ( j = 0, s = 0; j < l; j++ ) {
2134: ord_o = op[j].order;
2135: ord_l = op[j].length;
2136: if ( !ord_o )
2137: for ( i = 0; i < ord_l; i++ )
2138: d[s+i] = GET_EXP(ndl,s+ord_l-i-1);
2139: else
2140: for ( i = 0; i < ord_l; i++ )
2141: d[s+i] = GET_EXP(ndl,s+i);
2142: s += ord_l;
2143: }
2144: } else {
2145: if ( nd_isrlex )
2146: for ( i = 0; i < n; i++ )
2147: d[i] = GET_EXP(ndl,n-1-i);
2148: else
2149: for ( i = 0; i < n; i++ )
2150: d[i] = GET_EXP(ndl,i);
2151: }
1.1 noro 2152: return dl;
2153: }
2154:
1.17 noro 2155: ND dptond(int mod,DP p)
1.1 noro 2156: {
2157: ND d;
2158: NM m0,m;
2159: MP t;
1.31 noro 2160: int n,l;
1.1 noro 2161:
2162: if ( !p )
2163: return 0;
2164: n = NV(p);
2165: m0 = 0;
1.31 noro 2166: for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ ) {
1.1 noro 2167: NEXTNM(m0,m);
1.34 noro 2168: if ( mod ) CM(m) = ITOS(C(t));
2169: else CQ(m) = (Q)C(t);
1.14 noro 2170: dltondl(n,DL(t),DL(m));
1.1 noro 2171: }
2172: NEXT(m) = 0;
1.31 noro 2173: MKND(n,m0,l,d);
1.14 noro 2174: SG(d) = SG(p);
1.1 noro 2175: return d;
2176: }
2177:
1.17 noro 2178: DP ndtodp(int mod,ND p)
1.1 noro 2179: {
2180: DP d;
2181: MP m0,m;
2182: NM t;
2183: int n;
2184:
2185: if ( !p )
2186: return 0;
2187: n = NV(p);
2188: m0 = 0;
2189: for ( t = BDY(p); t; t = NEXT(t) ) {
2190: NEXTMP(m0,m);
1.34 noro 2191: if ( mod ) C(m) = STOI(CM(t));
2192: else C(m) = (P)CQ(t);
2193: DL(m) = ndltodl(n,DL(t));
1.1 noro 2194: }
2195: NEXT(m) = 0;
2196: MKDP(n,m0,d);
1.14 noro 2197: SG(d) = SG(p);
1.1 noro 2198: return d;
2199: }
2200:
2201: void ndl_print(unsigned int *dl)
2202: {
2203: int n;
1.43 noro 2204: int i,j,l,ord_o,ord_l,s,s0;
2205: struct order_pair *op;
1.1 noro 2206:
2207: n = nd_nvar;
2208: printf("<<");
1.43 noro 2209: if ( nd_blockmask ) {
2210: l = nd_blockmask->n;
2211: op = nd_blockmask->order_pair;
2212: for ( j = 0, s = s0 = 0; j < l; j++ ) {
2213: ord_o = op[j].order;
2214: ord_l = op[j].length;
2215: if ( !ord_o )
2216: for ( i = 0, s0 += ord_l; i < ord_l; i++, s++ )
2217: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s0-i-1));
2218: else
2219: for ( i = 0; i < ord_l; i++, s++ )
2220: printf(s==n-1?"%d":"%d,",GET_EXP(dl,s));
2221: }
2222: } else {
2223: if ( nd_isrlex )
2224: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,n-1-i));
2225: else
2226: for ( i = 0; i < n; i++ ) printf(i==n-1?"%d":"%d,",GET_EXP(dl,i));
2227: }
1.1 noro 2228: printf(">>");
2229: }
2230:
2231: void nd_print(ND p)
2232: {
2233: NM m;
2234:
2235: if ( !p )
2236: printf("0\n");
2237: else {
2238: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 noro 2239: printf("+%d*",CM(m));
2240: ndl_print(DL(m));
1.1 noro 2241: }
2242: printf("\n");
2243: }
2244: }
2245:
1.16 noro 2246: void nd_print_q(ND p)
2247: {
2248: NM m;
2249:
2250: if ( !p )
2251: printf("0\n");
2252: else {
2253: for ( m = BDY(p); m; m = NEXT(m) ) {
2254: printf("+");
2255: printexpr(CO,CQ(m));
2256: printf("*");
2257: ndl_print(DL(m));
2258: }
2259: printf("\n");
2260: }
2261: }
2262:
1.1 noro 2263: void ndp_print(ND_pairs d)
2264: {
2265: ND_pairs t;
2266:
1.34 noro 2267: for ( t = d; t; t = NEXT(t) ) printf("%d,%d ",t->i1,t->i2);
1.1 noro 2268: printf("\n");
2269: }
2270:
1.20 noro 2271: void nd_removecont(int mod,ND p)
1.16 noro 2272: {
2273: int i,n;
2274: Q *w;
2275: Q dvr,t;
2276: NM m;
1.21 noro 2277: struct oVECT v;
2278: N q,r;
1.16 noro 2279:
1.34 noro 2280: if ( mod ) nd_mul_c(mod,p,invm(HCM(p),mod));
1.20 noro 2281: else {
2282: for ( m = BDY(p), n = 0; m; m = NEXT(m), n++ );
2283: w = (Q *)ALLOCA(n*sizeof(Q));
1.21 noro 2284: v.len = n;
2285: v.body = (pointer *)w;
1.34 noro 2286: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2287: removecont_array(w,n);
2288: for ( m = BDY(p), i = 0; i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
1.16 noro 2289: }
2290: }
2291:
1.21 noro 2292: void nd_removecont2(ND p1,ND p2)
2293: {
2294: int i,n1,n2,n;
2295: Q *w;
2296: Q dvr,t;
2297: NM m;
2298: struct oVECT v;
2299: N q,r;
2300:
2301: if ( !p1 ) {
2302: nd_removecont(0,p2); return;
2303: } else if ( !p2 ) {
2304: nd_removecont(0,p1); return;
2305: }
2306: n1 = nd_length(p1);
2307: n2 = nd_length(p2);
2308: n = n1+n2;
2309: w = (Q *)ALLOCA(n*sizeof(Q));
2310: v.len = n;
2311: v.body = (pointer *)w;
1.34 noro 2312: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) w[i] = CQ(m);
2313: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) w[i] = CQ(m);
1.21 noro 2314: removecont_array(w,n);
2315: for ( m = BDY(p1), i = 0; i < n1; m = NEXT(m), i++ ) CQ(m) = w[i];
2316: for ( m = BDY(p2); i < n; m = NEXT(m), i++ ) CQ(m) = w[i];
2317: }
2318:
1.20 noro 2319: void ndv_removecont(int mod,NDV p)
1.16 noro 2320: {
2321: int i,len;
2322: Q *w;
2323: Q dvr,t;
2324: NMV m;
2325:
1.20 noro 2326: if ( mod )
2327: ndv_mul_c(mod,p,invm(HCM(p),mod));
2328: else {
2329: len = p->len;
2330: w = (Q *)ALLOCA(len*sizeof(Q));
1.34 noro 2331: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) w[i] = CQ(m);
1.20 noro 2332: sortbynm(w,len);
2333: qltozl(w,len,&dvr);
2334: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ ) {
2335: divq(CQ(m),dvr,&t); CQ(m) = t;
2336: }
1.16 noro 2337: }
1.21 noro 2338: }
2339:
1.45 noro 2340: void ndv_dehomogenize(NDV p,struct order_spec *ord)
1.23 noro 2341: {
1.45 noro 2342: int i,j,adj,len,newnvar,newwpd,newadv,newexporigin;
1.23 noro 2343: Q *w;
2344: Q dvr,t;
2345: NMV m,r;
2346: #define NEWADV(m) (m = (NMV)(((char *)m)+newadv))
2347:
2348: len = p->len;
2349: newnvar = nd_nvar-1;
1.45 noro 2350: newexporigin = ord->ord.block.length+1;
2351: newwpd = newnvar/nd_epw+(newnvar%nd_epw?1:0)+newexporigin;
1.23 noro 2352: for ( m = BDY(p), i = 0; i < len; NMV_ADV(m), i++ )
1.34 noro 2353: ndl_dehomogenize(DL(m));
1.23 noro 2354: if ( newwpd != nd_wpd ) {
1.41 noro 2355: newadv = sizeof(struct oNMV)+(newwpd-1)*sizeof(unsigned int);
1.23 noro 2356: for ( m = r = BDY(p), i = 0; i < len; NMV_ADV(m), NEWADV(r), i++ ) {
1.45 noro 2357: CQ(r) = CQ(m);
2358: for ( j = 0; j < newexporigin; j++ ) DL(r)[j] = DL(m)[j];
2359: adj = nd_exporigin-newexporigin;
2360: for ( ; j < newwpd; j++ ) DL(r)[j] = DL(m)[j+adj];
1.23 noro 2361: }
2362: }
2363: NV(p)--;
2364: }
2365:
1.21 noro 2366: void removecont_array(Q *c,int n)
2367: {
2368: struct oVECT v;
2369: Q d0,d1,a,u,u1,gcd;
2370: int i;
2371: N qn,rn,gn;
2372: Q *q,*r;
2373:
2374: q = (Q *)ALLOCA(n*sizeof(Q));
2375: r = (Q *)ALLOCA(n*sizeof(Q));
2376: v.id = O_VECT; v.len = n; v.body = (pointer *)c;
2377: igcdv_estimate(&v,&d0);
2378: for ( i = 0; i < n; i++ ) {
2379: divn(NM(c[i]),NM(d0),&qn,&rn);
2380: NTOQ(qn,SGN(c[i])*SGN(d0),q[i]);
2381: NTOQ(rn,SGN(c[i]),r[i]);
2382: }
1.34 noro 2383: for ( i = 0; i < n; i++ ) if ( r[i] ) break;
1.21 noro 2384: if ( i < n ) {
2385: v.id = O_VECT; v.len = n; v.body = (pointer *)r;
2386: igcdv(&v,&d1);
2387: gcdn(NM(d0),NM(d1),&gn); NTOQ(gn,1,gcd);
2388: divsn(NM(d0),gn,&qn); NTOQ(qn,1,a);
2389: for ( i = 0; i < n; i++ ) {
2390: mulq(a,q[i],&u);
2391: if ( r[i] ) {
2392: divsn(NM(r[i]),gn,&qn); NTOQ(qn,SGN(r[i]),u1);
2393: addq(u,u1,&q[i]);
2394: } else
2395: q[i] = u;
2396: }
2397: }
1.34 noro 2398: for ( i = 0; i < n; i++ ) c[i] = q[i];
1.16 noro 2399: }
2400:
1.19 noro 2401: void nd_mul_c(int mod,ND p,int mul)
1.1 noro 2402: {
2403: NM m;
2404: int c,c1;
2405:
1.34 noro 2406: if ( !p ) return;
1.1 noro 2407: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 noro 2408: c1 = CM(m);
1.19 noro 2409: DMAR(c1,mul,0,mod,c);
1.14 noro 2410: CM(m) = c;
1.1 noro 2411: }
2412: }
2413:
1.16 noro 2414: void nd_mul_c_q(ND p,Q mul)
2415: {
2416: NM m;
2417: Q c;
2418:
1.34 noro 2419: if ( !p ) return;
1.16 noro 2420: for ( m = BDY(p); m; m = NEXT(m) ) {
2421: mulq(CQ(m),mul,&c); CQ(m) = c;
2422: }
2423: }
2424:
1.1 noro 2425: void nd_free(ND p)
2426: {
2427: NM t,s;
2428:
1.34 noro 2429: if ( !p ) return;
1.1 noro 2430: t = BDY(p);
2431: while ( t ) {
2432: s = NEXT(t);
2433: FREENM(t);
2434: t = s;
2435: }
2436: FREEND(p);
2437: }
2438:
1.23 noro 2439: void ndv_free(NDV p)
2440: {
2441: GC_free(BDY(p));
2442: }
2443:
1.34 noro 2444: void nd_append_red(unsigned int *d,int i)
1.1 noro 2445: {
1.13 noro 2446: RHist m,m0;
1.1 noro 2447: int h;
2448:
1.13 noro 2449: NEWRHist(m);
1.34 noro 2450: h = ndl_hash_value(d);
1.13 noro 2451: m->index = i;
1.14 noro 2452: ndl_copy(d,DL(m));
1.1 noro 2453: NEXT(m) = nd_red[h];
2454: nd_red[h] = m;
2455: }
2456:
1.5 noro 2457: unsigned int *dp_compute_bound(DP p)
2458: {
2459: unsigned int *d,*d1,*d2,*t;
2460: MP m;
1.7 noro 2461: int i,l;
1.5 noro 2462:
2463: if ( !p )
2464: return 0;
2465: d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
2466: d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
2467: m = BDY(p);
1.35 noro 2468: d = DL(m)->d;
2469: for ( i = 0; i < nd_nvar; i++ ) d1[i] = d[i];
1.5 noro 2470: for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14 noro 2471: d = DL(m)->d;
1.5 noro 2472: for ( i = 0; i < nd_nvar; i++ )
2473: d2[i] = d[i] > d1[i] ? d[i] : d1[i];
2474: t = d1; d1 = d2; d2 = t;
2475: }
1.13 noro 2476: l = (nd_nvar+31);
1.7 noro 2477: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1.35 noro 2478: for ( i = 0; i < nd_nvar; i++ ) t[i] = d1[i];
2479: for ( ; i < l; i++ ) t[i] = 0;
1.5 noro 2480: return t;
2481: }
2482:
1.45 noro 2483: unsigned int *ndv_compute_bound(NDV p)
1.1 noro 2484: {
2485: unsigned int *d1,*d2,*t;
1.45 noro 2486: int i,l,len;
2487: NMV m;
1.1 noro 2488:
2489: if ( !p )
2490: return 0;
2491: d1 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
2492: d2 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1.45 noro 2493: len = LEN(p);
2494: m = BDY(p); ndl_copy(DL(m),d1); NMV_ADV(m);
2495: for ( i = 1; i < len; i++, NMV_ADV(m) ) {
1.14 noro 2496: ndl_lcm(DL(m),d1,d2);
1.1 noro 2497: t = d1; d1 = d2; d2 = t;
2498: }
1.12 noro 2499: l = nd_nvar+31;
1.9 noro 2500: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1.42 noro 2501: for ( i = 0; i < nd_nvar; i++ ) t[i] = GET_EXP(d1,i);
1.35 noro 2502: for ( ; i < l; i++ ) t[i] = 0;
1.1 noro 2503: return t;
2504: }
2505:
2506: void nd_setup_parameters() {
1.43 noro 2507: int i,n,elen;
1.1 noro 2508:
2509: nd_epw = (sizeof(unsigned int)*8)/nd_bpe;
1.43 noro 2510: elen = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
2511:
1.41 noro 2512: switch ( nd_ord->id ) {
2513: case 0:
2514: nd_exporigin = 1;
2515: break;
2516: case 1:
2517: /* block order */
1.43 noro 2518: /* d[0]:weight d[1]:w0,...,d[nd_exporigin-1]:w(n-1) */
2519: nd_exporigin = nd_ord->ord.block.length+1;
1.41 noro 2520: break;
2521: case 2:
2522: error("nd_setup_parameters : matrix order is not supported yet.");
2523: break;
2524: }
1.43 noro 2525: nd_wpd = nd_exporigin+elen;
2526: nd_epos = (EPOS)MALLOC_ATOMIC(nd_nvar*sizeof(struct oEPOS));
2527: for ( i = 0; i < nd_nvar; i++ ) {
2528: nd_epos[i].i = nd_exporigin + i/nd_epw;
2529: nd_epos[i].s = (nd_epw-(i%nd_epw)-1)*nd_bpe;
2530: }
1.1 noro 2531: if ( nd_bpe < 32 ) {
2532: nd_mask0 = (1<<nd_bpe)-1;
2533: } else {
2534: nd_mask0 = 0xffffffff;
2535: }
2536: bzero(nd_mask,sizeof(nd_mask));
2537: nd_mask1 = 0;
2538: for ( i = 0; i < nd_epw; i++ ) {
2539: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
2540: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
2541: }
1.41 noro 2542: nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
2543: nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int);
1.43 noro 2544: nd_blockmask = nd_create_blockmask(nd_ord);
1.1 noro 2545: }
2546:
1.20 noro 2547: ND_pairs nd_reconstruct(int mod,int trace,ND_pairs d)
1.1 noro 2548: {
1.37 noro 2549: int i,obpe,oadv,h;
1.13 noro 2550: NM prev_nm_free_list;
2551: RHist mr0,mr;
2552: RHist r;
1.37 noro 2553: RHist *old_red;
1.1 noro 2554: ND_pairs s0,s,t,prev_ndp_free_list;
1.43 noro 2555: EPOS oepos;
1.15 noro 2556:
1.1 noro 2557: obpe = nd_bpe;
1.11 noro 2558: oadv = nmv_adv;
1.43 noro 2559: oepos = nd_epos;
1.34 noro 2560: if ( obpe < 4 ) nd_bpe = 4;
2561: else if ( obpe < 6 ) nd_bpe = 6;
2562: else if ( obpe < 8 ) nd_bpe = 8;
2563: else if ( obpe < 16 ) nd_bpe = 16;
2564: else if ( obpe < 32 ) nd_bpe = 32;
2565: else error("nd_reconstruct : exponent too large");
1.5 noro 2566:
1.1 noro 2567: nd_setup_parameters();
2568: prev_nm_free_list = _nm_free_list;
2569: prev_ndp_free_list = _ndp_free_list;
2570: _nm_free_list = 0;
2571: _ndp_free_list = 0;
1.20 noro 2572: if ( mod != 0 )
1.43 noro 2573: for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_ps[i],obpe,oadv,oepos);
1.20 noro 2574: if ( !mod || trace )
1.43 noro 2575: for ( i = nd_psn-1; i >= 0; i-- ) ndv_realloc(nd_psq[i],obpe,oadv,oepos);
1.1 noro 2576: s0 = 0;
2577: for ( t = d; t; t = NEXT(t) ) {
2578: NEXTND_pairs(s0,s);
2579: s->i1 = t->i1;
2580: s->i2 = t->i2;
1.14 noro 2581: SG(s) = SG(t);
1.43 noro 2582: ndl_reconstruct(obpe,oepos,LCM(t),LCM(s));
1.1 noro 2583: }
1.37 noro 2584:
2585: old_red = (RHist *)ALLOCA(REDTAB_LEN*sizeof(RHist));
1.6 noro 2586: for ( i = 0; i < REDTAB_LEN; i++ ) {
1.37 noro 2587: old_red[i] = nd_red[i];
2588: nd_red[i] = 0;
2589: }
2590: for ( i = 0; i < REDTAB_LEN; i++ )
2591: for ( r = old_red[i]; r; r = NEXT(r) ) {
2592: NEWRHist(mr);
1.13 noro 2593: mr->index = r->index;
1.20 noro 2594: SG(mr) = SG(r);
1.43 noro 2595: ndl_reconstruct(obpe,oepos,DL(r),DL(mr));
1.37 noro 2596: h = ndl_hash_value(DL(mr));
2597: NEXT(mr) = nd_red[h];
2598: nd_red[h] = mr;
1.6 noro 2599: }
1.37 noro 2600: for ( i = 0; i < REDTAB_LEN; i++ ) old_red[i] = 0;
2601: old_red = 0;
1.11 noro 2602: for ( i = 0; i < nd_psn; i++ ) {
1.20 noro 2603: NEWRHist(r); SG(r) = SG(nd_psh[i]);
1.43 noro 2604: ndl_reconstruct(obpe,oepos,DL(nd_psh[i]),DL(r));
1.13 noro 2605: nd_psh[i] = r;
1.11 noro 2606: }
1.1 noro 2607: if ( s0 ) NEXT(s) = 0;
2608: prev_nm_free_list = 0;
2609: prev_ndp_free_list = 0;
2610: GC_gcollect();
2611: return s0;
2612: }
2613:
1.23 noro 2614: void nd_reconstruct_direct(int mod,NDV *ps,int len)
2615: {
1.37 noro 2616: int i,obpe,oadv,h;
1.45 noro 2617: unsigned int **bound;
1.23 noro 2618: NM prev_nm_free_list;
2619: RHist mr0,mr;
2620: RHist r;
1.37 noro 2621: RHist *old_red;
1.23 noro 2622: ND_pairs s0,s,t,prev_ndp_free_list;
1.43 noro 2623: EPOS oepos;
1.23 noro 2624:
2625: obpe = nd_bpe;
2626: oadv = nmv_adv;
1.43 noro 2627: oepos = nd_epos;
1.34 noro 2628: if ( obpe < 4 ) nd_bpe = 4;
2629: else if ( obpe < 6 ) nd_bpe = 6;
2630: else if ( obpe < 8 ) nd_bpe = 8;
2631: else if ( obpe < 16 ) nd_bpe = 16;
2632: else if ( obpe < 32 ) nd_bpe = 32;
2633: else error("nd_reconstruct_direct : exponent too large");
1.23 noro 2634:
2635: nd_setup_parameters();
2636: prev_nm_free_list = _nm_free_list;
2637: prev_ndp_free_list = _ndp_free_list;
1.34 noro 2638: _nm_free_list = 0; _ndp_free_list = 0;
1.43 noro 2639: for ( i = len-1; i >= 0; i-- ) ndv_realloc(ps[i],obpe,oadv,oepos);
1.23 noro 2640: prev_nm_free_list = 0;
2641: prev_ndp_free_list = 0;
2642: GC_gcollect();
2643: }
2644:
1.43 noro 2645: void ndl_reconstruct(int obpe,EPOS oepos,unsigned int *d,unsigned int *r)
1.1 noro 2646: {
1.43 noro 2647: int n,i,ei,oepw,omask0,j,s,ord_l,ord_o,l;
2648: struct order_pair *op;
2649: #define GET_EXP_OLD(d,a) (((d)[oepos[a].i]>>oepos[a].s)&omask0)
2650: #define PUT_EXP_OLD(r,a,e) ((r)[oepos[a].i] |= ((e)<<oepos[a].s))
1.1 noro 2651:
2652: n = nd_nvar;
2653: oepw = (sizeof(unsigned int)*8)/obpe;
1.43 noro 2654: omask0 = (1<<obpe)-1;
1.34 noro 2655: TD(r) = TD(d);
1.41 noro 2656: for ( i = nd_exporigin; i < nd_wpd; i++ ) r[i] = 0;
1.43 noro 2657: if ( nd_blockmask ) {
2658: l = nd_blockmask->n;
2659: op = nd_blockmask->order_pair;
2660: for ( i = 1; i < nd_exporigin; i++ )
2661: r[i] = d[i];
2662: for ( j = 0, s = 0; j < l; j++ ) {
2663: ord_o = op[j].order;
2664: ord_l = op[j].length;
2665: if ( !ord_o )
2666: for ( i = 0; i < ord_l; i++ ) {
2667: ei = GET_EXP_OLD(d,s+ord_l-i-1);
2668: PUT_EXP(r,s+ord_l-i-1,ei);
2669: }
2670: else
2671: for ( i = 0; i < ord_l; i++ ) {
2672: ei = GET_EXP_OLD(d,s+i);
2673: PUT_EXP(r,s+i,ei);
2674: }
2675: s += ord_l;
1.1 noro 2676: }
1.43 noro 2677: } else {
2678: if ( nd_isrlex )
2679: for ( i = 0; i < n; i++ ) {
2680: ei = GET_EXP_OLD(d,n-1-i);
2681: PUT_EXP(r,n-1-i,ei);
2682: }
2683: else
2684: for ( i = 0; i < n; i++ ) {
2685: ei = GET_EXP_OLD(d,i);
2686: PUT_EXP(r,i,ei);
2687: }
1.1 noro 2688: }
2689: }
1.3 noro 2690:
1.6 noro 2691: ND nd_copy(ND p)
2692: {
2693: NM m,mr,mr0;
1.41 noro 2694: int c,n;
1.6 noro 2695: ND r;
2696:
2697: if ( !p )
2698: return 0;
2699: else {
2700: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
2701: NEXTNM(mr0,mr);
1.14 noro 2702: CM(mr) = CM(m);
2703: ndl_copy(DL(m),DL(mr));
1.6 noro 2704: }
2705: NEXT(mr) = 0;
1.31 noro 2706: MKND(NV(p),mr0,LEN(p),r);
1.14 noro 2707: SG(r) = SG(p);
1.6 noro 2708: return r;
2709: }
2710: }
2711:
1.16 noro 2712: int nd_sp(int mod,ND_pairs p,ND *rp)
1.11 noro 2713: {
2714: NM m;
2715: NDV p1,p2;
2716: ND t1,t2;
2717: unsigned int *lcm;
1.31 noro 2718: int td;
1.11 noro 2719:
1.20 noro 2720: if ( mod ) {
2721: p1 = nd_ps[p->i1]; p2 = nd_ps[p->i2];
2722: } else {
2723: p1 = nd_psq[p->i1]; p2 = nd_psq[p->i2];
2724: }
1.34 noro 2725: lcm = LCM(p);
1.11 noro 2726: NEWNM(m);
1.20 noro 2727: CQ(m) = HCQ(p2);
1.34 noro 2728: ndl_sub(lcm,HDL(p1),DL(m));
2729: if ( ndl_check_bound2(p->i1,DL(m)) ) return 0;
1.25 noro 2730: t1 = ndv_mul_nm(mod,p1,m);
1.34 noro 2731: if ( mod ) CM(m) = mod-HCM(p1);
2732: else chsgnq(HCQ(p1),&CQ(m));
2733: ndl_sub(lcm,HDL(p2),DL(m));
1.14 noro 2734: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 2735: nd_free(t1);
2736: return 0;
2737: }
1.25 noro 2738: t2 = ndv_mul_nm(mod,p2,m);
1.31 noro 2739: *rp = nd_add(mod,t1,t2);
1.11 noro 2740: FREENM(m);
2741: return 1;
2742: }
2743:
1.19 noro 2744: void ndv_mul_c(int mod,NDV p,int mul)
1.11 noro 2745: {
2746: NMV m;
2747: int c,c1,len,i;
2748:
1.34 noro 2749: if ( !p ) return;
1.14 noro 2750: len = LEN(p);
1.11 noro 2751: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.34 noro 2752: c1 = CM(m); DMAR(c1,mul,0,mod,c); CM(m) = c;
1.11 noro 2753: }
2754: }
2755:
1.16 noro 2756: void ndv_mul_c_q(NDV p,Q mul)
2757: {
2758: NMV m;
2759: Q c;
2760: int len,i;
2761:
1.34 noro 2762: if ( !p ) return;
1.16 noro 2763: len = LEN(p);
2764: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
2765: mulq(CQ(m),mul,&c); CQ(m) = c;
2766: }
2767: }
2768:
1.25 noro 2769: ND ndv_mul_nm(int mod,NDV p,NM m0)
1.9 noro 2770: {
2771: NM mr,mr0;
2772: NMV m;
2773: unsigned int *d,*dt,*dm;
2774: int c,n,td,i,c1,c2,len;
1.16 noro 2775: Q q;
1.9 noro 2776: ND r;
2777:
1.34 noro 2778: if ( !p ) return 0;
1.9 noro 2779: else {
2780: n = NV(p); m = BDY(p);
1.34 noro 2781: d = DL(m0);
1.14 noro 2782: len = LEN(p);
1.9 noro 2783: mr0 = 0;
1.34 noro 2784: td = TD(d);
1.16 noro 2785: if ( mod ) {
2786: c = CM(m0);
2787: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
2788: NEXTNM(mr0,mr);
2789: c1 = CM(m);
1.19 noro 2790: DMAR(c1,c,0,mod,c2);
1.16 noro 2791: CM(mr) = c2;
2792: ndl_add(DL(m),d,DL(mr));
2793: }
2794: } else {
2795: q = CQ(m0);
2796: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
2797: NEXTNM(mr0,mr);
2798: mulq(CQ(m),q,&CQ(mr));
2799: ndl_add(DL(m),d,DL(mr));
2800: }
1.4 noro 2801: }
1.9 noro 2802: NEXT(mr) = 0;
1.31 noro 2803: MKND(NV(p),mr0,len,r);
1.34 noro 2804: SG(r) = SG(p) + TD(d);
1.9 noro 2805: return r;
1.4 noro 2806: }
2807: }
2808:
1.43 noro 2809: void ndv_realloc(NDV p,int obpe,int oadv,EPOS oepos)
1.11 noro 2810: {
1.13 noro 2811: NMV m,mr,mr0,t;
2812: int len,i,k;
1.11 noro 2813:
1.13 noro 2814: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
2815: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.11 noro 2816:
2817: if ( p ) {
1.14 noro 2818: m = BDY(p); len = LEN(p);
1.34 noro 2819: mr0 = nmv_adv>oadv?(NMV)REALLOC(BDY(p),len*nmv_adv):BDY(p);
1.13 noro 2820: m = (NMV)((char *)mr0+(len-1)*oadv);
2821: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
2822: t = (NMV)ALLOCA(nmv_adv);
2823: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.16 noro 2824: CQ(t) = CQ(m);
1.14 noro 2825: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
1.43 noro 2826: ndl_reconstruct(obpe,oepos,DL(m),DL(t));
1.16 noro 2827: CQ(mr) = CQ(t);
1.14 noro 2828: ndl_copy(DL(t),DL(mr));
1.11 noro 2829: }
2830: BDY(p) = mr0;
2831: }
2832: }
2833:
1.16 noro 2834: NDV ndtondv(int mod,ND p)
1.3 noro 2835: {
2836: NDV d;
2837: NMV m,m0;
2838: NM t;
2839: int i,len;
2840:
1.34 noro 2841: if ( !p ) return 0;
1.31 noro 2842: len = LEN(p);
1.34 noro 2843: m0 = m = (NMV)(mod?MALLOC_ATOMIC(len*nmv_adv):MALLOC(len*nmv_adv));
1.3 noro 2844: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
1.14 noro 2845: ndl_copy(DL(t),DL(m));
1.16 noro 2846: CQ(m) = CQ(t);
1.3 noro 2847: }
2848: MKNDV(NV(p),m0,len,d);
1.23 noro 2849: SG(d) = SG(p);
2850: return d;
2851: }
2852:
2853: ND ndvtond(int mod,NDV p)
2854: {
2855: ND d;
2856: NM m,m0;
2857: NMV t;
2858: int i,len;
2859:
1.34 noro 2860: if ( !p ) return 0;
1.23 noro 2861: m0 = 0;
2862: len = p->len;
2863: for ( t = BDY(p), i = 0; i < len; NMV_ADV(t), i++ ) {
2864: NEXTNM(m0,m);
2865: ndl_copy(DL(t),DL(m));
2866: CQ(m) = CQ(t);
2867: }
2868: NEXT(m) = 0;
1.31 noro 2869: MKND(NV(p),m0,len,d);
1.14 noro 2870: SG(d) = SG(p);
1.3 noro 2871: return d;
2872: }
2873:
1.16 noro 2874: NDV dptondv(int mod,DP p)
1.11 noro 2875: {
2876: NDV d;
2877: NMV m0,m;
2878: MP t;
1.20 noro 2879: DP q;
1.11 noro 2880: int l,i,n;
2881:
1.46 ! noro 2882: if ( mod ) {
! 2883: _dp_mod(p,mod,0,&q); p = q;
! 2884: }
1.34 noro 2885: if ( !p ) return 0;
1.11 noro 2886: for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ );
1.46 ! noro 2887: if ( mod )
1.16 noro 2888: m0 = m = (NMV)MALLOC_ATOMIC(l*nmv_adv);
1.46 ! noro 2889: else
1.16 noro 2890: m0 = m = (NMV)MALLOC(l*nmv_adv);
1.11 noro 2891: n = NV(p);
1.46 ! noro 2892: for ( t = BDY(p); t; t = NEXT(t), NMV_ADV(m) ) {
1.34 noro 2893: if ( mod ) CM(m) = ITOS(C(t));
2894: else CQ(m) = (Q)C(t);
1.17 noro 2895: dltondl(n,DL(t),DL(m));
1.11 noro 2896: }
2897: MKNDV(n,m0,l,d);
1.14 noro 2898: SG(d) = SG(p);
1.11 noro 2899: return d;
2900: }
2901:
1.16 noro 2902: DP ndvtodp(int mod,NDV p)
1.11 noro 2903: {
2904: DP d;
2905: MP m0,m;
2906: NMV t;
2907: int len,i,n;
2908:
1.34 noro 2909: if ( !p ) return 0;
1.11 noro 2910: m0 = 0;
1.14 noro 2911: len = LEN(p);
1.11 noro 2912: n = NV(p);
1.17 noro 2913: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
2914: NEXTMP(m0,m);
1.34 noro 2915: if ( mod ) C(m) = STOI(CM(t));
2916: else C(m) = (P)CQ(t);
2917: DL(m) = ndltodl(n,DL(t));
1.11 noro 2918: }
2919: NEXT(m) = 0;
2920: MKDP(NV(p),m0,d);
1.14 noro 2921: SG(d) = SG(p);
1.11 noro 2922: return d;
2923: }
2924:
1.3 noro 2925: void ndv_print(NDV p)
2926: {
2927: NMV m;
2928: int i,len;
2929:
1.34 noro 2930: if ( !p ) printf("0\n");
1.3 noro 2931: else {
1.14 noro 2932: len = LEN(p);
1.3 noro 2933: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14 noro 2934: printf("+%d*",CM(m));
1.16 noro 2935: ndl_print(DL(m));
2936: }
2937: printf("\n");
2938: }
2939: }
2940:
2941: void ndv_print_q(NDV p)
2942: {
2943: NMV m;
2944: int i,len;
2945:
1.34 noro 2946: if ( !p ) printf("0\n");
1.16 noro 2947: else {
2948: len = LEN(p);
2949: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
2950: printf("+");
2951: printexpr(CO,CQ(m));
2952: printf("*");
1.14 noro 2953: ndl_print(DL(m));
1.3 noro 2954: }
2955: printf("\n");
2956: }
1.25 noro 2957: }
2958:
1.27 noro 2959: NODE nd_reducebase(NODE x)
2960: {
2961: int len,i,j;
2962: NDV *w;
2963: NODE t,t0;
2964:
2965: len = length(x);
2966: w = (NDV *)ALLOCA(len*sizeof(NDV));
2967: for ( i = 0, t = x; i < len; i++, t = NEXT(t) ) w[i] = BDY(t);
2968: for ( i = 0; i < len; i++ ) {
2969: for ( j = 0; j < i; j++ ) {
2970: if ( w[i] && w[j] )
2971: if ( ndl_reducible(HDL(w[i]),HDL(w[j])) ) w[i] = 0;
2972: else if ( ndl_reducible(HDL(w[j]),HDL(w[i])) ) w[j] = 0;
2973: }
2974: }
2975: for ( i = len-1, t0 = 0; i >= 0; i-- ) {
2976: if ( w[i] ) { NEXTNODE(t0,t); BDY(t) = (pointer)w[i]; }
2977: }
2978: NEXT(t) = 0; x = t0;
2979: return x;
1.11 noro 2980: }
1.32 noro 2981:
1.43 noro 2982: /* XXX incomplete */
2983:
1.32 noro 2984: void nd_init_ord(struct order_spec *ord)
2985: {
1.43 noro 2986: switch ( ord->id ) {
1.32 noro 2987: case 0:
1.43 noro 2988: switch ( ord->ord.simple ) {
2989: case 0:
2990: nd_dcomp = 1;
2991: nd_isrlex = 1;
2992: break;
2993: case 1:
2994: nd_dcomp = 1;
2995: nd_isrlex = 0;
2996: break;
2997: case 2:
2998: nd_dcomp = 0;
2999: nd_isrlex = 0;
1.45 noro 3000: ndl_compare_function = ndl_lex_compare;
1.43 noro 3001: break;
3002: default:
3003: error("nd_gr : unsupported order");
3004: }
1.32 noro 3005: break;
3006: case 1:
1.43 noro 3007: /* XXX */
3008: nd_dcomp = -1;
1.32 noro 3009: nd_isrlex = 0;
1.45 noro 3010: ndl_compare_function = ndl_block_compare;
1.34 noro 3011: break;
1.43 noro 3012: case 2:
3013: error("nd_init_ord : matrix order is not supported yet.");
1.32 noro 3014: break;
3015: }
1.41 noro 3016: nd_ord = ord;
1.32 noro 3017: }
3018:
1.43 noro 3019: BlockMask nd_create_blockmask(struct order_spec *ord)
3020: {
3021: int n,i,j,s,l;
3022: unsigned int *t;
3023: BlockMask bm;
3024:
3025: if ( !ord->id )
3026: return 0;
3027: n = ord->ord.block.length;
3028: bm = (BlockMask)MALLOC(sizeof(struct oBlockMask));
3029: bm->n = n;
3030: bm->order_pair = ord->ord.block.order_pair;
3031: bm->mask = (unsigned int **)MALLOC(n*sizeof(unsigned int *));
3032: for ( i = 0, s = 0; i < n; i++ ) {
3033: bm->mask[i] = t
3034: = (unsigned int *)MALLOC_ATOMIC(nd_wpd*sizeof(unsigned int));
3035: for ( j = 0; j < nd_wpd; j++ ) t[j] = 0;
3036: l = bm->order_pair[i].length;
3037: for ( j = 0; j < l; j++, s++ ) PUT_EXP(t,s,nd_mask0);
3038: }
3039: return bm;
3040: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>