[BACK]Return to ddM.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / asm

Annotation of OpenXM_contrib2/asir2000/asm/ddM.c, Revision 1.6

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.6     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/asm/ddM.c,v 1.5 2001/10/09 01:36:02 noro Exp $
1.2       noro       49: */
1.1       noro       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:
1.5       noro       59: void mulum(int mod,UM p1,UM p2,UM pr)
1.1       noro       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++ )
                     73:                if ( mul = *c2++ )
                     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:
1.5       noro       80: void mulsum(int mod,UM p,int n,UM pr)
1.1       noro       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:
1.5       noro       91: int divum(int mod,UM p1,UM p2,UM pq)
1.1       noro       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-- )
                    111:                if ( tmp = *ct-- ) {
                    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:
1.5       noro      131: void diffum(int mod,UM f,UM fd)
1.1       noro      132: {
                    133:        int *dp,*sp;
                    134:        int i;
1.5       noro      135: #if !defined(VISUAL)
1.1       noro      136:        UL ltmp;
1.5       noro      137: #endif
1.1       noro      138:
                    139:        for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i;
                    140:                i >= 1; i--, dp--, sp-- ) {
                    141:                DMAR(*sp,i,0,mod,*dp)
                    142:        }
                    143:        degum(fd,DEG(f) - 1);
                    144: }
                    145:
1.5       noro      146: unsigned int pwrm(int mod,int a,int n)
1.1       noro      147: {
                    148:        unsigned int s,t;
                    149:
                    150:        if ( !n )
                    151:                return 1;
                    152:        else if ( n == 1 )
                    153:                return a;
                    154:        else {
                    155:                t = pwrm(mod,a,n/2);
                    156:                DMAR(t,t,0,mod,s)
                    157:                if ( n % 2 ) {
                    158:                        DMAR(s,a,0,mod,t)
                    159:                        return t;
                    160:                } else
                    161:                        return s;
                    162:        }
                    163: }
                    164:
1.5       noro      165: unsigned int invm(unsigned int s,int mod)
1.1       noro      166: {
                    167:        unsigned int r,a2,q;
                    168:        unsigned int f1,f2,a1;
                    169:
                    170:        for ( f1 = s, f2 = mod, a1 = 1, a2 = 0; ; ) {
                    171:                q = f1/f2; r = f1 - f2*q; f1 = f2;
                    172:                if ( !(f2 = r) )
                    173:                        break;
                    174:                DMAR(a2,q,0,mod,r)
                    175: /*             r = ( a1 - r + mod ) % mod; */
                    176:                if ( a1 >= r )
                    177:                        r = a1 - r;
                    178:                else {
                    179:                        r = (mod - r) + a1;
                    180:                }
                    181:                a1 = a2; a2 = r;
                    182:        }
                    183: /*     return( ( a2 >= 0 ? a2 : a2 + mod ) ); */
                    184:        return a2;
                    185: }
                    186:
1.5       noro      187: unsigned int rem(N n,int m)
1.1       noro      188: {
                    189:        unsigned int *x;
                    190:        unsigned int t,r;
                    191:        int i;
                    192:
                    193:        if ( !n )
                    194:                return 0;
                    195:        for ( i = PL(n)-1, x = BD(n)+i, r = 0; i >= 0; i--, x-- ) {
1.6     ! noro      196: #if defined(sparc) || !defined(__sparcv9)
1.1       noro      197:                r = dsar(m,r,*x);
                    198: #else
                    199:                DSAB(m,r,*x,t,r)
                    200: #endif
                    201:        }
                    202:        return r;
                    203: }
                    204:
1.6     ! noro      205: #if !defined(sparc) || defined(__sparcv9)
1.5       noro      206: void addpadic(int mod,int n,unsigned int *n1,unsigned int *n2)
1.1       noro      207: {
                    208:        unsigned int carry,tmp;
                    209:        int i;
                    210:
                    211:        for ( i = 0, carry = 0; i < n; i++ ) {
                    212:                tmp = *n1++ + *n2 + carry;
                    213:                DQR(tmp,mod,carry,*n2++)
                    214: /*
                    215:                carry = tmp / mod;
                    216:                *n2++ = tmp - ( carry * mod );
                    217: */
                    218:        }
                    219: }
                    220: #endif
                    221:
