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 to exceed 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 - trans1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.1 lcov report (development 28695-49bb1ac00f) Lines: 2234 2305 96.9 %
Date: 2023-09-24 07:47:42 Functions: 166 167 99.4 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /********************************************************************/
      16             : /**                                                                **/
      17             : /**                   TRANSCENDENTAL FUNCTIONS                     **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : #define DEBUGLEVEL DEBUGLEVEL_trans
      24             : 
      25             : #ifdef LONG_IS_64BIT
      26             : static const long SQRTVERYBIGINT = 3037000500L; /* ceil(sqrt(LONG_MAX)) */
      27             : #else
      28             : static const long SQRTVERYBIGINT = 46341L;
      29             : #endif
      30             : 
      31             : static THREAD GEN gcatalan, geuler, glog2, gpi;
      32             : void
      33      320699 : pari_init_floats(void)
      34             : {
      35      320699 :   gcatalan = geuler = gpi = zetazone = bernzone = glog2 = eulerzone = NULL;
      36      320699 : }
      37             : 
      38             : void
      39      318779 : pari_close_floats(void)
      40             : {
      41      318779 :   guncloneNULL(gcatalan);
      42      317815 :   guncloneNULL(geuler);
      43      316618 :   guncloneNULL(gpi);
      44      316451 :   guncloneNULL(glog2);
      45      316358 :   guncloneNULL(zetazone);
      46      316021 :   guncloneNULL_deep(bernzone);
      47      315628 :   guncloneNULL_deep(eulerzone);
      48      315246 : }
      49             : 
      50             : /********************************************************************/
      51             : /**                   GENERIC BINARY SPLITTING                     **/
      52             : /**                    (Haible, Papanikolaou)                      **/
      53             : /********************************************************************/
      54             : void
      55      274134 : abpq_init(struct abpq *A, long n)
      56             : {
      57      274134 :   A->a = (GEN*)new_chunk(n+1);
      58      274257 :   A->b = (GEN*)new_chunk(n+1);
      59      274306 :   A->p = (GEN*)new_chunk(n+1);
      60      274464 :   A->q = (GEN*)new_chunk(n+1);
      61      274470 : }
      62             : static GEN
      63    20146323 : mulii3(GEN a, GEN b, GEN c) { return mulii(mulii(a,b),c); }
      64             : 
      65             : /* T_{n1,n1+1} */
      66             : static GEN
      67     4300862 : T2(struct abpq *A, long n1)
      68             : {
      69     4300862 :   GEN u = mulii3(A->a[n1], A->b[n1+1], A->q[n1+1]);
      70     4300938 :   GEN v = mulii3(A->b[n1], A->a[n1+1], A->p[n1+1]);
      71     4300963 :   return mulii(A->p[n1], addii(u, v));
      72             : }
      73             : 
      74             : /* assume n2 > n1. Compute sum_{n1 <= n < n2} a/b(n) p/q(n1)... p/q(n) */
      75             : void
      76     8374974 : abpq_sum(struct abpq_res *r, long n1, long n2, struct abpq *A)
      77             : {
      78             :   struct abpq_res L, R;
      79             :   GEN u1, u2;
      80             :   pari_sp av;
      81             :   long n;
      82     8374974 :   switch(n2 - n1)
      83             :   {
      84             :     GEN b, q;
      85          57 :     case 1:
      86          57 :       r->P = A->p[n1];
      87          57 :       r->Q = A->q[n1];
      88          57 :       r->B = A->b[n1];
      89          57 :       r->T = mulii(A->a[n1], A->p[n1]);
      90     4315230 :       return;
      91     2425721 :     case 2:
      92     2425721 :       r->P = mulii(A->p[n1], A->p[n1+1]);
      93     2416304 :       r->Q = mulii(A->q[n1], A->q[n1+1]);
      94     2415582 :       r->B = mulii(A->b[n1], A->b[n1+1]);
      95     2415538 :       av = avma;
      96     2415538 :       r->T = gerepileuptoint(av, T2(A, n1));
      97     2421038 :       return;
      98             : 
      99     1903082 :     case 3:
     100     1903082 :       q = mulii(A->q[n1+1], A->q[n1+2]);
     101     1899423 :       b = mulii(A->b[n1+1], A->b[n1+2]);
     102     1899117 :       r->P = mulii3(A->p[n1], A->p[n1+1], A->p[n1+2]);
     103     1899083 :       r->Q = mulii(A->q[n1], q);
     104     1899071 :       r->B = mulii(A->b[n1], b);
     105     1899107 :       av = avma;
     106     1899107 :       u1 = mulii3(b, q, A->a[n1]);
     107     1898945 :       u2 = mulii(A->b[n1], T2(A, n1+1));
     108     1899081 :       r->T = gerepileuptoint(av, mulii(A->p[n1], addii(u1, u2)));
     109     1894135 :       return;
     110             :   }
     111             : 
     112     4046114 :   av = avma;
     113     4046114 :   n = (n1 + n2) >> 1;
     114     4046114 :   abpq_sum(&L, n1, n, A);
     115     4049970 :   abpq_sum(&R, n, n2, A);
     116             : 
     117     4050973 :   r->P = mulii(L.P, R.P);
     118     4035113 :   r->Q = mulii(L.Q, R.Q);
     119     4036366 :   r->B = mulii(L.B, R.B);
     120     4034631 :   u1 = mulii3(R.B, R.Q, L.T);
     121     4035217 :   u2 = mulii3(L.B, L.P, R.T);
     122     4033494 :   r->T = addii(u1,u2);
     123     4036075 :   set_avma(av);
     124     4037441 :   r->P = icopy(r->P);
     125     4049084 :   r->Q = icopy(r->Q);
     126     4052248 :   r->B = icopy(r->B);
     127     4052625 :   r->T = icopy(r->T);
     128             : }
     129             : 
     130             : /********************************************************************/
     131             : /**                                                                **/
     132             : /**                               PI                               **/
     133             : /**                                                                **/
     134             : /********************************************************************/
     135             : /* replace *old clone by c. Protect against SIGINT */
     136             : static void
     137       78062 : swap_clone(GEN *old, GEN c)
     138       78062 : { GEN tmp = *old; *old = c; guncloneNULL(tmp); }
     139             : 
     140             : /*                         ----
     141             :  *  53360 (640320)^(1/2)   \    (6n)! (545140134 n + 13591409)
     142             :  *  -------------------- = /    ------------------------------
     143             :  *        Pi               ----   (n!)^3 (3n)! (-640320)^(3n)
     144             :  *                         n>=0
     145             :  *
     146             :  * Ramanujan's formula + binary splitting */
     147             : static GEN
     148       38372 : pi_ramanujan(long prec)
     149             : {
     150       38372 :   const ulong B = 545140134, A = 13591409, C = 640320;
     151       38372 :   const double alpha2 = 47.11041314; /* 3log(C/12) / log(2) */
     152             :   long n, nmax, prec2;
     153             :   struct abpq_res R;
     154             :   struct abpq S;
     155             :   GEN D, u;
     156             : 
     157       38372 :   nmax = (long)(1 + prec2nbits(prec)/alpha2);
     158             : #ifdef LONG_IS_64BIT
     159       37875 :   D = utoipos(10939058860032000UL); /* C^3/24 */
     160             : #else
     161         500 :   D = uutoi(2546948UL,495419392UL);
     162             : #endif
     163       38373 :   abpq_init(&S, nmax);
     164       38372 :   S.a[0] = utoipos(A);
     165       38368 :   S.b[0] = S.p[0] = S.q[0] = gen_1;
     166      312789 :   for (n = 1; n <= nmax; n++)
     167             :   {
     168      274457 :     S.a[n] = addiu(muluu(B, n), A);
     169      274401 :     S.b[n] = gen_1;
     170      274401 :     S.p[n] = mulis(muluu(6*n-5, 2*n-1), 1-6*n);
     171      274381 :     S.q[n] = mulii(sqru(n), muliu(D,n));
     172             :   }
     173       38332 :   abpq_sum(&R, 0, nmax, &S); prec2 = prec+EXTRAPREC64;
     174       38374 :   u = itor(muliu(R.Q,C/12), prec2);
     175       38377 :   return rtor(mulrr(divri(u, R.T), sqrtr_abs(utor(C,prec2))), prec);
     176             : }
     177             : 
     178             : #if 0 /* Much slower than binary splitting at least up to prec = 10^8 */
     179             : /* Gauss - Brent-Salamin AGM iteration */
     180             : static GEN
     181             : pi_brent_salamin(long prec)
     182             : {
     183             :   GEN A, B, C;
     184             :   pari_sp av2;
     185             :   long i, G;
     186             : 
     187             :   G = - prec2nbits(prec);
     188             :   incrprec(prec);
     189             : 
     190             :   A = real2n(-1, prec);
     191             :   B = sqrtr_abs(A); /* = 1/sqrt(2) */
     192             :   setexpo(A, 0);
     193             :   C = real2n(-2, prec); av2 = avma;
     194             :   for (i = 0;; i++)
     195             :   {
     196             :     GEN y, a, b, B_A = subrr(B, A);
     197             :     pari_sp av3 = avma;
     198             :     if (expo(B_A) < G) break;
     199             :     a = addrr(A,B); shiftr_inplace(a, -1);
     200             :     b = mulrr(A,B);
     201             :     affrr(a, A);
     202             :     affrr(sqrtr_abs(b), B); set_avma(av3);
     203             :     y = sqrr(B_A); shiftr_inplace(y, i - 2);
     204             :     affrr(subrr(C, y), C); set_avma(av2);
     205             :   }
     206             :   shiftr_inplace(C, 2);
     207             :   return divrr(sqrr(addrr(A,B)), C);
     208             : }
     209             : #endif
     210             : 
     211             : GEN
     212    34717491 : constpi(long prec)
     213             : {
     214             :   pari_sp av;
     215             :   GEN tmp;
     216    34717491 :   if (gpi && realprec(gpi) >= prec) return gpi;
     217             : 
     218       38196 :   av = avma;
     219       38196 :   tmp = gclone(pi_ramanujan(prec));
     220       38387 :   swap_clone(&gpi,tmp);
     221       38392 :   return gc_const(av, gpi);
     222             : }
     223             : 
     224             : GEN
     225    34717411 : mppi(long prec) { return rtor(constpi(prec), prec); }
     226             : 
     227             : /* Pi * 2^n */
     228             : GEN
     229    21674583 : Pi2n(long n, long prec)
     230             : {
     231    21674583 :   GEN x = mppi(prec); shiftr_inplace(x, n);
     232    21674623 :   return x;
     233             : }
     234             : 
     235             : /* I * Pi * 2^n */
     236             : GEN
     237      262235 : PiI2n(long n, long prec) { retmkcomplex(gen_0, Pi2n(n, prec)); }
     238             : 
     239             : /* 2I * Pi */
     240             : GEN
     241      261346 : PiI2(long prec) { return PiI2n(1, prec); }
     242             : 
     243             : /********************************************************************/
     244             : /**                                                                **/
     245             : /**                       EULER CONSTANT                           **/
     246             : /**                                                                **/
     247             : /********************************************************************/
     248             : 
     249             : GEN
     250       58034 : consteuler(long prec)
     251             : {
     252             :   GEN u,v,a,b,tmpeuler;
     253             :   long l, n1, n, k, x;
     254             :   pari_sp av1, av2;
     255             : 
     256       58034 :   if (geuler && realprec(geuler) >= prec) return geuler;
     257             : 
     258         519 :   av1 = avma; tmpeuler = cgetr_block(prec);
     259             : 
     260         519 :   incrprec(prec);
     261             : 
     262         519 :   l = prec+EXTRAPREC64; x = (long) (1 + prec2nbits_mul(l, M_LN2/4));
     263         519 :   a = utor(x,l); u=logr_abs(a); setsigne(u,-1); affrr(u,a);
     264         519 :   b = real_1(l);
     265         519 :   v = real_1(l);
     266         519 :   n = (long)(1+3.591*x); /* z=3.591: z*[ ln(z)-1 ]=1 */
     267         519 :   n1 = minss(n, SQRTVERYBIGINT);
     268         519 :   if (x < SQRTVERYBIGINT)
     269             :   {
     270         519 :     ulong xx = x*x;
     271         519 :     av2 = avma;
     272      170037 :     for (k=1; k<n1; k++)
     273             :     {
     274      169518 :       affrr(divru(mulur(xx,b),k*k), b);
     275      169537 :       affrr(divru(addrr(divru(mulur(xx,a),k),b),k), a);
     276      169537 :       affrr(addrr(u,a), u);
     277      169505 :       affrr(addrr(v,b), v); set_avma(av2);
     278             :     }
     279        1038 :     for (   ; k<=n; k++)
     280             :     {
     281         519 :       affrr(divru(divru(mulur(xx,b),k),k), b);
     282         519 :       affrr(divru(addrr(divru(mulur(xx,a),k),b),k), a);
     283         519 :       affrr(addrr(u,a), u);
     284         519 :       affrr(addrr(v,b), v); set_avma(av2);
     285             :     }
     286             :   }
     287             :   else
     288             :   {
     289           0 :     GEN xx = sqru(x);
     290           0 :     av2 = avma;
     291           0 :     for (k=1; k<n1; k++)
     292             :     {
     293           0 :       affrr(divru(mulir(xx,b),k*k), b);
     294           0 :       affrr(divru(addrr(divru(mulir(xx,a),k),b),k), a);
     295           0 :       affrr(addrr(u,a), u);
     296           0 :       affrr(addrr(v,b), v); set_avma(av2);
     297             :     }
     298           0 :     for (   ; k<=n; k++)
     299             :     {
     300           0 :       affrr(divru(divru(mulir(xx,b),k),k), b);
     301           0 :       affrr(divru(addrr(divru(mulir(xx,a),k),b),k), a);
     302           0 :       affrr(addrr(u,a), u);
     303           0 :       affrr(addrr(v,b), v); set_avma(av2);
     304             :     }
     305             :   }
     306         519 :   divrrz(u,v,tmpeuler);
     307         519 :   swap_clone(&geuler,tmpeuler);
     308         519 :   return gc_const(av1, geuler);
     309             : }
     310             : 
     311             : GEN
     312       58034 : mpeuler(long prec) { return rtor(consteuler(prec), prec); }
     313             : 
     314             : /********************************************************************/
     315             : /**                                                                **/
     316             : /**                       CATALAN CONSTANT                         **/
     317             : /**                                                                **/
     318             : /********************************************************************/
     319             : /*        inf  256^i (580i^2 - 184i + 15) (2i)!^3 (3i)!^2
     320             :  * 64 G = SUM  ------------------------------------------
     321             :  *        i=1             i^3 (2i-1) (6i)!^2           */
     322             : static GEN
     323          14 : catalan(long prec)
     324             : {
     325          14 :   long i, nmax = 1 + prec2nbits(prec) / 7.509; /* / log2(729/4) */
     326             :   struct abpq_res R;
     327             :   struct abpq A;
     328             :   GEN u;
     329          14 :   abpq_init(&A, nmax);
     330          14 :   A.a[0] = gen_0; A.b[0] = A.p[0] = A.q[0] = gen_1;
     331        1750 :   for (i = 1; i <= nmax; i++)
     332             :   {
     333        1736 :     A.a[i] = addiu(muluu(580*i - 184, i), 15);
     334        1736 :     A.b[i] = muliu(powuu(i, 3), 2*i - 1);
     335        1736 :     A.p[i] = mului(64*i-32, powuu(i,3));
     336        1736 :     A.q[i] = sqri(muluu(6*i - 1, 18*i - 15));
     337             :   }
     338          14 :   abpq_sum(&R, 0, nmax, &A);
     339          14 :   u = rdivii(R.T, mulii(R.B,R.Q),prec);
     340          14 :   shiftr_inplace(u, -6); return u;
     341             : }
     342             : 
     343             : GEN
     344          14 : constcatalan(long prec)
     345             : {
     346          14 :   pari_sp av = avma;
     347             :   GEN tmp;
     348          14 :   if (gcatalan && realprec(gcatalan) >= prec) return gcatalan;
     349          14 :   tmp = gclone(catalan(prec));
     350          14 :   swap_clone(&gcatalan,tmp);
     351          14 :   return gc_const(av, gcatalan);
     352             : }
     353             : 
     354             : GEN
     355          14 : mpcatalan(long prec) { return rtor(constcatalan(prec), prec); }
     356             : 
     357             : /********************************************************************/
     358             : /**                                                                **/
     359             : /**          TYPE CONVERSION FOR TRANSCENDENTAL FUNCTIONS          **/
     360             : /**                                                                **/
     361             : /********************************************************************/
     362             : static GEN
     363     2010505 : transvec(GEN (*f)(GEN,long), GEN x, long prec)
     364     6531758 : { pari_APPLY_same(f(gel(x,i), prec)); }
     365             : static GEN
     366         329 : transvecgen(void *E, GEN (*f)(void *,GEN,long), GEN x, long prec)
     367         735 : { pari_APPLY_same(f(E, gel(x,i), prec)); }
     368             : 
     369             : GEN
     370     3730733 : trans_eval(const char *fun, GEN (*f)(GEN,long), GEN x, long prec)
     371             : {
     372     3730733 :   pari_sp av = avma;
     373     3730733 :   if (prec < 3) pari_err_BUG("trans_eval [prec < 3]");
     374     3730749 :   switch(typ(x))
     375             :   {
     376     1473007 :     case t_INT:    x = f(itor(x,prec),prec); break;
     377      247179 :     case t_FRAC:   x = f(fractor(x, prec),prec); break;
     378           7 :     case t_QUAD:   x = f(quadtofp(x,prec),prec); break;
     379          14 :     case t_POLMOD: x = transvec(f, polmod_to_embed(x,prec), prec); break;
     380     2010493 :     case t_VEC:
     381             :     case t_COL:
     382     2010493 :     case t_MAT: return transvec(f, x, prec);
     383          49 :     default: pari_err_TYPE(fun,x);
     384             :       return NULL;/*LCOV_EXCL_LINE*/
     385             :   }
     386     1720186 :   return gerepileupto(av, x);
     387             : }
     388             : 
     389             : GEN
     390        1883 : trans_evalgen(const char *fun, void *E, GEN (*f)(void*,GEN,long),
     391             :               GEN x, long prec)
     392             : {
     393        1883 :   pari_sp av = avma;
     394        1883 :   if (prec < 3) pari_err_BUG("trans_eval [prec < 3]");
     395        1883 :   switch(typ(x))
     396             :   {
     397         273 :     case t_INT:    x = f(E, itor(x,prec),prec); break;
     398        1246 :     case t_FRAC:   x = f(E, fractor(x, prec),prec); break;
     399           0 :     case t_QUAD:   x = f(E, quadtofp(x,prec),prec); break;
     400          70 :     case t_POLMOD: x = transvecgen(E, f, polmod_to_embed(x,prec), prec); break;
     401         259 :     case t_VEC:
     402             :     case t_COL:
     403         259 :     case t_MAT: return transvecgen(E, f, x, prec);
     404          35 :     default: pari_err_TYPE(fun,x);
     405             :       return NULL;/*LCOV_EXCL_LINE*/
     406             :   }
     407        1589 :   return gerepileupto(av, x);
     408             : }
     409             : 
     410             : /*******************************************************************/
     411             : /*                                                                 */
     412             : /*                            POWERING                             */
     413             : /*                                                                 */
     414             : /*******************************************************************/
     415             : /* x a t_REAL 0, return exp(x) */
     416             : static GEN
     417       72188 : mpexp0(GEN x)
     418             : {
     419       72188 :   long e = expo(x);
     420       72188 :   return e >= 0? real_0_bit(e): real_1_bit(-e);
     421             : }
     422             : static GEN
     423       21042 : powr0(GEN x)
     424       21042 : { return signe(x)? real_1(realprec(x)): mpexp0(x); }
     425             : 
     426             : /* assume typ(x) = t_VEC */
     427             : static int
     428          63 : is_ext_qfr(GEN x)
     429          42 : { return lg(x) == 3 && typ(gel(x,1)) == t_QFB && !qfb_is_qfi(gel(x,1))
     430         105 :                     && typ(gel(x,2)) == t_REAL; }
     431             : 
     432             : /* x t_POL or t_SER, return scalarpol(Rg_get_1(x)) */
     433             : static GEN
     434      367439 : scalarpol_get_1(GEN x)
     435             : {
     436      367439 :   GEN y = cgetg(3,t_POL);
     437      367439 :   y[1] = evalvarn(varn(x)) | evalsigne(1);
     438      367439 :   gel(y,2) = Rg_get_1(x); return y;
     439             : }
     440             : /* to be called by the generic function gpowgs(x,s) when s = 0 */
     441             : static GEN
     442     1603223 : gpowg0(GEN x)
     443             : {
     444             :   long lx, i;
     445             :   GEN y;
     446             : 
     447     1603223 :   switch(typ(x))
     448             :   {
     449     1191889 :     case t_INT: case t_REAL: case t_FRAC: case t_PADIC:
     450     1191889 :       return gen_1;
     451             : 
     452           7 :     case t_QUAD: x++; /*fall through*/
     453       37839 :     case t_COMPLEX: {
     454       37839 :       pari_sp av = avma;
     455       37839 :       GEN a = gpowg0(gel(x,1));
     456       37839 :       GEN b = gpowg0(gel(x,2));
     457       37839 :       if (a == gen_1) return b;
     458          14 :       if (b == gen_1) return a;
     459           7 :       return gerepileupto(av, gmul(a,b));
     460             :     }
     461         133 :     case t_INTMOD:
     462         133 :       y = cgetg(3,t_INTMOD);
     463         133 :       gel(y,1) = icopy(gel(x,1));
     464         133 :       gel(y,2) = is_pm1(gel(x,1))? gen_0: gen_1;
     465         133 :       return y;
     466             : 
     467        5754 :     case t_FFELT: return FF_1(x);
     468             : 
     469         973 :     case t_POLMOD:
     470         973 :       retmkpolmod(scalarpol_get_1(gel(x,1)), gcopy(gel(x,1)));
     471             : 
     472           7 :     case t_RFRAC:
     473           7 :       return scalarpol_get_1(gel(x,2));
     474      366459 :     case t_POL: case t_SER:
     475      366459 :       return scalarpol_get_1(x);
     476             : 
     477          84 :     case t_MAT:
     478          84 :       lx=lg(x); if (lx==1) return cgetg(1,t_MAT);
     479          77 :       if (lx != lgcols(x)) pari_err_DIM("gpow");
     480          77 :       y = matid(lx-1);
     481         252 :       for (i=1; i<lx; i++) gcoeff(y,i,i) = gpowg0(gcoeff(x,i,i));
     482          77 :       return y;
     483          21 :     case t_VEC: if (!is_ext_qfr(x)) break;
     484             :     /* fall through handle extended t_QFB */
     485          28 :     case t_QFB: return qfbpow(x, gen_0);
     486          49 :     case t_VECSMALL: return identity_perm(lg(x) - 1);
     487             :   }
     488          12 :   pari_err_TYPE("gpow",x);
     489             :   return NULL; /* LCOV_EXCL_LINE */
     490             : }
     491             : 
     492             : static GEN
     493     6055573 : _sqr(void *data /* ignored */, GEN x) { (void)data; return gsqr(x); }
     494             : static GEN
     495     3528561 : _mul(void *data /* ignored */, GEN x, GEN y) { (void)data; return gmul(x,y); }
     496             : static GEN
     497      324683 : _one(void *x) { return gpowg0((GEN) x); }
     498             : static GEN
     499    82368406 : _sqri(void *data /* ignored */, GEN x) { (void)data; return sqri(x); }
     500             : static GEN
     501    30295414 : _muli(void *data /* ignored */, GEN x, GEN y) { (void)data; return mulii(x,y); }
     502             : static GEN
     503    19743876 : _sqrr(void *data /* ignored */, GEN x) { (void)data; return sqrr(x); }
     504             : static GEN
     505     6416219 : _mulr(void *data /* ignored */, GEN x, GEN y) { (void)data; return mulrr(x,y); }
     506             : static GEN
     507       13797 : _oner(void *data /* prec */) { return real_1( *(long*) data); }
     508             : 
     509             : /* INTEGER POWERING (a^n for integer a != 0 and integer n > 0)
     510             :  *
     511             :  * Use left shift binary algorithm (RS is wasteful: multiplies big numbers,
     512             :  * with LS one of them is the base, hence small). Sign of result is set
     513             :  * to s (= 1,-1). Makes life easier for caller, which otherwise might do a
     514             :  * setsigne(gen_1 / gen_m1) */
     515             : static GEN
     516    99052707 : powiu_sign(GEN a, ulong N, long s)
     517             : {
     518             :   pari_sp av;
     519             :   GEN y;
     520             : 
     521    99052707 :   if (lgefint(a) == 3)
     522             :   { /* easy if |a| < 3 */
     523    95102666 :     ulong q = a[2];
     524    95102666 :     if (q == 1) return (s>0)? gen_1: gen_m1;
     525    85897156 :     if (q == 2) { a = int2u(N); setsigne(a,s); return a; }
     526    63419091 :     q = upowuu(q, N);
     527    63421652 :     if (q) return s>0? utoipos(q): utoineg(q);
     528             :   }
     529    35140904 :   if (N <= 2) {
     530     4344339 :     if (N == 2) return sqri(a);
     531       19394 :     a = icopy(a); setsigne(a,s); return a;
     532             :   }
     533    30796565 :   av = avma;
     534    30796565 :   y = gen_powu_i(a, N, NULL, &_sqri, &_muli);
     535    30796990 :   setsigne(y,s); return gerepileuptoint(av, y);
     536             : }
     537             : /* a^n */
     538             : GEN
     539    99211967 : powiu(GEN a, ulong n)
     540             : {
     541             :   long s;
     542    99211967 :   if (!n) return gen_1;
     543    97955878 :   s = signe(a);
     544    97955878 :   if (!s) return gen_0;
     545    97881434 :   return powiu_sign(a, n, (s < 0 && odd(n))? -1: 1);
     546             : }
     547             : GEN
     548    23644215 : powis(GEN a, long n)
     549             : {
     550             :   long s;
     551             :   GEN t, y;
     552    23644215 :   if (n >= 0) return powiu(a, n);
     553      577720 :   s = signe(a);
     554      577720 :   if (!s) pari_err_INV("powis",gen_0);
     555      577724 :   t = (s < 0 && odd(n))? gen_m1: gen_1;
     556      577724 :   if (is_pm1(a)) return t;
     557             :   /* n < 0, |a| > 1 */
     558      575323 :   y = cgetg(3,t_FRAC);
     559      575321 :   gel(y,1) = t;
     560      575321 :   gel(y,2) = powiu_sign(a, -n, 1); /* force denominator > 0 */
     561      575323 :   return y;
     562             : }
     563             : GEN
     564    47847336 : powuu(ulong p, ulong N)
     565             : {
     566             :   pari_sp av;
     567             :   ulong pN;
     568             :   GEN y;
     569    47847336 :   if (!p) return gen_0;
     570    47847259 :   if (N <= 2)
     571             :   {
     572    41774427 :     if (N == 2) return sqru(p);
     573    37942129 :     if (N == 1) return utoipos(p);
     574     5095786 :     return gen_1;
     575             :   }
     576     6072832 :   pN = upowuu(p, N);
     577     6073000 :   if (pN) return utoipos(pN);
     578     1134189 :   if (p == 2) return int2u(N);
     579     1120739 :   av = avma;
     580     1120739 :   y = gen_powu_i(utoipos(p), N, NULL, &_sqri, &_muli);
     581     1120736 :   return gerepileuptoint(av, y);
     582             : }
     583             : 
     584             : /* return 0 if overflow */
     585             : static ulong
     586    19151424 : usqru(ulong p) { return p & HIGHMASK? 0: p*p; }
     587             : ulong
     588   100145998 : upowuu(ulong p, ulong k)
     589             : {
     590             : #ifdef LONG_IS_64BIT
     591    85999857 :   const ulong CUTOFF3 = 2642245;
     592    85999857 :   const ulong CUTOFF4 = 65535;
     593    85999857 :   const ulong CUTOFF5 = 7131;
     594    85999857 :   const ulong CUTOFF6 = 1625;
     595    85999857 :   const ulong CUTOFF7 = 565;
     596    85999857 :   const ulong CUTOFF8 = 255;
     597    85999857 :   const ulong CUTOFF9 = 138;
     598    85999857 :   const ulong CUTOFF10 = 84;
     599    85999857 :   const ulong CUTOFF11 = 56;
     600    85999857 :   const ulong CUTOFF12 = 40;
     601    85999857 :   const ulong CUTOFF13 = 30;
     602    85999857 :   const ulong CUTOFF14 = 23;
     603    85999857 :   const ulong CUTOFF15 = 19;
     604    85999857 :   const ulong CUTOFF16 = 15;
     605    85999857 :   const ulong CUTOFF17 = 13;
     606    85999857 :   const ulong CUTOFF18 = 11;
     607    85999857 :   const ulong CUTOFF19 = 10;
     608    85999857 :   const ulong CUTOFF20 =  9;
     609             : #else
     610    14146141 :   const ulong CUTOFF3 = 1625;
     611    14146141 :   const ulong CUTOFF4 =  255;
     612    14146141 :   const ulong CUTOFF5 =   84;
     613    14146141 :   const ulong CUTOFF6 =   40;
     614    14146141 :   const ulong CUTOFF7 =   23;
     615    14146141 :   const ulong CUTOFF8 =   15;
     616    14146141 :   const ulong CUTOFF9 =   11;
     617    14146141 :   const ulong CUTOFF10 =   9;
     618    14146141 :   const ulong CUTOFF11 =   7;
     619    14146141 :   const ulong CUTOFF12 =   6;
     620    14146141 :   const ulong CUTOFF13 =   5;
     621    14146141 :   const ulong CUTOFF14 =   4;
     622    14146141 :   const ulong CUTOFF15 =   4;
     623    14146141 :   const ulong CUTOFF16 =   3;
     624    14146141 :   const ulong CUTOFF17 =   3;
     625    14146141 :   const ulong CUTOFF18 =   3;
     626    14146141 :   const ulong CUTOFF19 =   3;
     627    14146141 :   const ulong CUTOFF20 =   3;
     628             : #endif
     629             : 
     630   100145998 :   if (p <= 2)
     631             :   {
     632     9634378 :     if (p < 2) return p;
     633     9107319 :     return k < BITS_IN_LONG? 1UL<<k: 0;
     634             :   }
     635    90511620 :   switch(k)
     636             :   {
     637             :     ulong p2, p3, p4, p5, p8;
     638     8259259 :     case 0:  return 1;
     639    19911851 :     case 1:  return p;
     640    19151437 :     case 2:  return usqru(p);
     641     3639140 :     case 3:  if (p > CUTOFF3) return 0; return p*p*p;
     642    11365956 :     case 4:  if (p > CUTOFF4) return 0; p2=p*p; return p2*p2;
     643     2179175 :     case 5:  if (p > CUTOFF5) return 0; p2=p*p; return p2*p2*p;
     644     6984221 :     case 6:  if (p > CUTOFF6) return 0; p2=p*p; return p2*p2*p2;
     645      351743 :     case 7:  if (p > CUTOFF7) return 0; p2=p*p; return p2*p2*p2*p;
     646      452893 :     case 8:  if (p > CUTOFF8) return 0; p2=p*p; p4=p2*p2; return p4*p4;
     647      394321 :     case 9:  if (p > CUTOFF9) return 0; p2=p*p; p4=p2*p2; return p4*p4*p;
     648     4912736 :     case 10: if (p > CUTOFF10)return 0; p2=p*p; p4=p2*p2; return p4*p4*p2;
     649      169201 :     case 11: if (p > CUTOFF11)return 0; p2=p*p; p4=p2*p2; return p4*p4*p2*p;
     650     4792272 :     case 12: if (p > CUTOFF12)return 0; p2=p*p; p4=p2*p2; return p4*p4*p4;
     651      135293 :     case 13: if (p > CUTOFF13)return 0; p2=p*p; p4=p2*p2; return p4*p4*p4*p;
     652     4779477 :     case 14: if (p > CUTOFF14)return 0; p2=p*p; p4=p2*p2; return p4*p4*p4*p2;
     653      152920 :     case 15: if (p > CUTOFF15)return 0;
     654       76240 :       p2=p*p; p3=p2*p; p5=p3*p2; return p5*p5*p5;
     655      113946 :     case 16: if (p > CUTOFF16)return 0;
     656       52258 :       p2=p*p; p4=p2*p2; p8=p4*p4; return p8*p8;
     657       83208 :     case 17: if (p > CUTOFF17)return 0;
     658       42002 :       p2=p*p; p4=p2*p2; p8=p4*p4; return p*p8*p8;
     659       72560 :     case 18: if (p > CUTOFF18)return 0;
     660       39313 :       p2=p*p; p4=p2*p2; p8=p4*p4; return p2*p8*p8;
     661      791993 :     case 19: if (p > CUTOFF19)return 0;
     662      764528 :       p2=p*p; p4=p2*p2; p8=p4*p4; return p*p2*p8*p8;
     663       46191 :     case 20: if (p > CUTOFF20)return 0;
     664       21557 :       p2=p*p; p4=p2*p2; p8=p4*p4; return p4*p8*p8;
     665             :   }
     666             : #ifdef LONG_IS_64BIT
     667     1527762 :   switch(p)
     668             :   {
     669      220885 :     case 3: if (k > 40) return 0;
     670      135022 :       break;
     671       17034 :     case 4: if (k > 31) return 0;
     672         780 :       return 1UL<<(2*k);
     673      647463 :     case 5: if (k > 27) return 0;
     674       19810 :       break;
     675       49650 :     case 6: if (k > 24) return 0;
     676        9180 :       break;
     677       56003 :     case 7: if (k > 22) return 0;
     678        2769 :       break;
     679      536727 :     default: return 0;
     680             :   }
     681             :   /* no overflow */
     682             :   {
     683      166781 :     ulong q = upowuu(p, k >> 1);
     684      166781 :     q *= q ;
     685      166781 :     return odd(k)? q*p: q;
     686             :   }
     687             : #else
     688      244065 :   return 0;
     689             : #endif
     690             : }
     691             : 
     692             : GEN
     693       12017 : upowers(ulong x, long n)
     694             : {
     695             :   long i;
     696       12017 :   GEN p = cgetg(n + 2, t_VECSMALL);
     697       12017 :   uel(p,1) = 1; if (n == 0) return p;
     698       12017 :   uel(p,2) = x;
     699       91465 :   for (i = 3; i <= n; i++)
     700       79448 :     uel(p,i) = uel(p,i-1)*x;
     701       12017 :   return p;
     702             : }
     703             : 
     704             : typedef struct {
     705             :   long prec, a;
     706             :   GEN (*sqr)(GEN);
     707             :   GEN (*mulug)(ulong,GEN);
     708             : } sr_muldata;
     709             : 
     710             : static GEN
     711     1594700 : _rpowuu_sqr(void *data, GEN x)
     712             : {
     713     1594700 :   sr_muldata *D = (sr_muldata *)data;
     714     1594700 :   if (typ(x) == t_INT && lgefint(x) >= D->prec)
     715             :   { /* switch to t_REAL */
     716      157684 :     D->sqr   = &sqrr;
     717      157684 :     D->mulug = &mulur; x = itor(x, D->prec);
     718             :   }
     719     1594700 :   return D->sqr(x);
     720             : }
     721             : 
     722             : static GEN
     723      623575 : _rpowuu_msqr(void *data, GEN x)
     724             : {
     725      623575 :   GEN x2 = _rpowuu_sqr(data, x);
     726      623575 :   sr_muldata *D = (sr_muldata *)data;
     727      623575 :   return D->mulug(D->a, x2);
     728             : }
     729             : 
     730             : /* return a^n as a t_REAL of precision prec. Assume a > 0, n > 0 */
     731             : GEN
     732      428375 : rpowuu(ulong a, ulong n, long prec)
     733             : {
     734             :   pari_sp av;
     735             :   GEN y, z;
     736             :   sr_muldata D;
     737             : 
     738      428375 :   if (a == 1) return real_1(prec);
     739      428375 :   if (a == 2) return real2n(n, prec);
     740      428375 :   if (n == 1) return utor(a, prec);
     741      423458 :   z = cgetr(prec);
     742      423458 :   av = avma;
     743      423458 :   D.sqr   = &sqri;
     744      423458 :   D.mulug = &mului;
     745      423458 :   D.prec = prec;
     746      423458 :   D.a = (long)a;
     747      423458 :   y = gen_powu_fold_i(utoipos(a), n, (void*)&D, &_rpowuu_sqr, &_rpowuu_msqr);
     748      423458 :   mpaff(y, z); return gc_const(av,z);
     749             : }
     750             : 
     751             : GEN
     752     9466757 : powrs(GEN x, long n)
     753             : {
     754     9466757 :   pari_sp av = avma;
     755             :   GEN y;
     756     9466757 :   if (!n) return powr0(x);
     757     9466757 :   y = gen_powu_i(x, (ulong)labs(n), NULL, &_sqrr, &_mulr);
     758     9467385 :   if (n < 0) y = invr(y);
     759     9466944 :   return gerepileuptoleaf(av,y);
     760             : }
     761             : GEN
     762     4877256 : powru(GEN x, ulong n)
     763             : {
     764     4877256 :   pari_sp av = avma;
     765             :   GEN y;
     766     4877256 :   if (!n) return powr0(x);
     767     4856725 :   y = gen_powu_i(x, n, NULL, &_sqrr, &_mulr);
     768     4856666 :   return gerepileuptoleaf(av,y);
     769             : }
     770             : 
     771             : GEN
     772       13797 : powersr(GEN x, long n)
     773             : {
     774       13797 :   long prec = realprec(x);
     775       13797 :   return gen_powers(x, n, 1, &prec, &_sqrr, &_mulr, &_oner);
     776             : }
     777             : 
     778             : /* x^(s/2), assume x t_REAL */
     779             : GEN
     780           0 : powrshalf(GEN x, long s)
     781             : {
     782           0 :   if (s & 1) return sqrtr(powrs(x, s));
     783           0 :   return powrs(x, s>>1);
     784             : }
     785             : /* x^(s/2), assume x t_REAL */
     786             : GEN
     787      117881 : powruhalf(GEN x, ulong s)
     788             : {
     789      117881 :   if (s & 1) return sqrtr(powru(x, s));
     790        7071 :   return powru(x, s>>1);
     791             : }
     792             : /* x^(n/d), assume x t_REAL, return t_REAL */
     793             : GEN
     794         511 : powrfrac(GEN x, long n, long d)
     795             : {
     796             :   long z;
     797         511 :   if (!n) return powr0(x);
     798           0 :   z = cgcd(n, d); if (z > 1) { n /= z; d /= z; }
     799           0 :   if (d == 1) return powrs(x, n);
     800           0 :   x = powrs(x, n);
     801           0 :   if (d == 2) return sqrtr(x);
     802           0 :   return sqrtnr(x, d);
     803             : }
     804             : 
     805             : /* assume x != 0 */
     806             : static GEN
     807      673638 : pow_monome(GEN x, long n)
     808             : {
     809      673638 :   long i, d, dx = degpol(x);
     810             :   GEN A, b, y;
     811             : 
     812      673638 :   if (n < 0) { n = -n; y = cgetg(3, t_RFRAC); } else y = NULL;
     813             : 
     814      673638 :   if (HIGHWORD(dx) || HIGHWORD(n))
     815           8 :   {
     816             :     LOCAL_HIREMAINDER;
     817           9 :     d = (long)mulll((ulong)dx, (ulong)n);
     818           9 :     if (hiremainder || (d &~ LGBITS)) d = LGBITS; /* overflow */
     819           9 :     d += 2;
     820             :   }
     821             :   else
     822      673629 :     d = dx*n + 2;
     823      673638 :   if ((d + 1) & ~LGBITS) pari_err(e_OVERFLOW,"pow_monome [degree]");
     824      673631 :   A = cgetg(d+1, t_POL); A[1] = x[1];
     825     6198524 :   for (i=2; i < d; i++) gel(A,i) = gen_0;
     826      673631 :   b = gpowgs(gel(x,dx+2), n); /* not memory clean if (n < 0) */
     827      673631 :   if (!y) y = A;
     828             :   else {
     829       20482 :     GEN c = denom_i(b);
     830       20482 :     gel(y,1) = c; if (c != gen_1) b = gmul(b,c);
     831       20482 :     gel(y,2) = A;
     832             :   }
     833      673631 :   gel(A,d) = b; return y;
     834             : }
     835             : 
     836             : /* x t_PADIC */
     837             : static GEN
     838     1304946 : powps(GEN x, long n)
     839             : {
     840     1304946 :   long e = n*valp(x), v;
     841     1304946 :   GEN t, y, mod, p = gel(x,2);
     842             :   pari_sp av;
     843             : 
     844     1304946 :   if (!signe(gel(x,4))) {
     845          84 :     if (n < 0) pari_err_INV("powps",x);
     846          77 :     return zeropadic(p, e);
     847             :   }
     848     1304862 :   v = z_pval(n, p);
     849             : 
     850     1304863 :   y = cgetg(5,t_PADIC);
     851     1304863 :   mod = gel(x,3);
     852     1304863 :   if (v == 0) mod = icopy(mod);
     853             :   else
     854             :   {
     855       86426 :     if (precp(x) == 1 && absequaliu(p, 2)) v++;
     856       86426 :     mod = mulii(mod, powiu(p,v));
     857       86426 :     mod = gerepileuptoint((pari_sp)y, mod);
     858             :   }
     859     1304861 :   y[1] = evalprecp(precp(x) + v) | evalvalp(e);
     860     1304860 :   gel(y,2) = icopy(p);
     861     1304862 :   gel(y,3) = mod;
     862             : 
     863     1304862 :   av = avma; t = gel(x,4);
     864     1304862 :   if (n < 0) { t = Fp_inv(t, mod); n = -n; }
     865     1304862 :   t = Fp_powu(t, n, mod);
     866     1304868 :   gel(y,4) = gerepileuptoint(av, t);
     867     1304866 :   return y;
     868             : }
     869             : /* x t_PADIC */
     870             : static GEN
     871         161 : powp(GEN x, GEN n)
     872             : {
     873             :   long v;
     874         161 :   GEN y, mod, p = gel(x,2);
     875             : 
     876         161 :   if (valp(x)) pari_err_OVERFLOW("valp()");
     877             : 
     878         161 :   if (!signe(gel(x,4))) {
     879          14 :     if (signe(n) < 0) pari_err_INV("powp",x);
     880           7 :     return zeropadic(p, 0);
     881             :   }
     882         147 :   v = Z_pval(n, p);
     883             : 
     884         147 :   y = cgetg(5,t_PADIC);
     885         147 :   mod = gel(x,3);
     886         147 :   if (v == 0) mod = icopy(mod);
     887             :   else
     888             :   {
     889          70 :     mod = mulii(mod, powiu(p,v));
     890          70 :     mod = gerepileuptoint((pari_sp)y, mod);
     891             :   }
     892         147 :   y[1] = evalprecp(precp(x) + v) | _evalvalp(0);
     893         147 :   gel(y,2) = icopy(p);
     894         147 :   gel(y,3) = mod;
     895         147 :   gel(y,4) = Fp_pow(gel(x,4), n, mod);
     896         147 :   return y;
     897             : }
     898             : static GEN
     899       31349 : pow_polmod(GEN x, GEN n)
     900             : {
     901       31349 :   GEN z = cgetg(3, t_POLMOD), a = gel(x,2), T = gel(x,1);
     902       31349 :   gel(z,1) = gcopy(T);
     903       31349 :   if (typ(a) != t_POL || varn(a) != varn(T) || lg(a) <= 3)
     904        1556 :     a = powgi(a, n);
     905             :   else {
     906       29793 :     pari_sp av = avma;
     907       29793 :     GEN p = NULL;
     908       29793 :     if (RgX_is_FpX(T, &p) && RgX_is_FpX(a, &p) && p)
     909             :     {
     910        7602 :       T = RgX_to_FpX(T, p); a = RgX_to_FpX(a, p);
     911        7602 :       if (lgefint(p) == 3)
     912             :       {
     913        7595 :         ulong pp = p[2];
     914        7595 :         a = Flxq_pow(ZX_to_Flx(a, pp), n, ZX_to_Flx(T, pp), pp);
     915        7595 :         a = Flx_to_ZX(a);
     916             :       }
     917             :       else
     918           7 :         a = FpXQ_pow(a, n, T, p);
     919        7602 :       a = FpX_to_mod(a, p);
     920        7602 :       a = gerepileupto(av, a);
     921             :     }
     922             :     else
     923             :     {
     924       22191 :       set_avma(av);
     925       22191 :       a = RgXQ_pow(a, n, gel(z,1));
     926             :     }
     927             :   }
     928       31349 :   gel(z,2) = a; return z;
     929             : }
     930             : 
     931             : GEN
     932   117124611 : gpowgs(GEN x, long n)
     933             : {
     934             :   long m;
     935             :   pari_sp av;
     936             :   GEN y;
     937             : 
     938   117124611 :   if (n == 0) return gpowg0(x);
     939   115921919 :   if (n == 1)
     940             :   {
     941    73228309 :     long t = typ(x);
     942    73228309 :     if (is_scalar_t(t)) return gcopy(x);
     943      715337 :     switch(t)
     944             :     {
     945      662363 :       case t_POL: case t_SER: case t_RFRAC: case t_MAT: case t_VECSMALL:
     946      662363 :         return gcopy(x);
     947          21 :       case t_VEC: if (!is_ext_qfr(x)) break;
     948             :       /* fall through handle extended t_QFB */
     949       52961 :       case t_QFB: return qfbred(x);
     950             :     }
     951          13 :     pari_err_TYPE("gpow", x);
     952             :   }
     953    42693777 :   if (n ==-1) return ginv(x);
     954    39537076 :   switch(typ(x))
     955             :   {
     956    23486834 :     case t_INT: return powis(x,n);
     957     9458344 :     case t_REAL: return powrs(x,n);
     958       29367 :     case t_INTMOD:
     959       29367 :       y = cgetg(3,t_INTMOD); gel(y,1) = icopy(gel(x,1));
     960       29367 :       gel(y,2) = Fp_pows(gel(x,2), n, gel(x,1));
     961       29367 :       return y;
     962      297020 :     case t_FRAC:
     963             :     {
     964      297020 :       GEN a = gel(x,1), b = gel(x,2);
     965      297020 :       long s = (signe(a) < 0 && odd(n))? -1: 1;
     966      297020 :       if (n < 0) {
     967         700 :         n = -n;
     968         700 :         if (is_pm1(a)) return powiu_sign(b, n, s); /* +-1/x[2] inverts to t_INT */
     969         490 :         swap(a, b);
     970             :       }
     971      296810 :       y = cgetg(3, t_FRAC);
     972      296810 :       gel(y,1) = powiu_sign(a, n, s);
     973      296810 :       gel(y,2) = powiu_sign(b, n, 1);
     974      296810 :       return y;
     975             :     }
     976     1304947 :     case t_PADIC: return powps(x, n);
     977      249165 :     case t_RFRAC:
     978             :     {
     979      249165 :       av = avma; y = cgetg(3, t_RFRAC); m = labs(n);
     980      249165 :       gel(y,1) = gpowgs(gel(x,1),m);
     981      249165 :       gel(y,2) = gpowgs(gel(x,2),m);
     982      249165 :       if (n < 0) y = ginv(y);
     983      249165 :       return gerepileupto(av,y);
     984             :     }
     985       31342 :     case t_POLMOD: {
     986       31342 :       long N[] = {evaltyp(t_INT) | _evallg(3),0,0};
     987       31342 :       affsi(n,N); return pow_polmod(x, N);
     988             :     }
     989          21 :     case t_VEC: if (!is_ext_qfr(x)) pari_err_TYPE("gpow", x);
     990             :     /* fall through handle extended t_QFB */
     991     1293837 :     case t_QFB: return qfbpows(x, n);
     992     1256253 :     case t_POL:
     993     1256253 :       if (RgX_is_monomial(x)) return pow_monome(x, n);
     994             :     default: {
     995     2712575 :       pari_sp av = avma;
     996     2712575 :       y = gen_powu_i(x, (ulong)labs(n), NULL, &_sqr, &_mul);
     997     2712586 :       if (n < 0) y = ginv(y);
     998     2712584 :       return gerepileupto(av,y);
     999             :     }
    1000             :   }
    1001             : }
    1002             : 
    1003             : /* n a t_INT */
    1004             : GEN
    1005    98380692 : powgi(GEN x, GEN n)
    1006             : {
    1007             :   GEN y;
    1008             : 
    1009    98380692 :   if (!is_bigint(n)) return gpowgs(x, itos(n));
    1010             :   /* probable overflow for nonmodular types (typical exception: (X^0)^N) */
    1011       25686 :   switch(typ(x))
    1012             :   {
    1013       25361 :     case t_INTMOD:
    1014       25361 :       y = cgetg(3,t_INTMOD); gel(y,1) = icopy(gel(x,1));
    1015       25362 :       gel(y,2) = Fp_pow(gel(x,2), n, gel(x,1));
    1016       25366 :       return y;
    1017         101 :     case t_FFELT: return FF_pow(x,n);
    1018         161 :     case t_PADIC: return powp(x, n);
    1019             : 
    1020          35 :     case t_INT:
    1021          35 :       if (is_pm1(x)) return (signe(x) < 0 && mpodd(n))? gen_m1: gen_1;
    1022          14 :       if (signe(x)) pari_err_OVERFLOW("lg()");
    1023           7 :       if (signe(n) < 0) pari_err_INV("powgi",gen_0);
    1024           7 :       return gen_0;
    1025           7 :     case t_FRAC:
    1026           7 :       pari_err_OVERFLOW("lg()");
    1027             : 
    1028           0 :     case t_VEC: if (!is_ext_qfr(x)) pari_err_TYPE("gpow",x);
    1029             :     /* fall through handle extended t_QFB */
    1030          12 :     case t_QFB: return qfbpow(x, n);
    1031           7 :     case t_POLMOD: return pow_polmod(x, n);
    1032           7 :     default: {
    1033           7 :       pari_sp av = avma;
    1034           7 :       y = gen_pow_i(x, n, NULL, &_sqr, &_mul);
    1035           7 :       if (signe(n) < 0) return gerepileupto(av, ginv(y));
    1036           7 :       return gerepilecopy(av,y);
    1037             :     }
    1038             :   }
    1039             : }
    1040             : 
    1041             : /* Assume x = 1 + O(t), n a scalar. Return x^n */
    1042             : static GEN
    1043        7847 : ser_pow_1(GEN x, GEN n)
    1044             : {
    1045             :   long lx, mi, i, j, d;
    1046        7847 :   GEN y = cgetg_copy(x, &lx), X = x+2, Y = y + 2;
    1047        7847 :   y[1] = evalsigne(1) | _evalvalser(0) | evalvarn(varn(x));
    1048       74172 :   d = mi = lx-3; while (mi>=1 && isrationalzero(gel(X,mi))) mi--;
    1049        7847 :   gel(Y,0) = gen_1;
    1050      110348 :   for (i=1; i<=d; i++)
    1051             :   {
    1052      102501 :     pari_sp av = avma;
    1053      102501 :     GEN s = gen_0;
    1054      487606 :     for (j=1; j<=minss(i,mi); j++)
    1055             :     {
    1056      385105 :       GEN t = gsubgs(gmulgu(n,j),i-j);
    1057      385105 :       s = gadd(s, gmul(gmul(t, gel(X,j)), gel(Y,i-j)));
    1058             :     }
    1059      102501 :     gel(Y,i) = gerepileupto(av, gdivgu(s,i));
    1060             :   }
    1061        7847 :   return y;
    1062             : }
    1063             : 
    1064             : /* we suppose n != 0, valser(x) = 0 and leading-term(x) != 0. Not stack clean */
    1065             : static GEN
    1066        7952 : ser_pow(GEN x, GEN n, long prec)
    1067             : {
    1068             :   GEN y, c, lead;
    1069        7952 :   if (varncmp(gvar(n), varn(x)) <= 0) return gexp(gmul(n, glog(x,prec)), prec);
    1070        7847 :   lead = gel(x,2);
    1071        7847 :   if (gequal1(lead)) return ser_pow_1(x, n);
    1072        7462 :   x = ser_normalize(x);
    1073        7462 :   if (typ(n) == t_FRAC && !isinexact(lead) && ispower(lead, gel(n,2), &c))
    1074         105 :     c = powgi(c, gel(n,1));
    1075             :   else
    1076        7357 :     c = gpow(lead,n, prec);
    1077        7462 :   y = gmul(c, ser_pow_1(x, n));
    1078             :   /* gpow(t_POLMOD,n) can be a t_COL [conjvec] */
    1079        7462 :   if (typ(y) != t_SER) pari_err_TYPE("gpow", y);
    1080        7462 :   return y;
    1081             : }
    1082             : 
    1083             : static long
    1084        7861 : val_from_i(GEN E)
    1085             : {
    1086        7861 :   if (is_bigint(E)) pari_err_OVERFLOW("sqrtn [valuation]");
    1087        7854 :   return itos(E);
    1088             : }
    1089             : 
    1090             : /* return x^q, assume typ(x) = t_SER, typ(q) = t_INT/t_FRAC and q != 0 */
    1091             : static GEN
    1092        7868 : ser_powfrac(GEN x, GEN q, long prec)
    1093             : {
    1094        7868 :   GEN y, E = gmulsg(valser(x), q);
    1095             :   long e;
    1096             : 
    1097        7868 :   if (!signe(x))
    1098             :   {
    1099          21 :     if (gsigne(q) < 0) pari_err_INV("gpow", x);
    1100          21 :     return zeroser(varn(x), val_from_i(gfloor(E)));
    1101             :   }
    1102        7847 :   if (typ(E) != t_INT)
    1103           7 :     pari_err_DOMAIN("sqrtn", "valuation", "!=", mkintmod(gen_0, gel(q,2)), x);
    1104        7840 :   e = val_from_i(E);
    1105        7840 :   y = leafcopy(x); setvalser(y, 0);
    1106        7840 :   y = ser_pow(y, q, prec);
    1107        7840 :   setvalser(y, e); return y;
    1108             : }
    1109             : 
    1110             : static GEN
    1111         126 : gpow0(GEN x, GEN n, long prec)
    1112             : {
    1113         126 :   pari_sp av = avma;
    1114             :   long i, lx;
    1115             :   GEN y;
    1116         126 :   switch(typ(n))
    1117             :   {
    1118          84 :     case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX: case t_QUAD:
    1119          84 :       break;
    1120          35 :     case t_VEC: case t_COL: case t_MAT:
    1121          35 :       y = cgetg_copy(n, &lx);
    1122         105 :       for (i=1; i<lx; i++) gel(y,i) = gpow0(x,gel(n,i),prec);
    1123          35 :       return y;
    1124           7 :     default: pari_err_TYPE("gpow(0,n)", n);
    1125             :   }
    1126          84 :   n = real_i(n);
    1127          84 :   if (gsigne(n) <= 0) pari_err_DOMAIN("gpow(0,n)", "n", "<=", gen_0, n);
    1128          77 :   if (!precision(x)) return gcopy(x);
    1129             : 
    1130          14 :   x = ground(gmulsg(gexpo(x),n));
    1131          14 :   if (is_bigint(x) || uel(x,2) >= HIGHEXPOBIT)
    1132           7 :     pari_err_OVERFLOW("gpow");
    1133           7 :   set_avma(av); return real_0_bit(itos(x));
    1134             : }
    1135             : 
    1136             : /* centermod(x, log(2)), set *sh to the quotient */
    1137             : static GEN
    1138    15728103 : modlog2(GEN x, long *sh)
    1139             : {
    1140    15728103 :   double d = rtodbl(x), qd = (fabs(d) + M_LN2/2)/M_LN2;
    1141             :   long q;
    1142    15728107 :   if (dblexpo(qd) >= BITS_IN_LONG-1) pari_err_OVERFLOW("expo()");
    1143    15728156 :   q = d < 0 ? - (long) qd: (long) qd;
    1144    15728156 :   *sh = q;
    1145    15728156 :   if (q) {
    1146    12739000 :     long l = realprec(x) + 1;
    1147    12739000 :     x = subrr(rtor(x,l), mulsr(q, mplog2(l)));
    1148    12737943 :     if (!signe(x)) return NULL;
    1149             :   }
    1150    15727099 :   return x;
    1151             : }
    1152             : 
    1153             : /* x^n, n a t_FRAC */
    1154             : static GEN
    1155     5130590 : powfrac(GEN x, GEN n, long prec)
    1156             : {
    1157     5130590 :   GEN a = gel(n,1), d = gel(n,2);
    1158     5130590 :   long D = itos_or_0(d);
    1159     5130567 :   if (D == 2)
    1160             :   {
    1161     3537012 :     GEN y = gsqrt(x,prec);
    1162     3537450 :     if (!equali1(a)) y = gmul(y, powgi(x, shifti(subiu(a,1), -1)));
    1163     3537175 :     return y;
    1164             :   }
    1165     1593555 :   if (D && (is_real_t(typ(x)) && gsigne(x) > 0))
    1166             :   {
    1167             :     GEN z;
    1168     1589534 :     prec += nbits2extraprec(expi(a));
    1169     1589540 :     if (typ(x) != t_REAL) x = gtofp(x, prec);
    1170     1589540 :     z = sqrtnr(x, D);
    1171     1589540 :     if (!equali1(a)) z = powgi(z, a);
    1172     1589540 :     return z;
    1173             :   }
    1174        4021 :   return NULL;
    1175             : }
    1176             : 
    1177             : /* n = a+ib, x > 0 real, ex ~ |log2(x)|; return precision at which
    1178             :  * log(x) must be computed to evaluate x^n */
    1179             : long
    1180      182141 : powcx_prec(long ex, GEN n, long prec)
    1181             : {
    1182      182141 :   GEN a = gel(n,1), b = gel(n,2);
    1183      182141 :   long e = (ex < 2)? 0: expu(ex);
    1184      182141 :   e += gexpo_safe(is_rational_t(typ(a))? b: n);
    1185      182141 :   return e > 2? prec + nbits2extraprec(e): prec;
    1186             : }
    1187             : GEN
    1188      266668 : powcx(GEN x, GEN logx, GEN n, long prec)
    1189             : {
    1190      266668 :   GEN sxb, cxb, xa, a = gel(n,1), xb = gmul(gel(n,2), logx);
    1191      266668 :   long sh, p = realprec(logx);
    1192      266668 :   switch(typ(a))
    1193             :   {
    1194       49498 :     case t_INT: xa = powgi(x, a); break;
    1195      127974 :     case t_FRAC: xa = powfrac(x, a, prec);
    1196      127974 :                  if (xa) break;
    1197             :     default:
    1198       89203 :       xa = modlog2(gmul(gel(n,1), logx), &sh);
    1199       89203 :       if (!xa) xa = real2n(sh, prec);
    1200             :       else
    1201             :       {
    1202       89203 :         if (signe(xa) && realprec(xa) > prec) setlg(xa, prec);
    1203       89203 :         xa = mpexp(xa); shiftr_inplace(xa, sh);
    1204             :       }
    1205             :   }
    1206      266668 :   if (typ(xb) != t_REAL) return xa;
    1207      266668 :   if (gexpo(xb) > 30)
    1208             :   {
    1209           0 :     GEN q, P = Pi2n(-2, p), z = addrr(xb,P); /* = x + Pi/4 */
    1210           0 :     shiftr_inplace(P, 1);
    1211           0 :     q = floorr(divrr(z, P)); /* round ( x / (Pi/2) ) */
    1212           0 :     xb = subrr(xb, mulir(q, P)); /* x mod Pi/2  */
    1213           0 :     sh = Mod4(q);
    1214             :   }
    1215             :   else
    1216             :   {
    1217      266668 :     long q = floor(rtodbl(xb) / (M_PI/2) + 0.5);
    1218      266668 :     if (q) xb = subrr(xb, mulsr(q, Pi2n(-1,p))); /* x mod Pi/2  */
    1219      266668 :     sh = q & 3;
    1220             :   }
    1221      266668 :   if (signe(xb) && realprec(xb) > prec) setlg(xb, prec);
    1222      266668 :   mpsincos(xb, &sxb, &cxb);
    1223      266668 :   return gmul(xa, mulcxpowIs(mkcomplex(cxb, sxb), sh));
    1224             : }
    1225             : 
    1226             : GEN
    1227    20371189 : gpow(GEN x, GEN n, long prec)
    1228             : {
    1229    20371189 :   long prec0, i, lx, tx, tn = typ(n);
    1230             :   pari_sp av;
    1231             :   GEN y;
    1232             : 
    1233    20371189 :   if (tn == t_INT) return powgi(x,n);
    1234     5343346 :   tx = typ(x);
    1235     5343346 :   if (is_matvec_t(tx))
    1236             :   {
    1237          49 :     y = cgetg_copy(x, &lx);
    1238         133 :     for (i=1; i<lx; i++) gel(y,i) = gpow(gel(x,i),n,prec);
    1239          49 :     return y;
    1240             :   }
    1241     5343370 :   av = avma;
    1242     5343370 :   switch (tx)
    1243             :   {
    1244          28 :     case t_POL: case t_RFRAC: x = toser_i(x); /* fall through */
    1245        7560 :     case t_SER:
    1246        7560 :       if (tn == t_FRAC) return gerepileupto(av, ser_powfrac(x, n, prec));
    1247         140 :       if (valser(x))
    1248          21 :         pari_err_DOMAIN("gpow [irrational exponent]",
    1249             :                         "valuation", "!=", gen_0, x);
    1250         119 :       if (lg(x) == 2) return gerepilecopy(av, x); /* O(1) */
    1251         112 :       return gerepileupto(av, ser_pow(x, n, prec));
    1252             :   }
    1253     5335819 :   if (gequal0(x)) return gpow0(x, n, prec);
    1254     5335772 :   if (tn == t_FRAC)
    1255             :   {
    1256     5005627 :     GEN p, z, a = gel(n,1), d = gel(n,2);
    1257     5005627 :     switch (tx)
    1258             :     {
    1259     1474004 :     case t_INT:
    1260     1474004 :       if (signe(x) < 0)
    1261             :       {
    1262          42 :         if (equaliu(d, 2) && Z_issquareall(negi(x), &z))
    1263             :         {
    1264          21 :           z = powgi(z, a);
    1265          21 :           if (Mod4(a) == 3) z = gneg(z);
    1266     5001631 :           return gerepilecopy(av, mkcomplex(gen_0, z));
    1267             :         }
    1268          21 :         break;
    1269             :       }
    1270     1473962 :       if (ispower(x, d, &z)) return powgi(z, a);
    1271     1472398 :       break;
    1272       69827 :     case t_FRAC:
    1273       69827 :       if (signe(gel(x,1)) < 0)
    1274             :       {
    1275          28 :         if (equaliu(d, 2) && ispower(absfrac(x), d, &z))
    1276           7 :           return gerepilecopy(av, mkcomplex(gen_0, powgi(z, a)));
    1277          21 :         break;
    1278             :       }
    1279       69799 :       if (ispower(x, d, &z)) return powgi(z, a);
    1280       68427 :       break;
    1281             : 
    1282          21 :     case t_INTMOD:
    1283          21 :       p = gel(x,1);
    1284          21 :       if (!BPSW_psp(p)) pari_err_PRIME("gpow",p);
    1285          14 :       y = cgetg(3,t_INTMOD); gel(y,1) = icopy(p);
    1286          14 :       av = avma;
    1287          14 :       z = Fp_sqrtn(gel(x,2), d, p, NULL);
    1288          14 :       if (!z) pari_err_SQRTN("gpow",x);
    1289           7 :       gel(y,2) = gerepileuptoint(av, Fp_pow(z, a, p));
    1290           7 :       return y;
    1291             : 
    1292          14 :     case t_PADIC:
    1293          14 :       z = Qp_sqrtn(x, d, NULL); if (!z) pari_err_SQRTN("gpow",x);
    1294           7 :       return gerepileupto(av, powgi(z, a));
    1295             : 
    1296          21 :     case t_FFELT:
    1297          21 :       return gerepileupto(av,FF_pow(FF_sqrtn(x,d,NULL),a));
    1298             :     }
    1299     5002607 :     z = powfrac(x, n, prec);
    1300     5002741 :     if (z) return gerepileupto(av, z);
    1301             :   }
    1302      334159 :   if (tn == t_COMPLEX && is_real_t(typ(x)) && gsigne(x) > 0)
    1303             :   {
    1304      171403 :     long p = powcx_prec(fabs(dbllog2(x)), n, prec);
    1305      171403 :     return gerepileupto(av, powcx(x, glog(x, p), n, prec));
    1306             :   }
    1307      162756 :   if (tn == t_PADIC) x = gcvtop(x, gel(n,2), precp(n));
    1308      162756 :   i = precision(n);
    1309      162756 :   if (i) prec = i;
    1310      162756 :   prec0 = prec;
    1311      162756 :   if (!gprecision(x))
    1312             :   {
    1313       38647 :     long e = gexpo_safe(n); /* avoided if n = 0 or gexpo not defined */
    1314       38647 :     if (e > 2) prec += nbits2extraprec(e);
    1315             :   }
    1316      162756 :   y = gmul(n, glog(x,prec));
    1317      162728 :   y = gexp(y,prec);
    1318      162728 :   if (prec0 == prec) return gerepileupto(av, y);
    1319       29246 :   return gerepilecopy(av, gprec_wtrunc(y,prec0));
    1320             : }
    1321             : GEN
    1322       10220 : powPis(GEN s, long prec)
    1323             : {
    1324       10220 :   pari_sp av = avma;
    1325             :   GEN x;
    1326       10220 :   if (typ(s) != t_COMPLEX) return gpow(mppi(prec), s, prec);
    1327         441 :   x = mppi(powcx_prec(1, s, prec));
    1328         441 :   return gerepileupto(av, powcx(x, logr_abs(x), s, prec));
    1329             : }
    1330             : GEN
    1331        7868 : pow2Pis(GEN s, long prec)
    1332             : {
    1333        7868 :   pari_sp av = avma;
    1334             :   GEN x;
    1335        7868 :   if (typ(s) != t_COMPLEX) return gpow(Pi2n(1,prec), s, prec);
    1336        1876 :   x = Pi2n(1, powcx_prec(2, s, prec));
    1337        1876 :   return gerepileupto(av, powcx(x, logr_abs(x), s, prec));
    1338             : }
    1339             : 
    1340             : GEN
    1341      200113 : gpowers0(GEN x, long n, GEN x0)
    1342             : {
    1343             :   long i, l;
    1344             :   GEN V;
    1345      200113 :   if (!x0) return gpowers(x,n);
    1346      185624 :   if (n < 0) return cgetg(1,t_VEC);
    1347      185624 :   l = n+2; V = cgetg(l, t_VEC); gel(V,1) = gcopy(x0);
    1348     7610955 :   for (i = 2; i < l; i++) gel(V,i) = gmul(gel(V,i-1),x);
    1349      185635 :   return V;
    1350             : }
    1351             : 
    1352             : GEN
    1353      324689 : gpowers(GEN x, long n)
    1354             : {
    1355      324689 :   if (n < 0) return cgetg(1,t_VEC);
    1356      324682 :   return gen_powers(x, n, 0, (void*)x, &_sqr, &_mul, &_one);
    1357             : }
    1358             : 
    1359             : /* return [q^1,q^4,...,q^{n^2}] */
    1360             : GEN
    1361       37434 : gsqrpowers(GEN q, long n)
    1362             : {
    1363       37434 :   pari_sp av = avma;
    1364       37434 :   GEN L = gpowers0(gsqr(q), n, q); /* L[i] = q^(2i - 1), i <= n+1 */
    1365       37434 :   GEN v = cgetg(n+1, t_VEC);
    1366             :   long i;
    1367       37434 :   gel(v, 1) = gcopy(q);
    1368     6945196 :   for (i = 2; i <= n ; ++i) gel(v, i) = q = gmul(q, gel(L,i)); /* q^(i^2) */
    1369       37434 :   return gerepileupto(av, v);
    1370             : }
    1371             : 
    1372             : /* 4 | N. returns a vector RU which contains exp(2*i*k*Pi/N), k=0..N-1 */
    1373             : static GEN
    1374      575683 : grootsof1_4(long N, long prec)
    1375             : {
    1376      575683 :   GEN z, RU = cgetg(N+1,t_COL), *v  = ((GEN*)RU) + 1;
    1377      575679 :   long i, N2 = (N>>1), N4 = (N>>2), N8 = (N>>3);
    1378             :   /* z^N2 = -1, z^N4 = I; if z^k = a+I*b, then z^(N4-k) = I*conj(z) = b+a*I */
    1379             : 
    1380      575679 :   v[0] = gen_1; v[1] = z = rootsof1u_cx(N, prec);
    1381      575686 :   if (odd(N4)) N8++;
    1382      686493 :   for (i=1; i<N8; i++)
    1383             :   {
    1384      110814 :     GEN t = v[i];
    1385      110814 :     v[i+1] = gmul(z, t);
    1386      110813 :     v[N4-i] = mkcomplex(gel(t,2), gel(t,1));
    1387             :   }
    1388     1687806 :   for (i=0; i<N4; i++) v[i+N4] = mulcxI(v[i]);
    1389     2799936 :   for (i=0; i<N2; i++) v[i+N2] = gneg(v[i]);
    1390      575680 :   return RU;
    1391             : }
    1392             : 
    1393             : /* as above, N arbitrary */
    1394             : GEN
    1395      713540 : grootsof1(long N, long prec)
    1396             : {
    1397             :   GEN z, RU, *v;
    1398             :   long i, k;
    1399             : 
    1400      713540 :   if (N <= 0) pari_err_DOMAIN("rootsof1", "N", "<=", gen_0, stoi(N));
    1401      713527 :   if ((N & 3) == 0) return grootsof1_4(N, prec);
    1402      137844 :   if (N <= 2) return N == 1? mkcol(gen_1): mkcol2(gen_1, gen_m1);
    1403       14227 :   k = (N+1)>>1;
    1404       14227 :   RU = cgetg(N+1,t_COL);
    1405       14227 :   v  = ((GEN*)RU) + 1;
    1406       14227 :   v[0] = gen_1; v[1] = z = rootsof1u_cx(N, prec);
    1407       74286 :   for (i=2; i<k; i++) v[i] = gmul(z, v[i-1]);
    1408       14227 :   if (!odd(N)) v[i++] = gen_m1; /*avoid loss of accuracy*/
    1409       88513 :   for (   ; i<N; i++) v[i] = gconj(v[N-i]);
    1410       14227 :   return RU;
    1411             : }
    1412             : 
    1413             : /********************************************************************/
    1414             : /**                                                                **/
    1415             : /**                        RACINE CARREE                           **/
    1416             : /**                                                                **/
    1417             : /********************************************************************/
    1418             : /* assume x unit, e = precp(x) */
    1419             : GEN
    1420      144690 : Z2_sqrt(GEN x, long e)
    1421             : {
    1422      144690 :   ulong r = signe(x)>=0?mod16(x):16-mod16(x);
    1423             :   GEN z;
    1424             :   long ez;
    1425             :   pari_sp av;
    1426             : 
    1427      144690 :   switch(e)
    1428             :   {
    1429           7 :     case 1: return gen_1;
    1430         161 :     case 2: return (r & 3UL) == 1? gen_1: NULL;
    1431          28 :     case 3: return (r & 7UL) == 1? gen_1: NULL;
    1432       71064 :     case 4: if (r == 1) return gen_1;
    1433       35133 :             else return (r == 9)? utoipos(3): NULL;
    1434       73430 :     default: if ((r&7UL) != 1) return NULL;
    1435             :   }
    1436       73430 :   av = avma; z = (r==1)? gen_1: utoipos(3);
    1437       73430 :   ez = 3; /* number of correct bits in z (compared to sqrt(x)) */
    1438             :   for(;;)
    1439       47978 :   {
    1440             :     GEN mod;
    1441      121408 :     ez = (ez<<1) - 1;
    1442      121408 :     if (ez > e) ez = e;
    1443      121408 :     mod = int2n(ez);
    1444      121408 :     z = addii(z, remi2n(mulii(x, Fp_inv(z,mod)), ez));
    1445      121408 :     z = shifti(z, -1); /* (z + x/z) / 2 */
    1446      121408 :     if (e == ez) return gerepileuptoint(av, z);
    1447       47978 :     if (ez < e) ez--;
    1448       47978 :     if (gc_needed(av,2))
    1449             :     {
    1450           0 :       if (DEBUGMEM > 1) pari_warn(warnmem,"Qp_sqrt");
    1451           0 :       z = gerepileuptoint(av,z);
    1452             :     }
    1453             :   }
    1454             : }
    1455             : 
    1456             : /* x unit defined modulo p^e, e > 0 */
    1457             : GEN
    1458        1897 : Qp_sqrt(GEN x)
    1459             : {
    1460        1897 :   long pp, e = valp(x);
    1461        1897 :   GEN z,y,mod, p = gel(x,2);
    1462             : 
    1463        1897 :   if (gequal0(x)) return zeropadic(p, (e+1) >> 1);
    1464        1883 :   if (e & 1) return NULL;
    1465             : 
    1466        1869 :   y = cgetg(5,t_PADIC);
    1467        1869 :   pp = precp(x);
    1468        1869 :   mod = gel(x,3);
    1469        1869 :   z   = gel(x,4); /* lift to t_INT */
    1470        1869 :   e >>= 1;
    1471        1869 :   z = Zp_sqrt(z, p, pp);
    1472        1869 :   if (!z) return NULL;
    1473        1806 :   if (absequaliu(p,2))
    1474             :   {
    1475         805 :     pp  = (pp <= 3) ? 1 : pp-1;
    1476         805 :     mod = int2n(pp);
    1477             :   }
    1478        1001 :   else mod = icopy(mod);
    1479        1806 :   y[1] = evalprecp(pp) | evalvalp(e);
    1480        1806 :   gel(y,2) = icopy(p);
    1481        1806 :   gel(y,3) = mod;
    1482        1806 :   gel(y,4) = z; return y;
    1483             : }
    1484             : 
    1485             : GEN
    1486         420 : Zn_sqrt(GEN d, GEN fn)
    1487             : {
    1488         420 :   pari_sp ltop = avma, btop;
    1489         420 :   GEN b = gen_0, m = gen_1;
    1490             :   long j, np;
    1491         420 :   if (typ(d) != t_INT) pari_err_TYPE("Zn_sqrt",d);
    1492         420 :   if (typ(fn) == t_INT)
    1493           0 :     fn = absZ_factor(fn);
    1494         420 :   else if (!is_Z_factorpos(fn))
    1495           0 :     pari_err_TYPE("Zn_sqrt",fn);
    1496         420 :   np = nbrows(fn);
    1497         420 :   btop = avma;
    1498        1680 :   for (j = 1; j <= np; ++j)
    1499             :   {
    1500             :     GEN  bp, mp, pr, r;
    1501        1260 :     GEN  p = gcoeff(fn, j, 1);
    1502        1260 :     long e = itos(gcoeff(fn, j, 2));
    1503        1260 :     long v = Z_pvalrem(d,p,&r);
    1504        1260 :     if (v >= e) bp =gen_0;
    1505             :     else
    1506             :     {
    1507        1134 :       if (odd(v)) return NULL;
    1508        1134 :       bp = Zp_sqrt(r, p, e-v);
    1509        1134 :       if (!bp)    return NULL;
    1510        1134 :       if (v) bp = mulii(bp, powiu(p, v>>1L));
    1511             :     }
    1512        1260 :     mp = powiu(p, e);
    1513        1260 :     pr = mulii(m, mp);
    1514        1260 :     b = Z_chinese_coprime(b, bp, m, mp, pr);
    1515        1260 :     m = pr;
    1516        1260 :     if (gc_needed(btop, 1))
    1517           0 :       gerepileall(btop, 2, &b, &m);
    1518             :   }
    1519         420 :   return gerepileupto(ltop, b);
    1520             : }
    1521             : 
    1522             : static GEN
    1523       18690 : sqrt_ser(GEN b, long prec)
    1524             : {
    1525       18690 :   long e = valser(b), vx = varn(b), lx, lold, j;
    1526             :   ulong mask;
    1527             :   GEN a, x, lta, ltx;
    1528             : 
    1529       18690 :   if (!signe(b)) return zeroser(vx, e>>1);
    1530       18690 :   a = leafcopy(b);
    1531       18690 :   x = cgetg_copy(b, &lx);
    1532       18690 :   if (e & 1)
    1533          14 :     pari_err_DOMAIN("sqrtn", "valuation", "!=", mkintmod(gen_0, gen_2), b);
    1534       18676 :   a[1] = x[1] = evalsigne(1) | evalvarn(0) | _evalvalser(0);
    1535       18676 :   lta = gel(a,2);
    1536       18676 :   if (gequal1(lta)) ltx = lta;
    1537       14833 :   else if (!issquareall(lta,&ltx)) ltx = gsqrt(lta,prec);
    1538       18669 :   gel(x,2) = ltx;
    1539      315819 :   for (j = 3; j < lx; j++) gel(x,j) = gen_0;
    1540       18669 :   setlg(x,3);
    1541       18669 :   mask = quadratic_prec_mask(lx - 2);
    1542       18669 :   lold = 1;
    1543       96421 :   while (mask > 1)
    1544             :   {
    1545       77752 :     GEN y, x2 = gmul2n(x,1);
    1546       77752 :     long l = lold << 1, lx;
    1547             : 
    1548       77752 :     if (mask & 1) l--;
    1549       77752 :     mask >>= 1;
    1550       77752 :     setlg(a, l + 2);
    1551       77752 :     setlg(x, l + 2);
    1552       77752 :     y = sqr_ser_part(x, lold, l-1) - lold;
    1553      374902 :     for (j = lold+2; j < l+2; j++) gel(y,j) = gsub(gel(y,j), gel(a,j));
    1554       77752 :     y += lold; setvalser(y, lold);
    1555       77752 :     y = normalizeser(y);
    1556       77752 :     y = gsub(x, gdiv(y, x2)); /* = gmul2n(gsub(x, gdiv(a,x)), -1); */
    1557       77752 :     lx = minss(l+2, lg(y));
    1558      374895 :     for (j = lold+2; j < lx; j++) gel(x,j) = gel(y,j);
    1559       77752 :     lold = l;
    1560             :   }
    1561       18669 :   x[1] = evalsigne(1) | evalvarn(vx) | _evalvalser(e >> 1);
    1562       18669 :   return x;
    1563             : }
    1564             : 
    1565             : GEN
    1566    34960892 : gsqrt(GEN x, long prec)
    1567             : {
    1568             :   pari_sp av;
    1569             :   GEN y;
    1570             : 
    1571    34960892 :   switch(typ(x))
    1572             :   {
    1573      304030 :     case t_INT:
    1574      304030 :       if (!signe(x)) return real_0(prec); /* no loss of accuracy */
    1575      303960 :       x = itor(x,prec); /* fall through */
    1576    28815250 :     case t_REAL: return sqrtr(x);
    1577             : 
    1578          35 :     case t_INTMOD:
    1579             :     {
    1580          35 :       GEN p = gel(x,1), a;
    1581          35 :       y = cgetg(3,t_INTMOD); gel(y,1) = icopy(p);
    1582          35 :       a = Fp_sqrt(gel(x,2),p);
    1583          21 :       if (!a)
    1584             :       {
    1585           7 :         if (!BPSW_psp(p)) pari_err_PRIME("sqrt [modulus]",p);
    1586           7 :         pari_err_SQRTN("gsqrt",x);
    1587             :       }
    1588          14 :       gel(y,2) = a; return y;
    1589             :     }
    1590             : 
    1591     5881175 :     case t_COMPLEX:
    1592             :     { /* (u+iv)^2 = a+ib <=> u^2+v^2 = sqrt(a^2+b^2), u^2-v^2=a, 2uv=b */
    1593     5881175 :       GEN a = gel(x,1), b = gel(x,2), r, u, v;
    1594     5881175 :       if (isrationalzero(b)) return gsqrt(a, prec);
    1595     5881173 :       y = cgetg(3,t_COMPLEX); av = avma;
    1596             : 
    1597     5881172 :       r = cxnorm(x);
    1598     5881170 :       if (typ(r) == t_INTMOD || typ(r) == t_PADIC)
    1599           0 :         pari_err_IMPL("sqrt(complex of t_INTMODs)");
    1600     5881170 :       r = gsqrt(r, prec); /* t_REAL, |a+Ib| */
    1601     5881165 :       if (!signe(r))
    1602          67 :         u = v = gerepileuptoleaf(av, sqrtr(r));
    1603     5881098 :       else if (gsigne(a) < 0)
    1604             :       {
    1605             :         /* v > 0 since r > 0, a < 0, rounding errors can't make the sum of two
    1606             :          * positive numbers = 0 */
    1607      135157 :         v = sqrtr( gmul2n(gsub(r,a), -1) );
    1608      135157 :         if (gsigne(b) < 0) togglesign(v);
    1609      135157 :         v = gerepileuptoleaf(av, v); av = avma;
    1610             :         /* v = 0 is impossible */
    1611      135158 :         u = gerepileuptoleaf(av, gdiv(b, shiftr(v,1)));
    1612             :       } else {
    1613     5745941 :         u = sqrtr( gmul2n(gadd(r,a), -1) );
    1614     5745948 :         u = gerepileuptoleaf(av, u); av = avma;
    1615     5745950 :         if (!signe(u)) /* possible if a = 0.0, e.g. sqrt(0.e-10+1e-10*I) */
    1616           7 :           v = u;
    1617             :         else
    1618     5745943 :           v = gerepileuptoleaf(av, gdiv(b, shiftr(u,1)));
    1619             :       }
    1620     5881174 :       gel(y,1) = u;
    1621     5881174 :       gel(y,2) = v; return y;
    1622             :     }
    1623             : 
    1624          63 :     case t_PADIC:
    1625          63 :       y = Qp_sqrt(x);
    1626          63 :       if (!y) pari_err_SQRTN("Qp_sqrt",x);
    1627          42 :       return y;
    1628             : 
    1629         161 :     case t_FFELT: return FF_sqrt(x);
    1630             : 
    1631      264140 :     default:
    1632      264140 :       av = avma; if (!(y = toser_i(x))) break;
    1633       18690 :       return gerepilecopy(av, sqrt_ser(y, prec));
    1634             :   }
    1635      245450 :   return trans_eval("sqrt",gsqrt,x,prec);
    1636             : }
    1637             : /********************************************************************/
    1638             : /**                                                                **/
    1639             : /**                          N-th ROOT                             **/
    1640             : /**                                                                **/
    1641             : /********************************************************************/
    1642             : 
    1643             : static GEN
    1644      305289 : Z_to_padic(GEN a, GEN p, long e)
    1645             : {
    1646      305289 :   if (signe(a)==0)
    1647        1034 :     return zeropadic(p, e);
    1648             :   else
    1649             :   {
    1650      304255 :     GEN z = cgetg(5, t_PADIC);
    1651      304255 :     long v = Z_pvalrem(a, p, &a), d = e - v;
    1652      304255 :     z[1] = evalprecp(d) | evalvalp(v);
    1653      304255 :     gel(z,2) = icopy(p);
    1654      304255 :     gel(z,3) = powiu(p, d);
    1655      304257 :     gel(z,4) = a;
    1656      304257 :     return z;
    1657             :   }
    1658             : }
    1659             : 
    1660             : GEN
    1661      198061 : Qp_log(GEN x)
    1662             : {
    1663      198061 :   pari_sp av = avma;
    1664      198061 :   GEN y, p = gel(x,2), a = gel(x,4);
    1665      198061 :   long e = precp(x);
    1666             : 
    1667      198061 :   if (!signe(a)) pari_err_DOMAIN("Qp_log", "argument", "=", gen_0, x);
    1668      198040 :   if (absequaliu(p,2) || equali1(modii(a, p)))
    1669       75515 :     y = Zp_log(a, p, e);
    1670             :   else
    1671             :   { /* compute log(x^(p-1)) / (p-1) */
    1672      122525 :     GEN q = gel(x,3), t = subiu(p, 1);
    1673      122525 :     a = Fp_pow(a, t, q);
    1674      122525 :     y = Fp_mul(Zp_log(a, p, e), diviiexact(subsi(1, q), t), q);
    1675             :   }
    1676      198040 :   return gerepileupto(av, Z_to_padic(y, p, e));
    1677             : }
    1678             : 
    1679             : static GEN Qp_exp_safe(GEN x);
    1680             : 
    1681             : /*compute the p^e th root of x p-adic, assume x != 0 */
    1682             : static GEN
    1683         854 : Qp_sqrtn_ram(GEN x, long e)
    1684             : {
    1685         854 :   pari_sp ltop=avma;
    1686         854 :   GEN a, p = gel(x,2), n = powiu(p,e);
    1687         854 :   long v = valp(x), va;
    1688         854 :   if (v)
    1689             :   {
    1690             :     long z;
    1691         161 :     v = sdivsi_rem(v, n, &z);
    1692         161 :     if (z) return NULL;
    1693          91 :     x = leafcopy(x);
    1694          91 :     setvalp(x,0);
    1695             :   }
    1696             :   /*If p = 2, -1 is a root of 1 in U1: need extra check*/
    1697         784 :   if (absequaliu(p, 2) && mod8(gel(x,4)) != 1) return NULL;
    1698         749 :   a = Qp_log(x);
    1699         749 :   va = valp(a) - e;
    1700         749 :   if (va <= 0)
    1701             :   {
    1702         287 :     if (signe(gel(a,4))) return NULL;
    1703             :     /* all accuracy lost */
    1704         119 :     a = cvtop(remii(gel(x,4),p), p, 1);
    1705             :   }
    1706             :   else
    1707             :   {
    1708         462 :     setvalp(a, va); /* divide by p^e */
    1709         462 :     a = Qp_exp_safe(a);
    1710         462 :     if (!a) return NULL;
    1711             :     /* n=p^e and a^n=z*x where z is a (p-1)th-root of 1.
    1712             :      * Since z^n=z, we have (a/z)^n = x. */
    1713         462 :     a = gdiv(x, powgi(a,subiu(n,1))); /* = a/z = x/a^(n-1)*/
    1714         462 :     if (v) setvalp(a,v);
    1715             :   }
    1716         581 :   return gerepileupto(ltop,a);
    1717             : }
    1718             : 
    1719             : /*compute the nth root of x p-adic p prime with n*/
    1720             : static GEN
    1721        2037 : Qp_sqrtn_unram(GEN x, GEN n, GEN *zetan)
    1722             : {
    1723             :   pari_sp av;
    1724        2037 :   GEN Z, a, r, p = gel(x,2);
    1725        2037 :   long v = valp(x);
    1726        2037 :   if (v)
    1727             :   {
    1728             :     long z;
    1729          84 :     v = sdivsi_rem(v,n,&z);
    1730          84 :     if (z) return NULL;
    1731             :   }
    1732        2030 :   r = cgetp(x); setvalp(r,v);
    1733        2030 :   Z = NULL; /* -Wall */
    1734        2030 :   if (zetan) Z = cgetp(x);
    1735        2030 :   av = avma; a = Fp_sqrtn(gel(x,4), n, p, zetan);
    1736        2030 :   if (!a) return NULL;
    1737        2016 :   affii(Zp_sqrtnlift(gel(x,4), n, a, p, precp(x)), gel(r,4));
    1738        2016 :   if (zetan)
    1739             :   {
    1740          14 :     affii(Zp_sqrtnlift(gen_1, n, *zetan, p, precp(x)), gel(Z,4));
    1741          14 :     *zetan = Z;
    1742             :   }
    1743        2016 :   return gc_const(av,r);
    1744             : }
    1745             : 
    1746             : GEN
    1747        2604 : Qp_sqrtn(GEN x, GEN n, GEN *zetan)
    1748             : {
    1749             :   pari_sp av, tetpil;
    1750             :   GEN q, p;
    1751             :   long e;
    1752        2604 :   if (absequaliu(n, 2))
    1753             :   {
    1754          70 :     if (zetan) *zetan = gen_m1;
    1755          70 :     if (signe(n) < 0) x = ginv(x);
    1756          63 :     return Qp_sqrt(x);
    1757             :   }
    1758        2534 :   av = avma; p = gel(x,2);
    1759        2534 :   if (!signe(gel(x,4)))
    1760             :   {
    1761         203 :     if (signe(n) < 0) pari_err_INV("Qp_sqrtn", x);
    1762         203 :     q = divii(addis(n, valp(x)-1), n);
    1763         203 :     if (zetan) *zetan = gen_1;
    1764         203 :     set_avma(av); return zeropadic(p, itos(q));
    1765             :   }
    1766             :   /* treat the ramified part using logarithms */
    1767        2331 :   e = Z_pvalrem(n, p, &q);
    1768        2331 :   if (e) { x = Qp_sqrtn_ram(x,e); if (!x) return NULL; }
    1769        2058 :   if (is_pm1(q))
    1770             :   { /* finished */
    1771          21 :     if (signe(q) < 0) x = ginv(x);
    1772          21 :     x = gerepileupto(av, x);
    1773          21 :     if (zetan)
    1774          28 :       *zetan = (e && absequaliu(p, 2))? gen_m1 /*-1 in Q_2*/
    1775          28 :                                    : gen_1;
    1776          21 :     return x;
    1777             :   }
    1778        2037 :   tetpil = avma;
    1779             :   /* use hensel lift for unramified case */
    1780        2037 :   x = Qp_sqrtn_unram(x, q, zetan);
    1781        2037 :   if (!x) return NULL;
    1782        2016 :   if (zetan)
    1783             :   {
    1784             :     GEN *gptr[2];
    1785          14 :     if (e && absequaliu(p, 2))/*-1 in Q_2*/
    1786             :     {
    1787           7 :       tetpil = avma; x = gcopy(x); *zetan = gneg(*zetan);
    1788             :     }
    1789          14 :     gptr[0] = &x; gptr[1] = zetan;
    1790          14 :     gerepilemanysp(av,tetpil,gptr,2);
    1791          14 :     return x;
    1792             :   }
    1793        2002 :   return gerepile(av,tetpil,x);
    1794             : }
    1795             : 
    1796             : GEN
    1797       23626 : sqrtnint(GEN a, long n)
    1798             : {
    1799       23626 :   pari_sp av = avma;
    1800             :   GEN x, b, q;
    1801             :   long s, k, e;
    1802       23626 :   const ulong nm1 = n - 1;
    1803       23626 :   if (n == 2) return sqrtint(a);
    1804       19643 :   if (typ(a) != t_INT)
    1805             :   {
    1806          35 :     if (typ(a) == t_REAL)
    1807             :     {
    1808             :       long e;
    1809          14 :       switch(signe(a))
    1810             :       {
    1811           0 :         case 0: return gen_0;
    1812           7 :         case -1: pari_err_DOMAIN("sqrtnint", "argument", "<", gen_0,a);
    1813             :       }
    1814           7 :       e = expo(a); if (e < 0) return gen_0;
    1815           7 :       if (nbits2lg(e+1) > lg(a))
    1816           0 :         a = floorr(sqrtnr(a,n)); /* try to avoid precision loss in truncation */
    1817             :       else
    1818           7 :         a = sqrtnint(truncr(a),n);
    1819             :     }
    1820             :     else
    1821             :     {
    1822          21 :       GEN b = gfloor(a);
    1823          21 :       if (typ(b) != t_INT) pari_err_TYPE("sqrtint",a);
    1824          14 :       if (signe(b) < 0) pari_err_DOMAIN("sqrtnint", "argument", "<", gen_0,b);
    1825           7 :       a = sqrtnint(b, n);
    1826             :     }
    1827          14 :     return gerepileuptoint(av, a);
    1828             :   }
    1829       19608 :   if (n <= 0) pari_err_DOMAIN("sqrtnint", "n", "<=", gen_0, stoi(n));
    1830       19601 :   if (n == 1) return icopy(a);
    1831       17459 :   s = signe(a);
    1832       17459 :   if (s < 0) pari_err_DOMAIN("sqrtnint", "x", "<", gen_0, a);
    1833       17459 :   if (!s) return gen_0;
    1834       17382 :   if (lgefint(a) == 3) return utoi(usqrtn(itou(a), n));
    1835       11606 :   e = expi(a); k = e/(2*n);
    1836       11606 :   if (k == 0)
    1837             :   {
    1838             :     long flag;
    1839         291 :     if (n > e) return gc_const(av, gen_1);
    1840         291 :     flag = cmpii(a, powuu(3, n)); set_avma(av);
    1841         291 :     return (flag < 0) ? gen_2: stoi(3);
    1842             :   }
    1843       11315 :   if (e < n*BITS_IN_LONG - 1)
    1844             :   {
    1845             :     ulong xs, qs;
    1846        4181 :     b = itor(a, (2*e < n*BITS_IN_LONG)? DEFAULTPREC: MEDDEFAULTPREC);
    1847        4181 :     x = mpexp(divru(logr_abs(b), n));
    1848        4181 :     xs = itou(floorr(x)) + 1; /* >= a^(1/n) */
    1849             :     for(;;) {
    1850        8184 :       q = divii(a, powuu(xs, nm1));
    1851        8184 :       if (lgefint(q) > 3) break;
    1852        8177 :       qs = itou(q); if (qs >= xs) break;
    1853        4003 :       xs -= (xs - qs + nm1)/n;
    1854             :     }
    1855        4181 :     return utoi(xs);
    1856             :   }
    1857        7134 :   b = addui(1, shifti(a, -n*k));
    1858        7134 :   x = shifti(addui(1, sqrtnint(b, n)), k);
    1859        7134 :   q = divii(a, powiu(x, nm1));
    1860       15994 :   while (cmpii(q, x) < 0) /* a priori one iteration, no GC necessary */
    1861             :   {
    1862        8860 :     x = subii(x, divis(addui(nm1, subii(x, q)), n));
    1863        8860 :     q = divii(a, powiu(x, nm1));
    1864             :   }
    1865        7134 :   return gerepileuptoleaf(av, x);
    1866             : }
    1867             : 
    1868             : ulong
    1869        7659 : usqrtn(ulong a, ulong n)
    1870             : {
    1871             :   ulong x, s, q;
    1872        7659 :   const ulong nm1 = n - 1;
    1873        7659 :   if (!n) pari_err_DOMAIN("sqrtnint", "n", "=", gen_0, utoi(n));
    1874        7659 :   if (n == 1 || a == 0) return a;
    1875        7659 :   s = 1 + expu(a)/n; x = 1UL << s;
    1876        7659 :   q = (nm1*s >= BITS_IN_LONG)? 0: a >> (nm1*s);
    1877       19624 :   while (q < x) {
    1878             :     ulong X;
    1879       11965 :     x -= (x - q + nm1)/n;
    1880       11965 :     X = upowuu(x, nm1);
    1881       11965 :     q = X? a/X: 0;
    1882             :   }
    1883        7659 :   return x;
    1884             : }
    1885             : 
    1886             : static ulong
    1887     1663501 : cubic_prec_mask(long n)
    1888             : {
    1889     1663501 :   long a = n, i;
    1890     1663501 :   ulong mask = 0;
    1891     1663501 :   for(i = 1;; i++, mask *= 3)
    1892     7900089 :   {
    1893     9563590 :     long c = a%3;
    1894     9563590 :     if (c) mask += 3 - c;
    1895     9563590 :     a = (a+2)/3;
    1896     9563590 :     if (a==1) return mask + upowuu(3, i);
    1897             :   }
    1898             : }
    1899             : 
    1900             : /* cubic Newton iteration, |a|^(1/n), assuming a != 0 */
    1901             : GEN
    1902     2485354 : sqrtnr_abs(GEN a, long n)
    1903             : {
    1904             :   pari_sp av;
    1905             :   GEN x, b;
    1906             :   long eextra, eold, n1, n2, prec, B, v;
    1907             :   ulong mask;
    1908             : 
    1909     2485354 :   if (n == 1) return mpabs(a);
    1910     2484607 :   if (n == 2) return sqrtr_abs(a);
    1911             : 
    1912     2187080 :   prec = realprec(a); v = expo(a) / n; av = avma;
    1913     2187080 :   if (v) a = shiftr(a, -n*v);
    1914     2187092 :   b = rtor(a, DEFAULTPREC);
    1915     2187110 :   x = mpexp(divru(logr_abs(b), n));
    1916     2187114 :   if (prec == DEFAULTPREC)
    1917             :   {
    1918      562671 :     if (v) shiftr_inplace(x, v);
    1919      562674 :     return gerepileuptoleaf(av, x);
    1920             :   }
    1921     1624443 :   n1 = n+1;
    1922     1624443 :   n2 = 2*n;
    1923     1624443 :   B = prec2nbits(prec);
    1924     1624443 :   eextra = expu(n)-1;
    1925     1624443 :   mask = cubic_prec_mask(B + 63);
    1926     1624443 :   eold = 1;
    1927             :   for(;;)
    1928     6469090 :   { /* reach 64 */
    1929     8093533 :     long enew = eold * 3;
    1930     8093533 :     enew -= mask % 3;
    1931     8093533 :     if (enew > 64) break; /* back up one step */
    1932     6469090 :     mask /= 3;
    1933     6469090 :     eold = enew;
    1934             :   }
    1935             :   for(;;)
    1936     1261385 :   {
    1937     2885828 :     long pr, enew = eold * 3;
    1938             :     GEN y, z;
    1939     2885828 :     enew -= mask % 3;
    1940     2885828 :     mask /= 3;
    1941     2885828 :     pr = nbits2prec(enew + eextra);
    1942     2885828 :     b = rtor(a, pr); setsigne(b,1);
    1943     2885828 :     x = rtor(x, pr);
    1944     2885828 :     y = subrr(powru(x, n), b);
    1945     2885828 :     z = divrr(y, addrr(mulur(n1, y), mulur(n2, b)));
    1946     2885828 :     shiftr_inplace(z,1);
    1947     2885828 :     x = mulrr(x, subsr(1,z));
    1948     2885828 :     if (mask == 1)
    1949             :     {
    1950     1624443 :       if (v) shiftr_inplace(x, v);
    1951     1624443 :       return gerepileuptoleaf(av, gprec_wtrunc(x,prec));
    1952             :     }
    1953     1261385 :     eold = enew;
    1954             :   }
    1955             : }
    1956             : 
    1957             : static void
    1958       56044 : shiftc_inplace(GEN z, long d)
    1959             : {
    1960       56044 :   shiftr_inplace(gel(z,1), d);
    1961       56044 :   shiftr_inplace(gel(z,2), d);
    1962       56044 : }
    1963             : 
    1964             : /* exp(2*Pi*I/n), same iteration as sqrtnr_abs, different initial point */
    1965             : static GEN
    1966      524342 : sqrtnof1(ulong n, long prec)
    1967             : {
    1968             :   pari_sp av;
    1969             :   GEN x;
    1970             :   long eold, n1, n2, B;
    1971             :   ulong mask;
    1972             : 
    1973      524342 :   B = prec2nbits(prec);
    1974      524343 :   n1 = n+1;
    1975      524343 :   n2 = 2*n; av = avma;
    1976             : 
    1977      524343 :   x = expIr(divru(Pi2n(1, LOWDEFAULTPREC), n));
    1978      524341 :   if (prec == LOWDEFAULTPREC) return gerepileupto(av, x);
    1979       39058 :   mask = cubic_prec_mask(B + BITS_IN_LONG-1);
    1980       39058 :   eold = 1;
    1981             :   for(;;)
    1982      152628 :   { /* reach BITS_IN_LONG */
    1983      191686 :     long enew = eold * 3;
    1984      191686 :     enew -= mask % 3;
    1985      191686 :     if (enew > BITS_IN_LONG) break; /* back up one step */
    1986      152628 :     mask /= 3;
    1987      152628 :     eold = enew;
    1988             :   }
    1989             :   for(;;)
    1990       16986 :   {
    1991       56044 :     long pr, enew = eold * 3;
    1992             :     GEN y, z;
    1993       56044 :     enew -= mask % 3;
    1994       56044 :     mask /= 3;
    1995       56044 :     pr = nbits2prec(enew);
    1996       56044 :     x = cxtofp(x, pr);
    1997       56044 :     y = gsub(gpowgs(x, n), gen_1);
    1998       56044 :     z = gdiv(y, gaddgs(gmulsg(n1, y), n2));
    1999       56044 :     shiftc_inplace(z,1);
    2000       56044 :     x = gmul(x, gsubsg(1, z));
    2001       56044 :     if (mask == 1) return gerepilecopy(av, gprec_w(x,prec));
    2002       16986 :     eold = enew;
    2003             :   }
    2004             : }
    2005             : 
    2006             : /* exp(2iPi/d) */
    2007             : GEN
    2008     1266543 : rootsof1u_cx(ulong n, long prec)
    2009             : {
    2010     1266543 :   switch(n)
    2011             :   {
    2012       13006 :     case 1: return gen_1;
    2013        2954 :     case 2: return gen_m1;
    2014      262015 :     case 4: return gen_I();
    2015       10567 :     case 3: case 6: case 12:
    2016             :     {
    2017       10567 :       pari_sp av = avma;
    2018       10567 :       GEN a = (n == 3)? mkfrac(gen_m1,gen_2): ghalf;
    2019       10567 :       GEN sq3 = sqrtr_abs(utor(3, prec));
    2020       10567 :       shiftr_inplace(sq3, -1);
    2021       10567 :       a = (n == 12)? mkcomplex(sq3, a): mkcomplex(a, sq3);
    2022       10567 :       return gerepilecopy(av, a);
    2023             :     }
    2024      453672 :     case 8:
    2025             :     {
    2026      453672 :       pari_sp av = avma;
    2027      453672 :       GEN sq2 = sqrtr_abs(utor(2, prec));
    2028      453650 :       shiftr_inplace(sq2,-1);
    2029      453649 :       return gerepilecopy(av, mkcomplex(sq2, sq2));
    2030             :     }
    2031             :   }
    2032      524329 :   return sqrtnof1(n, prec);
    2033             : }
    2034             : /* e(a/b) */
    2035             : GEN
    2036       14154 : rootsof1q_cx(long a, long b, long prec)
    2037             : {
    2038       14154 :   long g = cgcd(a,b);
    2039             :   GEN z;
    2040       14154 :   if (g != 1) { a /= g; b /= g; }
    2041       14154 :   if (b < 0) { b = -b; a = -a; }
    2042       14154 :   z = rootsof1u_cx(b, prec);
    2043       14154 :   if (a < 0) { z = conj_i(z); a = -a; }
    2044       14154 :   return gpowgs(z, a);
    2045             : }
    2046             : 
    2047             : /* initializes powers of e(a/b) */
    2048             : GEN
    2049       14987 : rootsof1powinit(long a, long b, long prec)
    2050             : {
    2051       14987 :   long g = cgcd(a,b);
    2052       14987 :   if (g != 1) { a /= g; b /= g; }
    2053       14987 :   if (b < 0) { b = -b; a = -a; }
    2054       14987 :   a %= b; if (a < 0) a += b;
    2055       14987 :   return mkvec2(grootsof1(b,prec), mkvecsmall2(a,b));
    2056             : }
    2057             : /* T = rootsof1powinit(a,b); return  e(a/b)^c */
    2058             : GEN
    2059    12516441 : rootsof1pow(GEN T, long c)
    2060             : {
    2061    12516441 :   GEN vz = gel(T,1), ab = gel(T,2);
    2062    12516441 :   long a = ab[1], b = ab[2]; /* a >= 0, b > 0 */
    2063    12516441 :   c %= b; if (c < 0) c += b;
    2064    12516441 :   a = Fl_mul(a, c, b);
    2065    12516441 :   return gel(vz, a + 1);
    2066             : }
    2067             : 
    2068             : /* exp(2iPi/d), assume d a t_INT */
    2069             : GEN
    2070        4788 : rootsof1_cx(GEN d, long prec)
    2071             : {
    2072        4788 :   if (lgefint(d) == 3) return rootsof1u_cx((ulong)d[2], prec);
    2073           0 :   return expIr(divri(Pi2n(1,prec), d));
    2074             : }
    2075             : 
    2076             : GEN
    2077       12741 : gsqrtn(GEN x, GEN n, GEN *zetan, long prec)
    2078             : {
    2079             :   long i, lx, tx;
    2080             :   pari_sp av;
    2081             :   GEN y, z;
    2082       12741 :   if (typ(n)!=t_INT) pari_err_TYPE("sqrtn",n);
    2083       12741 :   if (!signe(n)) pari_err_DOMAIN("sqrtn", "n", "=", gen_0, n);
    2084       12741 :   if (is_pm1(n))
    2085             :   {
    2086          70 :     if (zetan) *zetan = gen_1;
    2087          70 :     return (signe(n) > 0)? gcopy(x): ginv(x);
    2088             :   }
    2089       12671 :   if (zetan) *zetan = gen_0;
    2090       12671 :   tx = typ(x);
    2091       12671 :   if (is_matvec_t(tx))
    2092             :   {
    2093           7 :     y = cgetg_copy(x, &lx);
    2094          21 :     for (i=1; i<lx; i++) gel(y,i) = gsqrtn(gel(x,i),n,NULL,prec);
    2095           7 :     return y;
    2096             :   }
    2097       12664 :   av = avma;
    2098       12664 :   switch(tx)
    2099             :   {
    2100         182 :   case t_INTMOD:
    2101             :     {
    2102         182 :       GEN p = gel(x,1), s;
    2103         182 :       z = gen_0;
    2104         182 :       y = cgetg(3,t_INTMOD);  gel(y,1) = icopy(p);
    2105         182 :       if (zetan) { z = cgetg(3,t_INTMOD); gel(z,1) = gel(y,1); }
    2106         182 :       s = Fp_sqrtn(gel(x,2),n,p,zetan);
    2107         161 :       if (!s) {
    2108          35 :         if (zetan) return gc_const(av,gen_0);
    2109          28 :         if (!BPSW_psp(p)) pari_err_PRIME("sqrtn [modulus]",p);
    2110          14 :         pari_err_SQRTN("gsqrtn",x);
    2111             :       }
    2112         126 :       gel(y,2) = s;
    2113         126 :       if (zetan) { gel(z,2) = *zetan; *zetan = z; }
    2114         126 :       return y;
    2115             :     }
    2116             : 
    2117          56 :   case t_PADIC:
    2118          56 :     y = Qp_sqrtn(x,n,zetan);
    2119          49 :     if (!y) {
    2120           7 :       if (zetan) return gen_0;
    2121           7 :       pari_err_SQRTN("gsqrtn",x);
    2122             :     }
    2123          42 :     return y;
    2124             : 
    2125         196 :   case t_FFELT: return FF_sqrtn(x,n,zetan);
    2126             : 
    2127       11775 :   case t_INT: case t_FRAC: case t_REAL: case t_COMPLEX:
    2128       11775 :     i = precision(x); if (i) prec = i;
    2129       11775 :     if (isint1(x))
    2130           7 :       y = real_1(prec);
    2131       11768 :     else if (gequal0(x))
    2132             :     {
    2133             :       long b;
    2134          21 :       if (signe(n) < 0) pari_err_INV("gsqrtn",x);
    2135          21 :       if (isinexactreal(x))
    2136          14 :         b = sdivsi(gexpo(x), n);
    2137             :       else
    2138           7 :         b = -prec2nbits(prec);
    2139          21 :       if (typ(x) == t_COMPLEX)
    2140             :       {
    2141           7 :         y = cgetg(3,t_COMPLEX);
    2142           7 :         gel(y,1) = gel(y,2) = real_0_bit(b);
    2143             :       }
    2144             :       else
    2145          14 :         y = real_0_bit(b);
    2146             :     }
    2147             :     else
    2148             :     {
    2149       11747 :       long nn = itos_or_0(n);
    2150       11747 :       if (tx == t_INT) { x = itor(x,prec); tx = t_REAL; }
    2151       11747 :       if (nn > 0 && tx == t_REAL && signe(x) > 0)
    2152        2081 :         y = sqrtnr(x, nn);
    2153             :       else
    2154        9666 :         y = gexp(gdiv(glog(x,prec), n), prec);
    2155       11747 :       y = gerepileupto(av, y);
    2156             :     }
    2157       11775 :     if (zetan) *zetan = rootsof1_cx(n, prec);
    2158       11775 :     return y;
    2159             : 
    2160           7 :   case t_QUAD:
    2161           7 :     return gsqrtn(quadtofp(x, prec), n, zetan, prec);
    2162             : 
    2163         448 :   default:
    2164         448 :     av = avma; if (!(y = toser_i(x))) break;
    2165         448 :     return gerepileupto(av, ser_powfrac(y, ginv(n), prec));
    2166             :   }
    2167           0 :   pari_err_TYPE("sqrtn",x);
    2168             :   return NULL;/* LCOV_EXCL_LINE */
    2169             : }
    2170             : 
    2171             : /********************************************************************/
    2172             : /**                                                                **/
    2173             : /**                             EXP(X) - 1                         **/
    2174             : /**                                                                **/
    2175             : /********************************************************************/
    2176             : /* exp(|x|) - 1, assume x != 0.
    2177             :  * For efficiency, x should be reduced mod log(2): if so, we have a < 0 */
    2178             : GEN
    2179    15700643 : exp1r_abs(GEN x)
    2180             : {
    2181    15700643 :   long l = realprec(x), a = expo(x), b = prec2nbits(l), L, i, n, m, B;
    2182             :   GEN y, p2, X;
    2183             :   pari_sp av;
    2184             :   double d;
    2185             : 
    2186    15700419 :   if (b + a <= 0) return mpabs(x);
    2187             : 
    2188    15686673 :   y = cgetr(l); av = avma;
    2189    15686891 :   B = b/3 + BITS_IN_LONG + (BITS_IN_LONG*BITS_IN_LONG)/ b;
    2190    15686891 :   d = a/2.; m = (long)(d + sqrt(d*d + B)); /* >= 0 */
    2191    15686891 :   if (m < (-a) * 0.1) m = 0; /* not worth it */
    2192    15686891 :   L = l + nbits2extraprec(m);
    2193             :  /* Multiplication is quadratic in this range (l is small, otherwise we
    2194             :   * use logAGM + Newton). Set Y = 2^(-e-a) x, compute truncated series
    2195             :   * sum_{k <= n} Y^k/k!: this costs roughly
    2196             :   *    m b^2 + sum_{k <= n} (k e + BITS_IN_LONG)^2
    2197             :   * bit operations with n ~ b/e, |x| <  2^(1+a), |Y| < 2^(1-e) , m = e+a and
    2198             :   * b bits of accuracy needed, so
    2199             :   *    B := (b / 3 + BITS_IN_LONG + BITS_IN_LONG^2 / b) ~ m(m-a)
    2200             :   * we want b ~ 3 m (m-a) or m~b+a hence
    2201             :   *     m = min( a/2 + sqrt(a^2/4 + B),  b + a )
    2202             :   * NB: e ~ (b/3)^(1/2) as b -> oo
    2203             :   *
    2204             :   * Truncate the sum at k = n (>= 1), the remainder is
    2205             :   *   sum_{k >= n+1} Y^k / k! < Y^(n+1) / (n+1)! (1-Y) < Y^(n+1) / n!
    2206             :   * We want Y^(n+1) / n! <= Y 2^-b, hence -n log_2 |Y| + log_2 n! >= b
    2207             :   *   log n! ~ (n + 1/2) log(n+1) - (n+1) + log(2Pi)/2,
    2208             :   * error bounded by 1/6(n+1) <= 1/12. Finally, we want
    2209             :   * n (-1/log(2) -log_2 |Y| + log_2(n+1)) >= b  */
    2210    15687349 :   b += m;
    2211    15687349 :   d = m-dbllog2(x)-1/M_LN2; /* ~ -log_2 Y - 1/log(2) */
    2212    15688204 :   n = (long)(b / d);
    2213    15688204 :   if (n > 1)
    2214    14944937 :     n = (long)(b / (d + log2((double)n+1))); /* log~constant in small ranges */
    2215    35514755 :   while (n*(d+log2((double)n+1)) < b) n++; /* expect few corrections */
    2216             : 
    2217    15688204 :   X = rtor(x,L); shiftr_inplace(X, -m); setsigne(X, 1);
    2218    15688309 :   if (n == 1) p2 = X;
    2219             :   else
    2220             :   {
    2221    15688309 :     long s = 0, l1 = nbits2prec((long)(d + n + 16));
    2222    15688228 :     GEN unr = real_1(L);
    2223             :     pari_sp av2;
    2224             : 
    2225    15688270 :     p2 = cgetr(L); av2 = avma;
    2226   201510792 :     for (i=n; i>=2; i--, set_avma(av2))
    2227             :     { /* compute X^(n-1)/n! + ... + X/2 + 1 */
    2228             :       GEN p1, p3;
    2229   185898361 :       setprec(X,l1); p3 = divru(X,i);
    2230   186059182 :       l1 += dvmdsBIL(s - expo(p3), &s); if (l1>L) l1=L;
    2231   186108518 :       setprec(unr,l1); p1 = addrr_sign(unr,1, i == n? p3: mulrr(p3,p2),1);
    2232   185665130 :       setprec(p2,l1); affrr(p1,p2); /* p2 <- 1 + (X/i)*p2 */
    2233             :     }
    2234    15686932 :     setprec(X,L); p2 = mulrr(X,p2);
    2235             :   }
    2236             : 
    2237   158171657 :   for (i=1; i<=m; i++)
    2238             :   {
    2239   142483152 :     if (realprec(p2) > L) setprec(p2,L);
    2240   142483152 :     p2 = mulrr(p2, addsr(2,p2));
    2241             :   }
    2242    15688505 :   affrr_fixlg(p2,y); return gc_const(av,y);
    2243             : }
    2244             : 
    2245             : GEN
    2246       10773 : mpexpm1(GEN x)
    2247             : {
    2248       10773 :   const long s = 6;
    2249       10773 :   long l, sx = signe(x);
    2250             :   GEN y, z;
    2251             :   pari_sp av;
    2252       10773 :   if (!sx) return real_0_bit(expo(x));
    2253       10766 :   l = realprec(x);
    2254       10766 :   if (l > maxss(EXPNEWTON_LIMIT, (1L<<s) + 2))
    2255             :   {
    2256           6 :     long e = expo(x);
    2257           6 :     if (e < 0) x = rtor(x, l + nbits2extraprec(-e));
    2258           6 :     return subrs(mpexp(x), 1);
    2259             :   }
    2260       10760 :   if (sx > 0) return exp1r_abs(x);
    2261             :   /* compute exp(x) * (1 - exp(-x)) */
    2262        4886 :   av = avma; y = exp1r_abs(x);
    2263        4886 :   z = addsr(1, y); setsigne(z, -1);
    2264        4886 :   return gerepileupto(av, divrr(y, z));
    2265             : }
    2266             : 
    2267             : static GEN serexp(GEN x, long prec);
    2268             : GEN
    2269       12582 : gexpm1(GEN x, long prec)
    2270             : {
    2271       12582 :   switch(typ(x))
    2272             :   {
    2273        4550 :     case t_REAL: return mpexpm1(x);
    2274        5932 :     case t_COMPLEX: return cxexpm1(x,prec);
    2275          14 :     case t_PADIC: return gsubgs(Qp_exp(x), 1);
    2276        2086 :     default:
    2277             :     {
    2278        2086 :       pari_sp av = avma;
    2279             :       long ey;
    2280             :       GEN y;
    2281        2086 :       if (!(y = toser_i(x))) break;
    2282        2065 :       ey = valser(y);
    2283        2065 :       if (ey < 0) pari_err_DOMAIN("expm1","valuation", "<", gen_0, x);
    2284        2065 :       if (gequal0(y)) return gcopy(y);
    2285        2058 :       if (ey)
    2286         504 :         return gerepileupto(av, gsubgs(serexp(y,prec), 1));
    2287             :       else
    2288             :       {
    2289        1554 :         GEN e1 = gexpm1(gel(y,2), prec), e = gaddgs(e1,1);
    2290        1554 :         y = gmul(e, serexp(serchop0(y),prec));
    2291        1554 :         gel(y,2) = e1;
    2292        1554 :         return gerepilecopy(av, y);
    2293             :       }
    2294             :     }
    2295             :   }
    2296          21 :   return trans_eval("expm1",gexpm1,x,prec);
    2297             : }
    2298             : /********************************************************************/
    2299             : /**                                                                **/
    2300             : /**                             EXP(X)                             **/
    2301             : /**                                                                **/
    2302             : /********************************************************************/
    2303             : static GEN
    2304    15638686 : mpexp_basecase(GEN x)
    2305             : {
    2306    15638686 :   pari_sp av = avma;
    2307    15638686 :   long sh, l = realprec(x);
    2308             :   GEN y, z;
    2309             : 
    2310    15638686 :   y = modlog2(x, &sh);
    2311    15637768 :   if (!y) { set_avma(av); return real2n(sh, l); }
    2312    15637768 :   z = addsr(1, exp1r_abs(y));
    2313    15637507 :   if (signe(y) < 0) z = invr(z);
    2314    15637581 :   if (sh) {
    2315    12649451 :     shiftr_inplace(z, sh);
    2316    12649314 :     if (realprec(z) > l) z = rtor(z, l); /* spurious precision increase */
    2317             :   }
    2318             : #ifdef DEBUG
    2319             : {
    2320             :   GEN t = mplog(z), u = divrr(subrr(x, t),x);
    2321             :   if (signe(u) && expo(u) > 5-prec2nbits(minss(l,realprec(t))))
    2322             :     pari_err_BUG("exp");
    2323             : }
    2324             : #endif
    2325    15638203 :   return gerepileuptoleaf(av, z); /* NOT affrr, precision often increases */
    2326             : }
    2327             : 
    2328             : GEN
    2329    15710750 : mpexp(GEN x)
    2330             : {
    2331    15710750 :   const long s = 6; /*Initial steps using basecase*/
    2332    15710750 :   long i, p, l = realprec(x), sh;
    2333             :   GEN a, t, z;
    2334             :   ulong mask;
    2335             : 
    2336    15710750 :   if (l <= maxss(EXPNEWTON_LIMIT, (1L<<s) + 2))
    2337             :   {
    2338    15710841 :     if (!signe(x)) return mpexp0(x);
    2339    15638654 :     return mpexp_basecase(x);
    2340             :   }
    2341          11 :   z = cgetr(l); /* room for result */
    2342          13 :   x = modlog2(x, &sh);
    2343          13 :   if (!x) { set_avma((pari_sp)(z+lg(z))); return real2n(sh, l); }
    2344          13 :   constpi(l); /* precompute for later logr_abs() */
    2345          13 :   mask = quadratic_prec_mask(prec2nbits(l)+BITS_IN_LONG);
    2346         168 :   for(i=0, p=1; i<s+TWOPOTBITS_IN_LONG; i++) { p <<= 1; if (mask & 1) p-=1; mask >>= 1; }
    2347          13 :   a = mpexp_basecase(rtor(x, nbits2prec(p)));
    2348          13 :   x = addrs(x,1);
    2349          13 :   if (realprec(x) < l+EXTRAPREC64) x = rtor(x, l+EXTRAPREC64);
    2350          13 :   a = rtor(a, l+EXTRAPREC64); /*append 0s */
    2351          13 :   t = NULL;
    2352             :   for(;;)
    2353             :   {
    2354          14 :     p <<= 1; if (mask & 1) p--;
    2355          14 :     mask >>= 1;
    2356          14 :     setprec(x, nbits2prec(p));
    2357          14 :     setprec(a, nbits2prec(p));
    2358          14 :     t = mulrr(a, subrr(x, logr_abs(a))); /* a (x - log(a)) */
    2359          14 :     if (mask == 1) break;
    2360           1 :     affrr(t, a); set_avma((pari_sp)a);
    2361             :   }
    2362          13 :   affrr(t,z);
    2363          13 :   if (sh) shiftr_inplace(z, sh);
    2364          13 :   return gc_const((pari_sp)z, z);
    2365             : }
    2366             : 
    2367             : /* x != 0; k = ceil(tn / (te-1)), t = p-1 */
    2368             : long
    2369          84 : Qp_exp_prec(GEN x)
    2370             : {
    2371          84 :   long e = valp(x), n = precp(x);
    2372             :   ulong a, b, q, r, p, t;
    2373             : 
    2374          84 :   if (e < 1) return -1;
    2375          63 :   if (e > n) return 1;
    2376          63 :   p = itos_or_0(gel(x,2));
    2377          63 :   if (!p) return n / e + 1;
    2378          63 :   if (p == 2) return e < 2? -1: ceildivuu(n, e - 1);
    2379             :   /* n >= e > 0, n = qe + r */
    2380             :   /* tn = q (te-1) + rt + q = (q+1)(te-1) - t(e-r) + q + 1 */
    2381          63 :   t = p - 1;
    2382          63 :   if (e == 1) return n + ceildivuu(n, t - 1);
    2383           0 :   q = n / e;
    2384           0 :   r = n % e; /* k = q + 1 if rt + q < te */
    2385           0 :   a = umuluu_or_0(e - r, t); if (!a || a > q) return q + 1;
    2386           0 :   b = umuluu_or_0(e, t); if (!b) return q + 2;
    2387           0 :   return q + 1 + ceildivuu(q + 1 - a, b - 1);
    2388             : }
    2389             : 
    2390             : static GEN
    2391      108762 : Qp_exp_safe(GEN x)
    2392             : {
    2393      108762 :   pari_sp av = avma;
    2394      108762 :   GEN p = gel(x,2), a = gel(x,4), z;
    2395      108762 :   long d = precp(x), v = valp(x), e = d+v;
    2396      108762 :   if (gequal0(x)) return gaddgs(x,1);
    2397      107257 :   if (v < (equaliu(p,2)? 2:1)) return NULL;
    2398      107250 :   z = Zp_exp(mulii(a,powiu(p,v)), p, e);
    2399      107251 :   return gerepileupto(av, Z_to_padic(z, p, e));
    2400             : }
    2401             : 
    2402             : GEN
    2403      108300 : Qp_exp(GEN x)
    2404             : {
    2405      108300 :   GEN y = Qp_exp_safe(x);
    2406      108300 :   if (!y) pari_err_DOMAIN("gexp(t_PADIC)","argument","",gen_0,x);
    2407      108293 :   return y;
    2408             : }
    2409             : 
    2410             : static GEN
    2411          49 : cos_p(GEN x)
    2412             : {
    2413             :   long k;
    2414             :   pari_sp av;
    2415             :   GEN x2, y;
    2416             : 
    2417          49 :   if (gequal0(x)) return gaddgs(x,1);
    2418          28 :   k = Qp_exp_prec(x);
    2419          28 :   if (k < 0) return NULL;
    2420          21 :   av = avma; x2 = gsqr(x);
    2421          21 :   if (k & 1) k--;
    2422         105 :   for (y=gen_1; k; k-=2)
    2423             :   {
    2424          84 :     GEN t = gdiv(gmul(y,x2), muluu(k, k-1));
    2425          84 :     y = gsubsg(1, t);
    2426             :   }
    2427          21 :   return gerepileupto(av, y);
    2428             : }
    2429             : static GEN
    2430          63 : sin_p(GEN x)
    2431             : {
    2432             :   long k;
    2433             :   pari_sp av;
    2434             :   GEN x2, y;
    2435             : 
    2436          63 :   if (gequal0(x)) return gcopy(x);
    2437          42 :   k = Qp_exp_prec(x);
    2438          42 :   if (k < 0) return NULL;
    2439          28 :   av = avma; x2 = gsqr(x);
    2440          28 :   if (k & 1) k--;
    2441         133 :   for (y=gen_1; k; k-=2)
    2442             :   {
    2443         105 :     GEN t = gdiv(gmul(y,x2), muluu(k, k+1));
    2444         105 :     y = gsubsg(1, t);
    2445             :   }
    2446          28 :   return gerepileupto(av, gmul(y, x));
    2447             : }
    2448             : 
    2449             : static GEN
    2450     3289401 : cxexp(GEN x, long prec)
    2451             : {
    2452     3289401 :   GEN r, p1, p2, y = cgetg(3,t_COMPLEX);
    2453     3289380 :   pari_sp av = avma, tetpil;
    2454             :   long l;
    2455     3289380 :   l = precision(x); if (l > prec) prec = l;
    2456     3289444 :   if (gequal0(gel(x,1)))
    2457             :   {
    2458      346157 :     gsincos(gel(x,2),&gel(y,2),&gel(y,1),prec);
    2459      346162 :     return y;
    2460             :   }
    2461     2943526 :   r = gexp(gel(x,1),prec);
    2462     2943630 :   gsincos(gel(x,2),&p2,&p1,prec);
    2463     2943814 :   tetpil = avma;
    2464     2943814 :   gel(y,1) = gmul(r,p1);
    2465     2943638 :   gel(y,2) = gmul(r,p2);
    2466     2943592 :   gerepilecoeffssp(av,tetpil,y+1,2);
    2467     2944038 :   return y;
    2468             : }
    2469             : 
    2470             : /* given a t_SER x^v s(x), with s(0) != 0, return x^v(s - s(0)), shallow */
    2471             : GEN
    2472       36547 : serchop0(GEN s)
    2473             : {
    2474       36547 :   long i, l = lg(s);
    2475             :   GEN y;
    2476       36547 :   if (l == 2) return s;
    2477       36547 :   if (l == 3 && isexactzero(gel(s,2))) return s;
    2478       36547 :   y = cgetg(l, t_SER); y[1] = s[1];
    2479      163779 :   gel(y,2) = gen_0; for (i=3; i <l; i++) gel(y,i) = gel(s,i);
    2480       36547 :   return normalizeser(y);
    2481             : }
    2482             : 
    2483             : GEN
    2484          42 : serchop_i(GEN s, long n)
    2485             : {
    2486          42 :   long i, m, l = lg(s);
    2487             :   GEN y;
    2488          42 :   if (l == 2 || (l == 3 && isexactzero(gel(s,2))))
    2489             :   {
    2490          14 :     if (valser(s) < n) { s = shallowcopy(s); setvalser(s,n); }
    2491          14 :     return s;
    2492             :   }
    2493          28 :   m = n - valser(s); if (m < 0) return s;
    2494          21 :   if (l-m <= 2) return zeroser(varn(s), n);
    2495          14 :   y = cgetg(l-m, t_SER); y[1] = s[1]; setvalser(y, valser(y)+m);
    2496          42 :   for (i=m+2; i < l; i++) gel(y,i-m) = gel(s,i);
    2497          14 :   return normalizeser(y);
    2498             : }
    2499             : GEN
    2500          42 : serchop(GEN s, long n)
    2501             : {
    2502          42 :   pari_sp av = avma;
    2503          42 :   if (typ(s) != t_SER) pari_err_TYPE("serchop",s);
    2504          42 :   return gerepilecopy(av, serchop_i(s,n));
    2505             : }
    2506             : 
    2507             : static GEN
    2508       77231 : serexp(GEN x, long prec)
    2509             : {
    2510       77231 :   long i, j, lx, ly, mi, e = valser(x);
    2511             :   GEN y, xd, yd;
    2512             :   pari_sp av;
    2513             : 
    2514       77231 :   if (e < 0) pari_err_DOMAIN("exp","valuation", "<", gen_0, x);
    2515       77224 :   if (gequal0(x)) return gaddsg(1,x);
    2516       66605 :   lx = lg(x);
    2517       66605 :   if (e)
    2518             :   {
    2519             :     GEN X;
    2520       52619 :     ly = lx+e; y = cgetg(ly,t_SER);
    2521      558061 :     mi = lx-1; while (mi>=3 && isrationalzero(gel(x,mi))) mi--;
    2522       52619 :     mi += e-2;
    2523       52619 :     y[1] = evalsigne(1) | _evalvalser(0) | evalvarn(varn(x));
    2524             :     /* zd[i] = coefficient of X^i in z */
    2525       52619 :     xd = x+2-e; yd = y+2; ly -= 2;
    2526       52619 :     X = gel(xd,e); if (e != 1) X = gmulgu(X, e); /* left on stack */
    2527       52619 :     X = isint1(X)? NULL: X;
    2528       52619 :     gel(yd,0) = gen_1;
    2529       52990 :     for (i = 1; i < e; i++) gel(yd,i) = gen_0;
    2530      652526 :     for (     ; i < ly; i++)
    2531             :     {
    2532      599907 :       GEN t = gel(yd,i-e);
    2533      599907 :       long J = minss(i, mi);
    2534      599907 :       av = avma; if (X) t = gmul(t, X);
    2535     2569707 :       for (j = e + 1; j <= J; j++)
    2536     1969800 :         t = gadd(t, gmulgu(gmul(gel(xd,j),gel(yd,i-j)), j));
    2537      599907 :       gel(yd,i) = gerepileupto(av, gdivgu(t, i));
    2538             :     }
    2539       52619 :     return y;
    2540             :   }
    2541       13986 :   av = avma;
    2542       13986 :   return gerepileupto(av, gmul(gexp(gel(x,2),prec), serexp(serchop0(x),prec)));
    2543             : }
    2544             : 
    2545             : static GEN
    2546     1468516 : expQ(GEN x, long prec)
    2547             : {
    2548     1468516 :   GEN p, q, z, z0 = NULL;
    2549             :   pari_sp av;
    2550     1468516 :   long n, nmax, s, e, b = prec2nbits(prec);
    2551             :   double ex;
    2552             :   struct abpq_res R;
    2553             :   struct abpq S;
    2554             : 
    2555     1468514 :   if (typ(x) == t_INT)
    2556             :   {
    2557       24624 :     if (!signe(x)) return real_1(prec);
    2558       24602 :     p = x; q = gen_1;
    2559       24602 :     e = expi(p);
    2560       24601 :     if (e > b) return mpexp(itor(x, prec));
    2561             :   }
    2562             :   else
    2563             :   {
    2564     1443890 :     long ep, eq, B = usqrt(b) / 2;
    2565     1443890 :     p = gel(x,1); ep = expi(p);
    2566     1443890 :     q = gel(x,2); eq = expi(q);
    2567     1443890 :     if (ep > B || eq > B) return mpexp(fractor(x, prec));
    2568       14637 :     e = ep - eq;
    2569       14637 :     if (e < -3) prec += nbits2extraprec(-e); /* see addrr 'extend' rule */
    2570             :   }
    2571       39238 :   if (e > 2) { z0 = cgetr(prec); prec += EXTRAPREC64; b += BITS_IN_LONG; }
    2572       39238 :   z = cgetr(prec); av = avma;
    2573       39239 :   if (e > 0)
    2574             :   { /* simplify x/2^e = p / (q * 2^e) */
    2575        2478 :     long v = minss(e, vali(p));
    2576        2478 :     if (v) p = shifti(p, -v);
    2577        2478 :     if (e - v) q = shifti(q, e - v);
    2578             :   }
    2579       39239 :   s = signe(p);
    2580       39239 :   if (s < 0) p = negi(p);
    2581       39246 :   ex = exp2(dbllog2(x) - e) * 2.718281828; /* exp(1) * x / 2^e,  x / 2^e < 2 */
    2582       39245 :   nmax = (long)(1 + exp(dbllambertW0(M_LN2 * b / ex)) * ex);
    2583       39249 :   abpq_init(&S, nmax);
    2584       39266 :   S.a[0] = S.b[0] = S.p[0] = S.q[0] = gen_1;
    2585     3369845 :   for (n = 1; n <= nmax; n++)
    2586             :   {
    2587     3330608 :     S.a[n] = gen_1;
    2588     3330608 :     S.b[n] = gen_1;
    2589     3330608 :     S.p[n] = p;
    2590     3330608 :     S.q[n] = muliu(q, n);
    2591             :   }
    2592       39237 :   abpq_sum(&R, 0, nmax, &S);
    2593       39255 :   if (s > 0) rdiviiz(R.T, R.Q, z); else rdiviiz(R.Q, R.T, z);
    2594       39253 :   if (e > 0)
    2595             :   {
    2596       17136 :     q = z; while (e--) q = sqrr(q);
    2597        2478 :     if (z0) { affrr(q, z0); z = z0; } else affrr(q,z);
    2598             :   }
    2599       39253 :   return gc_const(av,z);
    2600             : }
    2601             : 
    2602             : GEN
    2603    14787579 : gexp(GEN x, long prec)
    2604             : {
    2605    14787579 :   switch(typ(x))
    2606             :   {
    2607     1468516 :     case t_INT: case t_FRAC: return expQ(x, prec);
    2608     8686903 :     case t_REAL: return mpexp(x);
    2609     3289421 :     case t_COMPLEX: return cxexp(x,prec);
    2610          70 :     case t_PADIC: return Qp_exp(x);
    2611     1342669 :     default:
    2612             :     {
    2613     1342669 :       pari_sp av = avma;
    2614             :       GEN y;
    2615     1342669 :       if (!(y = toser_i(x))) break;
    2616       61187 :       return gerepileupto(av, serexp(y,prec));
    2617             :     }
    2618             :   }
    2619     1282080 :   return trans_eval("exp",gexp,x,prec);
    2620             : }
    2621             : 
    2622             : /********************************************************************/
    2623             : /**                                                                **/
    2624             : /**                           AGM(X, Y)                            **/
    2625             : /**                                                                **/
    2626             : /********************************************************************/
    2627             : static int
    2628    18434909 : agmr_gap(GEN a, GEN b, long L)
    2629             : {
    2630    18434909 :   GEN d = subrr(b, a);
    2631    18434854 :   return (signe(d) && expo(d) - expo(b) >= L);
    2632             : }
    2633             : /* assume x > 0 */
    2634             : static GEN
    2635     1281709 : agm1r_abs(GEN x)
    2636             : {
    2637     1281709 :   long l = realprec(x), L = 5-prec2nbits(l);
    2638     1281709 :   GEN a1, b1, y = cgetr(l);
    2639     1281711 :   pari_sp av = avma;
    2640             : 
    2641     1281711 :   a1 = addrr(real_1(l), x); shiftr_inplace(a1, -1);
    2642     1281709 :   b1 = sqrtr_abs(x);
    2643    18434947 :   while (agmr_gap(a1,b1,L))
    2644             :   {
    2645    17153203 :     GEN a = a1;
    2646    17153203 :     a1 = addrr(a,b1); shiftr_inplace(a1, -1);
    2647    17153347 :     b1 = sqrtr_abs(mulrr(a,b1));
    2648             :   }
    2649     1281654 :   affrr_fixlg(a1,y); return gc_const(av,y);
    2650             : }
    2651             : 
    2652             : struct agmcx_gap_t { long L, ex, cnt; };
    2653             : 
    2654             : static void
    2655      305399 : agmcx_init(GEN x, long *prec, struct agmcx_gap_t *S)
    2656             : {
    2657      305399 :   long l = precision(x);
    2658      305399 :   if (l) *prec = l;
    2659      305399 :   S->L = 1-prec2nbits(*prec);
    2660      305399 :   S->cnt = 0;
    2661      305399 :   S->ex = LONG_MAX;
    2662      305399 : }
    2663             : 
    2664             : static long
    2665      305399 : agmcx_a_b(GEN x, GEN *a1, GEN *b1, long prec)
    2666             : {
    2667      305399 :   long rotate = 0;
    2668      305399 :   if (gsigne(real_i(x))<0)
    2669             :   { /* Rotate by +/-Pi/2, so that the choice of the principal square
    2670             :      * root gives the optimal AGM. So a1 = +/-I*a1, b1=sqrt(-x). */
    2671        1561 :     if (gsigne(imag_i(x))<0) { *a1=mulcxI(*a1);  rotate=-1; }
    2672        1043 :     else                     { *a1=mulcxmI(*a1); rotate=1; }
    2673        1561 :     x = gneg(x);
    2674             :   }
    2675      305399 :   *b1 = gsqrt(x, prec);
    2676      305399 :   return rotate;
    2677             : }
    2678             : /* return 0 if we must stop the AGM loop (a=b or a ~ b), 1 otherwise */
    2679             : static int
    2680     5097356 : agmcx_gap(GEN a, GEN b, struct agmcx_gap_t *S)
    2681             : {
    2682     5097356 :   GEN d = gsub(b, a);
    2683     5097356 :   long ex = S->ex;
    2684     5097356 :   S->ex = gexpo(d);
    2685     5097356 :   if (gequal0(d) || S->ex - gexpo(b) < S->L) return 0;
    2686             :   /* if (S->ex >= ex) we're no longer making progress; twice in a row */
    2687     4883059 :   if (S->ex < ex) S->cnt = 0;
    2688             :   else
    2689      182276 :     if (S->cnt++) return 0;
    2690     4791957 :   return 1;
    2691             : }
    2692             : static GEN
    2693      305350 : agm1cx(GEN x, long prec)
    2694             : {
    2695             :   struct agmcx_gap_t S;
    2696             :   GEN a1, b1;
    2697      305350 :   pari_sp av = avma;
    2698             :   long rotate;
    2699      305350 :   agmcx_init(x, &prec, &S);
    2700      305350 :   a1 = gtofp(gmul2n(gadd(real_1(prec), x), -1), prec);
    2701      305350 :   rotate = agmcx_a_b(x, &a1, &b1, prec);
    2702     5097033 :   while (agmcx_gap(a1,b1,&S))
    2703             :   {
    2704     4791683 :     GEN a = a1;
    2705     4791683 :     a1 = gmul2n(gadd(a,b1),-1);
    2706     4791683 :     b1 = gsqrt(gmul(a,b1), prec);
    2707             :   }
    2708      305350 :   if (rotate) a1 = rotate>0 ? mulcxI(a1):mulcxmI(a1);
    2709      305350 :   return gerepilecopy(av,a1);
    2710             : }
    2711             : 
    2712             : GEN
    2713          49 : zellagmcx(GEN a0, GEN b0, GEN r, GEN t, long prec)
    2714             : {
    2715             :   struct agmcx_gap_t S;
    2716          49 :   pari_sp av = avma;
    2717          49 :   GEN x = gdiv(a0, b0), a1, b1;
    2718             :   long rotate;
    2719          49 :   agmcx_init(x, &prec, &S);
    2720          49 :   a1 = gtofp(gmul2n(gadd(real_1(prec), x), -1), prec);
    2721          49 :   r = gsqrt(gdiv(gmul(a1,gaddgs(r, 1)),gadd(r, x)), prec);
    2722          49 :   t = gmul(r, t);
    2723          49 :   rotate = agmcx_a_b(x, &a1, &b1, prec);
    2724         323 :   while (agmcx_gap(a1,b1,&S))
    2725             :   {
    2726         274 :     GEN a = a1, b = b1;
    2727         274 :     a1 = gmul2n(gadd(a,b),-1);
    2728         274 :     b1 = gsqrt(gmul(a,b), prec);
    2729         274 :     r = gsqrt(gdiv(gmul(a1,gaddgs(r, 1)),gadd(gmul(b, r), a )), prec);
    2730         274 :     t = gmul(r, t);
    2731             :   }
    2732          49 :   if (rotate) a1 = rotate>0 ? mulcxI(a1):mulcxmI(a1);
    2733          49 :   a1 = gmul(a1, b0);
    2734          49 :   t = gatan(gdiv(a1,t), prec);
    2735             :   /* send t to the fundamental domain if necessary */
    2736          49 :   if (gsigne(real_i(t))<0) t = gadd(t, mppi(prec));
    2737          49 :   return gerepileupto(av,gdiv(t,a1));
    2738             : }
    2739             : 
    2740             : static long
    2741          49 : ser_cmp_expo(GEN A, GEN B)
    2742             : {
    2743          49 :   long e = -(long)HIGHEXPOBIT, d = valser(B) - valser(A);
    2744          49 :   long i, la = lg(A), v = varn(B);
    2745        9849 :   for (i = 2; i < la; i++)
    2746             :   {
    2747        9800 :     GEN a = gel(A,i), b;
    2748             :     long ei;
    2749        9800 :     if (isexactzero(a)) continue;
    2750        9800 :     b = polcoef_i(B, i-2 + d, v);
    2751        9800 :     ei = gexpo(a);
    2752        9800 :     if (!isexactzero(b)) ei -= gexpo(b);
    2753        9800 :     e = maxss(e, ei);
    2754             :   }
    2755          49 :   return e;
    2756             : }
    2757             : 
    2758             : static GEN
    2759          21 : ser_agm1(GEN y, long prec)
    2760             : {
    2761          21 :   GEN a1 = y, b1 = gen_1;
    2762          21 :   long l = lg(y)-2, l2 = 6-prec2nbits(prec), eold = LONG_MAX;
    2763             :   for(;;)
    2764          84 :   {
    2765         105 :     GEN a = a1, p1;
    2766         105 :     a1 = gmul2n(gadd(a,b1),-1);
    2767         105 :     b1 = gsqrt(gmul(a,b1), prec);
    2768         105 :     p1 = gsub(b1,a1);
    2769         105 :     if (isinexactreal(p1))
    2770             :     {
    2771          49 :       long e = ser_cmp_expo(p1, b1);
    2772          49 :       if (e < l2 || e >= eold) break;
    2773          42 :       eold = e;
    2774             :     }
    2775          56 :     else if (valser(p1)-valser(b1) >= l || gequal0(p1)) break;
    2776             :   }
    2777          21 :   return a1;
    2778             : }
    2779             : 
    2780             : /* agm(1,x) */
    2781             : static GEN
    2782       17637 : agm1(GEN x, long prec)
    2783             : {
    2784             :   GEN y;
    2785             :   pari_sp av;
    2786             : 
    2787       17637 :   if (gequal0(x)) return gcopy(x);
    2788       17637 :   switch(typ(x))
    2789             :   {
    2790          28 :     case t_INT:
    2791          28 :       if (!is_pm1(x)) break;
    2792          21 :       return (signe(x) > 0)? real_1(prec): real_0(prec);
    2793             : 
    2794       11925 :     case t_REAL: return signe(x) > 0? agm1r_abs(x): agm1cx(x, prec);
    2795             : 
    2796        5544 :     case t_COMPLEX:
    2797        5544 :       if (gequal0(gel(x,2))) return agm1(gel(x,1), prec);
    2798        5537 :       return agm1cx(x, prec);
    2799             : 
    2800          14 :     case t_PADIC:
    2801             :     {
    2802          14 :       GEN a1 = x, b1 = gen_1;
    2803          14 :       long l = precp(x);
    2804          14 :       av = avma;
    2805             :       for(;;)
    2806          14 :       {
    2807          28 :         GEN a = a1, p1;
    2808             :         long ep;
    2809          28 :         a1 = gmul2n(gadd(a,b1),-1);
    2810          28 :         a = gmul(a,b1);
    2811          28 :         b1 = Qp_sqrt(a); if (!b1) pari_err_SQRTN("Qp_sqrt",a);
    2812          21 :         p1 = gsub(b1,a1); ep = valp(p1)-valp(b1);
    2813          21 :         if (ep<=0) { b1 = gneg_i(b1); p1 = gsub(b1,a1); ep=valp(p1)-valp(b1); }
    2814          21 :         if (ep >= l || gequal0(p1)) return gerepilecopy(av,a1);
    2815             :       }
    2816             :     }
    2817             : 
    2818         126 :     default:
    2819         126 :       av = avma; if (!(y = toser_i(x))) break;
    2820          21 :       return gerepilecopy(av, ser_agm1(y, prec));
    2821             :   }
    2822         112 :   return trans_eval("agm",agm1,x,prec);
    2823             : }
    2824             : 
    2825             : GEN
    2826       17483 : agm(GEN x, GEN y, long prec)
    2827             : {
    2828             :   pari_sp av;
    2829       17483 :   if (is_matvec_t(typ(y)))
    2830             :   {
    2831          14 :     if (is_matvec_t(typ(x))) pari_err_TYPE2("agm",x,y);
    2832           7 :     swap(x, y);
    2833             :   }
    2834       17476 :   if (gequal0(y)) return gcopy(y);
    2835       17476 :   av = avma;
    2836       17476 :   return gerepileupto(av, gmul(y, agm1(gdiv(x,y), prec)));
    2837             : }
    2838             : 
    2839             : /* b2 != 0 */
    2840             : static GEN
    2841          35 : ellK_i(GEN b2, long prec)
    2842          35 : { return gdiv(Pi2n(-1, prec), agm1(gsqrt(b2, prec), prec)); }
    2843             : GEN
    2844          28 : ellK(GEN k, long prec)
    2845             : {
    2846          28 :   pari_sp av = avma;
    2847          28 :   GEN k2 = gsqr(k), b2 = gsubsg(1, k2);
    2848          28 :   if (gequal0(b2)) pari_err_DOMAIN("ellK", "k^2", "=", gen_1, k2);
    2849          21 :   return gerepileupto(av, ellK_i(b2, prec));
    2850             : }
    2851             : 
    2852             : static int
    2853          84 : magm_gap(GEN a, GEN b, long L)
    2854             : {
    2855          84 :   GEN d = gsub(b, a);
    2856          84 :   return !gequal0(d) && gexpo(d) - gexpo(b) >= L;
    2857             : }
    2858             : 
    2859             : /* http://www.ams.org/notices/201208/rtx120801094p.pdf
    2860             :  * An Eloquent Formula for the Perimeter of an Ellipse
    2861             :  * Semjon Adlaj, Notices of the AMS */
    2862             : static GEN
    2863          14 : magm(GEN a, GEN b, long prec)
    2864             : {
    2865          14 :   long L = -prec2nbits(prec) + 16;
    2866          14 :   GEN c = gen_0;
    2867          84 :   while (magm_gap(a, b, L))
    2868             :   {
    2869          70 :     GEN u = gsqrt(gmul(gsub(a, c), gsub(b, c)), prec);
    2870          70 :     a = gmul2n(gadd(a, b), -1);
    2871          70 :     b = gadd(c, u); c = gsub(c, u);
    2872             :   }
    2873          14 :   return gmul2n(gadd(a, b), -1);
    2874             : }
    2875             : 
    2876             : GEN
    2877          21 : ellE(GEN k, long prec)
    2878             : {
    2879          21 :   pari_sp av = avma;
    2880          21 :   GEN b2 = gsubsg(1, gsqr(k));
    2881          21 :   if (gequal0(b2)) { set_avma(av); return real_1(prec); }
    2882          14 :   return gerepileupto(av, gmul(ellK_i(b2, prec), magm(gen_1, b2, prec)));
    2883             : }
    2884             : 
    2885             : /********************************************************************/
    2886             : /**                                                                **/
    2887             : /**                             LOG(X)                             **/
    2888             : /**                                                                **/
    2889             : /********************************************************************/
    2890             : /* log(2) = 18*atanh(1/26)-2*atanh(1/4801)+8*atanh(1/8749)
    2891             :  * faster than 10*atanh(1/17)+4*atanh(13/499) for all precisions,
    2892             :  * and than Pi/2M(1,4/2^n) ~ n log(2) for bitprec at least up to 10^8 */
    2893             : static GEN
    2894       39161 : log2_split(long prec)
    2895             : {
    2896       39161 :   GEN u = atanhuu(1, 26, prec);
    2897       39154 :   GEN v = atanhuu(1, 4801, prec);
    2898       39158 :   GEN w = atanhuu(1, 8749, prec);
    2899       39155 :   shiftr_inplace(v, 1); setsigne(v, -1);
    2900       39152 :   shiftr_inplace(w, 3);
    2901       39162 :   return addrr(mulur(18, u), addrr(v, w));
    2902             : }
    2903             : GEN
    2904    24626520 : constlog2(long prec)
    2905             : {
    2906             :   pari_sp av;
    2907             :   GEN tmp;
    2908    24626520 :   if (glog2 && realprec(glog2) >= prec) return glog2;
    2909             : 
    2910       38776 :   tmp = cgetr_block(prec);
    2911       39160 :   av = avma;
    2912       39160 :   affrr(log2_split(prec+EXTRAPREC64), tmp);
    2913       39154 :   swap_clone(&glog2,tmp);
    2914       39172 :   return gc_const(av,glog2);
    2915             : }
    2916             : 
    2917             : GEN
    2918    24626530 : mplog2(long prec) { return rtor(constlog2(prec), prec); }
    2919             : 
    2920             : /* dont check that q != 2^expo(q), done in logr_abs */
    2921             : static GEN
    2922     1269829 : logagmr_abs(GEN q)
    2923             : {
    2924     1269829 :   long prec = realprec(q), e = expo(q), lim;
    2925     1269829 :   GEN z = cgetr(prec), y, Q, _4ovQ;
    2926     1269830 :   pari_sp av = avma;
    2927             : 
    2928     1269830 :   incrprec(prec);
    2929     1269830 :   lim = prec2nbits(prec) >> 1;
    2930     1269830 :   Q = rtor(q,prec);
    2931     1269830 :   shiftr_inplace(Q,lim-e); setsigne(Q,1);
    2932             : 
    2933     1269830 :   _4ovQ = invr(Q); shiftr_inplace(_4ovQ, 2); /* 4/Q */
    2934             :   /* Pi / 2agm(1, 4/Q) ~ log(Q), q = Q * 2^(e-lim) */
    2935     1269826 :   y = divrr(Pi2n(-1, prec), agm1r_abs(_4ovQ));
    2936     1269826 :   y = addrr(y, mulsr(e - lim, mplog2(prec)));
    2937     1269827 :   affrr_fixlg(y, z); return gc_const(av,z);
    2938             : }
    2939             : 
    2940             : /* sum_{k >= 0} y^(2k+1) / (2k+1), y close to 0 */
    2941             : static GEN
    2942    10553337 : logr_aux(GEN y)
    2943             : {
    2944    10553337 :   long k, L = realprec(y); /* should be ~ l+1 - (k-2) */
    2945             :   /* log(x) = log(1+y) - log(1-y) = 2 sum_{k odd} y^k / k
    2946             :    * Truncate the sum at k = 2n+1, the remainder is
    2947             :    *   2 sum_{k >= 2n+3} y^k / k < 2y^(2n+3) / (2n+3)(1-y) < y^(2n+3)
    2948             :    * We want y^(2n+3) < y 2^(-prec2nbits(L)), hence
    2949             :    *   n+1 > -prec2nbits(L) /-log_2(y^2) */
    2950    10553337 :   double d = -2*dbllog2r(y); /* ~ -log_2(y^2) */
    2951    10553337 :   k = (long)(2*(prec2nbits(L) / d));
    2952    10553297 :   k |= 1;
    2953    10553297 :   if (k >= 3)
    2954             :   {
    2955    10533417 :     GEN T, S = cgetr(L), y2 = sqrr(y), unr = real_1(L);
    2956    10534236 :     pari_sp av = avma;
    2957    10534236 :     long s = 0, incs = (long)d, l1 = nbits2prec((long)d);
    2958    10534202 :     setprec(S,  l1);
    2959    10534176 :     setprec(unr,l1); affrr(divru(unr,k), S);
    2960   183236598 :     for (k -= 2;; k -= 2) /* k = 2n+1, ..., 1 */
    2961             :     { /* S = y^(2n+1-k)/(2n+1) + ... + 1 / k */
    2962   183236598 :       setprec(y2, l1); T = mulrr(S,y2);
    2963   183247844 :       if (k == 1) break;
    2964             : 
    2965   172713791 :       l1 += dvmdsBIL(s + incs, &s); if (l1>L) l1=L;
    2966   172678966 :       setprec(S, l1);
    2967   172726132 :       setprec(unr,l1);
    2968   172745346 :       affrr(addrr(divru(unr, k), T), S); set_avma(av);
    2969             :     }
    2970             :     /* k = 1 special-cased for eficiency */
    2971    10534053 :     y = mulrr(y, addsr(1,T)); /* = log(X)/2 */
    2972             :   }
    2973    10553929 :   return y;
    2974             : }
    2975             : /*return log(|x|), assuming x != 0 */
    2976             : GEN
    2977    12291282 : logr_abs(GEN X)
    2978             : {
    2979    12291282 :   long EX, L, m, k, a, b, l = realprec(X);
    2980             :   GEN z, x, y;
    2981             :   ulong u;
    2982             :   double d;
    2983             : 
    2984             :  /* Assuming 1 < x < 2, we want delta = x-1, 1-x/2, 1-1/x, or 2/x-1 small.
    2985             :   * We have 2/x-1 > 1-x/2, 1-1/x < x-1. So one should be choosing between
    2986             :   * 1-1/x and 1-x/2 ( crossover sqrt(2), worse ~ 0.29 ). To avoid an inverse,
    2987             :   * we choose between x-1 and 1-x/2 ( crossover 4/3, worse ~ 0.33 ) */
    2988    12291282 :   EX = expo(X);
    2989    12291282 :   u = uel(X,2);
    2990    12291282 :   k = 2;
    2991    12291282 :   if (u > (~0UL / 3) * 2) { /* choose 1-x/2 */
    2992     6990013 :     EX++; u = ~u;
    2993     7052866 :     while (!u && ++k < l) { u = uel(X,k); u = ~u; }
    2994             :   } else { /* choose x - 1 */
    2995     5301269 :     u &= ~HIGHBIT; /* u - HIGHBIT, assuming HIGHBIT set */
    2996     6468869 :     while (!u && ++k < l) u = uel(X,k);
    2997             :   }
    2998    12291282 :   if (k == l) return EX? mulsr(EX, mplog2(l)): real_0(l);
    2999    11823949 :   a = prec2nbits(k) + bfffo(u); /* ~ -log2 |1-x| */
    3000    11824024 :   L = l+1;
    3001    11824024 :   b = prec2nbits(L - (k-2)); /* take loss of accuracy into account */
    3002    20761513 :   if (b > 24*a*log2(L) &&
    3003    10207195 :       prec2nbits(l) > prec2nbits(LOGAGM_LIMIT)) return logagmr_abs(X);
    3004             : 
    3005    10554318 :   z = cgetr(EX? l: l - (k-2));
    3006             : 
    3007             :  /* Multiplication is quadratic in this range (l is small, otherwise we
    3008             :   * use AGM). Set Y = x^(1/2^m), y = (Y - 1) / (Y + 1) and compute truncated
    3009             :   * series sum y^(2k+1)/(2k+1): the costs is less than
    3010             :   *    m b^2 + sum_{k <= n} ((2k+1) e + BITS_IN_LONG)^2
    3011             :   * bit operations with |x-1| <  2^(1-a), |Y| < 2^(1-e) , m = e-a and b bits of
    3012             :   * accuracy needed (+ BITS_IN_LONG since bit accuracies increase by
    3013             :   * increments of BITS_IN_LONG), so
    3014             :   * 4n^3/3 e^2 + n^2 2e BITS_IN_LONG+ n BITS_IN_LONG ~ m b^2, with n ~ b/2e
    3015             :   * or b/6e + BITS_IN_LONG/2e + BITS_IN_LONG/2be ~ m
    3016             :   *    B := (b / 6 + BITS_IN_LONG/2 + BITS_IN_LONG^2 / 2b) ~ m(m+a)
    3017             :   *     m = min( -a/2 + sqrt(a^2/4 + B),  b - a )
    3018             :   * NB: e ~ (b/6)^(1/2) as b -> oo
    3019             :   * Instead of the above pessimistic estimate for the cost of the sum, use
    3020             :   * optimistic estimate (BITS_IN_LONG -> 0) */
    3021    10554202 :   d = -a/2.; m = (long)(d + sqrt(d*d + b/6)); /* >= 0 */
    3022             : 
    3023    10554202 :   if (m > b-a) m = b-a;
    3024    10554202 :   if (m < 0.2*a) m = 0; else L += nbits2extraprec(m);
    3025    10554220 :   x = rtor(X,L);
    3026    10554231 :   setsigne(x,1); shiftr_inplace(x,-EX);
    3027             :   /* 2/3 < x < 4/3 */
    3028    59764318 :   for (k=1; k<=m; k++) x = sqrtr_abs(x);
    3029             : 
    3030    10553598 :   y = divrr(subrs(x,1), addrs(x,1)); /* = (x-1) / (x+1), close to 0 */
    3031    10553340 :   y = logr_aux(y); /* log(1+y) - log(1-y) = log(x) */
    3032    10553859 :   shiftr_inplace(y, m + 1);
    3033    10553739 :   if (EX) y = addrr(y, mulsr(EX, mplog2(l+1)));
    3034    10553564 :   affrr_fixlg(y, z); return gc_const((pari_sp)z, z);
    3035             : }
    3036             : 
    3037             : /* assume Im(q) != 0 and precision(q) >= prec. Compute log(q) with accuracy
    3038             :  * prec [disregard input accuracy] */
    3039             : GEN
    3040      299771 : logagmcx(GEN q, long prec)
    3041             : {
    3042      299771 :   GEN z = cgetc(prec), y, Q, a, b;
    3043             :   long lim, e, ea, eb;
    3044      299771 :   pari_sp av = avma;
    3045      299771 :   int neg = 0;
    3046             : 
    3047      299771 :   incrprec(prec);
    3048      299771 :   if (gsigne(gel(q,1)) < 0) { q = gneg(q); neg = 1; }
    3049      299771 :   lim = prec2nbits(prec) >> 1;
    3050      299771 :   Q = gtofp(q, prec);
    3051      299771 :   a = gel(Q,1);
    3052      299771 :   b = gel(Q,2);
    3053      299771 :   if (gequal0(a)) {
    3054           0 :     affrr_fixlg(logr_abs(b), gel(z,1));
    3055           0 :     y = Pi2n(-1, prec);
    3056           0 :     if (signe(b) < 0) setsigne(y, -1);
    3057           0 :     affrr_fixlg(y, gel(z,2)); return gc_const(av,z);
    3058             :   }
    3059      299771 :   ea = expo(a);
    3060      299771 :   eb = expo(b);
    3061      299771 :   e = ea <= eb ? lim - eb : lim - ea;
    3062      299771 :   shiftr_inplace(a, e);
    3063      299771 :   shiftr_inplace(b, e);
    3064             : 
    3065             :   /* Pi / 2agm(1, 4/Q) ~ log(Q), q = Q * 2^e */
    3066      299771 :   y = gdiv(Pi2n(-1, prec), agm1cx( gdivsg(4, Q), prec ));
    3067      299771 :   a = gel(y,1);
    3068      299771 :   b = gel(y,2);
    3069      299771 :   a = addrr(a, mulsr(-e, mplog2(prec)));
    3070      299771 :   if (realprec(a) <= LOWDEFAULTPREC) a = real_0_bit(expo(a));
    3071      438334 :   if (neg) b = gsigne(b) <= 0? gadd(b, mppi(prec))
    3072      138563 :                              : gsub(b, mppi(prec));
    3073      299771 :   affrr_fixlg(a, gel(z,1));
    3074      299771 :   affrr_fixlg(b, gel(z,2)); return gc_const(av,z);
    3075             : }
    3076             : 
    3077             : GEN
    3078      146513 : mplog(GEN x)
    3079             : {
    3080      146513 :   if (signe(x)<=0) pari_err_DOMAIN("mplog", "argument", "<=", gen_0, x);
    3081      146513 :   return logr_abs(x);
    3082             : }
    3083             : 
    3084             : /* pe = p^e, p prime, 0 < x < pe a t_INT coprime to p. Return the (p-1)-th
    3085             :  * root of 1 in (Z/pe)^* congruent to x mod p, resp x mod 4 if p = 2.
    3086             :  * Simplified form of Zp_sqrtnlift: 1/(p-1) is trivial to compute */
    3087             : GEN
    3088        9933 : Zp_teichmuller(GEN x, GEN p, long e, GEN pe)
    3089             : {
    3090             :   GEN q, z, p1;
    3091             :   pari_sp av;
    3092             :   ulong mask;
    3093        9933 :   if (absequaliu(p,2)) return (mod4(x) & 2)? subiu(pe,1): gen_1;
    3094        9394 :   if (e == 1) return icopy(x);
    3095        9394 :   av = avma;
    3096        9394 :   p1 = subiu(p, 1);
    3097        9394 :   mask = quadratic_prec_mask(e);
    3098        9394 :   q = p; z = remii(x, p);
    3099       31192 :   while (mask > 1)
    3100             :   { /* Newton iteration solving z^{1 - p} = 1, z = x (mod p) */
    3101       21798 :     GEN w, t, qold = q;
    3102       21798 :     if (mask <= 3) /* last iteration */
    3103        9394 :       q = pe;
    3104             :     else
    3105             :     {
    3106       12404 :       q = sqri(q);
    3107       12404 :       if (mask & 1) q = diviiexact(q, p);
    3108             :     }
    3109       21798 :     mask >>= 1;
    3110             :     /* q <= qold^2 */
    3111       21798 :     if (lgefint(q) == 3)
    3112             :     {
    3113       21650 :       ulong Z = uel(z,2), Q = uel(q,2), P1 = uel(p1,2);
    3114       21650 :       ulong W = (Q-1) / P1; /* -1/(p-1) + O(qold) */
    3115       21650 :       ulong T = Fl_mul(W, Fl_powu(Z,P1,Q) - 1, Q);
    3116       21650 :       Z = Fl_mul(Z, 1 + T, Q);
    3117       21650 :       z = utoi(Z);
    3118             :     }
    3119             :     else
    3120             :     {
    3121         148 :       w = diviiexact(subiu(qold,1),p1); /* -1/(p-1) + O(qold) */
    3122         148 :       t = Fp_mul(w, subiu(Fp_pow(z,p1,q), 1), q);
    3123         148 :       z = Fp_mul(z, addui(1,t), q);
    3124             :     }
    3125             :   }
    3126        9394 :   return gerepileuptoint(av, z);
    3127             : }
    3128             : 
    3129             : GEN
    3130        1225 : teichmullerinit(long p, long n)
    3131             : {
    3132             :   GEN t, pn, g, v;
    3133             :   ulong gp, tp;
    3134             :   long a, m;
    3135             : 
    3136        1225 :   if (p == 2) return mkvec(gen_1);
    3137        1225 :   if (!uisprime(p)) pari_err_PRIME("teichmullerinit",utoipos(p));
    3138             : 
    3139        1225 :   m = p >> 1; /* (p-1)/2 */
    3140        1225 :   tp= gp= pgener_Fl(p); /* order (p-1), gp^m = -1 */
    3141        1225 :   pn = powuu(p, n);
    3142        1225 :   v = cgetg(p, t_VEC);
    3143        1225 :   t = g = Zp_teichmuller(utoipos(gp), utoipos(p), n, pn);
    3144        1225 :   gel(v, 1) = gen_1;
    3145        1225 :   gel(v, p-1) = subiu(pn,1);
    3146        3031 :   for (a = 1; a < m; a++)
    3147             :   {
    3148        1806 :     gel(v, tp) = t;
    3149        1806 :     gel(v, p - tp) = Fp_neg(t, pn); /* g^(m+a) = -g^a */
    3150        1806 :     if (a < m-1)
    3151             :     {
    3152        1029 :       t = Fp_mul(t, g, pn); /* g^(a+1) */
    3153        1029 :       tp = Fl_mul(tp, gp, p); /* t mod p  */
    3154             :     }
    3155             :   }
    3156        1225 :   return v;
    3157             : }
    3158             : 
    3159             : /* tab from teichmullerinit or NULL */
    3160             : GEN
    3161        4977 : teichmuller(GEN x, GEN tab)
    3162             : {
    3163             :   GEN p, q, y, z;
    3164        4977 :   long n, tx = typ(x);
    3165             : 
    3166        4977 :   if (!tab)
    3167             :   {
    3168        4865 :     if (tx == t_VEC && lg(x) == 3)
    3169             :     {
    3170           7 :       p = gel(x,1);
    3171           7 :       q = gel(x,2);
    3172           7 :       if (typ(p) == t_INT && typ(q) == t_INT)
    3173           7 :         return teichmullerinit(itos(p), itos(q));
    3174             :     }
    3175             :   }
    3176         112 :   else if (typ(tab) != t_VEC) pari_err_TYPE("teichmuller",tab);
    3177        4970 :   if (tx!=t_PADIC) pari_err_TYPE("teichmuller",x);
    3178        4970 :   z = gel(x,4);
    3179        4970 :   if (!signe(z)) return gcopy(x);
    3180        4970 :   p = gel(x,2);
    3181        4970 :   q = gel(x,3);
    3182        4970 :   n = precp(x);
    3183        4970 :   y = cgetg(5,t_PADIC);
    3184        4970 :   y[1] = evalprecp(n) | _evalvalp(0);
    3185        4970 :   gel(y,2) = icopy(p);
    3186        4970 :   gel(y,3) = icopy(q);
    3187        4970 :   if (tab)
    3188             :   {
    3189         112 :     ulong pp = itou_or_0(p);
    3190         112 :     if (lg(tab) != (long)pp) pari_err_TYPE("teichmuller",tab);
    3191         112 :     z = gel(tab, umodiu(z, pp));
    3192         112 :     if (typ(z) != t_INT) pari_err_TYPE("teichmuller",tab);
    3193         112 :     z = remii(z, q);
    3194             :   }
    3195             :   else
    3196        4858 :     z = Zp_teichmuller(z, p, n, q);
    3197        4970 :   gel(y,4) = z;
    3198        4970 :   return y;
    3199             : }
    3200             : GEN
    3201        4739 : teich(GEN x) { return teichmuller(x, NULL); }
    3202             : 
    3203             : GEN
    3204    15880063 : glog(GEN x, long prec)
    3205             : {
    3206             :   pari_sp av, tetpil;
    3207             :   GEN y, p1;
    3208             :   long l;
    3209             : 
    3210    15880063 :   switch(typ(x))
    3211             :   {
    3212     9297224 :     case t_REAL:
    3213     9297224 :       if (signe(x) >= 0)
    3214             :       {
    3215     7649634 :         if (!signe(x)) pari_err_DOMAIN("log", "argument", "=", gen_0, x);
    3216     7649620 :         return logr_abs(x);
    3217             :       }
    3218     1647590 :       retmkcomplex(logr_abs(x), mppi(realprec(x)));
    3219             : 
    3220      518776 :     case t_FRAC:
    3221             :     {
    3222             :       GEN a, b;
    3223             :       long e1, e2;
    3224      518776 :       av = avma;
    3225      518776 :       a = gel(x,1);
    3226      518776 :       b = gel(x,2);
    3227      518776 :       e1 = expi(subii(a,b)); e2 = expi(b);
    3228      518776 :       if (e2 > e1) prec += nbits2nlong(e2 - e1);
    3229      518776 :       x = fractor(x, prec);
    3230      518776 :       return gerepileupto(av, glog(x, prec));
    3231             :     }
    3232     3863765 :     case t_COMPLEX:
    3233     3863765 :       if (ismpzero(gel(x,2))) return glog(gel(x,1), prec);
    3234     3853622 :       l = precision(x); if (l > prec) prec = l;
    3235     3853649 :       if (ismpzero(gel(x,1)))
    3236             :       {
    3237       72829 :         GEN a = gel(x,2), b;
    3238       72829 :         av = avma; b = Pi2n(-1,prec);
    3239       72829 :         if (gsigne(a) < 0) { setsigne(b, -1); a = gabs(a,prec); }
    3240       72829 :         a = isint1(a) ? gen_0: glog(a,prec);
    3241       72829 :         return gerepilecopy(av, mkcomplex(a, b));
    3242             :       }
    3243     3780828 :       if (prec >= LOGAGMCX_LIMIT) return logagmcx(x, prec);
    3244     3481253 :       y = cgetg(3,t_COMPLEX);
    3245     3481234 :       gel(y,2) = garg(x,prec);
    3246     3481269 :       av = avma; p1 = glog(cxnorm(x),prec); tetpil = avma;
    3247     3481298 :       gel(y,1) = gerepile(av,tetpil,gmul2n(p1,-1)); return y;
    3248             : 
    3249         322 :     case t_PADIC: return Qp_log(x);
    3250     2199976 :     default:
    3251     2199976 :       av = avma; if (!(y = toser_i(x))) break;
    3252         140 :       if (!signe(y)) pari_err_DOMAIN("log", "argument", "=", gen_0, x);
    3253         140 :       if (valser(y)) pari_err_DOMAIN("log", "series valuation", "!=", gen_0, x);
    3254         133 :       p1 = integser(gdiv(derivser(y), y)); /* log(y)' = y'/y */
    3255         133 :       if (!gequal1(gel(y,2))) p1 = gadd(p1, glog(gel(y,2),prec));
    3256         133 :       return gerepileupto(av, p1);
    3257             :   }
    3258     2200126 :   return trans_eval("log",glog,x,prec);
    3259             : }
    3260             : 
    3261             : static GEN
    3262          63 : mplog1p(GEN x)
    3263             : {
    3264             :   long ex, a, b, l, L;
    3265          63 :   if (!signe(x)) return rcopy(x);
    3266          63 :   ex = expo(x); if (ex >= -3) return glog(addrs(x,1), 0);
    3267          42 :   a = -ex;
    3268          42 :   b = realprec(x); L = b+1;
    3269          42 :   if (b > a*log2(L) && prec2nbits(b) > prec2nbits(LOGAGM_LIMIT))
    3270             :   {
    3271           0 :     x = addrs(x,1); l = b + nbits2extraprec(a);
    3272           0 :     if (realprec(x) < l) x = rtor(x,l);
    3273           0 :     return logagmr_abs(x);
    3274             :   }
    3275          42 :   x = rtor(x, L);
    3276          42 :   x = logr_aux(divrr(x, addrs(x,2)));
    3277          42 :   if (realprec(x) > b) fixlg(x, b);
    3278          42 :   shiftr_inplace(x,1); return x;
    3279             : }
    3280             : 
    3281             : static GEN log1p_i(GEN x, long prec);
    3282             : static GEN
    3283          14 : cxlog1p(GEN x, long prec)
    3284             : {
    3285             :   pari_sp av;
    3286          14 :   GEN z, a, b = gel(x,2);
    3287             :   long l;
    3288          14 :   if (ismpzero(b)) return log1p_i(gel(x,1), prec);
    3289          14 :   l = precision(x); if (l > prec) prec = l;
    3290          14 :   if (prec >= LOGAGMCX_LIMIT) return logagmcx(gaddgs(x,1), prec);
    3291          14 :   a = gel(x,1);
    3292          14 :   z = cgetg(3,t_COMPLEX); av = avma;
    3293          14 :   a = gadd(gadd(gmul2n(a,1), gsqr(a)), gsqr(b));
    3294          14 :   a = log1p_i(a, prec); shiftr_inplace(a,-1);
    3295          14 :   gel(z,1) = gerepileupto(av, a);
    3296          14 :   gel(z,2) = garg(gaddgs(x,1),prec); return z;
    3297             : }
    3298             : static GEN
    3299         133 : log1p_i(GEN x, long prec)
    3300             : {
    3301         133 :   switch(typ(x))
    3302             :   {
    3303          63 :     case t_REAL: return mplog1p(x);
    3304          14 :     case t_COMPLEX: return cxlog1p(x, prec);
    3305           7 :     case t_PADIC: return Qp_log(gaddgs(x,1));
    3306          49 :     default:
    3307             :     {
    3308             :       long ey;
    3309             :       GEN y;
    3310          49 :       if (!(y = toser_i(x))) break;
    3311          21 :       ey = valser(y);
    3312          21 :       if (ey < 0) pari_err_DOMAIN("log1p","valuation", "<", gen_0, x);
    3313          21 :       if (gequal0(y)) return gcopy(y);
    3314          14 :       if (ey)
    3315           7 :         return glog(gaddgs(y,1),prec);
    3316             :       else
    3317             :       {
    3318           7 :         GEN a = gel(y,2), a1 = gaddgs(a,1);
    3319           7 :         y = gdiv(y, a1); gel(y,2) = gen_1;
    3320           7 :         return gadd(glog1p(a,prec), glog(y, prec));
    3321             :       }
    3322             :     }
    3323             :   }
    3324          28 :   return trans_eval("log1p",glog1p,x,prec);
    3325             : }
    3326             : GEN
    3327         119 : glog1p(GEN x, long prec)
    3328             : {
    3329         119 :   pari_sp av = avma;
    3330         119 :   return gerepileupto(av, log1p_i(x, prec));
    3331             : }
    3332             : /********************************************************************/
    3333             : /**                                                                **/
    3334             : /**                        SINE, COSINE                            **/
    3335             : /**                                                                **/
    3336             : /********************************************************************/
    3337             : 
    3338             : /* Reduce x0 mod Pi/2 to x in [-Pi/4, Pi/4]. Return cos(x)-1 */
    3339             : static GEN
    3340    10371598 : mpcosm1(GEN x, long *ptmod8)
    3341             : {
    3342    10371598 :   long a = expo(x), l = realprec(x), b, L, i, n, m, B;
    3343             :   GEN y, u, x2;
    3344             :   double d;
    3345             : 
    3346    10371598 :   n = 0;
    3347    10371598 :   if (a >= 0)
    3348             :   {
    3349             :     long p;
    3350             :     GEN q;
    3351     8497466 :     if (a > 30)
    3352             :     {
    3353           7 :       GEN z, P = Pi2n(-2, nbits2prec(a + 32));
    3354           7 :       z = addrr(x,P); /* = x + Pi/4 */
    3355           7 :       if (expo(z) >= bit_prec(z) + 3) pari_err_PREC("mpcosm1");
    3356           7 :       shiftr_inplace(P, 1);
    3357           7 :       q = floorr(divrr(z, P)); /* round ( x / (Pi/2) ) */
    3358           7 :       p = l+EXTRAPREC64; x = rtor(x,p);
    3359             :     } else {
    3360     8497459 :       q = stoi((long)floor(rtodbl(x) / (M_PI/2) + 0.5));
    3361     8497431 :       p = l;
    3362             :     }
    3363     8497663 :     if (signe(q))
    3364             :     {
    3365     8497447 :       GEN y = subrr(x, mulir(q, Pi2n(-1,p))); /* x mod Pi/2  */
    3366     8497188 :       long b = expo(y);
    3367     8497188 :       if (a - b < 7) x = y;
    3368             :       else
    3369             :       {
    3370     4484649 :         p += nbits2extraprec(a-b); x = rtor(x, p);
    3371     4484667 :         x = subrr(x, mulir(q, Pi2n(-1,p)));
    3372             :       }
    3373     8497108 :       a = b;
    3374     8497108 :       if (!signe(x) && a >= 0) pari_err_PREC("mpcosm1");
    3375     8497108 :       n = Mod4(q);
    3376             :     }
    3377             :   }
    3378             :   /* a < 0 */
    3379    10371526 :   b = signe(x); *ptmod8 = (b < 0)? 4 + n: n;
    3380    10371526 :   if (!b) return real_0_bit(expo(x)*2 - 1);
    3381             : 
    3382    10371526 :   b = prec2nbits(l);
    3383    10371528 :   if (b + 2*a <= 0) {
    3384     1369156 :     y = sqrr(x); shiftr_inplace(y, -1); setsigne(y, -1);
    3385     1369154 :     return y;
    3386             :   }
    3387             : 
    3388     9002372 :   y = cgetr(l);
    3389     9002333 :   B = b/6 + BITS_IN_LONG/2 + (BITS_IN_LONG*BITS_IN_LONG/2)/ b;
    3390     9002333 :   d = a/2.; m = (long)(d + sqrt(d*d + B)); /* >= 0 ,*/
    3391     9002333 :   if (m < (-a) * 0.1) m = 0; /* not worth it */
    3392     9002333 :   L = l + nbits2extraprec(m);
    3393             : 
    3394     9002402 :   b += m;
    3395     9002402 :   d = 2.0 * (m-dbllog2r(x)-1/M_LN2); /* ~ 2( - log_2 Y - 1/log(2) ) */
    3396     9002503 :   n = (long)(b / d);
    3397     9002503 :   if (n > 1)
    3398     8944778 :     n = (long)(b / (d + log2((double)n+1))); /* log~constant in small ranges */
    3399    19089355 :   while (n*(d+log2((double)n+1)) < b) n++; /* expect few corrections */
    3400             : 
    3401             :  /* Multiplication is quadratic in this range (l is small, otherwise we
    3402             :   * use logAGM + Newton). Set Y = 2^(-e-a) x, compute truncated series
    3403             :   * sum Y^2k/(2k)!: this costs roughly
    3404             :   *   m b^2 + sum_{k <= n} (2k e + BITS_IN_LONG)^2
    3405             :   *   ~ (b/2e) b^2 / 3  + m b^2
    3406             :   * bit operations with n ~ b/2e, |x| <  2^(1+a), |Y| < 2^(1-e) , m = e+a and
    3407             :   * b bits of accuracy needed, so
    3408             :   *    B := (b / 6 + BITS_IN_LONG/2 + BITS_IN_LONG^2 / 2b) ~ m(m-a)
    3409             :   * we want b ~ 6 m (m-a) or m~b+a hence
    3410             :   *     m = min( a/2 + sqrt(a^2/4 + b/6),  b/2 + a )
    3411             :   * NB: e ~ (b/6)^(1/2) or b/2.
    3412             :   *
    3413             :   * Truncate the sum at k = n (>= 1), the remainder is
    3414             :   * < sum_{k >= n+1} Y^2k / 2k! < Y^(2n+2) / (2n+2)!(1-Y^2) < Y^(2n+2)/(2n+1)!
    3415             :   * We want ... <= Y^2 2^-b, hence -2n log_2 |Y| + log_2 (2n+1)! >= b
    3416             :   *   log n! ~ (n + 1/2) log(n+1) - (n+1) + log(2Pi)/2,
    3417             :   * error bounded by 1/6(n+1) <= 1/12. Finally, we want
    3418             :   * 2n (-1/log(2) - log_2 |Y| + log_2(2n+2)) >= b  */
    3419     9002503 :   x = rtor(x, L); shiftr_inplace(x, -m); setsigne(x, 1);
    3420     9002401 :   x2 = sqrr(x);
    3421     9002369 :   if (n == 1) { u = x2; shiftr_inplace(u, -1); setsigne(u, -1); } /*-Y^2/2*/
    3422             :   else
    3423             :   {
    3424     9002369 :     GEN un = real_1(L);
    3425             :     pari_sp av;
    3426     9002438 :     long s = 0, l1 = nbits2prec((long)(d + n + 16));
    3427             : 
    3428     9002372 :     u = cgetr(L); av = avma;
    3429   115533719 :     for (i = n; i >= 2; i--)
    3430             :     {
    3431             :       GEN t;
    3432   106531417 :       setprec(x2,l1); t = divrunextu(x2, 2*i-1);
    3433   106537094 :       l1 += dvmdsBIL(s - expo(t), &s); if (l1 > L) l1 = L;
    3434   106537104 :       if (i != n) t = mulrr(t,u);
    3435   106534039 :       setprec(un,l1); t = addrr_sign(un,1, t,-signe(t));
    3436   106514629 :       setprec(u,l1); affrr(t,u); set_avma(av);
    3437             :     }
    3438     9002302 :     shiftr_inplace(u, -1); togglesign(u); /* u := -u/2 */
    3439     9002323 :     setprec(x2,L); u = mulrr(x2,u);
    3440             :   }
    3441             :   /* Now u = sum {1<= i <=n} (-1)^i x^(2i) / (2i)! ~ cos(x) - 1 */
    3442    77277317 :   for (i = 1; i <= m; i++)
    3443             :   { /* u = cos(x)-1 <- cos(2x)-1 = 2cos(x)^2 - 2 = 4u + 2u^2*/
    3444    68277478 :     GEN q = sqrr(u);
    3445    68290575 :     shiftr_inplace(u, 1); u = addrr(u, q);
    3446    68277235 :     shiftr_inplace(u, 1);
    3447    68274642 :     if ((i & 31) == 0) u = gerepileuptoleaf((pari_sp)y, u);
    3448             :   }
    3449     8999839 :   affrr_fixlg(u, y); return y;
    3450             : }
    3451             : 
    3452             : /* sqrt (|1 - (1+x)^2|) = sqrt(|x*(x+2)|). Sends cos(x)-1 to |sin(x)| */
    3453             : static GEN
    3454     8665624 : mpaut(GEN x)
    3455             : {
    3456     8665624 :   GEN t = mulrr(x, addsr(2,x)); /* != 0 */
    3457     8665745 :   if (!signe(t)) return real_0_bit(expo(t) >> 1);
    3458     8665745 :   return sqrtr_abs(t);
    3459             : }
    3460             : 
    3461             : /********************************************************************/
    3462             : /**                            COSINE                              **/
    3463             : /********************************************************************/
    3464             : 
    3465             : GEN
    3466     2745066 : mpcos(GEN x)
    3467             : {
    3468             :   long mod8;
    3469             :   pari_sp av;
    3470             :   GEN y, z;
    3471             : 
    3472     2745066 :   if (!signe(x)) {
    3473          75 :     long l = nbits2prec(-expo(x));
    3474          75 :     if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
    3475          75 :     return real_1(l);
    3476             :   }
    3477     2744991 :   av = avma; z = mpcosm1(x,&mod8);
    3478     2744979 :   switch(mod8)
    3479             :   {
    3480      760057 :     case 0: case 4: y = addsr(1,z); break;
    3481      688608 :     case 1: case 7: y = mpaut(z); togglesign(y); break;
    3482      682289 :     case 2: case 6: y = subsr(-1,z); break;
    3483      614025 :     default:        y = mpaut(z); break; /* case 3: case 5: */
    3484             :   }
    3485     2745011 :   return gerepileuptoleaf(av, y);
    3486             : }
    3487             : 
    3488             : /* convert INT or FRAC to REAL, which is later reduced mod 2Pi : avoid
    3489             :  * cancellation */
    3490             : static GEN
    3491       13257 : tofp_safe(GEN x, long prec)
    3492             : {
    3493       13257 :   return (typ(x) == t_INT || gexpo(x) > 0)? gadd(x, real_0(prec))
    3494       26504 :                                           : fractor(x, prec);
    3495             : }
    3496             : 
    3497             : GEN
    3498      154842 : gcos(GEN x, long prec)
    3499             : {
    3500             :   pari_sp av;
    3501             :   GEN a, b, u, v, y, u1, v1;
    3502             :   long i;
    3503             : 
    3504      154842 :   switch(typ(x))
    3505             :   {
    3506      153576 :     case t_REAL: return mpcos(x);
    3507          28 :     case t_COMPLEX:
    3508          28 :       a = gel(x,1);
    3509          28 :       b = gel(x,2);
    3510          28 :       if (isintzero(a)) return gcosh(b, prec);
    3511          14 :       i = precision(x); if (i) prec = i;
    3512          14 :       y = cgetc(prec); av = avma;
    3513          14 :       if (typ(b) != t_REAL) b = gtofp(b, prec);
    3514          14 :       mpsinhcosh(b, &u1, &v1); u1 = mpneg(u1);
    3515          14 :       if (typ(a) != t_REAL) a = gtofp(a, prec);
    3516          14 :       mpsincos(a, &u, &v);
    3517          14 :       affrr_fixlg(gmul(v1,v), gel(y,1));
    3518          14 :       affrr_fixlg(gmul(u1,u), gel(y,2)); return gc_const(av,y);
    3519             : 
    3520        1156 :     case t_INT: case t_FRAC:
    3521        1156 :       y = cgetr(prec); av = avma;
    3522        1156 :       affrr_fixlg(mpcos(tofp_safe(x,prec)), y); return gc_const(av,y);
    3523             : 
    3524          49 :     case t_PADIC: y = cos_p(x);
    3525          49 :       if (!y) pari_err_DOMAIN("gcos(t_PADIC)","argument","",gen_0,x);
    3526          42 :       return y;
    3527             : 
    3528          33 :     default:
    3529          33 :       av = avma; if (!(y = toser_i(x))) break;
    3530          28 :       if (gequal0(y)) return gerepileupto(av, gaddsg(1,y));
    3531          28 :       if (valser(y) < 0)
    3532           7 :         pari_err_DOMAIN("cos","valuation", "<", gen_0, x);
    3533          21 :       gsincos(y,&u,&v,prec);
    3534          21 :       return gerepilecopy(av,v);
    3535             :   }
    3536           7 :   return trans_eval("cos",gcos,x,prec);
    3537             : }
    3538             : /********************************************************************/
    3539             : /**                             SINE                               **/
    3540             : /********************************************************************/
    3541             : 
    3542             : GEN
    3543      849542 : mpsin(GEN x)
    3544             : {
    3545             :   long mod8;
    3546             :   pari_sp av;
    3547             :   GEN y, z;
    3548             : 
    3549      849542 :   if (!signe(x)) return real_0_bit(expo(x));
    3550      849333 :   av = avma; z = mpcosm1(x,&mod8);
    3551      849316 :   switch(mod8)
    3552             :   {
    3553      316706 :     case 0: case 6: y = mpaut(z); break;
    3554      134034 :     case 1: case 5: y = addsr(1,z); break;
    3555      269118 :     case 2: case 4: y = mpaut(z); togglesign(y); break;
    3556      129458 :     default:        y = subsr(-1,z); break; /* case 3: case 7: */
    3557             :   }
    3558      849348 :   return gerepileuptoleaf(av, y);
    3559             : }
    3560             : 
    3561             : GEN
    3562      882475 : gsin(GEN x, long prec)
    3563             : {
    3564             :   pari_sp av;
    3565             :   GEN a, b, u, v, y, v1, u1;
    3566             :   long i;
    3567             : 
    3568      882475 :   switch(typ(x))
    3569             :   {
    3570      844368 :     case t_REAL: return mpsin(x);
    3571       32711 :     case t_COMPLEX:
    3572       32711 :       a = gel(x,1);
    3573       32711 :       b = gel(x,2);
    3574       32711 :       if (isintzero(a)) retmkcomplex(gen_0,gsinh(b,prec));
    3575       17402 :       i = precision(x); if (i) prec = i;
    3576       17402 :       y = cgetc(prec); av = avma;
    3577       17402 :       if (typ(b) != t_REAL) b = gtofp(b, prec);
    3578       17402 :       mpsinhcosh(b, &u1, &v1);
    3579       17402 :       if (typ(a) != t_REAL) a = gtofp(a, prec);
    3580       17402 :       mpsincos(a, &u, &v);
    3581       17402 :       affrr_fixlg(gmul(v1,u), gel(y,1));
    3582       17402 :       affrr_fixlg(gmul(u1,v), gel(y,2)); return gc_const(av,y);
    3583             : 
    3584        5118 :     case t_INT: case t_FRAC:
    3585        5118 :       y = cgetr(prec); av = avma;
    3586        5118 :       affrr_fixlg(mpsin(tofp_safe(x,prec)), y); return gc_const(av,y);
    3587             : 
    3588          49 :     case t_PADIC: y = sin_p(x);
    3589          49 :       if (!y) pari_err_DOMAIN("gsin(t_PADIC)","argument","",gen_0,x);
    3590          42 :       return y;
    3591             : 
    3592         229 :     default:
    3593         229 :       av = avma; if (!(y = toser_i(x))) break;
    3594         224 :       if (gequal0(y)) return gerepilecopy(av, y);
    3595         224 :       if (valser(y) < 0)
    3596           7 :         pari_err_DOMAIN("sin","valuation", "<", gen_0, x);
    3597         217 :       gsincos(y,&u,&v,prec);
    3598         217 :       return gerepilecopy(av,u);
    3599             :   }
    3600           7 :   return trans_eval("sin",gsin,x,prec);
    3601             : }
    3602             : /********************************************************************/
    3603             : /**                       SINE, COSINE together                    **/
    3604             : /********************************************************************/
    3605             : 
    3606             : void
    3607     6774808 : mpsincos(GEN x, GEN *s, GEN *c)
    3608             : {
    3609             :   long mod8;
    3610             :   pari_sp av, tetpil;
    3611             :   GEN z, *gptr[2];
    3612             : 
    3613     6774808 :   if (!signe(x))
    3614             :   {
    3615        3365 :     long e = expo(x);
    3616        3365 :     *s = real_0_bit(e);
    3617        3365 :     *c = e >= 0? real_0_bit(e): real_1_bit(-e);
    3618        3365 :     return;
    3619             :   }
    3620             : 
    3621     6771443 :   av = avma; z = mpcosm1(x, &mod8); tetpil = avma;
    3622     6771557 :   switch(mod8)
    3623             :   {
    3624     1567730 :     case 0: *c = addsr( 1,z); *s = mpaut(z); break;
    3625      452163 :     case 1: *s = addsr( 1,z); *c = mpaut(z); togglesign(*c); break;
    3626      798404 :     case 2: *c = subsr(-1,z); *s = mpaut(z); togglesign(*s); break;
    3627      435699 :     case 3: *s = subsr(-1,z); *c = mpaut(z); break;
    3628      925001 :     case 4: *c = addsr( 1,z); *s = mpaut(z); togglesign(*s); break;
    3629      428928 :     case 5: *s = addsr( 1,z); *c = mpaut(z); break;
    3630     1696622 :     case 6: *c = subsr(-1,z); *s = mpaut(z); break;
    3631      467079 :     case 7: *s = subsr(-1,z); *c = mpaut(z); togglesign(*c); break;
    3632             :   }
    3633     6771299 :   gptr[0] = s; gptr[1] = c; gerepilemanysp(av,tetpil,gptr,2);
    3634             : }
    3635             : 
    3636             : /* SINE and COSINE - 1 */
    3637             : void
    3638        5890 : mpsincosm1(GEN x, GEN *s, GEN *c)
    3639             : {
    3640             :   long mod8;
    3641             :   pari_sp av, tetpil;
    3642             :   GEN z, *gptr[2];
    3643             : 
    3644        5890 :   if (!signe(x))
    3645             :   {
    3646           0 :     long e = expo(x);
    3647           0 :     *s = real_0_bit(e);
    3648           0 :     *c = real_0_bit(2*e-1);
    3649           0 :     return;
    3650             :   }
    3651        5890 :   av = avma; z = mpcosm1(x,&mod8); tetpil = avma;
    3652        5890 :   switch(mod8)
    3653             :   {
    3654        4924 :     case 0: *c = rcopy(z); *s = mpaut(z); break;
    3655          42 :     case 1: *s = addsr(1,z); *c = addrs(mpaut(z),1); togglesign(*c); break;
    3656           0 :     case 2: *c = subsr(-2,z); *s = mpaut(z); togglesign(*s); break;
    3657           0 :     case 3: *s = subsr(-1,z); *c = subrs(mpaut(z),1); break;
    3658         819 :     case 4: *c = rcopy(z); *s = mpaut(z); togglesign(*s); break;
    3659          91 :     case 5: *s = addsr( 1,z); *c = subrs(mpaut(z),1); break;
    3660           7 :     case 6: *c = subsr(-2,z); *s = mpaut(z); break;
    3661           7 :     case 7: *s = subsr(-1,z); *c = subsr(-1,mpaut(z)); break;
    3662             :   }
    3663        5890 :   gptr[0] = s; gptr[1] = c;
    3664        5890 :   gerepilemanysp(av,tetpil,gptr,2);
    3665             : }
    3666             : 
    3667             : /* return exp(ix), x a t_REAL */
    3668             : GEN
    3669      762215 : expIr(GEN x)
    3670             : {
    3671      762215 :   pari_sp av = avma;
    3672      762215 :   GEN v = cgetg(3,t_COMPLEX);
    3673      762219 :   mpsincos(x, (GEN*)(v+2), (GEN*)(v+1));
    3674      762223 :   if (!signe(gel(v,2))) return gerepilecopy(av, gel(v,1));
    3675      760066 :   return v;
    3676             : }
    3677             : 
    3678             : /* return exp(ix)-1, x a t_REAL */
    3679             : static GEN
    3680        5890 : expm1_Ir(GEN x)
    3681             : {
    3682        5890 :   pari_sp av = avma;
    3683        5890 :   GEN v = cgetg(3,t_COMPLEX);
    3684        5890 :   mpsincosm1(x, (GEN*)(v+2), (GEN*)(v+1));
    3685        5890 :   if (!signe(gel(v,2))) return gerepilecopy(av, gel(v,1));
    3686        5890 :   return v;
    3687             : }
    3688             : 
    3689             : /* return exp(z)-1, z complex */
    3690             : GEN
    3691        5946 : cxexpm1(GEN z, long prec)
    3692             : {
    3693        5946 :   pari_sp av = avma;
    3694        5946 :   GEN X, Y, x = real_i(z), y = imag_i(z);
    3695        5946 :   long l = precision(z);
    3696        5946 :   if (l) prec = l;
    3697        5946 :   if (typ(x) != t_REAL) x = gtofp(x, prec);
    3698        5946 :   if (typ(y) != t_REAL) y = gtofp(y, prec);
    3699        5946 :   if (gequal0(y)) return mpexpm1(x);
    3700        5890 :   if (gequal0(x)) return expm1_Ir(y);
    3701        5757 :   X = mpexpm1(x); /* t_REAL */
    3702        5757 :   Y = expm1_Ir(y);
    3703             :   /* exp(x+iy) - 1 = (exp(x)-1)(exp(iy)-1) + exp(x)-1 + exp(iy)-1 */
    3704        5757 :   return gerepileupto(av, gadd(gadd(X,Y), gmul(X,Y)));
    3705             : }
    3706             : 
    3707             : void
    3708     3298956 : gsincos(GEN x, GEN *s, GEN *c, long prec)
    3709             : {
    3710             :   long i, j, ex, ex2, lx, ly, mi;
    3711             :   pari_sp av, tetpil;
    3712             :   GEN y, r, u, v, u1, v1, p1, p2, p3, p4, ps, pc;
    3713             :   GEN *gptr[4];
    3714             : 
    3715     3298956 :   switch(typ(x))
    3716             :   {
    3717        6954 :     case t_INT: case t_FRAC:
    3718        6954 :       *s = cgetr(prec);
    3719        6949 :       *c = cgetr(prec); av = avma;
    3720        6950 :       mpsincos(tofp_safe(x, prec), &ps, &pc);
    3721        6950 :       affrr_fixlg(ps,*s);
    3722     3299160 :       affrr_fixlg(pc,*c); set_avma(av); return;
    3723             : 
    3724     3287421 :     case t_REAL:
    3725     3287421 :       mpsincos(x,s,c); return;
    3726             : 
    3727        4130 :     case t_COMPLEX:
    3728        4130 :       i = precision(x); if (i) prec = i;
    3729        4130 :       ps = cgetc(prec); *s = ps;
    3730        4130 :       pc = cgetc(prec); *c = pc; av = avma;
    3731        4130 :       r = gexp(gel(x,2),prec);
    3732        4130 :       v1 = gmul2n(addrr(invr(r),r), -1); /* = cos(I*Im(x)) */
    3733        4130 :       u1 = subrr(r, v1); /* = I*sin(I*Im(x)) */
    3734        4130 :       gsincos(gel(x,1), &u,&v, prec);
    3735        4130 :       affrr_fixlg(mulrr(v1,u), gel(ps,1));
    3736        4130 :       affrr_fixlg(mulrr(u1,v), gel(ps,2));
    3737        4130 :       affrr_fixlg(mulrr(v1,v), gel(pc,1));
    3738        4130 :       affrr_fixlg(mulrr(u1,u), gel(pc,2)); togglesign(gel(pc,2));
    3739        4130 :       set_avma(av); return;
    3740             : 
    3741           0 :     case t_QUAD:
    3742           0 :       av = avma; gsincos(quadtofp(x, prec), s, c, prec);
    3743           0 :       gerepileall(av, 2, s, c); return;
    3744             : 
    3745         451 :     default:
    3746         451 :       av = avma; if (!(y = toser_i(x))) break;
    3747         504 :       if (gequal0(y)) { *s = gerepilecopy(av,y); *c = gaddsg(1,*s); return; }
    3748             : 
    3749         504 :       ex = valser(y); lx = lg(y); ex2 = 2*ex+2;
    3750         504 :       if (ex < 0) pari_err_DOMAIN("gsincos","valuation", "<", gen_0, x);
    3751         504 :       if (ex2 > lx)
    3752             :       {
    3753          98 :         *s = x == y? gcopy(y): gerepilecopy(av, y); av = avma;
    3754          98 :         *c = gerepileupto(av, gsubsg(1, gdivgu(gsqr(y),2)));
    3755          98 :         return;
    3756             :       }
    3757         406 :       if (!ex)
    3758             :       {
    3759         105 :         gsincos(serchop0(y),&u,&v,prec);
    3760         105 :         gsincos(gel(y,2),&u1,&v1,prec);
    3761         105 :         p1 = gmul(v1,v);
    3762         105 :         p2 = gmul(u1,u);
    3763         105 :         p3 = gmul(v1,u);
    3764         105 :         p4 = gmul(u1,v); tetpil = avma;
    3765         105 :         *c = gsub(p1,p2);
    3766         105 :         *s = gadd(p3,p4);
    3767         105 :         gptr[0]=s; gptr[1]=c;
    3768         105 :         gerepilemanysp(av,tetpil,gptr,2);
    3769         105 :         return;
    3770             :       }
    3771             : 
    3772         301 :       ly = lx+2*ex;
    3773        2842 :       mi = lx-1; while (mi>=3 && isrationalzero(gel(y,mi))) mi--;
    3774         301 :       mi += ex-2;
    3775         301 :       pc = cgetg(ly,t_SER); *c = pc;
    3776         301 :       ps = cgetg(lx,t_SER); *s = ps;
    3777         301 :       pc[1] = evalsigne(1) | _evalvalser(0) | evalvarn(varn(y));
    3778         301 :       gel(pc,2) = gen_1; ps[1] = y[1];
    3779         609 :       for (i=2; i<ex+2; i++) gel(ps,i) = gcopy(gel(y,i));
    3780         616 :       for (i=3; i< ex2; i++) gel(pc,i) = gen_0;
    3781        3339 :       for (i=ex2; i<ly; i++)
    3782             :       {
    3783        3038 :         long ii = i-ex;
    3784        3038 :         av = avma; p1 = gen_0;
    3785        7028 :         for (j=ex; j<=minss(ii-2,mi); j++)
    3786        3990 :           p1 = gadd(p1, gmulgu(gmul(gel(y,j-ex+2),gel(ps,ii-j)),j));
    3787        3038 :         gel(pc,i) = gerepileupto(av, gdivgs(p1,2-i));
    3788        3038 :         if (ii < lx)
    3789             :         {
    3790        2730 :           av = avma; p1 = gen_0;
    3791        5796 :           for (j=ex; j<=minss(i-ex2,mi); j++)
    3792        3066 :             p1 = gadd(p1,gmulgu(gmul(gel(y,j-ex+2),gel(pc,i-j)),j));
    3793        2730 :           p1 = gdivgu(p1,i-2);
    3794        2730 :           gel(ps,ii) = gerepileupto(av, gadd(p1,gel(y,ii)));
    3795             :         }
    3796             :       }
    3797         301 :       return;
    3798             :   }
    3799           0 :   pari_err_TYPE("gsincos",x);
    3800             : }
    3801             : 
    3802             : /********************************************************************/
    3803             : /**                                                                **/
    3804             : /**                              SINC                              **/
    3805             : /**                                                                **/
    3806             : /********************************************************************/
    3807             : static GEN
    3808     2428754 : mpsinc(GEN x)
    3809             : {
    3810     2428754 :   pari_sp av = avma;
    3811             :   GEN s, c;
    3812             : 
    3813     2428754 :   if (!signe(x)) {
    3814           0 :     long l = nbits2prec(-expo(x));
    3815           0 :     if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
    3816           0 :     return real_1(l);
    3817             :   }
    3818             : 
    3819     2428754 :   mpsincos(x,&s,&c);
    3820     2428754 :   return gerepileuptoleaf(av, divrr(s,x));
    3821             : }
    3822             : 
    3823             : GEN
    3824     2428866 : gsinc(GEN x, long prec)
    3825             : {
    3826             :   pari_sp av;
    3827             :   GEN r, u, v, y, u1, v1;
    3828             :   long i;
    3829             : 
    3830     2428866 :   switch(typ(x))
    3831             :   {
    3832     2428733 :     case t_REAL: return mpsinc(x);
    3833          49 :     case t_COMPLEX:
    3834          49 :       if (isintzero(gel(x,1)))
    3835             :       {
    3836          28 :         av = avma; x = gel(x,2);
    3837          28 :         if (gequal0(x)) return gcosh(x,prec);
    3838          14 :         return gerepileuptoleaf(av,gdiv(gsinh(x,prec),x));
    3839             :       }
    3840          21 :       i = precision(x); if (i) prec = i;
    3841          21 :       y = cgetc(prec); av = avma;
    3842          21 :       r = gexp(gel(x,2),prec);
    3843          21 :       v1 = gmul2n(addrr(invr(r),r), -1); /* = cos(I*Im(x)) */
    3844          21 :       u1 = subrr(r, v1); /* = I*sin(I*Im(x)) */
    3845          21 :       gsincos(gel(x,1),&u,&v,prec);
    3846          21 :       affc_fixlg(gdiv(mkcomplex(gmul(v1,u), gmul(u1,v)), x), y);
    3847          21 :       return gc_const(av,y);
    3848             : 
    3849          14 :     case t_INT:
    3850          14 :       if (!signe(x)) return real_1(prec); /*fall through*/
    3851             :     case t_FRAC:
    3852          21 :       y = cgetr(prec); av = avma;
    3853          21 :       affrr_fixlg(mpsinc(tofp_safe(x,prec)), y); return gc_const(av,y);
    3854             : 
    3855          21 :     case t_PADIC:
    3856          21 :       if (gequal0(x)) return cvtop(gen_1, gel(x,2), valp(x));
    3857          14 :       av = avma; y = sin_p(x);
    3858          14 :       if (!y) pari_err_DOMAIN("gsinc(t_PADIC)","argument","",gen_0,x);
    3859           7 :       return gerepileupto(av,gdiv(y,x));
    3860             : 
    3861          35 :     default:
    3862             :     {
    3863             :       long ex;
    3864          35 :       av = avma; if (!(y = toser_i(x))) break;
    3865          35 :       if (gequal0(y)) return gerepileupto(av, gaddsg(1,y));
    3866          35 :       ex = valser(y);
    3867          35 :       if (ex < 0) pari_err_DOMAIN("sinc","valuation", "<", gen_0, x);
    3868          28 :       if (ex)
    3869             :       {
    3870          28 :         gsincos(y,&u,&v,prec);
    3871          28 :         y = gerepileupto(av, gdiv(u,y));
    3872          28 :         if (lg(y) > 2) gel(y,2) = gen_1;
    3873          28 :         return y;
    3874             :       }
    3875             :       else
    3876             :       {
    3877           0 :         GEN z0, y0 = gel(y,2), y1 = serchop0(y), y10 = y1;
    3878           0 :         if (!gequal1(y0)) y10 = gdiv(y10, y0);
    3879           0 :         gsincos(y1,&u,&v,prec);
    3880           0 :         z0 = gdiv(gcos(y0,prec), y0);
    3881           0 :         y = gaddsg(1, y10);
    3882           0 :         u = gadd(gmul(gsinc(y0, prec),v), gmul(z0, u));
    3883           0 :         return gerepileupto(av,gdiv(u,y));
    3884             :       }
    3885             :     }
    3886             :   }
    3887           0 :   return trans_eval("sinc",gsinc,x,prec);
    3888             : }
    3889             : 
    3890             : /********************************************************************/
    3891             : /**                                                                **/
    3892             : /**                     TANGENT and COTANGENT                      **/
    3893             : /**                                                                **/
    3894             : /********************************************************************/
    3895             : static GEN
    3896         133 : mptan(GEN x)
    3897             : {
    3898         133 :   pari_sp av = avma;
    3899             :   GEN s, c;
    3900             : 
    3901         133 :   mpsincos(x,&s,&c);
    3902         133 :   if (!signe(c))
    3903           0 :     pari_err_DOMAIN("tan", "argument", "=", strtoGENstr("Pi/2 + kPi"),x);
    3904         133 :   return gerepileuptoleaf(av, divrr(s,c));
    3905             : }
    3906             : 
    3907             : /* If exp(-|im(x)|) << 1, avoid overflow in sincos(x) */
    3908             : static int
    3909        4018 : tan_huge_im(GEN ix, long prec)
    3910             : {
    3911        4018 :   long b, p = precision(ix);
    3912        4018 :   if (!p) p = prec;
    3913        4018 :   b = bit_accuracy(p);
    3914        4018 :   return (gexpo(ix) > b || fabs(gtodouble(ix)) > (M_LN2 / 2) * b);
    3915             : }
    3916             : /* \pm I */
    3917             : static GEN
    3918          35 : real_I(long s, long prec)
    3919             : {
    3920          35 :   GEN z = cgetg(3, t_COMPLEX);
    3921          35 :   gel(z,1) = real_0(prec);
    3922          35 :   gel(z,2) = s > 0? real_1(prec): real_m1(prec); return z;
    3923             : }
    3924             : 
    3925             : GEN
    3926         217 : gtan(GEN x, long prec)
    3927             : {
    3928             :   pari_sp av;
    3929             :   GEN y, s, c;
    3930             : 
    3931         217 :   switch(typ(x))
    3932             :   {
    3933         126 :     case t_REAL: return mptan(x);
    3934             : 
    3935          42 :     case t_COMPLEX: {
    3936          42 :       if (isintzero(gel(x,1))) retmkcomplex(gen_0,gtanh(gel(x,2),prec));
    3937          28 :       if (tan_huge_im(gel(x,2), prec)) return real_I(gsigne(gel(x,2)), prec);
    3938          14 :       av = avma; y = mulcxmI(gtanh(mulcxI(x), prec)); /* tan x = -I th(I x) */
    3939          14 :       gel(y,1) = gcopy(gel(y,1)); return gerepileupto(av, y);
    3940             :     }
    3941           7 :     case t_INT: case t_FRAC:
    3942           7 :       y = cgetr(prec); av = avma;
    3943           7 :       affrr_fixlg(mptan(tofp_safe(x,prec)), y); return gc_const(av,y);
    3944             : 
    3945          14 :     case t_PADIC:
    3946          14 :       av = avma;
    3947          14 :       return gerepileupto(av, gdiv(gsin(x,prec), gcos(x,prec)));
    3948             : 
    3949          28 :     default:
    3950          28 :       av = avma; if (!(y = toser_i(x))) break;
    3951          21 :       if (gequal0(y)) return gerepilecopy(av, y);
    3952          21 :       if (valser(y) < 0)
    3953           7 :         pari_err_DOMAIN("tan","valuation", "<", gen_0, x);
    3954          14 :       gsincos(y,&s,&c,prec);
    3955          14 :       return gerepileupto(av, gdiv(s,c));
    3956             :   }
    3957           7 :   return trans_eval("tan",gtan,x,prec);
    3958             : }
    3959             : 
    3960             : static GEN
    3961          70 : mpcotan(GEN x)
    3962             : {
    3963          70 :   pari_sp av=avma, tetpil;
    3964             :   GEN s,c;
    3965             : 
    3966          70 :   mpsincos(x,&s,&c); tetpil=avma;
    3967          70 :   return gerepile(av,tetpil,divrr(c,s));
    3968             : }
    3969             : 
    3970             : GEN
    3971        4207 : gcotan(GEN x, long prec)
    3972             : {
    3973             :   pari_sp av;
    3974             :   GEN y, s, c;
    3975             : 
    3976        4207 :   switch(typ(x))
    3977             :   {
    3978          63 :     case t_REAL:
    3979          63 :       return mpcotan(x);
    3980             : 
    3981        4011 :     case t_COMPLEX:
    3982        4011 :       if (isintzero(gel(x,1))) {
    3983          21 :         GEN z = cgetg(3, t_COMPLEX);
    3984          21 :         gel(z,1) = gen_0; av = avma;
    3985          21 :         gel(z,2) = gerepileupto(av, gneg(ginv(gtanh(gel(x,2),prec))));
    3986          21 :         return z;
    3987             :       }
    3988        3990 :       if (tan_huge_im(gel(x,2), prec)) return real_I(-gsigne(gel(x,2)), prec);
    3989        3969 :       av = avma; gsincos(x,&s,&c,prec);
    3990        3969 :       return gerepileupto(av, gdiv(c,s));
    3991             : 
    3992           7 :     case t_INT: case t_FRAC:
    3993           7 :       y = cgetr(prec); av = avma;
    3994           7 :       affrr_fixlg(mpcotan(tofp_safe(x,prec)), y); return gc_const(av,y);
    3995             : 
    3996          14 :     case t_PADIC:
    3997          14 :       av = avma;
    3998          14 :       return gerepileupto(av, gdiv(gcos(x,prec), gsin(x,prec)));
    3999             : 
    4000         112 :     default:
    4001         112 :       av = avma; if (!(y = toser_i(x))) break;
    4002         105 :       if (gequal0(y)) pari_err_DOMAIN("cotan", "argument", "=", gen_0, y);
    4003         105 :       if (valser(y) < 0) pari_err_DOMAIN("cotan","valuation", "<", gen_0, x);
    4004          98 :       gsincos(y,&s,&c,prec);
    4005          98 :       return gerepileupto(av, gdiv(c,s));
    4006             :   }
    4007           7 :   return trans_eval("cotan",gcotan,x,prec);
    4008             : }

Generated by: LCOV version 1.14