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