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

Annotation of OpenXM_contrib2/asir2018/engine/H.c, Revision 1.2

1.1       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.2     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2018/engine/H.c,v 1.1 2018/09/19 05:45:07 noro Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "inline.h"
                     52: #include "base.h"
                     53: #include <math.h>
                     54:
                     55: #if 1
                     56: #define Mulum mulum
                     57: #define Divum divum
                     58: #define Mulsum mulsum
                     59: #define Gcdum gcdum
                     60: #endif
                     61:
                     62: #define FCTR 0
                     63: #define SQFR 1
                     64: #define DDD 2
                     65: #define NEWDDD 3
                     66:
                     67: LUM LUMALLOC();
                     68:
                     69: void berle(int index,int count,P f,ML *listp)
                     70: {
                     71:   UM wf,wf1,wf2,wfs,gcd;
                     72:   ML flist;
                     73:   int fn,fn1,fm,m,n,fhd;
                     74:   register int i,j,inv,hd,*ptr,*ptr1;
                     75:
                     76:   n = UDEG(f);
                     77:   wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
                     78:   wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
                     79:   for ( j = 0, fn = n + 1; (j < count) && (fn > 1); ) {
                     80:     m = sprime[index++];
                     81:     if ( !remqi((Q)UCOEF(f),m) )
                     82:       continue;
                     83:     ptoum(m,f,wf); cpyum(wf,wf1);
                     84:     diffum(m,wf1,wf2); gcdum(m,wf1,wf2,gcd);
                     85:     if ( DEG(gcd) > 0 )
                     86:       continue;
                     87:     hd = COEF(wf)[n]; inv = invm(hd,m);
                     88:     for ( i = n, ptr = COEF(wf); i >= 0; i-- )
                     89:       ptr[i] = ( ptr[i] * inv ) % m;
                     90:     fn1 = berlecnt(m,wf);
                     91:     if ( fn1 < fn ) {
                     92:       fn = fn1; fm = m; fhd = hd;
                     93:       for ( i = n, ptr = COEF(wf), ptr1 = COEF(wfs); i >= 0; i-- )
                     94:         ptr1[i] = ptr[i];
                     95:     }
                     96:     j++;
                     97:   }
                     98:   DEG(wfs) = n;
                     99:   *listp = flist = MLALLOC(fn); flist->n = fn; flist->mod = fm;
                    100: /*  berlemain(fm,wfs,(UM *)flist->c); */
                    101:   if ( fm == 2 )
                    102:     berlemain(fm,wfs,(UM *)flist->c);
                    103:   else
                    104:     newddd(fm,wfs,(UM *)flist->c);
                    105:   for ( i = DEG((UM)(flist->c[0])),
                    106:     ptr = COEF((UM)(flist->c[0])),
                    107:     hd = fhd, m = fm; i >= 0; i-- )
                    108:     ptr[i] = ( ptr[i] * hd ) % m;
                    109: }
                    110:
                    111: int berlecnt(int mod,UM f)
                    112: {
                    113:   register int i,j,**c;
                    114:   int d,dr,n;
                    115:   UM w,q;
                    116:   int **almat();
                    117:
                    118:   n = DEG(f); c = almat(n,n);
                    119:   w = W_UMALLOC(mod + n); q = W_UMALLOC(mod + n);
                    120:   for ( i = 1; ( d = ( mod * i ) ) < n; i++ )
                    121:     c[d][i - 1] = 1;
                    122:   DEG(w) = d; COEF(w)[d] = 1;
                    123:   for ( j = d - 1; j >= 0; j-- )
                    124:     COEF(w)[j] = 0;
                    125:   for ( ; ( i < n ) && ( ( dr = divum(mod,w,f,q) ) != -1 ); i++ ) {
                    126:     for ( j = dr; j >= 0; j-- )
                    127:       COEF(w)[j + mod] = c[j][i - 1] = COEF(w)[j];
                    128:     for ( j = mod - 1; j >= 0; j-- )
                    129:       COEF(w)[j] = 0;
                    130:     DEG(w) = dr + mod;
                    131:   }
                    132:   for ( i = 1; i < n; i++ )
                    133:     c[i][i - 1] = ( c[i][i - 1] + mod - 1 ) % mod;
                    134:   return berlecntmain(mod,n,n-1,c);
                    135: }
                    136:
                    137: /* XXX berlecntmain should not be used for large mod */
                    138:
                    139: int berlecntmain(int mod,int n,int m,int **c)
                    140: {
                    141:   register int *p1,*p2,i,j,k,l,a;
                    142:   int *tmp,inv;
                    143:   int cfs;
                    144:
                    145:   for ( cfs = 1, j = k = 0; j < m; j++ ) {
                    146:     for ( i = k; ( n > i ) && ( c[i][j] == 0 ); i++ );
                    147:     if ( i == n ) {
                    148:       cfs++; continue;
                    149:     }
                    150:     if ( i != k ) {
                    151:       tmp = c[i]; c[i] = c[k]; c[k] = tmp;
                    152:     }
                    153:     p1 = c[k]; inv = invm((p1[j] + mod) % mod,mod);
                    154:     for ( l = j; l < m; l++ )
                    155:       p1[l] = ( p1[l] * inv ) % mod;
                    156:     for ( i = k + 1; i < n; c[i][j] = 0, i++ )
                    157:       if ( i != k && ( a = -c[i][j] ) )
                    158:         for ( l = j + 1, p2 = c[i]; l < m; l++ )
                    159:           p2[l] = (a*p1[l] + p2[l]) % mod;
                    160:     k++;
                    161:   }
                    162:   return ( cfs );
                    163: }
                    164:
                    165: UM *berlemain(int mod,UM f,UM *fp)
                    166: {
                    167:   UM wg,ws,wf,f0,gcd,q;
                    168:   int n;
                    169:   register int i;
                    170:
                    171:   n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
                    172:   if ( DEG(wg) <= 0 ) {
                    173:     f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
                    174:     return ( fp );
                    175:   }
                    176:   f0 = W_UMALLOC(n); cpyum(f,f0);
                    177:   ws = W_UMALLOC(n); wf = W_UMALLOC(n);
                    178:   q = W_UMALLOC(n); gcd = W_UMALLOC(n);
                    179:   for ( i = 0; i < mod; i++ ) {
                    180:     cpyum(f0,wf); cpyum(wg,ws);
                    181:     COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
                    182:     gcdum(mod,wf,ws,gcd);
                    183:     if ( DEG(gcd) > 0 ) {
                    184:       if ( DEG(gcd) < n ) {
                    185:         divum(mod,f0,gcd,q); f0 = q; fp = berlemain(mod,gcd,fp);
                    186:       }
                    187:       break;
                    188:     }
                    189:   }
                    190:   fp = berlemain(mod,f0,fp);
                    191:   return ( fp );
                    192: }
                    193:
                    194: void hensel(int index,int count,P f,ML *listp)
                    195: {
                    196:   register int i,j;
                    197:   int q,n,bound;
                    198:   int *p;
                    199:   int **pp;
                    200:   ML blist,clist,bqlist,cqlist,rlist;
                    201:   UM *b;
                    202:   LUM fl,tl;
                    203:   LUM *l;
                    204:
                    205:   if ( UDEG(f) == 1 ) {
                    206:     *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
                    207:     return;
                    208:   }
                    209:   berle(index,count,f,&blist);
                    210:   if ( blist->n == 1 ) {
                    211:     *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
                    212:     return;
                    213:   }
                    214:   gcdgen(f,blist,&clist); henprep(f,blist,clist,&bqlist,&cqlist);
                    215:   n = bqlist->n; q = bqlist->mod;
                    216:   bqlist->bound = cqlist->bound = bound = mignotte(q,f);
                    217:   if ( bound == 1 ) {
                    218:     *listp = rlist = MLALLOC(n);
                    219:     rlist->n = n; rlist->mod = q; rlist->bound = bound;
                    220:     for ( i = 0, b = (UM *)bqlist->c, l = (LUM *)rlist->c; i < n; i++ ) {
                    221:       tl = LUMALLOC(DEG(b[i]),1); l[i] = tl; p = COEF(b[i]);
                    222:       for ( j = 0, pp = COEF(tl); j <= DEG(tl); j++ )
                    223:           pp[j][0] = p[j];
                    224:     }
                    225:   } else {
                    226:     W_LUMALLOC((int)UDEG(f),bound,fl);
                    227:     ptolum(q,bound,f,fl); henmain(fl,bqlist,cqlist,listp);
                    228:   }
                    229: }
                    230:
                    231: void hensel2(int index,int count,P f,ML *listp)
                    232: {
                    233:   register int i,j;
                    234:   int mod,q,n,bound,dx;
                    235:   ML blist,clist,bqlist,cqlist,rlist;
                    236:   UM fm,qfm,gm,qgm,hm,qhm,qam,qbm,w;
                    237:   UM *b;
                    238:   LUM fl,tl;
                    239:   int k;
                    240:
                    241:   dx = UDEG(f);
                    242:   if ( dx == 1 ) {
                    243:     *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
                    244:     return;
                    245:   }
                    246:   berle(index,count,f,&blist);
                    247:   n = blist->n;
                    248:   mod = blist->mod;
                    249:
                    250:   if ( n == 1 ) {
                    251:     *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
                    252:     return;
                    253:   }
                    254:
                    255:   /* find k s.t. mod^k <= 2^27 < mod^(k+1); set q = mod^k */
                    256:   for ( k = 1, q = mod; q <= ((1<<27)/mod); q *= mod, k++ );
                    257:
                    258:   /* mignotte bound */
                    259:   bound = mignotte(q,f);
                    260:
                    261:   *listp = rlist = MLALLOC(n);
                    262:   rlist->n = n;
                    263:   rlist->mod = q;
                    264:   rlist->bound = bound;
                    265:
                    266:   if ( bound == 1 ) {
                    267:     gcdgen(f,blist,&clist);
                    268:     henprep(f,blist,clist,&bqlist,&cqlist);
                    269:
                    270:     for ( i = 0, b = (UM *)bqlist->c; i < n; i++ ) {
                    271:       COEF(rlist)[i] = tl = LUMALLOC(DEG(b[i]),1);
                    272:       for ( j = 0; j <= DEG(tl); j++ )
                    273:           COEF(tl)[j][0] = COEF(b[i])[j];
                    274:       COEF(rlist)[i] = tl;
                    275:     }
                    276:   } else {
                    277:     /* fl = f mod q */
                    278:     fl = LUMALLOC(dx,bound);
                    279:     ptolum(q,bound,f,fl);
                    280:     /* fm = f mod mod */
                    281:     fm = W_UMALLOC(dx);
                    282:     ptoum(mod,f,fm);
                    283:     /* fm = f mod q */
                    284:     qfm = W_UMALLOC(dx);
                    285:     ptoum(q,f,qfm);
                    286:
                    287:     gm = W_UMALLOC(dx); qgm = W_UMALLOC(dx);
                    288:     hm = W_UMALLOC(dx); qhm = W_UMALLOC(dx);
                    289:     qam = W_UMALLOC(dx); qbm = W_UMALLOC(dx);
                    290:     w = W_UMALLOC(dx);
                    291:     for ( i = 0; i < n-1; i++ ) {
                    292:       cpyum(COEF(blist)[i],gm);
                    293:       cpyum(fm,w);
                    294:       divum(mod,w,gm,hm);
                    295:
                    296:       /* find am,bm s.t. qam*qgm+qbm*qhm=1 mod q, qgm=gm mod mod, qhm=hm mod mod */
                    297:       henprep2(mod,q,k,qfm,gm,hm,qgm,qhm,qam,qbm);
                    298:
                    299:       henmain2(fl,qgm,qhm,qam,qbm,q,bound,&tl);
                    300:       rlist->c[i] = (pointer)tl;
                    301:       cpyum(hm,fm);
                    302:       cpyum(qhm,qfm);
                    303:     }
                    304:     rlist->c[i] = fl;
                    305:   }
                    306: }
                    307:
                    308: /*
                    309:   f = g0*h0 mod m -> f = gk*hk mod m^(bound), f is replaced by hk
                    310: */
                    311:
                    312: void henmain2(LUM f,UM g0,UM h0,UM a0,UM b0,int m,int bound,LUM *gp)
                    313: {
                    314:   int n,dg,dh,i,k,j,dg1,dh1;
                    315:   UM wu,wr,ws,wt,q,wh1,wg1,wc,wd,we,wz;
                    316:   LUM wb0,wb1,wb2,fk,gk,hk;
                    317:
                    318:   n = DEG(f); dg = DEG(g0); dh = DEG(h0);
                    319:
                    320:   W_LUMALLOC(n,bound,wb0);
                    321:   W_LUMALLOC(n,bound,wb1);
                    322:   W_LUMALLOC(n,bound,wb2);
                    323:
                    324:   wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n);
                    325:   wr = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
                    326:   q = W_UMALLOC(2*n);
                    327:   wh1 = W_UMALLOC(2*n); wg1 = W_UMALLOC(2*n);
                    328:
                    329:   /* gk = g0 */
                    330:   gk = LUMALLOC(n,bound);
                    331:   DEG(gk) = dg;
                    332:   for ( i = 0; i <= dg; i++ )
                    333:     COEF(gk)[i][0] = COEF(g0)[i];
                    334:
                    335:   /* hk = h0 */
                    336:   W_LUMALLOC(n,bound,hk);
                    337:   DEG(hk) = dh;
                    338:   for ( i = 0; i <= dh; i++ )
                    339:     COEF(hk)[i][0] = COEF(h0)[i];
                    340:
                    341:   /* fk = gk*hk */
                    342:   W_LUMALLOC(n,bound,fk);
                    343:   mullum(m,bound,gk,hk,fk);
                    344:
                    345:   wc = W_UMALLOC(2*n); wd = W_UMALLOC(2*n);
                    346:   we = W_UMALLOC(2*n); wz = W_UMALLOC(2*n);
                    347:
                    348: #if 0
                    349:   mulum(m,a0,g0,wc);
                    350:   mulum(m,b0,h0,wd);
                    351:   addum(m,wc,wd,wz);
                    352:   if ( DEG(wz) != 0 || COEF(wz)[0] != 1 )
                    353:     error("henmain2 : cannot happen(extgcd)");
                    354: #endif
                    355:
                    356: #if 1
                    357:   fprintf(stderr,"bound=%d\n",bound);
                    358: #endif
                    359:   for ( k = 1; k < bound; k++ ) {
                    360: #if 1
                    361:     fprintf(stderr,".");
                    362: #endif
                    363:
                    364: #if defined(VISUAL) || defined(__MINGW32__)
                    365:     check_intr();
                    366: #endif
                    367:     /* at this point, f = gk*hk mod y^k */
                    368:
                    369: #if 0
                    370:     for ( j = 0; j < k; j++ )
                    371:       for ( i = 0; i <= n; i++ )
                    372:         if ( COEF(f)[i][j] != COEF(f)[i][j] )
                    373:           error("henmain2 : cannot happen(f=fk)");
                    374: #endif
                    375:
                    376:     /* wt = (f-gk*hk)/y^k */
                    377:     for ( i = 0; i <= n; i++ )
                    378:       COEF(ws)[i] = COEF(f)[i][k];
                    379:     degum(ws,n);
                    380:     for ( i = 0; i <= n; i++ )
                    381:       COEF(wu)[i] = COEF(fk)[i][k];
                    382:     degum(wu,n);
                    383:     subum(m,ws,wu,wt);
                    384:
                    385:     /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
                    386:     mulum(m,a0,wt,wh1); DEG(wh1) = divum(m,wh1,h0,q);
                    387:     mulum(m,wh1,g0,wc); subum(m,wt,wc,wd); DEG(wd) = divum(m,wd,h0,wg1);
                    388:
                    389:     /* check */
                    390: #if 0
                    391:     if ( DEG(wd) >= 0 || DEG(wg1) > dg )
                    392:       error("henmain2 : cannot happen(adj)");
                    393:
                    394:     mulum(m,wg1,h0,wc); mulum(m,wh1,g0,wd); addum(m,wc,wd,we);
                    395:     subum(m,we,wt,wz);
                    396:     if ( DEG(wz) >= 0 )
                    397:       error("henmain2 : cannot happen(coef)");
                    398: #endif
                    399:
                    400:     /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod m^bound */
                    401:
                    402:     /* wb0 = wh1*y^k */
                    403:     clearlum(n,bound,wb0);
                    404:     DEG(wb0) = dh1 = DEG(wh1);
                    405:     for ( i = 0; i <= dh1; i++ )
                    406:       COEF(wb0)[i][k] = COEF(wh1)[i];
                    407:
                    408:     /* wb2 = gk*wb0 mod y^bound */
                    409:     clearlum(n,bound,wb2);
                    410:     mullum(m,bound,gk,wb0,wb2);
                    411:
                    412:     /* fk += wb2 */
                    413:     addtolum(m,bound,wb2,fk);
                    414:
                    415:     /* wb1 = wg1*y^k */
                    416:     clearlum(n,bound,wb1);
                    417:     DEG(wb1) = dg1 = DEG(wg1);
                    418:     for ( i = 0; i <= n; i++ )
                    419:       COEF(wb1)[i][k] = COEF(wg1)[i];
                    420:
                    421:     /* wb2 = hk*wb1 mod y^bound */
                    422:     clearlum(n,bound,wb2);
                    423:     mullum(m,bound,hk,wb1,wb2);
                    424:
                    425:     /* fk += wb2 */
                    426:     addtolum(m,bound,wb2,fk);
                    427:
                    428:     /* fk += wg1*wh1*y^(2*k) mod y^bound) */
                    429:     if ( 2*k < bound ) {
                    430:       clearlum(n,bound,wb2);
                    431:       mullum(m,bound,wb0,wb1,wb2);
                    432:       addtolum(m,bound,wb2,fk);
                    433:     }
                    434:
                    435:     /* gk += wg1*y^k, hk += wh1*y^k */
                    436:     for ( i = 0; i <= DEG(wg1); i++ )
                    437:       COEF(gk)[i][k] = COEF(wg1)[i];
                    438:     for ( i = 0; i <= DEG(wh1); i++ )
                    439:       COEF(hk)[i][k] = COEF(wh1)[i];
                    440:   }
                    441: #if 1
                    442:   fprintf(stderr,"\n");
                    443: #endif
                    444:   *gp = gk;
                    445:   clearlum(n,bound,f);
                    446:   DEG(f) = dh;
                    447:   for ( i = 0; i <= dh; i++ )
                    448:     for ( j = 0; j < bound; j++ )
                    449:       COEF(f)[i][j] = COEF(hk)[i][j];
                    450: }
                    451:
                    452: void clearlum(int n,int bound,LUM f)
                    453: {
                    454:   int i;
                    455:
                    456:   for ( i = 0; i <= n; i++ )
                    457:     bzero(COEF(f)[i],bound*sizeof(int));
                    458: }
                    459:
                    460: /* g += f */
                    461:
                    462: void addtolum(int m,int bound,LUM f,LUM g)
                    463: {
                    464:   int n,i;
                    465:
                    466:   n = DEG(f);
                    467:   for ( i = 0; i <= n; i++ )
                    468:     addpadic(m,bound,(unsigned int *)COEF(f)[i],(unsigned int *)COEF(g)[i]);
                    469: }
                    470:
                    471: void hsq(int index,int count,P f,int *nindex,DCP *dcp)
                    472: {
                    473:   register int i,j,k;
                    474:   register int **pp,**fpp;
                    475:   register int *px,*py;
                    476:   int **wpp;
                    477:   int n,dr,tmp,m,b,e,np,dt;
                    478:   LUM fpa,wb0,wb1,lcpa,tpa,tlum;
                    479:   struct oDUM *dct;
                    480:   UM wt,wq0,wq,wr,wm,wm0,wa,ws,wb;
                    481:   LUM *llist,*ll;
                    482:   UM *dlist,*l,*c;
                    483:   ML list,fp,cfp;
                    484:   DCP dc;
                    485:
                    486:   sqfrum(index,count,f,nindex,&dct,&fp);
                    487:   np = fp->n; m = fp->mod;
                    488:   if ( ( np == 1 ) && ( dct[0].n == 1 ) ) {
                    489:     NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
                    490:     return;
                    491:   }
                    492:   for ( i = 0, dt = 0; i < np; i++ )
                    493:     dt = MAX(DEG(dct[i].f),dt);
                    494:   b = mig(m,dt,f); fp->bound = b;
                    495:   if ( np == 1 ) {
                    496:     nthrootchk(f,dct,fp,dcp);
                    497:     return;
                    498:   }
                    499:   list = W_MLALLOC(np); list->n = np; list->mod = m; list->bound = 1;
                    500:   for ( i = 0, ll = (LUM *)list->c; i < np; i++ ) {
                    501:     W_LUMALLOC(DEG(dct[i].f),b,ll[i]);
                    502:     for ( j = 0, px = COEF(dct[i].f), pp = COEF(ll[i]);
                    503:       j <= DEG(ll[i]); j++ )
                    504:         pp[j][0] = px[j];
                    505:   }
                    506:   dtestsql(f,list,dct,&dc);
                    507:   if ( dc ) {
                    508:     *dcp = dc;
                    509:     return;
                    510:   }
                    511:   n = UDEG(f);
                    512:   W_LUMALLOC(n,b,fpa); W_LUMALLOC(0,b,lcpa);
                    513:   W_LUMALLOC(n,b,wb0); W_LUMALLOC(n,b,wb1);
                    514:   W_LUMALLOC(n,b,tpa);
                    515:   wt = W_UMALLOC(n); ws = W_UMALLOC(n);
                    516:   wr = W_UMALLOC(n);
                    517:   wq = W_UMALLOC(2*n); wq0 = W_UMALLOC(n);
                    518:   wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
                    519:   wa = W_UMALLOC(2*n);
                    520:   ptolum(m,b,f,fpa); DEG(lcpa) = 0;
                    521:   for ( i = 0, pp = COEF(lcpa), fpp = COEF(fpa); i < b; i++ )
                    522:     pp[0][i] = fpp[n][i];
                    523:   gcdgen(f,fp,&cfp);
                    524:   llist = (LUM *) ALLOCA(np*sizeof(LUM));
                    525:   dlist = (UM *) ALLOCA(np*sizeof(UM));
                    526:   l = (UM *)fp->c; c = (UM *)cfp->c;
                    527:   for ( i = 0; i < np; i++ ) {
                    528:     W_LUMALLOC(DEG(l[i]),b,llist[i]);
                    529:     for ( j = DEG(l[i]), pp = COEF(llist[i]), px = COEF(l[i]); j >= 0; j-- )
                    530:       pp[j][0] = px[j];
                    531:     if ( ( e = dct[i].n ) != 1 ) {
                    532:       wb = dct[i].f;
                    533:       dlist[i] = W_UMALLOC(DEG(wb)*e); cpyum(l[i],dlist[i]);
                    534:       divum(m,dlist[i],wb,wq); DEG(dlist[i])= DEG(wq);
                    535:       for ( k = 0; k <= DEG(wq); k++ )
                    536:         COEF(dlist[i])[k] = dmb(m,COEF(wq)[k],e,&tmp);
                    537:     }
                    538:   }
                    539:   for ( i = 1; i < b; i++ ) {
                    540:     mullum(m,i+1,lcpa,llist[0],wb0);
                    541:     for ( j = 1; j < np; j++ ) {
                    542:       mullum(m,i+1,llist[j],wb0,wb1);
                    543:       tlum = wb0; wb0 = wb1; wb1 = tlum;
                    544:     }
                    545:     for ( j = n, px = COEF(wt), pp = COEF(fpa), wpp = COEF(wb0);
                    546:       j >= 0; j-- )
                    547:       px[j] = ( pp[j][i] - wpp[j][i] + m ) % m;
                    548:     degum(wt,n);
                    549:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    550:       px[j] = 0;
                    551:     for ( j = 1; j < np; j++ ) {
                    552:       mulum(m,wt,c[j],wm); dr = divum(m,wm,l[j],wq);
                    553:       for ( k = DEG(wq), px = COEF(wq0), py = COEF(wq); k >= 0; k-- )
                    554:         px[k] = ( px[k] + py[k] ) % m;
                    555:       for ( k = dr, pp = COEF(llist[j]), px = COEF(wm); k >= 0; k-- )
                    556:         pp[k][i] = px[k];
                    557:     }
                    558:     degum(wq0,n); mulum(m,wq0,l[0],wm);
                    559:     mulum(m,wt,c[0],wm0); addum(m,wm,wm0,wa);
                    560:     for ( j = DEG(wa), pp = COEF(llist[0]), px = COEF(wa); j >= 0; j-- )
                    561:       pp[j][i] = px[j];
                    562:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    563:       px[j] = 0;
                    564:     for ( j = 0; j < np; j++ )
                    565:       if ( dct[j].n == 1 )
                    566:         for ( k = 0,
                    567:           pp = COEF(llist[j]),
                    568:           wpp = COEF(((LUM *)list->c)[j]);
                    569:           k <= DEG(llist[j]); k++ )
                    570:           wpp[k][i] = pp[k][i];
                    571:       else {
                    572:         pwrlum(m,i+1,((LUM *)list->c)[j],dct[j].n,tpa);
                    573:         for ( k = 0,
                    574:           pp = COEF(llist[j]),
                    575:           wpp = COEF(tpa);
                    576:           k <= DEG(l[j]); k++ )
                    577:           COEF(wt)[k] = (pp[k][i]-wpp[k][i]+m)%m;
                    578:         degum(wt,DEG(l[j])); dr = divum(m,wt,dlist[j],ws);
                    579:         if ( dr >= 0 ) {
                    580:           *dcp = 0;
                    581:           return;
                    582:         } else
                    583:           for ( k = 0,
                    584:             pp = COEF(((LUM *)list->c)[j]);
                    585:             k <= DEG(ws); k++ )
                    586:             pp[k][i] = COEF(ws)[k];
                    587:       }
                    588:     list->bound = i+1; dtestsql(f,list,dct,&dc);
                    589:     if ( dc ) {
                    590:       *dcp = dc;
                    591:       return;
                    592:     }
                    593:   }
                    594:   *dcp = 0;
                    595: }
                    596:
                    597: void gcdgen(P f,ML blist,ML *clistp)
                    598: {
                    599:   register int i;
                    600:   int n,d,mod,np;
                    601:   UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
                    602:   UM *in,*out;
                    603:   ML clist;
                    604:
                    605:   n = UDEG(f); mod = blist->mod; np = blist->n;
                    606:   d = 2*n;
                    607:   q = W_UMALLOC(d); wf = W_UMALLOC(d);
                    608:   wm = W_UMALLOC(d); wx = W_UMALLOC(d);
                    609:   wy = W_UMALLOC(d); wu = W_UMALLOC(d);
                    610:   wv = W_UMALLOC(d); wg = W_UMALLOC(d);
                    611:   wa = W_UMALLOC(d); wb = W_UMALLOC(d);
                    612:   ptoum(mod,f,wf); DEG(wg) = 0; COEF(wg)[0] = 1;
                    613:   *clistp = clist = MLALLOC(np); clist->mod = mod; clist->n = np;
                    614:   for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
                    615:     divum(mod,wf,in[i],q); tum = wf; wf = q; q = tum;
                    616:     cpyum(wf,wx); cpyum(in[i],wy);
                    617:     eucum(mod,wx,wy,wa,wb); mulum(mod,wa,wg,wm);
                    618:     DEG(wm) = divum(mod,wm,in[i],q); out[i] = UMALLOC(DEG(wm));
                    619:     cpyum(wm,out[i]); mulum(mod,q,wf,wu);
                    620:     mulum(mod,wg,wb,wv); addum(mod,wu,wv,wg);
                    621:   }
                    622: }
                    623:
                    624: /* find a,b s.t. qa*qg+qb*qh=1 mod q, qg=g mod mod, qh=h mod mod */
                    625: /* q = mod^k */
                    626:
                    627: void henprep2(int mod,int q,int k,UM f,UM g,UM h,UM qg,UM qh,UM qa,UM qb)
                    628: {
                    629:   int n;
                    630:   UM wg,wh,wa,wb;
                    631:   ML bl,cl,bql,cql;
                    632:   P ff;
                    633:
                    634:   n = DEG(f);
                    635:   wg = W_UMALLOC(2*n); wh = W_UMALLOC(2*n);
                    636:   wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n);
                    637:   cpyum(g,wg); cpyum(h,wh);
                    638:
                    639:   /* wa*g+wb*h = 1 mod mod */
                    640:   eucum(mod,wg,wh,wa,wb);
                    641:
                    642: #if 0
                    643:   /* check */
                    644:   wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
                    645:   mulum(mod,wa,g,wt);
                    646:   mulum(mod,wb,h,ws);
                    647:   addum(mod,wt,ws,wu);
                    648:    if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
                    649:     error("henprep 1");
                    650: #endif
                    651:
                    652:   bl = MLALLOC(2); bl->n = 2; bl->mod = mod; bl->c[0] = g; bl->c[1] = h;
                    653:   cl = MLALLOC(2); cl->n = 2; cl->mod = mod; cl->c[0] = wb; cl->c[1] = wa;
                    654:   umtop(CO->v,f,&ff); /* XXX */
                    655:   henprep(ff,bl,cl,&bql,&cql); /* XXX */
                    656:
                    657:   cpyum(bql->c[0],qg); cpyum(bql->c[1],qh);
                    658:   cpyum(cql->c[0],qb); cpyum(cql->c[1],qa);
                    659:
                    660: #if 0
                    661:   /* check */
                    662:   mulum(q,qa,qg,wt);
                    663:   mulum(q,qb,qh,ws);
                    664:   addum(q,wt,ws,wu);
                    665:    if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
                    666:     error("henprep 2");
                    667: #endif
                    668: }
                    669:
                    670: /*
                    671:   henprep(f,blist,clist,&bqlist,&cqlist);
                    672:  */
                    673:
                    674: void henprep(P f,ML blist,ML clist,ML *bqlistp,ML *cqlistp)
                    675: {
                    676:   register int i,j,k,*px,*py,*pz;
                    677:   int n,pmax,dr,tmp,p,p1,mod,np,b,q;
                    678:   UM w,wm,wn,wa,wt,wq,wf,quot,tum,*in,*inc,*out,*outc;
                    679:   ML bqlist,cqlist;
                    680:
                    681:   n = UDEG(f); p = mod = blist->mod; np = blist->n;
                    682: /*  for ( b = 1, q = mod; q <= (unsigned int)(LBASE / (L)mod); q *= mod, b++ ); */
                    683:   for ( b = 1, q = mod; q <= ((1<<27) / mod); q *= mod, b++ );
                    684:   w = W_UMALLOC(n); ptoum(q,f,w);
                    685:   wm = W_UMALLOC(2*n); wn = W_UMALLOC(2*n);
                    686:   wa = W_UMALLOC(2*n); wt = W_UMALLOC(2*n);
                    687:   wq = W_UMALLOC(2*n); wf = W_UMALLOC(2*n);
                    688:   quot = W_UMALLOC(2*n);
                    689:   *bqlistp = bqlist = MLALLOC(np); *cqlistp = cqlist = MLALLOC(np);
                    690:   for ( i = 0; i < n+2; i++ )
                    691:     COEF(wq)[i] = 0;
                    692:   for ( i = 0,
                    693:       in = (UM *)blist->c, inc = (UM *)clist->c,
                    694:       out = (UM *)bqlist->c, outc = (UM *)cqlist->c;
                    695:       i < np; i++ ) {
                    696:     out[i] = C_UMALLOC(n+1); cpyum(in[i],out[i]);
                    697:     outc[i] = C_UMALLOC(n+1); cpyum(inc[i],outc[i]);
                    698:   }
                    699:   for ( pmax = 1, i = b; i > 0; i-- )
                    700:     pmax *= mod;
                    701:   for ( i = 1; i < b; i++, p = p1 ) {
                    702:     cpyum(out[0],wm);
                    703:     for ( j = 1; j < np; j++ ) {
                    704:       mulum(pmax,wm,out[j],wn);
                    705:       tum = wm; wm = wn; wn = tum;
                    706:     }
                    707:     for ( j = n, px = COEF(w), py = COEF(wm), pz = COEF(wt); j >= 0; j-- ) {
                    708:       tmp = ( ( px[j] - py[j] ) / p ) % mod;
                    709:       pz[j] = ( tmp >= 0? tmp : tmp + mod );
                    710:     }
                    711:     degum(wt,n);
                    712:     for ( j = 1; j < np; j++ ) {
                    713:       mulum(mod,wt,inc[j],wm); dr = divum(mod,wm,in[j],quot);
                    714:       for ( k = DEG(quot); k >= 0; k-- )
                    715:         COEF(wq)[k] = ( COEF(wq)[k] + COEF(quot)[k] ) % mod;
                    716:       for ( k = dr, px = COEF(out[j]), py = COEF(wm); k >= 0; k-- )
                    717:         px[k] += p * py[k];
                    718:     }
                    719:     degum(wq,n); mulum(mod,wq,in[0],wm);
                    720:     mulum(mod,wt,inc[0],wn); addum(mod,wm,wn,wa);
                    721:     for ( j = DEG(wa), px = COEF(out[0]), py = COEF(wa); j >= 0; j-- )
                    722:       px[j] += p * py[j];
                    723:     for ( j = n, px = COEF(wq); j >= 0; j-- )
                    724:       px[j] = 0;
                    725:     p1 = p * mod;
                    726:     for ( j = n, px = COEF(wt); j >= 1; j-- )
                    727:       px[j] = 0;
                    728:     px[0] = 1;
                    729:     for ( j = 0; j < np; j++ ) {
                    730:       cpyum(w,wf);
                    731:       for ( k = DEG(wf), px = COEF(wf); k >= 0; k-- )
                    732:         px[k] %= p1;
                    733:       divum(p1,wf,out[j],quot); mulum(p1,outc[j],quot,wm);
                    734:       for ( k = DEG(wm), px = COEF(wt), py = COEF(wm); k >= 0; k-- )
                    735:         px[k] = ( px[k] - py[k] ) % p1;
                    736:     }
                    737:     degum(wt,n);
                    738:     for ( j = DEG(wt), px = COEF(wt); j >= 0; j-- )
                    739:       px[j] = ((tmp=(px[j]/p)%mod)>= 0?tmp:tmp + mod);
                    740:     for ( j = 0; j < np; j++ ) {
                    741:       mulum(mod,wt,outc[j],wm); dr = divum(mod,wm,in[j],quot);
                    742:       for ( k = dr, px = COEF(outc[j]), py = COEF(wm); k >= 0; k-- )
                    743:         px[k] += p * py[k];
                    744:       degum(outc[j],MAX(DEG(outc[j]),dr));
                    745:     }
                    746:   }
                    747:   bqlist->n = cqlist->n = np;
                    748:   bqlist->mod = cqlist->mod = q;
                    749: }
                    750:
                    751: /*
                    752:   henmain(fl,bqlist,cqlist,listp)
                    753: */
                    754:
                    755: void henmain(LUM f,ML bqlist,ML cqlist,ML *listp)
                    756: {
                    757:   register int i,j,k;
                    758:   int *px,*py;
                    759:   int **pp,**pp1;
                    760:   int n,np,mod,bound,dr,tmp;
                    761:   UM wt,wq0,wq,wr,wm,wm0,wa,q;
                    762:   LUM wb0,wb1,tlum;
                    763:   UM *b,*c;
                    764:   LUM *l;
                    765:   ML list;
                    766:
                    767:   n = DEG(f); np = bqlist->n; mod = bqlist->mod; bound = bqlist->bound;
                    768:   *listp = list = MLALLOC(n);
                    769:   list->n = np; list->mod = mod; list->bound = bound;
                    770:   W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
                    771:   wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
                    772:   wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
                    773:   wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
                    774:   b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
                    775:   for ( i = 0; i < np; i++ ) {
                    776:     l[i] = LUMALLOC(DEG(b[i]),bound);
                    777:     for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
                    778:       pp[j][0] = px[j];
                    779:   }
                    780: #if 0
                    781:   fprintf(stderr,"bound=%d\n",bound);
                    782: #endif
                    783:   for ( i = 1; i < bound; i++ ) {
                    784: #if 0
                    785:     fprintf(stderr,".");
                    786: #endif
                    787: #if defined(VISUAL) || defined(__MINGW32__)
                    788:     check_intr();
                    789: #endif
                    790:     mullum(mod,i+1,l[0],l[1],wb0);
                    791:     for ( j = 2; j < np; j++ ) {
                    792:       mullum(mod,i+1,l[j],wb0,wb1);
                    793:       tlum = wb0; wb0 = wb1; wb1 = tlum;
                    794:     }
                    795:     for ( j = n, px = COEF(wt); j >= 0; j-- )
                    796:       px[j] = 0;
                    797:     for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
                    798:       tmp = ( pp[j][i] - pp1[j][i] ) % mod;
                    799:       COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
                    800:     }
                    801:     degum(wt,n);
                    802:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    803:       px[j] = 0;
                    804:     for ( j = 1; j < np; j++ ) {
                    805:       mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
                    806:       for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
                    807:         px[k] = ( px[k] + py[k] ) % mod;
                    808:       for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
                    809:         pp[k][i] = px[k];
                    810:     }
                    811:     degum(wq0,n); mulum(mod,wq0,b[0],wm);
                    812:     mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
                    813:     for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
                    814:       pp[j][i] = px[j];
                    815:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    816:       px[j] = 0;
                    817:   }
                    818: #if 0
                    819:   fprintf(stderr,"\n");
                    820: #endif
                    821: }
                    822:
                    823: /*
                    824:   henmain_incremental(fl,bqlist,cqlist,start)
                    825:     fl = bqlist[0]*... mod q^start
                    826: */
                    827:
                    828: void henmain_incremental(LUM f,LUM *bqlist,ML cqlist,
                    829:   int np, int mod, int start, int bound)
                    830: {
                    831:   register int i,j,k;
                    832:   int *px,*py;
                    833:   int **pp,**pp1;
                    834:   int n,dr,tmp;
                    835:   UM wt,wq0,wq,wr,wm,wm0,wa,q;
                    836:   LUM wb0,wb1,tlum;
                    837:   UM *b,*c;
                    838:   LUM *l;
                    839:   ML list;
                    840:
                    841:   n = DEG(f);
                    842:   W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
                    843:   wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
                    844:   wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
                    845:   wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
                    846:   c = (UM *)cqlist->c; l = bqlist;
                    847:   b = (UM *)ALLOCA(n*sizeof(UM));
                    848:   for ( i = 0; i < np; i++ ) {
                    849:     j = DEG(l[i]);
                    850:     b[i] = W_UMALLOC(j);
                    851:     DEG(b[i]) = j;
                    852:     for ( pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
                    853:       px[j] = pp[j][0];
                    854:   }
                    855: #if 0
                    856:   fprintf(stderr,"bound=%d\n",bound);
                    857: #endif
                    858:   for ( i = start; i < bound; i++ ) {
                    859: #if 0
                    860:     fprintf(stderr,".");
                    861: #endif
                    862:     mullum(mod,i+1,l[0],l[1],wb0);
                    863:     for ( j = 2; j < np; j++ ) {
                    864:       mullum(mod,i+1,l[j],wb0,wb1);
                    865:       tlum = wb0; wb0 = wb1; wb1 = tlum;
                    866:     }
                    867:     for ( j = n, px = COEF(wt); j >= 0; j-- )
                    868:       px[j] = 0;
                    869:     for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
                    870:       tmp = ( pp[j][i] - pp1[j][i] ) % mod;
                    871:       COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
                    872:     }
                    873:     degum(wt,n);
                    874:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    875:       px[j] = 0;
                    876:     for ( j = 1; j < np; j++ ) {
                    877:       mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
                    878:       for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
                    879:         px[k] = ( px[k] + py[k] ) % mod;
                    880:       for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
                    881:         pp[k][i] = px[k];
                    882:     }
                    883:     degum(wq0,n); mulum(mod,wq0,b[0],wm);
                    884:     mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
                    885:     for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
                    886:       pp[j][i] = px[j];
                    887:     for ( j = n, px = COEF(wq0); j >= 0; j-- )
                    888:       px[j] = 0;
                    889:   }
                    890: #if 0
                    891:   fprintf(stderr,"\n");
                    892: #endif
                    893: }
                    894:
                    895: static double M;
                    896: static int E;
                    897:
                    898: int mig(int q,int d,P f)
                    899: {
                    900:   mpz_t z;
                    901:   int s;
                    902:   mp_limb_t m;
                    903:   DCP dc;
                    904:
                    905:   for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
                    906:     z[0] = BDY((Z)COEF(dc))[0]; s = mpz_size(z); m = mpz_getlimbn(z,s-1);
                    907:     sqad(m,(s-1)*sizeof(mp_limb_t)*8);
                    908:   }
                    909:   if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
                    910:   z[0] = BDY((Z)COEF(DC(f)))[0]; s = mpz_size(z); m = mpz_getlimbn(z,s-1);
                    911:   M *= ((double)m)+1; E += (s-1) * sizeof(mp_limb_t)*8;
                    912:   return (int)ceil( (0.31*(E+d+1)+log10((double)M)) / log10((double)q) );
                    913: }
                    914:
                    915: int mignotte(int q,P f)
                    916: {
                    917:   return mig(q,UDEG(f),f);
                    918: }
                    919:
                    920: /* man*2^exp */
                    921:
                    922: void sqad(mp_limb_t man,int exp)
                    923: {
                    924:   int e,sqe;
                    925:   mp_limb_t t;
                    926:   double man1,d,sqm;
                    927:   int diff;
                    928:
                    929:   if ( man+1 == 0 ) {
                    930:     e = sizeof(mp_limb_t)*8; man1 = 1.0;
                    931:   } else {
                    932:     man += 1;
                    933:     for ( e = 0, t = man; t; e++, t >>= 1 );
                    934:     e--; d = (double)(1<<e);
                    935:     man1 = ((double)man)/d;
                    936:   }
                    937:   exp += e; sqm = man1 * man1; sqe = 2 * exp;
                    938:   if ( sqm >= 2.0 ) {
                    939:     sqm /= 2.0; sqe++;
                    940:   }
                    941:   diff = E - sqe;
                    942:   if ( diff > 18 )
                    943:     return;
                    944:   if ( diff < -18 ) {
                    945:     M = sqm; E = sqe;
                    946:     return;
                    947:   }
                    948:   if ( diff >= 0 )
                    949:     M += (sqm / (double)(1<<diff));
                    950:   else {
                    951:     M = ( ( M / (double)(1<<-diff)) + sqm ); E = sqe;
                    952:   }
                    953:   if ( M >= 2.0 ) {
                    954:     M /= 2.0; E++;
                    955:   }
                    956: }
                    957:
                    958: void ptolum(int q,int bound,P f,LUM fl)
                    959: {
                    960:   DCP dc;
                    961:   int i,j;
                    962:   int **pp;
                    963:   int d,br,s;
                    964:   unsigned int r;
                    965:   int *c;
                    966:   unsigned int *w;
                    967:   Z z;
                    968:
                    969:   for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
                    970:     absz((Z)COEF(dc),&z);
                    971:     d = ztonadic(q,z,&w);
1.2     ! noro      972:     c = pp[ZTOS(DEG(dc))];
1.1       noro      973:     for ( i = 0; i < d; i++ ) c[i] = w[i];
                    974:     for ( ; i < bound; i++ ) c[i] = 0;
                    975:     if ( sgnz((Z)COEF(dc)) < 0 )
                    976:       for (i = 0, br = 0; i < bound; i++ ) {
                    977:         if ( ( s = -(c[i] + br) ) < 0 ) {
                    978:           c[i] = s + q; br = 1;
                    979:         } else {
                    980:           c[i] = 0; br = 0;
                    981:         }
                    982:       }
                    983:   }
                    984: }
                    985:
                    986: void modfctrp(P p,int mod,int flag,DCP *dcp)
                    987: {
                    988:   int cm,n,i,j,k;
                    989:   DCP dc,dc0;
                    990:   P zp;
                    991:   Q c;
                    992:   Z q;
                    993:   UM mp;
                    994:   UM *tl;
                    995:   struct oDUM *udc,*udc1;
                    996:
                    997:   if ( !p ) {
                    998:     *dcp = 0; return;
                    999:   }
                   1000:   ptozp(p,1,&c,&zp);
                   1001:   if ( !INT(c) || !(cm = remqi(c,mod)) ) {
                   1002:     *dcp = 0; return;
                   1003:   }
                   1004:   mp = W_UMALLOC(UDEG(p));
                   1005:   ptoum(mod,zp,mp);
                   1006:   if ( (n = DEG(mp)) < 0 ) {
                   1007:     *dcp = 0; return;
                   1008:   } else if ( n == 0 ) {
1.2     ! noro     1009:     cm = dmar(cm,COEF(mp)[0],0,mod); STOZ(cm,q);
1.1       noro     1010:     NEWDC(dc); COEF(dc) = (P)q; DEG(dc) = ONE;
                   1011:     NEXT(dc) = 0; *dcp = dc;
                   1012:     return;
                   1013:   }
                   1014:   if ( COEF(mp)[n] != 1 ) {
                   1015:     cm = dmar(cm,COEF(mp)[n],0,mod);
                   1016:     i = invm(COEF(mp)[n],mod);
                   1017:     for ( j = 0; j <= n; j++ )
                   1018:       COEF(mp)[j] = dmar(COEF(mp)[j],i,0,mod);
                   1019:   }
                   1020:   W_CALLOC(n+1,struct oDUM,udc);
                   1021:   gensqfrum(mod,mp,udc);
                   1022:   switch ( flag ) {
                   1023:     case FCTR:
                   1024:       tl = (UM *)ALLOCA((n+1)*sizeof(UM));
                   1025:       W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
                   1026:       for ( i = 0,j = 0; udc[i].f; i++ )
                   1027:         if ( DEG(udc[i].f) == 1 ) {
                   1028:           udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
                   1029:         } else {
                   1030:           bzero((char *)tl,(n+1)*sizeof(UM));
                   1031:           berlemain(mod,udc[i].f,tl);
                   1032:           for ( k = 0; tl[k]; k++, j++ ) {
                   1033:             udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
                   1034:           }
                   1035:         }
                   1036:       udc = udc1; break;
                   1037:     case SQFR:
                   1038:       break;
                   1039:     case DDD:
                   1040:       tl = (UM *)ALLOCA((n+1)*sizeof(UM));
                   1041:       W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
                   1042:       for ( i = 0,j = 0; udc[i].f; i++ )
                   1043:         if ( DEG(udc[i].f) == 1 ) {
                   1044:           udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
                   1045:         } else {
                   1046:           bzero((char *)tl,(n+1)*sizeof(UM));
                   1047:           ddd(mod,udc[i].f,tl);
                   1048:           for ( k = 0; tl[k]; k++, j++ ) {
                   1049:             udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
                   1050:           }
                   1051:         }
                   1052:       udc = udc1; break;
                   1053:     case NEWDDD:
                   1054:       tl = (UM *)ALLOCA((n+1)*sizeof(UM));
                   1055:       W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
                   1056:       for ( i = 0,j = 0; udc[i].f; i++ )
                   1057:         if ( DEG(udc[i].f) == 1 ) {
                   1058:           udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
                   1059:         } else {
                   1060:           bzero((char *)tl,(n+1)*sizeof(UM));
                   1061:           if ( mod == 2 )
                   1062:             berlemain(mod,udc[i].f,tl);
                   1063:           else
                   1064:             newddd(mod,udc[i].f,tl);
                   1065:           for ( k = 0; tl[k]; k++, j++ ) {
                   1066:             udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
                   1067:           }
                   1068:         }
                   1069:       udc = udc1; break;
                   1070:   }
1.2     ! noro     1071:   NEWDC(dc0); STOZ(cm,q); COEF(dc0) = (P)q; DEG(dc0) = ONE; dc = dc0;
1.1       noro     1072:   for ( n = 0; udc[n].f; n++ ) {
                   1073:     NEWDC(NEXT(dc)); dc = NEXT(dc);
1.2     ! noro     1074:     STOZ(udc[n].n,DEG(dc)); umtop(VR(p),udc[n].f,&COEF(dc));
1.1       noro     1075:   }
                   1076:   NEXT(dc) = 0; *dcp = dc0;
                   1077: }
                   1078:
                   1079: void gensqfrum(int mod,UM p,struct oDUM *dc)
                   1080: {
                   1081:   int n,i,j,d;
                   1082:   UM t,s,g,f,f1,b;
                   1083:
                   1084:   if ( (n = DEG(p)) == 1 ) {
                   1085:     dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
                   1086:     return;
                   1087:   }
                   1088:   t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
                   1089:   f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
                   1090:   diffum(mod,p,t); cpyum(p,s); Gcdum(mod,t,s,g);
                   1091:   if ( !DEG(g) ) {
                   1092:     dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
                   1093:     return;
                   1094:   }
                   1095:   cpyum(p,b); cpyum(p,t); Divum(mod,t,g,f);
                   1096:   for ( i = 0, d = 0; DEG(f); i++ ) {
                   1097:     while ( 1 ) {
                   1098:       cpyum(b,t);
                   1099:       if ( Divum(mod,t,f,s) >= 0 )
                   1100:         break;
                   1101:       else {
                   1102:         cpyum(s,b); d++;
                   1103:       }
                   1104:     }
                   1105:     cpyum(b,t); cpyum(f,s); Gcdum(mod,t,s,f1);
                   1106:     Divum(mod,f,f1,s); cpyum(f1,f);
                   1107:     dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
                   1108:   }
                   1109:   if ( DEG(b) > 0 ) {
                   1110:     d = 1;
                   1111:     while ( 1 ) {
                   1112:       cpyum(b,t);
                   1113:       for ( j = DEG(t); j >= 0; j-- )
                   1114:         if ( COEF(t)[j] && (j % mod) )
                   1115:           break;
                   1116:       if ( j >= 0 )
                   1117:         break;
                   1118:       else {
                   1119:         DEG(s) = DEG(t)/mod;
                   1120:         for ( j = 0; j <= DEG(t); j++ )
                   1121:           COEF(s)[j] = COEF(t)[j*mod];
                   1122:         cpyum(s,b); d *= mod;
                   1123:       }
                   1124:     }
                   1125:     gensqfrum(mod,b,dc+i);
                   1126:     for ( j = i; dc[j].f; j++ )
                   1127:       dc[j].n *= d;
                   1128:   }
                   1129: }
                   1130:
                   1131: #if 0
                   1132: void srchum(int mod,UM p1,UM p2,UM gr)
                   1133: {
                   1134:   UM m,m1,m2,q,r,t,g1,g2;
                   1135:   int lc,d,d1,d2,i,j,k,l,l1,l2,l3,tmp,adj;
                   1136:   V v;
                   1137:
                   1138:   d = MAX(DEG(p1),DEG(p2));
                   1139:   g1 = W_UMALLOC(d); g2 = W_UMALLOC(d);
                   1140:   bzero((char *)g1,(d+2)*sizeof(int)); bzero((char *)g2,(d+2)*sizeof(int));
                   1141:   if ( d == DEG(p1) ) {
                   1142:     cpyum(p1,g1); cpyum(p2,g2);
                   1143:   } else {
                   1144:     cpyum(p1,g2); cpyum(p2,g1);
                   1145:   }
                   1146:   if ( ( d1 = DEG(g1) ) > ( d2 = DEG(g2) ) ) {
                   1147:     j = d1 - 1; adj = 1;
                   1148:   } else
                   1149:     j = d2;
                   1150:   lc = 1;
                   1151:   r = W_UMALLOC(d1+d2); q = W_UMALLOC(d1+d2);
                   1152:   m1 = W_UMALLOC(d1+d2); t = W_UMALLOC(d1+d2);
                   1153:   bzero((char *)r,(d1+d2+2)*sizeof(int)); bzero((char *)q,(d1+d2+2)*sizeof(int));
                   1154:   bzero((char *)m1,(d1+d2+2)*sizeof(int)); bzero((char *)t,(d1+d2+2)*sizeof(int));
                   1155:   m = W_UMALLOC(0); bzero((char *)m,2*sizeof(int));
                   1156:   adj = pwrm(mod,COEF(g2)[DEG(g2)],DEG(g1));
                   1157:   DEG(m) = 0; COEF(m)[0] = invm(COEF(g2)[DEG(g2)],mod);
                   1158:   Mulum(mod,g2,m,r); cpyum(r,g2);
                   1159:   while ( 1 ) {
                   1160:     if ( ( k = DEG(g2) ) < 0 ) {
                   1161:       DEG(gr) = -1;
                   1162:       return;
                   1163:     }
                   1164:     if ( k == j ) {
                   1165:       if ( k == 0 ) {
                   1166:         DEG(m) = 0; COEF(m)[0] = adj;
                   1167:         Mulum(mod,g2,m,gr);
                   1168:         return;
                   1169:       } else {
                   1170:         DEG(m) = 0;
                   1171:         COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
                   1172:         Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,t);
                   1173:         DEG(m) = 0; COEF(m)[0] = dmb(mod,lc,lc,&tmp);
                   1174:         Divum(mod,r,m,q); cpyum(g2,g1); cpyum(q,g2);
                   1175:         lc = COEF(g1)[DEG(g1)]; j = k - 1;
                   1176:       }
                   1177:     } else {
                   1178:       d = j - k;
                   1179:       DEG(m) = 0; COEF(m)[0] = pwrm(mod,COEF(g2)[DEG(g2)],d);
                   1180:       Mulum(mod,g2,m,m1); l = pwrm(mod,lc,d);
                   1181:       DEG(m) = 0; COEF(m)[0] = l; Divum(mod,m1,m,t);
                   1182:       if ( k == 0 ) {
                   1183:         DEG(m) = 0; COEF(m)[0] = adj;
                   1184:         Mulum(mod,t,m,gr);
                   1185:         return;
                   1186:       } else {
                   1187:         DEG(m) = 0;
                   1188:         COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
                   1189:         Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,q);
                   1190:         l1 = dmb(mod,lc,lc,&tmp); l2 = dmb(mod,l,l1,&tmp);
                   1191:         DEG(m) = 0; COEF(m)[0] = l2;
                   1192:         Divum(mod,r,m,q); cpyum(t,g1); cpyum(q,g2);
                   1193:         if ( d % 2 )
                   1194:           for ( i = DEG(g2); i >= 0; i-- )
                   1195:             COEF(g2)[i] = ( mod - COEF(g2)[i] ) % mod;
                   1196:         lc = COEF(g1)[DEG(g1)]; j = k - 1;
                   1197:       }
                   1198:     }
                   1199:   }
                   1200: }
                   1201:
                   1202: UM *resberle(int mod,UM f,UM *fp)
                   1203: {
                   1204:   UM w,wg,ws,wf,f0,gcd,q,res;
                   1205:   int n;
                   1206:   register int i;
                   1207:
                   1208:   n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
                   1209:   if ( DEG(wg) <= 0 ) {
                   1210:     f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
                   1211:     return ( fp );
                   1212:   }
                   1213:   f0 = W_UMALLOC(n); cpyum(f,f0);
                   1214:   ws = W_UMALLOC(n); wf = W_UMALLOC(n);
                   1215:   q = W_UMALLOC(n); gcd = W_UMALLOC(n);
                   1216:   res = W_UMALLOC(2*n);
                   1217:   srchum(mod,f,wg,res);
                   1218:   for ( i = 0; i < mod; i++ ) {
                   1219:     if ( substum(mod,res,i)  )
                   1220:       continue;
                   1221:     cpyum(f0,wf); cpyum(wg,ws);
                   1222:     COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
                   1223:     Gcdum(mod,wf,ws,gcd);
                   1224:     if ( DEG(gcd) > 0 ) {
                   1225:       if ( DEG(gcd) < n ) {
                   1226:         Divum(mod,f0,gcd,q); f0 = q; fp = resberle(mod,gcd,fp);
                   1227:       }
                   1228:       break;
                   1229:     }
                   1230:   }
                   1231:   fp = resberle(mod,f0,fp);
                   1232:   return ( fp );
                   1233: }
                   1234:
                   1235: int substum(int mod,UM p,int a)
                   1236: {
                   1237:   int i,j,s;
                   1238:   int *c;
                   1239:
                   1240:   if ( DEG(p) < 0 )
                   1241:     return 0;
                   1242:   if ( DEG(p) == 0 )
                   1243:     return COEF(p)[0];
                   1244:   for ( i = DEG(p), c = COEF(p), s = c[i]; i >= 0; ) {
                   1245:     for ( j = i--; (i>=0) && !c[i]; i-- );
                   1246:     if ( i >= 0 )
                   1247:       s = (s*pwrm(mod,a,j-i)%mod+c[i])%mod;
                   1248:     else
                   1249:       s = s*pwrm(mod,a,j)%mod;
                   1250:   }
                   1251:   return s;
                   1252: }
                   1253: #endif
                   1254:
                   1255: void ddd(int mod,UM f,UM *r)
                   1256: {
                   1257:   register int i,j;
                   1258:   int d,n;
                   1259:   UM q,s,t,u,v,w,g,x,m;
                   1260:   UM *base;
                   1261:
                   1262:   n = DEG(f);
                   1263:   if ( n == 1 ) {
                   1264:     r[0] = UMALLOC(1); cpyum(f,r[0]); r[1] = 0; return;
                   1265:   }
                   1266:   base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
                   1267:   w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
                   1268:   base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
                   1269:   t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
                   1270:   pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
                   1271:   for ( i = 2; i < n; i++ ) {
                   1272:     mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
                   1273:     base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
                   1274:   }
                   1275:   v = W_UMALLOC(n); cpyum(f,v);
                   1276:   DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
                   1277:   x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
                   1278:   t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
                   1279:   for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
                   1280:     for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
                   1281:       if ( COEF(w)[i] ) {
                   1282:         Mulsum(mod,base[i],COEF(w)[i],s);
                   1283:         addum(mod,s,t,u); cpyum(u,t);
                   1284:       }
                   1285:     cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
                   1286:     if ( DEG(g) >= 1 ) {
                   1287:       canzas(mod,g,d,base,r+j); j += DEG(g)/d;
                   1288:       Divum(mod,v,g,q); cpyum(q,v);
                   1289:       DEG(w) = Divum(mod,w,v,q);
                   1290:       for ( i = 0; i < DEG(v); i++ )
                   1291:         DEG(base[i]) = Divum(mod,base[i],v,q);
                   1292:     }
                   1293:   }
                   1294:   if ( DEG(v) ) {
                   1295:     r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
                   1296:   }
                   1297:   r[j] = 0;
                   1298: }
                   1299:
                   1300: void canzas(int mod,UM f,int d,UM *base,UM *r)
                   1301: {
                   1302:   UM t,s,u,w,g,o,q;
                   1303:   Z n1,n2,n3,n4,n5,z;
                   1304:   UM *b;
                   1305:   int n,m,i;
                   1306:
                   1307:   if ( DEG(f) == d ) {
                   1308:     r[0] = UMALLOC(d); cpyum(f,r[0]);
                   1309:     return;
                   1310:   } else {
                   1311:     n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
                   1312:     for ( i = 0, m = 0; i < n; i++ )
                   1313:       m = MAX(m,DEG(base[i]));
                   1314:     q = W_UMALLOC(m);
                   1315:     for ( i = 0; i < n; i++ ) {
                   1316:       b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
                   1317:       DEG(b[i]) = Divum(mod,b[i],f,q);
                   1318:     }
                   1319:     t = W_UMALLOC(2*d);
                   1320:     s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
                   1321:     w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
                   1322:     o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1.2     ! noro     1323:     STOZ(mod,n1); STOZ(d,z); pwrz(n1,z,&n2); subz(n2,ONE,&n3);
        !          1324:     STOZ(2,n4); divz(n3,n4,&n5);
1.1       noro     1325:     while ( 1 ) {
                   1326:       randum(mod,2*d,t); spwrum0(mod,f,t,n5,s);
                   1327:       subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
                   1328:       if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
                   1329:         canzas(mod,g,d,b,r);
                   1330:         cpyum(f,w); Divum(mod,w,g,s);
                   1331:         canzas(mod,s,d,b,r+DEG(g)/d);
                   1332:         return;
                   1333:       }
                   1334:     }
                   1335:   }
                   1336: }
                   1337:
                   1338: void randum(int mod,int d,UM p)
                   1339: {
                   1340:   unsigned int n;
                   1341:   int i;
                   1342:
                   1343:   n = ((unsigned int)random()) % d; DEG(p) = n; COEF(p)[n] = 1;
                   1344:   for ( i = 0; i < (int)n; i++ )
                   1345:     COEF(p)[i] = ((unsigned int)random()) % mod;
                   1346: }
                   1347:
                   1348: void pwrmodum(int mod,UM p,int e,UM f,UM pr)
                   1349: {
                   1350:   UM wt,ws,q;
                   1351:
                   1352:   if ( e == 0 ) {
                   1353:     DEG(pr) = 0; COEF(pr)[0] = 1;
                   1354:   } else if ( DEG(p) < 0 )
                   1355:     DEG(pr) = -1;
                   1356:   else if ( e == 1 ) {
                   1357:     q = W_UMALLOC(DEG(p)); cpyum(p,pr);
                   1358:     DEG(pr) = divum(mod,pr,f,q);
                   1359:   } else if ( DEG(p) == 0 ) {
                   1360:     DEG(pr) = 0; COEF(pr)[0] = pwrm(mod,COEF(p)[0],e);
                   1361:   } else {
                   1362:     wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
                   1363:     q = W_UMALLOC(2*DEG(f));
                   1364:     pwrmodum(mod,p,e/2,f,wt);
                   1365:     if ( !(e%2) )  {
                   1366:       mulum(mod,wt,wt,pr); DEG(pr) = divum(mod,pr,f,q);
                   1367:     } else {
                   1368:       mulum(mod,wt,wt,ws); DEG(ws) = divum(mod,ws,f,q);
                   1369:       mulum(mod,ws,p,pr); DEG(pr) = divum(mod,pr,f,q);
                   1370:     }
                   1371:   }
                   1372: }
                   1373:
                   1374: void spwrum0(int mod,UM m,UM f,Z e,UM r)
                   1375: {
                   1376:   UM t,s,q;
                   1377:   Z e1,rem,two;
                   1378:
                   1379:   if ( !e ) {
                   1380:     DEG(r) = 0; COEF(r)[0] = 1;
                   1381:   } else if ( UNIZ(e) )
                   1382:     cpyum(f,r);
                   1383:   else {
1.2     ! noro     1384:     STOZ(2,two);
1.1       noro     1385:     divqrz(e,two,&e1,&rem);
                   1386:     t = W_UMALLOC(2*DEG(m)); spwrum0(mod,m,f,e1,t);
                   1387:     s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
                   1388:     Mulum(mod,t,t,s); DEG(s) = Divum(mod,s,m,q);
                   1389:     if ( rem ) {
                   1390:       Mulum(mod,s,f,t); DEG(t) = Divum(mod,t,m,q); cpyum(t,r);
                   1391:         } else
                   1392:       cpyum(s,r);
                   1393:   }
                   1394: }
                   1395:
                   1396: void mult_mod_tab(UM p,int mod,UM *tab,UM r,int d)
                   1397: {
                   1398:   UM w,w1,c;
                   1399:   int n,i;
                   1400:   int *pc;
                   1401:
                   1402:   w = W_UMALLOC(d); w1 = W_UMALLOC(d);
                   1403:   c = W_UMALLOC(1); DEG(c) = 0;
                   1404:   n = DEG(p); DEG(r) = -1;
                   1405:   for ( i = 0, pc = COEF(p); i <= n; i++ )
                   1406:     if ( pc[i] ) {
                   1407:       COEF(c)[0] = pc[i];
                   1408:       mulum(mod,tab[i],c,w);
                   1409:       addum(mod,r,w,w1);
                   1410:       cpyum(w1,r);
                   1411:     }
                   1412: }
                   1413:
                   1414: void make_qmat(UM p,int mod,UM *tab,int ***mp)
                   1415: {
                   1416:   int n,i,j;
                   1417:   int *c;
                   1418:   UM q,r;
                   1419:   int **mat;
                   1420:
                   1421:   n = DEG(p);
                   1422:   *mp = mat = almat(n,n);
                   1423:   for ( j = 0; j < n; j++ ) {
                   1424:     r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
                   1425:     cpyum(tab[j],r); DEG(r) = divum(mod,r,p,q);
                   1426:     for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
                   1427:       mat[i][j] = c[i];
                   1428:   }
                   1429:   for ( i = 0; i < n; i++ )
                   1430:     mat[i][i] = (mat[i][i]+mod-1) % mod;
                   1431: }
                   1432:
                   1433: void null_mod(int **mat,int mod,int n,int *ind)
                   1434: {
                   1435:   int i,j,l,s,h,inv;
                   1436:   int *t,*u;
                   1437:
                   1438:   bzero((char *)ind,n*sizeof(int));
                   1439:   ind[0] = 0;
                   1440:   for ( i = j = 0; j < n; i++, j++ ) {
                   1441:     for ( ; j < n; j++ ) {
                   1442:       for ( l = i; l < n; l++ )
                   1443:         if ( mat[l][j] )
                   1444:           break;
                   1445:       if ( l < n ) {
                   1446:         t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
                   1447:       } else
                   1448:         ind[j] = 1;
                   1449:     }
                   1450:     if ( j == n )
                   1451:       break;
                   1452:     inv = invm(mat[i][j],mod);
                   1453:     for ( s = j, t = mat[i]; s < n; s++ )
                   1454:       t[s] =  dmar(t[s],inv,0,mod);
                   1455:     for ( l = 0; l < n; l++ ) {
                   1456:       if ( l == i )
                   1457:         continue;
                   1458:       for ( s = j, u = mat[l], h = (mod-u[j])%mod; s < n; s++ )
                   1459:         u[s] = dmar(h,t[s],u[s],mod);
                   1460:     }
                   1461:   }
                   1462: }
                   1463:
                   1464: void null_to_sol(int **mat,int *ind,int mod,int n,UM *r)
                   1465: {
                   1466:   int i,j,k,l;
                   1467:   int *c;
                   1468:   UM w;
                   1469:
                   1470:   for ( i = 0, l = 0; i < n; i++ ) {
                   1471:     if ( !ind[i] )
                   1472:       continue;
                   1473:     w = UMALLOC(n);
                   1474:     for ( j = k = 0, c = COEF(w); j < n; j++ )
                   1475:       if ( ind[j] )
                   1476:         c[j] = 0;
                   1477:       else
                   1478:         c[j] = mat[k++][i];
                   1479:     c[i] = mod-1;
                   1480:     for ( j = n; j >= 0; j-- )
                   1481:       if ( c[j] )
                   1482:         break;
                   1483:     DEG(w) = j;
                   1484:     r[l++] = w;
                   1485:   }
                   1486: }
                   1487: /*
                   1488: make_qmat(p,mod,tab,mp)
                   1489: null_mod(mat,mod,n,ind)
                   1490: null_to_sol(mat,ind,mod,n,r)
                   1491: */
                   1492:
                   1493: void newddd(int mod,UM f,UM *r)
                   1494: {
                   1495:   register int i,j;
                   1496:   int d,n;
                   1497:   UM q,s,t,u,v,w,g,x,m;
                   1498:   UM *base;
                   1499:
                   1500:   n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
                   1501:   w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
                   1502:   base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
                   1503:   t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
                   1504:   pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
                   1505:   for ( i = 2; i < n; i++ ) {
                   1506: /*    fprintf(stderr,"i=%d\n",i); */
                   1507:     mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
                   1508:     base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
                   1509:   }
                   1510:   v = W_UMALLOC(n); cpyum(f,v);
                   1511:   DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
                   1512:   x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
                   1513:   t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
                   1514:   for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
                   1515: /*    fprintf(stderr,"d=%d\n",d); */
                   1516:     for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
                   1517:       if ( COEF(w)[i] ) {
                   1518:         Mulsum(mod,base[i],COEF(w)[i],s);
                   1519:         addum(mod,s,t,u); cpyum(u,t);
                   1520:       }
                   1521:     cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
                   1522:     if ( DEG(g) >= 1 ) {
                   1523:       berlekamp(g,mod,d,base,r+j); j += DEG(g)/d;
                   1524:       Divum(mod,v,g,q); cpyum(q,v);
                   1525:       DEG(w) = Divum(mod,w,v,q);
                   1526:       for ( i = 0; i < DEG(v); i++ )
                   1527:         DEG(base[i]) = Divum(mod,base[i],v,q);
                   1528:     }
                   1529:   }
                   1530:   if ( DEG(v) ) {
                   1531:     r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
                   1532:   }
                   1533:   r[j] = 0;
                   1534: }
                   1535:
                   1536: int nfctr_mod(UM f,int mod)
                   1537: {
                   1538:   register int i,j;
                   1539:   int d,n;
                   1540:   UM q,s,t,u,v,w,g,x,m;
                   1541:   UM *base;
                   1542:
                   1543:   n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
                   1544:   w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
                   1545:   base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
                   1546:   t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
                   1547:   pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
                   1548:   for ( i = 2; i < n; i++ ) {
                   1549: /*    fprintf(stderr,"i=%d\n",i); */
                   1550:     mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
                   1551:     base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
                   1552:   }
                   1553:   v = W_UMALLOC(n); cpyum(f,v);
                   1554:   DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
                   1555:   x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
                   1556:   t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
                   1557:   for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
                   1558: /*    fprintf(stderr,"d=%d\n",d); */
                   1559:     for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
                   1560:       if ( COEF(w)[i] ) {
                   1561:         Mulsum(mod,base[i],COEF(w)[i],s);
                   1562:         addum(mod,s,t,u); cpyum(u,t);
                   1563:       }
                   1564:     cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
                   1565:     if ( DEG(g) >= 1 ) {
                   1566:       j += DEG(g)/d;
                   1567:       Divum(mod,v,g,q); cpyum(q,v);
                   1568:       DEG(w) = Divum(mod,w,v,q);
                   1569:       for ( i = 0; i < DEG(v); i++ )
                   1570:         DEG(base[i]) = Divum(mod,base[i],v,q);
                   1571:     }
                   1572:   }
                   1573:   if ( DEG(v) ) j++;
                   1574:   return j;
                   1575: }
                   1576:
                   1577: int irred_check(UM f,int mod)
                   1578: {
                   1579:   register int i,j;
                   1580:   int d,n;
                   1581:   UM q,s,t,u,v,w,g,x,m,f1,b;
                   1582:   UM *base;
                   1583:
                   1584:   if ( (n = DEG(f)) == 1 )
                   1585:     return 1;
                   1586:   t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
                   1587:   f1 = W_UMALLOC(n); b = W_UMALLOC(n);
                   1588:   diffum(mod,f,t); cpyum(f,s); Gcdum(mod,t,s,g);
                   1589:   if ( DEG(g) )
                   1590:     return 0;
                   1591:
                   1592:   base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
                   1593:   w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
                   1594:   base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
                   1595:   t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
                   1596:   pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
                   1597:   for ( i = 2; i < n; i++ ) {
                   1598: /*    fprintf(stderr,"i=%d\n",i); */
                   1599:     mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
                   1600:     base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
                   1601:   }
                   1602:   v = W_UMALLOC(n); cpyum(f,v);
                   1603:   DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
                   1604:   x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
                   1605:   t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
                   1606:   for ( j = 0, d = 1; 2*d <= n; d++ ) {
                   1607: /*    fprintf(stderr,"d=%d\n",d); */
                   1608:     for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
                   1609:       if ( COEF(w)[i] ) {
                   1610:         Mulsum(mod,base[i],COEF(w)[i],s);
                   1611:         addum(mod,s,t,u); cpyum(u,t);
                   1612:       }
                   1613:     cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
                   1614:     if ( DEG(g) >= 1 )
                   1615:       return 0;
                   1616:   }
                   1617:   return 1;
                   1618: }
                   1619:
                   1620: int berlekamp(UM p,int mod,int df,UM *tab,UM *r)
                   1621: {
                   1622:   int n,i,j,k,nf,d,nr;
                   1623:   int **mat;
                   1624:   int *ind;
                   1625:   UM mp,w,q,gcd,w1,w2;
                   1626:   UM *u;
                   1627:   int *root;
                   1628:
                   1629:   n = DEG(p);
                   1630:   ind = ALLOCA(n*sizeof(int));
                   1631:   make_qmat(p,mod,tab,&mat);
                   1632:   null_mod(mat,mod,n,ind);
                   1633:   for ( i = 0, d = 0; i < n; i++ )
                   1634:     if ( ind[i] )
                   1635:       d++;
                   1636:   if ( d == 1 ) {
                   1637:     r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
                   1638:   }
                   1639:   u = ALLOCA(d*sizeof(UM *));
                   1640:   r[0] = UMALLOC(n); cpyum(p,r[0]);
                   1641:   null_to_sol(mat,ind,mod,n,u);
                   1642:   root = ALLOCA(d*sizeof(int));
                   1643:   w = W_UMALLOC(n); mp = W_UMALLOC(d);
                   1644:   w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
                   1645:   for ( i = 1, nf = 1; i < d; i++ ) {
                   1646:     minipoly_mod(mod,u[i],p,mp);
                   1647:     nr = find_root(mod,mp,root);
                   1648:     for ( j = 0; j < nf; j++ ) {
                   1649:       if ( DEG(r[j]) == df )
                   1650:         continue;
                   1651:       for ( k = 0; k < nr; k++ ) {
                   1652:         cpyum(u[i],w1); cpyum(r[j],w2);
                   1653:         COEF(w1)[0] = (mod-root[k]) % mod;
                   1654:         gcdum(mod,w1,w2,w);
                   1655:         if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
                   1656:           gcd = UMALLOC(DEG(w));
                   1657:           q = UMALLOC(DEG(r[j])-DEG(w));
                   1658:           cpyum(w,gcd); divum(mod,r[j],w,q);
                   1659:           r[j] = q; r[nf++] = gcd;
                   1660:         }
                   1661:         if ( nf == d )
                   1662:           return d;
                   1663:       }
                   1664:     }
                   1665:   }
                   1666:   /* NOTREACHED */
                   1667:   error("berlekamp : cannot happen");
                   1668:   return -1;
                   1669: }
                   1670:
                   1671: void minipoly_mod(int mod,UM f,UM p,UM mp)
                   1672: {
                   1673:   struct p_pair *list,*l,*l1,*lprev;
                   1674:   int n,d;
                   1675:   UM u,p0,p1,np0,np1,q,w;
                   1676:
                   1677:   list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
                   1678:   list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = 1;
                   1679:   list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
                   1680:   list->next = 0;
                   1681:   n = DEG(p); w = UMALLOC(2*n);
                   1682:   p0 = UMALLOC(2*n); cpyum(list->p0,p0);
                   1683:   p1 = UMALLOC(2*n); cpyum(list->p1,p1);
                   1684:   q = W_UMALLOC(2*n);
                   1685:   while ( 1 ) {
                   1686:     COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = 1;
                   1687:     mulum(mod,f,p1,w); DEG(w) = divum(mod,w,p,q); cpyum(w,p1);
                   1688:     np0 = UMALLOC(n); np1 = UMALLOC(n);
                   1689:     lnf_mod(mod,n,p0,p1,list,np0,np1);
                   1690:     if ( DEG(np1) < 0 ) {
                   1691:       cpyum(np0,mp); return;
                   1692:     } else {
                   1693:       l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
                   1694:       l1->p0 = np0; l1->p1 = np1;
                   1695:       for ( l = list, lprev = 0, d = DEG(np1);
                   1696:         l && (DEG(l->p1) > d); lprev = l, l = l->next );
                   1697:       if ( lprev ) {
                   1698:         lprev->next = l1; l1->next = l;
                   1699:       } else {
                   1700:         l1->next = list; list = l1;
                   1701:       }
                   1702:     }
                   1703:   }
                   1704: }
                   1705:
                   1706: void lnf_mod(int mod,int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1)
                   1707: {
                   1708:   int inv,h,d1;
                   1709:   UM t0,t1,s0,s1;
                   1710:   struct p_pair *l;
                   1711:
                   1712:   cpyum(p0,np0); cpyum(p1,np1);
                   1713:   t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
                   1714:   s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
                   1715:   for ( l = list; l; l = l->next ) {
                   1716:     d1 = DEG(np1);
                   1717:     if ( d1 == DEG(l->p1) ) {
                   1718:       inv = invm((mod-COEF(l->p1)[d1])%mod,mod);
                   1719:       h = dmar(COEF(np1)[d1],inv,0,mod);
                   1720:       mulsum(mod,l->p0,h,t0); addum(mod,np0,t0,s0); cpyum(s0,np0);
                   1721:       mulsum(mod,l->p1,h,t1); addum(mod,np1,t1,s1); cpyum(s1,np1);
                   1722:     }
                   1723:   }
                   1724: }
                   1725:
                   1726: int find_root(int mod,UM p,int *root)
                   1727: {
                   1728:   UM *r;
                   1729:   int i,j;
                   1730:
                   1731:   r = ALLOCA((DEG(p)+1)*sizeof(UM));
                   1732:   ddd(mod,p,r);
                   1733:   for ( i = 0, j = 0; r[i]; i++ )
                   1734:     if ( DEG(r[i]) == 1 )
                   1735:       root[j++] = (mod - COEF(r[i])[0]) % mod;
                   1736:   return j;
                   1737: }
                   1738:
                   1739: void showum(UM p)
                   1740: {
                   1741:   int i;
                   1742:   int *c;
                   1743:
                   1744:   for ( i = DEG(p), c = COEF(p); i >= 0; i-- )
                   1745:     if ( c[i] )
                   1746:       printf("+%dx^%d",c[i],i);
                   1747:   printf("\n");
                   1748: }
                   1749:
                   1750: void showumat(int **mat,int n)
                   1751: {
                   1752:   int i,j;
                   1753:
                   1754:   for ( i = 0; i < n; i++ ) {
                   1755:     for ( j = 0; j < n; j++ )
                   1756:       printf("%d ",mat[i][j]);
                   1757:     printf("\n");
                   1758:   }
                   1759: }

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