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