Annotation of OpenXM_contrib2/asir2000/engine/nd.c, Revision 1.14
1.14 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/nd.c,v 1.13 2003/07/30 00:55:41 noro Exp $ */
1.2 noro 2:
1.1 noro 3: #include "ca.h"
4: #include "inline.h"
5:
1.6 noro 6: #define USE_NDV 1
1.4 noro 7:
1.1 noro 8: #if defined(__GNUC__)
9: #define INLINE inline
10: #elif defined(VISUAL)
11: #define INLINE __inline
12: #else
13: #define INLINE
14: #endif
15:
16: #define REDTAB_LEN 32003
17:
18: typedef struct oPGeoBucket {
19: int m;
20: struct oND *body[32];
21: } *PGeoBucket;
22:
23: typedef struct oND {
24: struct oNM *body;
25: int nv;
26: int sugar;
27: } *ND;
28:
1.3 noro 29: typedef struct oNDV {
30: struct oNMV *body;
31: int nv;
32: int sugar;
33: int len;
34: } *NDV;
35:
1.1 noro 36: typedef struct oNM {
37: struct oNM *next;
1.14 ! noro 38: union {
! 39: int m;
! 40: Q z;
! 41: } c;
1.1 noro 42: int td;
43: unsigned int dl[1];
44: } *NM;
45:
1.3 noro 46: typedef struct oNMV {
1.14 ! noro 47: union {
! 48: int m;
! 49: Q z;
! 50: } c;
1.3 noro 51: int td;
52: unsigned int dl[1];
53: } *NMV;
54:
1.13 noro 55: typedef struct oRHist {
56: struct oRHist *next;
57: int index;
58: int td;
59: unsigned int dl[1];
60: } *RHist;
61:
1.1 noro 62: typedef struct oND_pairs {
63: struct oND_pairs *next;
64: int i1,i2;
65: int td,sugar;
66: unsigned int lcm[1];
67: } *ND_pairs;
68:
69: static unsigned int **nd_bound;
70: int nd_mod,nd_nvar;
71: int is_rlex;
72: int nd_epw,nd_bpe,nd_wpd;
73: unsigned int nd_mask[32];
74: unsigned int nd_mask0,nd_mask1;
75: NM _nm_free_list;
76: ND _nd_free_list;
77: ND_pairs _ndp_free_list;
1.13 noro 78: RHist *nd_red;
1.1 noro 79: int nd_red_len;
80:
81: extern int Top,Reverse;
82: int nd_psn,nd_pslen;
83: int nd_found,nd_create,nd_notfirst;
1.6 noro 84: int *nd_psl;
1.13 noro 85: RHist *nd_psh;
86: int nm_adv;
87: #define NM_ADV(m) (m = (NM)(((char *)m)+nm_adv))
1.1 noro 88:
89: void GC_gcollect();
90: NODE append_one(NODE,int);
91:
92: #define HTD(d) ((d)->body->td)
93: #define HDL(d) ((d)->body->dl)
1.14 ! noro 94: #define HCM(d) ((d)->body->c.m)
! 95: #define CM(a) ((a)->c.m)
! 96: #define DL(a) ((a)->dl)
! 97: #define TD(a) ((a)->td)
! 98: #define SG(a) ((a)->sugar)
! 99: #define LEN(a) ((a)->len)
1.1 noro 100:
1.13 noro 101: #define NEWRHist(r) ((r)=(RHist)MALLOC(sizeof(struct oRHist)))
1.1 noro 102: #define NEWND_pairs(m) if(!_ndp_free_list)_NDP_alloc(); (m)=_ndp_free_list; _ndp_free_list = NEXT(_ndp_free_list)
103: #define NEWNM(m) if(!_nm_free_list)_NM_alloc(); (m)=_nm_free_list; _nm_free_list = NEXT(_nm_free_list)
104: #define MKND(n,m,d) if(!_nd_free_list)_ND_alloc(); (d)=_nd_free_list; _nd_free_list = (ND)BDY(_nd_free_list); (d)->nv=(n); BDY(d)=(m)
105:
1.13 noro 106: #define NEXTRHist(r,c) \
107: if(!(r)){NEWRHist(r);(c)=(r);}else{NEWRHist(NEXT(c));(c)=NEXT(c);}
1.1 noro 108: #define NEXTNM(r,c) \
109: if(!(r)){NEWNM(r);(c)=(r);}else{NEWNM(NEXT(c));(c)=NEXT(c);}
110: #define NEXTNM2(r,c,s) \
111: if(!(r)){(c)=(r)=(s);}else{NEXT(c)=(s);(c)=(s);}
112: #define FREENM(m) NEXT(m)=_nm_free_list; _nm_free_list=(m)
113: #define FREENDP(m) NEXT(m)=_ndp_free_list; _ndp_free_list=(m)
114: #define FREEND(m) BDY(m)=(NM)_nd_free_list; _nd_free_list=(m)
115:
116: #define NEXTND_pairs(r,c) \
117: if(!(r)){NEWND_pairs(r);(c)=(r);}else{NEWND_pairs(NEXT(c));(c)=NEXT(c);}
118:
119: ND_pairs crit_B( ND_pairs d, int s );
120: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp);
121: NODE nd_setup(NODE f);
122: int nd_newps(ND a);
123: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest );
124: NODE update_base(NODE nd,int ndp);
125: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest );
126: int crit_2( int dp1, int dp2 );
127: ND_pairs crit_F( ND_pairs d1 );
128: ND_pairs crit_M( ND_pairs d1 );
129: ND_pairs nd_newpairs( NODE g, int t );
130: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t);
131: NODE nd_gb(NODE f);
132: void nd_free_private_storage();
133: void _NM_alloc();
134: void _ND_alloc();
135: int ndl_td(unsigned int *d);
136: ND nd_add(ND p1,ND p2);
137: ND nd_mul_nm(ND p,NM m0);
1.6 noro 138: ND nd_mul_ind_nm(int index,NM m0);
1.1 noro 139: ND nd_mul_term(ND p,int td,unsigned int *d);
140: int nd_sp(ND_pairs p,ND *nf);
1.6 noro 141: int nd_find_reducer(ND g);
1.1 noro 142: int nd_nf(ND g,int full,ND *nf);
143: ND nd_reduce(ND p1,ND p2);
144: ND nd_reduce_special(ND p1,ND p2);
145: void nd_free(ND p);
146: void ndl_print(unsigned int *dl);
147: void nd_print(ND p);
148: void ndp_print(ND_pairs d);
149: int nd_length(ND p);
150: void nd_monic(ND p);
151: void nd_mul_c(ND p,int mul);
152: void nd_free_redlist();
153: void nd_append_red(unsigned int *d,int td,int i);
154: unsigned int *nd_compute_bound(ND p);
1.5 noro 155: unsigned int *dp_compute_bound(DP p);
1.1 noro 156: ND_pairs nd_reconstruct(ND_pairs);
157: void nd_setup_parameters();
1.11 noro 158: void nd_realloc(ND p,int obpe);
1.6 noro 159: ND nd_copy(ND p);
1.1 noro 160: void ndl_dup(int obpe,unsigned int *d,unsigned int *r);
1.4 noro 161:
162: #if USE_NDV
1.11 noro 163: static NDV *nd_ps;
164:
1.4 noro 165: #define NMV_ADV(m) (m = (NMV)(((char *)m)+nmv_adv))
166: #define NEWNDV(d) ((d)=(NDV)MALLOC(sizeof(struct oNDV)))
1.14 ! noro 167: #define MKNDV(n,m,l,d) NEWNDV(d); NV(d)=(n); BDY(d)=(m); LEN(d) = l;
1.4 noro 168:
169: int nmv_adv;
170: int nmv_len;
171: NDV ndv_red;
172:
1.11 noro 173: void ndv_mul_c(NDV p,int mul);
1.4 noro 174: ND ndv_add(ND p1,NDV p2);
1.3 noro 175: NDV ndtondv(ND p);
176: void ndv_mul_nm(NDV pv,NM m,NDV r);
1.9 noro 177: ND ndv_mul_nm_create(NDV p,NM m0);
1.11 noro 178: void ndv_realloc(NDV p,int obpe,int oadv);
179: NDV dptondv(DP);
180: DP ndvtodp(NDV);
181: #else
182: static ND *nd_ps;
183: ND dptond(DP);
184: DP ndtodp(ND);
1.4 noro 185: #endif
1.1 noro 186:
187: void nd_free_private_storage()
188: {
189: _nd_free_list = 0;
190: _nm_free_list = 0;
1.5 noro 191: _ndp_free_list = 0;
1.13 noro 192: bzero(nd_red,sizeof(REDTAB_LEN*sizeof(RHist)));
1.1 noro 193: GC_gcollect();
194: }
195:
196: void _NM_alloc()
197: {
198: NM p;
199: int i;
200:
1.11 noro 201: for ( i = 0; i < 1024; i++ ) {
1.1 noro 202: p = (NM)GC_malloc(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
203: p->next = _nm_free_list; _nm_free_list = p;
204: }
205: }
206:
207: void _ND_alloc()
208: {
209: ND p;
210: int i;
211:
212: for ( i = 0; i < 1024; i++ ) {
213: p = (ND)GC_malloc(sizeof(struct oND));
214: p->body = (NM)_nd_free_list; _nd_free_list = p;
215: }
216: }
217:
218: void _NDP_alloc()
219: {
220: ND_pairs p;
221: int i;
222:
1.11 noro 223: for ( i = 0; i < 1024; i++ ) {
1.1 noro 224: p = (ND_pairs)GC_malloc(sizeof(struct oND_pairs)
225: +(nd_wpd-1)*sizeof(unsigned int));
226: p->next = _ndp_free_list; _ndp_free_list = p;
227: }
228: }
229:
230: INLINE nd_length(ND p)
231: {
232: NM m;
233: int i;
234:
235: if ( !p )
236: return 0;
237: else {
238: for ( i = 0, m = BDY(p); m; m = NEXT(m), i++ );
239: return i;
240: }
241: }
242:
243: INLINE int ndl_reducible(unsigned int *d1,unsigned int *d2)
244: {
245: unsigned int u1,u2;
246: int i,j;
247:
248: switch ( nd_bpe ) {
249: case 4:
250: for ( i = 0; i < nd_wpd; i++ ) {
251: u1 = d1[i]; u2 = d2[i];
252: if ( (u1&0xf0000000) < (u2&0xf0000000) ) return 0;
253: if ( (u1&0xf000000) < (u2&0xf000000) ) return 0;
254: if ( (u1&0xf00000) < (u2&0xf00000) ) return 0;
255: if ( (u1&0xf0000) < (u2&0xf0000) ) return 0;
256: if ( (u1&0xf000) < (u2&0xf000) ) return 0;
257: if ( (u1&0xf00) < (u2&0xf00) ) return 0;
258: if ( (u1&0xf0) < (u2&0xf0) ) return 0;
259: if ( (u1&0xf) < (u2&0xf) ) return 0;
260: }
261: return 1;
262: break;
263: case 6:
264: for ( i = 0; i < nd_wpd; i++ ) {
265: u1 = d1[i]; u2 = d2[i];
266: if ( (u1&0x3f000000) < (u2&0x3f000000) ) return 0;
267: if ( (u1&0xfc0000) < (u2&0xfc0000) ) return 0;
268: if ( (u1&0x3f000) < (u2&0x3f000) ) return 0;
269: if ( (u1&0xfc0) < (u2&0xfc0) ) return 0;
270: if ( (u1&0x3f) < (u2&0x3f) ) return 0;
271: }
272: return 1;
273: break;
274: case 8:
275: for ( i = 0; i < nd_wpd; i++ ) {
276: u1 = d1[i]; u2 = d2[i];
277: if ( (u1&0xff000000) < (u2&0xff000000) ) return 0;
278: if ( (u1&0xff0000) < (u2&0xff0000) ) return 0;
279: if ( (u1&0xff00) < (u2&0xff00) ) return 0;
280: if ( (u1&0xff) < (u2&0xff) ) return 0;
281: }
282: return 1;
283: break;
284: case 16:
285: for ( i = 0; i < nd_wpd; i++ ) {
286: u1 = d1[i]; u2 = d2[i];
287: if ( (u1&0xffff0000) < (u2&0xffff0000) ) return 0;
288: if ( (u1&0xffff) < (u2&0xffff) ) return 0;
289: }
290: return 1;
291: break;
292: case 32:
293: for ( i = 0; i < nd_wpd; i++ )
294: if ( d1[i] < d2[i] ) return 0;
295: return 1;
296: break;
297: default:
298: for ( i = 0; i < nd_wpd; i++ ) {
299: u1 = d1[i]; u2 = d2[i];
300: for ( j = 0; j < nd_epw; j++ )
301: if ( (u1&nd_mask[j]) < (u2&nd_mask[j]) ) return 0;
302: }
303: return 1;
304: }
305: }
306:
307: void ndl_lcm(unsigned int *d1,unsigned *d2,unsigned int *d)
308: {
309: unsigned int t1,t2,u,u1,u2;
310: int i,j;
311:
312: switch ( nd_bpe ) {
313: case 4:
314: for ( i = 0; i < nd_wpd; i++ ) {
315: u1 = d1[i]; u2 = d2[i];
316: t1 = (u1&0xf0000000); t2 = (u2&0xf0000000); u = t1>t2?t1:t2;
317: t1 = (u1&0xf000000); t2 = (u2&0xf000000); u |= t1>t2?t1:t2;
318: t1 = (u1&0xf00000); t2 = (u2&0xf00000); u |= t1>t2?t1:t2;
319: t1 = (u1&0xf0000); t2 = (u2&0xf0000); u |= t1>t2?t1:t2;
320: t1 = (u1&0xf000); t2 = (u2&0xf000); u |= t1>t2?t1:t2;
321: t1 = (u1&0xf00); t2 = (u2&0xf00); u |= t1>t2?t1:t2;
322: t1 = (u1&0xf0); t2 = (u2&0xf0); u |= t1>t2?t1:t2;
323: t1 = (u1&0xf); t2 = (u2&0xf); u |= t1>t2?t1:t2;
324: d[i] = u;
325: }
326: break;
327: case 6:
328: for ( i = 0; i < nd_wpd; i++ ) {
329: u1 = d1[i]; u2 = d2[i];
330: t1 = (u1&0x3f000000); t2 = (u2&0x3f000000); u = t1>t2?t1:t2;
331: t1 = (u1&0xfc0000); t2 = (u2&0xfc0000); u |= t1>t2?t1:t2;
332: t1 = (u1&0x3f000); t2 = (u2&0x3f000); u |= t1>t2?t1:t2;
333: t1 = (u1&0xfc0); t2 = (u2&0xfc0); u |= t1>t2?t1:t2;
334: t1 = (u1&0x3f); t2 = (u2&0x3f); u |= t1>t2?t1:t2;
335: d[i] = u;
336: }
337: break;
338: case 8:
339: for ( i = 0; i < nd_wpd; i++ ) {
340: u1 = d1[i]; u2 = d2[i];
341: t1 = (u1&0xff000000); t2 = (u2&0xff000000); u = t1>t2?t1:t2;
342: t1 = (u1&0xff0000); t2 = (u2&0xff0000); u |= t1>t2?t1:t2;
343: t1 = (u1&0xff00); t2 = (u2&0xff00); u |= t1>t2?t1:t2;
344: t1 = (u1&0xff); t2 = (u2&0xff); u |= t1>t2?t1:t2;
345: d[i] = u;
346: }
347: break;
348: case 16:
349: for ( i = 0; i < nd_wpd; i++ ) {
350: u1 = d1[i]; u2 = d2[i];
351: t1 = (u1&0xffff0000); t2 = (u2&0xffff0000); u = t1>t2?t1:t2;
352: t1 = (u1&0xffff); t2 = (u2&0xffff); u |= t1>t2?t1:t2;
353: d[i] = u;
354: }
355: break;
356: case 32:
357: for ( i = 0; i < nd_wpd; i++ ) {
358: u1 = d1[i]; u2 = d2[i];
359: d[i] = u1>u2?u1:u2;
360: }
361: break;
362: default:
363: for ( i = 0; i < nd_wpd; i++ ) {
364: u1 = d1[i]; u2 = d2[i];
365: for ( j = 0, u = 0; j < nd_epw; j++ ) {
366: t1 = (u1&nd_mask[j]); t2 = (u2&nd_mask[j]); u |= t1>t2?t1:t2;
367: }
368: d[i] = u;
369: }
370: break;
371: }
372: }
373:
374: int ndl_td(unsigned int *d)
375: {
376: unsigned int t,u;
377: int i,j;
378:
379: for ( t = 0, i = 0; i < nd_wpd; i++ ) {
380: u = d[i];
381: for ( j = 0; j < nd_epw; j++, u>>=nd_bpe )
382: t += (u&nd_mask0);
383: }
384: return t;
385: }
386:
387: INLINE int ndl_compare(unsigned int *d1,unsigned int *d2)
388: {
389: int i;
390:
391: for ( i = 0; i < nd_wpd; i++, d1++, d2++ )
392: if ( *d1 > *d2 )
393: return is_rlex ? -1 : 1;
394: else if ( *d1 < *d2 )
395: return is_rlex ? 1 : -1;
396: return 0;
397: }
398:
399: INLINE int ndl_equal(unsigned int *d1,unsigned int *d2)
400: {
401: int i;
402:
403: for ( i = 0; i < nd_wpd; i++ )
404: if ( d1[i] != d2[i] )
405: return 0;
406: return 1;
407: }
408:
1.6 noro 409: INLINE void ndl_copy(unsigned int *d1,unsigned int *d2)
410: {
411: int i;
412:
413: switch ( nd_wpd ) {
414: case 1:
415: d2[0] = d1[0];
416: break;
417: case 2:
418: d2[0] = d1[0];
419: d2[1] = d1[1];
420: break;
421: default:
422: for ( i = 0; i < nd_wpd; i++ )
423: d2[i] = d1[i];
424: break;
425: }
426: }
427:
1.1 noro 428: INLINE void ndl_add(unsigned int *d1,unsigned int *d2,unsigned int *d)
429: {
430: int i;
431:
1.6 noro 432: switch ( nd_wpd ) {
433: case 1:
434: d[0] = d1[0]+d2[0];
435: break;
436: case 2:
437: d[0] = d1[0]+d2[0];
438: d[1] = d1[1]+d2[1];
439: break;
440: default:
441: for ( i = 0; i < nd_wpd; i++ )
442: d[i] = d1[i]+d2[i];
443: break;
444: }
445: }
446:
447: INLINE void ndl_add2(unsigned int *d1,unsigned int *d2)
448: {
449: int i;
450:
451: switch ( nd_wpd ) {
452: case 1:
453: d2[0] += d1[0];
454: break;
455: case 2:
456: d2[0] += d1[0];
457: d2[1] += d1[1];
458: break;
459: default:
460: for ( i = 0; i < nd_wpd; i++ )
461: d2[i] += d1[i];
462: break;
1.1 noro 463: }
464: }
465:
466: void ndl_sub(unsigned int *d1,unsigned int *d2,unsigned int *d)
467: {
468: int i;
469:
470: for ( i = 0; i < nd_wpd; i++ )
471: d[i] = d1[i]-d2[i];
472: }
473:
474: int ndl_disjoint(unsigned int *d1,unsigned int *d2)
475: {
476: unsigned int t1,t2,u,u1,u2;
477: int i,j;
478:
479: switch ( nd_bpe ) {
480: case 4:
481: for ( i = 0; i < nd_wpd; i++ ) {
482: u1 = d1[i]; u2 = d2[i];
483: t1 = u1&0xf0000000; t2 = u2&0xf0000000; if ( t1&&t2 ) return 0;
484: t1 = u1&0xf000000; t2 = u2&0xf000000; if ( t1&&t2 ) return 0;
485: t1 = u1&0xf00000; t2 = u2&0xf00000; if ( t1&&t2 ) return 0;
486: t1 = u1&0xf0000; t2 = u2&0xf0000; if ( t1&&t2 ) return 0;
487: t1 = u1&0xf000; t2 = u2&0xf000; if ( t1&&t2 ) return 0;
488: t1 = u1&0xf00; t2 = u2&0xf00; if ( t1&&t2 ) return 0;
489: t1 = u1&0xf0; t2 = u2&0xf0; if ( t1&&t2 ) return 0;
490: t1 = u1&0xf; t2 = u2&0xf; if ( t1&&t2 ) return 0;
491: }
492: return 1;
493: break;
494: case 6:
495: for ( i = 0; i < nd_wpd; i++ ) {
496: u1 = d1[i]; u2 = d2[i];
497: t1 = u1&0x3f000000; t2 = u2&0x3f000000; if ( t1&&t2 ) return 0;
498: t1 = u1&0xfc0000; t2 = u2&0xfc0000; if ( t1&&t2 ) return 0;
499: t1 = u1&0x3f000; t2 = u2&0x3f000; if ( t1&&t2 ) return 0;
500: t1 = u1&0xfc0; t2 = u2&0xfc0; if ( t1&&t2 ) return 0;
501: t1 = u1&0x3f; t2 = u2&0x3f; if ( t1&&t2 ) return 0;
502: }
503: return 1;
504: break;
505: case 8:
506: for ( i = 0; i < nd_wpd; i++ ) {
507: u1 = d1[i]; u2 = d2[i];
508: t1 = u1&0xff000000; t2 = u2&0xff000000; if ( t1&&t2 ) return 0;
509: t1 = u1&0xff0000; t2 = u2&0xff0000; if ( t1&&t2 ) return 0;
510: t1 = u1&0xff00; t2 = u2&0xff00; if ( t1&&t2 ) return 0;
511: t1 = u1&0xff; t2 = u2&0xff; if ( t1&&t2 ) return 0;
512: }
513: return 1;
514: break;
515: case 16:
516: for ( i = 0; i < nd_wpd; i++ ) {
517: u1 = d1[i]; u2 = d2[i];
518: t1 = u1&0xffff0000; t2 = u2&0xffff0000; if ( t1&&t2 ) return 0;
519: t1 = u1&0xffff; t2 = u2&0xffff; if ( t1&&t2 ) return 0;
520: }
521: return 1;
522: break;
523: case 32:
524: for ( i = 0; i < nd_wpd; i++ )
525: if ( d1[i] && d2[i] ) return 0;
526: return 1;
527: break;
528: default:
529: for ( i = 0; i < nd_wpd; i++ ) {
530: u1 = d1[i]; u2 = d2[i];
531: for ( j = 0; j < nd_epw; j++ ) {
532: if ( (u1&nd_mask0) && (u2&nd_mask0) ) return 0;
533: u1 >>= nd_bpe; u2 >>= nd_bpe;
534: }
535: }
536: return 1;
537: break;
538: }
539: }
540:
541: ND nd_reduce(ND p1,ND p2)
542: {
543: int c,c1,c2,t,td,td2,mul;
544: NM m2,prev,head,cur,new;
545: unsigned int *d;
546:
547: if ( !p1 )
548: return 0;
549: else {
1.14 ! noro 550: c2 = invm(HCM(p2),nd_mod);
! 551: c1 = nd_mod-HCM(p1);
1.1 noro 552: DMAR(c1,c2,0,nd_mod,mul);
553: td = HTD(p1)-HTD(p2);
554: d = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
555: ndl_sub(HDL(p1),HDL(p2),d);
556: prev = 0; head = cur = BDY(p1);
557: NEWNM(new);
558: for ( m2 = BDY(p2); m2; ) {
1.14 ! noro 559: td2 = TD(new) = TD(m2)+td;
! 560: ndl_add(DL(m2),d,DL(new));
1.1 noro 561: if ( !cur ) {
1.14 ! noro 562: c1 = CM(m2);
1.1 noro 563: DMAR(c1,mul,0,nd_mod,c2);
1.14 ! noro 564: CM(new) = c2;
1.1 noro 565: if ( !prev ) {
566: prev = new;
567: NEXT(prev) = 0;
568: head = prev;
569: } else {
570: NEXT(prev) = new;
571: NEXT(new) = 0;
572: prev = new;
573: }
574: m2 = NEXT(m2);
575: NEWNM(new);
576: continue;
577: }
1.14 ! noro 578: if ( TD(cur) > td2 )
1.1 noro 579: c = 1;
1.14 ! noro 580: else if ( TD(cur) < td2 )
1.1 noro 581: c = -1;
582: else
1.14 ! noro 583: c = ndl_compare(DL(cur),DL(new));
1.1 noro 584: switch ( c ) {
585: case 0:
1.14 ! noro 586: c2 = CM(m2);
! 587: c1 = CM(cur);
1.1 noro 588: DMAR(c2,mul,c1,nd_mod,t);
589: if ( t )
1.14 ! noro 590: CM(cur) = t;
1.1 noro 591: else if ( !prev ) {
592: head = NEXT(cur);
593: FREENM(cur);
594: cur = head;
595: } else {
596: NEXT(prev) = NEXT(cur);
597: FREENM(cur);
598: cur = NEXT(prev);
599: }
600: m2 = NEXT(m2);
601: break;
602: case 1:
603: prev = cur;
604: cur = NEXT(cur);
605: break;
606: case -1:
607: if ( !prev ) {
608: /* cur = head */
609: prev = new;
1.14 ! noro 610: c2 = CM(m2);
1.1 noro 611: DMAR(c2,mul,0,nd_mod,c1);
1.14 ! noro 612: CM(prev) = c1;
1.1 noro 613: NEXT(prev) = head;
614: head = prev;
615: } else {
1.14 ! noro 616: c2 = CM(m2);
1.1 noro 617: DMAR(c2,mul,0,nd_mod,c1);
1.14 ! noro 618: CM(new) = c1;
1.1 noro 619: NEXT(prev) = new;
620: NEXT(new) = cur;
621: prev = new;
622: }
623: NEWNM(new);
624: m2 = NEXT(m2);
625: break;
626: }
627: }
628: FREENM(new);
629: if ( head ) {
630: BDY(p1) = head;
1.14 ! noro 631: SG(p1) = MAX(SG(p1),SG(p2)+td);
1.1 noro 632: return p1;
633: } else {
634: FREEND(p1);
635: return 0;
636: }
637:
638: }
639: }
640:
641: /* HDL(p1) = HDL(p2) */
642:
643: ND nd_reduce_special(ND p1,ND p2)
644: {
645: int c,c1,c2,t,td,td2,mul;
646: NM m2,prev,head,cur,new;
647:
648: if ( !p1 )
649: return 0;
650: else {
1.14 ! noro 651: c2 = invm(HCM(p2),nd_mod);
! 652: c1 = nd_mod-HCM(p1);
1.1 noro 653: DMAR(c1,c2,0,nd_mod,mul);
654: prev = 0; head = cur = BDY(p1);
655: NEWNM(new);
656: for ( m2 = BDY(p2); m2; ) {
1.14 ! noro 657: td2 = TD(new) = TD(m2);
1.1 noro 658: if ( !cur ) {
1.14 ! noro 659: c1 = CM(m2);
1.1 noro 660: DMAR(c1,mul,0,nd_mod,c2);
1.14 ! noro 661: CM(new) = c2;
! 662: bcopy(DL(m2),DL(new),nd_wpd*sizeof(unsigned int));
1.1 noro 663: if ( !prev ) {
664: prev = new;
665: NEXT(prev) = 0;
666: head = prev;
667: } else {
668: NEXT(prev) = new;
669: NEXT(new) = 0;
670: prev = new;
671: }
672: m2 = NEXT(m2);
673: NEWNM(new);
674: continue;
675: }
1.14 ! noro 676: if ( TD(cur) > td2 )
1.1 noro 677: c = 1;
1.14 ! noro 678: else if ( TD(cur) < td2 )
1.1 noro 679: c = -1;
680: else
1.14 ! noro 681: c = ndl_compare(DL(cur),DL(m2));
1.1 noro 682: switch ( c ) {
683: case 0:
1.14 ! noro 684: c2 = CM(m2);
! 685: c1 = CM(cur);
1.1 noro 686: DMAR(c2,mul,c1,nd_mod,t);
687: if ( t )
1.14 ! noro 688: CM(cur) = t;
1.1 noro 689: else if ( !prev ) {
690: head = NEXT(cur);
691: FREENM(cur);
692: cur = head;
693: } else {
694: NEXT(prev) = NEXT(cur);
695: FREENM(cur);
696: cur = NEXT(prev);
697: }
698: m2 = NEXT(m2);
699: break;
700: case 1:
701: prev = cur;
702: cur = NEXT(cur);
703: break;
704: case -1:
1.14 ! noro 705: bcopy(DL(m2),DL(new),nd_wpd*sizeof(unsigned int));
1.1 noro 706: if ( !prev ) {
707: /* cur = head */
708: prev = new;
1.14 ! noro 709: c2 = CM(m2);
1.1 noro 710: DMAR(c2,mul,0,nd_mod,c1);
1.14 ! noro 711: CM(prev) = c1;
1.1 noro 712: NEXT(prev) = head;
713: head = prev;
714: } else {
1.14 ! noro 715: c2 = CM(m2);
1.1 noro 716: DMAR(c2,mul,0,nd_mod,c1);
1.14 ! noro 717: CM(new) = c1;
1.1 noro 718: NEXT(prev) = new;
719: NEXT(new) = cur;
720: prev = new;
721: }
722: NEWNM(new);
723: m2 = NEXT(m2);
724: break;
725: }
726: }
727: FREENM(new);
728: if ( head ) {
729: BDY(p1) = head;
1.14 ! noro 730: SG(p1)= MAX(SG(p1),SG(p2)+td);
1.1 noro 731: return p1;
732: } else {
733: FREEND(p1);
734: return 0;
735: }
736:
737: }
738: }
739:
1.5 noro 740: int ndl_check_bound2(int index,unsigned int *d2)
1.1 noro 741: {
1.5 noro 742: unsigned int u2;
743: unsigned int *d1;
744: int i,j,ind,k;
1.1 noro 745:
1.5 noro 746: d1 = nd_bound[index];
747: ind = 0;
748: switch ( nd_bpe ) {
749: case 4:
750: for ( i = 0; i < nd_wpd; i++ ) {
751: u2 = d2[i];
752: if ( d1[ind++]+((u2>>28)&0xf) >= 0x10 ) return 1;
753: if ( d1[ind++]+((u2>>24)&0xf) >= 0x10 ) return 1;
754: if ( d1[ind++]+((u2>>20)&0xf) >= 0x10 ) return 1;
755: if ( d1[ind++]+((u2>>16)&0xf) >= 0x10 ) return 1;
756: if ( d1[ind++]+((u2>>12)&0xf) >= 0x10 ) return 1;
757: if ( d1[ind++]+((u2>>8)&0xf) >= 0x10 ) return 1;
758: if ( d1[ind++]+((u2>>4)&0xf) >= 0x10 ) return 1;
759: if ( d1[ind++]+(u2&0xf) >= 0x10 ) return 1;
760: }
761: return 0;
762: break;
763: case 6:
764: for ( i = 0; i < nd_wpd; i++ ) {
765: u2 = d2[i];
766: if ( d1[ind++]+((u2>>24)&0x3f) >= 0x40 ) return 1;
767: if ( d1[ind++]+((u2>>18)&0x3f) >= 0x40 ) return 1;
768: if ( d1[ind++]+((u2>>12)&0x3f) >= 0x40 ) return 1;
769: if ( d1[ind++]+((u2>>6)&0x3f) >= 0x40 ) return 1;
770: if ( d1[ind++]+(u2&0x3f) >= 0x40 ) return 1;
771: }
772: return 0;
773: break;
774: case 8:
775: for ( i = 0; i < nd_wpd; i++ ) {
776: u2 = d2[i];
777: if ( d1[ind++]+((u2>>24)&0xff) >= 0x100 ) return 1;
778: if ( d1[ind++]+((u2>>16)&0xff) >= 0x100 ) return 1;
779: if ( d1[ind++]+((u2>>8)&0xff) >= 0x100 ) return 1;
780: if ( d1[ind++]+(u2&0xff) >= 0x100 ) return 1;
781: }
782: return 0;
783: break;
784: case 16:
785: for ( i = 0; i < nd_wpd; i++ ) {
786: u2 = d2[i];
787: if ( d1[ind++]+((u2>>16)&0xffff) > 0x10000 ) return 1;
788: if ( d1[ind++]+(u2&0xffff) > 0x10000 ) return 1;
789: }
790: return 0;
791: break;
792: case 32:
793: for ( i = 0; i < nd_wpd; i++ )
794: if ( d1[i]+d2[i]<d1[i] ) return 1;
795: return 0;
796: break;
797: default:
798: for ( i = 0; i < nd_wpd; i++ ) {
799: u2 = d2[i];
800: k = (nd_epw-1)*nd_bpe;
801: for ( j = 0; j < nd_epw; j++, k -= nd_bpe )
802: if ( d1[ind++]+((u2>>k)&nd_mask0) > nd_mask0 ) return 1;
803: }
804: return 0;
805: break;
806: }
1.1 noro 807: }
808:
1.6 noro 809: INLINE int ndl_hash_value(int td,unsigned int *d)
1.1 noro 810: {
811: int i;
812: int r;
813:
814: r = td;
815: for ( i = 0; i < nd_wpd; i++ )
816: r = ((r<<16)+d[i])%REDTAB_LEN;
817: return r;
818: }
819:
1.9 noro 820: INLINE int nd_find_reducer(ND g)
1.1 noro 821: {
1.13 noro 822: RHist r;
1.6 noro 823: int d,k,i;
1.1 noro 824:
825: d = ndl_hash_value(HTD(g),HDL(g));
1.13 noro 826: for ( r = nd_red[d], k = 0; r; r = NEXT(r), k++ ) {
1.14 ! noro 827: if ( HTD(g) == TD(r) && ndl_equal(HDL(g),DL(r)) ) {
1.1 noro 828: if ( k > 0 ) nd_notfirst++;
829: nd_found++;
1.13 noro 830: return r->index;
1.1 noro 831: }
832: }
833:
1.13 noro 834: if ( Reverse )
835: for ( i = nd_psn-1; i >= 0; i-- ) {
836: r = nd_psh[i];
1.14 ! noro 837: if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13 noro 838: nd_create++;
839: nd_append_red(HDL(g),HTD(g),i);
840: return i;
841: }
842: }
843: else
844: for ( i = 0; i < nd_psn; i++ ) {
845: r = nd_psh[i];
1.14 ! noro 846: if ( HTD(g) >= TD(r) && ndl_reducible(HDL(g),DL(r)) ) {
1.13 noro 847: nd_create++;
848: nd_append_red(HDL(g),HTD(g),i);
849: return i;
850: }
1.1 noro 851: }
1.6 noro 852: return -1;
1.1 noro 853: }
854:
855: ND nd_add(ND p1,ND p2)
856: {
857: int n,c;
858: int t;
859: ND r;
860: NM m1,m2,mr0,mr,s;
861:
862: if ( !p1 )
863: return p2;
864: else if ( !p2 )
865: return p1;
866: else {
867: for ( n = NV(p1), m1 = BDY(p1), m2 = BDY(p2), mr0 = 0; m1 && m2; ) {
1.14 ! noro 868: if ( TD(m1) > TD(m2) )
1.1 noro 869: c = 1;
1.14 ! noro 870: else if ( TD(m1) < TD(m2) )
1.1 noro 871: c = -1;
872: else
1.14 ! noro 873: c = ndl_compare(DL(m1),DL(m2));
1.1 noro 874: switch ( c ) {
875: case 0:
1.14 ! noro 876: t = ((CM(m1))+(CM(m2))) - nd_mod;
1.1 noro 877: if ( t < 0 )
878: t += nd_mod;
879: s = m1; m1 = NEXT(m1);
880: if ( t ) {
1.14 ! noro 881: NEXTNM2(mr0,mr,s); CM(mr) = (t);
1.1 noro 882: } else {
883: FREENM(s);
884: }
885: s = m2; m2 = NEXT(m2); FREENM(s);
886: break;
887: case 1:
888: s = m1; m1 = NEXT(m1); NEXTNM2(mr0,mr,s);
889: break;
890: case -1:
891: s = m2; m2 = NEXT(m2); NEXTNM2(mr0,mr,s);
892: break;
893: }
894: }
895: if ( !mr0 )
896: if ( m1 )
897: mr0 = m1;
898: else if ( m2 )
899: mr0 = m2;
900: else
901: return 0;
902: else if ( m1 )
903: NEXT(mr) = m1;
904: else if ( m2 )
905: NEXT(mr) = m2;
906: else
907: NEXT(mr) = 0;
908: BDY(p1) = mr0;
1.14 ! noro 909: SG(p1) = MAX(SG(p1),SG(p2));
1.1 noro 910: FREEND(p2);
911: return p1;
912: }
913: }
914:
915: #if 1
916: /* ret=1 : success, ret=0 : overflow */
917: int nd_nf(ND g,int full,ND *rp)
918: {
1.11 noro 919: ND d;
1.1 noro 920: NM m,mrd,tail;
1.7 noro 921: NM mul;
1.10 noro 922: int n,sugar,psugar,sugar0,stat,index;
1.6 noro 923: int c,c1,c2;
1.4 noro 924: #if USE_NDV
1.11 noro 925: NDV p,red;
1.4 noro 926: #else
1.11 noro 927: ND p,red;
1.4 noro 928: #endif
1.1 noro 929:
930: if ( !g ) {
931: *rp = 0;
932: return 1;
933: }
1.14 ! noro 934: sugar0 = sugar = SG(g);
1.1 noro 935: n = NV(g);
1.7 noro 936: mul = (NM)ALLOCA(sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int));
1.1 noro 937: for ( d = 0; g; ) {
1.6 noro 938: index = nd_find_reducer(g);
939: if ( index >= 0 ) {
940: p = nd_ps[index];
1.14 ! noro 941: ndl_sub(HDL(g),HDL(p),DL(mul));
! 942: TD(mul) = HTD(g)-HTD(p);
1.10 noro 943: #if 0
1.14 ! noro 944: if ( d && (SG(p)+TD(mul)) > sugar ) {
1.10 noro 945: goto afo;
946: }
947: #endif
1.14 ! noro 948: if ( ndl_check_bound2(index,DL(mul)) ) {
1.6 noro 949: nd_free(g); nd_free(d);
950: return 0;
951: }
1.14 ! noro 952: c1 = invm(HCM(p),nd_mod); c2 = nd_mod-HCM(g);
! 953: DMAR(c1,c2,0,nd_mod,c); CM(mul) = c;
1.4 noro 954: #if USE_NDV
1.11 noro 955: ndv_mul_nm(nd_ps[index],mul,ndv_red);
1.4 noro 956: g = ndv_add(g,ndv_red);
1.14 ! noro 957: sugar = MAX(sugar,SG(ndv_red));
1.4 noro 958: #else
1.7 noro 959: red = nd_mul_ind_nm(index,mul);
1.1 noro 960: g = nd_add(g,red);
1.14 ! noro 961: sugar = MAX(sugar,SG(red));
1.1 noro 962: #endif
963: } else if ( !full ) {
964: *rp = g;
965: return 1;
966: } else {
1.10 noro 967: afo:
1.1 noro 968: m = BDY(g);
969: if ( NEXT(m) ) {
970: BDY(g) = NEXT(m); NEXT(m) = 0;
971: } else {
972: FREEND(g); g = 0;
973: }
974: if ( d ) {
975: NEXT(tail)=m;
976: tail=m;
977: } else {
978: MKND(n,m,d);
979: tail = BDY(d);
980: }
981: }
982: }
983: if ( d )
1.14 ! noro 984: SG(d) = sugar;
1.1 noro 985: *rp = d;
986: return 1;
987: }
988: #else
989:
990: ND nd_remove_head(ND p)
991: {
992: NM m;
993:
994: m = BDY(p);
995: if ( !NEXT(m) ) {
996: FREEND(p);
997: p = 0;
998: } else
999: BDY(p) = NEXT(m);
1000: FREENM(m);
1001: return p;
1002: }
1003:
1004: PGeoBucket create_pbucket()
1005: {
1006: PGeoBucket g;
1007:
1008: g = CALLOC(1,sizeof(struct oPGeoBucket));
1009: g->m = -1;
1010: return g;
1011: }
1012:
1013: void add_pbucket(PGeoBucket g,ND d)
1014: {
1015: int l,k,m;
1016:
1017: l = nd_length(d);
1018: for ( k = 0, m = 1; l > m; k++, m <<= 2 );
1019: /* 4^(k-1) < l <= 4^k */
1020: d = nd_add(g->body[k],d);
1021: for ( ; d && nd_length(d) > 1<<(2*k); k++ ) {
1022: g->body[k] = 0;
1023: d = nd_add(g->body[k+1],d);
1024: }
1025: g->body[k] = d;
1026: g->m = MAX(g->m,k);
1027: }
1028:
1029: int head_pbucket(PGeoBucket g)
1030: {
1031: int j,i,c,k,nv,sum;
1032: unsigned int *di,*dj;
1033: ND gi,gj;
1034:
1035: k = g->m;
1036: while ( 1 ) {
1037: j = -1;
1038: for ( i = 0; i <= k; i++ ) {
1039: if ( !(gi = g->body[i]) )
1040: continue;
1041: if ( j < 0 ) {
1042: j = i;
1043: gj = g->body[j];
1044: dj = HDL(gj);
1.14 ! noro 1045: sum = HCM(gj);
1.1 noro 1046: } else {
1047: di = HDL(gi);
1048: nv = NV(gi);
1049: if ( HTD(gi) > HTD(gj) )
1050: c = 1;
1051: else if ( HTD(gi) < HTD(gj) )
1052: c = -1;
1053: else
1054: c = ndl_compare(di,dj);
1055: if ( c > 0 ) {
1056: if ( sum )
1.14 ! noro 1057: HCM(gj) = sum;
1.1 noro 1058: else
1059: g->body[j] = nd_remove_head(gj);
1060: j = i;
1061: gj = g->body[j];
1062: dj = HDL(gj);
1.14 ! noro 1063: sum = HCM(gj);
1.1 noro 1064: } else if ( c == 0 ) {
1.14 ! noro 1065: sum = sum+HCM(gi)-nd_mod;
1.1 noro 1066: if ( sum < 0 )
1067: sum += nd_mod;
1068: g->body[i] = nd_remove_head(gi);
1069: }
1070: }
1071: }
1072: if ( j < 0 )
1073: return -1;
1074: else if ( sum ) {
1.14 ! noro 1075: HCM(gj) = sum;
1.1 noro 1076: return j;
1077: } else
1078: g->body[j] = nd_remove_head(gj);
1079: }
1080: }
1081:
1082: ND normalize_pbucket(PGeoBucket g)
1083: {
1084: int i;
1085: ND r,t;
1086:
1087: r = 0;
1088: for ( i = 0; i <= g->m; i++ )
1089: r = nd_add(r,g->body[i]);
1090: return r;
1091: }
1092:
1093: ND nd_nf(ND g,int full)
1094: {
1095: ND u,p,d,red;
1096: NODE l;
1097: NM m,mrd;
1098: int sugar,psugar,n,h_reducible,h;
1099: PGeoBucket bucket;
1100:
1101: if ( !g ) {
1102: return 0;
1103: }
1.14 ! noro 1104: sugar = SG(g);
! 1105: n = NV(g);
1.1 noro 1106: bucket = create_pbucket();
1107: add_pbucket(bucket,g);
1108: d = 0;
1109: while ( 1 ) {
1110: h = head_pbucket(bucket);
1111: if ( h < 0 ) {
1112: if ( d )
1.14 ! noro 1113: SG(d) = sugar;
1.1 noro 1114: return d;
1115: }
1116: g = bucket->body[h];
1117: red = nd_find_reducer(g);
1118: if ( red ) {
1119: bucket->body[h] = nd_remove_head(g);
1120: red = nd_remove_head(red);
1121: add_pbucket(bucket,red);
1.14 ! noro 1122: sugar = MAX(sugar,SG(red));
1.1 noro 1123: } else if ( !full ) {
1124: g = normalize_pbucket(bucket);
1125: if ( g )
1.14 ! noro 1126: SG(g) = sugar;
1.1 noro 1127: return g;
1128: } else {
1129: m = BDY(g);
1130: if ( NEXT(m) ) {
1131: BDY(g) = NEXT(m); NEXT(m) = 0;
1132: } else {
1133: FREEND(g); g = 0;
1134: }
1135: bucket->body[h] = g;
1136: NEXT(m) = 0;
1137: if ( d ) {
1138: for ( mrd = BDY(d); NEXT(mrd); mrd = NEXT(mrd) );
1139: NEXT(mrd) = m;
1140: } else {
1141: MKND(n,m,d);
1142: }
1143: }
1144: }
1145: }
1146: #endif
1147:
1148: NODE nd_gb(NODE f)
1149: {
1150: int i,nh,sugar,stat;
1151: NODE r,g,gall;
1152: ND_pairs d;
1153: ND_pairs l;
1154: ND h,nf;
1155:
1156: for ( gall = g = 0, d = 0, r = f; r; r = NEXT(r) ) {
1157: i = (int)BDY(r);
1158: d = update_pairs(d,g,i);
1159: g = update_base(g,i);
1160: gall = append_one(gall,i);
1161: }
1162: sugar = 0;
1163: while ( d ) {
1164: again:
1165: l = nd_minp(d,&d);
1.14 ! noro 1166: if ( SG(l) != sugar ) {
! 1167: sugar = SG(l);
1.1 noro 1168: fprintf(asir_out,"%d",sugar);
1169: }
1170: stat = nd_sp(l,&h);
1171: if ( !stat ) {
1172: NEXT(l) = d; d = l;
1173: d = nd_reconstruct(d);
1174: goto again;
1175: }
1176: stat = nd_nf(h,!Top,&nf);
1177: if ( !stat ) {
1178: NEXT(l) = d; d = l;
1179: d = nd_reconstruct(d);
1180: goto again;
1181: } else if ( nf ) {
1182: printf("+"); fflush(stdout);
1183: nh = nd_newps(nf);
1184: d = update_pairs(d,g,nh);
1185: g = update_base(g,nh);
1186: gall = append_one(gall,nh);
1187: FREENDP(l);
1188: } else {
1189: printf("."); fflush(stdout);
1190: FREENDP(l);
1191: }
1192: }
1193: return g;
1194: }
1195:
1196: ND_pairs update_pairs( ND_pairs d, NODE /* of index */ g, int t)
1197: {
1198: ND_pairs d1,nd,cur,head,prev,remove;
1199:
1200: if ( !g ) return d;
1201: d = crit_B(d,t);
1202: d1 = nd_newpairs(g,t);
1203: d1 = crit_M(d1);
1204: d1 = crit_F(d1);
1205: prev = 0; cur = head = d1;
1206: while ( cur ) {
1207: if ( crit_2( cur->i1,cur->i2 ) ) {
1208: remove = cur;
1209: if ( !prev ) {
1210: head = cur = NEXT(cur);
1211: } else {
1212: cur = NEXT(prev) = NEXT(cur);
1213: }
1214: FREENDP(remove);
1215: } else {
1216: prev = cur;
1217: cur = NEXT(cur);
1218: }
1219: }
1220: if ( !d )
1221: return head;
1222: else {
1223: nd = d;
1224: while ( NEXT(nd) )
1225: nd = NEXT(nd);
1226: NEXT(nd) = head;
1227: return d;
1228: }
1229: }
1230:
1231: ND_pairs nd_newpairs( NODE g, int t )
1232: {
1233: NODE h;
1234: unsigned int *dl;
1235: int td,ts,s;
1236: ND_pairs r,r0;
1237:
1238: dl = HDL(nd_ps[t]);
1239: td = HTD(nd_ps[t]);
1.14 ! noro 1240: ts = SG(nd_ps[t]) - td;
1.1 noro 1241: for ( r0 = 0, h = g; h; h = NEXT(h) ) {
1242: NEXTND_pairs(r0,r);
1243: r->i1 = (int)BDY(h);
1244: r->i2 = t;
1245: ndl_lcm(HDL(nd_ps[r->i1]),dl,r->lcm);
1.14 ! noro 1246: TD(r) = ndl_td(r->lcm);
! 1247: s = SG(nd_ps[r->i1])-HTD(nd_ps[r->i1]);
! 1248: SG(r) = MAX(s,ts) + TD(r);
1.1 noro 1249: }
1250: NEXT(r) = 0;
1251: return r0;
1252: }
1253:
1254: ND_pairs crit_B( ND_pairs d, int s )
1255: {
1256: ND_pairs cur,head,prev,remove;
1257: unsigned int *t,*tl,*lcm;
1258: int td,tdl;
1259:
1260: if ( !d ) return 0;
1261: t = HDL(nd_ps[s]);
1262: prev = 0;
1263: head = cur = d;
1264: lcm = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1265: while ( cur ) {
1266: tl = cur->lcm;
1267: if ( ndl_reducible(tl,t)
1268: && (ndl_lcm(HDL(nd_ps[cur->i1]),t,lcm),!ndl_equal(lcm,tl))
1269: && (ndl_lcm(HDL(nd_ps[cur->i2]),t,lcm),!ndl_equal(lcm,tl)) ) {
1270: remove = cur;
1271: if ( !prev ) {
1272: head = cur = NEXT(cur);
1273: } else {
1274: cur = NEXT(prev) = NEXT(cur);
1275: }
1276: FREENDP(remove);
1277: } else {
1278: prev = cur;
1279: cur = NEXT(cur);
1280: }
1281: }
1282: return head;
1283: }
1284:
1285: ND_pairs crit_M( ND_pairs d1 )
1286: {
1287: ND_pairs e,d2,d3,dd,p;
1288: unsigned int *id,*jd;
1289: int itd,jtd;
1290:
1291: for ( dd = 0, e = d1; e; e = d3 ) {
1292: if ( !(d2 = NEXT(e)) ) {
1293: NEXT(e) = dd;
1294: return e;
1295: }
1296: id = e->lcm;
1.14 ! noro 1297: itd = TD(e);
1.1 noro 1298: for ( d3 = 0; d2; d2 = p ) {
1299: p = NEXT(d2),
1300: jd = d2->lcm;
1.14 ! noro 1301: jtd = TD(d2);
1.1 noro 1302: if ( jtd == itd )
1303: if ( id == jd );
1304: else if ( ndl_reducible(jd,id) ) continue;
1305: else if ( ndl_reducible(id,jd) ) goto delit;
1306: else ;
1307: else if ( jtd > itd )
1308: if ( ndl_reducible(jd,id) ) continue;
1309: else ;
1310: else if ( ndl_reducible(id,jd ) ) goto delit;
1311: NEXT(d2) = d3;
1312: d3 = d2;
1313: }
1314: NEXT(e) = dd;
1315: dd = e;
1316: continue;
1317: /**/
1318: delit: NEXT(d2) = d3;
1319: d3 = d2;
1320: for ( ; p; p = d2 ) {
1321: d2 = NEXT(p);
1322: NEXT(p) = d3;
1323: d3 = p;
1324: }
1325: FREENDP(e);
1326: }
1327: return dd;
1328: }
1329:
1330: ND_pairs crit_F( ND_pairs d1 )
1331: {
1332: ND_pairs rest, head,remove;
1333: ND_pairs last, p, r, w;
1334: int s;
1335:
1336: for ( head = last = 0, p = d1; NEXT(p); ) {
1337: r = w = equivalent_pairs(p,&rest);
1.14 ! noro 1338: s = SG(r);
1.1 noro 1339: w = NEXT(w);
1340: while ( w ) {
1341: if ( crit_2(w->i1,w->i2) ) {
1342: r = w;
1343: w = NEXT(w);
1344: while ( w ) {
1345: remove = w;
1346: w = NEXT(w);
1347: FREENDP(remove);
1348: }
1349: break;
1.14 ! noro 1350: } else if ( SG(w) < s ) {
1.1 noro 1351: FREENDP(r);
1352: r = w;
1.14 ! noro 1353: s = SG(r);
1.1 noro 1354: w = NEXT(w);
1355: } else {
1356: remove = w;
1357: w = NEXT(w);
1358: FREENDP(remove);
1359: }
1360: }
1361: if ( last ) NEXT(last) = r;
1362: else head = r;
1363: NEXT(last = r) = 0;
1364: p = rest;
1365: if ( !p ) return head;
1366: }
1367: if ( !last ) return p;
1368: NEXT(last) = p;
1369: return head;
1370: }
1371:
1372: int crit_2( int dp1, int dp2 )
1373: {
1374: return ndl_disjoint(HDL(nd_ps[dp1]),HDL(nd_ps[dp2]));
1375: }
1376:
1377: static ND_pairs equivalent_pairs( ND_pairs d1, ND_pairs *prest )
1378: {
1379: ND_pairs w,p,r,s;
1380: unsigned int *d;
1381: int td;
1382:
1383: w = d1;
1384: d = w->lcm;
1.14 ! noro 1385: td = TD(w);
1.1 noro 1386: s = NEXT(w);
1387: NEXT(w) = 0;
1388: for ( r = 0; s; s = p ) {
1389: p = NEXT(s);
1.14 ! noro 1390: if ( td == TD(s) && ndl_equal(d,s->lcm) ) {
1.1 noro 1391: NEXT(s) = w;
1392: w = s;
1393: } else {
1394: NEXT(s) = r;
1395: r = s;
1396: }
1397: }
1398: *prest = r;
1399: return w;
1400: }
1401:
1402: NODE update_base(NODE nd,int ndp)
1403: {
1404: unsigned int *dl, *dln;
1405: NODE last, p, head;
1406: int td,tdn;
1407:
1408: dl = HDL(nd_ps[ndp]);
1409: td = HTD(nd_ps[ndp]);
1410: for ( head = last = 0, p = nd; p; ) {
1411: dln = HDL(nd_ps[(int)BDY(p)]);
1412: tdn = HTD(nd_ps[(int)BDY(p)]);
1413: if ( tdn >= td && ndl_reducible( dln, dl ) ) {
1414: p = NEXT(p);
1415: if ( last ) NEXT(last) = p;
1416: } else {
1417: if ( !last ) head = p;
1418: p = NEXT(last = p);
1419: }
1420: }
1421: head = append_one(head,ndp);
1422: return head;
1423: }
1424:
1425: ND_pairs nd_minp( ND_pairs d, ND_pairs *prest )
1426: {
1427: ND_pairs m,ml,p,l;
1428: unsigned int *lcm;
1429: int s,td,len,tlen,c;
1430:
1431: if ( !(p = NEXT(m = d)) ) {
1432: *prest = p;
1433: NEXT(m) = 0;
1434: return m;
1435: }
1436: lcm = m->lcm;
1.14 ! noro 1437: s = SG(m);
! 1438: td = TD(m);
1.6 noro 1439: len = nd_psl[m->i1]+nd_psl[m->i2];
1.1 noro 1440: for ( ml = 0, l = m; p; p = NEXT(l = p) ) {
1.14 ! noro 1441: if (SG(p) < s)
1.1 noro 1442: goto find;
1.14 ! noro 1443: else if ( SG(p) == s ) {
! 1444: if ( TD(p) < td )
1.1 noro 1445: goto find;
1.14 ! noro 1446: else if ( TD(p) == td ) {
1.1 noro 1447: c = ndl_compare(p->lcm,lcm);
1448: if ( c < 0 )
1449: goto find;
1.10 noro 1450: #if 0
1.1 noro 1451: else if ( c == 0 ) {
1.6 noro 1452: tlen = nd_psl[p->i1]+nd_psl[p->i2];
1.1 noro 1453: if ( tlen < len )
1454: goto find;
1455: }
1.10 noro 1456: #endif
1.1 noro 1457: }
1458: }
1459: continue;
1460: find:
1461: ml = l;
1462: m = p;
1463: lcm = m->lcm;
1.14 ! noro 1464: s = SG(m);
! 1465: td = TD(m);
1.1 noro 1466: len = tlen;
1467: }
1468: if ( !ml ) *prest = NEXT(m);
1469: else {
1470: NEXT(ml) = NEXT(m);
1471: *prest = d;
1472: }
1473: NEXT(m) = 0;
1474: return m;
1475: }
1476:
1477: int nd_newps(ND a)
1478: {
1.3 noro 1479: int len;
1.13 noro 1480: RHist r;
1.3 noro 1481:
1.1 noro 1482: if ( nd_psn == nd_pslen ) {
1483: nd_pslen *= 2;
1.6 noro 1484: nd_psl = (int *)REALLOC((char *)nd_psl,nd_pslen*sizeof(int));
1.4 noro 1485: #if USE_NDV
1.11 noro 1486: nd_ps = (NDV *)REALLOC((char *)nd_ps,nd_pslen*sizeof(NDV));
1487: #else
1488: nd_ps = (ND *)REALLOC((char *)nd_ps,nd_pslen*sizeof(ND));
1.4 noro 1489: #endif
1.13 noro 1490: nd_psh = (RHist *)REALLOC((char *)nd_psh,nd_pslen*sizeof(RHist));
1.1 noro 1491: nd_bound = (unsigned int **)
1492: REALLOC((char *)nd_bound,nd_pslen*sizeof(unsigned int *));
1493: }
1494: nd_monic(a);
1495: nd_bound[nd_psn] = nd_compute_bound(a);
1.14 ! noro 1496: NEWRHist(r); TD(r) = HTD(a); ndl_copy(HDL(a),DL(r)); nd_psh[nd_psn] = r;
1.5 noro 1497: #if USE_NDV
1.11 noro 1498: nd_ps[nd_psn]= ndtondv(a);
1.13 noro 1499: nd_free(a);
1.14 ! noro 1500: nd_psl[nd_psn] = len = LEN(nd_ps[nd_psn]);
1.3 noro 1501: if ( len > nmv_len ) {
1502: nmv_len = 2*len;
1503: BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv);
1504: }
1.11 noro 1505: #else
1506: nd_ps[nd_psn] = a;
1507: nd_psl[nd_psn] = nd_length(a);
1.4 noro 1508: #endif
1.1 noro 1509: return nd_psn++;
1510: }
1511:
1512: NODE nd_setup(NODE f)
1513: {
1.5 noro 1514: int i,j,td,len,max;
1.1 noro 1515: NODE s,s0,f0;
1.5 noro 1516: unsigned int *d;
1.13 noro 1517: RHist r;
1.11 noro 1518:
1519: nd_found = 0; nd_notfirst = 0; nd_create = 0;
1.1 noro 1520:
1521: nd_psn = length(f); nd_pslen = 2*nd_psn;
1.6 noro 1522: nd_psl = (int *)MALLOC(nd_pslen*sizeof(int));
1.4 noro 1523: #if USE_NDV
1.11 noro 1524: nd_ps = (NDV *)MALLOC(nd_pslen*sizeof(NDV));
1525: #else
1526: nd_ps = (ND *)MALLOC(nd_pslen*sizeof(ND));
1.4 noro 1527: #endif
1.13 noro 1528: nd_psh = (RHist *)MALLOC(nd_pslen*sizeof(RHist));
1.1 noro 1529: nd_bound = (unsigned int **)MALLOC(nd_pslen*sizeof(unsigned int *));
1.5 noro 1530: for ( max = 0, i = 0, s = f; i < nd_psn; i++, s = NEXT(s) ) {
1531: nd_bound[i] = d = dp_compute_bound((DP)BDY(s));
1532: for ( j = 0; j < nd_nvar; j++ )
1533: max = MAX(d[j],max);
1534: }
1.11 noro 1535: if ( !nd_red )
1.13 noro 1536: nd_red = (RHist *)MALLOC(REDTAB_LEN*sizeof(RHist));
1537: bzero(nd_red,REDTAB_LEN*sizeof(RHist));
1.5 noro 1538:
1539: if ( max < 2 )
1540: nd_bpe = 2;
1541: else if ( max < 4 )
1542: nd_bpe = 4;
1543: else if ( max < 64 )
1544: nd_bpe = 6;
1545: else if ( max < 256 )
1546: nd_bpe = 8;
1547: else if ( max < 65536 )
1548: nd_bpe = 16;
1549: else
1550: nd_bpe = 32;
1.13 noro 1551:
1.1 noro 1552: nd_setup_parameters();
1553: nd_free_private_storage();
1.3 noro 1554: len = 0;
1.1 noro 1555: for ( i = 0; i < nd_psn; i++, f = NEXT(f) ) {
1.11 noro 1556: #if USE_NDV
1557: nd_ps[i] = dptondv((DP)BDY(f));
1.14 ! noro 1558: ndv_mul_c(nd_ps[i],invm(HCM(nd_ps[i]),nd_mod));
! 1559: len = MAX(len,LEN(nd_ps[i]));
1.11 noro 1560: #else
1.1 noro 1561: nd_ps[i] = dptond((DP)BDY(f));
1.11 noro 1562: nd_mul_c(nd_ps[i],1);
1.6 noro 1563: nd_psl[i] = nd_length(nd_ps[i]);
1.4 noro 1564: #endif
1.14 ! noro 1565: NEWRHist(r); TD(r) = HTD(nd_ps[i]); ndl_copy(HDL(nd_ps[i]),DL(r));
1.13 noro 1566: nd_psh[i] = r;
1.1 noro 1567: }
1.4 noro 1568: #if USE_NDV
1.3 noro 1569: nmv_len = 16*len;
1570: NEWNDV(ndv_red);
1571: BDY(ndv_red) = (NMV)MALLOC_ATOMIC(nmv_len*nmv_adv);
1.4 noro 1572: #endif
1.1 noro 1573: for ( s0 = 0, i = 0; i < nd_psn; i++ ) {
1574: NEXTNODE(s0,s); BDY(s) = (pointer)i;
1575: }
1576: if ( s0 ) NEXT(s) = 0;
1577: return s0;
1578: }
1579:
1580: void nd_gr(LIST f,LIST v,int m,struct order_spec *ord,LIST *rp)
1581: {
1582: struct order_spec ord1;
1583: VL fv,vv,vc;
1584: NODE fd,fd0,r,r0,t,x,s,xx;
1585: DP a,b,c;
1586:
1587: get_vars((Obj)f,&fv); pltovl(v,&vv);
1588: nd_nvar = length(vv);
1589: if ( ord->id )
1590: error("nd_gr : unsupported order");
1591: switch ( ord->ord.simple ) {
1592: case 0:
1593: is_rlex = 1;
1594: break;
1595: case 1:
1596: is_rlex = 0;
1597: break;
1598: default:
1599: error("nd_gr : unsupported order");
1600: }
1601: initd(ord);
1602: nd_mod = m;
1603: for ( fd0 = 0, t = BDY(f); t; t = NEXT(t) ) {
1604: ptod(CO,vv,(P)BDY(t),&b);
1605: _dp_mod(b,m,0,&c);
1606: if ( c ) {
1607: NEXTNODE(fd0,fd); BDY(fd) = (pointer)c;
1608: }
1609: }
1610: if ( fd0 ) NEXT(fd) = 0;
1611: s = nd_setup(fd0);
1612: x = nd_gb(s);
1613: #if 0
1614: x = nd_reduceall(x,m);
1615: #endif
1616: for ( r0 = 0; x; x = NEXT(x) ) {
1617: NEXTNODE(r0,r);
1.11 noro 1618: #if USE_NDV
1619: a = ndvtodp(nd_ps[(int)BDY(x)]);
1620: #else
1.1 noro 1621: a = ndtodp(nd_ps[(int)BDY(x)]);
1.11 noro 1622: #endif
1.1 noro 1623: _dtop_mod(CO,vv,a,(P *)&BDY(r));
1624: }
1625: if ( r0 ) NEXT(r) = 0;
1626: MKLIST(*rp,r0);
1627: fprintf(asir_out,"found=%d,notfirst=%d,create=%d\n",
1628: nd_found,nd_notfirst,nd_create);
1629: }
1630:
1631: void dltondl(int n,DL dl,unsigned int *r)
1632: {
1633: unsigned int *d;
1634: int i;
1635:
1636: d = dl->d;
1637: bzero(r,nd_wpd*sizeof(unsigned int));
1638: if ( is_rlex )
1639: for ( i = 0; i < n; i++ )
1640: r[(n-1-i)/nd_epw] |= (d[i]<<((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe));
1641: else
1642: for ( i = 0; i < n; i++ )
1643: r[i/nd_epw] |= d[i]<<((nd_epw-(i%nd_epw)-1)*nd_bpe);
1644: }
1645:
1646: DL ndltodl(int n,int td,unsigned int *ndl)
1647: {
1648: DL dl;
1649: int *d;
1650: int i;
1651:
1652: NEWDL(dl,n);
1.14 ! noro 1653: TD(dl) = td;
1.1 noro 1654: d = dl->d;
1655: if ( is_rlex )
1656: for ( i = 0; i < n; i++ )
1657: d[i] = (ndl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
1658: &((1<<nd_bpe)-1);
1659: else
1660: for ( i = 0; i < n; i++ )
1661: d[i] = (ndl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
1662: &((1<<nd_bpe)-1);
1663: return dl;
1664: }
1665:
1666: ND dptond(DP p)
1667: {
1668: ND d;
1669: NM m0,m;
1670: MP t;
1671: int n;
1672:
1673: if ( !p )
1674: return 0;
1675: n = NV(p);
1676: m0 = 0;
1677: for ( t = BDY(p); t; t = NEXT(t) ) {
1678: NEXTNM(m0,m);
1.14 ! noro 1679: CM(m) = ITOS(C(t));
! 1680: TD(m) = TD(DL(t));
! 1681: dltondl(n,DL(t),DL(m));
1.1 noro 1682: }
1683: NEXT(m) = 0;
1684: MKND(n,m0,d);
1.14 ! noro 1685: NV(d) = n;
! 1686: SG(d) = SG(p);
1.1 noro 1687: return d;
1688: }
1689:
1690: DP ndtodp(ND p)
1691: {
1692: DP d;
1693: MP m0,m;
1694: NM t;
1695: int n;
1696:
1697: if ( !p )
1698: return 0;
1699: n = NV(p);
1700: m0 = 0;
1701: for ( t = BDY(p); t; t = NEXT(t) ) {
1702: NEXTMP(m0,m);
1.14 ! noro 1703: C(m) = STOI(CM(t));
! 1704: DL(m) = ndltodl(n,TD(t),DL(t));
1.1 noro 1705: }
1706: NEXT(m) = 0;
1707: MKDP(n,m0,d);
1.14 ! noro 1708: SG(d) = SG(p);
1.1 noro 1709: return d;
1710: }
1711:
1712: void ndl_print(unsigned int *dl)
1713: {
1714: int n;
1715: int i;
1716:
1717: n = nd_nvar;
1718: printf("<<");
1719: if ( is_rlex )
1720: for ( i = 0; i < n; i++ )
1721: printf(i==n-1?"%d":"%d,",
1722: (dl[(n-1-i)/nd_epw]>>((nd_epw-((n-1-i)%nd_epw)-1)*nd_bpe))
1723: &((1<<nd_bpe)-1));
1724: else
1725: for ( i = 0; i < n; i++ )
1726: printf(i==n-1?"%d":"%d,",
1727: (dl[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))
1728: &((1<<nd_bpe)-1));
1729: printf(">>");
1730: }
1731:
1732: void nd_print(ND p)
1733: {
1734: NM m;
1735:
1736: if ( !p )
1737: printf("0\n");
1738: else {
1739: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 ! noro 1740: printf("+%d*",CM(m));
! 1741: ndl_print(DL(m));
1.1 noro 1742: }
1743: printf("\n");
1744: }
1745: }
1746:
1747: void ndp_print(ND_pairs d)
1748: {
1749: ND_pairs t;
1750:
1751: for ( t = d; t; t = NEXT(t) ) {
1752: printf("%d,%d ",t->i1,t->i2);
1753: }
1754: printf("\n");
1755: }
1756:
1757: void nd_monic(ND p)
1758: {
1759: if ( !p )
1760: return;
1761: else
1.14 ! noro 1762: nd_mul_c(p,invm(HCM(p),nd_mod));
1.1 noro 1763: }
1764:
1765: void nd_mul_c(ND p,int mul)
1766: {
1767: NM m;
1768: int c,c1;
1769:
1770: if ( !p )
1771: return;
1772: for ( m = BDY(p); m; m = NEXT(m) ) {
1.14 ! noro 1773: c1 = CM(m);
1.1 noro 1774: DMAR(c1,mul,0,nd_mod,c);
1.14 ! noro 1775: CM(m) = c;
1.1 noro 1776: }
1777: }
1778:
1779: void nd_free(ND p)
1780: {
1781: NM t,s;
1782:
1783: if ( !p )
1784: return;
1785: t = BDY(p);
1786: while ( t ) {
1787: s = NEXT(t);
1788: FREENM(t);
1789: t = s;
1790: }
1791: FREEND(p);
1792: }
1793:
1794: void nd_append_red(unsigned int *d,int td,int i)
1795: {
1.13 noro 1796: RHist m,m0;
1.1 noro 1797: int h;
1798:
1.13 noro 1799: NEWRHist(m);
1.1 noro 1800: h = ndl_hash_value(td,d);
1.13 noro 1801: m->index = i;
1.14 ! noro 1802: TD(m) = td;
! 1803: ndl_copy(d,DL(m));
1.1 noro 1804: NEXT(m) = nd_red[h];
1805: nd_red[h] = m;
1806: }
1807:
1.5 noro 1808: unsigned int *dp_compute_bound(DP p)
1809: {
1810: unsigned int *d,*d1,*d2,*t;
1811: MP m;
1.7 noro 1812: int i,l;
1.5 noro 1813:
1814: if ( !p )
1815: return 0;
1816: d1 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
1817: d2 = (unsigned int *)ALLOCA(nd_nvar*sizeof(unsigned int));
1818: m = BDY(p);
1.14 ! noro 1819: bcopy(DL(m)->d,d1,nd_nvar*sizeof(unsigned int));
1.5 noro 1820: for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14 ! noro 1821: d = DL(m)->d;
1.5 noro 1822: for ( i = 0; i < nd_nvar; i++ )
1823: d2[i] = d[i] > d1[i] ? d[i] : d1[i];
1824: t = d1; d1 = d2; d2 = t;
1825: }
1.13 noro 1826: l = (nd_nvar+31);
1.7 noro 1827: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1828: bzero(t,l*sizeof(unsigned int));
1.5 noro 1829: bcopy(d1,t,nd_nvar*sizeof(unsigned int));
1830: return t;
1831: }
1832:
1.1 noro 1833: unsigned int *nd_compute_bound(ND p)
1834: {
1835: unsigned int *d1,*d2,*t;
1.9 noro 1836: int i,l;
1.1 noro 1837: NM m;
1838:
1839: if ( !p )
1840: return 0;
1841: d1 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1842: d2 = (unsigned int *)ALLOCA(nd_wpd*sizeof(unsigned int));
1843: bcopy(HDL(p),d1,nd_wpd*sizeof(unsigned int));
1844: for ( m = NEXT(BDY(p)); m; m = NEXT(m) ) {
1.14 ! noro 1845: ndl_lcm(DL(m),d1,d2);
1.1 noro 1846: t = d1; d1 = d2; d2 = t;
1847: }
1.12 noro 1848: l = nd_nvar+31;
1.9 noro 1849: t = (unsigned int *)MALLOC_ATOMIC(l*sizeof(unsigned int));
1850: bzero(t,l*sizeof(unsigned int));
1.5 noro 1851: for ( i = 0; i < nd_nvar; i++ )
1852: t[i] = (d1[i/nd_epw]>>((nd_epw-(i%nd_epw)-1)*nd_bpe))&nd_mask0;
1.1 noro 1853: return t;
1854: }
1855:
1856: void nd_setup_parameters() {
1857: int i;
1858:
1859: nd_epw = (sizeof(unsigned int)*8)/nd_bpe;
1860: nd_wpd = nd_nvar/nd_epw+(nd_nvar%nd_epw?1:0);
1861: if ( nd_bpe < 32 ) {
1862: nd_mask0 = (1<<nd_bpe)-1;
1863: } else {
1864: nd_mask0 = 0xffffffff;
1865: }
1866: bzero(nd_mask,sizeof(nd_mask));
1867: nd_mask1 = 0;
1868: for ( i = 0; i < nd_epw; i++ ) {
1869: nd_mask[nd_epw-i-1] = (nd_mask0<<(i*nd_bpe));
1870: nd_mask1 |= (1<<(nd_bpe-1))<<(i*nd_bpe);
1871: }
1.13 noro 1872: nm_adv = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
1.4 noro 1873: #if USE_NDV
1.3 noro 1874: nmv_adv = sizeof(struct oNMV)+(nd_wpd-1)*sizeof(unsigned int);
1.4 noro 1875: #endif
1.1 noro 1876: }
1877:
1878: ND_pairs nd_reconstruct(ND_pairs d)
1879: {
1.11 noro 1880: int i,obpe,oadv;
1.13 noro 1881: NM prev_nm_free_list;
1882: RHist mr0,mr;
1883: RHist r;
1.1 noro 1884: ND_pairs s0,s,t,prev_ndp_free_list;
1885:
1886: obpe = nd_bpe;
1.11 noro 1887: #if USE_NDV
1888: oadv = nmv_adv;
1889: #endif
1.5 noro 1890: if ( obpe < 4 )
1891: nd_bpe = 4;
1892: else if ( obpe < 6 )
1893: nd_bpe = 6;
1894: else if ( obpe < 8 )
1895: nd_bpe = 8;
1896: else if ( obpe < 16 )
1897: nd_bpe = 16;
1898: else if ( obpe < 32 )
1899: nd_bpe = 32;
1900: else
1901: error("nd_reconstruct : exponent too large");
1902:
1.1 noro 1903: nd_setup_parameters();
1904: prev_nm_free_list = _nm_free_list;
1905: prev_ndp_free_list = _ndp_free_list;
1906: _nm_free_list = 0;
1907: _ndp_free_list = 0;
1.13 noro 1908: for ( i = nd_psn-1; i >= 0; i-- ) {
1.4 noro 1909: #if USE_NDV
1.11 noro 1910: ndv_realloc(nd_ps[i],obpe,oadv);
1911: #else
1912: nd_realloc(nd_ps[i],obpe);
1.4 noro 1913: #endif
1.1 noro 1914: }
1915: s0 = 0;
1916: for ( t = d; t; t = NEXT(t) ) {
1917: NEXTND_pairs(s0,s);
1918: s->i1 = t->i1;
1919: s->i2 = t->i2;
1.14 ! noro 1920: TD(s) = TD(t);
! 1921: SG(s) = SG(t);
1.1 noro 1922: ndl_dup(obpe,t->lcm,s->lcm);
1923: }
1.6 noro 1924: for ( i = 0; i < REDTAB_LEN; i++ ) {
1.13 noro 1925: for ( mr0 = 0, r = nd_red[i]; r; r = NEXT(r) ) {
1926: NEXTRHist(mr0,r);
1927: mr->index = r->index;
1.14 ! noro 1928: TD(mr) = TD(r);
! 1929: ndl_dup(obpe,DL(r),DL(mr));
1.6 noro 1930: }
1931: if ( mr0 ) NEXT(mr) = 0;
1932: nd_red[i] = mr0;
1933: }
1.11 noro 1934: for ( i = 0; i < nd_psn; i++ ) {
1.14 ! noro 1935: NEWRHist(r); TD(r) = TD(nd_psh[i]); ndl_dup(obpe,DL(nd_psh[i]),DL(r));
1.13 noro 1936: nd_psh[i] = r;
1.11 noro 1937: }
1.1 noro 1938: if ( s0 ) NEXT(s) = 0;
1939: prev_nm_free_list = 0;
1940: prev_ndp_free_list = 0;
1.4 noro 1941: #if USE_NDV
1.3 noro 1942: BDY(ndv_red) = (NMV)REALLOC(BDY(ndv_red),nmv_len*nmv_adv);
1.4 noro 1943: #endif
1.1 noro 1944: GC_gcollect();
1945: return s0;
1946: }
1947:
1948: void ndl_dup(int obpe,unsigned int *d,unsigned int *r)
1949: {
1950: int n,i,ei,oepw,cepw,cbpe;
1951:
1952: n = nd_nvar;
1953: oepw = (sizeof(unsigned int)*8)/obpe;
1954: cepw = nd_epw;
1955: cbpe = nd_bpe;
1956: if ( is_rlex )
1957: for ( i = 0; i < n; i++ ) {
1958: ei = (d[(n-1-i)/oepw]>>((oepw-((n-1-i)%oepw)-1)*obpe))
1959: &((1<<obpe)-1);
1960: r[(n-1-i)/cepw] |= (ei<<((cepw-((n-1-i)%cepw)-1)*cbpe));
1961: }
1962: else
1963: for ( i = 0; i < n; i++ ) {
1964: ei = (d[i/oepw]>>((oepw-(i%oepw)-1)*obpe))
1965: &((1<<obpe)-1);
1966: r[i/cepw] |= (ei<<((cepw-(i%cepw)-1)*cbpe));
1967: }
1968: }
1969:
1.11 noro 1970: void nd_realloc(ND p,int obpe)
1.1 noro 1971: {
1972: NM m,mr,mr0;
1973:
1.11 noro 1974: if ( p ) {
1975: m = BDY(p);
1.1 noro 1976: for ( mr0 = 0; m; m = NEXT(m) ) {
1977: NEXTNM(mr0,mr);
1.14 ! noro 1978: CM(mr) = CM(m);
! 1979: TD(mr) = TD(m);
! 1980: ndl_dup(obpe,DL(m),DL(mr));
1.1 noro 1981: }
1982: NEXT(mr) = 0;
1.11 noro 1983: BDY(p) = mr0;
1.1 noro 1984: }
1985: }
1.3 noro 1986:
1.6 noro 1987: ND nd_copy(ND p)
1988: {
1989: NM m,mr,mr0;
1990: int c,n,s;
1991: ND r;
1992:
1993: if ( !p )
1994: return 0;
1995: else {
1996: s = sizeof(struct oNM)+(nd_wpd-1)*sizeof(unsigned int);
1997: for ( mr0 = 0, m = BDY(p); m; m = NEXT(m) ) {
1998: NEXTNM(mr0,mr);
1.14 ! noro 1999: CM(mr) = CM(m);
! 2000: TD(mr) = TD(m);
! 2001: ndl_copy(DL(m),DL(mr));
1.6 noro 2002: }
2003: NEXT(mr) = 0;
2004: MKND(NV(p),mr0,r);
1.14 ! noro 2005: SG(r) = SG(p);
1.6 noro 2006: return r;
2007: }
2008: }
2009:
1.4 noro 2010: #if USE_NDV
1.11 noro 2011: int nd_sp(ND_pairs p,ND *rp)
2012: {
2013: NM m;
2014: NDV p1,p2;
2015: ND t1,t2;
2016: unsigned int *lcm;
2017: int td;
2018:
2019: p1 = nd_ps[p->i1];
2020: p2 = nd_ps[p->i2];
2021: lcm = p->lcm;
1.14 ! noro 2022: td = TD(p);
1.11 noro 2023: NEWNM(m);
1.14 ! noro 2024: CM(m) = HCM(p2); TD(m) = td-HTD(p1); ndl_sub(lcm,HDL(p1),DL(m));
! 2025: if ( ndl_check_bound2(p->i1,DL(m)) )
1.11 noro 2026: return 0;
2027: t1 = ndv_mul_nm_create(p1,m);
1.14 ! noro 2028: CM(m) = nd_mod-HCM(p1); TD(m) = td-HTD(p2); ndl_sub(lcm,HDL(p2),DL(m));
! 2029: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 2030: nd_free(t1);
2031: return 0;
2032: }
2033: ndv_mul_nm(p2,m,ndv_red);
2034: FREENM(m);
2035: *rp = ndv_add(t1,ndv_red);
2036: return 1;
2037: }
2038:
2039: void ndv_mul_c(NDV p,int mul)
2040: {
2041: NMV m;
2042: int c,c1,len,i;
2043:
2044: if ( !p )
2045: return;
1.14 ! noro 2046: len = LEN(p);
1.11 noro 2047: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14 ! noro 2048: c1 = CM(m);
1.11 noro 2049: DMAR(c1,mul,0,nd_mod,c);
1.14 ! noro 2050: CM(m) = c;
1.11 noro 2051: }
2052: }
2053:
1.4 noro 2054: void ndv_mul_nm(NDV p,NM m0,NDV r)
2055: {
2056: NMV m,mr,mr0;
2057: unsigned int *d,*dt,*dm;
2058: int c,n,td,i,c1,c2,len;
2059:
2060: if ( !p )
2061: /* XXX */
1.14 ! noro 2062: LEN(r) = 0;
1.4 noro 2063: else {
1.14 ! noro 2064: n = NV(p); m = BDY(p); len = LEN(p);
! 2065: d = DL(m0); td = TD(m0); c = CM(m0);
1.4 noro 2066: mr = BDY(r);
1.9 noro 2067: for ( ; len > 0; len--, NMV_ADV(m), NMV_ADV(mr) ) {
1.14 ! noro 2068: c1 = CM(m); DMAR(c1,c,0,nd_mod,c2); CM(mr) = c2;
! 2069: TD(mr) = TD(m)+td; ndl_add(DL(m),d,DL(mr));
1.9 noro 2070: }
2071: NV(r) = NV(p);
1.14 ! noro 2072: LEN(r) = LEN(p);
! 2073: SG(r) = SG(p) + td;
1.9 noro 2074: }
2075: }
2076:
2077: ND ndv_mul_nm_create(NDV p,NM m0)
2078: {
2079: NM mr,mr0;
2080: NMV m;
2081: unsigned int *d,*dt,*dm;
2082: int c,n,td,i,c1,c2,len;
2083: ND r;
2084:
2085: if ( !p )
2086: return 0;
2087: else {
2088: n = NV(p); m = BDY(p);
1.14 ! noro 2089: d = DL(m0); td = TD(m0); c = CM(m0);
! 2090: len = LEN(p);
1.9 noro 2091: mr0 = 0;
2092: for ( i = 0; i < len; i++, NMV_ADV(m) ) {
2093: NEXTNM(mr0,mr);
1.14 ! noro 2094: c1 = CM(m);
1.4 noro 2095: DMAR(c1,c,0,nd_mod,c2);
1.14 ! noro 2096: CM(mr) = c2;
! 2097: TD(mr) = TD(m)+td;
! 2098: ndl_add(DL(m),d,DL(mr));
1.4 noro 2099: }
1.9 noro 2100: NEXT(mr) = 0;
2101: MKND(NV(p),mr0,r);
1.14 ! noro 2102: SG(r) = SG(p) + td;
1.9 noro 2103: return r;
1.4 noro 2104: }
2105: }
2106:
2107: ND ndv_add(ND p1,NDV p2)
2108: {
1.9 noro 2109: register NM prev,cur,new;
1.4 noro 2110: int c,c1,c2,t,td,td2,mul,len,i;
1.9 noro 2111: NM head;
1.4 noro 2112: unsigned int *d;
2113: NMV m2;
2114:
2115: if ( !p1 )
2116: return 0;
2117: else {
2118: prev = 0; head = cur = BDY(p1);
1.14 ! noro 2119: NEWNM(new); len = LEN(p2);
1.9 noro 2120: for ( m2 = BDY(p2), i = 0; cur && i < len; ) {
1.14 ! noro 2121: td2 = TD(new) = TD(m2);
! 2122: if ( TD(cur) > td2 ) {
1.13 noro 2123: prev = cur; cur = NEXT(cur);
2124: continue;
1.14 ! noro 2125: } else if ( TD(cur) < td2 ) c = -1;
1.13 noro 2126: else if ( nd_wpd == 1 ) {
1.14 ! noro 2127: if ( DL(cur)[0] > DL(m2)[0] ) c = is_rlex ? -1 : 1;
! 2128: else if ( DL(cur)[0] < DL(m2)[0] ) c = is_rlex ? 1 : -1;
1.13 noro 2129: else c = 0;
2130: }
1.14 ! noro 2131: else c = ndl_compare(DL(cur),DL(m2));
1.4 noro 2132: switch ( c ) {
2133: case 0:
1.14 ! noro 2134: t = CM(m2)+CM(cur)-nd_mod;
1.9 noro 2135: if ( t < 0 ) t += nd_mod;
1.14 ! noro 2136: if ( t ) CM(cur) = t;
1.4 noro 2137: else if ( !prev ) {
1.9 noro 2138: head = NEXT(cur); FREENM(cur); cur = head;
1.4 noro 2139: } else {
1.9 noro 2140: NEXT(prev) = NEXT(cur); FREENM(cur); cur = NEXT(prev);
1.4 noro 2141: }
2142: NMV_ADV(m2); i++;
2143: break;
2144: case 1:
1.9 noro 2145: prev = cur; cur = NEXT(cur);
1.4 noro 2146: break;
2147: case -1:
1.14 ! noro 2148: ndl_copy(DL(m2),DL(new));
! 2149: CM(new) = CM(m2);
1.4 noro 2150: if ( !prev ) {
2151: /* cur = head */
1.9 noro 2152: prev = new; NEXT(prev) = head; head = prev;
1.4 noro 2153: } else {
1.9 noro 2154: NEXT(prev) = new; NEXT(new) = cur; prev = new;
1.4 noro 2155: }
1.9 noro 2156: NEWNM(new); NMV_ADV(m2); i++;
1.4 noro 2157: break;
2158: }
2159: }
1.9 noro 2160: for ( ; i < len; i++, NMV_ADV(m2) ) {
1.14 ! noro 2161: td2 = TD(new) = TD(m2); CM(new) = CM(m2); ndl_copy(DL(m2),DL(new));
1.9 noro 2162: if ( !prev ) {
2163: prev = new; NEXT(prev) = 0; head = prev;
2164: } else {
2165: NEXT(prev) = new; NEXT(new) = 0; prev = new;
2166: }
2167: NEWNM(new);
2168: }
1.4 noro 2169: FREENM(new);
2170: if ( head ) {
1.14 ! noro 2171: BDY(p1) = head; SG(p1) = MAX(SG(p1),SG(p2));
1.4 noro 2172: return p1;
2173: } else {
2174: FREEND(p1);
2175: return 0;
2176: }
2177:
2178: }
2179: }
2180:
1.11 noro 2181: void ndv_realloc(NDV p,int obpe,int oadv)
2182: {
1.13 noro 2183: NMV m,mr,mr0,t;
2184: int len,i,k;
1.11 noro 2185:
1.13 noro 2186: #define NMV_OPREV(m) (m = (NMV)(((char *)m)-oadv))
2187: #define NMV_PREV(m) (m = (NMV)(((char *)m)-nmv_adv))
1.11 noro 2188:
2189: if ( p ) {
1.14 ! noro 2190: m = BDY(p); len = LEN(p);
1.13 noro 2191: mr0 = (NMV)REALLOC(BDY(p),len*nmv_adv);
2192: m = (NMV)((char *)mr0+(len-1)*oadv);
2193: mr = (NMV)((char *)mr0+(len-1)*nmv_adv);
2194: t = (NMV)ALLOCA(nmv_adv);
2195: for ( i = 0; i < len; i++, NMV_OPREV(m), NMV_PREV(mr) ) {
1.14 ! noro 2196: CM(t) = CM(m);
! 2197: TD(t) = TD(m);
! 2198: for ( k = 0; k < nd_wpd; k++ ) DL(t)[k] = 0;
! 2199: ndl_dup(obpe,DL(m),DL(t));
! 2200: CM(mr) = CM(t);
! 2201: TD(mr) = TD(t);
! 2202: ndl_copy(DL(t),DL(mr));
1.11 noro 2203: }
2204: BDY(p) = mr0;
2205: }
2206: }
2207:
1.3 noro 2208: NDV ndtondv(ND p)
2209: {
2210: NDV d;
2211: NMV m,m0;
2212: NM t;
2213: int i,len;
2214:
2215: if ( !p )
2216: return 0;
2217: len = nd_length(p);
2218: m0 = m = (NMV)MALLOC_ATOMIC(len*nmv_adv);
2219: for ( t = BDY(p), i = 0; t; t = NEXT(t), i++, NMV_ADV(m) ) {
1.14 ! noro 2220: TD(m) = TD(t);
! 2221: ndl_copy(DL(t),DL(m));
! 2222: CM(m) = CM(t);
1.3 noro 2223: }
2224: MKNDV(NV(p),m0,len,d);
1.14 ! noro 2225: SG(d) = SG(p);
1.3 noro 2226: return d;
2227: }
2228:
1.11 noro 2229: NDV dptondv(DP p)
2230: {
2231: NDV d;
2232: NMV m0,m;
2233: MP t;
2234: int l,i,n;
2235:
2236: if ( !p )
2237: return 0;
2238: for ( t = BDY(p), l = 0; t; t = NEXT(t), l++ );
2239: m0 = m = (NMV)MALLOC_ATOMIC(l*nmv_adv);
2240: n = NV(p);
2241: for ( t = BDY(p), i = 0; i < l; i++, t = NEXT(t), NMV_ADV(m) ) {
1.14 ! noro 2242: CM(m) = ITOS(C(t));
! 2243: TD(m) = TD(DL(t));
! 2244: dltondl(n,DL(t),DL(m));
1.11 noro 2245: }
2246: MKNDV(n,m0,l,d);
1.14 ! noro 2247: SG(d) = SG(p);
1.11 noro 2248: return d;
2249: }
2250:
2251: DP ndvtodp(NDV p)
2252: {
2253: DP d;
2254: MP m0,m;
2255: NMV t;
2256: int len,i,n;
2257:
2258: if ( !p )
2259: return 0;
2260: m0 = 0;
1.14 ! noro 2261: len = LEN(p);
1.11 noro 2262: n = NV(p);
2263: for ( t = BDY(p), i = 0; i < len; i++, NMV_ADV(t) ) {
2264: NEXTMP(m0,m);
1.14 ! noro 2265: C(m) = STOI(CM(t));
! 2266: DL(m) = ndltodl(n,TD(t),DL(t));
1.11 noro 2267: }
2268: NEXT(m) = 0;
2269: MKDP(NV(p),m0,d);
1.14 ! noro 2270: SG(d) = SG(p);
1.11 noro 2271: return d;
2272: }
2273:
1.3 noro 2274: void ndv_print(NDV p)
2275: {
2276: NMV m;
2277: int i,len;
2278:
2279: if ( !p )
2280: printf("0\n");
2281: else {
1.14 ! noro 2282: len = LEN(p);
1.3 noro 2283: for ( m = BDY(p), i = 0; i < len; i++, NMV_ADV(m) ) {
1.14 ! noro 2284: printf("+%d*",CM(m));
! 2285: ndl_print(DL(m));
1.3 noro 2286: }
2287: printf("\n");
2288: }
1.11 noro 2289: }
2290: #else
2291: int nd_sp(ND_pairs p,ND *rp)
2292: {
2293: NM m;
2294: ND p1,p2;
2295: ND t1,t2;
2296: unsigned int *lcm;
2297: int td;
2298:
2299: p1 = nd_ps[p->i1];
2300: p2 = nd_ps[p->i2];
2301: lcm = p->lcm;
1.14 ! noro 2302: td = TD(p);
1.11 noro 2303: NEWNM(m);
1.14 ! noro 2304: CM(m) = HCM(p2); TD(m) = td-HTD(p1); ndl_sub(lcm,HDL(p1),DL(m));
! 2305: if ( ndl_check_bound2(p->i1,DL(m)) )
1.11 noro 2306: return 0;
2307: t1 = nd_mul_ind_nm(p->i1,m);
1.14 ! noro 2308: CM(m) = nd_mod-HCM(p1); TD(m) = td-HTD(p2); ndl_sub(lcm,HDL(p2),DL(m));
! 2309: if ( ndl_check_bound2(p->i2,DL(m)) ) {
1.11 noro 2310: nd_free(t1);
2311: return 0;
2312: }
2313: t2 = nd_mul_ind_nm(p->i2,m);
2314: FREENM(m);
2315: *rp = nd_add(t1,t2);
2316: return 1;
2317: }
2318:
2319: ND nd_mul_nm(ND p,NM m0)
2320: {
2321: NM m,mr,mr0;
2322: unsigned int *d,*dt,*dm;
2323: int c,n,td,i,c1,c2;
2324: int *pt,*p1,*p2;
2325: ND r;
2326:
2327: if ( !p )
2328: return 0;
2329: else {
2330: n = NV(p); m = BDY(p);
1.14 ! noro 2331: d = DL(m0); td = TD(m0); c = CM(m0);
1.11 noro 2332: mr0 = 0;
2333: for ( ; m; m = NEXT(m) ) {
2334: NEXTNM(mr0,mr);
1.14 ! noro 2335: c1 = CM(m);
1.11 noro 2336: DMAR(c1,c,0,nd_mod,c2);
1.14 ! noro 2337: CM(mr) = c2;
! 2338: TD(mr) = TD(m)+td;
! 2339: ndl_add(DL(m),d,DL(mr));
1.11 noro 2340: }
2341: NEXT(mr) = 0;
2342: MKND(NV(p),mr0,r);
1.14 ! noro 2343: SG(r) = SG(p) + td;
1.11 noro 2344: return r;
2345: }
2346: }
2347:
2348: ND nd_mul_ind_nm(int index,NM m0)
2349: {
2350: register int c1,c2,c;
2351: register NM m,new,prev;
2352: NM mr0;
2353: unsigned int *d;
2354: int n,td,i,len,d0,d1;
2355: ND p,r;
2356:
2357: p = nd_ps[index];
2358: len = nd_psl[index];
2359: n = NV(p); m = BDY(p);
1.14 ! noro 2360: d = DL(m0); td = TD(m0); c = CM(m0);
1.11 noro 2361:
2362: NEWNM(mr0);
1.14 ! noro 2363: c1 = CM(m); DMAR(c1,c,0,nd_mod,c2); CM(mr0) = c2;
! 2364: TD(mr0) = TD(m)+td; ndl_add(DL(m),d,DL(mr0));
1.11 noro 2365: prev = mr0; m = NEXT(m);
2366: len--;
2367:
2368: switch ( nd_wpd ) {
2369: case 1:
2370: d0 = d[0];
2371: while ( len-- ) {
1.14 ! noro 2372: c1 = CM(m); DMAR(c1,c,0,nd_mod,c2);
! 2373: NEWNM(new); CM(new) = c2;
! 2374: TD(new) = TD(m)+td; DL(new)[0] = DL(m)[0]+d0;
1.11 noro 2375: m = NEXT(m); NEXT(prev) = new; prev = new;
2376: }
2377: break;
2378: case 2:
2379: d0 = d[0]; d1 = d[1];
2380: while ( len-- ) {
1.14 ! noro 2381: c1 = CM(m); DMAR(c1,c,0,nd_mod,c2);
! 2382: NEWNM(new); CM(new) = c2;
! 2383: TD(new) = TD(m)+td;
! 2384: DL(new)[0] = DL(m)[0]+d0;
! 2385: DL(new)[1] = DL(m)[1]+d1;
1.11 noro 2386: m = NEXT(m); NEXT(prev) = new; prev = new;
2387: }
2388: break;
2389: default:
2390: while ( len-- ) {
1.14 ! noro 2391: c1 = CM(m); DMAR(c1,c,0,nd_mod,c2);
! 2392: NEWNM(new); CM(new) = c2;
! 2393: TD(new) = TD(m)+td; ndl_add(DL(m),d,DL(new));
1.11 noro 2394: m = NEXT(m); NEXT(prev) = new; prev = new;
2395: }
2396: break;
2397: }
2398:
2399: NEXT(prev) = 0;
2400: MKND(NV(p),mr0,r);
1.14 ! noro 2401: SG(r) = SG(p) + td;
1.11 noro 2402: return r;
1.3 noro 2403: }
1.4 noro 2404: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>