=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/engine/N.c,v retrieving revision 1.1 retrieving revision 1.10 diff -u -p -r1.1 -r1.10 --- OpenXM_contrib2/asir2000/engine/N.c 1999/12/03 07:39:08 1.1 +++ OpenXM_contrib2/asir2000/engine/N.c 2009/03/02 16:28:30 1.10 @@ -1,15 +1,57 @@ -/* $OpenXM: OpenXM/src/asir99/engine/N.c,v 1.1.1.1 1999/11/10 08:12:26 noro Exp $ */ +/* + * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED + * All rights reserved. + * + * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited, + * non-exclusive and royalty-free license to use, copy, modify and + * redistribute, solely for non-commercial and non-profit purposes, the + * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and + * conditions of this Agreement. For the avoidance of doubt, you acquire + * only a limited right to use the SOFTWARE hereunder, and FLL or any + * third party developer retains all rights, including but not limited to + * copyrights, in and to the SOFTWARE. + * + * (1) FLL does not grant you a license in any way for commercial + * purposes. You may use the SOFTWARE only for non-commercial and + * non-profit purposes only, such as academic, research and internal + * business use. + * (2) The SOFTWARE is protected by the Copyright Law of Japan and + * international copyright treaties. If you make copies of the SOFTWARE, + * with or without modification, as permitted hereunder, you shall affix + * to all such copies of the SOFTWARE the above copyright notice. + * (3) An explicit reference to this SOFTWARE and its copyright owner + * 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@sec.flab.fujitsu.co.jp of the detailed specification + * for such modification or the source code of the modified part of the + * SOFTWARE. + * + * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL + * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND + * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES' + * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY + * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY. + * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT, + * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL + * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES + * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES + * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY + * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF + * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART + * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY + * 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.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; @@ -29,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 @@ -56,6 +98,7 @@ N n1,n2,*nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -73,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; @@ -90,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; @@ -134,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 @@ -161,6 +204,7 @@ N n1,n2,*nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -178,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++; @@ -197,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; @@ -217,7 +261,7 @@ N n1,n2,nr; } mr = BD(nr); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -244,6 +288,7 @@ N n1,n2,nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -261,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; @@ -278,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; @@ -321,7 +366,7 @@ N n1,n2,nr; } mr = BD(nr); -#if defined(VISUAL) +#if defined(_M_IX86) __asm { push esi push edi @@ -348,6 +393,7 @@ N n1,n2,nr; } #else asm volatile("\ + pushl %%ebx;\ movl %1,%%esi;\ movl %2,%%edi;\ movl %3,%%ebx;\ @@ -365,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++; @@ -385,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; @@ -427,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; @@ -486,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; @@ -526,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; @@ -587,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; @@ -616,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; @@ -635,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; @@ -685,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 { @@ -694,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; @@ -722,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; @@ -793,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; @@ -863,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; @@ -890,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; @@ -912,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; }