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

Annotation of OpenXM_contrib2/asir2000/builtin/poly.c, Revision 1.6

1.4       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.5       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4       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.6     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/poly.c,v 1.5 2000/08/22 05:03:59 noro Exp $
1.4       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "base.h"
                     53:
                     54: void Pranp();
                     55:
                     56: void Pumul(),Pumul_ff(),Pusquare(),Pusquare_ff(),Putmul(),Putmul_ff();
                     57: void Pkmul(),Pksquare(),Pktmul();
                     58: void Pord(), Pcoef0(), Pcoef(), Pdeg(), Pmindeg(), Psetmod();
                     59: void Pcoef_gf2n();
                     60: void getcoef(), getdeglist(), mergedeglist(), change_mvar(), restore_mvar();
                     61:
1.2       noro       62: void Pp_mag(),Pmaxblen();
1.1       noro       63: void Pmergelist(), Pch_mv(), Pre_mv(), Pdeglist();
                     64: void Pptomp(),Pmptop();
                     65: void Pptolmp(),Plmptop();
1.6     ! noro       66: void Psfptop();
1.1       noro       67: void Pptogf2n(),Pgf2ntop(),Pgf2ntovect();
                     68: void Pptogfpn(),Pgfpntop();
                     69: void Pfind_root_gf2n();
1.2       noro       70: void Pumul_specialmod(),Pusquare_specialmod(),Putmul_specialmod();
1.1       noro       71:
                     72: void Pureverse(),Putrunc(),Pudecomp(),Purembymul(),Purembymul_precomp();
                     73: void Puinvmod(),Purevinvmod();
                     74: void Ppwrmod_ff(),Ppwrtab_ff(),Pgeneric_pwrmod_ff();
                     75: void Pkpwrmod_lm(),Pkpwrtab_lm(),Pkgeneric_pwrmod_lm();
                     76:
                     77: void Pkmulum();
                     78: void Pksquareum();
                     79:
                     80: void Pfmultest();
                     81: void Phfmul_lm();
                     82: void Plazy_lm();
                     83:
                     84: void Psetmod_ff();
                     85: void Psimp_ff();
                     86: void Pextdeg_ff();
                     87: void Pcharacteristic_ff();
                     88: void Pfield_type_ff();
                     89: void Pfield_order_ff();
                     90: void Prandom_ff();
                     91:
                     92: void Ptracemod_gf2n();
                     93: void Psparsemod_gf2n();
                     94: void Pmultest_gf2n();
                     95: void Psquaretest_gf2n();
                     96: void Pinvtest_gf2n();
                     97: void Pbininv_gf2n();
                     98: void Prinvtest_gf2n();
                     99: void Pis_irred_gf2();
                    100: void Pis_irred_ddd_gf2();
1.2       noro      101: void Pget_next_fft_prime();
1.3       noro      102: void Puadj_coef();
1.1       noro      103: void simp_ff(Obj,Obj *);
                    104: void ranp(int,UP *);
                    105: void field_order_ff(N *);
                    106:
                    107: extern int current_mod;
                    108: extern GEN_UP2 current_mod_gf2n;
                    109: extern int lm_lazy;
                    110:
                    111: int current_ff;
                    112:
                    113: struct ftab poly_tab[] = {
1.3       noro      114:        {"uadj_coef",Puadj_coef,3},
1.1       noro      115:        {"ranp",Pranp,2},
                    116:        {"p_mag",Pp_mag,1},
1.2       noro      117:        {"maxblen",Pmaxblen,1},
1.1       noro      118:        {"ord",Pord,-1},
                    119:        {"coef0",Pcoef0,-3},
                    120:        {"coef",Pcoef,-3},
                    121:        {"coef_gf2n",Pcoef_gf2n,2},
                    122:        {"deg",Pdeg,2},
                    123:        {"mindeg",Pmindeg,2},
                    124:        {"setmod",Psetmod,-1},
                    125:
                    126:        {"sparsemod_gf2n",Psparsemod_gf2n,-1},
                    127:
                    128:        {"setmod_ff",Psetmod_ff,-2},
                    129:        {"simp_ff",Psimp_ff,1},
                    130:        {"extdeg_ff",Pextdeg_ff,0},
                    131:        {"characteristic_ff",Pcharacteristic_ff,0},
                    132:        {"field_type_ff",Pfield_type_ff,0},
                    133:        {"field_order_ff",Pfield_order_ff,0},
                    134:        {"random_ff",Prandom_ff,0},
                    135:
                    136:        {"deglist",Pdeglist,2},
                    137:        {"mergelist",Pmergelist,2},
                    138:        {"ch_mv",Pch_mv,2},
                    139:        {"re_mv",Pre_mv,2},
                    140:
                    141:        {"ptomp",Pptomp,2},
                    142:        {"mptop",Pmptop,1},
                    143:
                    144:        {"ptolmp",Pptolmp,1},
                    145:        {"lmptop",Plmptop,1},
                    146:
1.6     ! noro      147:        {"sfptop",Psfptop,1},
        !           148:
1.1       noro      149:        {"ptogf2n",Pptogf2n,1},
                    150:        {"gf2ntop",Pgf2ntop,-2},
                    151:        {"gf2ntovect",Pgf2ntovect,1},
                    152:
                    153:        {"ptogfpn",Pptogfpn,1},
                    154:        {"gfpntop",Pgfpntop,-2},
                    155:
                    156:        {"kmul",Pkmul,2},
                    157:        {"ksquare",Pksquare,1},
                    158:        {"ktmul",Pktmul,3},
                    159:
                    160:        {"umul",Pumul,2},
                    161:        {"usquare",Pusquare,1},
                    162:        {"ureverse_inv_as_power_series",Purevinvmod,2},
                    163:        {"uinv_as_power_series",Puinvmod,2},
                    164:
1.2       noro      165:        {"umul_specialmod",Pumul_specialmod,3},
                    166:        {"usquare_specialmod",Pusquare_specialmod,2},
                    167:        {"utmul_specialmod",Putmul_specialmod,4},
                    168:
1.1       noro      169:        {"utmul",Putmul,3},
                    170:        {"umul_ff",Pumul_ff,2},
                    171:        {"usquare_ff",Pusquare_ff,1},
                    172:        {"utmul_ff",Putmul_ff,3},
                    173:
                    174:        /* for historical reason */
                    175:        {"trunc",Putrunc,2},
                    176:        {"decomp",Pudecomp,2},
                    177:
                    178:        {"utrunc",Putrunc,2},
                    179:        {"udecomp",Pudecomp,2},
1.3       noro      180:        {"ureverse",Pureverse,-2},
1.1       noro      181:        {"urembymul",Purembymul,2},
                    182:        {"urembymul_precomp",Purembymul_precomp,3},
                    183:
                    184:        {"lazy_lm",Plazy_lm,1},
                    185:        {"lazy_ff",Plazy_lm,1},
                    186:
                    187:        {"pwrmod_ff",Ppwrmod_ff,1},
                    188:        {"generic_pwrmod_ff",Pgeneric_pwrmod_ff,3},
                    189:        {"pwrtab_ff",Ppwrtab_ff,2},
                    190:
                    191:        {"tracemod_gf2n",Ptracemod_gf2n,3},
                    192:        {"b_find_root_gf2n",Pfind_root_gf2n,1},
                    193:
                    194:        {"is_irred_gf2",Pis_irred_gf2,1},
                    195:        {"is_irred_ddd_gf2",Pis_irred_ddd_gf2,1},
                    196:
                    197:        {"kpwrmod_lm",Pkpwrmod_lm,1},
                    198:        {"kgeneric_pwrmod_lm",Pkgeneric_pwrmod_lm,3},
                    199:        {"kpwrtab_lm",Pkpwrtab_lm,2},
                    200:
                    201:        {"kmulum",Pkmulum,3},
                    202:        {"ksquareum",Pksquareum,2},
                    203:
                    204:        {"fmultest",Pfmultest,3},
                    205:        {"hfmul_lm",Phfmul_lm,2},
                    206:
                    207:        {"multest_gf2n",Pmultest_gf2n,2},
                    208:        {"squaretest_gf2n",Psquaretest_gf2n,1},
                    209:        {"bininv_gf2n",Pbininv_gf2n,2},
                    210:        {"invtest_gf2n",Pinvtest_gf2n,1},
                    211:        {"rinvtest_gf2n",Prinvtest_gf2n,0},
1.2       noro      212:        {"get_next_fft_prime",Pget_next_fft_prime,2},
1.1       noro      213:        {0,0,0},
                    214: };
                    215:
                    216: extern V up_var;