1.5       noro      222: void mulpadic(int mod,int n,unsigned int *n1,unsigned int *n2,unsigned int *nr)
1.1       noro      223: {
                    224:        unsigned int *pn1,*pnr;
                    225:        unsigned int carry,mul;
                    226:        int i,j;
                    227:
                    228:        bzero((char *)nr,(int)(n*sizeof(int)));
                    229:        for ( j = 0; j < n; j++, n2++, nr++ )
                    230:                if ( mul = *n2 )
                    231:                        for ( i = j, carry = 0, pn1 = n1, pnr = nr;
                    232:                                i < n; i++, pn1++, pnr++ ) {
                    233:                                carry += *pnr;
                    234:                                DMAB(mod,*pn1,mul,carry,carry,*pnr)
                    235:                        }
                    236: }
                    237:
                    238: extern up_kara_mag;
                    239:
1.5       noro      240: void kmulum(int mod,UM n1,UM n2,UM nr)
1.1       noro      241: {
                    242:        UM n,t,s,m,carry;
                    243:        int d,d1,d2,len,i,l;
                    244:        unsigned int *r,*r0;
                    245:
                    246:        if ( !n1 || !n2 ) {
                    247:                nr->d = -1; return;
                    248:        }
                    249:        d1 = DEG(n1)+1; d2 = DEG(n2)+1;
                    250:        if ( (d1 < up_kara_mag) || (d2 < up_kara_mag) ) {
                    251:                mulum(mod,n1,n2,nr); return;
                    252:        }
                    253:        if ( d1 < d2 ) {
                    254:                n = n1; n1 = n2; n2 = n;
                    255:                d = d1; d1 = d2; d2 = d;
                    256:        }
                    257:        if ( d2 > (d1+1)/2 ) {
                    258:                kmulummain(mod,n1,n2,nr); return;
                    259:        }
                    260:        d = (d1/d2)+((d1%d2)!=0);
                    261:        len = (d+1)*d2;
                    262:        r0 = (unsigned int *)ALLOCA(len*sizeof(int));
                    263:        bzero((char *)r0,len*sizeof(int));
                    264:        m = W_UMALLOC(d2+1);
                    265:        carry = W_UMALLOC(d2+1);
                    266:        t = W_UMALLOC(d1+d2+1);
                    267:        s = W_UMALLOC(d1+d2+1);
1.4       noro      268:        for ( DEG(carry) = -1, i = 0, r = r0; i < d; i++, r += d2 ) {
1.1       noro      269:                extractum(n1,i*d2,d2,m);
                    270:                if ( m ) {
                    271:                        kmulum(mod,m,n2,t);
                    272:                        addum(mod,t,carry,s);
                    273:                        c_copyum(s,d2,r);
                    274:                        extractum(s,d2,d2,carry);
                    275:                } else {
                    276:                        c_copyum(carry,d2,r);
                    277:                        carry = 0;
                    278:                }
                    279:        }
                    280:        c_copyum(carry,d2,r);
                    281:        for ( l = len - 1; !r0[l]; l-- );
                    282:        l++;
                    283:        DEG(nr) = l-1;
                    284:        bcopy((char *)r0,(char *)COEF(nr),l*sizeof(int));
                    285: }
                    286:
1.5       noro      287: void ksquareum(int mod,UM n1,UM nr)
1.1       noro      288: {
                    289:        int d1;
                    290:
                    291:        if ( !n1 ) {
                    292:                nr->d = -1; return;
                    293:        }
                    294:        d1 = DEG(n1)+1;
                    295:        if ( (d1 < up_kara_mag) ) {
                    296:                pwrum(mod,n1,2,nr); return;
                    297:        }
                    298:        ksquareummain(mod,n1,nr);
                    299: }
                    300:
1.5       noro      301: void extractum(UM n,int index,int len,UM nr)
1.1       noro      302: {
                    303:        int *m;
                    304:        int l;
                    305:
                    306:        if ( !n ) {
                    307:                nr->d = -1; return;
                    308:        }
                    309:        m = COEF(n)+index;
                    310:        if ( (l = (DEG(n)+1)-index) >= len ) {
                    311:                for ( l = len - 1; (l >= 0) && !m[l]; l-- );
                    312:                l++;
                    313:        }
                    314:        if ( l <= 0 ) {
                    315:                nr->d = -1; return;
                    316:        } else {
                    317:                DEG(nr) = l-1;
                    318:                bcopy((char *)m,(char *)COEF(nr),l*sizeof(Q));
                    319:        }
                    320: }
                    321:
1.5       noro      322: void copyum(UM n1,UM n2)
1.1       noro      323: {
                    324:        n2->d = n1->d;
                    325:        bcopy((char *)n1->c,(char *)n2->c,(n1->d+1)*sizeof(int));
                    326: }
                    327:
