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 - arith2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20916-a74d914) Lines: 596 638 93.4 %
Date: 2017-08-18 06:23:59 Functions: 86 89 96.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             : /*********************************************************************/
      15             : /**                                                                 **/
      16             : /**                     ARITHMETIC FUNCTIONS                        **/
      17             : /**                        (second part)                            **/
      18             : /**                                                                 **/
      19             : /*********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : GEN
      24          35 : boundfact(GEN n, ulong lim)
      25             : {
      26          35 :   switch(typ(n))
      27             :   {
      28          21 :     case t_INT: return Z_factor_limit(n,lim);
      29             :     case t_FRAC: {
      30          14 :       pari_sp av = avma;
      31          14 :       GEN a = Z_factor_limit(gel(n,1),lim);
      32          14 :       GEN b = Z_factor_limit(gel(n,2),lim);
      33          14 :       gel(b,2) = ZC_neg(gel(b,2));
      34          14 :       return gerepilecopy(av, merge_factor(a,b,(void*)&cmpii,cmp_nodata));
      35             :     }
      36             :   }
      37           0 :   pari_err_TYPE("boundfact",n);
      38             :   return NULL; /* LCOV_EXCL_LINE */
      39             : }
      40             : 
      41             : /* NOT memory clean */
      42             : GEN
      43       11299 : Z_smoothen(GEN N, GEN L, GEN *pP, GEN *pe)
      44             : {
      45       11299 :   long i, j, l = lg(L);
      46       11299 :   GEN e = new_chunk(l), P = new_chunk(l);
      47       99701 :   for (i = j = 1; i < l; i++)
      48             :   {
      49       95210 :     ulong p = uel(L,i);
      50       95210 :     long v = Z_lvalrem(N, p, &N);
      51       95210 :     if (v) { P[j] = p; e[j] = v; j++; if (is_pm1(N)) { N = NULL; break; } }
      52             :   }
      53       11299 :   P[0] = evaltyp(t_VECSMALL) | evallg(j); *pP = P;
      54       11299 :   e[0] = evaltyp(t_VECSMALL) | evallg(j); *pe = e; return N;
      55             : }
      56             : /***********************************************************************/
      57             : /**                                                                   **/
      58             : /**                    SIMPLE FACTORISATIONS                          **/
      59             : /**                                                                   **/
      60             : /***********************************************************************/
      61             : /* Factor n and output [p,e,c] where
      62             :  * p, e and c are vecsmall with n = prod{p[i]^e[i]} and c[i] = p[i]^e[i] */
      63             : GEN
      64       30177 : factoru_pow(ulong n)
      65             : {
      66       30177 :   GEN f = cgetg(4,t_VEC);
      67       30168 :   pari_sp av = avma;
      68             :   GEN F, P, E, p, e, c;
      69             :   long i, l;
      70             :   /* enough room to store <= 15 * [p,e,p^e] (OK if n < 2^64) */
      71       30168 :   (void)new_chunk((15 + 1)*3);
      72       30188 :   F = factoru(n);
      73       30210 :   P = gel(F,1);
      74       30210 :   E = gel(F,2); l = lg(P);
      75       30210 :   avma = av;
      76       30210 :   gel(f,1) = p = cgetg(l,t_VECSMALL);
      77       30222 :   gel(f,2) = e = cgetg(l,t_VECSMALL);
      78       30212 :   gel(f,3) = c = cgetg(l,t_VECSMALL);
      79       95804 :   for(i = 1; i < l; i++)
      80             :   {
      81       65582 :     p[i] = P[i];
      82       65582 :     e[i] = E[i];
      83       65582 :     c[i] = upowuu(p[i], e[i]);
      84             :   }
      85       30222 :   return f;
      86             : }
      87             : 
      88             : static GEN
      89       87510 : factorlim(GEN n, ulong lim)
      90       87510 : { return lim? Z_factor_limit(n, lim): Z_factor(n); }
      91             : /* factor p^n - 1, assuming p prime. If lim != 0, limit factorization to
      92             :  * primes <= lim */
      93             : GEN
      94       67336 : factor_pn_1_limit(GEN p, long n, ulong lim)
      95             : {
      96       67336 :   pari_sp av = avma;
      97       67336 :   GEN A = factorlim(subiu(p,1), lim), d = divisorsu(n);
      98       67336 :   long i, pp = itos_or_0(p);
      99       82316 :   for(i=2; i<lg(d); i++)
     100             :   {
     101             :     GEN B;
     102       27034 :     if (pp && d[i]%pp==0 && (
     103       24108 :        ((pp&3)==1 && (d[i]&1)) ||
     104       12145 :        ((pp&3)==3 && (d[i]&3)==2) ||
     105       11949 :        (pp==2 && (d[i]&7)==4)))
     106        5194 :     {
     107        5194 :       GEN f=factor_Aurifeuille_prime(p,d[i]);
     108        5194 :       B = factorlim(f, lim);
     109        5194 :       A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     110        5194 :       B = factorlim(diviiexact(polcyclo_eval(d[i],p), f), lim);
     111             :     }
     112             :     else
     113        9786 :       B = factorlim(polcyclo_eval(d[i],p), lim);
     114       14980 :     A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     115             :   }
     116       67336 :   return gerepilecopy(av, A);
     117             : }
     118             : GEN
     119       67336 : factor_pn_1(GEN p, ulong n)
     120       67336 : { return factor_pn_1_limit(p, n, 0); }
     121             : 
     122             : #if 0
     123             : static GEN
     124             : to_mat(GEN p, long e) {
     125             :   GEN B = cgetg(3, t_MAT);
     126             :   gel(B,1) = mkcol(p);
     127             :   gel(B,2) = mkcol(utoipos(e)); return B;
     128             : }
     129             : /* factor phi(n) */
     130             : GEN
     131             : factor_eulerphi(GEN n)
     132             : {
     133             :   pari_sp av = avma;
     134             :   GEN B = NULL, A, P, E, AP, AE;
     135             :   long i, l, v = vali(n);
     136             : 
     137             :   l = lg(n);
     138             :   /* result requires less than this: at most expi(n) primes */
     139             :   (void)new_chunk(bit_accuracy(l) * (l /*p*/ + 3 /*e*/ + 2 /*vectors*/) + 3+2);
     140             :   if (v) { n = shifti(n, -v); v--; }
     141             :   A = Z_factor(n); P = gel(A,1); E = gel(A,2); l = lg(P);
     142             :   for(i = 1; i < l; i++)
     143             :   {
     144             :     GEN p = gel(P,i), q = subiu(p,1), fa;
     145             :     long e = itos(gel(E,i)), w;
     146             : 
     147             :     w = vali(q); v += w; q = shifti(q,-w);
     148             :     if (! is_pm1(q))
     149             :     {
     150             :       fa = Z_factor(q);
     151             :       B = B? merge_factor(B, fa, (void*)&cmpii, cmp_nodata): fa;
     152             :     }
     153             :     if (e > 1) {
     154             :       if (B) {
     155             :         gel(B,1) = shallowconcat(gel(B,1), p);
     156             :         gel(B,2) = shallowconcat(gel(B,2), utoipos(e-1));
     157             :       } else
     158             :         B = to_mat(p, e-1);
     159             :     }
     160             :   }
     161             :   avma = av;
     162             :   if (!B) return v? to_mat(gen_2, v): trivial_fact();
     163             :   A = cgetg(3, t_MAT);
     164             :   P = gel(B,1); E = gel(B,2); l = lg(P);
     165             :   AP = cgetg(l+1, t_COL); gel(A,1) = AP; AP++;
     166             :   AE = cgetg(l+1, t_COL); gel(A,2) = AE; AE++;
     167             :   /* prepend "2^v" */
     168             :   gel(AP,0) = gen_2;
     169             :   gel(AE,0) = utoipos(v);
     170             :   for (i = 1; i < l; i++)
     171             :   {
     172             :     gel(AP,i) = icopy(gel(P,i));
     173             :     gel(AE,i) = icopy(gel(E,i));
     174             :   }
     175             :   return A;
     176             : }
     177             : #endif
     178             : 
     179             : /***********************************************************************/
     180             : /**                                                                   **/
     181             : /**         CHECK FACTORIZATION FOR ARITHMETIC FUNCTIONS              **/
     182             : /**                                                                   **/
     183             : /***********************************************************************/
     184             : int
     185     7082595 : RgV_is_ZVpos(GEN v)
     186             : {
     187     7082595 :   long i, l = lg(v);
     188    20321587 :   for (i = 1; i < l; i++)
     189             :   {
     190    13240763 :     GEN c = gel(v,i);
     191    13240763 :     if (typ(c) != t_INT || signe(c) <= 0) return 0;
     192             :   }
     193     7080824 :   return 1;
     194             : }
     195             : /* check whether v is a ZV with non-0 entries */
     196             : int
     197        6615 : RgV_is_ZVnon0(GEN v)
     198             : {
     199        6615 :   long i, l = lg(v);
     200       19817 :   for (i = 1; i < l; i++)
     201             :   {
     202       13258 :     GEN c = gel(v,i);
     203       13258 :     if (typ(c) != t_INT || !signe(c)) return 0;
     204             :   }
     205        6559 :   return 1;
     206             : }
     207             : /* check whether v is a ZV with non-zero entries OR exactly [0] */
     208             : static int
     209       20909 : RgV_is_ZV0(GEN v)
     210             : {
     211       20909 :   long i, l = lg(v);
     212       42763 :   for (i = 1; i < l; i++)
     213             :   {
     214       21959 :     GEN c = gel(v,i);
     215             :     long s;
     216       21959 :     if (typ(c) != t_INT) return 0;
     217       21959 :     s = signe(c);
     218       21959 :     if (!s) return (l == 2);
     219             :   }
     220       20804 :   return 1;
     221             : }
     222             : 
     223             : static int
     224       22904 : RgV_is_prV(GEN v)
     225             : {
     226       22904 :   long l = lg(v), i;
     227       23261 :   for (i = 1; i < l; i++)
     228       22904 :     if (!checkprid_i(gel(v,i))) return 0;
     229         357 :   return 1;
     230             : }
     231             : int
     232       26712 : is_nf_factor(GEN F)
     233             : {
     234       78876 :   return typ(F) == t_MAT && lg(F) == 3
     235       49609 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZVpos(gel(F,2));
     236             : }
     237             : int
     238          14 : is_nf_extfactor(GEN F)
     239             : {
     240          42 :   return typ(F) == t_MAT && lg(F) == 3
     241          21 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZV(gel(F,2));
     242             : }
     243             : 
     244             : static int
     245     3554013 : is_Z_factor_i(GEN f)
     246     3554013 : { return typ(f) == t_MAT && lg(f) == 3 && RgV_is_ZVpos(gel(f,2)); }
     247             : int
     248     3526489 : is_Z_factorpos(GEN f)
     249     3526489 : { return is_Z_factor_i(f) && RgV_is_ZVpos(gel(f,1)); }
     250             : int
     251       20909 : is_Z_factor(GEN f)
     252       20909 : { return is_Z_factor_i(f) && RgV_is_ZV0(gel(f,1)); }
     253             : /* as is_Z_factorpos, also allow factor(0) */
     254             : int
     255        6615 : is_Z_factornon0(GEN f)
     256        6615 : { return is_Z_factor_i(f) && RgV_is_ZVnon0(gel(f,1)); }
     257             : GEN
     258       20853 : clean_Z_factor(GEN f)
     259             : {
     260       20853 :   GEN P = gel(f,1);
     261       20853 :   long n = lg(P)-1;
     262       20853 :   if (n && equalim1(gel(P,1)))
     263        2513 :     return mkmat2(vecslice(P,2,n), vecslice(gel(f,2),2,n));
     264       18340 :   return f;
     265             : }
     266             : GEN
     267           0 : fuse_Z_factor(GEN f, GEN B)
     268             : {
     269           0 :   GEN P = gel(f,1), E = gel(f,2), P2,E2;
     270           0 :   long i, l = lg(P);
     271           0 :   if (l == 1) return f;
     272           0 :   for (i = 1; i < l; i++)
     273           0 :     if (abscmpii(gel(P,i), B) > 0) break;
     274           0 :   if (i == l) return f;
     275             :   /* tail / initial segment */
     276           0 :   P2 = vecslice(P, i, l-1); P = vecslice(P, 1, i-1);
     277           0 :   E2 = vecslice(E, i, l-1); E = vecslice(E, 1, i-1);
     278           0 :   P = shallowconcat(P, mkvec(factorback2(P2,E2)));
     279           0 :   E = shallowconcat(E, mkvec(gen_1));
     280           0 :   return mkmat2(P, E);
     281             : }
     282             : 
     283             : /* n attached to a factorization of a positive integer: either N (t_INT)
     284             :  * a factorization matrix faN, or a t_VEC: [N, faN] */
     285             : GEN
     286         210 : check_arith_pos(GEN n, const char *f) {
     287         210 :   switch(typ(n))
     288             :   {
     289             :     case t_INT:
     290         203 :       if (signe(n) <= 0) pari_err_DOMAIN(f, "argument", "<=", gen_0, gen_0);
     291         203 :       return NULL;
     292             :     case t_VEC:
     293           0 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT || signe(gel(n,1)) <= 0) break;
     294           0 :       n = gel(n,2); /* fall through */
     295             :     case t_MAT:
     296           7 :       if (!is_Z_factorpos(n)) break;
     297           7 :       return n;
     298             :   }
     299           0 :   pari_err_TYPE(f,n);
     300           0 :   return NULL;
     301             : }
     302             : /* n attached to a factorization of a non-0 integer */
     303             : GEN
     304       76078 : check_arith_non0(GEN n, const char *f) {
     305       76078 :   switch(typ(n))
     306             :   {
     307             :     case t_INT:
     308       69790 :       if (!signe(n)) pari_err_DOMAIN(f, "argument", "=", gen_0, gen_0);
     309       69733 :       return NULL;
     310             :     case t_VEC:
     311           7 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT || !signe(gel(n,1))) break;
     312           7 :       n = gel(n,2); /* fall through */
     313             :     case t_MAT:
     314        6566 :       if (!is_Z_factornon0(n)) break;
     315        6510 :       return n;
     316             :   }
     317          48 :   pari_err_TYPE(f,n);
     318           0 :   return NULL;
     319             : }
     320             : /* n attached to a factorization of an integer */
     321             : GEN
     322      240205 : check_arith_all(GEN n, const char *f) {
     323      240205 :   switch(typ(n))
     324             :   {
     325             :     case t_INT:
     326      219296 :       return NULL;
     327             :     case t_VEC:
     328       15036 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT) break;
     329       15036 :       n = gel(n,2); /* fall through */
     330             :     case t_MAT:
     331       20909 :       if (!is_Z_factor(n)) break;
     332       20909 :       return n;
     333             :   }
     334           0 :   pari_err_TYPE(f,n);
     335           0 :   return NULL;
     336             : }
     337             : 
     338             : /***********************************************************************/
     339             : /**                                                                   **/
     340             : /**                MISCELLANEOUS ARITHMETIC FUNCTIONS                 **/
     341             : /**                (ultimately depend on Z_factor())                  **/
     342             : /**                                                                   **/
     343             : /***********************************************************************/
     344             : /* set P,E from F. Check whether F is an integer and kill "factor" -1 */
     345             : static void
     346      350749 : set_fact_check(GEN F, GEN *pP, GEN *pE, int *isint)
     347             : {
     348             :   GEN E, P;
     349      350749 :   if (lg(F) != 3) pari_err_TYPE("divisors",F);
     350      350749 :   P = gel(F,1);
     351      350749 :   E = gel(F,2);
     352      350749 :   RgV_check_ZV(E, "divisors");
     353      350749 :   *isint = RgV_is_ZV(P);
     354      350749 :   if (*isint)
     355             :   {
     356      350735 :     long i, l = lg(P);
     357             :     /* skip -1 */
     358      350735 :     if (l>1 && signe(gel(P,1)) < 0) { E++; P = vecslice(P,2,--l); }
     359             :     /* test for 0 */
     360     1143135 :     for (i = 1; i < l; i++)
     361      792407 :       if (!signe(gel(P,i)) && signe(gel(E,i)))
     362           7 :         pari_err_DOMAIN("divisors", "argument", "=", gen_0, F);
     363             :   }
     364      350742 :   *pP = P;
     365      350742 :   *pE = E;
     366      350742 : }
     367             : static void
     368       11410 : set_fact(GEN F, GEN *pP, GEN *pE) { *pP = gel(F,1); *pE = gel(F,2); }
     369             : 
     370             : int
     371      362166 : divisors_init(GEN n, GEN *pP, GEN *pE)
     372             : {
     373             :   long i,l;
     374             :   GEN E, P, e;
     375             :   int isint;
     376             : 
     377      362166 :   switch(typ(n))
     378             :   {
     379             :     case t_INT:
     380       11389 :       if (!signe(n)) pari_err_DOMAIN("divisors", "argument", "=", gen_0, gen_0);
     381       11389 :       set_fact(absZ_factor(n), &P,&E);
     382       11389 :       isint = 1; break;
     383             :     case t_VEC:
     384          14 :       if (lg(n) != 3 || typ(gel(n,2)) !=t_MAT) pari_err_TYPE("divisors",n);
     385           7 :       set_fact_check(gel(n,2), &P,&E, &isint);
     386           7 :       break;
     387             :     case t_MAT:
     388      350742 :       set_fact_check(n, &P,&E, &isint);
     389      350735 :       break;
     390             :     default:
     391          21 :       set_fact(factor(n), &P,&E);
     392          21 :       isint = 0; break;
     393             :   }
     394      362152 :   l = lg(P);
     395      362152 :   e = cgetg(l, t_VECSMALL);
     396     1186339 :   for (i=1; i<l; i++)
     397             :   {
     398      824194 :     e[i] = itos(gel(E,i));
     399      824194 :     if (e[i] < 0) pari_err_TYPE("divisors [denominator]",n);
     400             :   }
     401      362145 :   *pP = P; *pE = e; return isint;
     402             : }
     403             : 
     404             : static long
     405      362103 : ndiv(GEN E)
     406             : {
     407             :   long i, l;
     408      362103 :   GEN e = cgetg_copy(E, &l); /* left on stack */
     409             :   ulong n;
     410      362103 :   for (i=1; i<l; i++) e[i] = E[i]+1;
     411      362103 :   n = itou_or_0( zv_prod_Z(e) );
     412      362103 :   if (!n || n & ~LGBITS) pari_err_OVERFLOW("divisors");
     413      362103 :   return n;
     414             : }
     415             : static int
     416        4354 : cmpi1(void *E, GEN a, GEN b) { (void)E; return cmpii(gel(a,1), gel(b,1)); }
     417             : /* P a t_COL of objects, E a t_VECSMALL of exponents, return cleaned-up
     418             :  * factorization (removing 0 exponents) as a t_MAT with 2 cols. */
     419             : static GEN
     420        3486 : fa_clean(GEN P, GEN E)
     421             : {
     422        3486 :   long i, j, l = lg(E);
     423        3486 :   GEN Q = cgetg(l, t_COL);
     424       10311 :   for (i = j = 1; i < l; i++)
     425        6825 :     if (E[i]) { gel(Q,j) = gel(P,i); E[j] = E[i]; j++; }
     426        3486 :   setlg(Q,j);
     427        3486 :   setlg(E,j); return mkmat2(Q,Flc_to_ZC(E));
     428             : }
     429             : GEN
     430         721 : divisors_factored(GEN N)
     431             : {
     432         721 :   pari_sp av = avma;
     433             :   GEN *d, *t1, *t2, *t3, D, P, E;
     434         721 :   int isint = divisors_init(N, &P, &E);
     435         721 :   GEN (*mul)(GEN,GEN) = isint? mulii: gmul;
     436         721 :   long i, j, l, n = ndiv(E);
     437             : 
     438         721 :   D = cgetg(n+1,t_VEC); d = (GEN*)D;
     439         721 :   l = lg(E);
     440         721 :   *++d = mkvec2(gen_1, const_vecsmall(l-1,0));
     441        1946 :   for (i=1; i<l; i++)
     442        2947 :     for (t1=(GEN*)D,j=E[i]; j; j--,t1=t2)
     443        6209 :       for (t2=d, t3=t1; t3<t2; )
     444             :       {
     445             :         GEN a, b;
     446        2765 :         a = mul(gel(*++t3,1), gel(P,i));
     447        2765 :         b = leafcopy(gel(*t3,2)); b[i]++;
     448        2765 :         *++d = mkvec2(a,b);
     449             :       }
     450         721 :   if (isint) gen_sort_inplace(D,NULL,&cmpi1,NULL);
     451         721 :   for (i = 1; i <= n; i++) gmael(D,i,2) = fa_clean(P, gmael(D,i,2));
     452         721 :   return gerepilecopy(av, D);
     453             : }
     454             : GEN
     455      361403 : divisors(GEN N)
     456             : {
     457             :   long i, j, l;
     458             :   GEN *d, *t1, *t2, *t3, D, P, E;
     459      361403 :   int isint = divisors_init(N, &P, &E);
     460      361382 :   GEN (*mul)(GEN,GEN) = isint? mulii: gmul;
     461             : 
     462      361382 :   D = cgetg(ndiv(E)+1,t_VEC); d = (GEN*)D;
     463      361382 :   l = lg(E);
     464      361382 :   *++d = gen_1;
     465     1184190 :   for (i=1; i<l; i++)
     466     2210243 :     for (t1=(GEN*)D,j=E[i]; j; j--,t1=t2)
     467     1387435 :       for (t2=d, t3=t1; t3<t2; ) *++d = mul(*++t3, gel(P,i));
     468      361382 :   if (isint) ZV_sort_inplace(D);
     469      361382 :   return D;
     470             : }
     471             : GEN
     472         777 : divisors0(GEN N, long flag)
     473             : {
     474         777 :   if (flag && flag != 1) pari_err_FLAG("divisors");
     475         777 :   return flag? divisors_factored(N): divisors(N);
     476             : }
     477             : 
     478             : GEN
     479    24528662 : divisorsu_fact(GEN P, GEN E)
     480             : {
     481    24528662 :   long i, j, l = lg(P);
     482    24528662 :   ulong nbdiv = 1;
     483             :   GEN d, t, t1, t2, t3;
     484    24528662 :   for (i=1; i<l; i++) nbdiv *= 1+E[i];
     485    24528662 :   d = t = cgetg(nbdiv+1,t_VECSMALL);
     486    24528662 :   *++d = 1;
     487    83642569 :   for (i=1; i<l; i++)
     488   126228679 :     for (t1=t,j=E[i]; j; j--,t1=t2)
     489    67114772 :       for (t2=d, t3=t1; t3<t2; ) *(++d) = *(++t3) * P[i];
     490    24528662 :   vecsmall_sort(t); return t;
     491             : }
     492             : GEN
     493      120382 : divisorsu(ulong N)
     494             : {
     495      120382 :   pari_sp av = avma;
     496      120382 :   GEN fa = factoru(N);
     497      120382 :   return gerepileupto(av, divisorsu_fact(gel(fa,1), gel(fa,2)));
     498             : }
     499             : 
     500             : static GEN
     501           0 : corefa(GEN fa)
     502             : {
     503           0 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1;
     504             :   long i;
     505           0 :   for (i=1; i<lg(P); i++)
     506           0 :     if (mod2(gel(E,i))) c = mulii(c,gel(P,i));
     507           0 :   return c;
     508             : }
     509             : static GEN
     510        1162 : core2fa(GEN fa)
     511             : {
     512        1162 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1, f = gen_1;
     513             :   long i;
     514        2709 :   for (i=1; i<lg(P); i++)
     515             :   {
     516        1547 :     long e = itos(gel(E,i));
     517        1547 :     if (e & 1)  c = mulii(c, gel(P,i));
     518        1547 :     if (e != 1) f = mulii(f, powiu(gel(P,i), e >> 1));
     519             :   }
     520        1162 :   return mkvec2(c,f);
     521             : }
     522             : GEN
     523           0 : corepartial(GEN n, long all)
     524             : {
     525           0 :   pari_sp av = avma;
     526           0 :   if (typ(n) != t_INT) pari_err_TYPE("corepartial",n);
     527           0 :   return gerepileuptoint(av, corefa(Z_factor_limit(n,all)));
     528             : }
     529             : GEN
     530        1162 : core2partial(GEN n, long all)
     531             : {
     532        1162 :   pari_sp av = avma;
     533        1162 :   if (typ(n) != t_INT) pari_err_TYPE("core2partial",n);
     534        1162 :   return gerepilecopy(av, core2fa(Z_factor_limit(n,all)));
     535             : }
     536             : /* given an arithmetic function argument, return the underlying integer */
     537             : static GEN
     538        2912 : arith_n(GEN A)
     539             : {
     540        2912 :   switch(typ(A))
     541             :   {
     542         728 :     case t_INT: return A;
     543           7 :     case t_VEC: return gel(A,1);
     544        2177 :     default: return factorback(A);
     545             :   }
     546             : }
     547             : static GEN
     548        1484 : core2_i(GEN n)
     549             : {
     550        1484 :   GEN f = core(n);
     551        1484 :   if (!signe(f)) return mkvec2(gen_0, gen_1);
     552        1449 :   return mkvec2(f, sqrtint(diviiexact(arith_n(n), f)));
     553             : }
     554             : GEN
     555        1477 : core2(GEN n) { pari_sp av = avma; return gerepilecopy(av, core2_i(n)); }
     556             : 
     557             : GEN
     558        3094 : core0(GEN n,long flag) { return flag? core2(n): core(n); }
     559             : 
     560             : static long
     561          56 : _mod4(GEN c) {
     562          56 :   long r, s = signe(c);
     563          56 :   if (!s) return 0;
     564          56 :   r = mod4(c); if (s < 0) r = 4-r;
     565          56 :   return r;
     566             : }
     567             : 
     568             : long
     569        3606 : corediscs(long D, ulong *f)
     570             : {
     571             :   /* D = f^2 dK */
     572        3606 :   long dK = D>=0 ? (long) coreu(D) : -(long) coreu(-(ulong) D);
     573        3606 :   ulong dKmod4 = ((ulong)dK)&3UL;
     574        3606 :   if (dKmod4 == 2 || dKmod4 == 3)
     575         392 :     dK *= 4;
     576        3606 :   if (f) *f = usqrt((ulong)(D/dK));
     577        3606 :   return D;
     578             : }
     579             : 
     580             : GEN
     581          49 : coredisc(GEN n)
     582             : {
     583          49 :   pari_sp av = avma;
     584          49 :   GEN c = core(n);
     585          49 :   if (_mod4(c)<=1) return c; /* c = 0 or 1 mod 4 */
     586           7 :   return gerepileuptoint(av, shifti(c,2));
     587             : }
     588             : 
     589             : GEN
     590           7 : coredisc2(GEN n)
     591             : {
     592           7 :   pari_sp av = avma;
     593           7 :   GEN y = core2_i(n);
     594           7 :   GEN c = gel(y,1), f = gel(y,2);
     595           7 :   if (_mod4(c)<=1) return gerepilecopy(av, y);
     596           7 :   y = cgetg(3,t_VEC);
     597           7 :   gel(y,1) = shifti(c,2);
     598           7 :   gel(y,2) = gmul2n(f,-1); return gerepileupto(av, y);
     599             : }
     600             : 
     601             : GEN
     602          14 : coredisc0(GEN n,long flag) { return flag? coredisc2(n): coredisc(n); }
     603             : 
     604             : long
     605         815 : omegau(ulong n)
     606             : {
     607             :   pari_sp av;
     608             :   GEN F;
     609         815 :   if (n == 1UL) return 0;
     610         801 :   av = avma; F = factoru(n);
     611         801 :   avma = av; return lg(gel(F,1))-1;
     612             : }
     613             : long
     614        1589 : omega(GEN n)
     615             : {
     616             :   pari_sp av;
     617             :   GEN F, P;
     618        1589 :   if ((F = check_arith_non0(n,"omega"))) {
     619             :     long n;
     620         721 :     P = gel(F,1); n = lg(P)-1;
     621         721 :     if (n && equalim1(gel(P,1))) n--;
     622         721 :     return n;
     623             :   }
     624         854 :   if (lgefint(n) == 3) return omegau(n[2]);
     625          39 :   av = avma;
     626          39 :   F = absZ_factor(n);
     627          39 :   P = gel(F,1); avma = av; return lg(P)-1;
     628             : }
     629             : 
     630             : long
     631         821 : bigomegau(ulong n)
     632             : {
     633             :   pari_sp av;
     634             :   GEN F;
     635         821 :   if (n == 1) return 0;
     636         807 :   av = avma; F = factoru(n);
     637         807 :   avma = av; return zv_sum(gel(F,2));
     638             : }
     639             : long
     640        1589 : bigomega(GEN n)
     641             : {
     642        1589 :   pari_sp av = avma;
     643             :   GEN F, E;
     644        1589 :   if ((F = check_arith_non0(n,"bigomega")))
     645             :   {
     646         721 :     GEN P = gel(F,1);
     647         721 :     long n = lg(P)-1;
     648         721 :     E = gel(F,2);
     649         721 :     if (n && equalim1(gel(P,1))) E = vecslice(E,2,n);
     650             :   }
     651         854 :   else if (lgefint(n) == 3)
     652         821 :     return bigomegau(n[2]);
     653             :   else
     654          33 :     E = gel(absZ_factor(n), 2);
     655         754 :   E = ZV_to_zv(E);
     656         754 :   avma = av; return zv_sum(E);
     657             : }
     658             : 
     659             : /* assume f = factoru(n), possibly with 0 exponents. Return phi(n) */
     660             : ulong
     661      291932 : eulerphiu_fact(GEN f)
     662             : {
     663      291932 :   GEN P = gel(f,1), E = gel(f,2);
     664      291932 :   long i, m = 1, l = lg(P);
     665      892173 :   for (i = 1; i < l; i++)
     666             :   {
     667      600241 :     ulong p = P[i], e = E[i];
     668      600241 :     if (!e) continue;
     669      600241 :     if (p == 2)
     670      246040 :     { if (e > 1) m <<= e-1; }
     671             :     else
     672             :     {
     673      354201 :       m *= (p-1);
     674      354201 :       if (e > 1) m *= upowuu(p, e-1);
     675             :     }
     676             :   }
     677      291932 :   return m;
     678             : }
     679             : ulong
     680      149964 : eulerphiu(ulong n)
     681             : {
     682      149964 :   pari_sp av = avma;
     683             :   GEN F;
     684      149964 :   if (!n) return 2;
     685      149964 :   F = factoru(n);
     686      149964 :   avma = av; return eulerphiu_fact(F);
     687             : }
     688             : GEN
     689      145607 : eulerphi(GEN n)
     690             : {
     691      145607 :   pari_sp av = avma;
     692             :   GEN Q, F, P, E;
     693             :   long i, l;
     694             : 
     695      145607 :   if ((F = check_arith_all(n,"eulerphi")))
     696             :   {
     697         735 :     F = clean_Z_factor(F);
     698         735 :     n = arith_n(n);
     699         735 :     if (lgefint(n) == 3)
     700             :     {
     701             :       ulong e;
     702         721 :       F = mkmat2(ZV_to_nv(gel(F,1)), ZV_to_nv(gel(F,2)));
     703         721 :       e = eulerphiu_fact(F);
     704         721 :       avma = av; return utoipos(e);
     705             :     }
     706             :   }
     707      144872 :   else if (lgefint(n) == 3) return utoipos(eulerphiu(uel(n,2)));
     708             :   else
     709          53 :     F = absZ_factor(n);
     710          67 :   if (!signe(n)) return gen_2;
     711          39 :   P = gel(F,1);
     712          39 :   E = gel(F,2); l = lg(P);
     713          39 :   Q = cgetg(l, t_VEC);
     714         252 :   for (i = 1; i < l; i++)
     715             :   {
     716         213 :     GEN p = gel(P,i), q;
     717         213 :     ulong v = itou(gel(E,i));
     718         213 :     q = subiu(p,1);
     719         213 :     if (v != 1) q = mulii(q, v == 2? p: powiu(p, v-1));
     720         213 :     gel(Q,i) = q;
     721             :   }
     722          39 :   return gerepileuptoint(av, ZV_prod(Q));
     723             : }
     724             : 
     725             : static GEN
     726         760 : numdiv_aux(GEN F)
     727             : {
     728         760 :   GEN x, E = gel(F,2);
     729         760 :   long i, l = lg(E);
     730         760 :   x = cgetg(l, t_VECSMALL);
     731         760 :   for (i=1; i<l; i++) x[i] = itou(gel(E,i))+1;
     732         760 :   return x;
     733             : }
     734             : GEN
     735        1589 : numdiv(GEN n)
     736             : {
     737        1589 :   pari_sp av = avma;
     738             :   GEN F, E;
     739             :   long i, l;
     740        1589 :   if ((F = check_arith_non0(n,"numdiv")))
     741             :   {
     742         721 :     F = clean_Z_factor(F);
     743         721 :     E = numdiv_aux(F);
     744             :   }
     745         854 :   else if (lgefint(n) == 3)
     746             :   {
     747         815 :     if (n[2] == 1) return gen_1;
     748         801 :     F = factoru(n[2]);
     749         801 :     E = gel(F,2); l = lg(E);
     750         801 :     for (i=1; i<l; i++) E[i]++;
     751             :   }
     752             :   else
     753          39 :     E = numdiv_aux(absZ_factor(n));
     754        1561 :   return gerepileuptoint(av, zv_prod_Z(E));
     755             : }
     756             : 
     757             : /* 1 + p + ... + p^v, p != 2^BIL - 1 */
     758             : static GEN
     759        2720 : u_euler_sumdiv(ulong p, long v)
     760             : {
     761        2720 :   GEN u = utoipos(1 + p); /* can't overflow */
     762        2720 :   for (; v > 1; v--) u = addui(1, mului(p, u));
     763        2720 :   return u;
     764             : }
     765             : /* 1 + q + ... + q^v */
     766             : static GEN
     767       17188 : euler_sumdiv(GEN q, long v)
     768             : {
     769       17188 :   GEN u = addui(1, q);
     770       17188 :   for (; v > 1; v--) u = addui(1, mulii(q, u));
     771       17188 :   return u;
     772             : }
     773             : 
     774             : static GEN
     775        1474 : sumdiv_aux(GEN F)
     776             : {
     777        1474 :   GEN x, P = gel(F,1), E = gel(F,2);
     778        1474 :   long i, l = lg(P);
     779        1474 :   x = cgetg(l, t_VEC);
     780        1474 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(gel(P,i), itou(gel(E,i)));
     781        1474 :   return ZV_prod(x);
     782             : }
     783             : GEN
     784        3017 : sumdiv(GEN n)
     785             : {
     786        3017 :   pari_sp av = avma;
     787             :   GEN F, v;
     788             : 
     789        3017 :   if ((F = check_arith_non0(n,"sumdiv")))
     790             :   {
     791        1442 :     F = clean_Z_factor(F);
     792        1442 :     v = sumdiv_aux(F);
     793             :   }
     794        1561 :   else if (lgefint(n) == 3)
     795             :   {
     796        1529 :     if (n[2] == 1) return gen_1;
     797        1501 :     F = factoru(n[2]);
     798        1501 :     v = usumdiv_fact(F);
     799             :   }
     800             :   else
     801          32 :     v = sumdiv_aux(absZ_factor(n));
     802        2975 :   return gerepileuptoint(av, v);
     803             : }
     804             : 
     805             : static GEN
     806        1506 : sumdivk_aux(GEN F, long k)
     807             : {
     808        1506 :   GEN x, P = gel(F,1), E = gel(F,2);
     809        1506 :   long i, l = lg(P);
     810        1506 :   x = cgetg(l, t_VEC);
     811        1506 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(powiu(gel(P,i),k), gel(E,i)[2]);
     812        1506 :   return ZV_prod(x);
     813             : }
     814             : GEN
     815        7987 : sumdivk(GEN n, long k)
     816             : {
     817        7987 :   pari_sp av = avma;
     818             :   GEN F, v;
     819             :   long k1;
     820             : 
     821        7987 :   if (!k) return numdiv(n);
     822        7987 :   if (k == 1) return sumdiv(n);
     823        6398 :   if ((F = check_arith_non0(n,"sumdivk"))) F = clean_Z_factor(F);
     824        6356 :   k1 = k; if (k < 0)  k = -k;
     825        6356 :   if (k == 1)
     826        1428 :     v = sumdiv(F? F: n);
     827             :   else
     828             :   {
     829        4928 :     if (F)
     830        1442 :       v = sumdivk_aux(F,k);
     831        3486 :     else if (lgefint(n) == 3)
     832             :     {
     833        3422 :       if (n[2] == 1) return gen_1;
     834        3324 :       F = factoru(n[2]);
     835        3324 :       v = usumdivk_fact(F,k);
     836             :     }
     837             :     else
     838          64 :       v = sumdivk_aux(absZ_factor(n), k);
     839        4830 :     if (k1 > 0) return gerepileuptoint(av, v);
     840             :   }
     841             : 
     842        1435 :   if (F) n = arith_n(n);
     843        1435 :   if (k != 1) n = powiu(n,k);
     844        1435 :   return gerepileupto(av, gdiv(v, n));
     845             : }
     846             : 
     847             : GEN
     848        1501 : usumdiv_fact(GEN f)
     849             : {
     850        1501 :   GEN P = gel(f,1), E = gel(f,2);
     851        1501 :   long i, l = lg(P);
     852        1501 :   GEN v = cgetg(l, t_VEC);
     853        1501 :   for (i=1; i<l; i++) gel(v,i) = u_euler_sumdiv(P[i],E[i]);
     854        1501 :   return ZV_prod(v);
     855             : }
     856             : GEN
     857        8371 : usumdivk_fact(GEN f, ulong k)
     858             : {
     859        8371 :   GEN P = gel(f,1), E = gel(f,2);
     860        8371 :   long i, l = lg(P);
     861        8371 :   GEN v = cgetg(l, t_VEC);
     862        8371 :   for (i=1; i<l; i++) gel(v,i) = euler_sumdiv(powuu(P[i],k),E[i]);
     863        8371 :   return ZV_prod(v);
     864             : }
     865             : 
     866             : long
     867        1155 : uissquarefree_fact(GEN f)
     868             : {
     869        1155 :   GEN E = gel(f,2);
     870        1155 :   long i, l = lg(E);
     871        2373 :   for (i = 1; i < l; i++)
     872        1771 :     if (E[i] > 1) return 0;
     873         602 :   return 1;
     874             : }
     875             : long
     876       17715 : uissquarefree(ulong n)
     877             : {
     878       17715 :   if (!n) return 0;
     879       17715 :   return moebiusu(n)? 1: 0;
     880             : }
     881             : long
     882        2179 : Z_issquarefree(GEN n)
     883             : {
     884        2179 :   switch(lgefint(n))
     885             :   {
     886          14 :     case 2: return 0;
     887        1406 :     case 3: return uissquarefree(n[2]);
     888             :   }
     889         759 :   return moebius(n)? 1: 0;
     890             : }
     891             : 
     892             : static int
     893        1407 : fa_issquarefree(GEN F)
     894             : {
     895        1407 :   GEN P = gel(F,1), E = gel(F,2);
     896        1407 :   long i, s, l = lg(P);
     897        1407 :   if (l == 1) return 1;
     898        1400 :   s = signe(gel(P,1)); /* = signe(x) */
     899        1400 :   if (!s) return 0;
     900        3577 :   for(i = 1; i < l; i++)
     901        2730 :     if (!equali1(gel(E,i))) return 0;
     902         847 :   return 1;
     903             : }
     904             : long
     905        3913 : issquarefree(GEN x)
     906             : {
     907             :   pari_sp av;
     908             :   GEN d;
     909        3913 :   switch(typ(x))
     910             :   {
     911        1421 :     case t_INT: return Z_issquarefree(x);
     912             :     case t_POL:
     913        1085 :       if (!signe(x)) return 0;
     914        1085 :       av = avma; d = RgX_gcd(x, RgX_deriv(x));
     915        1085 :       avma = av; return (lg(d) == 3);
     916             :     case t_VEC:
     917        1407 :     case t_MAT: return fa_issquarefree(check_arith_all(x,"issquarefree"));
     918           0 :     default: pari_err_TYPE("issquarefree",x);
     919             :       return 0; /* LCOV_EXCL_LINE */
     920             :   }
     921             : }
     922             : 
     923             : /*********************************************************************/
     924             : /**                                                                 **/
     925             : /**                    DIGITS / SUM OF DIGITS                       **/
     926             : /**                                                                 **/
     927             : /*********************************************************************/
     928             : 
     929             : /* set v[i] = 1 iff B^i is needed in the digits_dac algorithm */
     930             : static void
     931     2491847 : set_vexp(GEN v, long l)
     932             : {
     933             :   long m;
     934     4983694 :   if (v[l]) return;
     935      824042 :   v[l] = 1; m = l>>1;
     936      824042 :   set_vexp(v, m);
     937      824042 :   set_vexp(v, l-m);
     938             : }
     939             : 
     940             : /* return all needed B^i for DAC algorithm, for lz digits */
     941             : static GEN
     942      843763 : get_vB(GEN T, long lz, void *E, struct bb_ring *r)
     943             : {
     944      843763 :   GEN vB, vexp = const_vecsmall(lz, 0);
     945      843763 :   long i, l = (lz+1) >> 1;
     946      843763 :   vexp[1] = 1;
     947      843763 :   vexp[2] = 1;
     948      843763 :   set_vexp(vexp, lz);
     949      843763 :   vB = zerovec(lz); /* unneeded entries remain = 0 */
     950      843763 :   gel(vB, 1) = T;
     951     1729367 :   for (i = 2; i <= l; i++)
     952      885604 :     if (vexp[i])
     953             :     {
     954      824042 :       long j = i >> 1;
     955      824042 :       GEN B2j = r->sqr(E, gel(vB,j));
     956      824042 :       gel(vB,i) = odd(i)? r->mul(E, B2j, T): B2j;
     957             :     }
     958      843763 :   return vB;
     959             : }
     960             : 
     961             : static void
     962      186422 : gen_digits_dac(GEN x, GEN vB, long l, GEN *z,
     963             :                void *E, GEN div(void *E, GEN a, GEN b, GEN *r))
     964             : {
     965             :   GEN q, r;
     966      186422 :   long m = l>>1;
     967      372844 :   if (l==1) { *z=x; return; }
     968       89105 :   q = div(E, x, gel(vB,m), &r);
     969       89105 :   gen_digits_dac(r, vB, m, z, E, div);
     970       89105 :   gen_digits_dac(q, vB, l-m, z+m, E, div);
     971             : }
     972             : 
     973             : static GEN
     974       28889 : gen_fromdigits_dac(GEN x, GEN vB, long i, long l, void *E,
     975             :                    GEN add(void *E, GEN a, GEN b),
     976             :                    GEN mul(void *E, GEN a, GEN b))
     977             : {
     978             :   GEN a, b;
     979       28889 :   long m = l>>1;
     980       28889 :   if (l==1) return gel(x,i);
     981       13181 :   a = gen_fromdigits_dac(x, vB, i, m, E, add, mul);
     982       13181 :   b = gen_fromdigits_dac(x, vB, i+m, l-m, E, add, mul);
     983       13181 :   return add(E, a, mul(E, b, gel(vB, m)));
     984             : }
     985             : 
     986             : static GEN
     987        8499 : gen_digits_i(GEN x, GEN B, long n, void *E, struct bb_ring *r,
     988             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
     989             : {
     990             :   GEN z, vB;
     991        8499 :   if (n==1) retmkvec(gcopy(x));
     992        8212 :   vB = get_vB(B, n, E, r);
     993        8212 :   z = cgetg(n+1, t_VEC);
     994        8212 :   gen_digits_dac(x, vB, n, (GEN*)(z+1), E, div);
     995        8212 :   return z;
     996             : }
     997             : 
     998             : GEN
     999        8449 : gen_digits(GEN x, GEN B, long n, void *E, struct bb_ring *r,
    1000             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
    1001             : {
    1002        8449 :   pari_sp av = avma;
    1003        8449 :   return gerepilecopy(av, gen_digits_i(x, B, n, E, r, div));
    1004             : }
    1005             : 
    1006             : GEN
    1007        2527 : gen_fromdigits(GEN x, GEN B, void *E, struct bb_ring *r)
    1008             : {
    1009        2527 :   pari_sp av = avma;
    1010        2527 :   long n = lg(x)-1;
    1011        2527 :   GEN vB = get_vB(B, n, E, r);
    1012        2527 :   GEN z = gen_fromdigits_dac(x, vB, 1, n, E, r->add, r->mul);
    1013        2527 :   return gerepilecopy(av, z);
    1014             : }
    1015             : 
    1016             : static GEN
    1017        1771 : _addii(void *data /* ignored */, GEN x, GEN y)
    1018        1771 : { (void)data; return addii(x,y); }
    1019             : static GEN
    1020      794229 : _sqri(void *data /* ignored */, GEN x) { (void)data; return sqri(x); }
    1021             : static GEN
    1022      200068 : _mulii(void *data /* ignored */, GEN x, GEN y)
    1023      200068 :  { (void)data; return mulii(x,y); }
    1024             : static GEN
    1025         436 : _dvmdii(void *data /* ignored */, GEN x, GEN y, GEN *r)
    1026         436 :  { (void)data; return dvmdii(x,y,r); }
    1027             : 
    1028             : static struct bb_ring Z_ring = { _addii, _mulii, _sqri };
    1029             : 
    1030             : static GEN
    1031         182 : check_basis(GEN B)
    1032             : {
    1033         182 :   if (!B) return utoipos(10);
    1034         161 :   if (typ(B)!=t_INT) pari_err_TYPE("digits",B);
    1035         161 :   if (abscmpiu(B,2)<0) pari_err_DOMAIN("digits","B","<",gen_2,B);
    1036         161 :   return B;
    1037             : }
    1038             : 
    1039             : /* x has l digits in base B, write them to z[0..l-1], vB[i] = B^i */
    1040             : static void
    1041        3075 : digits_dacsmall(GEN x, GEN vB, long l, ulong* z)
    1042             : {
    1043        3075 :   pari_sp av = avma;
    1044             :   GEN q,r;
    1045             :   long m;
    1046        6150 :   if (l==1) { *z=itou(x); return; }
    1047        1524 :   m=l>>1;
    1048        1524 :   q = dvmdii(x, gel(vB,m), &r);
    1049        1524 :   digits_dacsmall(q,vB,l-m,z);
    1050        1524 :   digits_dacsmall(r,vB,m,z+l-m);
    1051        1524 :   avma = av;
    1052             : }
    1053             : 
    1054             : GEN
    1055          56 : digits(GEN x, GEN B)
    1056             : {
    1057          56 :   pari_sp av=avma;
    1058             :   long lz;
    1059             :   GEN z, vB;
    1060          56 :   if (typ(x)!=t_INT) pari_err_TYPE("digits",x);
    1061          56 :   B = check_basis(B);
    1062          56 :   if (signe(B)<0) pari_err_DOMAIN("digits","B","<",gen_0,B);
    1063          56 :   if (!signe(x))       {avma = av; return cgetg(1,t_VEC); }
    1064          49 :   if (abscmpii(x,B)<0) {avma = av; retmkvec(absi(x)); }
    1065          49 :   if (Z_ispow2(B))
    1066             :   {
    1067          14 :     long k = expi(B);
    1068          14 :     if (k == 1) return binaire(x);
    1069           7 :     if (k < BITS_IN_LONG)
    1070             :     {
    1071           0 :       (void)new_chunk(4*(expi(x) + 2)); /* HACK */
    1072           0 :       z = binary_2k_nv(x, k);
    1073           0 :       avma = av; return Flv_to_ZV(z);
    1074             :     }
    1075             :     else
    1076             :     {
    1077           7 :       avma = av; return binary_2k(x, k);
    1078             :     }
    1079             :   }
    1080          35 :   if (signe(x) < 0) x = absi(x);
    1081          35 :   lz = logint(x,B) + 1;
    1082          35 :   if (lgefint(B)>3)
    1083             :   {
    1084           8 :     z = gerepileupto(av, gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii));
    1085           8 :     vecreverse_inplace(z);
    1086           8 :     return z;
    1087             :   }
    1088             :   else
    1089             :   {
    1090          27 :     vB = get_vB(B, lz, NULL, &Z_ring);
    1091          27 :     (void)new_chunk(3*lz); /* HACK */
    1092          27 :     z = zero_zv(lz);
    1093          27 :     digits_dacsmall(x,vB,lz,(ulong*)(z+1));
    1094          27 :     avma = av; return Flv_to_ZV(z);
    1095             :   }
    1096             : }
    1097             : 
    1098             : static GEN
    1099     2622631 : fromdigitsu_dac(GEN x, GEN vB, long i, long l)
    1100             : {
    1101             :   GEN a, b;
    1102     2622631 :   long m = l>>1;
    1103     2622631 :   if (l==1) return utoi(uel(x,i));
    1104     2163766 :   if (l==2) return addumului(uel(x,i), uel(x,i+1), gel(vB, m));
    1105      894817 :   a = fromdigitsu_dac(x, vB, i, m);
    1106      894817 :   b = fromdigitsu_dac(x, vB, i+m, l-m);
    1107      894817 :   return addii(a, mulii(b, gel(vB, m)));
    1108             : }
    1109             : 
    1110             : GEN
    1111      832997 : fromdigitsu(GEN x, GEN B)
    1112             : {
    1113      832997 :   pari_sp av = avma;
    1114      832997 :   long n = lg(x)-1;
    1115             :   GEN vB, z;
    1116      832997 :   if (n==0) return gen_0;
    1117      832997 :   vB = get_vB(B, n, NULL, &Z_ring);
    1118      832997 :   z = fromdigitsu_dac(x, vB, 1, n);
    1119      832997 :   return gerepileuptoint(av, z);
    1120             : }
    1121             : 
    1122             : static int
    1123          14 : ZV_in_range(GEN v, GEN B)
    1124             : {
    1125          14 :   long i, l = lg(v);
    1126        1659 :   for(i=1; i < l; i++)
    1127             :   {
    1128        1652 :     GEN vi = gel(v, i);
    1129        1652 :     if (signe(vi) < 0 || cmpii(vi, B) >= 0)
    1130           7 :       return 0;
    1131             :   }
    1132           7 :   return 1;
    1133             : }
    1134             : 
    1135             : GEN
    1136          56 : fromdigits(GEN x, GEN B)
    1137             : {
    1138          56 :   pari_sp av = avma;
    1139          56 :   if (typ(x)!=t_VEC || !RgV_is_ZV(x)) pari_err_TYPE("fromdigits",x);
    1140          56 :   if (lg(x)==1) return gen_0;
    1141          49 :   B = check_basis(B);
    1142          49 :   if (Z_ispow2(B) && ZV_in_range(x, B))
    1143           7 :     return fromdigits_2k(x, expi(B));
    1144          42 :   x = vecreverse(x);
    1145          42 :   return gerepileuptoint(av, gen_fromdigits(x, B, NULL, &Z_ring));
    1146             : }
    1147             : 
    1148             : static const ulong digsum[] ={
    1149             :   0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,
    1150             :   9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,
    1151             :   12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,
    1152             :   12,13,14,15,16,17,18,1,2,3,4,5,6,7,8,9,10,2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,
    1153             :   9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,
    1154             :   12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,
    1155             :   12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,2,3,4,5,6,7,8,9,10,11,3,
    1156             :   4,5,6,7,8,9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,
    1157             :   9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,
    1158             :   9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,
    1159             :   16,17,18,19,20,3,4,5,6,7,8,9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,
    1160             :   11,12,13,14,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,
    1161             :   12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,
    1162             :   19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,21,4,5,6,7,8,9,
    1163             :   10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,
    1164             :   12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,
    1165             :   11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,
    1166             :   18,19,20,21,13,14,15,16,17,18,19,20,21,22,5,6,7,8,9,10,11,12,13,14,6,7,8,9,
    1167             :   10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,
    1168             :   10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,
    1169             :   17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,20,21,22,14,
    1170             :   15,16,17,18,19,20,21,22,23,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,
    1171             :   15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,12,13,
    1172             :   14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,
    1173             :   21,13,14,15,16,17,18,19,20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,17,18,
    1174             :   19,20,21,22,23,24,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,
    1175             :   10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,
    1176             :   17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,20,21,22,14,
    1177             :   15,16,17,18,19,20,21,22,23,15,16,17,18,19,20,21,22,23,24,16,17,18,19,20,21,
    1178             :   22,23,24,25,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,
    1179             :   12,13,14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,
    1180             :   19,20,21,13,14,15,16,17,18,19,20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,
    1181             :   17,18,19,20,21,22,23,24,16,17,18,19,20,21,22,23,24,25,17,18,19,20,21,22,23,
    1182             :   24,25,26,9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,
    1183             :   13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,
    1184             :   20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,17,18,19,20,21,22,23,24,16,17,
    1185             :   18,19,20,21,22,23,24,25,17,18,19,20,21,22,23,24,25,26,18,19,20,21,22,23,24,
    1186             :   25,26,27
    1187             : };
    1188             : 
    1189             : ulong
    1190      355152 : sumdigitsu(ulong n)
    1191             : {
    1192      355152 :   ulong s = 0;
    1193      355152 :   while (n) { s += digsum[n % 1000]; n /= 1000; }
    1194      355152 :   return s;
    1195             : }
    1196             : 
    1197             : /* res=array of 9-digits integers, return \sum_{0 <= i < l} sumdigits(res[i]) */
    1198             : static ulong
    1199          14 : sumdigits_block(ulong *res, long l)
    1200             : {
    1201          14 :   long s = sumdigitsu(*--res);
    1202          14 :   while (--l > 0) s += sumdigitsu(*--res);
    1203          14 :   return s;
    1204             : }
    1205             : 
    1206             : GEN
    1207          35 : sumdigits(GEN n)
    1208             : {
    1209          35 :   pari_sp av = avma;
    1210             :   ulong s, *res;
    1211             :   long l;
    1212             : 
    1213          35 :   if (typ(n) != t_INT) pari_err_TYPE("sumdigits", n);
    1214          35 :   l = lgefint(n);
    1215          35 :   switch(l)
    1216             :   {
    1217           7 :     case 2: return gen_0;
    1218          14 :     case 3: return utoipos(sumdigitsu(n[2]));
    1219             :   }
    1220          14 :   res = convi(n, &l);
    1221          14 :   if ((ulong)l < ULONG_MAX / 81)
    1222             :   {
    1223          14 :     s = sumdigits_block(res, l);
    1224          14 :     avma = av; return utoipos(s);
    1225             :   }
    1226             :   else /* Huge. Overflows ulong */
    1227             :   {
    1228           0 :     const long L = (long)(ULONG_MAX / 81);
    1229           0 :     GEN S = gen_0;
    1230           0 :     while (l > L)
    1231             :     {
    1232           0 :       S = addiu(S, sumdigits_block(res, L));
    1233           0 :       res += L; l -= L;
    1234             :     }
    1235           0 :     if (l)
    1236           0 :       S = addiu(S, sumdigits_block(res, l));
    1237           0 :     return gerepileuptoint(av, S);
    1238             :   }
    1239             : }
    1240             : 
    1241             : GEN
    1242         105 : sumdigits0(GEN x, GEN B)
    1243             : {
    1244         105 :   pari_sp av = avma;
    1245             :   GEN z;
    1246             :   long lz;
    1247             : 
    1248         105 :   if (!B) return sumdigits(x);
    1249          77 :   if (typ(x) != t_INT) pari_err_TYPE("sumdigits", x);
    1250          77 :   B = check_basis(B);
    1251          77 :   if (Z_ispow2(B))
    1252             :   {
    1253          28 :     long k = expi(B);
    1254          28 :     if (k == 1) { avma = av; return utoi(hammingweight(x)); }
    1255          21 :     if (k < BITS_IN_LONG)
    1256             :     {
    1257          14 :       GEN z = binary_2k_nv(x, k);
    1258          14 :       if (lg(z)-1 > 1L<<(BITS_IN_LONG-k)) /* may overflow */
    1259           0 :         return gerepileuptoint(av, ZV_sum(Flv_to_ZV(z)));
    1260          14 :       avma = av; return utoi(zv_sum(z));
    1261             :     }
    1262           7 :     return gerepileuptoint(av, ZV_sum(binary_2k(x, k)));
    1263             :   }
    1264          49 :   if (!signe(x))       { avma = av; return gen_0; }
    1265          49 :   if (abscmpii(x,B)<0) { avma = av; return absi(x); }
    1266          49 :   if (absequaliu(B,10))   { avma = av; return sumdigits(x); }
    1267          42 :   lz = logint(x,B) + 1;
    1268          42 :   z = gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii);
    1269          42 :   return gerepileuptoint(av, ZV_sum(z));
    1270             : }

Generated by: LCOV version 1.11