=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/engine/N.c,v retrieving revision 1.2 retrieving revision 1.10 diff -u -p -r1.2 -r1.10 --- OpenXM_contrib2/asir2000/engine/N.c 2000/08/21 08:31:25 1.2 +++ OpenXM_contrib2/asir2000/engine/N.c 2009/03/02 16:28:30 1.10 @@ -23,7 +23,7 @@ * shall be made on your publication or presentation in any form of the * results obtained by use of the SOFTWARE. * (4) In the event that you modify the SOFTWARE, you shall notify FLL by - * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification + * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification * for such modification or the source code of the modified part of the * SOFTWARE. * @@ -45,19 +45,13 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/engine/N.c,v 1.1.1.1 1999/12/03 07:39:08 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/engine/N.c,v 1.9 2007/09/15 10:17:08 noro Exp $ */ #include "ca.h" #include "base.h" -void addn(); -int subn(); -void _bshiftn(); -void dupn(); - -#if defined(VISUAL) || defined(i386) -void addn(n1,n2,nr) -N n1,n2,*nr; +#if defined(_M_IX86) || defined(i386) +void addn(N n1,N n2,N *nr) { unsigned int *m1,*m2,*mr; unsigned int c; @@ -77,7 +71,7 @@ N n1,n2,*nr; } *nr = r = NALLOC(d1 + 1); INITRC(r); mr = BD(r); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -104,6 +98,7 @@ N n1,n2,*nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -121,10 +116,11 @@ N n1,n2,*nr; jnz Lstart_addn;\ movl $0,%%eax;\ adcl %%eax,%%eax;\ - movl %%eax,%0"\ + movl %%eax,%0;\ + popl %%ebx"\ :"=m"(c)\ :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ - :"eax","ebx","ecx","edx","esi","edi"); + :"eax","ecx","edx","esi","edi"); #endif for ( i = d2, m1 += d2, mr += d2; (i < d1) && c ; i++ ) { tmp = *m1++ + c; @@ -138,8 +134,7 @@ N n1,n2,*nr; } } -int subn(n1,n2,nr) -N n1,n2,*nr; +int subn(N n1,N n2,N *nr) { N r; unsigned int *m1,*m2,*mr,br; @@ -182,7 +177,7 @@ N n1,n2,*nr; } *nr = r = NALLOC(d1); INITRC(r); mr = BD(r); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -209,6 +204,7 @@ N n1,n2,*nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -226,10 +222,11 @@ N n1,n2,*nr; jnz Lstart_subn;\ movl $0,%%eax;\ adcl %%eax,%%eax;\ - movl %%eax,%0"\ + movl %%eax,%0;\ + popl %%ebx"\ :"=m"(br)\ :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ - :"eax","ebx","ecx","edx","esi","edi"); + :"eax","ecx","edx","esi","edi"); #endif for ( i = d2, m1 += d2, mr += d2; (i < d1) && br; i++ ) { t = *m1++; @@ -245,8 +242,7 @@ N n1,n2,*nr; } } -void _addn(n1,n2,nr) -N n1,n2,nr; +void _addn(N n1,N n2,N nr) { unsigned int *m1,*m2,*mr; unsigned int c; @@ -265,7 +261,7 @@ N n1,n2,nr; } mr = BD(nr); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -292,6 +288,7 @@ N n1,n2,nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -309,10 +306,11 @@ N n1,n2,nr; jnz Lstart__addn;\ movl $0,%%eax;\ adcl %%eax,%%eax;\ - movl %%eax,%0"\ + movl %%eax,%0;\ + popl %%ebx"\ :"=m"(c)\ :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ - :"eax","ebx","ecx","edx","esi","edi"); + :"eax","ecx","edx","esi","edi"); #endif for ( i = d2, m1 += d2, mr += d2; (i < d1) && c ; i++ ) { tmp = *m1++ + c; @@ -326,8 +324,7 @@ N n1,n2,nr; } } -int _subn(n1,n2,nr) -N n1,n2,nr; +int _subn(N n1,N n2,N nr) { unsigned int *m1,*m2,*mr,br; unsigned int tmp,t; @@ -369,7 +366,7 @@ N n1,n2,nr; } mr = BD(nr); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -396,6 +393,7 @@ N n1,n2,nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -413,10 +411,11 @@ N n1,n2,nr; jnz Lstart__subn;\ movl $0,%%eax;\ adcl %%eax,%%eax;\ - movl %%eax,%0"\ + movl %%eax,%0;\ + popl %%ebx"\ :"=m"(br)\ :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ - :"eax","ebx","ecx","edx","esi","edi"); + :"eax","ecx","edx","esi","edi"); #endif for ( i = d2, m1 += d2, mr += d2; (i < d1) && br; i++ ) { t = *m1++; @@ -433,8 +432,7 @@ N n1,n2,nr; } #else -void addn(n1,n2,nr) -N n1,n2,*nr; +void addn(N n1,N n2,N *nr) { unsigned int *m1,*m2,*mr,i,c; N r; @@ -475,8 +473,7 @@ N n1,n2,*nr; } } -int subn(n1,n2,nr) -N n1,n2,*nr; +int subn(N n1,N n2,N *nr) { N r; unsigned int *m1,*m2,*mr,i,br; @@ -534,8 +531,7 @@ N n1,n2,*nr; } } -void _addn(n1,n2,nr) -N n1,n2,nr; +void _addn(N n1,N n2,N nr) { unsigned int *m1,*m2,*mr,i,c; int d1,d2; @@ -574,8 +570,7 @@ N n1,n2,nr; } } -int _subn(n1,n2,nr) -N n1,n2,nr; +int _subn(N n1,N n2,N nr) { N r; unsigned int *m1,*m2,*mr,i,br; @@ -635,11 +630,7 @@ N n1,n2,nr; /* a2 += a1; n2 >= n1 */ -void addarray_to(a1,n1,a2,n2) -unsigned int *a1; -int n1; -unsigned int *a2; -int n2; +void addarray_to(unsigned int *a1,int n1,unsigned int *a2,int n2) { int i; unsigned int c,tmp; @@ -664,9 +655,7 @@ int n2; *a2 = c; } -void pwrn(n,e,nr) -N n,*nr; -int e; +void pwrn(N n,int e,N *nr) { N nw,nw1; @@ -683,33 +672,33 @@ int e; FREEN(nw); } + + extern int igcd_algorithm; -void gcdEuclidn(); +void gcdEuclidn(), gcdn_HMEXT(); -void gcdn(n1,n2,nr) -N n1,n2,*nr; +void lcmn(N n1,N n2,N *nr) { - N m1,m2,g; + N g,t; + gcdn(n1,n2,&g); + divsn(n1,g,&t); + muln(t,n2,nr); +} + +void gcdn(N n1,N n2,N *nr) +{ if ( !igcd_algorithm ) gcdEuclidn(n1,n2,nr); else { - if ( !n1 ) - *nr = n2; - else if ( !n2 ) - *nr = n1; - else { - n32ton27(n1,&m1); - n32ton27(n2,&m2); - gcdBinary_27n(m1,m2,&g); - n27ton32(g,nr); - } + gcdn_HMEXT(n1,n2,nr); } } -void gcdEuclidn(n1,n2,nr) -N n1,n2,*nr; +#include "Ngcd.c" + +void gcdEuclidn(N n1,N n2,N *nr) { N m1,m2,q,r; unsigned int i1,i2,ir; @@ -733,7 +722,8 @@ N n1,n2,*nr; m1 = m2; m2 = r; } } - for ( i1 = BD(m1)[0], i2 = BD(m2)[0]; ir = i1 % i2; i1 = i2, i2 = ir ); + for ( i1 = BD(m1)[0], i2 = BD(m2)[0]; ir = i1 % i2; i2 = ir ) + i1 = i2; if ( i2 == 1 ) COPY(ONEN,*nr); else { @@ -742,8 +732,7 @@ N n1,n2,*nr; } } -int cmpn(n1,n2) -N n1,n2; +int cmpn(N n1,N n2) { int i; unsigned int *m1,*m2; @@ -770,10 +759,7 @@ N n1,n2; } } -void bshiftn(n,b,r) -N n; -int b; -N *r; +void bshiftn(N n,int b,N *r) { int w,l,nl,i,j; N z; @@ -841,10 +827,7 @@ N *r; } #if 0 -void _bshiftn(n,b,z) -N n; -int b; -N z; +void _bshiftn(N n,int b,N z) { int w,l,nl,i,j; unsigned int msw; @@ -911,10 +894,7 @@ N z; } #endif -void shiftn(n,w,r) -N n; -int w; -N *r; +void shiftn(N n,int w,N *r) { int l,nl; N z; @@ -938,9 +918,7 @@ N *r; } } -void randomn(bits,r) -int bits; -N *r; +void randomn(int bits,N *r) { int l,i; unsigned int *tb; @@ -960,21 +938,23 @@ N *r; PL(t) = i+1; } -void freen(n) -N n; +void freen(N n) { if ( n && (n != ONEN) ) free(n); } -int n_bits(n) -N n; +/* accepts Z */ +int n_bits(N n) { - unsigned int l,i,t; + unsigned int i,t; + int l; if ( !n ) return 0; - l = PL(n); t = BD(n)[l-1]; + l = PL(n); + if ( l < 0 ) l = -l; + t = BD(n)[l-1]; for ( i = 0; t; t>>=1, i++); return i + (l-1)*BSH; }