Annotation of OpenXM_contrib2/asir2000/engine/C.c, Revision 1.8
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.8 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/C.c,v 1.7 2001/05/28 08:22:01 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "inline.h"
52: #include "base.h"
53:
54: V up_var;
55:
56: /* binary has at least 32 leading 0 chars. */
57: void binaryton(binary,np)
58: char *binary;
59: N *np;
60: {
61: int i,w,len;
62: N n;
63: char buf[33];
64:
65: binary += strlen(binary)%32;
66: len = strlen(binary);
67: w = len/32; /* sufficient for holding binary */
68: n = NALLOC(w);
69: for ( i = 0; i < w; i++ ) {
70: strncpy(buf,binary+len-32*(i+1),32); buf[32] = 0;
71: n->b[i] = strtoul(buf,0,2);
72: }
73: for ( i = w-1; i >= 0 && !n->b[i]; i-- );
74: if ( i < 0 )
75: *np = 0;
76: else {
77: n->p = i+1;
78: *np = n;
79: }
80: }
81:
82: /* hex has at least 8 leading 0 chars. */
83: void hexton(hex,np)
84: char *hex;
85: N *np;
86: {
87: int i,w,len;
88: N n;
89: char buf[9];
90:
91: hex += strlen(hex)%8;
92: len = strlen(hex);
93: w = len/8; /* sufficient for holding hex */
94: n = NALLOC(w);
95: for ( i = 0; i < w; i++ ) {
96: strncpy(buf,hex+len-8*(i+1),8); buf[8] = 0;
97: n->b[i] = strtoul(buf,0,16);
98: }
99: for ( i = w-1; i >= 0 && !n->b[i]; i-- );
100: if ( i < 0 )
101: *np = 0;
102: else {
103: n->p = i+1;
104: *np = n;
105: }
106: }
107:
108: void ntobn(base,n,nrp)
109: int base;
110: N n,*nrp;
111: {
112: int i,d,plc;
113: unsigned int *c,*x,*w;
114: unsigned int r;
115: L m;
116: N nr;
117:
118: if ( !n ) {
119: *nrp = NULL;
120: return;
121: }
122:
123: d = PL(n);
124: w = BD(n);
125:
126: for ( i = 1, m = 1; m <= LBASE/(L)base; m *= base, i++ );
127:
128: c = (unsigned int *)W_ALLOC(d*i+1);
129: x = (unsigned int *)W_ALLOC(d+1);
130: for ( i = 0; i < d; i++ )
131: x[i] = w[i];
132: for ( plc = 0; d >= 1; plc++ ) {
133: for ( i = d - 1, r = 0; i >= 0; i-- ) {
134: DSAB((unsigned int)base,r,x[i],x[i],r)
135: }
136: c[plc] = r;
137: if ( !x[d-1] ) d--;
138: }
139:
140: *nrp = nr = NALLOC(plc); INITRC(nr);
141: PL(nr) = plc;
142: for ( i = 0; i < plc; i++ )
143: BD(nr)[i] = c[i];
144: }
145:
146: void bnton(base,n,nrp)
147: int base;
148: N n,*nrp;
149: {
150: unsigned int carry;
151: unsigned int *x,*w;
152: int i,j,d,plc;
153: N nr;
154:
155: if ( !n ) {
156: *nrp = 0;
157: return;
158: }
159:
160: d = PL(n);
161: w = BD(n);
162: x = (unsigned int *)W_ALLOC(d + 1);
163:
164: for ( plc = 0, i = d - 1; i >= 0; i-- ) {
165: for ( carry = w[i],j = 0; j < plc; j++ ) {
166: DMA(x[j],(unsigned int)base,carry,carry,x[j])
167: }
168: if ( carry ) x[plc++] = carry;
169: }
170: *nrp = nr = NALLOC(plc); INITRC(nr);
171: PL(nr) = plc;
172: for ( i = 0; i < plc; i++ )
173: BD(nr)[i] = x[i];
174: }
175:
176: void ptomp(m,p,pr)
177: int m;
178: P p;
179: P *pr;
180: {
181: DCP dc,dcr,dcr0;
182: Q q;
183: unsigned int a,b;
184: P t;
185: MQ s;
186:
187: if ( !p )
188: *pr = 0;
189: else if ( NUM(p) ) {
190: q = (Q)p;
191: a = rem(NM(q),m);
192: if ( a && (SGN(q) < 0) )
193: a = m-a;
194: b = !DN(q)?1:rem(DN(q),m);
195: if ( !b )
196: error("ptomp : denominator = 0");
197: a = dmar(a,invm(b,m),0,m); STOMQ(a,s); *pr = (P)s;
198: } else {
199: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
200: ptomp(m,COEF(dc),&t);
201: if ( t ) {
202: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
203: }
204: }
205: if ( !dcr0 )
206: *pr = 0;
207: else {
208: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
209: }
210: }
211: }
212:
213: void mptop(f,gp)
214: P f;
215: P *gp;
216: {
217: DCP dc,dcr,dcr0;
218: Q q;
219:
220: if ( !f )
221: *gp = 0;
222: else if ( NUM(f) )
223: STOQ(CONT((MQ)f),q),*gp = (P)q;
224: else {
225: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
226: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); mptop(COEF(dc),&COEF(dcr));
1.4 noro 227: }
228: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
229: }
230: }
231:
1.7 noro 232: void ptosfp(p,pr)
233: P p;
234: P *pr;
235: {
236: DCP dc,dcr,dcr0;
237: GFS a;
238: P t;
239:
240: if ( !p )
241: *pr = 0;
242: else if ( NUM(p) ) {
243: qtogfs((Q)p,&a); *pr = (P)a;
244: } else {
245: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
246: ptosfp(COEF(dc),&t);
247: if ( t ) {
248: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
249: }
250: }
251: if ( !dcr0 )
252: *pr = 0;
253: else {
254: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
255: }
256: }
257: }
258:
1.4 noro 259: void sfptop(f,gp)
260: P f;
261: P *gp;
262: {
263: DCP dc,dcr,dcr0;
264: Q q;
1.5 noro 265: MQ fq;
1.4 noro 266:
267: if ( !f )
268: *gp = 0;
269: else if ( NUM(f) ) {
1.5 noro 270: gfstomq((GFS)f,&fq);
271: STOQ(CONT(fq),q);
272: *gp = (P)q;
1.4 noro 273: } else {
274: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
275: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); sfptop(COEF(dc),&COEF(dcr));
1.1 noro 276: }
277: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
1.7 noro 278: }
279: }
280:
281: void sf_galois_action(p,e,pr)
282: P p;
283: Q e;
284: P *pr;
285: {
286: DCP dc,dcr,dcr0;
287: GFS a;
288: P t;
289:
290: if ( !p )
291: *pr = 0;
292: else if ( NUM(p) ) {
293: gfs_galois_action(p,e,&a); *pr = (P)a;
294: } else {
295: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
296: sf_galois_action(COEF(dc),e,&t);
297: if ( t ) {
298: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
299: }
300: }
301: if ( !dcr0 )
302: *pr = 0;
303: else {
304: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
305: }
1.1 noro 306: }
307: }
308:
309: void ptolmp(p,pr)
310: P p;
311: P *pr;
312: {
313: DCP dc,dcr,dcr0;
314: LM a;
315: P t;
316:
317: if ( !p )
318: *pr = 0;
319: else if ( NUM(p) ) {
320: qtolm((Q)p,&a); *pr = (P)a;
321: } else {
322: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
323: ptolmp(COEF(dc),&t);
324: if ( t ) {
325: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
326: }
327: }
328: if ( !dcr0 )
329: *pr = 0;
330: else {
331: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
332: }
333: }
334: }
335:
336: void lmptop(f,gp)
337: P f;
338: P *gp;
339: {
340: DCP dc,dcr,dcr0;
341: Q q;
342:
343: if ( !f )
344: *gp = 0;
345: else if ( NUM(f) ) {
346: NTOQ(((LM)f)->body,1,q); *gp = (P)q;
347: } else {
348: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
349: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); lmptop(COEF(dc),&COEF(dcr));
350: }
351: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
352: }
353: }
354:
355: void ptoum(m,f,wf)
356: int m;
357: P f;
358: UM wf;
359: {
360: unsigned int r;
361: int i;
362: DCP dc;
363:
364: for ( i = UDEG(f); i >= 0; i-- )
365: COEF(wf)[i] = 0;
366:
367: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
368: r = rem(NM((Q)COEF(dc)),m);
369: if ( r && (SGN((Q)COEF(dc)) < 0) )
370: r = m-r;
371: COEF(wf)[QTOS(DEG(dc))] = r;
372: }
373: degum(wf,UDEG(f));
374: }
375:
376: void umtop(v,w,f)
377: V v;
378: UM w;
379: P *f;
380: {
381: int *c;
382: DCP dc,dc0;
383: int i;
384: Q q;
385:
386: if ( DEG(w) < 0 )
387: *f = 0;
388: else if ( DEG(w) == 0 )
389: STOQ(COEF(w)[0],q), *f = (P)q;
390: else {
391: for ( i = DEG(w), c = COEF(w), dc0 = 0; i >= 0; i-- )
392: if ( c[i] ) {
393: NEXTDC(dc0,dc);
394: STOQ(i,DEG(dc));
395: STOQ(c[i],q), COEF(dc) = (P)q;
1.8 ! noro 396: }
! 397: NEXT(dc) = 0;
! 398: MKP(v,dc0,*f);
! 399: }
! 400: }
! 401:
! 402: void ptosfum(f,wf)
! 403: P f;
! 404: UM wf;
! 405: {
! 406: GFS c;
! 407: int i;
! 408: DCP dc;
! 409:
! 410: for ( i = UDEG(f); i >= 0; i-- )
! 411: COEF(wf)[i] = 0;
! 412:
! 413: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
! 414: c = (GFS)COEF(dc);
! 415: if ( c )
! 416: COEF(wf)[QTOS(DEG(dc))] = FTOIF(CONT(c));
! 417: }
! 418: degum(wf,UDEG(f));
! 419: }
! 420:
! 421: void sfumtop(v,w,f)
! 422: V v;
! 423: UM w;
! 424: P *f;
! 425: {
! 426: int *c;
! 427: DCP dc,dc0;
! 428: int i,t;
! 429: GFS q;
! 430:
! 431: if ( DEG(w) < 0 )
! 432: *f = 0;
! 433: else if ( DEG(w) == 0 ) {
! 434: t = COEF(w)[0];
! 435: t = IFTOF(t);
! 436: MKGFS(t,q);
! 437: *f = (P)q;
! 438: } else {
! 439: for ( i = DEG(w), c = COEF(w), dc0 = 0; i >= 0; i-- )
! 440: if ( c[i] ) {
! 441: NEXTDC(dc0,dc);
! 442: STOQ(i,DEG(dc));
! 443: t = COEF(w)[i];
! 444: t = IFTOF(t);
! 445: MKGFS(t,q);
! 446: COEF(dc) = (P)q;
1.1 noro 447: }
448: NEXT(dc) = 0;
449: MKP(v,dc0,*f);
450: }
451: }
452:
453: void ptoup(n,nr)
454: P n;
455: UP *nr;
456: {
457: DCP dc;
458: UP r;
459: int d;
460:
461: if ( !n )
462: *nr = 0;
463: else if ( OID(n) == O_N ) {
464: *nr = r = UPALLOC(0);
465: DEG(r) = 0; COEF(r)[0] = (Num)n;
466: } else {
467: d = UDEG(n);
468: up_var = VR(n);
469: *nr = r = UPALLOC(d); DEG(r) = d;
470: for ( dc = DC(n); dc; dc = NEXT(dc) ) {
471: COEF(r)[QTOS(DEG(dc))] = (Num)COEF(dc);
472: }
473: }
474: }
475:
476: void uptop(n,nr)
477: UP n;
478: P *nr;
479: {
480: int i;
481: DCP dc0,dc;
482:
483: if ( !n )
484: *nr = 0;
485: else if ( !DEG(n) )
486: *nr = (P)COEF(n)[0];
487: else {
488: for ( i = DEG(n), dc0 = 0; i >= 0; i-- )
489: if ( COEF(n)[i] ) {
490: NEXTDC(dc0,dc); STOQ(i,DEG(dc)); COEF(dc) = (P)COEF(n)[i];
491: }
492: if ( !up_var )
493: up_var = CO->v;
494: MKP(up_var,dc0,*nr);
495: }
496: }
497:
498: void ulmptoum(m,f,wf)
499: int m;
500: UP f;
501: UM wf;
502: {
503: int i,d;
504: LM *c;
505:
506: if ( !f )
507: wf->d = -1;
508: else {
509: wf->d = d = f->d;
510: c = (LM *)f->c;
511: for ( i = 0, d = f->d; i <= d; i++ )
512: COEF(wf)[i] = rem(c[i]->body,m);
513: }
514: }
515:
516: void objtobobj(base,p,rp)
517: int base;
518: Obj p;
519: Obj *rp;
520: {
521: if ( !p )
522: *rp = 0;
523: else
524: switch ( OID(p) ) {
525: case O_N:
526: numtobnum(base,(Num)p,(Num *)rp); break;
527: case O_P:
528: ptobp(base,(P)p,(P *)rp); break;
529: case O_LIST:
530: listtoblist(base,(LIST)p,(LIST *)rp); break;
531: case O_VECT:
532: vecttobvect(base,(VECT)p,(VECT *)rp); break;
533: case O_MAT:
534: mattobmat(base,(MAT)p,(MAT *)rp); break;
535: case O_STR:
536: *rp = p; break;
537: case O_COMP: default:
538: error("objtobobj : not implemented"); break;
539: }
540: }
541:
542: void bobjtoobj(base,p,rp)
543: int base;
544: Obj p;
545: Obj *rp;
546: {
547: if ( !p )
548: *rp = 0;
549: else
550: switch ( OID(p) ) {
551: case O_N:
552: bnumtonum(base,(Num)p,(Num *)rp); break;
553: case O_P:
554: bptop(base,(P)p,(P *)rp); break;
555: case O_LIST:
556: blisttolist(base,(LIST)p,(LIST *)rp); break;
557: case O_VECT:
558: bvecttovect(base,(VECT)p,(VECT *)rp); break;
559: case O_MAT:
560: bmattomat(base,(MAT)p,(MAT *)rp); break;
561: case O_STR:
562: *rp = p; break;
563: case O_COMP: default:
564: error("bobjtoobj : not implemented"); break;
565: }
566: }
567:
568: void numtobnum(base,p,rp)
569: int base;
570: Num p;
571: Num *rp;
572: {
573: N nm,dn,body;
574: Q q;
575: LM l;
576:
577: if ( !p )
578: *rp = 0;
579: else
580: switch ( NID(p) ) {
581: case N_Q:
582: ntobn(base,NM((Q)p),&nm);
583: if ( DN((Q)p) ) {
584: ntobn(base,DN((Q)p),&dn);
585: NDTOQ(nm,dn,SGN((Q)p),q);
586: } else
587: NTOQ(nm,SGN((Q)p),q);
588: *rp = (Num)q;
589: break;
590: case N_R:
591: *rp = p; break;
592: case N_LM:
593: ntobn(base,((LM)p)->body,&body);
594: MKLM(body,l); *rp = (Num)l;
595: break;
596: default:
597: error("numtobnum : not implemented"); break;
598: }
599: }
600:
601: void bnumtonum(base,p,rp)
602: int base;
603: Num p;
604: Num *rp;
605: {
606: N nm,dn,body;
607: Q q;
608: LM l;
609:
610: if ( !p )
611: *rp = 0;
612: else
613: switch ( NID(p) ) {
614: case N_Q:
615: bnton(base,NM((Q)p),&nm);
616: if ( DN((Q)p) ) {
617: bnton(base,DN((Q)p),&dn);
618: NDTOQ(nm,dn,SGN((Q)p),q);
619: } else
620: NTOQ(nm,SGN((Q)p),q);
621: *rp = (Num)q;
622: break;
623: case N_R:
624: *rp = p; break;
625: case N_LM:
626: bnton(base,((LM)p)->body,&body);
627: MKLM(body,l); *rp = (Num)l;
628: break;
629: default:
630: error("bnumtonum : not implemented"); break;
631: }
632: }
633:
634: void ptobp(base,p,rp)
635: int base;
636: P p;
637: P *rp;
638: {
639: DCP dcr0,dcr,dc;
640:
641: if ( !p )
642: *rp = p;
643: else {
644: for ( dcr0 = 0, dc = DC(p); dc; dc = NEXT(dc) ) {
645: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc);
646: objtobobj(base,(Obj)COEF(dc),(Obj *)&COEF(dcr));
647: }
648: NEXT(dcr) = 0;
649: MKP(VR(p),dcr0,*rp);
650: }
651: }
652:
653: void bptop(base,p,rp)
654: int base;
655: P p;
656: P *rp;
657: {
658: DCP dcr0,dcr,dc;
659:
660: if ( !p )
661: *rp = p;
662: else {
663: for ( dcr0 = 0, dc = DC(p); dc; dc = NEXT(dc) ) {
664: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc);
665: bobjtoobj(base,(Obj)COEF(dc),(Obj *)&COEF(dcr));
666: }
667: NEXT(dcr) = 0;
668: MKP(VR(p),dcr0,*rp);
669: }
670: }
671:
672: void listtoblist(base,p,rp)
673: int base;
674: LIST p;
675: LIST *rp;
676: {
677: NODE nr0,nr,n;
678:
679: if ( !p )
680: *rp = p;
681: else {
682: for ( nr0 = 0, n = BDY(p); n; n = NEXT(n) ) {
683: NEXTNODE(nr0,nr);
684: objtobobj(base,BDY(n),(Obj *)&BDY(nr));
685: }
686: NEXT(nr) = 0;
687: MKLIST(*rp,nr0);
688: }
689: }
690:
691: void blisttolist(base,p,rp)
692: int base;
693: LIST p;
694: LIST *rp;
695: {
696: NODE nr0,nr,n;
697:
698: if ( !p )
699: *rp = p;
700: else {
701: for ( nr0 = 0, n = BDY(p); n; n = NEXT(n) ) {
702: NEXTNODE(nr0,nr);
703: bobjtoobj(base,BDY(n),(Obj *)&BDY(nr));
704: }
705: NEXT(nr) = 0;
706: MKLIST(*rp,nr0);
707: }
708: }
709:
710: void vecttobvect(base,p,rp)
711: int base;
712: VECT p;
713: VECT *rp;
714: {
715: int i,l;
716: VECT r;
717:
718: if ( !p )
719: *rp = p;
720: else {
721: l = p->len;
722: MKVECT(r,l); *rp = r;
723: for ( i = 0; i < l; i++ )
724: objtobobj(base,p->body[i],(Obj *)&r->body[i]);
725: }
726: }
727:
728: void bvecttovect(base,p,rp)
729: int base;
730: VECT p;
731: VECT *rp;
732: {
733: int i,l;
734: VECT r;
735:
736: if ( !p )
737: *rp = p;
738: else {
739: l = p->len;
740: MKVECT(r,l); *rp = r;
741: for ( i = 0; i < l; i++ )
742: bobjtoobj(base,p->body[i],(Obj *)&r->body[i]);
743: }
744: }
745:
746: void mattobmat(base,p,rp)
747: int base;
748: MAT p;
749: MAT *rp;
750: {
751: int row,col,i,j;
752: MAT r;
753:
754: if ( !p )
755: *rp = p;
756: else {
757: row = p->row; col = p->col;
758: MKMAT(r,row,col); *rp = r;
759: for ( i = 0; i < row; i++ )
760: for ( j = 0; i < col; j++ )
761: objtobobj(base,p->body[i][j],(Obj *)&r->body[i][j]);
762: }
763: }
764:
765: void bmattomat(base,p,rp)
766: int base;
767: MAT p;
768: MAT *rp;
769: {
770: int row,col,i,j;
771: MAT r;
772:
773: if ( !p )
774: *rp = p;
775: else {
776: row = p->row; col = p->col;
777: MKMAT(r,row,col); *rp = r;
778: for ( i = 0; i < row; i++ )
779: for ( j = 0; i < col; j++ )
780: bobjtoobj(base,p->body[i][j],(Obj *)&r->body[i][j]);
781: }
782: }
783:
784: void n32ton27(g,rp)
785: N g;
786: N *rp;
787: {
788: int i,j,k,l,r,bits,words;
789: unsigned int t;
790: unsigned int *a,*b;
791: N z;
792:
793: l = PL(g); a = BD(g);
794: for ( i = 31, t = a[l-1]; !(t&(1<<i)); i-- );
795: bits = (l-1)*32+i+1; words = (bits+26)/27;
796: *rp = z = NALLOC(words); PL(z) = words;
797: bzero((char *)BD(z),words*sizeof(unsigned int));
798: for ( j = 0, b = BD(z); j < words; j++ ) {
799: k = (27*j)/32; r = (27*j)%32;
800: if ( r > 5 )
801: b[j] = (a[k]>>r)|(k==(l-1)?0:((a[k+1]&((1<<(r-5))-1))<<(32-r)));
802: else
803: b[j] = (a[k]>>r)&((1<<27)-1);
804: }
805: if ( !(r = bits%27) )
806: r = 27;
807: b[words-1] &= ((1<<r)-1);
808: }
809:
810: void n27ton32(a,rp)
811: N a;
812: N *rp;
813: {
814: int i,j,k,l,r,bits,words;
815: unsigned int t;
816: unsigned int *b,*c;
817: N z;
818:
819: l = PL(a); b = BD(a);
820: for ( i = 26, t = b[l-1]; !(t&(1<<i)); i-- );
821: bits = (l-1)*27+i+1; words = (bits+31)/32;
822: *rp = z = NALLOC(words); PL(z) = words;
823: bzero((char *)BD(z),words*sizeof(unsigned int));
824: for ( j = 0, c = BD(z); j < l; j++ ) {
825: k = (27*j)/32; r = (27*j)%32;
826: if ( r > 5 ) {
827: c[k] |= (b[j]&((1<<(32-r))-1))<<r;
828: if ( k+1 < words )
829: c[k+1] = (b[j]>>(32-r));
830: } else
831: c[k] |= (b[j]<<r);
832: }
833: }
834:
835: void mptoum(p,pr)
836: P p;
837: UM pr;
838: {
839: DCP dc;
840:
841: if ( !p )
842: DEG(pr) = -1;
843: else if ( NUM(p) ) {
844: DEG(pr) = 0; COEF(pr)[0] = CONT((MQ)p);
845: } else {
846: bzero((char *)pr,(int)((UDEG(p)+2)*sizeof(int)));
847: for ( dc = DC(p); dc; dc = NEXT(dc) )
848: COEF(pr)[QTOS(DEG(dc))] = CONT((MQ)COEF(dc));
849: degum(pr,UDEG(p));
850: }
851: }
852:
853: void umtomp(v,p,pr)
854: V v;
855: UM p;
856: P *pr;
857: {
858: DCP dc,dc0;
859: int i;
860: MQ q;
861:
862: if ( !p || (DEG(p) < 0) )
863: *pr = 0;
864: else if ( !DEG(p) )
865: STOMQ(COEF(p)[0],q), *pr = (P)q;
866: else {
867: for ( dc0 = 0, i = DEG(p); i >= 0; i-- )
868: if ( COEF(p)[i] ) {
869: NEXTDC(dc0,dc); STOQ(i,DEG(dc));
870: STOMQ(COEF(p)[i],q), COEF(dc) = (P)q;
871: }
872: NEXT(dc) = 0; MKP(v,dc0,*pr);
873: }
1.6 noro 874: }
875:
876: /* f(p) -> f(x) */
877:
878: void enc_to_p(p,a,v,pr)
879: int p,a;
880: V v;
881: P *pr;
882: {
883: DCP dc,dct;
884: int i,c;
885: Q dq,cq;
886:
887: dc = 0;
888: for ( i = 0; a; i++, a /= p ) {
889: c = a%p;
890: if ( c ) {
891: STOQ(i,dq); STOQ(c,cq);
892: NEWDC(dct); DEG(dct) = dq; COEF(dct) = (P)cq;
893: NEXT(dct) = dc; dc = dct;
894: }
895: }
896: MKP(v,dc,*pr);
1.1 noro 897: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>