Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - buch1.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22860-5579deb0b) Lines: 635 670 94.8 %
Date: 2018-07-18 05:36:42 Functions: 44 47 93.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /*******************************************************************/
      17             : /*                                                                 */
      18             : /*         CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN)          */
      19             : /*                   QUADRATIC FIELDS                              */
      20             : /*                                                                 */
      21             : /*******************************************************************/
      22             : /* For largeprime() hashtable. Note that hashed pseudoprimes are odd (unless
      23             :  * 2 | index), hence the low order bit is not useful. So we hash
      24             :  * HASHBITS bits starting at bit 1, not bit 0 */
      25             : #define HASHBITS 10
      26             : static const long HASHT = 1L << HASHBITS;
      27             : 
      28             : static long
      29     1557376 : hash(long q) { return (q & ((1L << (HASHBITS+1)) - 1)) >> 1; }
      30             : #undef HASHBITS
      31             : 
      32             : /* See buch2.c:
      33             :  * B->subFB contains split p such that \prod p > sqrt(B->Disc)
      34             :  * B->powsubFB contains powers of forms in B->subFB */
      35             : #define RANDOM_BITS 4
      36             : static const long CBUCH = (1L<<RANDOM_BITS)-1;
      37             : 
      38             : struct buch_quad
      39             : {
      40             :   ulong limhash;
      41             :   long KC, KC2, PRECREG;
      42             :   long *primfact, *exprimfact, **hashtab;
      43             :   GEN FB, numFB;
      44             :   GEN powsubFB, vperm, subFB, badprim;
      45             :   struct qfr_data *QFR;
      46             : };
      47             : 
      48             : /*******************************************************************/
      49             : /*                                                                 */
      50             : /*  Routines related to binary quadratic forms (for internal use)  */
      51             : /*                                                                 */
      52             : /*******************************************************************/
      53             : /* output canonical representative wrt projection Cl^+ --> Cl (a > 0) */
      54             : static GEN
      55     1164331 : qfr3_canon(GEN x, struct qfr_data *S)
      56             : {
      57     1164331 :   GEN a = gel(x,1), c = gel(x,3);
      58     1164331 :   if (signe(a) < 0) {
      59      502173 :     if (absequalii(a,c)) return qfr3_rho(x, S);
      60      502166 :     setsigne(a, 1);
      61      502166 :     setsigne(c,-1);
      62             :   }
      63     1164324 :   return x;
      64             : }
      65             : static GEN
      66        3710 : qfr3_canon_safe(GEN x, struct qfr_data *S)
      67             : {
      68        3710 :   GEN a = gel(x,1), c = gel(x,3);
      69        3710 :   if (signe(a) < 0) {
      70         189 :     if (absequalii(a,c)) return qfr3_rho(x, S);
      71         189 :     gel(x,1) = negi(a);
      72         189 :     gel(x,3) = negi(c);
      73             :   }
      74        3710 :   return x;
      75             : }
      76             : static GEN
      77     1947281 : qfr5_canon(GEN x, struct qfr_data *S)
      78             : {
      79     1947281 :   GEN a = gel(x,1), c = gel(x,3);
      80     1947281 :   if (signe(a) < 0) {
      81      868427 :     if (absequalii(a,c)) return qfr5_rho(x, S);
      82      868420 :     setsigne(a, 1);
      83      868420 :     setsigne(c,-1);
      84             :   }
      85     1947274 :   return x;
      86             : }
      87             : static GEN
      88     1730337 : QFR5_comp(GEN x,GEN y, struct qfr_data *S)
      89     1730337 : { return qfr5_canon(qfr5_comp(x,y,S), S); }
      90             : static GEN
      91     1005354 : QFR3_comp(GEN x, GEN y, struct qfr_data *S)
      92     1005354 : { return qfr3_canon(qfr3_comp(x,y,S), S); }
      93             : 
      94             : /* compute rho^n(x) */
      95             : static GEN
      96      220584 : qfr5_rho_pow(GEN x, long n, struct qfr_data *S)
      97             : {
      98             :   long i;
      99      220584 :   pari_sp av = avma;
     100     2216151 :   for (i=1; i<=n; i++)
     101             :   {
     102     1995567 :     x = qfr5_rho(x,S);
     103     1995567 :     if (gc_needed(av,1))
     104             :     {
     105           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"qfr5_rho_pow");
     106           0 :       x = gerepilecopy(av, x);
     107             :     }
     108             :   }
     109      220584 :   return gerepilecopy(av, x);
     110             : }
     111             : 
     112             : static GEN
     113      216944 : qfr5_pf(struct qfr_data *S, long p, long prec)
     114             : {
     115      216944 :   GEN y = primeform_u(S->D,p);
     116      216944 :   return qfr5_canon(qfr5_red(qfr_to_qfr5(y,prec), S), S);
     117             : }
     118             : 
     119             : static GEN
     120      158956 : qfr3_pf(struct qfr_data *S, long p)
     121             : {
     122      158956 :   GEN y = primeform_u(S->D,p);
     123      158956 :   return qfr3_canon(qfr3_red(y, S), S);
     124             : }
     125             : 
     126             : #define qfi_pf primeform_u
     127             : 
     128             : /* Warning: ex[0] not set in general */
     129             : static GEN
     130     1804104 : init_form(struct buch_quad *B, GEN ex,
     131             :           GEN (*comp)(GEN,GEN,struct qfr_data *S))
     132             : {
     133     1804104 :   long i, l = lg(B->powsubFB);
     134     1804104 :   GEN F = NULL;
     135    12716767 :   for (i=1; i<l; i++)
     136    10912663 :     if (ex[i])
     137             :     {
     138    10231255 :       GEN t = gmael(B->powsubFB,i,ex[i]);
     139    10231255 :       F = F? comp(F, t, B->QFR): t;
     140             :     }
     141     1804104 :   return F;
     142             : }
     143             : static GEN
     144      197442 : qfr5_factorback(struct buch_quad *B, GEN ex) { return init_form(B, ex, &QFR5_comp); }
     145             : 
     146             : static GEN
     147     6544242 : QFI_comp(GEN x, GEN y, struct qfr_data *S) { (void)S; return qficomp(x,y); }
     148             : 
     149             : static GEN
     150       47624 : qfi_factorback(struct buch_quad *B, GEN ex) { return init_form(B, ex, &QFI_comp); }
     151             : 
     152             : static GEN
     153     1558926 : random_form(struct buch_quad *B, GEN ex,
     154             :             GEN (*comp)(GEN,GEN, struct qfr_data *S))
     155             : {
     156     1558926 :   long i, l = lg(ex);
     157     1558926 :   pari_sp av = avma;
     158             :   GEN F;
     159             :   for(;;)
     160             :   {
     161     1559150 :     for (i=1; i<l; i++) ex[i] = random_bits(RANDOM_BITS);
     162     3117964 :     if ((F = init_form(B, ex, comp))) return F;
     163         112 :     avma = av;
     164             :   }
     165             : }
     166             : static GEN
     167      161133 : qfr3_random(struct buch_quad *B,GEN ex){ return random_form(B, ex, &QFR3_comp); }
     168             : static GEN
     169     1397793 : qfi_random(struct buch_quad *B,GEN ex) { return random_form(B, ex, &QFI_comp); }
     170             : 
     171             : /*******************************************************************/
     172             : /*                                                                 */
     173             : /*                     Common subroutines                          */
     174             : /*                                                                 */
     175             : /*******************************************************************/
     176             : long
     177         350 : bnf_increase_LIMC(long LIMC, long LIMCMAX)
     178             : {
     179         350 :   if (LIMC >= LIMCMAX) pari_err_BUG("Buchmann's algorithm");
     180         350 :   if (LIMC <= LIMCMAX/40) /* cbach <= 0.3 */
     181          28 :     LIMC *= 2;
     182         322 :   else if (LIMCMAX < 60) /* \Delta_K <= 9 */
     183           0 :     LIMC++;
     184             :   else
     185         322 :     LIMC += LIMCMAX / 60; /* cbach += 0.2 */
     186         350 :   if (LIMC > LIMCMAX) LIMC = LIMCMAX;
     187         350 :   return LIMC;
     188             : }
     189             : 
     190             : /* Is |q| <= p ? */
     191             : static int
     192       73056 : isless_iu(GEN q, ulong p) {
     193       73056 :   long l = lgefint(q);
     194       73056 :   return l==2 || (l == 3 && uel(q,2) <= p);
     195             : }
     196             : 
     197             : static long
     198     2779302 : factorquad(struct buch_quad *B, GEN f, long nFB, ulong limp)
     199             : {
     200             :   ulong X;
     201     2779302 :   long i, lo = 0;
     202     2779302 :   GEN x = gel(f,1), FB = B->FB, P = B->primfact, E = B->exprimfact;
     203             : 
     204     5703932 :   for (i=1; lgefint(x) > 3; i++)
     205             :   {
     206       73056 :     ulong p = uel(FB,i), r;
     207       73056 :     GEN q = absdiviu_rem(x, p, &r);
     208       73056 :     if (!r)
     209             :     {
     210        3822 :       long k = 0;
     211        6154 :       do { k++; x = q; q = absdiviu_rem(x, p, &r); } while (!r);
     212        3822 :       lo++; P[lo] = p; E[lo] = k;
     213             :     }
     214       73056 :     if (isless_iu(q,p)) {
     215           1 :       if (lgefint(x) == 3) { X = uel(x,2); goto END; }
     216         391 :       return 0;
     217             :     }
     218       73055 :     if (i == nFB) return 0;
     219             :   }
     220     2778910 :   X = uel(x,2);
     221     2778910 :   if (X == 1) { P[0] = 0; return 1; }
     222   116522086 :   for (;; i++)
     223   116522086 :   { /* single precision affair, split for efficiency */
     224   119294069 :     ulong p = uel(FB,i);
     225   119294069 :     ulong q = X / p, r = X % p; /* gcc makes a single div */
     226   119294069 :     if (!r)
     227             :     {
     228     4382025 :       long k = 0;
     229     5324811 :       do { k++; X = q; q = X / p; r = X % p; } while (!r);
     230     4382025 :       lo++; P[lo] = p; E[lo] = k;
     231             :     }
     232   119294069 :     if (q <= p) break;
     233   117193954 :     if (i == nFB) return 0;
     234             :   }
     235             : END:
     236     2100116 :   if (X > B->limhash) return 0;
     237     2100088 :   if (X != 1 && X <= limp)
     238             :   {
     239      419519 :     if (B->badprim && ugcdui(X, B->badprim) > 1) return 0;
     240      418768 :     lo++; P[lo] = X; E[lo] = 1;
     241      418768 :     X = 1;
     242             :   }
     243     2099337 :   P[0] = lo; return X;
     244             : }
     245             : 
     246             : /* Check for a "large prime relation" involving q; q may not be prime */
     247             : static long *
     248     1557376 : largeprime(struct buch_quad *B, long q, GEN ex, long np, long nrho)
     249             : {
     250     1557376 :   const long hashv = hash(q);
     251     1557376 :   long *pt, i, l = lg(B->subFB);
     252             : 
     253     1777967 :   for (pt = B->hashtab[hashv]; ; pt = (long*) pt[0])
     254             :   {
     255     1998558 :     if (!pt)
     256             :     {
     257     1469747 :       pt = (long*) pari_malloc((l+3) * sizeof(long));
     258     1469747 :       *pt++ = nrho; /* nrho = pt[-3] */
     259     1469747 :       *pt++ = np;   /* np   = pt[-2] */
     260     1469747 :       *pt++ = q;    /* q    = pt[-1] */
     261     1469747 :       pt[0] = (long)B->hashtab[hashv];
     262     1469747 :       for (i=1; i<l; i++) pt[i]=ex[i];
     263     1469747 :       B->hashtab[hashv]=pt; return NULL;
     264             :     }
     265      308220 :     if (pt[-1] == q) break;
     266             :   }
     267      122717 :   for(i=1; i<l; i++)
     268      119042 :     if (pt[i] != ex[i]) return pt;
     269        3675 :   return (pt[-2]==np)? NULL: pt;
     270             : }
     271             : 
     272             : static void
     273       39625 : clearhash(long **hash)
     274             : {
     275             :   long *pt;
     276             :   long i;
     277    40615625 :   for (i=0; i<HASHT; i++) {
     278    82621747 :     for (pt = hash[i]; pt; ) {
     279     1469747 :       void *z = (void*)(pt - 3);
     280     1469747 :       pt = (long*) pt[0]; pari_free(z);
     281             :     }
     282    40576000 :     hash[i] = NULL;
     283             :   }
     284       39625 : }
     285             : 
     286             : /* last prime stored */
     287             : ulong
     288       16274 : GRH_last_prime(GRHcheck_t *S) { return (S->primes + S->nprimes-1)->p; }
     289             : /* ensure that S->primes can hold at least nb primes */
     290             : void
     291       56473 : GRH_ensure(GRHcheck_t *S, long nb)
     292             : {
     293       56473 :   if (S->maxprimes <= nb)
     294             :   {
     295           0 :     do S->maxprimes *= 2; while (S->maxprimes <= nb);
     296           0 :     S->primes = (GRHprime_t*)pari_realloc((void*)S->primes,
     297           0 :                                           S->maxprimes*sizeof(*S->primes));
     298             :   }
     299       56473 : }
     300             : /* cache data for all primes up to the LIM */
     301             : static void
     302      250311 : cache_prime_quad(GRHcheck_t *S, ulong LIM, GEN D)
     303             : {
     304             :   GRHprime_t *pr;
     305             :   long nb;
     306             : 
     307      250311 :   if (S->limp >= LIM) return;
     308       17870 :   nb = (long)primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     309       17870 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     310       17870 :   for (pr = S->primes + S->nprimes;;)
     311     2208196 :   {
     312     2226066 :     ulong p = u_forprime_next(&(S->P));
     313     2226066 :     pr->p = p;
     314     2226066 :     pr->logp = log((double)p);
     315     2226066 :     pr->dec = (GEN)kroiu(D,p);
     316     2226066 :     S->nprimes++;
     317     2226066 :     pr++;
     318             :     /* store up to nextprime(LIM) included */
     319     2226066 :     if (p >= LIM) { S->limp = p; break; }
     320             :   }
     321             : }
     322             : 
     323             : static GEN
     324       16274 : compute_invresquad(GRHcheck_t *S)
     325             : {
     326       16274 :   pari_sp av = avma;
     327       16274 :   GEN invres = real_1(DEFAULTPREC);
     328       16274 :   GRHprime_t *pr = S->primes;
     329       16274 :   long i = S->nprimes, LIMC = GRH_last_prime(S)+diffptr[i]-1; /* nextprime(p+1)-1*/
     330       16274 :   double limp = log((double)LIMC) / 2;
     331     2242340 :   for (; i > 0; pr++, i--)
     332             :   {
     333     2226066 :     long s = (long)pr->dec;
     334     2226066 :     if (s)
     335             :     {
     336     2197684 :       ulong p = pr->p;
     337     2197684 :       if (s>0 || pr->logp <= limp)
     338             :         /* Both p and P contribute */
     339     1150112 :         invres = mulur(p - s, divru(invres, p));
     340     1047572 :       else if (s<0)
     341             :         /* Only p contributes */
     342     1047572 :         invres = mulur(p, divru(invres, p - 1));
     343             :     }
     344             :   }
     345       16274 :   return gerepileuptoleaf(av, invres);
     346             : }
     347             : 
     348             : /* p | conductor of order of disc D ? */
     349             : static int
     350       80907 : is_bad(GEN D, ulong p)
     351             : {
     352       80907 :   pari_sp av = avma;
     353             :   int r;
     354       80907 :   if (p == 2)
     355             :   {
     356       21636 :     r = mod16(D) >> 1;
     357       21636 :     if (r && signe(D) < 0) r = 8-r;
     358       21636 :     return (r < 4);
     359             :   }
     360       59271 :   r = dvdii(D, sqru(p)); /* p^2 | D ? */
     361       59271 :   avma = av; return r;
     362             : }
     363             : 
     364             : /* returns the n-th suitable ideal for the factorbase */
     365             : static long
     366       16274 : nthidealquad(GEN D, long n)
     367             : {
     368       16274 :   pari_sp av = avma;
     369             :   forprime_t S;
     370             :   ulong p;
     371       16274 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     372       88481 :   while ((p = u_forprime_next(&S)) && n > 0)
     373       55933 :     if (!is_bad(D, p) && kroiu(D, p) >= 0) n--;
     374       16274 :   avma = av; return p;
     375             : }
     376             : 
     377             : static int
     378      217588 : quadGRHchk(GEN D, GRHcheck_t *S, ulong LIMC)
     379             : {
     380      217588 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     381             :   long i;
     382             : 
     383      217588 :   cache_prime_quad(S, LIMC, D);
     384    10531147 :   for (i = 0;; i++)
     385    10313559 :   {
     386    10531147 :     GRHprime_t *pr = S->primes+i;
     387    10531147 :     ulong p = pr->p;
     388             :     long M;
     389             :     double logNP, q, A, B;
     390    10531147 :     if (p > LIMC) break;
     391    10336049 :     if ((long)pr->dec < 0)
     392             :     {
     393     5079042 :       logNP = 2 * pr->logp;
     394     5079042 :       q = 1/(double)p;
     395             :     }
     396             :     else
     397             :     {
     398     5257007 :       logNP = pr->logp;
     399     5257007 :       q = 1/sqrt((double)p);
     400             :     }
     401    10336049 :     A = logNP * q; B = logNP * A; M = (long)(logC/logNP);
     402    10336049 :     if (M > 1)
     403             :     {
     404      614351 :       double inv1_q = 1 / (1-q);
     405      614351 :       A *= (1 - pow(q, (double) M)) * inv1_q;
     406      614351 :       B *= (1 - pow(q, (double) M)*(M+1 - M*q)) * inv1_q * inv1_q;
     407             :     }
     408    10336049 :     if ((long)pr->dec>0) { SA += 2*A;SB += 2*B; } else { SA += A; SB += B; }
     409    10336049 :     if (p == LIMC) break;
     410             :   }
     411      217588 :   return GRHok(S, logC, SA, SB);
     412             : }
     413             : 
     414             : /* C2 >= C1; create B->FB, B->numFB; set B->badprim. Return L(kro_D, 1) */
     415             : static void
     416       16449 : FBquad(struct buch_quad *B, ulong C2, ulong C1, GRHcheck_t *S)
     417             : {
     418       16449 :   GEN D = B->QFR->D;
     419             :   long i;
     420             :   pari_sp av;
     421             :   GRHprime_t *pr;
     422             : 
     423       16449 :   cache_prime_quad(S, C2, D);
     424       16449 :   pr = S->primes;
     425       16449 :   B->numFB = cgetg(C2+1, t_VECSMALL);
     426       16449 :   B->FB    = cgetg(C2+1, t_VECSMALL);
     427       16449 :   av = avma;
     428       16449 :   B->KC = 0; i = 0;
     429       16449 :   B->badprim = gen_1;
     430      896937 :   for (;; pr++) /* p <= C2 */
     431      896937 :   {
     432      913386 :     ulong p = pr->p;
     433      913386 :     if (!B->KC && p > C1) B->KC = i;
     434      913386 :     if (p > C2) break;
     435      898206 :     switch ((long)pr->dec)
     436             :     {
     437      436726 :       case -1: break; /* inert */
     438             :       case  0: /* ramified */
     439       24974 :         if (is_bad(D, p)) { B->badprim = muliu(B->badprim, p); break; }
     440             :         /* fall through */
     441             :       default:  /* split */
     442      461256 :         i++; B->numFB[p] = i; B->FB[i] = p; break;
     443             :     }
     444      898206 :     if (p == C2)
     445             :     {
     446        1269 :       if (!B->KC) B->KC = i;
     447        1269 :       break;
     448             :     }
     449             :   }
     450       16449 :   B->KC2 = i;
     451       16449 :   setlg(B->FB, B->KC2+1);
     452       16449 :   if (B->badprim != gen_1)
     453         196 :     B->badprim = gerepileuptoint(av, B->badprim);
     454             :   else
     455             :   {
     456       16253 :     B->badprim = NULL; avma = av;
     457             :   }
     458       16449 : }
     459             : 
     460             : /* create B->vperm, return B->subFB */
     461             : static GEN
     462       16449 : subFBquad(struct buch_quad *B, GEN D, double PROD, long minSFB)
     463             : {
     464       16449 :   long i, j, lgsub = 1, ino = 1, lv = B->KC+1;
     465       16449 :   double prod = 1.;
     466             :   pari_sp av;
     467             :   GEN no;
     468             : 
     469       16449 :   B->vperm = cgetg(lv, t_VECSMALL);
     470       16449 :   av = avma;
     471       16449 :   no    = cgetg(lv, t_VECSMALL);
     472       84037 :   for (j = 1; j < lv; j++)
     473             :   {
     474       83855 :     ulong p = uel(B->FB,j);
     475       83855 :     if (!umodiu(D, p)) no[ino++] = j; /* ramified */
     476             :     else
     477             :     {
     478       64706 :       B->vperm[lgsub++] = j;
     479       64706 :       prod *= p;
     480       64706 :       if (lgsub > minSFB && prod > PROD) break;
     481             :     }
     482             :   }
     483             :   /* lgsub >= 1 otherwise quadGRHchk is false */
     484       16449 :   i = lgsub;
     485       16449 :   for (j = 1; j < ino;i++,j++) B->vperm[i] = no[j];
     486       16449 :   for (     ; i < lv; i++)     B->vperm[i] = i;
     487       16449 :   no = gclone(vecslice(B->vperm, 1, lgsub-1));
     488       16449 :   avma = av; return no;
     489             : }
     490             : 
     491             : /* assume n >= 1, x[i][j] = B->subFB[i]^j, for j = 1..n */
     492             : static GEN
     493       23176 : powsubFBquad(struct buch_quad *B, long n)
     494             : {
     495       23176 :   pari_sp av = avma;
     496       23176 :   long i,j, l = lg(B->subFB);
     497       23176 :   GEN F, y, x = cgetg(l, t_VEC), D = B->QFR->D;
     498             : 
     499       23176 :   if (B->PRECREG) /* real */
     500             :   {
     501       39088 :     for (i=1; i<l; i++)
     502             :     {
     503       34055 :       F = qfr5_pf(B->QFR, B->FB[B->subFB[i]], B->PRECREG);
     504       34055 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     505       34055 :       gel(y,1) = F;
     506       34055 :       for (j=2; j<=n; j++) gel(y,j) = QFR5_comp(gel(y,j-1), F, B->QFR);
     507             :     }
     508             :   }
     509             :   else /* imaginary */
     510             :   {
     511       85373 :     for (i=1; i<l; i++)
     512             :     {
     513       67230 :       F = qfi_pf(D, B->FB[B->subFB[i]]);
     514       67230 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     515       67230 :       gel(y,1) = F;
     516       67230 :       for (j=2; j<=n; j++) gel(y,j) = qficomp(gel(y,j-1), F);
     517             :     }
     518             :   }
     519       23176 :   x = gclone(x); avma = av; return x;
     520             : }
     521             : 
     522             : static void
     523       41962 : sub_fact(struct buch_quad *B, GEN col, GEN F)
     524             : {
     525       41962 :   GEN b = gel(F,2);
     526             :   long i;
     527      128743 :   for (i=1; i<=B->primfact[0]; i++)
     528             :   {
     529       86781 :     ulong p = B->primfact[i], k = B->numFB[p];
     530       86781 :     long e = B->exprimfact[i];
     531       86781 :     if (umodiu(b, p<<1) > p) e = -e;
     532       86781 :     col[k] -= e;
     533             :   }
     534       41962 : }
     535             : static void
     536      590334 : add_fact(struct buch_quad *B, GEN col, GEN F)
     537             : {
     538      590334 :   GEN b = gel(F,2);
     539             :   long i;
     540     2296832 :   for (i=1; i<=B->primfact[0]; i++)
     541             :   {
     542     1706498 :     ulong p = B->primfact[i], k = B->numFB[p];
     543     1706498 :     long e = B->exprimfact[i];
     544     1706498 :     if (umodiu(b, p<<1) > p) e = -e;
     545     1706498 :     col[k] += e;
     546             :   }
     547      590334 : }
     548             : 
     549             : static GEN
     550       16274 : get_clgp(struct buch_quad *B, GEN W, GEN *ptD, long prec)
     551             : {
     552       16274 :   GEN res, init, u1, D = ZM_snf_group(W,NULL,&u1), Z = prec? real_0(prec): NULL;
     553       16274 :   long i, j, l = lg(W), c = lg(D);
     554             : 
     555       16274 :   res=cgetg(c,t_VEC); init = cgetg(l,t_VEC);
     556       16274 :   for (i=1; i<l; i++) gel(init,i) = primeform_u(B->QFR->D, B->FB[B->vperm[i]]);
     557       44587 :   for (j=1; j<c; j++)
     558             :   {
     559       28313 :     GEN g = NULL;
     560       28313 :     if (prec)
     561             :     {
     562       13293 :       for (i=1; i<l; i++)
     563             :       {
     564        9583 :         GEN t, u = gcoeff(u1,i,j);
     565        9583 :         if (!signe(u)) continue;
     566        4389 :         t = qfr3_pow(gel(init,i), u, B->QFR);
     567        4389 :         g = g? qfr3_comp(g, t, B->QFR): t;
     568             :       }
     569        3710 :       g = qfr3_to_qfr(qfr3_canon_safe(qfr3_red(g, B->QFR), B->QFR), Z);
     570             :     }
     571             :     else
     572             :     {
     573       86169 :       for (i=1; i<l; i++)
     574             :       {
     575       61566 :         GEN t, u = gcoeff(u1,i,j);
     576       61566 :         if (!signe(u)) continue;
     577       39251 :         t = powgi(gel(init,i), u);
     578       39251 :         g = g? qficomp(g, t): t;
     579             :       }
     580             :     }
     581       28313 :     gel(res,j) = g;
     582             :   }
     583       16274 :   *ptD = D; return res;
     584             : }
     585             : 
     586             : static long
     587       16274 : trivial_relations(struct buch_quad *B, GEN mat, GEN C)
     588             : {
     589       16274 :   long i, j = 0;
     590       16274 :   GEN col, D = B->QFR->D;
     591      476361 :   for (i = 1; i <= B->KC; i++)
     592             :   { /* ramified prime ==> trivial relation */
     593      460087 :     if (umodiu(D, B->FB[i])) continue;
     594       24260 :     col = zero_zv(B->KC);
     595       24260 :     col[i] = 2; j++;
     596       24260 :     gel(mat,j) = col;
     597       24260 :     gel(C,j) = gen_0;
     598             :   }
     599       16274 :   return j;
     600             : }
     601             : 
     602             : static void
     603           0 : dbg_all(pari_timer *T, const char *phase, long s, long n)
     604             : {
     605           0 :   err_printf("\n");
     606           0 :   timer_printf(T, "%s rel [#rel/#test = %ld/%ld]", phase,s,n);
     607           0 : }
     608             : 
     609             : /* Imaginary Quadratic fields */
     610             : 
     611             : static void
     612       18688 : imag_relations(struct buch_quad *B, long need, long *pc, ulong LIMC, GEN mat)
     613             : {
     614             :   pari_timer T;
     615       18688 :   long lgsub = lg(B->subFB), current = *pc, nbtest = 0, s = 0;
     616             :   long i, fpc;
     617             :   pari_sp av;
     618       18688 :   GEN col, form, ex = cgetg(lgsub, t_VECSMALL);
     619             : 
     620       18688 :   if (!current) current = 1;
     621       18688 :   if (DEBUGLEVEL>2) timer_start(&T);
     622       18688 :   av = avma;
     623             :   for(;;)
     624             :   {
     625     2813336 :     if (s >= need) break;
     626     1397324 :     avma = av;
     627     1397324 :     form = qfi_random(B,ex);
     628     1397324 :     form = qficomp(form, qfi_pf(B->QFR->D, B->FB[current]));
     629     1397324 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     630     1397324 :     if (!fpc)
     631             :     {
     632      287527 :       if (DEBUGLEVEL>3) err_printf(".");
     633      287527 :       if ((nbtest & 0xff) == 0 && ++current > B->KC) current = 1;
     634      287527 :       continue;
     635             :     }
     636     1109797 :     if (fpc > 1)
     637             :     {
     638      793263 :       long *fpd = largeprime(B,fpc,ex,current,0);
     639             :       ulong b1, b2, p;
     640             :       GEN form2;
     641      793263 :       if (!fpd)
     642             :       {
     643      745639 :         if (DEBUGLEVEL>3) err_printf(".");
     644      745639 :         continue;
     645             :       }
     646       47624 :       form2 = qficomp(qfi_factorback(B,fpd), qfi_pf(B->QFR->D, B->FB[fpd[-2]]));
     647       47624 :       p = fpc << 1;
     648       47624 :       b1 = umodiu(gel(form2,2), p);
     649       47624 :       b2 = umodiu(gel(form,2),  p);
     650       47624 :       if (b1 != b2 && b1+b2 != p) continue;
     651             : 
     652       47589 :       col = gel(mat,++s);
     653       47589 :       add_fact(B,col, form);
     654       47589 :       (void)factorquad(B,form2,B->KC,LIMC);
     655       47589 :       if (b1==b2)
     656             :       {
     657       23727 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     658       23727 :         sub_fact(B, col, form2); col[fpd[-2]]++;
     659             :       }
     660             :       else
     661             :       {
     662       23862 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     663       23862 :         add_fact(B, col, form2); col[fpd[-2]]--;
     664             :       }
     665       47589 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     666             :     }
     667             :     else
     668             :     {
     669      316534 :       col = gel(mat,++s);
     670      316534 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     671      316534 :       add_fact(B, col, form);
     672      316534 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     673             :     }
     674      364123 :     col[current]--;
     675      364123 :     if (++current > B->KC) current = 1;
     676             :   }
     677       18688 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     678       18688 :   *pc = current;
     679       18688 : }
     680             : 
     681             : static int
     682           7 : imag_be_honest(struct buch_quad *B)
     683             : {
     684           7 :   long p, fpc, s = B->KC, nbtest = 0;
     685           7 :   GEN F, ex = cgetg(lg(B->subFB), t_VECSMALL);
     686           7 :   pari_sp av = avma;
     687             : 
     688         483 :   while (s<B->KC2)
     689             :   {
     690         469 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     691         469 :     F = qficomp(qfi_pf(B->QFR->D, p), qfi_random(B, ex));
     692         469 :     fpc = factorquad(B,F,s,p-1);
     693         469 :     if (fpc == 1) { nbtest=0; s++; }
     694             :     else
     695         322 :       if (++nbtest > 40) return 0;
     696         469 :     avma = av;
     697             :   }
     698           7 :   return 1;
     699             : }
     700             : 
     701             : /* Real Quadratic fields */
     702             : 
     703             : static void
     704        5124 : real_relations(struct buch_quad *B, long need, long *pc, long lim, ulong LIMC, GEN mat, GEN C)
     705             : {
     706             :   pari_timer T;
     707        5124 :   long lgsub = lg(B->subFB), prec = B->PRECREG, current = *pc, nbtest=0, s=0;
     708             :   long i, fpc, endcycle, rhoacc, rho;
     709             :   /* in a 2nd phase, don't include FB[current] but run along the cyle
     710             :    * ==> get more units */
     711        5124 :   int first = (current == 0);
     712             :   pari_sp av, av1;
     713        5124 :   GEN d, col, form, form0, form1, ex = cgetg(lgsub, t_VECSMALL);
     714             : 
     715        5124 :   if (DEBUGLEVEL>2) timer_start(&T);
     716        5124 :   if (!current) current = 1;
     717        5124 :   if (lim > need) lim = need;
     718        5124 :   av = avma;
     719             :   for(;;)
     720             :   {
     721      327348 :     if (s >= need) break;
     722      161112 :     if (first && s >= lim) {
     723        2142 :       first = 0;
     724        2142 :       if (DEBUGLEVEL>2) dbg_all(&T, "initial", s, nbtest);
     725             :     }
     726      161112 :     avma = av; form = qfr3_random(B, ex);
     727      161112 :     if (!first)
     728      158935 :       form = QFR3_comp(form, qfr3_pf(B->QFR, B->FB[current]), B->QFR);
     729      161112 :     av1 = avma;
     730      161112 :     form0 = form; form1 = NULL;
     731      161112 :     endcycle = rhoacc = 0;
     732      161112 :     rho = -1;
     733             : 
     734             : CYCLE:
     735     1297569 :     if (endcycle || rho > 5000)
     736             :     {
     737          21 :       if (++current > B->KC) current = 1;
     738          21 :       continue;
     739             :     }
     740     1297548 :     if (gc_needed(av,1))
     741             :     {
     742           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"real_relations");
     743           0 :       gerepileall(av1, form1? 2: 1, &form, &form1);
     744             :     }
     745     1297548 :     if (rho < 0) rho = 0; /* first time in */
     746             :     else
     747             :     {
     748     1136436 :       form = qfr3_rho(form, B->QFR); rho++;
     749     1136436 :       rhoacc++;
     750     1136436 :       if (first)
     751      444430 :         endcycle = (absequalii(gel(form,1),gel(form0,1))
     752      222215 :              && equalii(gel(form,2),gel(form0,2)));
     753             :       else
     754             :       {
     755      914221 :         if (absequalii(gel(form,1), gel(form,3))) /* a = -c */
     756             :         {
     757           0 :           if (absequalii(gel(form,1),gel(form0,1)) &&
     758           0 :                   equalii(gel(form,2),gel(form0,2))) continue;
     759           0 :           form = qfr3_rho(form, B->QFR); rho++;
     760           0 :           rhoacc++;
     761             :         }
     762             :         else
     763      914221 :           { setsigne(form[1],1); setsigne(form[3],-1); }
     764      914277 :         if (equalii(gel(form,1),gel(form0,1)) &&
     765          56 :             equalii(gel(form,2),gel(form0,2))) continue;
     766             :       }
     767             :     }
     768     1297548 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     769     1297548 :     if (!fpc)
     770             :     {
     771      385511 :       if (DEBUGLEVEL>3) err_printf(".");
     772      385511 :       goto CYCLE;
     773             :     }
     774      912037 :     if (fpc > 1)
     775             :     { /* look for Large Prime relation */
     776      764113 :       long *fpd = largeprime(B,fpc,ex,first? 0: current,rhoacc);
     777             :       ulong b1, b2, p;
     778             :       GEN form2;
     779      764113 :       if (!fpd)
     780             :       {
     781      727783 :         if (DEBUGLEVEL>3) err_printf(".");
     782      727783 :         goto CYCLE;
     783             :       }
     784       36330 :       if (!form1)
     785             :       {
     786       36330 :         form1 = qfr5_factorback(B,ex);
     787       36330 :         if (!first)
     788       36330 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     789             :       }
     790       36330 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     791       36330 :       rho = 0;
     792             : 
     793       36330 :       form2 = qfr5_factorback(B,fpd);
     794       36330 :       if (fpd[-2])
     795       23954 :         form2 = QFR5_comp(form2, qfr5_pf(B->QFR, B->FB[fpd[-2]], prec), B->QFR);
     796       36330 :       form2 = qfr5_rho_pow(form2, fpd[-3], B->QFR);
     797       36330 :       if (!absequalii(gel(form2,1),gel(form2,3)))
     798             :       {
     799       36330 :         setsigne(form2[1], 1);
     800       36330 :         setsigne(form2[3],-1);
     801             :       }
     802       36330 :       p = fpc << 1;
     803       36330 :       b1 = umodiu(gel(form2,2), p);
     804       36330 :       b2 = umodiu(gel(form1,2), p);
     805       36330 :       if (b1 != b2 && b1+b2 != p) goto CYCLE;
     806             : 
     807       36330 :       col = gel(mat,++s);
     808       36330 :       add_fact(B, col, form1);
     809       36330 :       (void)factorquad(B,form2,B->KC,LIMC);
     810       36330 :       if (b1==b2)
     811             :       {
     812       18235 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     813       18235 :         sub_fact(B,col, form2);
     814       18235 :         if (fpd[-2]) col[fpd[-2]]++;
     815       36470 :         d = qfr5_dist(subii(gel(form1,4),gel(form2,4)),
     816       36470 :                       divrr(gel(form1,5),gel(form2,5)), prec);
     817             :       }
     818             :       else
     819             :       {
     820       18095 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     821       18095 :         add_fact(B, col, form2);
     822       18095 :         if (fpd[-2]) col[fpd[-2]]--;
     823       36190 :         d = qfr5_dist(addii(gel(form1,4),gel(form2,4)),
     824       36190 :                       mulrr(gel(form1,5),gel(form2,5)), prec);
     825             :       }
     826       36330 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     827             :     }
     828             :     else
     829             :     { /* standard relation */
     830      147924 :       if (!form1)
     831             :       {
     832      124782 :         form1 = qfr5_factorback(B, ex);
     833      124782 :         if (!first)
     834      122605 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     835             :       }
     836      147924 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     837      147924 :       rho = 0;
     838             : 
     839      147924 :       col = gel(mat,++s);
     840      147924 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     841      147924 :       add_fact(B, col, form1);
     842      147924 :       d = qfr5_dist(gel(form1,4), gel(form1,5), prec);
     843      147924 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     844             :     }
     845      184254 :     affrr(d, gel(C,s));
     846      184254 :     if (first)
     847             :     {
     848       25319 :       if (s >= lim) continue;
     849       23163 :       goto CYCLE;
     850             :     }
     851             :     else
     852             :     {
     853      158935 :       col[current]--;
     854      158935 :       if (++current > B->KC) current = 1;
     855             :     }
     856             :   }
     857        5124 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     858        5124 :   *pc = current;
     859        5124 : }
     860             : 
     861             : static int
     862           7 : real_be_honest(struct buch_quad *B)
     863             : {
     864           7 :   long p, fpc, s = B->KC, nbtest = 0;
     865           7 :   GEN F,F0, ex = cgetg(lg(B->subFB), t_VECSMALL);
     866           7 :   pari_sp av = avma;
     867             : 
     868          35 :   while (s<B->KC2)
     869             :   {
     870          21 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     871          21 :     F = QFR3_comp(qfr3_random(B, ex), qfr3_pf(B->QFR, p), B->QFR);
     872          21 :     for (F0 = F;;)
     873             :     {
     874          63 :       fpc = factorquad(B,F,s,p-1);
     875          42 :       if (fpc == 1) { nbtest=0; s++; break; }
     876          21 :       if (++nbtest > 40) return 0;
     877          21 :       F = qfr3_canon(qfr3_rho(F, B->QFR), B->QFR);
     878          21 :       if (equalii(gel(F,1),gel(F0,1))
     879           0 :        && equalii(gel(F,2),gel(F0,2))) break;
     880             :     }
     881          21 :     avma = av;
     882             :   }
     883           7 :   return 1;
     884             : }
     885             : 
     886             : static GEN
     887       32025 : gcdreal(GEN a,GEN b)
     888             : {
     889       32025 :   if (!signe(a)) return mpabs_shallow(b);
     890       31361 :   if (!signe(b)) return mpabs_shallow(a);
     891       31213 :   if (typ(a)==t_INT)
     892             :   {
     893           0 :     if (typ(b)==t_INT) return gcdii(a,b);
     894           0 :     a = itor(a, lg(b));
     895             :   }
     896       31213 :   else if (typ(b)==t_INT)
     897           0 :     b = itor(b, lg(a));
     898       31213 :   if (expo(a)<-5) return absr(b);
     899       11305 :   if (expo(b)<-5) return absr(a);
     900        8456 :   a = absr(a); b = absr(b);
     901       27135 :   while (expo(b) >= -5  && signe(b))
     902             :   {
     903             :     long e;
     904       10223 :     GEN r, q = gcvtoi(divrr(a,b),&e);
     905       10223 :     if (e > 0) return NULL;
     906       10223 :     r = subrr(a, mulir(q,b)); a = b; b = r;
     907             :   }
     908        8456 :   return mpabs_shallow(a);
     909             : }
     910             : 
     911             : static int
     912       19884 : get_R(struct buch_quad *B, GEN C, long sreg, GEN z, GEN *ptR)
     913             : {
     914       19884 :   GEN R = gen_1;
     915             :   double c;
     916             :   long i;
     917             : 
     918       19884 :   if (B->PRECREG)
     919             :   {
     920        2877 :     R = mpabs_shallow(gel(C,1));
     921       34902 :     for (i=2; i<=sreg; i++)
     922             :     {
     923       32025 :       R = gcdreal(gel(C,i), R);
     924       32025 :       if (!R) return fupb_PRECI;
     925             :     }
     926        2877 :     if (gexpo(R) <= -3)
     927             :     {
     928           0 :       if (DEBUGLEVEL>2) err_printf("regulator is zero.\n");
     929           0 :       return fupb_RELAT;
     930             :     }
     931        2877 :     if (DEBUGLEVEL>2) err_printf("#### Tentative regulator: %Ps\n",R);
     932             :   }
     933       19884 :   c = gtodouble(gmul(z, R));
     934       19884 :   if (c < 0.8 || c > 1.3) return fupb_RELAT;
     935       16274 :   *ptR = R; return fupb_NONE;
     936             : }
     937             : 
     938             : static int
     939       16274 : quad_be_honest(struct buch_quad *B)
     940             : {
     941             :   int r;
     942       16274 :   if (B->KC2 <= B->KC) return 1;
     943          14 :   if (DEBUGLEVEL>2)
     944           0 :     err_printf("be honest for primes from %ld to %ld\n", B->FB[B->KC+1],B->FB[B->KC2]);
     945          14 :   r = B->PRECREG? real_be_honest(B): imag_be_honest(B);
     946          14 :   if (DEBUGLEVEL>2) err_printf("\n");
     947          14 :   return r;
     948             : }
     949             : 
     950             : 
     951             : GEN
     952       16281 : Buchquad(GEN D, double cbach, double cbach2, long prec)
     953             : {
     954       16281 :   const long MAXRELSUP = 7, SFB_MAX = 3;
     955             :   pari_timer T;
     956       16281 :   pari_sp av0 = avma, av, av2;
     957       16281 :   const long RELSUP = 5;
     958             :   long i, s, current, triv, sfb_trials, nrelsup, nreldep, need, nsubFB, minSFB;
     959             :   ulong low, high, LIMC0, LIMC, LIMC2, LIMCMAX, cp;
     960       16281 :   GEN W, cyc, res, gen, dep, mat, C, extraC, B, R, invhr, h = NULL; /*-Wall*/
     961             :   double drc, sdrc, lim, LOGD, LOGD2;
     962             :   GRHcheck_t GRHcheck;
     963             :   struct qfr_data QFR;
     964             :   struct buch_quad BQ;
     965       16281 :   int FIRST = 1;
     966             : 
     967       16281 :   check_quaddisc(D, &s, /*junk*/&i, "Buchquad");
     968       16281 :   R = NULL; /* -Wall */
     969       16281 :   BQ.QFR = &QFR;
     970       16281 :   QFR.D = D;
     971       16281 :   if (s < 0)
     972             :   {
     973       14125 :     if (abscmpiu(QFR.D,4) <= 0)
     974             :     {
     975           7 :       GEN z = cgetg(5,t_VEC);
     976           7 :       gel(z,1) = gel(z,4) = gen_1; gel(z,2) = gel(z,3) = cgetg(1,t_VEC);
     977           7 :       return z;
     978             :     }
     979       14118 :     prec = BQ.PRECREG = 0;
     980             :   } else {
     981        2156 :     BQ.PRECREG = maxss(prec+EXTRAPRECWORD, nbits2prec(2*expi(QFR.D) + 128));
     982             :   }
     983       16274 :   if (DEBUGLEVEL>2) timer_start(&T);
     984       16274 :   BQ.primfact   = new_chunk(100);
     985       16274 :   BQ.exprimfact = new_chunk(100);
     986       16274 :   BQ.hashtab = (long**) new_chunk(HASHT);
     987       16274 :   for (i=0; i<HASHT; i++) BQ.hashtab[i] = NULL;
     988             : 
     989       16274 :   drc = fabs(gtodouble(QFR.D));
     990       16274 :   LOGD = log(drc);
     991       16274 :   LOGD2 = LOGD * LOGD;
     992             : 
     993       16274 :   sdrc = lim = sqrt(drc);
     994       16274 :   if (!BQ.PRECREG) lim /= sqrt(3.);
     995       16274 :   cp = (ulong)exp(sqrt(LOGD*log(LOGD)/8.0));
     996       16274 :   if (cp < 20) cp = 20;
     997       16274 :   if (cbach > 6.) {
     998           0 :     if (cbach2 < cbach) cbach2 = cbach;
     999           0 :     cbach = 6.;
    1000             :   }
    1001       16274 :   if (cbach < 0.)
    1002           0 :     pari_err_DOMAIN("Buchquad","Bach constant","<",gen_0,dbltor(cbach));
    1003       16274 :   av = avma;
    1004       16274 :   BQ.powsubFB = BQ.subFB = NULL;
    1005       16274 :   minSFB = (expi(D) > 15)? 3: 2;
    1006       16274 :   init_GRHcheck(&GRHcheck, 2, BQ.PRECREG? 2: 0, LOGD);
    1007       16274 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    1008       16274 :   LIMCMAX = (long)(6.*LOGD2);
    1009             :   /* 97/1223 below to ensure a good enough approximation of residue */
    1010       16274 :   cache_prime_quad(&GRHcheck, expi(D) < 16 ? 97: 1223, D);
    1011      141307 :   while (!quadGRHchk(D, &GRHcheck, high))
    1012             :   {
    1013      108759 :     low = high;
    1014      108759 :     high *= 2;
    1015             :   }
    1016      125103 :   while (high - low > 1)
    1017             :   {
    1018       92555 :     long test = (low+high)/2;
    1019       92555 :     if (quadGRHchk(D, &GRHcheck, test))
    1020       49196 :       high = test;
    1021             :     else
    1022       43359 :       low = test;
    1023             :   }
    1024       16274 :   if (high == LIMC0+1 && quadGRHchk(D, &GRHcheck, LIMC0))
    1025           0 :     LIMC2 = LIMC0;
    1026             :   else
    1027       16274 :     LIMC2 = high;
    1028       16274 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    1029       16274 :   LIMC0 = (long)(cbach*LOGD2);
    1030       16274 :   LIMC = cbach ? LIMC0 : LIMC2;
    1031       16274 :   LIMC = maxss(LIMC, nthidealquad(D, 2));
    1032             : 
    1033             : /* LIMC = Max(cbach*(log D)^2, exp(sqrt(log D loglog D) / 8)) */
    1034             : START:
    1035             :   do
    1036             :   {
    1037       16449 :     if (!FIRST) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    1038       16449 :     if (DEBUGLEVEL>2 && LIMC > LIMC0)
    1039           0 :       err_printf("%s*** Bach constant: %f\n", FIRST?"":"\n", LIMC/LOGD2);
    1040       16449 :     FIRST = 0; avma = av;
    1041       16449 :     if (BQ.subFB) gunclone(BQ.subFB);
    1042       16449 :     if (BQ.powsubFB) gunclone(BQ.powsubFB);
    1043       16449 :     clearhash(BQ.hashtab);
    1044       16449 :     if (LIMC < cp) LIMC = cp;
    1045       16449 :     if (LIMC2 < LIMC) LIMC2 = LIMC;
    1046       16449 :     if (BQ.PRECREG) qfr_data_init(QFR.D, BQ.PRECREG, &QFR);
    1047             : 
    1048       16449 :     FBquad(&BQ, LIMC2, LIMC, &GRHcheck);
    1049       16449 :     if (DEBUGLEVEL>2) timer_printf(&T, "factor base");
    1050       16449 :     BQ.subFB = subFBquad(&BQ, QFR.D, lim + 0.5, minSFB);
    1051       16449 :     if (DEBUGLEVEL>2) timer_printf(&T, "subFBquad = %Ps",
    1052             :                                  vecpermute(BQ.FB, BQ.subFB));
    1053       16449 :     nsubFB = lg(BQ.subFB) - 1;
    1054             :   }
    1055       16449 :   while (nsubFB < (expi(D) > 15 ? 3 : 2));
    1056             :   /* invhr = 2^r1 (2pi)^r2 / sqrt(D) w ~ L(chi,1) / hR */
    1057       16274 :   invhr = gmul(dbltor((BQ.PRECREG?2.:M_PI)/sdrc), compute_invresquad(&GRHcheck));
    1058       16274 :   BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1059       16274 :   if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1060       16274 :   BQ.limhash = (LIMC & HIGHMASK)? (HIGHBIT>>1): LIMC*LIMC;
    1061             : 
    1062       16274 :   need = BQ.KC + RELSUP - 2;
    1063       16274 :   current = 0;
    1064       16274 :   W = NULL;
    1065       16274 :   sfb_trials = nreldep = nrelsup = 0;
    1066       16274 :   s = nsubFB + RELSUP;
    1067       16274 :   av2 = avma;
    1068             : 
    1069             :   do
    1070             :   {
    1071       23812 :     if ((nreldep & 3) == 1 || (nrelsup & 7) == 1) {
    1072        6902 :       if (DEBUGLEVEL>2) err_printf("*** Changing sub factor base\n");
    1073        6902 :       gunclone(BQ.subFB);
    1074        6902 :       gunclone(BQ.powsubFB);
    1075        6902 :       BQ.subFB = gclone(vecslice(BQ.vperm, 1, nsubFB));
    1076        6902 :       BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1077        6902 :       if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1078        6902 :       clearhash(BQ.hashtab);
    1079             :     }
    1080       23812 :     need += 2;
    1081       23812 :     mat    = cgetg(need+1, t_MAT);
    1082       23812 :     extraC = cgetg(need+1, t_VEC);
    1083       23812 :     if (!W) { /* first time */
    1084       16274 :       C = extraC;
    1085       16274 :       triv = trivial_relations(&BQ, mat, C);
    1086       16274 :       if (DEBUGLEVEL>2) err_printf("KC = %ld, need %ld relations\n", BQ.KC, need);
    1087             :     } else {
    1088        7538 :       triv = 0;
    1089        7538 :       if (DEBUGLEVEL>2) err_printf("...need %ld more relations\n", need);
    1090             :     }
    1091       23812 :     if (BQ.PRECREG) {
    1092      189378 :       for (i = triv+1; i<=need; i++) {
    1093      184254 :         gel(mat,i) = zero_zv(BQ.KC);
    1094      184254 :         gel(extraC,i) = cgetr(BQ.PRECREG);
    1095             :       }
    1096        5124 :       real_relations(&BQ, need - triv, &current, s,LIMC,mat + triv,extraC + triv);
    1097             :     } else {
    1098      382811 :       for (i = triv+1; i<=need; i++) {
    1099      364123 :         gel(mat,i) = zero_zv(BQ.KC);
    1100      364123 :         gel(extraC,i) = gen_0;
    1101             :       }
    1102       18688 :       imag_relations(&BQ, need - triv, &current, LIMC,mat + triv);
    1103             :     }
    1104             : 
    1105       23812 :     if (!W)
    1106       16274 :       W = hnfspec_i(mat,BQ.vperm,&dep,&B,&C,nsubFB);
    1107             :     else
    1108        7538 :       W = hnfadd_i(W,BQ.vperm,&dep,&B,&C, mat,extraC);
    1109       23812 :     gerepileall(av2, 4, &W,&C,&B,&dep);
    1110       23812 :     need = BQ.KC - (lg(W)-1) - (lg(B)-1);
    1111       23812 :     if (need)
    1112             :     {
    1113        3928 :       if (++nreldep > 15 && cbach < 1) goto START;
    1114        3928 :       continue;
    1115             :     }
    1116             : 
    1117       19884 :     h = ZM_det_triangular(W);
    1118       19884 :     if (DEBUGLEVEL>2) err_printf("\n#### Tentative class number: %Ps\n", h);
    1119             : 
    1120       19884 :     switch(get_R(&BQ, C, (lg(C)-1) - (lg(B)-1) - (lg(W)-1), mulir(h,invhr), &R))
    1121             :     {
    1122             :       case fupb_PRECI:
    1123           0 :         BQ.PRECREG = precdbl(BQ.PRECREG);
    1124           0 :         FIRST = 1; goto START;
    1125             : 
    1126             :       case fupb_RELAT:
    1127        3610 :         if (++nrelsup > MAXRELSUP)
    1128             :         {
    1129          21 :           if (++sfb_trials > SFB_MAX && cbach <= 1) goto START;
    1130          21 :           if (nsubFB < minss(10,BQ.KC)) nsubFB++;
    1131             :         }
    1132        3610 :         need = minss(BQ.KC, nrelsup);
    1133             :     }
    1134             :   }
    1135       23812 :   while (need);
    1136             :   /* DONE */
    1137       16274 :   if (!quad_be_honest(&BQ)) goto START;
    1138       16274 :   if (DEBUGLEVEL>2) timer_printf(&T, "be honest");
    1139       16274 :   clearhash(BQ.hashtab);
    1140       16274 :   free_GRHcheck(&GRHcheck);
    1141             : 
    1142       16274 :   gen = get_clgp(&BQ,W,&cyc,prec);
    1143       16274 :   gunclone(BQ.subFB);
    1144       16274 :   gunclone(BQ.powsubFB);
    1145       16274 :   res = cgetg(5,t_VEC);
    1146       16274 :   gel(res,1) = h;
    1147       16274 :   gel(res,2) = cyc;
    1148       16274 :   gel(res,3) = gen;
    1149       16274 :   gel(res,4) = R; return gerepilecopy(av0,res);
    1150             : }
    1151             : 
    1152             : GEN
    1153           0 : buchimag(GEN D, GEN c, GEN c2, GEN REL)
    1154           0 : { (void)REL; return Buchquad(D,gtodouble(c),gtodouble(c2),0); }
    1155             : 
    1156             : GEN
    1157           0 : buchreal(GEN D, GEN flag, GEN c, GEN c2, GEN REL, long prec) {
    1158           0 :   if (signe(flag)) pari_err_IMPL("narrow class group");
    1159           0 :   (void)REL; return Buchquad(D,gtodouble(c),gtodouble(c2),prec);
    1160             : }
    1161             : 
    1162             : GEN
    1163       16281 : quadclassunit0(GEN x, long flag, GEN data, long prec)
    1164             : {
    1165             :   long lx;
    1166       16281 :   double c1 = 0.0, c2 = 0.0;
    1167             : 
    1168       16281 :   if (!data) lx=1;
    1169             :   else
    1170             :   {
    1171          28 :     lx = lg(data);
    1172          28 :     if (typ(data)!=t_VEC) pari_err_TYPE("quadclassunit", data);
    1173          28 :     if (lx > 7) pari_err_DIM("quadclassunit [tech vector]");
    1174          28 :     if (lx > 3) lx = 3;
    1175             :   }
    1176       16281 :   switch(lx)
    1177             :   {
    1178          21 :     case 3: c2 = gtodouble(gel(data,2));
    1179          28 :     case 2: c1 = gtodouble(gel(data,1));
    1180             :   }
    1181       16281 :   if (flag) pari_err_IMPL("narrow class group");
    1182       16281 :   return Buchquad(x,c1,c2,prec);
    1183             : }

Generated by: LCOV version 1.13