1.3       noro      217:
                    218: /*
                    219:        uadj_coef(F,M,M2)
                    220:        if ( F is a non-negative integer )
                    221:                return F > M2 ? F-M : M2;
                    222:        else
                    223:                F = CN*V^N+...+C0
                    224:                return uadj_coef(CN,M,M2)*V^N+...+uadj_coef(C0,M,M2);
                    225: */
                    226:
                    227: void Puadj_coef(arg,rp)
                    228: NODE arg;
                    229: P *rp;
                    230: {
                    231:        UP f,r;
                    232:        N m,m2;
                    233:
                    234:        ptoup((P)ARG0(arg),&f);
                    235:        m = NM((Q)ARG1(arg));
                    236:        m2 = NM((Q)ARG2(arg));
                    237:        adj_coefup(f,m,m2,&r);
                    238:        uptop(r,rp);
                    239: }
                    240:
1.2       noro      241: /*
                    242:        get_next_fft_prime(StartIndex,Bits)
                    243:        tries to find smallest Index >= StartIndex s.t.
                    244:                2^(Bits-1)|FFTprime[Index]-1
                    245:        return [Index,Mod] or 0 (not exist)
                    246: */
                    247:
                    248: void Pget_next_fft_prime(arg,rp)
                    249: NODE arg;
                    250: LIST *rp;
                    251: {
                    252:        unsigned int mod,d;
                    253:        int start,bits,i;
                    254:        NODE n;
                    255:        Q q,ind;
                    256:
                    257:        start = QTOS((Q)ARG0(arg));
                    258:        bits = QTOS((Q)ARG1(arg));
                    259:        for ( i = start; ; i++ ) {
                    260:                get_fft_prime(i,&mod,&d);
                    261:                if ( !mod ) {
                    262:                        *rp = 0; return;
                    263:                }
                    264:                if ( bits <= d ) {
                    265:                        UTOQ(mod,q);
                    266:                        UTOQ(i,ind);
                    267:                        n = mknode(2,ind,q);
                    268:                        MKLIST(*rp,n);
                    269:                        return;
                    270:                }
                    271:        }
                    272: }
1.1       noro      273:
                    274: void Pranp(arg,rp)
                    275: NODE arg;
                    276: P *rp;
                    277: {
                    278:        int n;
                    279:        UP c;
                    280:
                    281:        n = QTOS((Q)ARG0(arg));
                    282:        ranp(n,&c);
                    283:        if ( c ) {
                    284:                up_var = VR((P)ARG1(arg));
                    285:                uptop(c,rp);
                    286:        } else
                    287:                *rp = 0;
                    288: }
                    289:
                    290: void ranp(n,nr)
                    291: int n;
                    292: UP *nr;
                    293: {
                    294:        int i;
                    295:        unsigned int r;
                    296:        Q q;
                    297:        UP c;
                    298:
                    299:        *nr = c = UPALLOC(n);
                    300:        for ( i = 0; i <= n; i++ ) {
                    301:                r = random();
                    302:                UTOQ(r,q);
                    303:                c->c[i] = (Num)q;
                    304:        }
                    305:        for ( i = n; i >= 0 && !c->c[i]; i-- );
                    306:        if ( i >= 0 )
                    307:                c->d = i;
                    308:        else
                    309:                *nr = 0;
                    310: }
                    311:
1.2       noro      312: void Pmaxblen(arg,rp)
                    313: NODE arg;
                    314: Q *rp;
                    315: {
                    316:        int l;
                    317:        l = maxblenp(ARG0(arg));
                    318:        STOQ(l,*rp);
                    319: }
                    320:
1.1       noro      321: void Pp_mag(arg,rp)
                    322: NODE arg;
                    323: Q *rp;
                    324: {
                    325:        int l;
                    326:        l = p_mag(ARG0(arg));
                    327:        STOQ(l,*rp);
                    328: }
                    329:
                    330: void Pord(arg,listp)
                    331: NODE arg;
                    332: LIST *listp;
                    333: {
                    334:        NODE n,tn;
                    335:        LIST l;
                    336:        VL vl,tvl,svl;
                    337:        P t;
                    338:        int i,j;
                    339:        V *va;
                    340:        V v;
                    341:
                    342:        if ( argc(arg) ) {
                    343:                asir_assert(ARG0(arg),O_LIST,"ord");
                    344:                for ( vl = 0, i = 0, n = BDY((LIST)ARG0(arg));
                    345:                        n; n = NEXT(n), i++ ) {
                    346:                        if ( !vl ) {
                    347:                                NEWVL(vl); tvl = vl;
                    348:                        } else {
                    349:                                NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    350:                        }
                    351:                        if ( !(t = (P)BDY(n)) || (OID(t) != O_P) )
                    352:                                error("ord : invalid argument");
                    353:                        VR(tvl) = VR(t);
                    354:                }
                    355:                va = (V *)ALLOCA(i*sizeof(V));
                    356:                for ( j = 0, svl = vl; j < i; j++, svl = NEXT(svl) )
                    357:                        va[j] = VR(svl);
                    358:                for ( svl = CO; svl; svl = NEXT(svl) ) {
                    359:                        v = VR(svl);
                    360:                        for ( j = 0; j < i; j++ )
                    361:                                if ( v == va[j] )
                    362:                                        break;
                    363:                        if ( j == i ) {
                    364:                                if ( !vl ) {
                    365:                                        NEWVL(vl); tvl = vl;
                    366:                                } else {
                    367:                                        NEWVL(NEXT(tvl)); tvl = NEXT(tvl);
                    368:                                }
                    369:                                VR(tvl) = v;
                    370:                        }
                    371:                }
                    372:                if ( vl )
                    373:                        NEXT(tvl) = 0;
                    374:                CO = vl;
                    375:        }
                    376:        for ( n = 0, vl = CO; vl; vl = NEXT(vl) ) {
                    377:                NEXTNODE(n,tn); MKV(VR(vl),t); BDY(tn) = (pointer)t;
                    378:        }
                    379:        NEXT(tn) = 0; MKLIST(l,n); *listp = l;
                    380: }
                    381:
                    382: void Pcoef0(arg,rp)
                    383: NODE arg;
                    384: Obj *rp;
                    385: {
                    386:        Obj t,n;
                    387:        P s;
                    388:        DCP dc;
                    389:        int id;
                    390:        V v;
                    391:        VL vl;
                    392:
                    393:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    394:                *rp = 0;
                    395:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    396:                *rp = 0;
                    397:        else if ( id == O_N )
                    398:                if ( !n )
                    399:                        *rp = t;
                    400:                else
                    401:                        *rp = 0;
                    402:        else {
                    403:                if ( argc(arg) == 3 ) {
                    404:                        if ( (v = VR((P)ARG2(arg))) != VR((P)t) ) {
                    405:                                reordvar(CO,v,&vl); reorderp(vl,CO,(P)t,&s);
                    406:                        } else
                    407:                                s = (P)t;
                    408:                        if ( VR(s) != v ) {
                    409:                                if ( n )
                    410:                                        *rp = 0;
                    411:                                else
                    412:                                        *rp = t;
                    413:                                return;
                    414:                        }
                    415:                } else
                    416:                        s = (P)t;
                    417:                for ( dc = DC(s); dc && cmpq(DEG(dc),(Q)n); dc = NEXT(dc) );
                    418:                if ( dc )
                    419:                        *rp = (Obj)COEF(dc);
                    420:                else
                    421:                        *rp = 0;
                    422:        }
                    423: }
                    424:
                    425: void Pcoef(arg,rp)
                    426: NODE arg;
                    427: Obj *rp;
                    428: {
                    429:        Obj t,n;
                    430:        P s;
                    431:        DCP dc;
                    432:        int id;
                    433:        V v;
                    434:
                    435:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    436:                *rp = 0;
                    437:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    438:                *rp = 0;
                    439:        else if ( id == O_N ) {
                    440:                if ( !n )
                    441:                        *rp = t;
                    442:                else
                    443:                        *rp = 0;
                    444:        } else {
                    445:                if ( argc(arg) == 3 ) {
                    446:                        if ( (v = VR((P)ARG2(arg))) != VR((P)t) ) {
                    447:                                getcoef(CO,(P)t,v,(Q)n,(P *)rp); return;
                    448:                        } else
                    449:                                s = (P)t;
                    450:                        if ( VR(s) != v ) {
                    451:                                if ( n )
                    452:                                        *rp = 0;
                    453:                                else
                    454:                                        *rp = t;
                    455:                                return;
                    456:                        }
                    457:                } else
                    458:                        s = (P)t;
                    459:                for ( dc = DC(s); dc && cmpq(DEG(dc),(Q)n); dc = NEXT(dc) );
                    460:                if ( dc )
                    461:                        *rp = (Obj)COEF(dc);
                    462:                else
                    463:                        *rp = 0;
                    464:        }
                    465: }
                    466:
                    467: void Pcoef_gf2n(arg,rp)
                    468: NODE arg;
                    469: Obj *rp;
                    470: {
                    471:        Obj t,n;
                    472:        int id,d;
                    473:        UP2 up2;
                    474:
                    475:        if ( !(t = (Obj)ARG0(arg)) || ((id = OID(ARG0(arg))) > O_P) )
                    476:                *rp = 0;
                    477:        else if ( (n = (Obj)ARG1(arg)) && (OID(n) > O_N) )
                    478:                *rp = 0;
                    479:        else if ( id == O_N && NID((Num)t) == N_GF2N ) {
                    480:                d = QTOS((Q)n);
                    481:                up2 = ((GF2N)t)->body;
                    482:                if ( d > degup2(up2) )
                    483:                        *rp = 0;
                    484:                else
                    485:                        *rp = (Obj)(up2->b[d/BSH]&(((unsigned long)1)<<(d%BSH))?ONE:0);
                    486:        } else
                    487:                *rp = 0;
                    488: }
                    489:
                    490: void Pdeg(arg,rp)
                    491: NODE arg;
                    492: Q *rp;
                    493: {
                    494:        Obj t,v;
                    495:        int d;
                    496:
                    497: #if 0
                    498:        if ( !(t = (Obj)ARG0(arg)) || (OID(t) != O_P) )
                    499:                *rp = 0;
                    500:        else if ( !(v = (Obj)ARG1(arg)) || (VR((P)v) != VR((P)t)) )
                    501:                *rp = 0;
                    502:        else
                    503:                *rp = (Obj)DEG(DC((P)t));
                    504: #endif
                    505:        if ( !(t = (Obj)ARG0(arg)) )
                    506:                STOQ(-1,*rp);
                    507:        else if ( OID(t) != O_P ) {
                    508:                if ( OID(t) == O_N && NID(t) == N_GF2N
                    509:                        && (v=(Obj)ARG1(arg)) && OID(v)== O_N && NID(v) == N_GF2N ) {
                    510:                        d = degup2(((GF2N)t)->body);
                    511:                        STOQ(d,*rp);
                    512:                } else
                    513:                        *rp = 0;
                    514:        } else
                    515:                degp(VR((P)ARG1(arg)),(P)ARG0(arg),rp);
                    516: }
                    517:
                    518: void Pmindeg(arg,rp)
                    519: NODE arg;
                    520: Q *rp;
                    521: {
                    522:        Obj t;
                    523:
                    524:        if ( !(t = (Obj)ARG0(arg)) || (OID(t) != O_P) )
                    525:                *rp = 0;
                    526:        else
                    527:                getmindeg(VR((P)ARG1(arg)),(P)ARG0(arg),rp);
                    528: }
                    529:
                    530: void Psetmod(arg,rp)
                    531: NODE arg;
                    532: Q *rp;
                    533: {
                    534:        if ( arg ) {
                    535:                asir_assert(ARG0(arg),O_N,"setmod");
                    536:                current_mod = QTOS((Q)ARG0(arg));
                    537:        }
                    538:        STOQ(current_mod,*rp);
                    539: }
                    540:
                    541: void Psparsemod_gf2n(arg,rp)
                    542: NODE arg;
                    543: Q *rp;
                    544: {
                    545:        int id;
                    546:
                    547:        if ( arg && current_mod_gf2n )
                    548:                current_mod_gf2n->id = ARG0(arg)?1:0;
                    549:        if ( !current_mod_gf2n )
                    550:                id = -1;
                    551:        else
                    552:                id = current_mod_gf2n->id;
                    553:        STOQ(id,*rp);
                    554: }
                    555:
                    556: void Pmultest_gf2n(arg,rp)
                    557: NODE arg;
                    558: GF2N *rp;
                    559: {
                    560:        GF2N a,b,c;
                    561:        int i;
                    562:
                    563:        a = (GF2N)ARG0(arg); b = (GF2N)ARG0(arg);
                    564:        for ( i = 0; i < 10000; i++ )
                    565:                mulgf2n(a,b,&c);
                    566:        *rp = c;
                    567: }
                    568:
                    569: void Psquaretest_gf2n(arg,rp)
                    570: NODE arg;
                    571: GF2N *rp;
                    572: {
                    573:        GF2N a,c;
                    574:        int i;
                    575:
                    576:        a = (GF2N)ARG0(arg);
                    577:        for ( i = 0; i < 10000; i++ )
                    578:                squaregf2n(a,&c);
                    579:        *rp = c;
                    580: }
                    581:
                    582: void Pinvtest_gf2n(arg,rp)
                    583: NODE arg;
                    584: GF2N *rp;
                    585: {
                    586:        GF2N a,c;
                    587:        int i;
                    588:
                    589:        a = (GF2N)ARG0(arg);
                    590:        for ( i = 0; i < 10000; i++ )
                    591:                invgf2n(a,&c);
                    592:        *rp = c;
                    593: }
                    594:
                    595: void Pbininv_gf2n(arg,rp)
                    596: NODE arg;
                    597: GF2N *rp;
                    598: {
                    599:        UP2 a,inv;
                    600:        int n;
                    601:
                    602:        a = ((GF2N)ARG0(arg))->body;
                    603:        n = QTOS((Q)ARG1(arg));
                    604:        type1_bin_invup2(a,n,&inv);
                    605:        MKGF2N(inv,*rp);
                    606: }
                    607:
                    608: void Prinvtest_gf2n(rp)
                    609: Real *rp;
                    610: {
                    611:        GF2N *a;
                    612:        GF2N c;
                    613:        double t0,t1,r;
                    614:        int i;
                    615:        double get_clock();
                    616:
                    617:        a = (GF2N *)ALLOCA(1000*sizeof(GF2N));
                    618:        for ( i = 0; i < 1000; i++ ) {
                    619:                randomgf2n(&a[i]);
                    620:        }
                    621:        t0 = get_clock();
                    622:        for ( i = 0; i < 1000; i++ )
                    623:                invgf2n(a[i],&c);
                    624:        t1 = get_clock();
                    625:        r = (t1-t0)/1000;
                    626:        MKReal(r,*rp);
                    627: }
                    628:
                    629: void Pfind_root_gf2n(arg,rp)
                    630: NODE arg;
                    631: GF2N *rp;
                    632: {
                    633:
                    634: #if 0
                    635:        UP p;
                    636:
                    637:        ptoup((P)ARG0(arg),&p);
                    638:        find_root_gf2n(p,rp);
                    639: #else
                    640:        UP2 p;
                    641:
                    642:        ptoup2((P)ARG0(arg),&p);
                    643:        find_root_up2(p,rp);
                    644: #endif
                    645: }
                    646:
                    647: void Pis_irred_gf2(arg,rp)
                    648: NODE arg;
                    649: Q *rp;
                    650: {
                    651:        UP2 t;
                    652:
                    653:        ptoup2(ARG0(arg),&t);
                    654:        *rp = irredcheckup2(t) ? ONE : 0;
                    655: }
                    656:
                    657: void Pis_irred_ddd_gf2(arg,rp)
                    658: NODE arg;
                    659: Q *rp;
                    660: {
                    661:        UP2 t;
                    662:        int ret;
                    663:
                    664:        ptoup2(ARG0(arg),&t);
                    665:        ret = irredcheck_dddup2(t);
                    666:        STOQ(ret,*rp);
                    667: }
                    668:
1.6     ! noro      669: extern int current_gfs_ext;
        !           670: extern int current_gfs_q;
        !           671: extern int current_gfs_q1;
        !           672: extern int *current_gfs_plus1;
        !           673: extern int *current_gfs_ntoi;
        !           674: extern int *current_gfs_iton;
        !           675:
1.1       noro      676: void Psetmod_ff(arg,rp)
                    677: NODE arg;
                    678: Obj *rp;
                    679: {
                    680:        int ac;
                    681:        Obj mod,defpoly;
                    682:        N n;
                    683:        UP up;
                    684:        UP2 up2;
1.6     ! noro      685:        Q q,r;
1.1       noro      686:        P p;
                    687:        NODE n0,n1;
                    688:        LIST list;
                    689:
                    690:        ac = argc(arg);
                    691:        if ( ac == 1 ) {
                    692:                mod = (Obj)ARG0(arg);
                    693:                if ( !mod )
                    694:                        error("setmod_ff : invalid argument");
                    695:                switch ( OID(mod) ) {
                    696:                        case O_N:
1.6     ! noro      697:                                if ( SGN((Q)mod) < 0 ) {
        !           698:                                        /* small finite field; primitive root representation */
        !           699:                                        current_ff = FF_GFS;
        !           700:                                        setmod_sf(BD(NM((Q)mod))[0],1);
        !           701:                                } else {
        !           702:                                        current_ff = FF_GFP;
        !           703:                                        setmod_lm(NM((Q)mod));
        !           704:                                }
        !           705:                                break;
1.1       noro      706:                        case O_P:
                    707:                                current_ff = FF_GF2N;
                    708:                                setmod_gf2n((P)mod); break;
                    709:                        default:
                    710:                                error("setmod_ff : invalid argument");
                    711:                }
                    712:        } else if ( ac == 2 ) {
                    713:                mod = (Obj)ARG1(arg);
1.6     ! noro      714:                if ( SGN((Q)mod) < 0 ) {
        !           715:                        error("setmod_ff : not implemented yet");
        !           716:                } else {
        !           717:                        current_ff = FF_GFPN;
        !           718:                        defpoly = (Obj)ARG0(arg);
        !           719:                        if ( !mod || !defpoly )
        !           720:                                error("setmod_ff : invalid argument");
        !           721:                        setmod_lm(NM((Q)mod));
        !           722:                        setmod_gfpn((P)defpoly);
        !           723:                }
1.1       noro      724:        }
                    725:        switch ( current_ff ) {
                    726:                case FF_GFP:
                    727:                        getmod_lm(&n); NTOQ(n,1,q); *rp = (Obj)q; break;
                    728:                case FF_GF2N:
                    729:                        getmod_gf2n(&up2); up2top(up2,&p); *rp = (Obj)p; break;
                    730:                case FF_GFPN:
                    731:                        getmod_lm(&n); NTOQ(n,1,q);
                    732:                        getmod_gfpn(&up); uptop(up,&p);
                    733:                        MKNODE(n1,q,0); MKNODE(n0,p,n1);
                    734:                        MKLIST(list,n0);
                    735:                        *rp = (Obj)list; break;
1.6     ! noro      736:                case FF_GFS:
        !           737:                        STOQ(current_gfs_q,q);
        !           738:                        STOQ(current_gfs_iton[1],r);
        !           739:                        n0 = mknode(3,ONE,q,r);
        !           740:                        MKLIST(list,n0);
        !           741:                        *rp = (Obj)list; break;
1.1       noro      742:                default:
                    743:                        *rp = 0; break;
                    744:        }
                    745: }
                    746:
                    747: void Pextdeg_ff(rp)
                    748: Q *rp;
                    749: {
                    750:        int d;
                    751:        UP2 up2;
                    752:        UP up;
                    753:
                    754:        switch ( current_ff ) {
                    755:                case FF_GFP:
                    756:                        *rp = ONE; break;
                    757:                case FF_GF2N:
                    758:                        getmod_gf2n(&up2); d = degup2(up2); STOQ(d,*rp); break;
                    759:                case FF_GFPN:
                    760:                        getmod_gfpn(&up); STOQ(up->d,*rp); break;
                    761:                default:
                    762:                        error("extdeg_ff : current_ff is not set");
                    763:        }
                    764: }
                    765:
                    766: void Pcharacteristic_ff(rp)
                    767: Q *rp;
                    768: {
                    769:        N lm;
                    770:
                    771:        switch ( current_ff ) {
                    772:                case FF_GFP:
                    773:                case FF_GFPN:
                    774:                        getmod_lm(&lm); NTOQ(lm,1,*rp); break;
                    775:                case FF_GF2N:
                    776:                        STOQ(2,*rp); break;
                    777:                default:
                    778:                        error("characteristic_ff : current_ff is not set");
                    779:        }
                    780: }
                    781:
                    782: void Pfield_type_ff(rp)
                    783: Q *rp;
                    784: {
                    785:        STOQ(current_ff,*rp);
                    786: }
                    787:
                    788: void Pfield_order_ff(rp)
                    789: Q *rp;
                    790: {
                    791:        N n;
                    792:
                    793:        field_order_ff(&n);
                    794:        NTOQ(n,1,*rp);
                    795: }
                    796:
                    797: void field_order_ff(order)
                    798: N *order;
                    799: {
                    800:        UP2 up2;
                    801:        UP up;
                    802:        N m;
                    803:        int d,w;
                    804:
                    805:        switch ( current_ff ) {
                    806:                case FF_GFP:
                    807:                        getmod_lm(order); break;
                    808:                case FF_GF2N:
                    809:                        getmod_gf2n(&up2); d = degup2(up2);
                    810:                        w = (d>>5)+1;
                    811:                        *order = m = NALLOC(w);
                    812:                        PL(m)=w;
                    813:                        bzero((char *)BD(m),w*sizeof(unsigned int));
                    814:                        BD(m)[d>>5] |= 1<<(d&31);
                    815:                        break;
                    816:                case FF_GFPN:
                    817:                        getmod_lm(&m);
                    818:                        getmod_gfpn(&up); pwrn(m,up->d,order); break;
                    819:                default:
                    820:                        error("field_order_ff : current_ff is not set");
                    821:        }
                    822: }
                    823:
                    824: void Prandom_ff(rp)
                    825: Obj *rp;
                    826: {
                    827:        LM l;
                    828:        GF2N g;
                    829:        GFPN p;
                    830:
                    831:        switch ( current_ff ) {
                    832:                case FF_GFP:
                    833:                        random_lm(&l); *rp = (Obj)l; break;
                    834:                case FF_GF2N:
                    835:                        randomgf2n(&g); *rp = (Obj)g; break;
                    836:                case FF_GFPN:
                    837:                        randomgfpn(&p); *rp = (Obj)p; break;
                    838:                default:
                    839:                        error("random_ff : current_ff is not set");
                    840:        }
                    841: }
                    842:
                    843: void Psimp_ff(arg,rp)
                    844: NODE arg;
                    845: Obj *rp;
                    846: {
                    847:        LM r;
                    848:        GF2N rg;
                    849:        extern lm_lazy;
                    850:
                    851:        simp_ff((Obj)ARG0(arg),rp);
                    852: }
                    853:
                    854: void simp_ff(p,rp)
                    855: Obj p;
                    856: Obj *rp;
                    857: {
                    858:        Num n;
                    859:        LM r,s;
                    860:        DCP dc,dcr0,dcr;
                    861:        GF2N rg,sg;
                    862:        GFPN rpn,spn;
1.6     ! noro      863:        GFS rs;
1.1       noro      864:        P t;
                    865:        Obj obj;
                    866:
                    867:        lm_lazy = 0;
                    868:        if ( !p )
                    869:                *rp = 0;
                    870:        else if ( OID(p) == O_N ) {
                    871:                switch ( current_ff ) {
                    872:                        case FF_GFP:
                    873:                                ptolmp((P)p,&t); simplm((LM)t,&s); *rp = (Obj)s;
                    874:                                break;
                    875:                        case FF_GF2N:
                    876:                                ptogf2n((Obj)p,&rg); simpgf2n((GF2N)rg,&sg); *rp = (Obj)sg;
                    877:                                break;
                    878:                        case FF_GFPN:
                    879:                                ntogfpn((Obj)p,&rpn); simpgfpn((GFPN)rpn,&spn); *rp = (Obj)spn;
                    880:                                break;
1.6     ! noro      881:                        case FF_GFS:
        !           882:                                if ( current_gfs_ext > 1 ) {
        !           883:                                        error("simp_ff : not implemented yet");
        !           884:                                } else {
        !           885:                                        ptomp(current_gfs_q,(P)p,&t); mqtogfs(t,&rs);
        !           886:                                        *rp = (Obj)rs;
        !           887:                                }
        !           888:                                break;
1.1       noro      889:                        default:
                    890:                                *rp = (Obj)p;
                    891:                                break;
                    892:                }
                    893:        } else if ( OID(p) == O_P ) {
                    894:                for ( dc = DC((P)p), dcr0 = 0; dc; dc = NEXT(dc) ) {
                    895:                        simp_ff((Obj)COEF(dc),&obj);
                    896:                        if ( obj ) {
                    897:                                NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = (P)obj;
                    898:                        }
                    899:                }
                    900:                if ( !dcr0 )
                    901:                        *rp = 0;
                    902:                else {
                    903:                        NEXT(dcr) = 0; MKP(VR((P)p),dcr0,t); *rp = (Obj)t;
                    904:                }
                    905:        } else
                    906:                error("simp_ff : not implemented yet");
                    907: }
                    908:
                    909: void getcoef(vl,p,v,d,r)
                    910: VL vl;
                    911: P p;
                    912: V v;
                    913: Q d;
                    914: P *r;
                    915: {
                    916:        P s,t,u,a,b,x;
                    917:        DCP dc;
                    918:        V w;
                    919:
                    920:        if ( !p )
                    921:                *r = 0;
                    922:        else if ( NUM(p) )
                    923:                *r = d ? 0 : p;
                    924:        else if ( (w=VR(p)) == v ) {
                    925:                for ( dc = DC(p); dc && cmpq(DEG(dc),d); dc = NEXT(dc) );
                    926:                *r = dc ? COEF(dc) : 0;
                    927:        } else {
                    928:                MKV(w,x);
                    929:                for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) {
                    930:                        getcoef(vl,COEF(dc),v,d,&t);
                    931:                        if ( t ) {
                    932:                                pwrp(vl,x,DEG(dc),&u); mulp(vl,t,u,&a);
                    933:                                addp(vl,s,a,&b); s = b;
                    934:                        }
                    935:                }
                    936:                *r = s;
                    937:        }
                    938: }
                    939:
                    940: void Pdeglist(arg,rp)
                    941: NODE arg;
                    942: LIST *rp;
                    943: {
                    944:        NODE d;
                    945:
                    946:        getdeglist((P)ARG0(arg),VR((P)ARG1(arg)),&d);
                    947:        MKLIST(*rp,d);
                    948: }
                    949:
                    950: void Pch_mv(arg,rp)
                    951: NODE arg;
                    952: P *rp;
                    953: {
                    954:        change_mvar(CO,(P)ARG0(arg),VR((P)ARG1(arg)),rp);
                    955: }
                    956:
                    957: void Pre_mv(arg,rp)
                    958: NODE arg;
                    959: P *rp;
                    960: {
                    961:        restore_mvar(CO,(P)ARG0(arg),VR((P)ARG1(arg)),rp);
                    962: }
                    963:
                    964: void change_mvar(vl,p,v,r)
                    965: VL vl;
                    966: P p;
                    967: V v;
                    968: P *r;
                    969: {
                    970:        Q d;
                    971:        DCP dc,dc0;
                    972:        NODE dl;
                    973:
                    974:        if ( !p || NUM(p) || (VR(p) == v) )
                    975:                *r = p;
                    976:        else {
                    977:                getdeglist(p,v,&dl);
                    978:                for ( dc0 = 0; dl; dl = NEXT(dl) ) {
                    979:                        NEXTDC(dc0,dc); DEG(dc) = d = (Q)BDY(dl);
                    980:                        getcoef(vl,p,v,d,&COEF(dc));
                    981:                }
                    982:                NEXT(dc) = 0; MKP(v,dc0,*r);
                    983:        }
                    984: }
                    985:
                    986: void restore_mvar(vl,p,v,r)
                    987: VL vl;
                    988: P p;
                    989: V v;
                    990: P *r;
                    991: {
                    992:        P s,u,a,b,x;
                    993:        DCP dc;
                    994:
                    995:        if ( !p || NUM(p) || (VR(p) != v) )
                    996:                *r = p;
                    997:        else {
                    998:                MKV(v,x);
                    999:                for ( dc = DC(p), s = 0; dc; dc = NEXT(dc) ) {
                   1000:                        pwrp(vl,x,DEG(dc),&u); mulp(vl,COEF(dc),u,&a);
                   1001:                        addp(vl,s,a,&b); s = b;
                   1002:                }
                   1003:                *r = s;
                   1004:        }
                   1005: }
                   1006:
                   1007: void getdeglist(p,v,d)
                   1008: P p;
                   1009: V v;
                   1010: NODE *d;
                   1011: {
                   1012:        NODE n,n0,d0,d1,d2;
                   1013:        DCP dc;
                   1014:
                   1015:        if ( !p || NUM(p) ) {
                   1016:                MKNODE(n,0,0); *d = n;
                   1017:        } else if ( VR(p) == v ) {
                   1018:                for ( n0 = 0, dc = DC(p); dc; dc = NEXT(dc) ) {
                   1019:                        NEXTNODE(n0,n); BDY(n) = (pointer)DEG(dc);
                   1020:                }
                   1021:                NEXT(n) = 0; *d = n0;
                   1022:        } else {
                   1023:                for ( dc = DC(p), d0 = 0; dc; dc = NEXT(dc) ) {
                   1024:                        getdeglist(COEF(dc),v,&d1); mergedeglist(d0,d1,&d2); d0 = d2;
                   1025:                }
                   1026:                *d = d0;
                   1027:        }
                   1028: }
                   1029: void Pmergelist(arg,rp)
                   1030: NODE arg;
                   1031: LIST *rp;
                   1032: {
                   1033:     NODE n;
                   1034:
                   1035:        asir_assert(ARG0(arg),O_LIST,"mergelist");
                   1036:        asir_assert(ARG1(arg),O_LIST,"mergelist");
                   1037:        mergedeglist(BDY((LIST)ARG0(arg)),BDY((LIST)ARG1(arg)),&n);
                   1038:        MKLIST(*rp,n);
                   1039: }
                   1040:
                   1041: void mergedeglist(d0,d1,dr)
                   1042: NODE d0,d1,*dr;
                   1043: {
                   1044:        NODE t0,t,dt;
                   1045:        Q d;
                   1046:        int c;
                   1047:
                   1048:        if ( !d0 )
                   1049:                *dr = d1;
                   1050:        else {
                   1051:                while ( d1 ) {
                   1052:                        dt = d1; d1 = NEXT(d1); d = (Q)BDY(dt);
                   1053:                        for ( t0 = 0, t = d0; t; t0 = t, t = NEXT(t) ) {
                   1054:                                c = cmpq(d,(Q)BDY(t));
                   1055:                                if ( !c )
                   1056:                                        break;
                   1057:                                else if ( c > 0 ) {
                   1058:                                        if ( !t0 ) {
                   1059:                                                NEXT(dt) = d0; d0 = dt;
                   1060:                                        } else {
                   1061:                                                NEXT(t0) = dt; NEXT(dt) = t;
                   1062:                                        }
                   1063:                                        break;
                   1064:                                }
                   1065:                        }
                   1066:                        if ( !t ) {
                   1067:                                NEXT(t0) = dt; *dr = d0; return;
                   1068:                        }
                   1069:                }
                   1070:                *dr = d0;
                   1071:        }
                   1072: }
                   1073:
                   1074: void Pptomp(arg,rp)
                   1075: NODE arg;
                   1076: P *rp;
                   1077: {
                   1078:        ptomp(QTOS((Q)ARG1(arg)),(P)ARG0(arg),rp);
                   1079: }
                   1080:
                   1081: void Pmptop(arg,rp)
                   1082: NODE arg;
                   1083: P *rp;
                   1084: {
                   1085:        mptop((P)ARG0(arg),rp);
                   1086: }
                   1087:
                   1088: void Pptolmp(arg,rp)
                   1089: NODE arg;
                   1090: P *rp;
                   1091: {
                   1092:        ptolmp((P)ARG0(arg),rp);
                   1093: }
                   1094:
                   1095: void Plmptop(arg,rp)
                   1096: NODE arg;
                   1097: P *rp;
                   1098: {
                   1099:        lmptop((P)ARG0(arg),rp);
1.6     ! noro     1100: }
        !          1101:
        !          1102: void Psfptop(arg,rp)
        !          1103: NODE arg;
        !          1104: P *rp;
        !          1105: {
        !          1106:        sfptop((P)ARG0(arg),rp);
1.1       noro     1107: }
                   1108:
                   1109: void Pptogf2n(arg,rp)
                   1110: NODE arg;
                   1111: GF2N *rp;
                   1112: {
                   1113:        ptogf2n((Obj)ARG0(arg),rp);
                   1114: }
                   1115:
                   1116: void Pgf2ntop(arg,rp)
                   1117: NODE arg;
                   1118: P *rp;
                   1119: {
                   1120:        extern V up2_var;
                   1121:
                   1122:        if ( argc(arg) == 2 )
                   1123:                up2_var = VR((P)ARG1(arg));
                   1124:        gf2ntop((GF2N)ARG0(arg),rp);
                   1125: }
                   1126:
                   1127: void Pgf2ntovect(arg,rp)
                   1128: NODE arg;
                   1129: VECT *rp;
                   1130: {
                   1131:        gf2ntovect((GF2N)ARG0(arg),rp);
                   1132: }
                   1133:
                   1134: void Pptogfpn(arg,rp)
                   1135: NODE arg;
                   1136: GF2N *rp;
                   1137: {
                   1138:        ptogfpn((Obj)ARG0(arg),rp);
                   1139: }
                   1140:
                   1141: void Pgfpntop(arg,rp)
                   1142: NODE arg;
                   1143: P *rp;
                   1144: {
                   1145:        extern V up_var;
                   1146:
                   1147:        if ( argc(arg) == 2 )
                   1148:                up_var = VR((P)ARG1(arg));
                   1149:        gfpntop((GFPN)ARG0(arg),rp);
                   1150: }
                   1151:
                   1152: void Pureverse(arg,rp)
                   1153: NODE arg;
                   1154: P *rp;
                   1155: {
                   1156:        UP p,r;
                   1157:
                   1158:        ptoup((P)ARG0(arg),&p);
1.3       noro     1159:        if ( argc(arg) == 1 )
                   1160:                reverseup(p,p->d,&r);
                   1161:        else
                   1162:                reverseup(p,QTOS((Q)ARG1(arg)),&r);
1.1       noro     1163:        uptop(r,rp);
                   1164: }
                   1165:
                   1166: void Putrunc(arg,rp)
                   1167: NODE arg;
                   1168: P *rp;
                   1169: {
                   1170:        UP p,r;
                   1171:
                   1172:        ptoup((P)ARG0(arg),&p);
                   1173:        truncup(p,QTOS((Q)ARG1(arg))+1,&r);
                   1174:        uptop(r,rp);
                   1175: }
                   1176:
                   1177: void Pudecomp(arg,rp)
                   1178: NODE arg;
                   1179: LIST *rp;
                   1180: {
                   1181:        P u,l;
                   1182:        UP p,up,low;
                   1183:        NODE n0,n1;
                   1184:
                   1185:        ptoup((P)ARG0(arg),&p);
                   1186:        decompup(p,QTOS((Q)ARG1(arg))+1,&low,&up);
                   1187:        uptop(low,&l);
                   1188:        uptop(up,&u);
                   1189:        MKNODE(n1,u,0); MKNODE(n0,l,n1);
                   1190:        MKLIST(*rp,n0);
                   1191: }
                   1192:
                   1193: void Purembymul(arg,rp)
                   1194: NODE arg;
                   1195: P *rp;
                   1196: {
                   1197:        UP p1,p2,r;
                   1198:
                   1199:        if ( !ARG0(arg) || !ARG1(arg) )
                   1200:                *rp = 0;
                   1201:        else {
                   1202:                ptoup((P)ARG0(arg),&p1);
                   1203:                ptoup((P)ARG1(arg),&p2);
                   1204:                rembymulup(p1,p2,&r);
                   1205:                uptop(r,rp);
                   1206:        }
                   1207: }
                   1208:
                   1209: /*
                   1210:  * d1 = deg(p1), d2 = deg(p2)
                   1211:  * d1 <= 2*d2-1
                   1212:  * p2*inv = 1 mod x^d2
                   1213:  */
                   1214:
                   1215: void Purembymul_precomp(arg,rp)
                   1216: NODE arg;
                   1217: P *rp;
                   1218: {
                   1219:        UP p1,p2,inv,r;
                   1220:
                   1221:        if ( !ARG0(arg) || !ARG1(arg) )
                   1222:                *rp = 0;
                   1223:        else {
                   1224:                ptoup((P)ARG0(arg),&p1);
                   1225:                ptoup((P)ARG1(arg),&p2);
                   1226:                ptoup((P)ARG2(arg),&inv);
                   1227:                if ( p1->d >= 2*p2->d ) {
                   1228:                        error("urembymul_precomp : degree of 1st arg is too large");
                   1229: /*                     fprintf(stderr,"urembymul_precomp : degree of 1st arg is too large"); */
                   1230:                        remup(p1,p2,&r);
                   1231:                } else
                   1232:                        hybrid_rembymulup_special(current_ff,p1,p2,inv,&r);
                   1233:                uptop(r,rp);
                   1234:        }
                   1235: }
                   1236:
                   1237: void Puinvmod(arg,rp)
                   1238: NODE arg;
                   1239: P *rp;
                   1240: {
                   1241:        UP p,r;
                   1242:
                   1243:        ptoup((P)ARG0(arg),&p);
                   1244:        invmodup(p,QTOS((Q)ARG1(arg)),&r);
                   1245:        uptop(r,rp);
                   1246: }
                   1247:
                   1248: void Purevinvmod(arg,rp)
                   1249: NODE arg;
                   1250: P *rp;
                   1251: {
                   1252:        UP p,pr,r;
                   1253:
                   1254:        ptoup((P)ARG0(arg),&p);
                   1255:        reverseup(p,p->d,&pr);
                   1256:        invmodup(pr,QTOS((Q)ARG1(arg)),&r);
                   1257:        uptop(r,rp);
                   1258: }
                   1259:
                   1260: void Ppwrmod_ff(arg,rp)
                   1261: NODE arg;
                   1262: P *rp;
                   1263: {
                   1264:        UP p1,p2;
                   1265:
                   1266:        ptoup((P)ARG0(arg),&p1);
                   1267:        switch ( current_ff ) {
                   1268:                case FF_GFP:
                   1269:                        hybrid_powermodup(p1,&p2); break;
                   1270:                case FF_GF2N:
                   1271:                        powermodup_gf2n(p1,&p2); break;
                   1272:                case FF_GFPN:
                   1273:                        powermodup(p1,&p2); break;
                   1274:                default:
                   1275:                        error("pwrmod_ff : current_ff is not set");
                   1276:        }
                   1277:        uptop(p2,rp);
                   1278: }
                   1279:
                   1280: void Pgeneric_pwrmod_ff(arg,rp)
                   1281: NODE arg;
                   1282: P *rp;
                   1283: {
                   1284:        UP g,f,r;
                   1285:
                   1286:        ptoup((P)ARG0(arg),&g);
                   1287:        ptoup((P)ARG1(arg),&f);
                   1288:        switch ( current_ff ) {
                   1289:                case FF_GFP:
                   1290:                        hybrid_generic_powermodup(g,f,(Q)ARG2(arg),&r); break;
                   1291:                case FF_GF2N:
                   1292:                        generic_powermodup_gf2n(g,f,(Q)ARG2(arg),&r); break;
                   1293:                case FF_GFPN:
                   1294:                        generic_powermodup(g,f,(Q)ARG2(arg),&r); break;
                   1295:                default:
                   1296:                        error("generic_pwrmod_ff : current_ff is not set");
                   1297:        }
                   1298:        uptop(r,rp);
                   1299: }
                   1300:
                   1301: void Ppwrtab_ff(arg,rp)
                   1302: NODE arg;
                   1303: VECT *rp;
                   1304: {
                   1305:        UP f,xp;
                   1306:        UP *tab;
                   1307:        VECT r;
                   1308:        int i,d;
                   1309:
                   1310:        ptoup((P)ARG0(arg),&f);
                   1311:        ptoup((P)ARG1(arg),&xp);
                   1312:        d = f->d;
                   1313:
                   1314:        tab = (UP *)ALLOCA(d*sizeof(UP));
                   1315:        switch ( current_ff ) {
                   1316:                case FF_GFP:
                   1317:                        hybrid_powertabup(f,xp,tab); break;
                   1318:                case FF_GF2N:
                   1319:                        powertabup_gf2n(f,xp,tab); break;
                   1320:                case FF_GFPN:
                   1321:                        powertabup(f,xp,tab); break;
                   1322:                default:
                   1323:                        error("pwrtab_ff : current_ff is not set");
                   1324:        }
                   1325:        MKVECT(r,d); *rp = r;
                   1326:        for ( i = 0; i < d; i++ )
                   1327:                uptop(tab[i],(P *)&BDY(r)[i]);
                   1328: }
                   1329:
                   1330: void Pkpwrmod_lm(arg,rp)
                   1331: NODE arg;
                   1332: P *rp;
                   1333: {
                   1334:        UP p1,p2;
                   1335:
                   1336:        ptoup((P)ARG0(arg),&p1);
                   1337:        powermodup(p1,&p2);
                   1338:        uptop(p2,rp);
                   1339: }
                   1340:
                   1341: void Pkgeneric_pwrmod_lm(arg,rp)
                   1342: NODE arg;
                   1343: P *rp;
                   1344: {
                   1345:        UP g,f,r;
                   1346:
                   1347:        ptoup((P)ARG0(arg),&g);
                   1348:        ptoup((P)ARG1(arg),&f);
                   1349:        generic_powermodup(g,f,(Q)ARG2(arg),&r);
                   1350:        uptop(r,rp);
                   1351: }
                   1352:
                   1353: void Pkpwrtab_lm(arg,rp)
                   1354: NODE arg;
                   1355: VECT *rp;
                   1356: {
                   1357:        UP f,xp;
                   1358:        UP *tab;
                   1359:        VECT r;
                   1360:        int i,d;
                   1361:
                   1362:        ptoup((P)ARG0(arg),&f);
                   1363:        ptoup((P)ARG1(arg),&xp);
                   1364:        d = f->d;
                   1365:
                   1366:        tab = (UP *)ALLOCA(d*sizeof(UP));
                   1367:        powertabup(f,xp,tab);
                   1368:        MKVECT(r,d); *rp = r;
                   1369:        for ( i = 0; i < d; i++ )
                   1370:                uptop(tab[i],(P *)&BDY(r)[i]);
                   1371: }
                   1372:
                   1373: void Plazy_lm(arg,rp)
                   1374: NODE arg;
                   1375: Q *rp;
                   1376: {
                   1377:        lm_lazy = QTOS((Q)ARG0(arg));
                   1378:        *rp = 0;
                   1379: }
                   1380:
                   1381: void Pkmul(arg,rp)
                   1382: NODE arg;
                   1383: P *rp;
                   1384: {
                   1385:        P n1,n2;
                   1386:
                   1387:        n1 = (P)ARG0(arg); n2 = (P)ARG1(arg);
                   1388:        asir_assert(n1,O_P,"kmul");
                   1389:        asir_assert(n2,O_P,"kmul");
                   1390:        kmulp(CO,n1,n2,rp);
                   1391: }
                   1392:
                   1393: void Pksquare(arg,rp)
                   1394: NODE arg;
                   1395: P *rp;
                   1396: {
                   1397:        P n1;
                   1398:
                   1399:        n1 = (P)ARG0(arg);
                   1400:        asir_assert(n1,O_P,"ksquare");
                   1401:        ksquarep(CO,n1,rp);
                   1402: }
                   1403:
                   1404: void Pktmul(arg,rp)
                   1405: NODE arg;
                   1406: P *rp;
                   1407: {
                   1408:        UP p1,p2,r;
                   1409:
                   1410:        ptoup((P)ARG0(arg),&p1);
                   1411:        ptoup((P)ARG1(arg),&p2);
                   1412:        tkmulup(p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1413:        uptop(r,rp);
                   1414: }
                   1415:
                   1416: void Pumul(arg,rp)
                   1417: NODE arg;
                   1418: P *rp;
                   1419: {
                   1420:        P a1,a2;
                   1421:        UP p1,p2,r;
                   1422:
                   1423:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1424:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1425:                mulp(CO,a1,a2,rp);
                   1426:        else {
                   1427:                if ( !uzpcheck(a1) || !uzpcheck(a2) || VR(a1) != VR(a2) )
                   1428:                        error("umul : invalid argument");
                   1429:                ptoup(a1,&p1);
                   1430:                ptoup(a2,&p2);
                   1431:                hybrid_mulup(0,p1,p2,&r);
                   1432:                uptop(r,rp);
                   1433:        }
                   1434: }
                   1435:
                   1436: void Pusquare(arg,rp)
                   1437: NODE arg;
                   1438: P *rp;
                   1439: {
                   1440:        UP p1,p2,r;
                   1441:
                   1442:        ptoup((P)ARG0(arg),&p1);
                   1443:        hybrid_squareup(0,p1,&r);
                   1444:        uptop(r,rp);
                   1445: }
                   1446:
                   1447: void Putmul(arg,rp)
                   1448: NODE arg;
                   1449: P *rp;
                   1450: {
                   1451:        UP p1,p2,r;
                   1452:
                   1453:        ptoup((P)ARG0(arg),&p1);
                   1454:        ptoup((P)ARG1(arg),&p2);
                   1455:        hybrid_tmulup(0,p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1456:        uptop(r,rp);
                   1457: }
                   1458:
                   1459: void Pumul_ff(arg,rp)
                   1460: NODE arg;
                   1461: Obj *rp;
                   1462: {
                   1463:        P a1,a2;
                   1464:        UP p1,p2,r;
                   1465:        P p;
                   1466:
                   1467:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1468:        ptoup(a1,&p1);
                   1469:        ptoup(a2,&p2);
                   1470:        hybrid_mulup(current_ff,p1,p2,&r);
                   1471:        uptop(r,&p);
                   1472:        simp_ff((Obj)p,rp);
                   1473: }
                   1474:
                   1475: void Pusquare_ff(arg,rp)
                   1476: NODE arg;
                   1477: Obj *rp;
                   1478: {
                   1479:        UP p1,p2,r;
                   1480:        P p;
                   1481:
                   1482:        ptoup((P)ARG0(arg),&p1);
                   1483:        hybrid_squareup(current_ff,p1,&r);
                   1484:        uptop(r,&p);
                   1485:        simp_ff((Obj)p,rp);
                   1486: }
                   1487:
                   1488: void Putmul_ff(arg,rp)
                   1489: NODE arg;
                   1490: Obj *rp;
                   1491: {
                   1492:        UP p1,p2,r;
                   1493:        P p;
                   1494:
                   1495:        ptoup((P)ARG0(arg),&p1);
                   1496:        ptoup((P)ARG1(arg),&p2);
                   1497:        hybrid_tmulup(current_ff,p1,p2,QTOS((Q)ARG2(arg))+1,&r);
                   1498:        uptop(r,&p);
                   1499:        simp_ff((Obj)p,rp);
                   1500: }
                   1501:
                   1502: void Phfmul_lm(arg,rp)
                   1503: NODE arg;
                   1504: P *rp;
                   1505: {
                   1506:        UP p1,p2,r;
                   1507:        UP hi,lo,mid,t,s,p10,p11,p20,p21;
                   1508:        unsigned int m,d;
                   1509:        int i;
                   1510:
                   1511:        ptoup((P)ARG0(arg),&p1);
                   1512:        ptoup((P)ARG1(arg),&p2);
                   1513:        d = MAX(p1->d,p2->d);
                   1514:        for ( m = 1; m < d; m <<= 1 );
                   1515:        if ( m > d )
                   1516:                m >>= 1;
                   1517:        if ( d-m < 10000 ) {
                   1518:                decompup(p1,m,&p10,&p11); /* p1 = p11*x^m+p10 */
                   1519:                decompup(p2,m,&p20,&p21); /* p2 = p21*x^m+p20 */
                   1520:                fft_mulup_lm(p10,p20,&lo);
                   1521:                kmulup(p11,p21,&hi);
                   1522:                kmulup(p11,p20,&t); kmulup(p10,p21,&s); addup(t,s,&mid);
                   1523:                r = UPALLOC(2*d);
                   1524:                bzero((char *)COEF(r),(2*d+1)*sizeof(Num));
                   1525:                if ( lo )
                   1526:                        bcopy((char *)COEF(lo),(char *)COEF(r),(DEG(lo)+1)*sizeof(Num));
                   1527:                if ( hi )
                   1528:                        bcopy((char *)COEF(hi),(char *)(COEF(r)+2*m),(DEG(hi)+1)*sizeof(Num));
                   1529:                for ( i = 2*d; i >= 0 && !COEF(r)[i]; i-- );
                   1530:                if ( i < 0 )
                   1531:                        r = 0;
                   1532:                else {
                   1533:                        DEG(r) = i;
                   1534:                        t = UPALLOC(DEG(mid)+m); DEG(t) = DEG(mid)+m;
                   1535:                        bzero((char *)COEF(t),(DEG(mid)+m+1)*sizeof(Num));
                   1536:                        bcopy((char *)COEF(mid),(char *)(COEF(t)+m),(DEG(mid)+1)*sizeof(Num));
                   1537:                        addup(t,r,&s);
                   1538:                        r = s;
                   1539:                }
                   1540:        } else
                   1541:                fft_mulup_lm(p1,p2,&r);
                   1542:        uptop(r,rp);
                   1543: }
                   1544:
                   1545: void Pfmultest(arg,rp)
                   1546: NODE arg;
                   1547: LIST *rp;
                   1548: {
                   1549:        P p1,p2,r;
                   1550:        int d1,d2;
                   1551:        UM w1,w2,wr;
                   1552:        unsigned int *f1,*f2,*fr,*w;
                   1553:        int index,mod,root,d,maxint,i;
                   1554:        int cond;
                   1555:        Q prime;
                   1556:        NODE n0,n1;
                   1557:
                   1558:        p1 = (P)ARG0(arg); p2 = (P)ARG1(arg); index = QTOS((Q)ARG2(arg));
                   1559:        FFT_primes(index,&mod,&root,&d);
                   1560:        maxint = 1<<d;
                   1561:        d1 = UDEG(p1); d2 = UDEG(p2);
                   1562:        if ( maxint < d1+d2+1 )
                   1563:                *rp = 0;
                   1564:        else {
                   1565:                w1 = W_UMALLOC(d1); w2 = W_UMALLOC(d2);
                   1566:                wr = W_UMALLOC(d1+d2);
                   1567:                ptoum(mod,p1,w1); ptoum(mod,p2,w2);
                   1568:                f1 = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1569:                f2 = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1570:                fr = (unsigned int *)ALLOCA(maxint*sizeof(unsigned int));
                   1571:                w = (unsigned int *)ALLOCA(12*maxint*sizeof(unsigned int));
                   1572:
                   1573:                for ( i = 0; i <= d1; i++ )
                   1574:                        f1[i] = (unsigned int)w1->c[i];
                   1575:                for ( i = 0; i <= d2; i++ )
                   1576:                        f2[i] = (unsigned int)w2->c[i];
                   1577:
                   1578:                cond = FFT_pol_product(d1,f1,d2,f2,fr,index,w);
                   1579:                if ( cond )
                   1580:                        error("fmultest : ???");
                   1581:                wr->d = d1+d2;
                   1582:                for ( i = 0; i <= d1+d2; i++ )
                   1583:                        wr->c[i] = (unsigned int)fr[i];
                   1584:                umtop(VR(p1),wr,&r);
                   1585:                STOQ(mod,prime);
                   1586:                MKNODE(n1,prime,0);
                   1587:                MKNODE(n0,r,n1);
                   1588:                MKLIST(*rp,n0);
                   1589:        }
                   1590: }
                   1591:
                   1592: void Pkmulum(arg,rp)
                   1593: NODE arg;
                   1594: P *rp;
                   1595: {
                   1596:        P p1,p2;
                   1597:        int d1,d2,mod;
                   1598:        UM w1,w2,wr;
                   1599:
                   1600:        p1 = (P)ARG0(arg); p2 = (P)ARG1(arg); mod = QTOS((Q)ARG2(arg));
                   1601:        d1 = UDEG(p1); d2 = UDEG(p2);
                   1602:        w1 = W_UMALLOC(d1); w2 = W_UMALLOC(d2);
                   1603:        wr = W_UMALLOC(d1+d2);
                   1604:        ptoum(mod,p1,w1); ptoum(mod,p2,w2);
                   1605:        kmulum(mod,w1,w2,wr);
                   1606:        umtop(VR(p1),wr,rp);
                   1607: }
                   1608:
                   1609: void Pksquareum(arg,rp)
                   1610: NODE arg;
                   1611: P *rp;
                   1612: {
                   1613:        P p1;
                   1614:        int d1,mod;
                   1615:        UM w1,wr;
                   1616:
                   1617:        p1 = (P)ARG0(arg); mod = QTOS((Q)ARG1(arg));
                   1618:        d1 = UDEG(p1);
                   1619:        w1 = W_UMALLOC(d1);
                   1620:        wr = W_UMALLOC(2*d1);
                   1621:        ptoum(mod,p1,w1);
                   1622:        kmulum(mod,w1,w1,wr);
                   1623:        umtop(VR(p1),wr,rp);
                   1624: }
                   1625:
                   1626: void Ptracemod_gf2n(arg,rp)
                   1627: NODE arg;
                   1628: P *rp;
                   1629: {
                   1630:        UP g,f,r;
                   1631:
                   1632:        ptoup((P)ARG0(arg),&g);
                   1633:        ptoup((P)ARG1(arg),&f);
                   1634:        tracemodup_gf2n(g,f,(Q)ARG2(arg),&r);
                   1635:        uptop(r,rp);
1.2       noro     1636: }
                   1637:
                   1638: void Pumul_specialmod(arg,rp)
                   1639: NODE arg;
                   1640: P *rp;
                   1641: {
                   1642:        P a1,a2;
                   1643:        UP p1,p2,r;
                   1644:        int i,nmod;
                   1645:        int *modind;
                   1646:        NODE t,n;
                   1647:
                   1648:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1649:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1650:                mulp(CO,a1,a2,rp);
                   1651:        else {
                   1652:                if ( !uzpcheck(a1) || !uzpcheck(a2) || VR(a1) != VR(a2) )
                   1653:                        error("umul_specialmod : invalid argument");
                   1654:                ptoup(a1,&p1);
                   1655:                ptoup(a2,&p2);
                   1656:                n = BDY((LIST)ARG2(arg));
                   1657:                nmod = length(n);
                   1658:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1659:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1660:                        modind[i] = QTOS((Q)BDY(t));
                   1661:                fft_mulup_specialmod_main(p1,p2,0,modind,nmod,&r);
                   1662:                uptop(r,rp);
                   1663:        }
                   1664: }
                   1665:
                   1666: void Pusquare_specialmod(arg,rp)
                   1667: NODE arg;
                   1668: P *rp;
                   1669: {
                   1670:        P a1;
                   1671:        UP p1,r;
                   1672:        int i,nmod;
                   1673:        int *modind;
                   1674:        NODE t,n;
                   1675:
                   1676:        a1 = (P)ARG0(arg);
                   1677:        if ( !a1 || NUM(a1) )
                   1678:                mulp(CO,a1,a1,rp);
                   1679:        else {
                   1680:                if ( !uzpcheck(a1) )
                   1681:                        error("usquare_specialmod : invalid argument");
                   1682:                ptoup(a1,&p1);
                   1683:                n = BDY((LIST)ARG1(arg));
                   1684:                nmod = length(n);
                   1685:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1686:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1687:                        modind[i] = QTOS((Q)BDY(t));
                   1688:                fft_mulup_specialmod_main(p1,p1,0,modind,nmod,&r);
                   1689:                uptop(r,rp);
                   1690:        }
                   1691: }
                   1692:
                   1693: void Putmul_specialmod(arg,rp)
                   1694: NODE arg;
                   1695: P *rp;
                   1696: {
                   1697:        P a1,a2;
                   1698:        UP p1,p2,r;
                   1699:        int i,nmod;
                   1700:        int *modind;
                   1701:        NODE t,n;
                   1702:
                   1703:        a1 = (P)ARG0(arg); a2 = (P)ARG1(arg);
                   1704:        if ( !a1 || !a2 || NUM(a1) || NUM(a2) )
                   1705:                mulp(CO,a1,a2,rp);
                   1706:        else {
                   1707:                if ( !uzpcheck(a1) || !uzpcheck(a2) || VR(a1) != VR(a2) )
                   1708:                        error("utmul_specialmod : invalid argument");
                   1709:                ptoup(a1,&p1);
                   1710:                ptoup(a2,&p2);
                   1711:                n = BDY((LIST)ARG3(arg));
                   1712:                nmod = length(n);
                   1713:                modind = (int *)MALLOC_ATOMIC(nmod*sizeof(int));
                   1714:                for ( i = 0, t = n; i < nmod; i++, t = NEXT(t) )
                   1715:                        modind[i] = QTOS((Q)BDY(t));
                   1716:                fft_mulup_specialmod_main(p1,p2,QTOS((Q)ARG2(arg))+1,modind,nmod,&r);
                   1717:                uptop(r,rp);
                   1718:        }
1.1       noro     1719: }

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