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

Annotation of OpenXM_contrib2/asir2000/engine/gfpn.c, Revision 1.5

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.5     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/engine/gfpn.c,v 1.4 2001/10/09 01:36:11 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "base.h"
                     52:
                     53: void chsgngfpn(GFPN,GFPN *);
                     54:
                     55: UP current_mod_gfpn;
                     56:
1.4       noro       57: void setmod_gfpn(P p)
1.1       noro       58: {
1.5     ! noro       59:   UP t;
1.1       noro       60:
1.5     ! noro       61:   ptoup(p,&t); uptolmup(t,&current_mod_gfpn);
1.1       noro       62: }
                     63:
1.4       noro       64: void getmod_gfpn(UP *up)
1.1       noro       65: {
1.5     ! noro       66:   *up = current_mod_gfpn;
1.1       noro       67: }
                     68:
1.4       noro       69: void ptogfpn(Obj q,GFPN *l)
1.1       noro       70: {
1.5     ! noro       71:   UP q1,q2;
1.1       noro       72:
1.5     ! noro       73:   if ( !q || (OID(q)==O_N && NID(q)==N_GFPN) ) {
        !            74:     *l = (GFPN)q;
        !            75:   } else if ( (OID(q)==O_N && NID(q)==N_Q) || OID(q)==O_P ) {
        !            76:     ptoup((P)q,&q1); uptolmup(q1,&q2);
        !            77:     MKGFPN(q2,*l);
        !            78:   } else
        !            79:     error("ptogfpn : invalid argument");
1.1       noro       80: }
                     81:
1.4       noro       82: void gfpntop(GFPN q,P *l)
1.1       noro       83: {
1.5     ! noro       84:   if ( !q )
        !            85:     *l = 0;
        !            86:   else if ( NID(q) == N_GFPN )
        !            87:     uptop(q->body,l);
        !            88:   else
        !            89:     *l = (P)q;
1.1       noro       90: }
                     91:
1.4       noro       92: void simpgfpn(GFPN n,GFPN *r)
1.1       noro       93: {
1.5     ! noro       94:   UP rem,t;
1.1       noro       95:
1.5     ! noro       96:   if ( !n )
        !            97:     *r = 0;
        !            98:   else if ( NID(n) != N_GFPN )
        !            99:     *r = n;
        !           100:   else {
        !           101:     simpup(n->body,&t);
        !           102:     remup(t,current_mod_gfpn,&rem);
        !           103:     MKGFPN(rem,*r);
        !           104:   }
1.1       noro      105: }
                    106:
                    107: #define NZGFPN(a) ((a)&&(OID(a)==O_N)&&(NID(a)==N_GFPN))
                    108:
1.4       noro      109: void ntogfpn(Obj a,GFPN *b)
1.1       noro      110: {
1.5     ! noro      111:   UP t;
        !           112:   LM lm;
1.1       noro      113:
1.5     ! noro      114:   if ( !a || (OID(a)==O_N && NID(a) == N_GFPN) )
        !           115:     *b = (GFPN)a;
        !           116:   else if ( OID(a) == O_N && (NID(a) == N_LM || NID(a) == N_Q) ) {
        !           117:     qtolm((Q)a,&lm);
        !           118:     if ( !lm )
        !           119:       *b = 0;
        !           120:     else {
        !           121:       t = UPALLOC(0); t->d = 0; t->c[0] = (Num)lm;
        !           122:       MKGFPN(t,*b);
        !           123:     }
        !           124:   } else
        !           125:     error("ntogfpn : invalid argument");
1.1       noro      126: }
                    127:
1.4       noro      128: void addgfpn(GFPN a,GFPN b,GFPN *c)
1.1       noro      129: {
1.5     ! noro      130:   UP t,t1,t2;
        !           131:   GFPN z;
1.1       noro      132:
1.5     ! noro      133:   ntogfpn((Obj)a,&z); a = z; ntogfpn((Obj)b,&z); b = z;
        !           134:   if ( !a )
        !           135:     *c = b;
        !           136:   else if ( !b )
        !           137:     *c = a;
        !           138:   else {
        !           139:     addup(a->body,b->body,&t);
        !           140:     simpup(t,&t1);
        !           141:     remup(t1,current_mod_gfpn,&t2);
        !           142:     MKGFPN(t2,*c);
        !           143:   }
1.1       noro      144: }
                    145:
1.4       noro      146: void subgfpn(GFPN a,GFPN b,GFPN *c)
1.1       noro      147: {
1.5     ! noro      148:   UP t,t1,t2;
        !           149:   GFPN z;
1.1       noro      150:
1.5     ! noro      151:   ntogfpn((Obj)a,&z); a = z; ntogfpn((Obj)b,&z); b = z;
        !           152:   if ( !a )
        !           153:     chsgngfpn(b,c);
        !           154:   else if ( !b )
        !           155:     *c = a;
        !           156:   else {
        !           157:     subup(a->body,b->body,&t);
        !           158:     simpup(t,&t1);
        !           159:     remup(t1,current_mod_gfpn,&t2);
        !           160:     MKGFPN(t2,*c);
        !           161:   }
1.1       noro      162: }
                    163:
                    164: extern int up_lazy;
                    165:
1.4       noro      166: void mulgfpn(GFPN a,GFPN b,GFPN *c)
1.1       noro      167: {
1.5     ! noro      168:   UP t,t1,t2;
        !           169:   GFPN z;
1.1       noro      170:
1.5     ! noro      171:   ntogfpn((Obj)a,&z); a = z; ntogfpn((Obj)b,&z); b = z;
        !           172:   if ( !a || !b )
        !           173:     *c = 0;
        !           174:   else {
        !           175:     up_lazy=1;
        !           176:     mulup(a->body,b->body,&t);
        !           177:     up_lazy=0;
        !           178:     simpup(t,&t1);
        !           179:     remup(t1,current_mod_gfpn,&t2);
        !           180:     MKGFPN(t2,*c);
        !           181:   }
1.1       noro      182: }
                    183:
1.4       noro      184: void squaregfpn(GFPN a,GFPN *c)
1.1       noro      185: {
1.5     ! noro      186:   UP t,t1,t2;
        !           187:   GFPN z;
1.1       noro      188:
1.5     ! noro      189:   ntogfpn((Obj)a,&z); a = z;
        !           190:   if ( !a )
        !           191:     *c = 0;
        !           192:   else {
        !           193:     squareup(a->body,&t);
        !           194:     simpup(t,&t1);
        !           195:     remup(t1,current_mod_gfpn,&t2);
        !           196:     MKGFPN(t2,*c);
        !           197:   }
1.1       noro      198: }
                    199:
1.4       noro      200: void divgfpn(GFPN a,GFPN b,GFPN *c)
1.1       noro      201: {
1.5     ! noro      202:   UP t,t1,i,s;
        !           203:   GFPN z;
1.1       noro      204:
1.5     ! noro      205:   ntogfpn((Obj)a,&z); a = z; ntogfpn((Obj)b,&z); b = z;
        !           206:   if ( !b )
        !           207:     error("divgfpn: division by 0");
        !           208:   else if ( !a )
        !           209:     *c = 0;
        !           210:   else {
        !           211:     extended_gcdup(b->body,current_mod_gfpn,&i);
        !           212:     mulup(a->body,i,&t);
        !           213:     simpup(t,&t1);
        !           214:     remup(t1,current_mod_gfpn,&s);
        !           215:     MKGFPN(s,*c);
        !           216:   }
1.1       noro      217: }
                    218:
1.4       noro      219: void invgfpn(GFPN b,GFPN *c)
1.1       noro      220: {
1.5     ! noro      221:   UP i,t;
        !           222:   GFPN z;
1.1       noro      223:
1.5     ! noro      224:   ntogfpn((Obj)b,&z); b = z;
        !           225:   if ( !b )
        !           226:     error("divgfpn: division by 0");
        !           227:   else {
        !           228:     simpup(b->body,&t);
        !           229:     extended_gcdup(t,current_mod_gfpn,&i);
        !           230:     MKGFPN(i,*c);
        !           231:   }
1.1       noro      232: }
                    233:
1.4       noro      234: void chsgngfpn(GFPN a,GFPN *c)
1.1       noro      235: {
1.5     ! noro      236:   GFPN z;
        !           237:   UP t,t1;
1.1       noro      238:
1.5     ! noro      239:   ntogfpn((Obj)a,&z); a = z;
        !           240:   if ( !a )
        !           241:     *c = 0;
        !           242:   else {
        !           243:     simpup(a->body,&t);
        !           244:     chsgnup(t,&t1);
        !           245:     MKGFPN(t1,*c);
        !           246:   }
1.1       noro      247: }
                    248:
1.4       noro      249: void pwrgfpn(GFPN a,Q b,GFPN *c)
1.1       noro      250: {
1.5     ! noro      251:   UP t,s;
        !           252:   GFPN r;
        !           253:   Q b0;
        !           254:
        !           255:   if ( !b ) {
        !           256:     t = UPALLOC(0); t->d = 0; t->c[0] = (Num)ONELM; MKGFPN(t,*c);
        !           257:   } else if ( !a )
        !           258:     *c = 0;
        !           259:   else {
        !           260:     DUPQ(b,b0); SGN(b0)=1;
        !           261:     simpup(a->body,&s);
        !           262:     hybrid_generic_powermodup(s,current_mod_gfpn,b0,&t);
        !           263:     MKGFPN(t,r);
        !           264:     if ( SGN(b) < 0 )
        !           265:       invgfpn(r,c);
        !           266:     else
        !           267:       *c = r;
        !           268:   }
1.1       noro      269: }
                    270:
1.4       noro      271: int cmpgfpn(GFPN a,GFPN b)
1.1       noro      272: {
1.5     ! noro      273:   GFPN z;
1.1       noro      274:
1.5     ! noro      275:   ntogfpn((Obj)a,&z); a = z; ntogfpn((Obj)b,&z); b = z;
        !           276:   if ( !a )
        !           277:     if ( !b )
        !           278:       return 0;
        !           279:     else
        !           280:       return -1;
        !           281:   else if ( !b )
        !           282:       return 1;
        !           283:   else
        !           284:     return compup(a->body,b->body);
1.1       noro      285: }
                    286:
1.4       noro      287: void randomgfpn(GFPN *r)
1.1       noro      288: {
1.5     ! noro      289:   int i,d;
        !           290:   LM *tb;
        !           291:   UP t;
        !           292:
        !           293:   if ( !current_mod_gfpn )
        !           294:     error("randomgfpn : current_mod_gfpn is not set");
        !           295:   d = current_mod_gfpn->d;
        !           296:   t = UPALLOC(d-1);
        !           297:   for ( i = 0, tb = (LM *)t->c; i < d; i++ )
        !           298:     random_lm(&tb[i]);
        !           299:   for ( i = d-1; i >= 0 && !tb[i]; i-- );
        !           300:   if ( i < 0 )
        !           301:     *r = 0;
        !           302:   else {
        !           303:     t->d = i; MKGFPN(t,*r);
        !           304:   }
1.1       noro      305: }

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