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

Annotation of OpenXM_contrib2/asir2000/engine/lmi.c, Revision 1.4

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.4     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/engine/lmi.c,v 1.3 2000/08/22 05:04:05 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "base.h"
                     52: #include "inline.h"
                     53:
                     54: typedef struct oGEN_LM {
                     55:        int id;
                     56:        N dense;
                     57:        N sparse;
                     58:        int bit;
                     59:        unsigned int lower;
                     60: } *GEN_LM;
                     61:
                     62: void pwrlm0(N,N,N *);
                     63: void gen_simpn(N,N*);
                     64: void gen_simpn_force(N,N*);
                     65:
                     66: int lm_lazy;
                     67:
                     68: static GEN_LM current_mod_lm;
                     69:
1.4     ! noro       70: void random_lm(LM *r)
1.1       noro       71: {
                     72:        N n,t;
                     73:
                     74:        if ( !current_mod_lm )
                     75:                error("random_lm : current_mod_lm is not set");
                     76:        randomn(current_mod_lm->bit+1,&n);
                     77:        gen_simpn_force(n,&t);
                     78:        MKLM(t,*r);
                     79: }
                     80:
1.4     ! noro       81: void ntosparsen(N p,N *bits)
1.1       noro       82: {
                     83:        int l,i,j,nz;
                     84:        N r;
                     85:        unsigned int *pb,*rb;
                     86:        if ( !p )
                     87:                *bits = 0;
                     88:        else {
                     89:                l = n_bits(p);
                     90:                pb = BD(p);
                     91:                for ( i = l-1, nz = 0; i >= 0; i-- )
                     92:                        if ( pb[i>>5]&(1<<i&31) ) nz++;
                     93:                *bits = r = NALLOC(nz); PL(r) = nz;
                     94:                rb = BD(r);
                     95:                for ( i = l-1, j = 0; i >= 0; i-- )
                     96:                        if ( pb[i>>5]&(1<<i&31) )
                     97:                                rb[j++] = i;
                     98:        }
                     99: }
                    100:
1.4     ! noro      101: void setmod_lm(N p)
1.1       noro      102: {
                    103:        int i;
                    104:
                    105:        if ( !current_mod_lm )
                    106:                current_mod_lm = (GEN_LM)MALLOC(sizeof(struct oGEN_LM));
                    107:        bzero((char *)current_mod_lm,sizeof(struct oGEN_LM));
                    108:        current_mod_lm->dense = p;
                    109:        ntosparsen(p,&current_mod_lm->sparse);
                    110:        current_mod_lm->bit = n_bits(p);
                    111:        current_mod_lm->id = UP2_DENSE; /* use ordinary rep. by default */
                    112:        if ( !(current_mod_lm->bit%32) ) {
                    113:                for ( i = PL(p)-1; i >= 1; i-- )
                    114:                        if ( BD(p)[i] != 0xffffffff )
                    115:                                break;
                    116:                if ( !i ) {
                    117:                        current_mod_lm->lower = (unsigned int)((((UL)1)<<32)-((UL)BD(p)[0]));
                    118:                        current_mod_lm->id = UP2_SPARSE; /* XXX */
                    119:                }
                    120:        }
                    121: }
                    122:
1.4     ! noro      123: void getmod_lm(N *p)
1.1       noro      124: {
                    125:        if ( !current_mod_lm )
                    126:                *p = 0;
                    127:        else
                    128:                *p = current_mod_lm->dense;
                    129: }
                    130:
1.4     ! noro      131: void simplm(LM n,LM *r)
1.1       noro      132: {
                    133:        N rem;
                    134:
                    135:        if ( !n )
                    136:                *r = 0;
                    137:        else if ( NID(n) != N_LM )
                    138:                *r = n;
                    139:        else {
                    140:                gen_simpn(n->body,&rem);
                    141:                MKLM(rem,*r);
                    142:        }
                    143: }
                    144:
1.4     ! noro      145: void qtolm(Q q,LM *l)
1.1       noro      146: {
                    147:        N rn;
                    148:        LM a,b,c;
                    149:
                    150:        if ( !q || (OID(q)==O_N && ((NID(q) == N_LM) || (NID(q) == N_GFPN))) ) { /* XXX */
                    151:                *l = (LM)q;
                    152:        } else if ( OID(q) == O_N && NID(q) == N_Q ) {
                    153:                gen_simpn(NM(q),&rn); MKLM(rn,a);
                    154:                if ( SGN(q) < 0 ) {
                    155:                        chsgnlm(a,&c); a = c;
                    156:                }
                    157:                if ( DN(q) ) {
                    158:                        gen_simpn_force(DN(q),&rn); MKLM(rn,b);
                    159:                        if ( !b )
                    160:                                error("qtolm : invalid denominator");
                    161:                        divlm(a,b,l);
                    162:                } else
                    163:                        *l = a;
                    164:        } else
                    165:                error("qtolm : invalid argument");
                    166: }
                    167:
                    168: #define NZLM(a) ((a)&&(NID(a)==N_LM))
                    169:
1.4     ! noro      170: void addlm(LM a,LM b,LM *c)
1.1       noro      171: {
                    172:        N t,t1;
                    173:        LM z;
                    174:
                    175:        qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    176:        if ( !a )
                    177:                *c = b;
                    178:        else if ( !b )
                    179:                *c = a;
                    180:        else {
                    181:                addn(a->body,b->body,&t);
                    182:                gen_simpn(t,&t1);
                    183:                MKLM(t1,*c);
                    184:        }
                    185: }
                    186:
1.4     ! noro      187: void sublm(LM a,LM b,LM *c)
1.1       noro      188: {
                    189:        int s;
1.4     ! noro      190:        N t;
1.1       noro      191:        LM z;
                    192:
                    193:        qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    194:        if ( !b )
                    195:                *c = a;
                    196:        else if ( !a )
                    197:                chsgnlm(b,c);
                    198:        else {
                    199:                s = subn(a->body,b->body,&t);
                    200:                if ( !s )
                    201:                        *c = 0;
                    202:                else {
                    203:                        MKLM(t,z);
                    204:                        if ( s < 0 )
                    205:                                chsgnlm(z,c);
                    206:                        else
                    207:                                *c = z;
                    208:                }
                    209:        }
                    210: }
                    211:
1.4     ! noro      212: void mullm(LM a,LM b,LM *c)
1.1       noro      213: {
1.4     ! noro      214:        N t,r;
1.1       noro      215:        LM z;
                    216:
                    217:        qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    218:        if ( !a || !b )
                    219:                *c = 0;
                    220:        else {
                    221:                kmuln(a->body,b->body,&t);
                    222:                gen_simpn(t,&r);
                    223:                MKLM(r,*c);
                    224:        }
                    225: }
                    226:
1.4     ! noro      227: void divlm(LM a,LM b,LM *c)
1.1       noro      228: {
                    229:        LM r;
                    230:        Q t,m,i;
                    231:        LM z;
                    232:
                    233:        qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    234:        if ( !b )
                    235:                error("divlm: division by 0");
                    236:        else if ( !a )
                    237:                *c = 0;
                    238:        else {
                    239:                NTOQ(b->body,1,t); NTOQ(current_mod_lm->dense,1,m);
                    240:                invl(t,m,&i);
                    241:                MKLM(NM(i),r);
                    242:                mullm(a,r,c);
                    243:        }
                    244: }
                    245:
1.4     ! noro      246: void chsgnlm(LM a,LM *c)
1.1       noro      247: {
                    248:        LM t;
                    249:        N s,u;
                    250:
                    251:        if ( !a )
                    252:                *c = a;
                    253:        else {
                    254:                qtolm((Q)a,&t); a = t;
                    255:                gen_simpn_force(a->body,&s);
                    256:                subn(current_mod_lm->dense,s,&u);
                    257:                MKLM(u,*c);
                    258:        }
                    259: }
                    260:
1.4     ! noro      261: void pwrlm(LM a,Q b,LM *c)
1.1       noro      262: {
                    263:        LM t;
                    264:        N s;
                    265:
                    266:        if ( !b ) {
                    267:                MKLM(ONEN,*c);
                    268:        } else if ( !a )
                    269:                *c = 0;
                    270:        else {
                    271:                qtolm((Q)a,&t); a = t;
                    272:                pwrlm0(a->body,NM(b),&s);
                    273:                MKLM(s,*c);
                    274:        }
                    275: }
                    276:
1.4     ! noro      277: void pwrlm0(N a,N n,N *c)
1.1       noro      278: {
                    279:        N n1,t,t1,t2,r1;
                    280:        int r;
                    281:
                    282:        if ( !n )
                    283:                *c = ONEN;
                    284:        else if ( UNIN(n) )
                    285:                *c = a;
                    286:        else {
                    287:                r = divin(n,2,&n1);
                    288:                pwrlm0(a,n1,&t);
                    289:                kmuln(t,t,&t1); gen_simpn(t1,&r1);
                    290:                if ( r ) {
                    291:                        kmuln(r1,a,&t2); gen_simpn(t2,&r1);
                    292:                }
                    293:                *c = r1;
                    294:        }
                    295: }
                    296:
1.4     ! noro      297: int cmplm(LM a,LM b)
1.1       noro      298: {
                    299:        LM z;
                    300:
                    301:        qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    302:        if ( !a )
                    303:                if ( !b )
                    304:                        return 0;
                    305:                else
                    306:                        return -1;
                    307:        else if ( !b )
                    308:                        return 1;
                    309:        else
                    310:                return cmpn(a->body,b->body);
                    311: }
                    312:
                    313: void remn_special(N,N,int,unsigned int ,N *);
                    314:
1.4     ! noro      315: void gen_simpn(N a,N *b)
1.1       noro      316: {
                    317:        if ( !current_mod_lm )
                    318:                error("gen_simpn: current_mod_lm is not set");
                    319:        if ( lm_lazy )
                    320:                *b = a;
                    321:        else if ( current_mod_lm->id == UP2_SPARSE )
                    322:                remn_special(a,current_mod_lm->dense,current_mod_lm->bit,current_mod_lm->lower,b);
                    323:        else
                    324:                remn(a,current_mod_lm->dense,b);
                    325: }
                    326:
1.4     ! noro      327: void gen_simpn_force(N a,N *b)
1.1       noro      328: {
                    329:        if ( !current_mod_lm )
                    330:                error("gen_simpn_force: current_mod_lm is not set");
                    331:        else if ( current_mod_lm->id == UP2_SPARSE )
                    332:                remn_special(a,current_mod_lm->dense,current_mod_lm->bit,current_mod_lm->lower,b);
                    333:        else
                    334:                remn(a,current_mod_lm->dense,b);
                    335: }
                    336:

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