Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - subcyclo.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21348-d75f58f) Lines: 522 550 94.9 %
Date: 2017-11-20 06:21:05 Functions: 40 41 97.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /*************************************************************************/
      18             : /**                                                                     **/
      19             : /**              Routines for handling subgroups of (Z/nZ)^*            **/
      20             : /**              without requiring discrete logarithms.                 **/
      21             : /**                                                                     **/
      22             : /*************************************************************************/
      23             : /* Subgroups are [gen,ord,bits] where
      24             :  * gen is a vecsmall of generators
      25             :  * ord is theirs relative orders
      26             :  * bits is a bit vector of the elements, of length(n). */
      27             : 
      28             : /*The algorithm is similar to testpermutation*/
      29             : static void
      30        5523 : znstar_partial_coset_func(long n, GEN H, void (*func)(void *data,long c)
      31             :     , void *data, long d, long c)
      32             : {
      33             :   GEN gen, ord, cache;
      34             :   long i, j, card;
      35             : 
      36       11046 :   if (!d) { (*func)(data,c); return; }
      37             : 
      38        3094 :   cache = const_vecsmall(d,c);
      39        3094 :   (*func)(data,c);  /* AFTER cache: may contain gerepileupto statement */
      40        3094 :   gen = gel(H,1);
      41        3094 :   ord = gel(H,2);
      42        3094 :   card = ord[1]; for (i = 2; i <= d; i++) card *= ord[i];
      43       68677 :   for(i=1; i<card; i++)
      44             :   {
      45       65583 :     long k, m = i;
      46       65583 :     for(j=1; j<d && m%ord[j]==0 ;j++) m /= ord[j];
      47       65583 :     cache[j] = Fl_mul(cache[j],gen[j],n);
      48       65583 :     for (k=1; k<j; k++) cache[k] = cache[j];
      49       65583 :     (*func)(data, cache[j]);
      50             :   }
      51             : }
      52             : 
      53             : static void
      54        1232 : znstar_coset_func(long n, GEN H, void (*func)(void *data,long c)
      55             :     , void *data, long c)
      56             : {
      57        1232 :   znstar_partial_coset_func(n, H, func,data, lg(gel(H,1))-1, c);
      58        1232 : }
      59             : 
      60             : /* Add the element of the bitvec of the coset c modulo the subgroup of H
      61             :  * generated by the first d generators to the bitvec bits.*/
      62             : 
      63             : static void
      64        4291 : znstar_partial_coset_bits_inplace(long n, GEN H, GEN bits, long d, long c)
      65             : {
      66        4291 :   pari_sp av = avma;
      67        4291 :   znstar_partial_coset_func(n,H, (void (*)(void *,long)) &F2v_set,
      68             :       (void *) bits, d, c);
      69        4291 :   avma = av;
      70        4291 : }
      71             : 
      72             : static void
      73         616 : znstar_coset_bits_inplace(long n, GEN H, GEN bits, long c)
      74             : {
      75         616 :   znstar_partial_coset_bits_inplace(n, H, bits, lg(gel(H,1))-1, c);
      76         616 : }
      77             : 
      78             : static GEN
      79        3675 : znstar_partial_coset_bits(long n, GEN H, long d, long c)
      80             : {
      81        3675 :   GEN bits = zero_F2v(n);
      82        3675 :   znstar_partial_coset_bits_inplace(n,H,bits,d,c);
      83        3675 :   return bits;
      84             : }
      85             : 
      86             : /* Compute the bitvec of the elements of the subgroup of H generated by the
      87             :  * first d generators.*/
      88             : static GEN
      89        3675 : znstar_partial_bits(long n, GEN H, long d)
      90             : {
      91        3675 :   return znstar_partial_coset_bits(n, H, d, 1);
      92             : }
      93             : 
      94             : /* Compute the bitvec of the elements of H. */
      95             : GEN
      96           0 : znstar_bits(long n, GEN H)
      97             : {
      98           0 :   return znstar_partial_bits(n,H,lg(gel(H,1))-1);
      99             : }
     100             : 
     101             : /* Compute the subgroup of (Z/nZ)^* generated by the elements of
     102             :  * the vecsmall V */
     103             : GEN
     104        2219 : znstar_generate(long n, GEN V)
     105             : {
     106        2219 :   pari_sp av = avma;
     107        2219 :   GEN gen = cgetg(lg(V),t_VECSMALL);
     108        2219 :   GEN ord = cgetg(lg(V),t_VECSMALL), res = mkvec2(gen,ord);
     109        2219 :   GEN bits = znstar_partial_bits(n,NULL,0);
     110        2219 :   long i, r = 0;
     111        5229 :   for(i=1; i<lg(V); i++)
     112             :   {
     113        3010 :     ulong v = uel(V,i), g = v;
     114        3010 :     long o = 0;
     115        3010 :     while (!F2v_coeff(bits, (long)g)) { g = Fl_mul(g, v, (ulong)n); o++; }
     116        3010 :     if (!o) continue;
     117        1456 :     r++;
     118        1456 :     gen[r] = v;
     119        1456 :     ord[r] = o+1;
     120        1456 :     cgiv(bits); bits = znstar_partial_bits(n,res,r);
     121             :   }
     122        2219 :   setlg(gen,r+1);
     123        2219 :   setlg(ord,r+1); return gerepilecopy(av, mkvec3(gen,ord,bits));
     124             : }
     125             : 
     126             : static ulong
     127        2282 : znstar_order(GEN H) { return zv_prod(gel(H,2)); }
     128             : 
     129             : /* Return the lists of element of H.
     130             :  * This can be implemented with znstar_coset_func instead. */
     131             : GEN
     132        2030 : znstar_elts(long n, GEN H)
     133             : {
     134        2030 :   long card = znstar_order(H);
     135        2030 :   GEN gen = gel(H,1), ord = gel(H,2);
     136        2030 :   GEN sg = cgetg(1 + card, t_VECSMALL);
     137             :   long k, j, l;
     138        2030 :   sg[1] = 1;
     139        3255 :   for (j = 1, l = 1; j < lg(gen); j++)
     140             :   {
     141        1225 :     long c = l * (ord[j]-1);
     142        1225 :     for (k = 1; k <= c; k++) sg[++l] = Fl_mul(sg[k], gen[j], n);
     143             :   }
     144        2030 :   vecsmall_sort(sg); return sg;
     145             : }
     146             : 
     147             : /* Take a znstar H and n dividing the modulus of H.
     148             :  * Output H reduced to modulus n */
     149             : GEN
     150          35 : znstar_reduce_modulus(GEN H, long n)
     151             : {
     152          35 :   pari_sp ltop=avma;
     153          35 :   GEN gen=cgetg(lgcols(H),t_VECSMALL);
     154             :   long i;
     155         119 :   for(i=1; i < lg(gen); i++)
     156          84 :     gen[i] = mael(H,1,i)%n;
     157          35 :   return gerepileupto(ltop, znstar_generate(n,gen));
     158             : }
     159             : 
     160             : /* Compute conductor of H, bits = H[3] */
     161             : long
     162         658 : znstar_conductor_bits(GEN bits)
     163             : {
     164         658 :   pari_sp av = avma;
     165         658 :   long i, f = 1, cnd0 = bits[1];
     166         658 :   GEN F = factoru(cnd0), P = gel(F,1), E = gel(F,2);
     167        1575 :   for (i = lg(P)-1; i > 0; i--)
     168             :   {
     169         917 :     long p = P[i], e = E[i], cnd = cnd0;
     170        1008 :     for (  ; e >= 2; e--)
     171             :     {
     172         308 :       long q = cnd / p;
     173         308 :       if (!F2v_coeff(bits, 1 + q)) break;
     174          91 :       cnd = q;
     175             :     }
     176         917 :     if (e == 1)
     177             :     {
     178         700 :       if (p == 2) e = 0;
     179             :       else
     180             :       {
     181         427 :         long h, g = pgener_Fl(p), q = cnd / p;
     182         427 :         h = Fl_mul(g-1, Fl_inv(q % p, p), p); /* 1+h*q = g (mod p) */
     183         427 :         if (F2v_coeff(bits, 1 + h*q)) e = 0;
     184             :       }
     185             :     }
     186         917 :     if (e) f *= upowuu(p, e);
     187             :   }
     188         658 :   avma = av; return f;
     189             : }
     190             : long
     191         154 : znstar_conductor(GEN H) { return znstar_conductor_bits(gel(H,3)); }
     192             : 
     193             : /* Compute the orbits of a subgroups of Z/nZ given by a generator
     194             :  * or a set of generators given as a vector.
     195             :  */
     196             : GEN
     197         126 : znstar_cosets(long n, long phi_n, GEN H)
     198             : {
     199             :   long    k;
     200         126 :   long    c = 0;
     201         126 :   long    card   = znstar_order(H);
     202         126 :   long    index  = phi_n/card;
     203         126 :   GEN     cosets = cgetg(index+1,t_VECSMALL);
     204         126 :   pari_sp ltop = avma;
     205         126 :   GEN     bits   = zero_F2v(n);
     206         742 :   for (k = 1; k <= index; k++)
     207             :   {
     208         616 :     for (c++ ; F2v_coeff(bits,c) || ugcd(c,n)!=1; c++);
     209         616 :     cosets[k]=c;
     210         616 :     znstar_coset_bits_inplace(n, H, bits, c);
     211             :   }
     212         126 :   avma=ltop;
     213         126 :   return cosets;
     214             : }
     215             : 
     216             : 
     217             : /*************************************************************************/
     218             : /**                                                                     **/
     219             : /**                     znstar/HNF interface                            **/
     220             : /**                                                                     **/
     221             : /*************************************************************************/
     222             : 
     223             : static long
     224        1204 : mod_to_small(GEN x)
     225        1204 : { return itos(typ(x) == t_INTMOD ? gel(x,2): x); }
     226             : 
     227             : static GEN
     228         938 : vecmod_to_vecsmall(GEN x)
     229         938 : { pari_APPLY_long(mod_to_small(gel(x,i))) }
     230             : 
     231             : /* Convert a true znstar output by znstar to a `small znstar' */
     232             : GEN
     233         938 : znstar_small(GEN zn)
     234             : {
     235         938 :   GEN Z = cgetg(4,t_VEC);
     236         938 :   gel(Z,1) = icopy(gmael3(zn,3,1,1));
     237         938 :   gel(Z,2) = vec_to_vecsmall(gel(zn,2));
     238         938 :   gel(Z,3) = vecmod_to_vecsmall(gel(zn,3)); return Z;
     239             : }
     240             : 
     241             : /* Compute generators for the subgroup of (Z/nZ)* given in HNF. */
     242             : GEN
     243        2093 : znstar_hnf_generators(GEN Z, GEN M)
     244             : {
     245        2093 :   long j, h, l = lg(M);
     246        2093 :   GEN gen = cgetg(l, t_VECSMALL);
     247        2093 :   pari_sp ltop = avma;
     248        2093 :   GEN zgen = gel(Z,3);
     249        2093 :   ulong n = itou(gel(Z,1));
     250        4928 :   for (j = 1; j < l; j++)
     251             :   {
     252        2835 :     GEN Mj = gel(M,j);
     253        2835 :     gen[j] = 1;
     254        7294 :     for (h = 1; h < l; h++)
     255             :     {
     256        4459 :       ulong u = itou(gel(Mj,h));
     257        4459 :       if (!u) continue;
     258        3031 :       gen[j] = Fl_mul(uel(gen,j), Fl_powu(uel(zgen,h), u, n), n);
     259             :     }
     260             :   }
     261        2093 :   avma = ltop; return gen;
     262             : }
     263             : 
     264             : GEN
     265        2030 : znstar_hnf(GEN Z, GEN M)
     266             : {
     267        2030 :   return znstar_generate(itos(gel(Z,1)),znstar_hnf_generators(Z,M));
     268             : }
     269             : 
     270             : GEN
     271        2030 : znstar_hnf_elts(GEN Z, GEN H)
     272             : {
     273        2030 :   pari_sp ltop = avma;
     274        2030 :   GEN G = znstar_hnf(Z,H);
     275        2030 :   long n = itos(gel(Z,1));
     276        2030 :   return gerepileupto(ltop, znstar_elts(n,G));
     277             : }
     278             : 
     279             : /*************************************************************************/
     280             : /**                                                                     **/
     281             : /**                     polsubcyclo                                     **/
     282             : /**                                                                     **/
     283             : /*************************************************************************/
     284             : 
     285             : static GEN
     286         168 : gscycloconductor(GEN g, long n, long flag)
     287             : {
     288         168 :   if (flag==2) retmkvec2(gcopy(g), stoi(n));
     289         161 :   return g;
     290             : }
     291             : 
     292             : static long
     293          98 : lift_check_modulus(GEN H, long n)
     294             : {
     295             :   long h;
     296          98 :   switch(typ(H))
     297             :   {
     298             :     case t_INTMOD:
     299           7 :       if (!equalsi(n, gel(H,1)))
     300           7 :         pari_err_MODULUS("galoissubcyclo", stoi(n), gel(H,1));
     301           0 :       H = gel(H,2);
     302             :     case t_INT:
     303          91 :       h = smodis(H,n);
     304          91 :       if (ugcd(h,n) != 1) pari_err_COPRIME("galoissubcyclo", H,stoi(n));
     305          91 :       return h;
     306             :   }
     307           0 :   pari_err_TYPE("galoissubcyclo [subgroup]", H);
     308             :   return 0;/*LCOV_EXCL_LINE*/
     309             : }
     310             : 
     311             : /* Compute z^ex using the baby-step/giant-step table powz
     312             :  * with only one multiply.
     313             :  * In the modular case, the result is not reduced. */
     314             : static GEN
     315      227668 : polsubcyclo_powz(GEN powz, long ex)
     316             : {
     317      227668 :   long m = lg(gel(powz,1))-1, q = ex/m, r = ex%m; /*ex=m*q+r*/
     318      227668 :   GEN g = gmael(powz,1,r+1), G = gmael(powz,2,q+1);
     319      227668 :   return (lg(powz)==4)? mulreal(g,G): gmul(g,G);
     320             : }
     321             : 
     322             : static GEN
     323        3325 : polsubcyclo_complex_bound(pari_sp av, GEN V, long prec)
     324             : {
     325        3325 :   GEN pol = real_i(roots_to_pol(V,0));
     326        3325 :   return gerepileuptoint(av, ceil_safe(gsupnorm(pol,prec)));
     327             : }
     328             : 
     329             : /* Newton sums mod le. if le==NULL, works with complex instead */
     330             : static GEN
     331        6398 : polsubcyclo_cyclic(long n, long d, long m ,long z, long g, GEN powz, GEN le)
     332             : {
     333        6398 :   GEN V = cgetg(d+1,t_VEC);
     334        6398 :   ulong base = 1;
     335             :   long i,k;
     336             :   pari_timer ti;
     337        6398 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     338       56070 :   for (i=1; i<=d; i++, base = Fl_mul(base,z,n))
     339             :   {
     340       49672 :     pari_sp av = avma;
     341       49672 :     long ex = base;
     342       49672 :     GEN s = gen_0;
     343      248696 :     for (k=0; k<m; k++, ex = Fl_mul(ex,g,n))
     344             :     {
     345      199024 :       s = gadd(s, polsubcyclo_powz(powz,ex));
     346      199024 :       if ((k&0xff)==0) s = gerepileupto(av,s);
     347             :     }
     348       49672 :     if (le) s = modii(s, le);
     349       49672 :     gel(V,i) = gerepileupto(av, s);
     350             :   }
     351        6398 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "polsubcyclo_cyclic");
     352        6398 :   return V;
     353             : }
     354             : 
     355             : struct _subcyclo_orbits_s
     356             : {
     357             :   GEN powz;
     358             :   GEN *s;
     359             :   ulong count;
     360             :   pari_sp ltop;
     361             : };
     362             : 
     363             : static void
     364       28644 : _subcyclo_orbits(struct _subcyclo_orbits_s *data, long k)
     365             : {
     366       28644 :   GEN powz = data->powz;
     367       28644 :   GEN *s = data->s;
     368             : 
     369       28644 :   if (!data->count) data->ltop = avma;
     370       28644 :   *s = gadd(*s, polsubcyclo_powz(powz,k));
     371       28644 :   data->count++;
     372       28644 :   if ((data->count & 0xffUL) == 0) *s = gerepileupto(data->ltop, *s);
     373       28644 : }
     374             : 
     375             : /* Newton sums mod le. if le==NULL, works with complex instead */
     376             : static GEN
     377         252 : polsubcyclo_orbits(long n, GEN H, GEN O, GEN powz, GEN le)
     378             : {
     379         252 :   long i, d = lg(O);
     380         252 :   GEN V = cgetg(d,t_VEC);
     381             :   struct _subcyclo_orbits_s data;
     382         252 :   long lle = le?lg(le)*2+1: 2*lg(gmael(powz,1,2))+3;/*dvmdii uses lx+ly space*/
     383         252 :   data.powz = powz;
     384        1484 :   for(i=1; i<d; i++)
     385             :   {
     386        1232 :     GEN s = gen_0;
     387        1232 :     pari_sp av = avma;
     388        1232 :     (void)new_chunk(lle);
     389        1232 :     data.count = 0;
     390        1232 :     data.s     = &s;
     391        1232 :     znstar_coset_func(n, H, (void (*)(void *,long)) _subcyclo_orbits,
     392        1232 :       (void *) &data, O[i]);
     393        1232 :     avma = av; /* HACK */
     394        1232 :     gel(V,i) = le? modii(s,le): gcopy(s);
     395             :   }
     396         252 :   return V;
     397             : }
     398             : 
     399             : static GEN
     400        8792 : polsubcyclo_start(long n, long d, long o, long e, GEN borne, long *ptr_val,long *ptr_l)
     401             : {
     402             :   pari_sp av;
     403             :   GEN le, z, gl;
     404             :   long i, l, val;
     405        8792 :   l = e*n+1;
     406        8792 :   while(!uisprime(l)) { l += n; e++; }
     407        8792 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: prime l=%ld\n",l);
     408        8792 :   gl = utoipos(l); av = avma;
     409        8792 :   if (!borne)
     410             :   { /* Use vecmax(Vec((x+o)^d)) = max{binomial(d,i)*o^i ;1<=i<=d} */
     411           0 :     i = d-(1+d)/(1+o);
     412           0 :     borne = mulii(binomial(utoipos(d),i),powuu(o,i));
     413             :   }
     414        8792 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: bound=2^%ld\n",expi(borne));
     415        8792 :   val = logint(shifti(borne,2), gl) + 1;
     416        8792 :   avma = av;
     417        8792 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: val=%ld\n",val);
     418        8792 :   le = powiu(gl,val);
     419        8792 :   z = utoipos( Fl_powu(pgener_Fl(l), e, l) );
     420        8792 :   z = Zp_sqrtnlift(gen_1,utoipos(n),z,gl,val);
     421        8792 :   *ptr_val = val;
     422        8792 :   *ptr_l = l;
     423        8792 :   return gmodulo(z,le);
     424             : }
     425             : 
     426             : /*Fill in the powz table:
     427             :  *  powz[1]: baby-step
     428             :  *  powz[2]: giant-step
     429             :  *  powz[3] exists only if the field is real (value is ignored). */
     430             : static GEN
     431        3325 : polsubcyclo_complex_roots(long n, long real, long prec)
     432             : {
     433        3325 :   long i, m = (long)(1+sqrt((double) n));
     434        3325 :   GEN bab, gig, powz = cgetg(real?4:3, t_VEC);
     435             : 
     436        3325 :   bab = cgetg(m+1,t_VEC);
     437        3325 :   gel(bab,1) = gen_1;
     438        3325 :   gel(bab,2) = rootsof1u_cx(n, prec); /* = e_n(1) */
     439        3325 :   for (i=3; i<=m; i++) gel(bab,i) = gmul(gel(bab,2),gel(bab,i-1));
     440        3325 :   gig = cgetg(m+1,t_VEC);
     441        3325 :   gel(gig,1) = gen_1;
     442        3325 :   gel(gig,2) = gmul(gel(bab,2),gel(bab,m));;
     443        3325 :   for (i=3; i<=m; i++) gel(gig,i) = gmul(gel(gig,2),gel(gig,i-1));
     444        3325 :   gel(powz,1) = bab;
     445        3325 :   gel(powz,2) = gig;
     446        3325 :   if (real) gel(powz,3) = gen_0;
     447        3325 :   return powz;
     448             : }
     449             : 
     450             : static GEN
     451       24311 : muliimod_sz(GEN x, GEN y, GEN l, long siz)
     452             : {
     453       24311 :   pari_sp av = avma;
     454             :   GEN p1;
     455       24311 :   (void)new_chunk(siz); /* HACK */
     456       24311 :   p1 = mulii(x,y);
     457       24311 :   avma = av; return modii(p1,l);
     458             : }
     459             : 
     460             : static GEN
     461        3325 : polsubcyclo_roots(long n, GEN zl)
     462             : {
     463        3325 :   GEN le = gel(zl,1), z = gel(zl,2);
     464        3325 :   long i, lle = lg(le)*3; /*Assume dvmdii use lx+ly space*/
     465        3325 :   long m = (long)(1+sqrt((double) n));
     466        3325 :   GEN bab, gig, powz = cgetg(3,t_VEC);
     467             :   pari_timer ti;
     468        3325 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     469        3325 :   bab = cgetg(m+1,t_VEC);
     470        3325 :   gel(bab,1) = gen_1;
     471        3325 :   gel(bab,2) = icopy(z);
     472        3325 :   for (i=3; i<=m; i++) gel(bab,i) = muliimod_sz(z,gel(bab,i-1),le,lle);
     473        3325 :   gig = cgetg(m+1,t_VEC);
     474        3325 :   gel(gig,1) = gen_1;
     475        3325 :   gel(gig,2) = muliimod_sz(z,gel(bab,m),le,lle);;
     476        3325 :   for (i=3; i<=m; i++) gel(gig,i) = muliimod_sz(gel(gig,2),gel(gig,i-1),le,lle);
     477        3325 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "polsubcyclo_roots");
     478        3325 :   gel(powz,1) = bab;
     479        3325 :   gel(powz,2) = gig; return powz;
     480             : }
     481             : 
     482             : GEN
     483         189 : galoiscyclo(long n, long v)
     484             : {
     485         189 :   ulong av = avma;
     486             :   GEN grp, G, z, le, L, elts;
     487             :   long val, l, i, j, k;
     488         189 :   GEN zn = znstar(stoi(n));
     489         189 :   long card = itos(gel(zn,1));
     490         189 :   GEN gen = vec_to_vecsmall(lift_shallow(gel(zn,3)));
     491         189 :   GEN ord = gtovecsmall(gel(zn,2));
     492         189 :   GEN T = polcyclo(n,v);
     493         189 :   long d = degpol(T);
     494         189 :   GEN borneabs = powuu(2,d);
     495         189 :   z = polsubcyclo_start(n,card/2,2,2*usqrt(d),borneabs,&val,&l);
     496         189 :   le = gel(z,1); z = gel(z,2);
     497         189 :   L = cgetg(1+card,t_VEC);
     498         189 :   gel(L,1) = z;
     499         420 :   for (j = 1, i = 1; j < lg(gen); j++)
     500             :   {
     501         231 :     long c = i * (ord[j]-1);
     502         231 :     for (k = 1; k <= c; k++) gel(L,++i) = Fp_powu(gel(L,k), gen[j], le);
     503             :   }
     504         189 :   G = abelian_group(ord);
     505         189 :   elts = group_elts(G, card); /*not stack clean*/
     506         189 :   grp = cgetg(9, t_VEC);
     507         189 :   gel(grp,1) = T;
     508         189 :   gel(grp,2) = mkvec3(stoi(l), stoi(val), icopy(le));
     509         189 :   gel(grp,3) = L;
     510         189 :   gel(grp,4) = FpV_invVandermonde(L,  NULL, le);
     511         189 :   gel(grp,5) = gen_1;
     512         189 :   gel(grp,6) = elts;
     513         189 :   gel(grp,7) = gel(G,1);
     514         189 :   gel(grp,8) = gel(G,2);
     515         189 :   return gerepilecopy(av, grp);
     516             : }
     517             : 
     518             : /* Convert a bnrinit(Q,n) to a znstar(n)
     519             :  * complex is set to 0 if the bnr is real and to 1 if it is complex.
     520             :  * Not stack clean */
     521             : GEN
     522          14 : bnr_to_znstar(GEN bnr, long *complex)
     523             : {
     524             :   GEN gen, F, v, bid;
     525             :   long l, i;
     526          14 :   checkbnr(bnr);
     527          14 :   bid = bnr_get_bid(bnr);
     528          14 :   gen = bid_get_gen(bid);
     529          14 :   F = bid_get_ideal(bid);
     530          14 :   if (lg(F) != 2)
     531           7 :     pari_err_DOMAIN("bnr_to_znstar", "bnr", "!=", strtoGENstr("Q"), bnr);
     532             :   /* F is the finite part of the conductor, complex is the infinite part*/
     533           7 :   F = gcoeff(F, 1, 1);
     534           7 :   *complex = signe(gel(bid_get_arch(bid), 1));
     535           7 :   l = lg(gen); v = cgetg(l, t_VEC);
     536          42 :   for (i = 1; i < l; ++i)
     537             :   {
     538          35 :     GEN x = gel(gen,i);
     539          35 :     if (typ(x) == t_COL) x = gel(x,1);
     540          35 :     gel(v,i) = gmodulo(absi(x), F);
     541             :   }
     542           7 :   return mkvec3(bnr_get_no(bnr), bnr_get_cyc(bnr), v);
     543             : }
     544             : 
     545             : GEN
     546         224 : galoissubcyclo(GEN N, GEN sg, long flag, long v)
     547             : {
     548         224 :   pari_sp ltop= avma, av;
     549         224 :   GEN H, V, B, zl, L, T, le, powz, O, Z = NULL;
     550         224 :   long i, card, phi_n, val,l, n, cnd, complex=1;
     551             :   pari_timer ti;
     552             : 
     553         224 :   if (flag<0 || flag>2) pari_err_FLAG("galoissubcyclo");
     554         224 :   if (v < 0) v = 0;
     555         224 :   if (!sg) sg = gen_1;
     556         224 :   switch(typ(N))
     557             :   {
     558             :     case t_INT:
     559         140 :       n = itos(N);
     560         140 :       if (n < 1)
     561           7 :         pari_err_DOMAIN("galoissubcyclo", "degree", "<=", gen_0, stoi(n));
     562         133 :       break;
     563             :     case t_VEC:
     564          84 :       if (lg(N)==7) N = bnr_to_znstar(N,&complex);
     565          77 :       if (lg(N)==4)
     566             :       { /* znstar */
     567          77 :         GEN gen = abgrp_get_gen(N);
     568          77 :         Z = N;
     569          77 :         if (typ(gen)!=t_VEC) pari_err_TYPE("galoissubcyclo",gen);
     570          77 :         if (lg(gen) == 1) n = 1;
     571          77 :         else if (typ(gel(gen,1)) == t_INTMOD)
     572             :         {
     573          70 :           GEN z = gel(gen,1);
     574          70 :           n = itos(gel(z,1));
     575             :         } else
     576             :         {
     577           7 :           pari_err_TYPE("galoissubcyclo",N);
     578             :           return NULL;/*LCOV_EXCL_LINE*/
     579             :         }
     580          70 :         break;
     581             :       }
     582             :     default: /*fall through*/
     583           0 :       pari_err_TYPE("galoissubcyclo",N);
     584             :       return NULL;/*LCOV_EXCL_LINE*/
     585             :   }
     586         203 :   if (n==1)
     587             :   {
     588          21 :     avma = ltop;
     589          21 :     if (flag == 1) return gen_1;
     590          14 :     return gscycloconductor(deg1pol_shallow(gen_1, gen_m1, v), 1, flag);
     591             :   }
     592             : 
     593         182 :   switch(typ(sg))
     594             :   {
     595             :      case t_INTMOD: case t_INT:
     596          98 :       V = mkvecsmall( lift_check_modulus(sg,n) );
     597          91 :       break;
     598             :     case t_VECSMALL:
     599           0 :       V = gcopy(sg);
     600           0 :       for (i=1; i<lg(V); i++) { V[i] %= n; if (V[i] < 0) V[i] += n; }
     601           0 :       break;
     602             :     case t_VEC:
     603             :     case t_COL:
     604           0 :       V = cgetg(lg(sg),t_VECSMALL);
     605           0 :       for(i=1;i<lg(sg);i++) V[i] = lift_check_modulus(gel(sg,i),n);
     606           0 :       break;
     607             :     case t_MAT:
     608          84 :       if (lg(sg) == 1 || lg(sg) != lgcols(sg))
     609           7 :         pari_err_TYPE("galoissubcyclo [H not in HNF]", sg);
     610          77 :       if (!Z) pari_err_TYPE("galoissubcyclo [N not a bnrinit or znstar]", sg);
     611          70 :       if ( lg(gel(Z,2)) != lg(sg) ) pari_err_DIM("galoissubcyclo");
     612          63 :       V = znstar_hnf_generators(znstar_small(Z),sg);
     613          63 :       break;
     614             :     default:
     615           0 :       pari_err_TYPE("galoissubcyclo",sg);
     616             :       return NULL;/*LCOV_EXCL_LINE*/
     617             :   }
     618         154 :   if (!complex) V = vecsmall_append(V,n-1); /*add complex conjugation*/
     619         154 :   H = znstar_generate(n,V);
     620         154 :   if (DEBUGLEVEL >= 6)
     621             :   {
     622           0 :     err_printf("Subcyclo: elements:");
     623           0 :     for (i=1;i<n;i++)
     624           0 :       if (F2v_coeff(gel(H,3),i)) err_printf(" %ld",i);
     625           0 :     err_printf("\n");
     626             :   }
     627             :   /* field is real iff z -> conj(z) = z^-1 = z^(n-1) is in H */
     628         154 :   complex = !F2v_coeff(gel(H,3),n-1);
     629         154 :   if (DEBUGLEVEL >= 6) err_printf("Subcyclo: complex=%ld\n",complex);
     630         154 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     631         154 :   cnd = znstar_conductor(H);
     632         154 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "znstar_conductor");
     633         154 :   if (flag == 1)  { avma=ltop; return stoi(cnd); }
     634         154 :   if (cnd == 1)
     635             :   {
     636          28 :     avma = ltop;
     637          28 :     return gscycloconductor(deg1pol_shallow(gen_1,gen_m1,v),1,flag);
     638             :   }
     639         126 :   if (n != cnd)
     640             :   {
     641          35 :     H = znstar_reduce_modulus(H, cnd);
     642          35 :     n = cnd;
     643             :   }
     644         126 :   card = znstar_order(H);
     645         126 :   phi_n = eulerphiu(n);
     646         126 :   if (card == phi_n)
     647             :   {
     648           0 :     avma = ltop;
     649           0 :     return gscycloconductor(polcyclo(n,v),n,flag);
     650             :   }
     651         126 :   O = znstar_cosets(n, phi_n, H);
     652         126 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "znstar_cosets");
     653         126 :   if (DEBUGLEVEL >= 6) err_printf("Subcyclo: orbits=%Ps\n",O);
     654         126 :   if (DEBUGLEVEL >= 4)
     655           0 :     err_printf("Subcyclo: %ld orbits with %ld elements each\n",phi_n/card,card);
     656         126 :   av = avma;
     657         126 :   powz = polsubcyclo_complex_roots(n,!complex,LOWDEFAULTPREC);
     658         126 :   L = polsubcyclo_orbits(n,H,O,powz,NULL);
     659         126 :   B = polsubcyclo_complex_bound(av,L,LOWDEFAULTPREC);
     660         126 :   zl = polsubcyclo_start(n,phi_n/card,card,1,B,&val,&l);
     661         126 :   powz = polsubcyclo_roots(n,zl);
     662         126 :   le = gel(zl,1);
     663         126 :   L = polsubcyclo_orbits(n,H,O,powz,le);
     664         126 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     665         126 :   T = FpV_roots_to_pol(L,le,v);
     666         126 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "roots_to_pol");
     667         126 :   T = FpX_center(T,le,shifti(le,-1));
     668         126 :   return gerepileupto(ltop, gscycloconductor(T,n,flag));
     669             : }
     670             : 
     671             : /* Z = znstar(n) cyclic. n = 1,2,4,p^a or 2p^a,
     672             :  * and d | phi(n) = 1,1,2,(p-1)p^(a-1) */
     673             : static GEN
     674        3290 : polsubcyclo_g(long n, long d, GEN Z, long v)
     675             : {
     676        3290 :   pari_sp ltop = avma;
     677             :   long o, p, r, g, gd, l , val;
     678             :   GEN zl, L, T, le, B, powz;
     679             :   pari_timer ti;
     680        3290 :   if (d==1) return deg1pol_shallow(gen_1,gen_m1,v); /* get rid of n=1,2 */
     681        3290 :   if ((n & 3) == 2) n >>= 1;
     682             :   /* n = 4 or p^a, p odd */
     683        3290 :   o = itos(gel(Z,1));
     684        3290 :   g = itos(gmael3(Z,3,1,2));
     685        3290 :   p = n / ugcd(n,o); /* p^a / gcd(p^a,phi(p^a)) = p*/
     686        3290 :   r = ugcd(d,n); /* = p^(v_p(d)) < n */
     687        3290 :   n = r*p; /* n is now the conductor */
     688        3290 :   o = n-r; /* = phi(n) */
     689        3290 :   if (o == d) return polcyclo(n,v);
     690        3199 :   o /= d;
     691        3199 :   gd = Fl_powu(g%n, d, n);
     692             :   /*FIXME: If degree is small, the computation of B is a waste of time*/
     693        3199 :   powz = polsubcyclo_complex_roots(n,(o&1)==0,LOWDEFAULTPREC);
     694        3199 :   L = polsubcyclo_cyclic(n,d,o,g,gd,powz,NULL);
     695        3199 :   B = polsubcyclo_complex_bound(ltop,L,LOWDEFAULTPREC);
     696        3199 :   zl = polsubcyclo_start(n,d,o,1,B,&val,&l);
     697        3199 :   le = gel(zl,1);
     698        3199 :   powz = polsubcyclo_roots(n,zl);
     699        3199 :   L = polsubcyclo_cyclic(n,d,o,g,gd,powz,le);
     700        3199 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     701        3199 :   T = FpV_roots_to_pol(L,le,v);
     702        3199 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "roots_to_pol");
     703        3199 :   return gerepileupto(ltop, FpX_center(T,le,shifti(le,-1)));
     704             : }
     705             : 
     706             : GEN
     707        3318 : polsubcyclo(long n, long d, long v)
     708             : {
     709        3318 :   pari_sp ltop = avma;
     710             :   GEN L, Z;
     711        3318 :   if (v<0) v = 0;
     712        3318 :   if (d<=0) pari_err_DOMAIN("polsubcyclo","d","<=",gen_0,stoi(d));
     713        3311 :   if (n<=0) pari_err_DOMAIN("polsubcyclo","n","<=",gen_0,stoi(n));
     714        3304 :   Z = znstar(stoi(n));
     715        3304 :   if (!dvdis(gel(Z,1), d)) { avma = ltop; return cgetg(1, t_VEC); }
     716        3304 :   if (lg(gel(Z,2)) == 2)
     717             :   { /* faster but Z must be cyclic */
     718        3290 :     avma = ltop;
     719        3290 :     return polsubcyclo_g(n, d, Z, v);
     720             :   }
     721          14 :   L = subgrouplist(gel(Z,2), mkvec(stoi(d)));
     722          14 :   if (lg(L) == 2)
     723           7 :     return gerepileupto(ltop, galoissubcyclo(Z, gel(L,1), 0, v));
     724             :   else
     725             :   {
     726           7 :     GEN V = cgetg(lg(L),t_VEC);
     727             :     long i;
     728           7 :     for (i=1; i< lg(V); i++) gel(V,i) = galoissubcyclo(Z, gel(L,i), 0, v);
     729           7 :     return gerepileupto(ltop, V);
     730             :   }
     731             : }
     732             : 
     733             : struct aurifeuille_t {
     734             :   GEN z, le;
     735             :   ulong l;
     736             :   long e;
     737             : };
     738             : 
     739             : /* Let z a primitive n-th root of 1, n > 1, A an integer such that
     740             :  * Aurifeuillian factorization of Phi_n(A) exists ( z.A is a square in Q(z) ).
     741             :  * Let G(p) the Gauss sum mod p prime:
     742             :  *      sum_x (x|p) z^(xn/p) for p odd,  i - 1 for p = 2 [ i := z^(n/4) ]
     743             :  * We have N(-1) = Nz = 1 (n != 1,2), and
     744             :  *      G^2 = (-1|p) p for p odd,  G^2 = -2i for p = 2
     745             :  * In particular, for odd A, (-1|A) A = g^2 is a square. If A = prod p^{e_p},
     746             :  * sigma_j(g) = \prod_p (sigma_j G(p)))^e_p = \prod_p (j|p)^e_p g = (j|A) g
     747             :  * n odd  : z^2 is a primitive root, A = g^2
     748             :  *   Phi_n(A) = N(A - z^2) = N(g - z) N(g + z)
     749             :  *
     750             :  * n = 2 (4) : -z^2 is a primitive root, -A = g^2
     751             :  *   Phi_n(A) = N(A - (-z^2)) = N(g^2 - z^2)  [ N(-1) = 1 ]
     752             :  *                            = N(g - z) N(g + z)
     753             :  *
     754             :  * n = 4 (8) : i z^2 primitive root, -Ai = g^2
     755             :  *   Phi_n(A) = N(A - i z^2) = N(-Ai -  z^2) = N(g - z) N(g + z)
     756             :  * sigma_j(g) / g =  (j|A)  if j = 1 (4)
     757             :  *                  (-j|A)i if j = 3 (4)
     758             :  *   */
     759             : /* factor Phi_n(A), Astar: A* = squarefree kernel of A, P = odd prime divisors
     760             :  * of n */
     761             : static GEN
     762        5278 : factor_Aurifeuille_aux(GEN A, long Astar, long n, GEN P,
     763             :                        struct aurifeuille_t *S)
     764             : {
     765             :   pari_sp av;
     766        5278 :   GEN f, a, b, s, powers, z = S->z, le = S->le;
     767        5278 :   long j, k, maxjump, lastj, e = S->e;
     768        5278 :   ulong l = S->l;
     769             :   char *invertible;
     770             : 
     771        5278 :   if ((n & 7) == 4)
     772             :   { /* A^* even */
     773        5215 :     GEN i = Fp_powu(z, n>>2, le), z2 = Fp_sqr(z, le);
     774             : 
     775        5215 :     invertible = stack_malloc(n); /* even indices unused */
     776        5215 :     for (j = 1; j < n; j+=2) invertible[j] = 1;
     777        5271 :     for (k = 1; k < lg(P); k++)
     778             :     {
     779          56 :       long p = P[k];
     780          56 :       for (j = p; j < n; j += 2*p) invertible[j] = 0;
     781             :     }
     782        5215 :     lastj = 1; maxjump = 2;
     783       10990 :     for (j= 3; j < n; j+=2)
     784        5775 :       if (invertible[j]) {
     785        5663 :         long jump = j - lastj;
     786        5663 :         if (jump > maxjump) maxjump = jump;
     787        5663 :         lastj = j;
     788             :       }
     789        5215 :     powers = cgetg(maxjump+1, t_VEC); /* powers[k] = z^k, odd indices unused */
     790        5215 :     gel(powers,2) = z2;
     791        5271 :     for (k = 4; k <= maxjump; k+=2)
     792         112 :       gel(powers,k) = odd(k>>1)? Fp_mul(gel(powers, k-2), z2, le)
     793          56 :                                : Fp_sqr(gel(powers, k>>1), le);
     794             : 
     795        5215 :     if (Astar == 2)
     796             :     { /* important special case (includes A=2), split for efficiency */
     797        5194 :       if (!equalis(A, 2))
     798             :       {
     799          14 :         GEN f = sqrti(shifti(A,-1)), mf = Fp_neg(f,le), fi = Fp_mul(f,i,le);
     800          14 :         a = Fp_add(mf, fi, le);
     801          14 :         b = Fp_sub(mf, fi, le);
     802             :       }
     803             :       else
     804             :       {
     805        5180 :         a = subiu(i,1);
     806        5180 :         b = subsi(-1,i);
     807             :       }
     808        5194 :       av = avma;
     809        5194 :       s = z; f = subii(a, s); lastj = 1;
     810       10640 :       for (j = 3, k = 0; j < n; j+=2)
     811        5446 :         if (invertible[j])
     812             :         {
     813        5376 :           s = Fp_mul(gel(powers, j-lastj), s, le); /* z^j */
     814        5376 :           lastj = j;
     815        5376 :           f = Fp_mul(f, subii((j & 3) == 1? a: b, s), le);
     816        5376 :           if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     817             :         }
     818             :     }
     819             :     else
     820             :     {
     821          21 :       GEN ma, mb, B = Fp_mul(A, i, le), gl = utoipos(l);
     822             :       long t;
     823          21 :       Astar >>= 1;
     824          21 :       t = Astar & 3; if (Astar < 0) t = 4-t; /* t = 1 or 3 */
     825          21 :       if (t == 1) B = Fp_neg(B, le);
     826          21 :       a = Zp_sqrtlift(B, Fp_sqrt(B, gl), gl, e);
     827          21 :       b = Fp_mul(a, i, le);
     828          21 :       ma = Fp_neg(a, le);
     829          21 :       mb = Fp_neg(b, le);
     830          21 :       av = avma;
     831          21 :       s = z; f = subii(a, s); lastj = 1;
     832         350 :       for (j = 3, k = 0; j<n; j+=2)
     833         329 :         if (invertible[j])
     834             :         {
     835             :           GEN t;
     836         287 :           if ((j & 3) == 1) t = (kross(j, Astar) < 0)? ma: a;
     837         154 :           else              t = (kross(j, Astar) < 0)? mb: b;
     838         287 :           s = Fp_mul(gel(powers, j-lastj), s, le); /* z^j */
     839         287 :           lastj = j;
     840         287 :           f = Fp_mul(f, subii(t, s), le);
     841         287 :           if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     842             :         }
     843             :     }
     844             :   }
     845             :   else /* A^* odd */
     846             :   {
     847             :     ulong g;
     848          63 :     if ((n & 3) == 2)
     849             :     { /* A^* = 3 (mod 4) */
     850           0 :       A = negi(A); Astar = -Astar;
     851           0 :       z = Fp_neg(z, le);
     852           0 :       n >>= 1;
     853             :     }
     854             :     /* A^* = 1 (mod 4) */
     855          63 :     g = Fl_sqrt(umodiu(A,l), l);
     856          63 :     a = Zp_sqrtlift(A, utoipos(g), utoipos(l), e);
     857          63 :     b = negi(a);
     858             : 
     859          63 :     invertible = stack_malloc(n);
     860          63 :     for (j = 1; j < n; j++) invertible[j] = 1;
     861         168 :     for (k = 1; k < lg(P); k++)
     862             :     {
     863         105 :       long p = P[k];
     864         105 :       for (j = p; j < n; j += p) invertible[j] = 0;
     865             :     }
     866          63 :     lastj = 2; maxjump = 1;
     867        1141 :     for (j= 3; j < n; j++)
     868        1078 :       if (invertible[j]) {
     869         714 :         long jump = j - lastj;
     870         714 :         if (jump > maxjump) maxjump = jump;
     871         714 :         lastj = j;
     872             :       }
     873          63 :     powers = cgetg(maxjump+1, t_VEC); /* powers[k] = z^k */
     874          63 :     gel(powers,1) = z;
     875         147 :     for (k = 2; k <= maxjump; k++)
     876         210 :       gel(powers,k) = odd(k)? Fp_mul(gel(powers, k-1), z, le)
     877         126 :                             : Fp_sqr(gel(powers, k>>1), le);
     878          63 :     av = avma;
     879          63 :     s = z; f = subii(a, s); lastj = 1;
     880        1204 :     for(j = 2, k = 0; j < n; j++)
     881        1141 :       if (invertible[j])
     882             :       {
     883         777 :         s = Fp_mul(gel(powers, j-lastj), s, le);
     884         777 :         lastj = j;
     885         777 :         f = Fp_mul(f, subii(kross(j,Astar)==1? a: b, s), le);
     886         777 :         if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     887             :       }
     888             :   }
     889        5278 :   return f;
     890             : }
     891             : 
     892             : /* fd = factoru(odd part of d = d or d/4). Return eulerphi(d) */
     893             : static ulong
     894        5278 : phi(long d, GEN fd)
     895             : {
     896        5278 :   GEN P = gel(fd,1), E = gel(fd,2);
     897        5278 :   long i, l = lg(P);
     898        5278 :   ulong phi = 1;
     899        5439 :   for (i = 1; i < l; i++)
     900             :   {
     901         161 :     ulong p = P[i], e = E[i];
     902         161 :     phi *= upowuu(p, e-1)*(p-1);
     903             :   }
     904        5278 :   if (!odd(d)) phi <<= 1;
     905        5278 :   return phi;
     906             : }
     907             : 
     908             : static void
     909        5278 : Aurifeuille_init(GEN a, long d, GEN fd, struct aurifeuille_t *S)
     910             : {
     911        5278 :   GEN sqrta = sqrtr_abs(itor(a, LOWDEFAULTPREC));
     912        5278 :   GEN bound = ceil_safe(powru(addrs(sqrta,1), phi(d, fd)));
     913        5278 :   GEN zl = polsubcyclo_start(d, 0, 0, 1, bound, &(S->e), (long*)&(S->l));
     914        5278 :   S->le = gel(zl,1);
     915        5278 :   S->z  = gel(zl,2);
     916        5278 : }
     917             : 
     918             : GEN
     919        5215 : factor_Aurifeuille_prime(GEN p, long d)
     920             : {
     921        5215 :   pari_sp av = avma;
     922             :   struct aurifeuille_t S;
     923             :   GEN fd;
     924             :   long pp;
     925        5215 :   if ((d & 3) == 2) { d >>= 1; p = negi(p); }
     926        5215 :   fd = factoru(odd(d)? d: d>>2);
     927        5215 :   pp = itos(p);
     928        5215 :   Aurifeuille_init(p, d, fd, &S);
     929        5215 :   return gerepileuptoint(av, factor_Aurifeuille_aux(p, pp, d, gel(fd,1), &S));
     930             : }
     931             : 
     932             : /* an algebraic factor of Phi_d(a), a != 0 */
     933             : GEN
     934          63 : factor_Aurifeuille(GEN a, long d)
     935             : {
     936          63 :   pari_sp av = avma;
     937             :   GEN fd, P, A;
     938          63 :   long i, lP, va = vali(a), sa, astar, D;
     939             :   struct aurifeuille_t S;
     940             : 
     941          63 :   if (d <= 0)
     942           0 :     pari_err_DOMAIN("factor_Aurifeuille", "degre", "<=",gen_0,stoi(d));
     943          63 :   if ((d & 3) == 2) { d >>= 1; a = negi(a); }
     944          63 :   if ((va & 1) == (d & 1)) { avma = av; return gen_1; }
     945          63 :   sa = signe(a);
     946          63 :   if (odd(d))
     947             :   {
     948             :     long a4;
     949          28 :     if (d == 1)
     950             :     {
     951           0 :       if (!Z_issquareall(a, &A)) return gen_1;
     952           0 :       return gerepileuptoint(av, addiu(A,1));
     953             :     }
     954          28 :     A = va? shifti(a, -va): a;
     955          28 :     a4 = mod4(A); if (sa < 0) a4 = 4 - a4;
     956          28 :     if (a4 != 1) { avma = av; return gen_1; }
     957             :   }
     958          35 :   else if ((d & 7) == 4)
     959          35 :     A = shifti(a, -va);
     960             :   else
     961             :   {
     962           0 :     avma = av; return gen_1;
     963             :   }
     964             :   /* v_2(d) = 0 or 2. Kill 2 from factorization (minor efficiency gain) */
     965          63 :   fd = factoru(odd(d)? d: d>>2); P = gel(fd,1); lP = lg(P);
     966          63 :   astar = sa;
     967          63 :   if (odd(va)) astar <<= 1;
     968         147 :   for (i = 1; i < lP; i++)
     969          84 :     if (odd( (Z_lvalrem(A, P[i], &A)) ) ) astar *= P[i];
     970          63 :   if (sa < 0)
     971             :   { /* negate in place if possible */
     972          14 :     if (A == a) A = icopy(A);
     973          14 :     setabssign(A);
     974             :   }
     975          63 :   if (!Z_issquare(A)) { avma = av; return gen_1; }
     976             : 
     977          63 :   D = odd(d)? 1: 4;
     978          63 :   for (i = 1; i < lP; i++) D *= P[i];
     979          63 :   if (D != d) { a = powiu(a, d/D); d = D; }
     980             : 
     981          63 :   Aurifeuille_init(a, d, fd, &S);
     982          63 :   return gerepileuptoint(av, factor_Aurifeuille_aux(a, astar, d, P, &S));
     983             : }

Generated by: LCOV version 1.11