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