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

Annotation of OpenXM_contrib2/asir2018/engine/gfpn.c, Revision 1.1

1.1     ! 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
        !            26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
        !            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:  *
        !            48:  * $OpenXM$
        !            49: */
        !            50: #include "ca.h"
        !            51: #include "base.h"
        !            52:
        !            53: void chsgngfpn(GFPN,GFPN *);
        !            54:
        !            55: UP current_mod_gfpn;
        !            56:
        !            57: void setmod_gfpn(P p)
        !            58: {
        !            59:   UP t;
        !            60:
        !            61:   ptoup(p,&t); uptolmup(t,&current_mod_gfpn);
        !            62: }
        !            63:
        !            64: void getmod_gfpn(UP *up)
        !            65: {
        !            66:   *up = current_mod_gfpn;
        !            67: }
        !            68:
        !            69: void ptogfpn(Obj q,GFPN *l)
        !            70: {
        !            71:   UP q1,q2;
        !            72:
        !            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");
        !            80: }
        !            81:
        !            82: void gfpntop(GFPN q,P *l)
        !            83: {
        !            84:   if ( !q )
        !            85:     *l = 0;
        !            86:   else if ( NID(q) == N_GFPN )
        !            87:     uptop(q->body,l);
        !            88:   else
        !            89:     *l = (P)q;
        !            90: }
        !            91:
        !            92: void simpgfpn(GFPN n,GFPN *r)
        !            93: {
        !            94:   UP rem,t;
        !            95:
        !            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:   }
        !           105: }
        !           106:
        !           107: #define NZGFPN(a) ((a)&&(OID(a)==O_N)&&(NID(a)==N_GFPN))
        !           108:
        !           109: void ntogfpn(Obj a,GFPN *b)
        !           110: {
        !           111:   UP t;
        !           112:   LM lm;
        !           113:
        !           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");
        !           126: }
        !           127:
        !           128: void addgfpn(GFPN a,GFPN b,GFPN *c)
        !           129: {
        !           130:   UP t,t1,t2;
        !           131:   GFPN z;
        !           132:
        !           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:   }
        !           144: }
        !           145:
        !           146: void subgfpn(GFPN a,GFPN b,GFPN *c)
        !           147: {
        !           148:   UP t,t1,t2;
        !           149:   GFPN z;
        !           150:
        !           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:   }
        !           162: }
        !           163:
        !           164: extern int up_lazy;
        !           165:
        !           166: void mulgfpn(GFPN a,GFPN b,GFPN *c)
        !           167: {
        !           168:   UP t,t1,t2;
        !           169:   GFPN z;
        !           170:
        !           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:   }
        !           182: }
        !           183:
        !           184: void squaregfpn(GFPN a,GFPN *c)
        !           185: {
        !           186:   UP t,t1,t2;
        !           187:   GFPN z;
        !           188:
        !           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:   }
        !           198: }
        !           199:
        !           200: void divgfpn(GFPN a,GFPN b,GFPN *c)
        !           201: {
        !           202:   UP t,t1,i,s;
        !           203:   GFPN z;
        !           204:
        !           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:   }
        !           217: }
        !           218:
        !           219: void invgfpn(GFPN b,GFPN *c)
        !           220: {
        !           221:   UP i,t;
        !           222:   GFPN z;
        !           223:
        !           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:   }
        !           232: }
        !           233:
        !           234: void chsgngfpn(GFPN a,GFPN *c)
        !           235: {
        !           236:   GFPN z;
        !           237:   UP t,t1;
        !           238:
        !           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:   }
        !           247: }
        !           248:
        !           249: void pwrgfpn(GFPN a,Z b,GFPN *c)
        !           250: {
        !           251:   UP t,s;
        !           252:   GFPN r;
        !           253:   Z 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:     absz(b,&b0);
        !           261:     simpup(a->body,&s);
        !           262:     hybrid_generic_powermodup(s,current_mod_gfpn,b0,&t);
        !           263:     MKGFPN(t,r);
        !           264:     if ( sgnz(b) < 0 )
        !           265:       invgfpn(r,c);
        !           266:     else
        !           267:       *c = r;
        !           268:   }
        !           269: }
        !           270:
        !           271: int cmpgfpn(GFPN a,GFPN b)
        !           272: {
        !           273:   GFPN z;
        !           274:
        !           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);
        !           285: }
        !           286:
        !           287: void randomgfpn(GFPN *r)
        !           288: {
        !           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:   }
        !           305: }

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