[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.7

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

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