Annotation of OpenXM_contrib2/asir2018/asm/ddM.c, Revision 1.2
1.1 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
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
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.2 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/asm/ddM.c,v 1.1 2018/09/19 05:45:05 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "base.h"
52: #include "inline.h"
53:
54: /*
55: * mod is declared as 'int', because several xxxum functions contains signed
56: * integer addition/subtraction. So mod should be less than 2^31.
57: */
58:
59: void mulum(int mod,UM p1,UM p2,UM pr)
60: {
61: int *pc1,*pcr;
62: int *c1,*c2,*cr;
63: unsigned int mul;
64: int i,j,d1,d2;
65:
66: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
67: DEG(pr) = -1;
68: return;
69: }
70: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
71: bzero((char *)cr,(int)((d1+d2+1)*sizeof(int)));
72: for ( i = 0; i <= d2; i++, cr++ )
1.2 ! noro 73: if ( (mul = *c2++) != 0 )
1.1 noro 74: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ ) {
75: DMAR(*pc1,mul,*pcr,mod,*pcr)
76: }
77: DEG(pr) = d1 + d2;
78: }
79:
80: void mulsum(int mod,UM p,int n,UM pr)
81: {
82: int *sp,*dp;
83: int i;
84:
85: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
86: i >= 0; i--, dp--, sp-- ) {
87: DMAR(*sp,n,0,mod,*dp)
88: }
89: }
90:
91: int divum(int mod,UM p1,UM p2,UM pq)
92: {
93: int *pc1,*pct;
94: int *c1,*c2,*ct;
95: unsigned int inv,hd,tmp;
96: int i,j, d1,d2,dd;
97:
98: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
99: DEG(pq) = -1;
100: return d1;
101: }
102: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
103: if ( ( hd = c2[d2] ) != 1 ) {
104: inv = invm(hd,mod);
105: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- ) {
106: DMAR(*pc1,inv,0,mod,*pc1)
107: }
108: } else
109: inv = 1;
110: for ( i = dd, ct = c1+d1; i >= 0; i-- )
1.2 ! noro 111: if ( (tmp = *ct--) != 0 ) {
1.1 noro 112: tmp = mod - tmp;
113: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- ) {
114: DMAR(*pc1,tmp,*pct,mod,*pct)
115: }
116: }
117: if ( inv != 1 ) {
118: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ ) {
119: DMAR(*pc1,inv,0,mod,*pc1)
120: }
121: for ( pc1 = c2, pct = c2+d2, inv = hd; pc1 <= pct; pc1++ ) {
122: DMAR(*pc1,inv,0,mod,*pc1)
123: }
124: }
125: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
126: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
127: *pct-- = *pc1--;
128: return i;
129: }
130:
131: void diffum(int mod,UM f,UM fd)
132: {
133: int *dp,*sp;
134: int i;
135:
136: for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i;
137: i >= 1; i--, dp--, sp-- ) {
138: DMAR(*sp,i,0,mod,*dp)
139: }
140: degum(fd,DEG(f) - 1);
141: }
142:
143: unsigned int pwrm(int mod,int a,int n)
144: {
145: unsigned int s,t;
146:
147: if ( !n )
148: return 1;
149: else if ( n == 1 )
150: return a;
151: else {
152: t = pwrm(mod,a,n/2);
153: DMAR(t,t,0,mod,s)
154: if ( n % 2 ) {
155: DMAR(s,a,0,mod,t)
156: return t;
157: } else
158: return s;
159: }
160: }
161:
162: unsigned int invm(unsigned int s,int mod)
163: {
164: unsigned int r,a2,q;
165: unsigned int f1,f2,a1;
166:
167: for ( f1 = s, f2 = mod, a1 = 1, a2 = 0; ; ) {
168: q = f1/f2; r = f1 - f2*q; f1 = f2;
169: if ( !(f2 = r) )
170: break;
171: DMAR(a2,q,0,mod,r)
172: /* r = ( a1 - r + mod ) % mod; */
173: if ( a1 >= r )
174: r = a1 - r;
175: else {
176: r = (mod - r) + a1;
177: }
178: a1 = a2; a2 = r;
179: }
180: /* return( ( a2 >= 0 ? a2 : a2 + mod ) ); */
181: return a2;
182: }
183:
184: #if !defined(sparc) || defined(__sparcv9)
185: void addpadic(int mod,int n,unsigned int *n1,unsigned int *n2)
186: {
187: unsigned int carry,tmp;
188: int i;
189:
190: for ( i = 0, carry = 0; i < n; i++ ) {
191: tmp = *n1++ + *n2 + carry;
192: DQR(tmp,mod,carry,*n2++)
193: /*
194: carry = tmp / mod;
195: *n2++ = tmp - ( carry * mod );
196: */
197: }
198: }
199: #endif
200:
201: void mulpadic(int mod,int n,unsigned int *n1,unsigned int *n2,unsigned int *nr)
202: {
203: unsigned int *pn1,*pnr;
204: unsigned int carry,mul;
205: int i,j;
206:
207: bzero((char *)nr,(int)(n*sizeof(int)));
208: for ( j = 0; j < n; j++, n2++, nr++ )
1.2 ! noro 209: if ( (mul = *n2) != 0 )
1.1 noro 210: for ( i = j, carry = 0, pn1 = n1, pnr = nr;
211: i < n; i++, pn1++, pnr++ ) {
212: carry += *pnr;
213: DMAB(mod,*pn1,mul,carry,carry,*pnr)
214: }
215: }
216:
217: extern int up_kara_mag;
218:
219: void kmulum(int mod,UM n1,UM n2,UM nr)
220: {
221: UM n,t,s,m,carry;
222: int d,d1,d2,len,i,l;
223: unsigned int *r,*r0;
224:
225: if ( !n1 || !n2 ) {
226: nr->d = -1; return;
227: }
228: d1 = DEG(n1)+1; d2 = DEG(n2)+1;
229: if ( (d1 < up_kara_mag) || (d2 < up_kara_mag) ) {
230: mulum(mod,n1,n2,nr); return;
231: }
232: if ( d1 < d2 ) {
233: n = n1; n1 = n2; n2 = n;
234: d = d1; d1 = d2; d2 = d;
235: }
236: if ( d2 > (d1+1)/2 ) {
237: kmulummain(mod,n1,n2,nr); return;
238: }
239: d = (d1/d2)+((d1%d2)!=0);
240: len = (d+1)*d2;
241: r0 = (unsigned int *)ALLOCA(len*sizeof(int));
242: bzero((char *)r0,len*sizeof(int));
243: m = W_UMALLOC(d2+1);
244: carry = W_UMALLOC(d2+1);
245: t = W_UMALLOC(d1+d2+1);
246: s = W_UMALLOC(d1+d2+1);
247: for ( DEG(carry) = -1, i = 0, r = r0; i < d; i++, r += d2 ) {
248: extractum(n1,i*d2,d2,m);
249: if ( m ) {
250: kmulum(mod,m,n2,t);
251: addum(mod,t,carry,s);
1.2 ! noro 252: c_copyum(s,d2,(int *)r);
1.1 noro 253: extractum(s,d2,d2,carry);
254: } else {
1.2 ! noro 255: c_copyum(carry,d2,(int *)r);
1.1 noro 256: carry = 0;
257: }
258: }
1.2 ! noro 259: c_copyum(carry,d2,(int *)r);
1.1 noro 260: for ( l = len - 1; !r0[l]; l-- );
261: l++;
262: DEG(nr) = l-1;
263: bcopy((char *)r0,(char *)COEF(nr),l*sizeof(int));
264: }
265:
266: void ksquareum(int mod,UM n1,UM nr)
267: {
268: int d1;
269:
270: if ( !n1 ) {
271: nr->d = -1; return;
272: }
273: d1 = DEG(n1)+1;
274: if ( (d1 < up_kara_mag) ) {
275: pwrum(mod,n1,2,nr); return;
276: }
277: ksquareummain(mod,n1,nr);
278: }
279:
280: void extractum(UM n,int index,int len,UM nr)
281: {
282: int *m;
283: int l;
284:
285: if ( !n ) {
286: nr->d = -1; return;
287: }
288: m = COEF(n)+index;
289: if ( (l = (DEG(n)+1)-index) >= len ) {
290: for ( l = len - 1; (l >= 0) && !m[l]; l-- );
291: l++;
292: }
293: if ( l <= 0 ) {
294: nr->d = -1; return;
295: } else {
296: DEG(nr) = l-1;
297: bcopy((char *)m,(char *)COEF(nr),l*sizeof(Q));
298: }
299: }
300:
301: void copyum(UM n1,UM n2)
302: {
303: n2->d = n1->d;
304: bcopy((char *)n1->c,(char *)n2->c,(n1->d+1)*sizeof(int));
305: }
306:
307: void c_copyum(UM n,int len,int *p)
308: {
309: if ( n )
310: bcopy((char *)COEF(n),(char *)p,MIN((DEG(n)+1),len)*sizeof(int));
311: }
312:
313: void kmulummain(int mod,UM n1,UM n2,UM nr)
314: {
315: int d1,d2,h,len;
316: UM n1lo,n1hi,n2lo,n2hi,hi,lo,mid1,mid2,mid,s1,s2,t1,t2;
317:
318: d1 = DEG(n1)+1; d2 = DEG(n2)+1; h = (d1+1)/2;
319: n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1);
320: n2lo = W_UMALLOC(d2+1); n2hi = W_UMALLOC(d2+1);
321: lo = W_UMALLOC(d1+d2+1); hi = W_UMALLOC(d1+d2+1);
322: mid1 = W_UMALLOC(d1+d2+1); mid2 = W_UMALLOC(d1+d2+1);
323: mid = W_UMALLOC(d1+d2+1);
324: s1 = W_UMALLOC(d1+d2+1); s2 = W_UMALLOC(d1+d2+1);
325: extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi);
326: extractum(n2,0,h,n2lo); extractum(n2,h,d2-h,n2hi);
327: kmulum(mod,n1hi,n2hi,hi); kmulum(mod,n1lo,n2lo,lo);
328: len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1;
329: bzero((char *)COEF(t1),len*sizeof(int));
330: if ( lo )
331: bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int));
332: if ( hi )
333: bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int));
334:
335: addum(mod,hi,lo,mid1);
336: subum(mod,n1hi,n1lo,s1); subum(mod,n2lo,n2hi,s2); kmulum(mod,s1,s2,mid2);
337: addum(mod,mid1,mid2,mid);
338: if ( mid ) {
339: len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1;
340: bzero((char *)COEF(t2),len*sizeof(int));
341: bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int));
342: addum(mod,t1,t2,nr);
343: } else
344: copyum(t1,nr);
345: }
346:
347: void ksquareummain(int mod,UM n1,UM nr)
348: {
349: int d1,h,len;
350: UM n1lo,n1hi,hi,lo,mid1,mid2,mid,s1,t1,t2;
351:
352: d1 = DEG(n1)+1; h = (d1+1)/2;
353: n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1);
354: lo = W_UMALLOC(2*d1+1); hi = W_UMALLOC(2*d1+1);
355: mid1 = W_UMALLOC(2*d1+1); mid2 = W_UMALLOC(2*d1+1);
356: mid = W_UMALLOC(2*d1+1);
357: s1 = W_UMALLOC(2*d1+1);
358: extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi);
359: ksquareum(mod,n1hi,hi); ksquareum(mod,n1lo,lo);
360: len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1;
361: bzero((char *)COEF(t1),len*sizeof(int));
362: if ( lo )
363: bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int));
364: if ( hi )
365: bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int));
366:
367: addum(mod,hi,lo,mid1);
368: subum(mod,n1hi,n1lo,s1); ksquareum(mod,s1,mid2);
369: subum(mod,mid1,mid2,mid);
370: if ( mid ) {
371: len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1;
372: bzero((char *)COEF(t2),len*sizeof(int));
373: bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int));
374: addum(mod,t1,t2,nr);
375: } else
376: copyum(t1,nr);
377: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>