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

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

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