1.5       noro      328: void c_copyum(UM n,int len,int *p)
1.1       noro      329: {
                    330:        if ( n )
                    331:                bcopy((char *)COEF(n),(char *)p,MIN((DEG(n)+1),len)*sizeof(int));
                    332: }
                    333:
1.5       noro      334: void kmulummain(int mod,UM n1,UM n2,UM nr)
1.1       noro      335: {
                    336:        int d1,d2,h,len;
                    337:        UM n1lo,n1hi,n2lo,n2hi,hi,lo,mid1,mid2,mid,s1,s2,t1,t2;
                    338:
                    339:        d1 = DEG(n1)+1; d2 = DEG(n2)+1; h = (d1+1)/2;
                    340:        n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1);
                    341:        n2lo = W_UMALLOC(d2+1); n2hi = W_UMALLOC(d2+1);
                    342:        lo = W_UMALLOC(d1+d2+1); hi = W_UMALLOC(d1+d2+1);
                    343:        mid1 = W_UMALLOC(d1+d2+1); mid2 = W_UMALLOC(d1+d2+1);
                    344:        mid = W_UMALLOC(d1+d2+1);
                    345:        s1 = W_UMALLOC(d1+d2+1); s2 = W_UMALLOC(d1+d2+1);
                    346:        extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi);
                    347:        extractum(n2,0,h,n2lo); extractum(n2,h,d2-h,n2hi);
                    348:        kmulum(mod,n1hi,n2hi,hi); kmulum(mod,n1lo,n2lo,lo);
                    349:        len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1;
                    350:        bzero((char *)COEF(t1),len*sizeof(int));
                    351:        if ( lo )
                    352:                bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int));
                    353:        if ( hi )
                    354:                bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int));
                    355:
                    356:        addum(mod,hi,lo,mid1);
                    357:        subum(mod,n1hi,n1lo,s1); subum(mod,n2lo,n2hi,s2); kmulum(mod,s1,s2,mid2);
                    358:        addum(mod,mid1,mid2,mid);
                    359:        if ( mid ) {
                    360:                len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1;
                    361:                bzero((char *)COEF(t2),len*sizeof(int));
                    362:                bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int));
                    363:                addum(mod,t1,t2,nr);
                    364:        } else
                    365:                copyum(t1,nr);
                    366: }
                    367:
1.5       noro      368: void ksquareummain(int mod,UM n1,UM nr)
1.1       noro      369: {
                    370:        int d1,h,len;
                    371:        UM n1lo,n1hi,hi,lo,mid1,mid2,mid,s1,t1,t2;
                    372:
                    373:        d1 = DEG(n1)+1; h = (d1+1)/2;
                    374:        n1lo = W_UMALLOC(d1+1); n1hi = W_UMALLOC(d1+1);
                    375:        lo = W_UMALLOC(2*d1+1); hi = W_UMALLOC(2*d1+1);
                    376:        mid1 = W_UMALLOC(2*d1+1); mid2 = W_UMALLOC(2*d1+1);
                    377:        mid = W_UMALLOC(2*d1+1);
                    378:        s1 = W_UMALLOC(2*d1+1);
                    379:        extractum(n1,0,h,n1lo); extractum(n1,h,d1-h,n1hi);
                    380:        ksquareum(mod,n1hi,hi); ksquareum(mod,n1lo,lo);
                    381:        len = DEG(hi)+1+2*h; t1 = W_UMALLOC(len-1); DEG(t1) = len-1;
                    382:        bzero((char *)COEF(t1),len*sizeof(int));
                    383:        if ( lo )
                    384:                bcopy((char *)COEF(lo),(char *)COEF(t1),(DEG(lo)+1)*sizeof(int));
                    385:        if ( hi )
                    386:                bcopy((char *)COEF(hi),(char *)(COEF(t1)+2*h),(DEG(hi)+1)*sizeof(int));
                    387:
                    388:        addum(mod,hi,lo,mid1);
                    389:        subum(mod,n1hi,n1lo,s1); ksquareum(mod,s1,mid2);
                    390:        subum(mod,mid1,mid2,mid);
                    391:        if ( mid ) {
                    392:                len = DEG(mid)+1+h; t2 = W_UMALLOC(len-1); DEG(t2) = len-1;
                    393:                bzero((char *)COEF(t2),len*sizeof(int));
                    394:                bcopy((char *)COEF(mid),(char *)(COEF(t2)+h),(DEG(mid)+1)*sizeof(int));
                    395:                addum(mod,t1,t2,nr);
                    396:        } else
                    397:                copyum(t1,nr);
                    398: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>