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 21343-6216058) Lines: 600 642 93.5 %
Date: 2017-11-19 06:21:17 Functions: 88 91 96.7 %
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       10794 : Z_smoothen(GEN N, GEN L, GEN *pP, GEN *pe)
      44             : {
      45       10794 :   long i, j, l = lg(L);
      46       10794 :   GEN e = new_chunk(l), P = new_chunk(l);
      47       93026 :   for (i = j = 1; i < l; i++)
      48             :   {
      49       88865 :     ulong p = uel(L,i);
      50       88865 :     long v = Z_lvalrem(N, p, &N);
      51       88865 :     if (v) { P[j] = p; e[j] = v; j++; if (is_pm1(N)) { N = NULL; break; } }
      52             :   }
      53       10794 :   P[0] = evaltyp(t_VECSMALL) | evallg(j); *pP = P;
      54       10794 :   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       30292 : factoru_pow(ulong n)
      65             : {
      66       30292 :   GEN f = cgetg(4,t_VEC);
      67       30285 :   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       30285 :   (void)new_chunk((15 + 1)*3);
      72       30292 :   F = factoru(n);
      73       30338 :   P = gel(F,1);
      74       30338 :   E = gel(F,2); l = lg(P);
      75       30338 :   avma = av;
      76       30338 :   gel(f,1) = p = cgetg(l,t_VECSMALL);
      77       30338 :   gel(f,2) = e = cgetg(l,t_VECSMALL);
      78       30324 :   gel(f,3) = c = cgetg(l,t_VECSMALL);
      79       96088 :   for(i = 1; i < l; i++)
      80             :   {
      81       65744 :     p[i] = P[i];
      82       65744 :     e[i] = E[i];
      83       65744 :     c[i] = upowuu(p[i], e[i]);
      84             :   }
      85       30344 :   return f;
      86             : }
      87             : 
      88             : static GEN
      89       87566 : factorlim(GEN n, ulong lim)
      90       87566 : { 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       67364 : factor_pn_1_limit(GEN p, long n, ulong lim)
      95             : {
      96       67364 :   pari_sp av = avma;
      97       67364 :   GEN A = factorlim(subiu(p,1), lim), d = divisorsu(n);
      98       67364 :   long i, pp = itos_or_0(p);
      99       82372 :   for(i=2; i<lg(d); i++)
     100             :   {
     101             :     GEN B;
     102       27090 :     if (pp && d[i]%pp==0 && (
     103       24164 :        ((pp&3)==1 && (d[i]&1)) ||
     104       12173 :        ((pp&3)==3 && (d[i]&3)==2) ||
     105       11977 :        (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        9814 :       B = factorlim(polcyclo_eval(d[i],p), lim);
     114       15008 :     A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     115             :   }
     116       67364 :   return gerepilecopy(av, A);
     117             : }
     118             : GEN
     119       67364 : factor_pn_1(GEN p, ulong n)
     120       67364 : { 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     4429671 : RgV_is_ZVpos(GEN v)
     186             : {
     187     4429671 :   long i, l = lg(v);
     188    13466527 :   for (i = 1; i < l; i++)
     189             :   {
     190     9041742 :     GEN c = gel(v,i);
     191     9041742 :     if (typ(c) != t_INT || signe(c) <= 0) return 0;
     192             :   }
     193     4424785 :   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       13475 : RgV_is_ZV0(GEN v)
     210             : {
     211       13475 :   long i, l = lg(v);
     212       34741 :   for (i = 1; i < l; i++)
     213             :   {
     214       21371 :     GEN c = gel(v,i);
     215             :     long s;
     216       21371 :     if (typ(c) != t_INT) return 0;
     217       21371 :     s = signe(c);
     218       21371 :     if (!s) return (l == 2);
     219             :   }
     220       13370 :   return 1;
     221             : }
     222             : 
     223             : static int
     224       22967 : RgV_is_prV(GEN v)
     225             : {
     226       22967 :   long l = lg(v), i;
     227       23324 :   for (i = 1; i < l; i++)
     228       22967 :     if (!checkprid_i(gel(v,i))) return 0;
     229         357 :   return 1;
     230             : }
     231             : int
     232       26775 : is_nf_factor(GEN F)
     233             : {
     234       79065 :   return typ(F) == t_MAT && lg(F) == 3
     235       49735 :     && 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     2222273 : is_Z_factor_i(GEN f)
     246     2222273 : { return typ(f) == t_MAT && lg(f) == 3 && RgV_is_ZVpos(gel(f,2)); }
     247             : int
     248     2202183 : is_Z_factorpos(GEN f)
     249     2202183 : { return is_Z_factor_i(f) && RgV_is_ZVpos(gel(f,1)); }
     250             : int
     251       13475 : is_Z_factor(GEN f)
     252       13475 : { 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       13419 : clean_Z_factor(GEN f)
     259             : {
     260       13419 :   GEN P = gel(f,1);
     261       13419 :   long n = lg(P)-1;
     262       13419 :   if (n && equalim1(gel(P,1)))
     263        2513 :     return mkmat2(vecslice(P,2,n), vecslice(gel(f,2),2,n));
     264       10906 :   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       76330 : check_arith_non0(GEN n, const char *f) {
     305       76330 :   switch(typ(n))
     306             :   {
     307             :     case t_INT:
     308       69905 :       if (!signe(n)) pari_err_DOMAIN(f, "argument", "=", gen_0, gen_0);
     309       69867 :       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      231280 : check_arith_all(GEN n, const char *f) {
     323      231280 :   switch(typ(n))
     324             :   {
     325             :     case t_INT:
     326      217805 :       return NULL;
     327             :     case t_VEC:
     328        7602 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT) break;
     329        7602 :       n = gel(n,2); /* fall through */
     330             :     case t_MAT:
     331       13475 :       if (!is_Z_factor(n)) break;
     332       13475 :       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    10602414 : divisorsu_fact(GEN fa)
     480             : {
     481    10602414 :   GEN d, t, t1, t2, t3, P = gel(fa,1), E = gel(fa,2);
     482    10602414 :   long i, j, l = lg(P);
     483    10602414 :   d = t = cgetg(numdivu_fact(fa) + 1,t_VECSMALL);
     484    10602414 :   *++d = 1;
     485    35922743 :   for (i=1; i<l; i++)
     486    54780400 :     for (t1=t,j=E[i]; j; j--,t1=t2)
     487    29460071 :       for (t2=d, t3=t1; t3<t2; ) *(++d) = *(++t3) * P[i];
     488    10602414 :   vecsmall_sort(t); return t;
     489             : }
     490             : GEN
     491      121733 : divisorsu(ulong N)
     492             : {
     493      121733 :   pari_sp av = avma;
     494      121733 :   return gerepileupto(av, divisorsu_fact(factoru(N)));
     495             : }
     496             : 
     497             : static GEN
     498           0 : corefa(GEN fa)
     499             : {
     500           0 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1;
     501             :   long i;
     502           0 :   for (i=1; i<lg(P); i++)
     503           0 :     if (mod2(gel(E,i))) c = mulii(c,gel(P,i));
     504           0 :   return c;
     505             : }
     506             : static GEN
     507        1176 : core2fa(GEN fa)
     508             : {
     509        1176 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1, f = gen_1;
     510             :   long i;
     511        2744 :   for (i=1; i<lg(P); i++)
     512             :   {
     513        1568 :     long e = itos(gel(E,i));
     514        1568 :     if (e & 1)  c = mulii(c, gel(P,i));
     515        1568 :     if (e != 1) f = mulii(f, powiu(gel(P,i), e >> 1));
     516             :   }
     517        1176 :   return mkvec2(c,f);
     518             : }
     519             : GEN
     520           0 : corepartial(GEN n, long all)
     521             : {
     522           0 :   pari_sp av = avma;
     523           0 :   if (typ(n) != t_INT) pari_err_TYPE("corepartial",n);
     524           0 :   return gerepileuptoint(av, corefa(Z_factor_limit(n,all)));
     525             : }
     526             : GEN
     527        1176 : core2partial(GEN n, long all)
     528             : {
     529        1176 :   pari_sp av = avma;
     530        1176 :   if (typ(n) != t_INT) pari_err_TYPE("core2partial",n);
     531        1176 :   return gerepilecopy(av, core2fa(Z_factor_limit(n,all)));
     532             : }
     533             : /* given an arithmetic function argument, return the underlying integer */
     534             : static GEN
     535        2912 : arith_n(GEN A)
     536             : {
     537        2912 :   switch(typ(A))
     538             :   {
     539         728 :     case t_INT: return A;
     540           7 :     case t_VEC: return gel(A,1);
     541        2177 :     default: return factorback(A);
     542             :   }
     543             : }
     544             : static GEN
     545        1484 : core2_i(GEN n)
     546             : {
     547        1484 :   GEN f = core(n);
     548        1484 :   if (!signe(f)) return mkvec2(gen_0, gen_1);
     549        1449 :   return mkvec2(f, sqrtint(diviiexact(arith_n(n), f)));
     550             : }
     551             : GEN
     552        1477 : core2(GEN n) { pari_sp av = avma; return gerepilecopy(av, core2_i(n)); }
     553             : 
     554             : GEN
     555        3094 : core0(GEN n,long flag) { return flag? core2(n): core(n); }
     556             : 
     557             : static long
     558          91 : _mod4(GEN c) {
     559          91 :   long r, s = signe(c);
     560          91 :   if (!s) return 0;
     561          91 :   r = mod4(c); if (s < 0) r = 4-r;
     562          91 :   return r;
     563             : }
     564             : 
     565             : long
     566        3606 : corediscs(long D, ulong *f)
     567             : {
     568             :   /* D = f^2 dK */
     569        3606 :   long dK = D>=0 ? (long) coreu(D) : -(long) coreu(-(ulong) D);
     570        3606 :   ulong dKmod4 = ((ulong)dK)&3UL;
     571        3606 :   if (dKmod4 == 2 || dKmod4 == 3)
     572         392 :     dK *= 4;
     573        3606 :   if (f) *f = usqrt((ulong)(D/dK));
     574        3606 :   return D;
     575             : }
     576             : 
     577             : GEN
     578          84 : coredisc(GEN n)
     579             : {
     580          84 :   pari_sp av = avma;
     581          84 :   GEN c = core(n);
     582          84 :   if (_mod4(c)<=1) return c; /* c = 0 or 1 mod 4 */
     583          21 :   return gerepileuptoint(av, shifti(c,2));
     584             : }
     585             : 
     586             : GEN
     587           7 : coredisc2(GEN n)
     588             : {
     589           7 :   pari_sp av = avma;
     590           7 :   GEN y = core2_i(n);
     591           7 :   GEN c = gel(y,1), f = gel(y,2);
     592           7 :   if (_mod4(c)<=1) return gerepilecopy(av, y);
     593           7 :   y = cgetg(3,t_VEC);
     594           7 :   gel(y,1) = shifti(c,2);
     595           7 :   gel(y,2) = gmul2n(f,-1); return gerepileupto(av, y);
     596             : }
     597             : 
     598             : GEN
     599          14 : coredisc0(GEN n,long flag) { return flag? coredisc2(n): coredisc(n); }
     600             : 
     601             : long
     602         815 : omegau(ulong n)
     603             : {
     604             :   pari_sp av;
     605             :   GEN F;
     606         815 :   if (n == 1UL) return 0;
     607         801 :   av = avma; F = factoru(n);
     608         801 :   avma = av; return lg(gel(F,1))-1;
     609             : }
     610             : long
     611        1589 : omega(GEN n)
     612             : {
     613             :   pari_sp av;
     614             :   GEN F, P;
     615        1589 :   if ((F = check_arith_non0(n,"omega"))) {
     616             :     long n;
     617         721 :     P = gel(F,1); n = lg(P)-1;
     618         721 :     if (n && equalim1(gel(P,1))) n--;
     619         721 :     return n;
     620             :   }
     621         854 :   if (lgefint(n) == 3) return omegau(n[2]);
     622          39 :   av = avma;
     623          39 :   F = absZ_factor(n);
     624          39 :   P = gel(F,1); avma = av; return lg(P)-1;
     625             : }
     626             : 
     627             : long
     628         821 : bigomegau(ulong n)
     629             : {
     630             :   pari_sp av;
     631             :   GEN F;
     632         821 :   if (n == 1) return 0;
     633         807 :   av = avma; F = factoru(n);
     634         807 :   avma = av; return zv_sum(gel(F,2));
     635             : }
     636             : long
     637        1589 : bigomega(GEN n)
     638             : {
     639        1589 :   pari_sp av = avma;
     640             :   GEN F, E;
     641        1589 :   if ((F = check_arith_non0(n,"bigomega")))
     642             :   {
     643         721 :     GEN P = gel(F,1);
     644         721 :     long n = lg(P)-1;
     645         721 :     E = gel(F,2);
     646         721 :     if (n && equalim1(gel(P,1))) E = vecslice(E,2,n);
     647             :   }
     648         854 :   else if (lgefint(n) == 3)
     649         821 :     return bigomegau(n[2]);
     650             :   else
     651          33 :     E = gel(absZ_factor(n), 2);
     652         754 :   E = ZV_to_zv(E);
     653         754 :   avma = av; return zv_sum(E);
     654             : }
     655             : 
     656             : /* assume f = factoru(n), possibly with 0 exponents. Return phi(n) */
     657             : ulong
     658      236653 : eulerphiu_fact(GEN f)
     659             : {
     660      236653 :   GEN P = gel(f,1), E = gel(f,2);
     661      236653 :   long i, m = 1, l = lg(P);
     662      782259 :   for (i = 1; i < l; i++)
     663             :   {
     664      545606 :     ulong p = P[i], e = E[i];
     665      545606 :     if (!e) continue;
     666      545606 :     if (p == 2)
     667      189648 :     { if (e > 1) m <<= e-1; }
     668             :     else
     669             :     {
     670      355958 :       m *= (p-1);
     671      355958 :       if (e > 1) m *= upowuu(p, e-1);
     672             :     }
     673             :   }
     674      236653 :   return m;
     675             : }
     676             : ulong
     677      150244 : eulerphiu(ulong n)
     678             : {
     679      150244 :   pari_sp av = avma;
     680             :   GEN F;
     681      150244 :   if (!n) return 2;
     682      150244 :   F = factoru(n);
     683      150244 :   avma = av; return eulerphiu_fact(F);
     684             : }
     685             : GEN
     686      145607 : eulerphi(GEN n)
     687             : {
     688      145607 :   pari_sp av = avma;
     689             :   GEN Q, F, P, E;
     690             :   long i, l;
     691             : 
     692      145607 :   if ((F = check_arith_all(n,"eulerphi")))
     693             :   {
     694         735 :     F = clean_Z_factor(F);
     695         735 :     n = arith_n(n);
     696         735 :     if (lgefint(n) == 3)
     697             :     {
     698             :       ulong e;
     699         721 :       F = mkmat2(ZV_to_nv(gel(F,1)), ZV_to_nv(gel(F,2)));
     700         721 :       e = eulerphiu_fact(F);
     701         721 :       avma = av; return utoipos(e);
     702             :     }
     703             :   }
     704      144872 :   else if (lgefint(n) == 3) return utoipos(eulerphiu(uel(n,2)));
     705             :   else
     706          53 :     F = absZ_factor(n);
     707          67 :   if (!signe(n)) return gen_2;
     708          39 :   P = gel(F,1);
     709          39 :   E = gel(F,2); l = lg(P);
     710          39 :   Q = cgetg(l, t_VEC);
     711         252 :   for (i = 1; i < l; i++)
     712             :   {
     713         213 :     GEN p = gel(P,i), q;
     714         213 :     ulong v = itou(gel(E,i));
     715         213 :     q = subiu(p,1);
     716         213 :     if (v != 1) q = mulii(q, v == 2? p: powiu(p, v-1));
     717         213 :     gel(Q,i) = q;
     718             :   }
     719          39 :   return gerepileuptoint(av, ZV_prod(Q));
     720             : }
     721             : 
     722             : long
     723    10603607 : numdivu_fact(GEN fa)
     724             : {
     725    10603607 :   GEN E = gel(fa,2);
     726    10603607 :   long n = 1, i, l = lg(E);
     727    10603607 :   for (i = 1; i < l; i++) n *= E[i]+1;
     728    10603607 :   return n;
     729             : }
     730             : long
     731         815 : numdivu(long N)
     732             : {
     733             :   pari_sp av;
     734             :   GEN fa;
     735         815 :   if (N == 1) return 1;
     736         801 :   av = avma; fa = factoru(N);
     737         801 :   avma = av; return numdivu_fact(fa);
     738             : }
     739             : static GEN
     740         760 : numdiv_aux(GEN F)
     741             : {
     742         760 :   GEN x, E = gel(F,2);
     743         760 :   long i, l = lg(E);
     744         760 :   x = cgetg(l, t_VECSMALL);
     745         760 :   for (i=1; i<l; i++) x[i] = itou(gel(E,i))+1;
     746         760 :   return x;
     747             : }
     748             : GEN
     749        1589 : numdiv(GEN n)
     750             : {
     751        1589 :   pari_sp av = avma;
     752             :   GEN F, E;
     753        1589 :   if ((F = check_arith_non0(n,"numdiv")))
     754             :   {
     755         721 :     F = clean_Z_factor(F);
     756         721 :     E = numdiv_aux(F);
     757             :   }
     758         854 :   else if (lgefint(n) == 3)
     759         815 :     return utoipos(numdivu(n[2]));
     760             :   else
     761          39 :     E = numdiv_aux(absZ_factor(n));
     762         760 :   return gerepileuptoint(av, zv_prod_Z(E));
     763             : }
     764             : 
     765             : /* 1 + p + ... + p^v, p != 2^BIL - 1 */
     766             : static GEN
     767       14648 : u_euler_sumdiv(ulong p, long v)
     768             : {
     769       14648 :   GEN u = utoipos(1 + p); /* can't overflow */
     770       14648 :   for (; v > 1; v--) u = addui(1, mului(p, u));
     771       14648 :   return u;
     772             : }
     773             : /* 1 + q + ... + q^v */
     774             : static GEN
     775       17881 : euler_sumdiv(GEN q, long v)
     776             : {
     777       17881 :   GEN u = addui(1, q);
     778       17881 :   for (; v > 1; v--) u = addui(1, mulii(q, u));
     779       17881 :   return u;
     780             : }
     781             : 
     782             : static GEN
     783        1474 : sumdiv_aux(GEN F)
     784             : {
     785        1474 :   GEN x, P = gel(F,1), E = gel(F,2);
     786        1474 :   long i, l = lg(P);
     787        1474 :   x = cgetg(l, t_VEC);
     788        1474 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(gel(P,i), itou(gel(E,i)));
     789        1474 :   return ZV_prod(x);
     790             : }
     791             : GEN
     792        3017 : sumdiv(GEN n)
     793             : {
     794        3017 :   pari_sp av = avma;
     795             :   GEN F, v;
     796             : 
     797        3017 :   if ((F = check_arith_non0(n,"sumdiv")))
     798             :   {
     799        1442 :     F = clean_Z_factor(F);
     800        1442 :     v = sumdiv_aux(F);
     801             :   }
     802        1561 :   else if (lgefint(n) == 3)
     803             :   {
     804        1529 :     if (n[2] == 1) return gen_1;
     805        1501 :     F = factoru(n[2]);
     806        1501 :     v = usumdiv_fact(F);
     807             :   }
     808             :   else
     809          32 :     v = sumdiv_aux(absZ_factor(n));
     810        2975 :   return gerepileuptoint(av, v);
     811             : }
     812             : 
     813             : static GEN
     814        1506 : sumdivk_aux(GEN F, long k)
     815             : {
     816        1506 :   GEN x, P = gel(F,1), E = gel(F,2);
     817        1506 :   long i, l = lg(P);
     818        1506 :   x = cgetg(l, t_VEC);
     819        1506 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(powiu(gel(P,i),k), gel(E,i)[2]);
     820        1506 :   return ZV_prod(x);
     821             : }
     822             : GEN
     823        7987 : sumdivk(GEN n, long k)
     824             : {
     825        7987 :   pari_sp av = avma;
     826             :   GEN F, v;
     827             :   long k1;
     828             : 
     829        7987 :   if (!k) return numdiv(n);
     830        7987 :   if (k == 1) return sumdiv(n);
     831        6398 :   if ((F = check_arith_non0(n,"sumdivk"))) F = clean_Z_factor(F);
     832        6356 :   k1 = k; if (k < 0)  k = -k;
     833        6356 :   if (k == 1)
     834        1428 :     v = sumdiv(F? F: n);
     835             :   else
     836             :   {
     837        4928 :     if (F)
     838        1442 :       v = sumdivk_aux(F,k);
     839        3486 :     else if (lgefint(n) == 3)
     840             :     {
     841        3422 :       if (n[2] == 1) return gen_1;
     842        3324 :       F = factoru(n[2]);
     843        3324 :       v = usumdivk_fact(F,k);
     844             :     }
     845             :     else
     846          64 :       v = sumdivk_aux(absZ_factor(n), k);
     847        4830 :     if (k1 > 0) return gerepileuptoint(av, v);
     848             :   }
     849             : 
     850        1435 :   if (F) n = arith_n(n);
     851        1435 :   if (k != 1) n = powiu(n,k);
     852        1435 :   return gerepileupto(av, gdiv(v, n));
     853             : }
     854             : 
     855             : GEN
     856       11217 : usumdiv_fact(GEN f)
     857             : {
     858       11217 :   GEN P = gel(f,1), E = gel(f,2);
     859       11217 :   long i, l = lg(P);
     860       11217 :   GEN v = cgetg(l, t_VEC);
     861       11217 :   for (i=1; i<l; i++) gel(v,i) = u_euler_sumdiv(P[i],E[i]);
     862       11217 :   return ZV_prod(v);
     863             : }
     864             : GEN
     865        9071 : usumdivk_fact(GEN f, ulong k)
     866             : {
     867        9071 :   GEN P = gel(f,1), E = gel(f,2);
     868        9071 :   long i, l = lg(P);
     869        9071 :   GEN v = cgetg(l, t_VEC);
     870        9071 :   for (i=1; i<l; i++) gel(v,i) = euler_sumdiv(powuu(P[i],k),E[i]);
     871        9071 :   return ZV_prod(v);
     872             : }
     873             : 
     874             : long
     875        1099 : uissquarefree_fact(GEN f)
     876             : {
     877        1099 :   GEN E = gel(f,2);
     878        1099 :   long i, l = lg(E);
     879        2275 :   for (i = 1; i < l; i++)
     880        1694 :     if (E[i] > 1) return 0;
     881         581 :   return 1;
     882             : }
     883             : long
     884       18275 : uissquarefree(ulong n)
     885             : {
     886       18275 :   if (!n) return 0;
     887       18275 :   return moebiusu(n)? 1: 0;
     888             : }
     889             : long
     890        2179 : Z_issquarefree(GEN n)
     891             : {
     892        2179 :   switch(lgefint(n))
     893             :   {
     894          14 :     case 2: return 0;
     895        1406 :     case 3: return uissquarefree(n[2]);
     896             :   }
     897         759 :   return moebius(n)? 1: 0;
     898             : }
     899             : 
     900             : static int
     901        1407 : fa_issquarefree(GEN F)
     902             : {
     903        1407 :   GEN P = gel(F,1), E = gel(F,2);
     904        1407 :   long i, s, l = lg(P);
     905        1407 :   if (l == 1) return 1;
     906        1400 :   s = signe(gel(P,1)); /* = signe(x) */
     907        1400 :   if (!s) return 0;
     908        3577 :   for(i = 1; i < l; i++)
     909        2730 :     if (!equali1(gel(E,i))) return 0;
     910         847 :   return 1;
     911             : }
     912             : long
     913        3913 : issquarefree(GEN x)
     914             : {
     915             :   pari_sp av;
     916             :   GEN d;
     917        3913 :   switch(typ(x))
     918             :   {
     919        1421 :     case t_INT: return Z_issquarefree(x);
     920             :     case t_POL:
     921        1085 :       if (!signe(x)) return 0;
     922        1085 :       av = avma; d = RgX_gcd(x, RgX_deriv(x));
     923        1085 :       avma = av; return (lg(d) == 3);
     924             :     case t_VEC:
     925        1407 :     case t_MAT: return fa_issquarefree(check_arith_all(x,"issquarefree"));
     926           0 :     default: pari_err_TYPE("issquarefree",x);
     927             :       return 0; /* LCOV_EXCL_LINE */
     928             :   }
     929             : }
     930             : 
     931             : /*********************************************************************/
     932             : /**                                                                 **/
     933             : /**                    DIGITS / SUM OF DIGITS                       **/
     934             : /**                                                                 **/
     935             : /*********************************************************************/
     936             : 
     937             : /* set v[i] = 1 iff B^i is needed in the digits_dac algorithm */
     938             : static void
     939     2491956 : set_vexp(GEN v, long l)
     940             : {
     941             :   long m;
     942     4983912 :   if (v[l]) return;
     943      824051 :   v[l] = 1; m = l>>1;
     944      824051 :   set_vexp(v, m);
     945      824051 :   set_vexp(v, l-m);
     946             : }
     947             : 
     948             : /* return all needed B^i for DAC algorithm, for lz digits */
     949             : static GEN
     950      843854 : get_vB(GEN T, long lz, void *E, struct bb_ring *r)
     951             : {
     952      843854 :   GEN vB, vexp = const_vecsmall(lz, 0);
     953      843854 :   long i, l = (lz+1) >> 1;
     954      843854 :   vexp[1] = 1;
     955      843854 :   vexp[2] = 1;
     956      843854 :   set_vexp(vexp, lz);
     957      843854 :   vB = zerovec(lz); /* unneeded entries remain = 0 */
     958      843854 :   gel(vB, 1) = T;
     959     1729467 :   for (i = 2; i <= l; i++)
     960      885613 :     if (vexp[i])
     961             :     {
     962      824051 :       long j = i >> 1;
     963      824051 :       GEN B2j = r->sqr(E, gel(vB,j));
     964      824051 :       gel(vB,i) = odd(i)? r->mul(E, B2j, T): B2j;
     965             :     }
     966      843854 :   return vB;
     967             : }
     968             : 
     969             : static void
     970      186422 : gen_digits_dac(GEN x, GEN vB, long l, GEN *z,
     971             :                void *E, GEN div(void *E, GEN a, GEN b, GEN *r))
     972             : {
     973             :   GEN q, r;
     974      186422 :   long m = l>>1;
     975      372844 :   if (l==1) { *z=x; return; }
     976       89105 :   q = div(E, x, gel(vB,m), &r);
     977       89105 :   gen_digits_dac(r, vB, m, z, E, div);
     978       89105 :   gen_digits_dac(q, vB, l-m, z+m, E, div);
     979             : }
     980             : 
     981             : static GEN
     982       28889 : gen_fromdigits_dac(GEN x, GEN vB, long i, long l, void *E,
     983             :                    GEN add(void *E, GEN a, GEN b),
     984             :                    GEN mul(void *E, GEN a, GEN b))
     985             : {
     986             :   GEN a, b;
     987       28889 :   long m = l>>1;
     988       28889 :   if (l==1) return gel(x,i);
     989       13181 :   a = gen_fromdigits_dac(x, vB, i, m, E, add, mul);
     990       13181 :   b = gen_fromdigits_dac(x, vB, i+m, l-m, E, add, mul);
     991       13181 :   return add(E, a, mul(E, b, gel(vB, m)));
     992             : }
     993             : 
     994             : static GEN
     995        8499 : gen_digits_i(GEN x, GEN B, long n, void *E, struct bb_ring *r,
     996             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
     997             : {
     998             :   GEN z, vB;
     999        8499 :   if (n==1) retmkvec(gcopy(x));
    1000        8212 :   vB = get_vB(B, n, E, r);
    1001        8212 :   z = cgetg(n+1, t_VEC);
    1002        8212 :   gen_digits_dac(x, vB, n, (GEN*)(z+1), E, div);
    1003        8212 :   return z;
    1004             : }
    1005             : 
    1006             : GEN
    1007        8449 : gen_digits(GEN x, GEN B, long n, void *E, struct bb_ring *r,
    1008             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
    1009             : {
    1010        8449 :   pari_sp av = avma;
    1011        8449 :   return gerepilecopy(av, gen_digits_i(x, B, n, E, r, div));
    1012             : }
    1013             : 
    1014             : GEN
    1015        2527 : gen_fromdigits(GEN x, GEN B, void *E, struct bb_ring *r)
    1016             : {
    1017        2527 :   pari_sp av = avma;
    1018        2527 :   long n = lg(x)-1;
    1019        2527 :   GEN vB = get_vB(B, n, E, r);
    1020        2527 :   GEN z = gen_fromdigits_dac(x, vB, 1, n, E, r->add, r->mul);
    1021        2527 :   return gerepilecopy(av, z);
    1022             : }
    1023             : 
    1024             : static GEN
    1025        1771 : _addii(void *data /* ignored */, GEN x, GEN y)
    1026        1771 : { (void)data; return addii(x,y); }
    1027             : static GEN
    1028      794238 : _sqri(void *data /* ignored */, GEN x) { (void)data; return sqri(x); }
    1029             : static GEN
    1030      200068 : _mulii(void *data /* ignored */, GEN x, GEN y)
    1031      200068 :  { (void)data; return mulii(x,y); }
    1032             : static GEN
    1033         436 : _dvmdii(void *data /* ignored */, GEN x, GEN y, GEN *r)
    1034         436 :  { (void)data; return dvmdii(x,y,r); }
    1035             : 
    1036             : static struct bb_ring Z_ring = { _addii, _mulii, _sqri };
    1037             : 
    1038             : static GEN
    1039         182 : check_basis(GEN B)
    1040             : {
    1041         182 :   if (!B) return utoipos(10);
    1042         161 :   if (typ(B)!=t_INT) pari_err_TYPE("digits",B);
    1043         161 :   if (abscmpiu(B,2)<0) pari_err_DOMAIN("digits","B","<",gen_2,B);
    1044         161 :   return B;
    1045             : }
    1046             : 
    1047             : /* x has l digits in base B, write them to z[0..l-1], vB[i] = B^i */
    1048             : static void
    1049        3075 : digits_dacsmall(GEN x, GEN vB, long l, ulong* z)
    1050             : {
    1051        3075 :   pari_sp av = avma;
    1052             :   GEN q,r;
    1053             :   long m;
    1054        6150 :   if (l==1) { *z=itou(x); return; }
    1055        1524 :   m=l>>1;
    1056        1524 :   q = dvmdii(x, gel(vB,m), &r);
    1057        1524 :   digits_dacsmall(q,vB,l-m,z);
    1058        1524 :   digits_dacsmall(r,vB,m,z+l-m);
    1059        1524 :   avma = av;
    1060             : }
    1061             : 
    1062             : GEN
    1063          56 : digits(GEN x, GEN B)
    1064             : {
    1065          56 :   pari_sp av=avma;
    1066             :   long lz;
    1067             :   GEN z, vB;
    1068          56 :   if (typ(x)!=t_INT) pari_err_TYPE("digits",x);
    1069          56 :   B = check_basis(B);
    1070          56 :   if (signe(B)<0) pari_err_DOMAIN("digits","B","<",gen_0,B);
    1071          56 :   if (!signe(x))       {avma = av; return cgetg(1,t_VEC); }
    1072          49 :   if (abscmpii(x,B)<0) {avma = av; retmkvec(absi(x)); }
    1073          49 :   if (Z_ispow2(B))
    1074             :   {
    1075          14 :     long k = expi(B);
    1076          14 :     if (k == 1) return binaire(x);
    1077           7 :     if (k < BITS_IN_LONG)
    1078             :     {
    1079           0 :       (void)new_chunk(4*(expi(x) + 2)); /* HACK */
    1080           0 :       z = binary_2k_nv(x, k);
    1081           0 :       avma = av; return Flv_to_ZV(z);
    1082             :     }
    1083             :     else
    1084             :     {
    1085           7 :       avma = av; return binary_2k(x, k);
    1086             :     }
    1087             :   }
    1088          35 :   if (signe(x) < 0) x = absi(x);
    1089          35 :   lz = logint(x,B) + 1;
    1090          35 :   if (lgefint(B)>3)
    1091             :   {
    1092           8 :     z = gerepileupto(av, gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii));
    1093           8 :     vecreverse_inplace(z);
    1094           8 :     return z;
    1095             :   }
    1096             :   else
    1097             :   {
    1098          27 :     vB = get_vB(B, lz, NULL, &Z_ring);
    1099          27 :     (void)new_chunk(3*lz); /* HACK */
    1100          27 :     z = zero_zv(lz);
    1101          27 :     digits_dacsmall(x,vB,lz,(ulong*)(z+1));
    1102          27 :     avma = av; return Flv_to_ZV(z);
    1103             :   }
    1104             : }
    1105             : 
    1106             : static GEN
    1107     2622740 : fromdigitsu_dac(GEN x, GEN vB, long i, long l)
    1108             : {
    1109             :   GEN a, b;
    1110     2622740 :   long m = l>>1;
    1111     2622740 :   if (l==1) return utoi(uel(x,i));
    1112     2163848 :   if (l==2) return addumului(uel(x,i), uel(x,i+1), gel(vB, m));
    1113      894826 :   a = fromdigitsu_dac(x, vB, i, m);
    1114      894826 :   b = fromdigitsu_dac(x, vB, i+m, l-m);
    1115      894826 :   return addii(a, mulii(b, gel(vB, m)));
    1116             : }
    1117             : 
    1118             : GEN
    1119      833088 : fromdigitsu(GEN x, GEN B)
    1120             : {
    1121      833088 :   pari_sp av = avma;
    1122      833088 :   long n = lg(x)-1;
    1123             :   GEN vB, z;
    1124      833088 :   if (n==0) return gen_0;
    1125      833088 :   vB = get_vB(B, n, NULL, &Z_ring);
    1126      833088 :   z = fromdigitsu_dac(x, vB, 1, n);
    1127      833088 :   return gerepileuptoint(av, z);
    1128             : }
    1129             : 
    1130             : static int
    1131          14 : ZV_in_range(GEN v, GEN B)
    1132             : {
    1133          14 :   long i, l = lg(v);
    1134        1659 :   for(i=1; i < l; i++)
    1135             :   {
    1136        1652 :     GEN vi = gel(v, i);
    1137        1652 :     if (signe(vi) < 0 || cmpii(vi, B) >= 0)
    1138           7 :       return 0;
    1139             :   }
    1140           7 :   return 1;
    1141             : }
    1142             : 
    1143             : GEN
    1144          56 : fromdigits(GEN x, GEN B)
    1145             : {
    1146          56 :   pari_sp av = avma;
    1147          56 :   if (typ(x)!=t_VEC || !RgV_is_ZV(x)) pari_err_TYPE("fromdigits",x);
    1148          56 :   if (lg(x)==1) return gen_0;
    1149          49 :   B = check_basis(B);
    1150          49 :   if (Z_ispow2(B) && ZV_in_range(x, B))
    1151           7 :     return fromdigits_2k(x, expi(B));
    1152          42 :   x = vecreverse(x);
    1153          42 :   return gerepileuptoint(av, gen_fromdigits(x, B, NULL, &Z_ring));
    1154             : }
    1155             : 
    1156             : static const ulong digsum[] ={
    1157             :   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,
    1158             :   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,
    1159             :   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,
    1160             :   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,
    1161             :   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,
    1162             :   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,
    1163             :   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,
    1164             :   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,
    1165             :   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,
    1166             :   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,
    1167             :   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,
    1168             :   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,
    1169             :   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,
    1170             :   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,
    1171             :   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,
    1172             :   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,
    1173             :   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,
    1174             :   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,
    1175             :   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,
    1176             :   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,
    1177             :   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,
    1178             :   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,
    1179             :   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,
    1180             :   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,
    1181             :   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,
    1182             :   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,
    1183             :   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,
    1184             :   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,
    1185             :   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,
    1186             :   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,
    1187             :   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,
    1188             :   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,
    1189             :   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,
    1190             :   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,
    1191             :   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,
    1192             :   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,
    1193             :   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,
    1194             :   25,26,27
    1195             : };
    1196             : 
    1197             : ulong
    1198      355152 : sumdigitsu(ulong n)
    1199             : {
    1200      355152 :   ulong s = 0;
    1201      355152 :   while (n) { s += digsum[n % 1000]; n /= 1000; }
    1202      355152 :   return s;
    1203             : }
    1204             : 
    1205             : /* res=array of 9-digits integers, return \sum_{0 <= i < l} sumdigits(res[i]) */
    1206             : static ulong
    1207          14 : sumdigits_block(ulong *res, long l)
    1208             : {
    1209          14 :   long s = sumdigitsu(*--res);
    1210          14 :   while (--l > 0) s += sumdigitsu(*--res);
    1211          14 :   return s;
    1212             : }
    1213             : 
    1214             : GEN
    1215          35 : sumdigits(GEN n)
    1216             : {
    1217          35 :   pari_sp av = avma;
    1218             :   ulong s, *res;
    1219             :   long l;
    1220             : 
    1221          35 :   if (typ(n) != t_INT) pari_err_TYPE("sumdigits", n);
    1222          35 :   l = lgefint(n);
    1223          35 :   switch(l)
    1224             :   {
    1225           7 :     case 2: return gen_0;
    1226          14 :     case 3: return utoipos(sumdigitsu(n[2]));
    1227             :   }
    1228          14 :   res = convi(n, &l);
    1229          14 :   if ((ulong)l < ULONG_MAX / 81)
    1230             :   {
    1231          14 :     s = sumdigits_block(res, l);
    1232          14 :     avma = av; return utoipos(s);
    1233             :   }
    1234             :   else /* Huge. Overflows ulong */
    1235             :   {
    1236           0 :     const long L = (long)(ULONG_MAX / 81);
    1237           0 :     GEN S = gen_0;
    1238           0 :     while (l > L)
    1239             :     {
    1240           0 :       S = addiu(S, sumdigits_block(res, L));
    1241           0 :       res += L; l -= L;
    1242             :     }
    1243           0 :     if (l)
    1244           0 :       S = addiu(S, sumdigits_block(res, l));
    1245           0 :     return gerepileuptoint(av, S);
    1246             :   }
    1247             : }
    1248             : 
    1249             : GEN
    1250         105 : sumdigits0(GEN x, GEN B)
    1251             : {
    1252         105 :   pari_sp av = avma;
    1253             :   GEN z;
    1254             :   long lz;
    1255             : 
    1256         105 :   if (!B) return sumdigits(x);
    1257          77 :   if (typ(x) != t_INT) pari_err_TYPE("sumdigits", x);
    1258          77 :   B = check_basis(B);
    1259          77 :   if (Z_ispow2(B))
    1260             :   {
    1261          28 :     long k = expi(B);
    1262          28 :     if (k == 1) { avma = av; return utoi(hammingweight(x)); }
    1263          21 :     if (k < BITS_IN_LONG)
    1264             :     {
    1265          14 :       GEN z = binary_2k_nv(x, k);
    1266          14 :       if (lg(z)-1 > 1L<<(BITS_IN_LONG-k)) /* may overflow */
    1267           0 :         return gerepileuptoint(av, ZV_sum(Flv_to_ZV(z)));
    1268          14 :       avma = av; return utoi(zv_sum(z));
    1269             :     }
    1270           7 :     return gerepileuptoint(av, ZV_sum(binary_2k(x, k)));
    1271             :   }
    1272          49 :   if (!signe(x))       { avma = av; return gen_0; }
    1273          49 :   if (abscmpii(x,B)<0) { avma = av; return absi(x); }
    1274          49 :   if (absequaliu(B,10))   { avma = av; return sumdigits(x); }
    1275          42 :   lz = logint(x,B) + 1;
    1276          42 :   z = gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii);
    1277          42 :   return gerepileuptoint(av, ZV_sum(z));
    1278             : }

Generated by: LCOV version 1.11