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.10.0 lcov report (development 20277-2bd9113) Lines: 636 671 94.8 %
Date: 2017-02-21 05:49:51 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     1562244 : 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     2021369 : init_form(struct buch_quad *B, GEN ex,
     131             :           GEN (*comp)(GEN,GEN,struct qfr_data *S))
     132             : {
     133     2021369 :   long i, l = lg(B->powsubFB);
     134     2021369 :   GEN F = NULL;
     135    13399912 :   for (i=1; i<l; i++)
     136    11378543 :     if (ex[i])
     137             :     {
     138    10668533 :       GEN t = gmael(B->powsubFB,i,ex[i]);
     139    10668533 :       F = F? comp(F, t, B->QFR): t;
     140             :     }
     141     2021369 :   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     6764925 : QFI_comp(GEN x, GEN y, struct qfr_data *S) { (void)S; return qficomp(x,y); }
     148             : 
     149             : static GEN
     150       48091 : qfi_factorback(struct buch_quad *B, GEN ex) { return init_form(B, ex, &QFI_comp); }
     151             : 
     152             : static GEN
     153     1775054 : random_form(struct buch_quad *B, GEN ex,
     154             :             GEN (*comp)(GEN,GEN, struct qfr_data *S))
     155             : {
     156     1775054 :   long i, l = lg(ex);
     157     1775054 :   pari_sp av = avma;
     158             :   GEN F;
     159             :   for(;;)
     160             :   {
     161     1775836 :     for (i=1; i<l; i++) ex[i] = random_bits(RANDOM_BITS);
     162     3550890 :     if ((F = init_form(B, ex, comp))) return F;
     163         782 :     avma = av;
     164         782 :   }
     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     1613921 : 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        2310 : bnf_increase_LIMC(long LIMC, long LIMCMAX)
     178             : {
     179        2310 :   if (LIMC >= LIMCMAX) pari_err_BUG("Buchmann's algorithm");
     180        2310 :   if (LIMC <= LIMCMAX/40) /* cbach <= 0.3 */
     181          14 :     LIMC *= 2;
     182        2296 :   else if (LIMCMAX < 60) /* \Delta_K <= 9 */
     183           0 :     LIMC++;
     184             :   else
     185        2296 :     LIMC += LIMCMAX / 60; /* cbach += 0.2 */
     186        2310 :   if (LIMC > LIMCMAX) LIMC = LIMCMAX;
     187        2310 :   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     2995897 : factorquad(struct buch_quad *B, GEN f, long nFB, ulong limp)
     199             : {
     200             :   ulong X;
     201     2995897 :   long i, lo = 0;
     202     2995897 :   GEN x = gel(f,1), FB = B->FB, P = B->primfact, E = B->exprimfact;
     203             : 
     204     6137122 :   for (i=1; lgefint(x) > 3; i++)
     205             :   {
     206       73056 :     ulong p = uel(FB,i), r;
     207       73056 :     GEN q = diviu_rem(x, p, &r);
     208       73056 :     if (!r)
     209             :     {
     210        3822 :       long k = 0;
     211        6154 :       do { k++; x = q; q = diviu_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     2995505 :   X = uel(x,2);
     221     2995505 :   if (X == 1) { P[0] = 0; return 1; }
     222   116557447 :   for (;; i++)
     223             :   { /* single precision affair, split for efficiency */
     224   119446166 :     ulong p = uel(FB,i);
     225   119446166 :     ulong q = X / p, r = X % p; /* gcc makes a single div */
     226   119446166 :     if (!r)
     227             :     {
     228     4471316 :       long k = 0;
     229     5436621 :       do { k++; X = q; q = X / p; r = X % p; } while (!r);
     230     4471316 :       lo++; P[lo] = p; E[lo] = k;
     231             :     }
     232   119446166 :     if (q <= p) break;
     233   117229315 :     if (i == nFB) return 0;
     234   116557447 :   }
     235             : END:
     236     2216852 :   if (X > B->limhash) return 0;
     237     2216824 :   if (X != 1 && X <= limp)
     238             :   {
     239      458817 :     if (B->badprim && ugcd(X, umodiu(B->badprim,X)) > 1) return 0;
     240      457997 :     lo++; P[lo] = X; E[lo] = 1;
     241      457997 :     X = 1;
     242             :   }
     243     2216004 :   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     1562244 : largeprime(struct buch_quad *B, long q, GEN ex, long np, long nrho)
     249             : {
     250     1562244 :   const long hashv = hash(q);
     251     1562244 :   long *pt, i, l = lg(B->subFB);
     252             : 
     253     1782835 :   for (pt = B->hashtab[hashv]; ; pt = (long*) pt[0])
     254             :   {
     255     1782835 :     if (!pt)
     256             :     {
     257     1474141 :       pt = (long*) pari_malloc((l+3) * sizeof(long));
     258     1474141 :       *pt++ = nrho; /* nrho = pt[-3] */
     259     1474141 :       *pt++ = np;   /* np   = pt[-2] */
     260     1474141 :       *pt++ = q;    /* q    = pt[-1] */
     261     1474141 :       pt[0] = (long)B->hashtab[hashv];
     262     1474141 :       for (i=1; i<l; i++) pt[i]=ex[i];
     263     1474141 :       B->hashtab[hashv]=pt; return NULL;
     264             :     }
     265      308694 :     if (pt[-1] == q) break;
     266      220591 :   }
     267      123308 :   for(i=1; i<l; i++)
     268      119626 :     if (pt[i] != ex[i]) return pt;
     269        3682 :   return (pt[-2]==np)? NULL: pt;
     270             : }
     271             : 
     272             : static void
     273       92303 : clearhash(long **hash)
     274             : {
     275             :   long *pt;
     276             :   long i;
     277    94610575 :   for (i=0; i<HASHT; i++) {
     278   190510685 :     for (pt = hash[i]; pt; ) {
     279     1474141 :       void *z = (void*)(pt - 3);
     280     1474141 :       pt = (long*) pt[0]; pari_free(z);
     281             :     }
     282    94518272 :     hash[i] = NULL;
     283             :   }
     284       92303 : }
     285             : 
     286             : /* last prime stored */
     287             : ulong
     288       40942 : 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       53957 : GRH_ensure(GRHcheck_t *S, long nb)
     292             : {
     293       53957 :   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       53957 : }
     300             : /* cache data for all primes up to the LIM */
     301             : static void
     302      489816 : cache_prime_quad(GRHcheck_t *S, ulong LIM, GEN D)
     303             : {
     304             :   GRHprime_t *pr;
     305             :   long nb;
     306             : 
     307      979632 :   if (S->limp >= LIM) return;
     308       42594 :   nb = (long)primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     309       42594 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     310       42594 :   for (pr = S->primes + S->nprimes;;)
     311             :   {
     312     2956922 :     ulong p = u_forprime_next(&(S->P));
     313     2956922 :     pr->p = p;
     314     2956922 :     pr->logp = log((double)p);
     315     2956922 :     pr->dec = (GEN)kroiu(D,p);
     316     2956922 :     S->nprimes++;
     317     2956922 :     pr++;
     318             :     /* store up to nextprime(LIM) included */
     319     2956922 :     if (p >= LIM) { S->limp = p; break; }
     320     2914328 :   }
     321             : }
     322             : 
     323             : static GEN
     324       40942 : compute_invresquad(GRHcheck_t *S)
     325             : {
     326       40942 :   pari_sp av = avma;
     327       40942 :   GEN invres = real_1(DEFAULTPREC);
     328       40942 :   GRHprime_t *pr = S->primes;
     329       40942 :   long i = S->nprimes, LIMC = GRH_last_prime(S)+diffptr[i]-1; /* nextprime(p+1)-1*/
     330       40942 :   double limp = log((double)LIMC) / 2;
     331     2997864 :   for (; i > 0; pr++, i--)
     332             :   {
     333     2956922 :     long s = (long)pr->dec;
     334     2956922 :     if (s)
     335             :     {
     336     2891762 :       ulong p = pr->p;
     337     2891762 :       if (s>0 || pr->logp <= limp)
     338             :         /* Both p and P contribute */
     339     1531122 :         invres = mulur(p - s, divru(invres, p));
     340     1360640 :       else if (s<0)
     341             :         /* Only p contributes */
     342     1360640 :         invres = mulur(p, divru(invres, p - 1));
     343             :     }
     344             :   }
     345       40942 :   return gerepileuptoleaf(av, invres);
     346             : }
     347             : 
     348             : /* p | conductor of order of disc D ? */
     349             : static int
     350      213620 : is_bad(GEN D, ulong p)
     351             : {
     352      213620 :   pari_sp av = avma;
     353             :   int r;
     354      213620 :   if (p == 2)
     355             :   {
     356       54116 :     r = mod16(D) >> 1;
     357       54116 :     if (r && signe(D) < 0) r = 8-r;
     358       54116 :     return (r < 4);
     359             :   }
     360      159504 :   r = (remii(D, sqru(p)) == gen_0); /* p^2 | D ? */
     361      159504 :   avma = av; return r;
     362             : }
     363             : 
     364             : /* returns the n-th suitable ideal for the factorbase */
     365             : static long
     366       40942 : nthidealquad(GEN D, long n)
     367             : {
     368       40942 :   pari_sp av = avma;
     369             :   forprime_t S;
     370             :   ulong p;
     371       40942 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     372      236160 :   while ((p = u_forprime_next(&S)) && n > 0)
     373      154276 :     if (!is_bad(D, p) && kroiu(D, p) >= 0) n--;
     374       40942 :   avma = av; return p;
     375             : }
     376             : 
     377             : static int
     378      405769 : quadGRHchk(GEN D, GRHcheck_t *S, ulong LIMC)
     379             : {
     380      405769 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     381             :   long i;
     382             : 
     383      405769 :   cache_prime_quad(S, LIMC, D);
     384    11575413 :   for (i = 0;; i++)
     385             :   {
     386    11575413 :     GRHprime_t *pr = S->primes+i;
     387    11575413 :     ulong p = pr->p;
     388             :     long M;
     389             :     double logNP, q, A, B;
     390    11575413 :     if (p > LIMC) break;
     391    11169644 :     if ((long)pr->dec < 0)
     392             :     {
     393     5513469 :       logNP = 2 * pr->logp;
     394     5513469 :       q = 1/(double)p;
     395             :     }
     396             :     else
     397             :     {
     398     5656175 :       logNP = pr->logp;
     399     5656175 :       q = 1/sqrt((double)p);
     400             :     }
     401    11169644 :     A = logNP * q; B = logNP * A; M = (long)(logC/logNP);
     402    11169644 :     if (M > 1)
     403             :     {
     404      758474 :       double inv1_q = 1 / (1-q);
     405      758474 :       A *= (1 - pow(q, (double) M)) * inv1_q;
     406      758474 :       B *= (1 - pow(q, (double) M)*(M+1 - M*q)) * inv1_q * inv1_q;
     407             :     }
     408    11169644 :     if ((long)pr->dec>0) { SA += 2*A;SB += 2*B; } else { SA += A; SB += B; }
     409    11169644 :   }
     410      405769 :   return GRHok(S, logC, SA, SB);
     411             : }
     412             : 
     413             : /* create B->FB, B->numFB; set B->badprim. Return L(kro_D, 1) */
     414             : static void
     415       43105 : FBquad(struct buch_quad *B, ulong C2, ulong C1, GRHcheck_t *S)
     416             : {
     417       43105 :   GEN D = B->QFR->D;
     418             :   long i;
     419             :   pari_sp av;
     420             :   GRHprime_t *pr;
     421             : 
     422       43105 :   cache_prime_quad(S, C2, D);
     423       43105 :   pr = S->primes;
     424       43105 :   B->numFB = cgetg(C2+1, t_VECSMALL);
     425       43105 :   B->FB    = cgetg(C2+1, t_VECSMALL);
     426       43105 :   av = avma;
     427       43105 :   B->KC = 0; i = 0;
     428       43105 :   B->badprim = gen_1;
     429     1133539 :   for (;; pr++) /* p <= C2 */
     430             :   {
     431     1176644 :     ulong p = pr->p;
     432     1176644 :     if (!B->KC && p > C1) B->KC = i;
     433     1176644 :     if (p > C2) break;
     434     1133539 :     switch ((long)pr->dec)
     435             :     {
     436      552968 :       case -1: break; /* inert */
     437             :       case  0: /* ramified */
     438       59344 :         if (is_bad(D, p)) { B->badprim = muliu(B->badprim, p); break; }
     439             :         /* fall through */
     440             :       default:  /* split */
     441      580347 :         i++; B->numFB[p] = i; B->FB[i] = p; break;
     442             :     }
     443     1133539 :   }
     444       43105 :   B->KC2 = i;
     445       43105 :   setlg(B->FB, B->KC2+1);
     446       43105 :   if (B->badprim != gen_1)
     447         196 :     B->badprim = gerepileuptoint(av, B->badprim);
     448             :   else
     449             :   {
     450       42909 :     B->badprim = NULL; avma = av;
     451             :   }
     452       43105 : }
     453             : 
     454             : /* create B->vperm, return B->subFB */
     455             : static GEN
     456       43105 : subFBquad(struct buch_quad *B, GEN D, double PROD, long minSFB)
     457             : {
     458       43105 :   long i, j, lgsub = 1, ino = 1, lv = B->KC+1;
     459       43105 :   double prod = 1.;
     460             :   pari_sp av;
     461             :   GEN no;
     462             : 
     463       43105 :   B->vperm = cgetg(lv, t_VECSMALL);
     464       43105 :   av = avma;
     465       43105 :   no    = cgetg(lv, t_VECSMALL);
     466      168793 :   for (j = 1; j < lv; j++)
     467             :   {
     468      166602 :     ulong p = uel(B->FB,j);
     469      166602 :     if (!umodiu(D, p)) no[ino++] = j; /* ramified */
     470             :     else
     471             :     {
     472      118011 :       B->vperm[lgsub++] = j;
     473      118011 :       prod *= p;
     474      118011 :       if (lgsub > minSFB && prod > PROD) break;
     475             :     }
     476             :   }
     477             :   /* lgsub >= 1 otherwise quadGRHchk is false */
     478       43105 :   i = lgsub;
     479       43105 :   for (j = 1; j < ino;i++,j++) B->vperm[i] = no[j];
     480       43105 :   for (     ; i < lv; i++)     B->vperm[i] = i;
     481       43105 :   no = gclone(vecslice(B->vperm, 1, lgsub-1));
     482       43105 :   avma = av; return no;
     483             : }
     484             : 
     485             : /* assume n >= 1, x[i][j] = B->subFB[i]^j, for j = 1..n */
     486             : static GEN
     487       49198 : powsubFBquad(struct buch_quad *B, long n)
     488             : {
     489       49198 :   pari_sp av = avma;
     490       49198 :   long i,j, l = lg(B->subFB);
     491       49198 :   GEN F, y, x = cgetg(l, t_VEC), D = B->QFR->D;
     492             : 
     493       49198 :   if (B->PRECREG) /* real */
     494             :   {
     495       39088 :     for (i=1; i<l; i++)
     496             :     {
     497       34055 :       F = qfr5_pf(B->QFR, B->FB[B->subFB[i]], B->PRECREG);
     498       34055 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     499       34055 :       gel(y,1) = F;
     500       34055 :       for (j=2; j<=n; j++) gel(y,j) = QFR5_comp(gel(y,j-1), F, B->QFR);
     501             :     }
     502             :   }
     503             :   else /* imaginary */
     504             :   {
     505      165892 :     for (i=1; i<l; i++)
     506             :     {
     507      121727 :       F = qfi_pf(D, B->FB[B->subFB[i]]);
     508      121727 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     509      121727 :       gel(y,1) = F;
     510      121727 :       for (j=2; j<=n; j++) gel(y,j) = qficomp(gel(y,j-1), F);
     511             :     }
     512             :   }
     513       49198 :   x = gclone(x); avma = av; return x;
     514             : }
     515             : 
     516             : static void
     517       42445 : sub_fact(struct buch_quad *B, GEN col, GEN F)
     518             : {
     519       42445 :   GEN b = gel(F,2);
     520             :   long i;
     521      129278 :   for (i=1; i<=B->primfact[0]; i++)
     522             :   {
     523       86833 :     ulong p = B->primfact[i], k = B->numFB[p];
     524       86833 :     long e = B->exprimfact[i];
     525       86833 :     if (umodiu(b, p<<1) > p) e = -e;
     526       86833 :     col[k] -= e;
     527             :   }
     528       42445 : }
     529             : static void
     530      801976 : add_fact(struct buch_quad *B, GEN col, GEN F)
     531             : {
     532      801976 :   GEN b = gel(F,2);
     533             :   long i;
     534     2635983 :   for (i=1; i<=B->primfact[0]; i++)
     535             :   {
     536     1834007 :     ulong p = B->primfact[i], k = B->numFB[p];
     537     1834007 :     long e = B->exprimfact[i];
     538     1834007 :     if (umodiu(b, p<<1) > p) e = -e;
     539     1834007 :     col[k] += e;
     540             :   }
     541      801976 : }
     542             : 
     543             : static GEN
     544       40942 : get_clgp(struct buch_quad *B, GEN W, GEN *ptD, long prec)
     545             : {
     546       40942 :   GEN res, init, u1, D = ZM_snf_group(W,NULL,&u1), Z = prec? real_0(prec): NULL;
     547       40942 :   long i, j, l = lg(W), c = lg(D);
     548             : 
     549       40942 :   res=cgetg(c,t_VEC); init = cgetg(l,t_VEC);
     550       40942 :   for (i=1; i<l; i++) gel(init,i) = primeform_u(B->QFR->D, B->FB[B->vperm[i]]);
     551       89205 :   for (j=1; j<c; j++)
     552             :   {
     553       48263 :     GEN g = NULL;
     554       48263 :     if (prec)
     555             :     {
     556       13293 :       for (i=1; i<l; i++)
     557             :       {
     558        9583 :         GEN t, u = gcoeff(u1,i,j);
     559        9583 :         if (!signe(u)) continue;
     560        4389 :         t = qfr3_pow(gel(init,i), u, B->QFR);
     561        4389 :         g = g? qfr3_comp(g, t, B->QFR): t;
     562             :       }
     563        3710 :       g = qfr3_to_qfr(qfr3_canon_safe(qfr3_red(g, B->QFR), B->QFR), Z);
     564             :     }
     565             :     else
     566             :     {
     567      132686 :       for (i=1; i<l; i++)
     568             :       {
     569       88133 :         GEN t, u = gcoeff(u1,i,j);
     570       88133 :         if (!signe(u)) continue;
     571       61103 :         t = powgi(gel(init,i), u);
     572       61103 :         g = g? qficomp(g, t): t;
     573             :       }
     574             :     }
     575       48263 :     gel(res,j) = g;
     576             :   }
     577       40942 :   *ptD = D; return res;
     578             : }
     579             : 
     580             : static long
     581       40942 : trivial_relations(struct buch_quad *B, GEN mat, GEN C)
     582             : {
     583       40942 :   long i, j = 0;
     584       40942 :   GEN col, D = B->QFR->D;
     585      613232 :   for (i = 1; i <= B->KC; i++)
     586             :   { /* ramified prime ==> trivial relation */
     587      572290 :     if (umodiu(D, B->FB[i])) continue;
     588       53730 :     col = zero_zv(B->KC);
     589       53730 :     col[i] = 2; j++;
     590       53730 :     gel(mat,j) = col;
     591       53730 :     gel(C,j) = gen_0;
     592             :   }
     593       40942 :   return j;
     594             : }
     595             : 
     596             : static void
     597           0 : dbg_all(pari_timer *T, const char *phase, long s, long n)
     598             : {
     599           0 :   err_printf("\n");
     600           0 :   timer_printf(T, "%s rel [#rel/#test = %ld/%ld]", phase,s,n);
     601           0 : }
     602             : 
     603             : /* Imaginary Quadratic fields */
     604             : 
     605             : static void
     606       45077 : imag_relations(struct buch_quad *B, long need, long *pc, ulong LIMC, GEN mat)
     607             : {
     608             :   pari_timer T;
     609       45077 :   long lgsub = lg(B->subFB), current = *pc, nbtest = 0, s = 0;
     610             :   long i, fpc;
     611             :   pari_sp av;
     612       45077 :   GEN col, form, ex = cgetg(lgsub, t_VECSMALL);
     613             : 
     614       45077 :   if (!current) current = 1;
     615       45077 :   if (DEBUGLEVEL>2) timer_start(&T);
     616       45077 :   av = avma;
     617             :   for(;;)
     618             :   {
     619     1658529 :     if (s >= need) break;
     620     1613452 :     avma = av;
     621     1613452 :     form = qfi_random(B,ex);
     622     1613452 :     form = qficomp(form, qfi_pf(B->QFR->D, B->FB[current]));
     623     1613452 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     624     1613452 :     if (!fpc)
     625             :     {
     626      287596 :       if (DEBUGLEVEL>3) err_printf(".");
     627      287596 :       if ((nbtest & 0xff) == 0 && ++current > B->KC) current = 1;
     628      287596 :       continue;
     629             :     }
     630     1325856 :     if (fpc > 1)
     631             :     {
     632      798131 :       long *fpd = largeprime(B,fpc,ex,current,0);
     633             :       ulong b1, b2, p;
     634             :       GEN form2;
     635      798131 :       if (!fpd)
     636             :       {
     637      750040 :         if (DEBUGLEVEL>3) err_printf(".");
     638      750040 :         continue;
     639             :       }
     640       48091 :       form2 = qficomp(qfi_factorback(B,fpd), qfi_pf(B->QFR->D, B->FB[fpd[-2]]));
     641       48091 :       p = fpc << 1;
     642       48091 :       b1 = umodiu(gel(form2,2), p);
     643       48091 :       b2 = umodiu(gel(form,2),  p);
     644       48091 :       if (b1 != b2 && b1+b2 != p) continue;
     645             : 
     646       48056 :       col = gel(mat,++s);
     647       48056 :       add_fact(B,col, form);
     648       48056 :       (void)factorquad(B,form2,B->KC,LIMC);
     649       48056 :       if (b1==b2)
     650             :       {
     651       24210 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     652       24210 :         sub_fact(B, col, form2); col[fpd[-2]]++;
     653             :       }
     654             :       else
     655             :       {
     656       23846 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     657       23846 :         add_fact(B, col, form2); col[fpd[-2]]--;
     658             :       }
     659       48056 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     660             :     }
     661             :     else
     662             :     {
     663      527725 :       col = gel(mat,++s);
     664      527725 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     665      527725 :       add_fact(B, col, form);
     666      527725 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     667             :     }
     668      575781 :     col[current]--;
     669      575781 :     if (++current > B->KC) current = 1;
     670     1613452 :   }
     671       45077 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     672       45077 :   *pc = current;
     673       45077 : }
     674             : 
     675             : static int
     676           7 : imag_be_honest(struct buch_quad *B)
     677             : {
     678           7 :   long p, fpc, s = B->KC, nbtest = 0;
     679           7 :   GEN F, ex = cgetg(lg(B->subFB), t_VECSMALL);
     680           7 :   pari_sp av = avma;
     681             : 
     682         483 :   while (s<B->KC2)
     683             :   {
     684         469 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     685         469 :     F = qficomp(qfi_pf(B->QFR->D, p), qfi_random(B, ex));
     686         469 :     fpc = factorquad(B,F,s,p-1);
     687         469 :     if (fpc == 1) { nbtest=0; s++; }
     688             :     else
     689         322 :       if (++nbtest > 40) return 0;
     690         469 :     avma = av;
     691             :   }
     692           7 :   return 1;
     693             : }
     694             : 
     695             : /* Real Quadratic fields */
     696             : 
     697             : static void
     698        5124 : real_relations(struct buch_quad *B, long need, long *pc, long lim, ulong LIMC, GEN mat, GEN C)
     699             : {
     700             :   pari_timer T;
     701        5124 :   long lgsub = lg(B->subFB), prec = B->PRECREG, current = *pc, nbtest=0, s=0;
     702             :   long i, fpc, endcycle, rhoacc, rho;
     703             :   /* in a 2nd phase, don't include FB[current] but run along the cyle
     704             :    * ==> get more units */
     705        5124 :   int first = (current == 0);
     706             :   pari_sp av, av1;
     707        5124 :   GEN d, col, form, form0, form1, ex = cgetg(lgsub, t_VECSMALL);
     708             : 
     709        5124 :   if (DEBUGLEVEL>2) timer_start(&T);
     710        5124 :   if (!current) current = 1;
     711        5124 :   if (lim > need) lim = need;
     712        5124 :   av = avma;
     713             :   for(;;)
     714             :   {
     715      166236 :     if (s >= need) break;
     716      161112 :     if (first && s >= lim) {
     717        2142 :       first = 0;
     718        2142 :       if (DEBUGLEVEL>2) dbg_all(&T, "initial", s, nbtest);
     719             :     }
     720      161112 :     avma = av; form = qfr3_random(B, ex);
     721      161112 :     if (!first)
     722      158935 :       form = QFR3_comp(form, qfr3_pf(B->QFR, B->FB[current]), B->QFR);
     723      161112 :     av1 = avma;
     724      161112 :     form0 = form; form1 = NULL;
     725      161112 :     endcycle = rhoacc = 0;
     726      161112 :     rho = -1;
     727             : 
     728             : CYCLE:
     729     1297569 :     if (endcycle || rho > 5000)
     730             :     {
     731          21 :       if (++current > B->KC) current = 1;
     732          21 :       continue;
     733             :     }
     734     1297548 :     if (gc_needed(av,1))
     735             :     {
     736           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"real_relations");
     737           0 :       gerepileall(av1, form1? 2: 1, &form, &form1);
     738             :     }
     739     1297548 :     if (rho < 0) rho = 0; /* first time in */
     740             :     else
     741             :     {
     742     1136436 :       form = qfr3_rho(form, B->QFR); rho++;
     743     1136436 :       rhoacc++;
     744     1136436 :       if (first)
     745      412688 :         endcycle = (absequalii(gel(form,1),gel(form0,1))
     746      190473 :              && equalii(gel(form,2),gel(form0,2)));
     747             :       else
     748             :       {
     749      914221 :         if (absequalii(gel(form,1), gel(form,3))) /* a = -c */
     750             :         {
     751           0 :           if (absequalii(gel(form,1),gel(form0,1)) &&
     752           0 :                   equalii(gel(form,2),gel(form0,2))) continue;
     753           0 :           form = qfr3_rho(form, B->QFR); rho++;
     754           0 :           rhoacc++;
     755             :         }
     756             :         else
     757      914221 :           { setsigne(form[1],1); setsigne(form[3],-1); }
     758      914277 :         if (equalii(gel(form,1),gel(form0,1)) &&
     759          56 :             equalii(gel(form,2),gel(form0,2))) continue;
     760             :       }
     761             :     }
     762     1297548 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     763     1297548 :     if (!fpc)
     764             :     {
     765      385511 :       if (DEBUGLEVEL>3) err_printf(".");
     766      385511 :       goto CYCLE;
     767             :     }
     768      912037 :     if (fpc > 1)
     769             :     { /* look for Large Prime relation */
     770      764113 :       long *fpd = largeprime(B,fpc,ex,first? 0: current,rhoacc);
     771             :       ulong b1, b2, p;
     772             :       GEN form2;
     773      764113 :       if (!fpd)
     774             :       {
     775      727783 :         if (DEBUGLEVEL>3) err_printf(".");
     776      727783 :         goto CYCLE;
     777             :       }
     778       36330 :       if (!form1)
     779             :       {
     780       36330 :         form1 = qfr5_factorback(B,ex);
     781       36330 :         if (!first)
     782       36330 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     783             :       }
     784       36330 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     785       36330 :       rho = 0;
     786             : 
     787       36330 :       form2 = qfr5_factorback(B,fpd);
     788       36330 :       if (fpd[-2])
     789       23954 :         form2 = QFR5_comp(form2, qfr5_pf(B->QFR, B->FB[fpd[-2]], prec), B->QFR);
     790       36330 :       form2 = qfr5_rho_pow(form2, fpd[-3], B->QFR);
     791       36330 :       if (!absequalii(gel(form2,1),gel(form2,3)))
     792             :       {
     793       36330 :         setsigne(form2[1], 1);
     794       36330 :         setsigne(form2[3],-1);
     795             :       }
     796       36330 :       p = fpc << 1;
     797       36330 :       b1 = umodiu(gel(form2,2), p);
     798       36330 :       b2 = umodiu(gel(form1,2), p);
     799       36330 :       if (b1 != b2 && b1+b2 != p) goto CYCLE;
     800             : 
     801       36330 :       col = gel(mat,++s);
     802       36330 :       add_fact(B, col, form1);
     803       36330 :       (void)factorquad(B,form2,B->KC,LIMC);
     804       36330 :       if (b1==b2)
     805             :       {
     806       18235 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     807       18235 :         sub_fact(B,col, form2);
     808       18235 :         if (fpd[-2]) col[fpd[-2]]++;
     809       36470 :         d = qfr5_dist(subii(gel(form1,4),gel(form2,4)),
     810       36470 :                       divrr(gel(form1,5),gel(form2,5)), prec);
     811             :       }
     812             :       else
     813             :       {
     814       18095 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     815       18095 :         add_fact(B, col, form2);
     816       18095 :         if (fpd[-2]) col[fpd[-2]]--;
     817       36190 :         d = qfr5_dist(addii(gel(form1,4),gel(form2,4)),
     818       36190 :                       mulrr(gel(form1,5),gel(form2,5)), prec);
     819             :       }
     820       36330 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     821             :     }
     822             :     else
     823             :     { /* standard relation */
     824      147924 :       if (!form1)
     825             :       {
     826      124782 :         form1 = qfr5_factorback(B, ex);
     827      124782 :         if (!first)
     828      122605 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     829             :       }
     830      147924 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     831      147924 :       rho = 0;
     832             : 
     833      147924 :       col = gel(mat,++s);
     834      147924 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     835      147924 :       add_fact(B, col, form1);
     836      147924 :       d = qfr5_dist(gel(form1,4), gel(form1,5), prec);
     837      147924 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     838             :     }
     839      184254 :     affrr(d, gel(C,s));
     840      184254 :     if (first)
     841             :     {
     842       25319 :       if (s >= lim) continue;
     843       23163 :       goto CYCLE;
     844             :     }
     845             :     else
     846             :     {
     847      158935 :       col[current]--;
     848      158935 :       if (++current > B->KC) current = 1;
     849             :     }
     850      161112 :   }
     851        5124 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     852        5124 :   *pc = current;
     853        5124 : }
     854             : 
     855             : static int
     856           7 : real_be_honest(struct buch_quad *B)
     857             : {
     858           7 :   long p, fpc, s = B->KC, nbtest = 0;
     859           7 :   GEN F,F0, ex = cgetg(lg(B->subFB), t_VECSMALL);
     860           7 :   pari_sp av = avma;
     861             : 
     862          35 :   while (s<B->KC2)
     863             :   {
     864          21 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     865          21 :     F = QFR3_comp(qfr3_random(B, ex), qfr3_pf(B->QFR, p), B->QFR);
     866          21 :     for (F0 = F;;)
     867             :     {
     868          42 :       fpc = factorquad(B,F,s,p-1);
     869          42 :       if (fpc == 1) { nbtest=0; s++; break; }
     870          21 :       if (++nbtest > 40) return 0;
     871          21 :       F = qfr3_canon(qfr3_rho(F, B->QFR), B->QFR);
     872          21 :       if (equalii(gel(F,1),gel(F0,1))
     873           0 :        && equalii(gel(F,2),gel(F0,2))) break;
     874          21 :     }
     875          21 :     avma = av;
     876             :   }
     877           7 :   return 1;
     878             : }
     879             : 
     880             : static GEN
     881       32025 : gcdreal(GEN a,GEN b)
     882             : {
     883       32025 :   if (!signe(a)) return mpabs(b);
     884       31361 :   if (!signe(b)) return mpabs(a);
     885       31213 :   if (typ(a)==t_INT)
     886             :   {
     887           0 :     if (typ(b)==t_INT) return gcdii(a,b);
     888           0 :     a = itor(a, lg(b));
     889             :   }
     890       31213 :   else if (typ(b)==t_INT)
     891           0 :     b = itor(b, lg(a));
     892       31213 :   if (expo(a)<-5) return absr(b);
     893       11305 :   if (expo(b)<-5) return absr(a);
     894        8456 :   a = absr(a); b = absr(b);
     895       27135 :   while (expo(b) >= -5  && signe(b))
     896             :   {
     897             :     long e;
     898       10223 :     GEN r, q = gcvtoi(divrr(a,b),&e);
     899       10223 :     if (e > 0) return NULL;
     900       10223 :     r = subrr(a, mulir(q,b)); a = b; b = r;
     901             :   }
     902        8456 :   return absr(a);
     903             : }
     904             : 
     905             : static int
     906       46146 : get_R(struct buch_quad *B, GEN C, long sreg, GEN z, GEN *ptR)
     907             : {
     908       46146 :   GEN R = gen_1;
     909             :   double c;
     910             :   long i;
     911             : 
     912       46146 :   if (B->PRECREG)
     913             :   {
     914        2877 :     R = mpabs(gel(C,1));
     915       34902 :     for (i=2; i<=sreg; i++)
     916             :     {
     917       32025 :       R = gcdreal(gel(C,i), R);
     918       32025 :       if (!R) return fupb_PRECI;
     919             :     }
     920        2877 :     if (gexpo(R) <= -3)
     921             :     {
     922           0 :       if (DEBUGLEVEL>2) err_printf("regulator is zero.\n");
     923           0 :       return fupb_RELAT;
     924             :     }
     925        2877 :     if (DEBUGLEVEL>2) err_printf("#### Tentative regulator: %Ps\n",R);
     926             :   }
     927       46146 :   c = gtodouble(gmul(z, R));
     928       46146 :   if (c < 0.8 || c > 1.3) return fupb_RELAT;
     929       40942 :   *ptR = R; return fupb_NONE;
     930             : }
     931             : 
     932             : static int
     933       40942 : quad_be_honest(struct buch_quad *B)
     934             : {
     935             :   int r;
     936       40942 :   if (B->KC2 <= B->KC) return 1;
     937          14 :   if (DEBUGLEVEL>2)
     938           0 :     err_printf("be honest for primes from %ld to %ld\n", B->FB[B->KC+1],B->FB[B->KC2]);
     939          14 :   r = B->PRECREG? real_be_honest(B): imag_be_honest(B);
     940          14 :   if (DEBUGLEVEL>2) err_printf("\n");
     941          14 :   return r;
     942             : }
     943             : 
     944             : 
     945             : GEN
     946       40949 : Buchquad(GEN D, double cbach, double cbach2, long prec)
     947             : {
     948       40949 :   const long MAXRELSUP = 7, SFB_MAX = 3;
     949             :   pari_timer T;
     950       40949 :   pari_sp av0 = avma, av, av2;
     951       40949 :   const long RELSUP = 5;
     952             :   long i, s, current, triv, sfb_trials, nrelsup, nreldep, need, nsubFB, minSFB;
     953             :   ulong low, high, LIMC0, LIMC, LIMC2, LIMCMAX, cp;
     954       40949 :   GEN W, cyc, res, gen, dep, mat, C, extraC, B, R, invhr, h = NULL; /*-Wall*/
     955             :   double drc, sdrc, lim, LOGD, LOGD2;
     956             :   GRHcheck_t GRHcheck;
     957             :   struct qfr_data QFR;
     958             :   struct buch_quad BQ;
     959       40949 :   int FIRST = 1;
     960             : 
     961       40949 :   check_quaddisc(D, &s, /*junk*/&i, "Buchquad");
     962       40949 :   R = NULL; /* -Wall */
     963       40949 :   BQ.QFR = &QFR;
     964       40949 :   QFR.D = D;
     965       40949 :   if (s < 0)
     966             :   {
     967       38793 :     if (abscmpiu(QFR.D,4) <= 0)
     968             :     {
     969           7 :       GEN z = cgetg(5,t_VEC);
     970           7 :       gel(z,1) = gel(z,4) = gen_1; gel(z,2) = gel(z,3) = cgetg(1,t_VEC);
     971           7 :       return z;
     972             :     }
     973       38786 :     prec = BQ.PRECREG = 0;
     974             :   } else {
     975        2156 :     BQ.PRECREG = maxss(prec+EXTRAPRECWORD, nbits2prec(2*expi(QFR.D) + 128));
     976             :   }
     977       40942 :   if (DEBUGLEVEL>2) timer_start(&T);
     978       40942 :   BQ.primfact   = new_chunk(100);
     979       40942 :   BQ.exprimfact = new_chunk(100);
     980       40942 :   BQ.hashtab = (long**) new_chunk(HASHT);
     981       40942 :   for (i=0; i<HASHT; i++) BQ.hashtab[i] = NULL;
     982             : 
     983       40942 :   drc = fabs(gtodouble(QFR.D));
     984       40942 :   LOGD = log(drc);
     985       40942 :   LOGD2 = LOGD * LOGD;
     986             : 
     987       40942 :   sdrc = lim = sqrt(drc);
     988       40942 :   if (!BQ.PRECREG) lim /= sqrt(3.);
     989       40942 :   cp = (ulong)exp(sqrt(LOGD*log(LOGD)/8.0));
     990       40942 :   if (cp < 20) cp = 20;
     991       40942 :   if (cbach > 6.) {
     992           0 :     if (cbach2 < cbach) cbach2 = cbach;
     993           0 :     cbach = 6.;
     994             :   }
     995       40942 :   if (cbach < 0.)
     996           0 :     pari_err_DOMAIN("Buchquad","Bach constant","<",gen_0,dbltor(cbach));
     997       40942 :   av = avma;
     998       40942 :   BQ.powsubFB = BQ.subFB = NULL;
     999       40942 :   minSFB = (expi(D) > 15)? 3: 2;
    1000       40942 :   init_GRHcheck(&GRHcheck, 2, BQ.PRECREG? 2: 0, LOGD);
    1001       40942 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    1002       40942 :   LIMCMAX = (long)(6.*LOGD2);
    1003             :   /* 97/1223 below to ensure a good enough approximation of residue */
    1004       40942 :   cache_prime_quad(&GRHcheck, expi(D) < 16 ? 97: 1223, D);
    1005      284737 :   while (!quadGRHchk(D, &GRHcheck, high))
    1006             :   {
    1007      202853 :     low = high;
    1008      202853 :     high *= 2;
    1009             :   }
    1010      243858 :   while (high - low > 1)
    1011             :   {
    1012      161974 :     long test = (low+high)/2;
    1013      161974 :     if (quadGRHchk(D, &GRHcheck, test))
    1014       89362 :       high = test;
    1015             :     else
    1016       72612 :       low = test;
    1017             :   }
    1018       40942 :   if (high == LIMC0+1 && quadGRHchk(D, &GRHcheck, LIMC0))
    1019           0 :     LIMC2 = LIMC0;
    1020             :   else
    1021       40942 :     LIMC2 = high;
    1022       40942 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    1023       40942 :   LIMC0 = (long)(cbach*LOGD2);
    1024       40942 :   LIMC = cbach ? LIMC0 : LIMC2;
    1025       40942 :   LIMC = maxss(LIMC, nthidealquad(D, 2));
    1026             : 
    1027             : /* LIMC = Max(cbach*(log D)^2, exp(sqrt(log D loglog D) / 8)) */
    1028             : START:
    1029             :   do
    1030             :   {
    1031       43105 :     if (!FIRST) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    1032       43105 :     if (DEBUGLEVEL>2 && LIMC > LIMC0)
    1033           0 :       err_printf("%s*** Bach constant: %f\n", FIRST?"":"\n", LIMC/LOGD2);
    1034       43105 :     FIRST = 0; avma = av;
    1035       43105 :     if (BQ.subFB) gunclone(BQ.subFB);
    1036       43105 :     if (BQ.powsubFB) gunclone(BQ.powsubFB);
    1037       43105 :     clearhash(BQ.hashtab);
    1038       43105 :     if (LIMC < cp) LIMC = cp;
    1039       43105 :     if (LIMC2 < LIMC) LIMC2 = LIMC;
    1040       43105 :     if (BQ.PRECREG) qfr_data_init(QFR.D, BQ.PRECREG, &QFR);
    1041             : 
    1042       43105 :     FBquad(&BQ, LIMC2, LIMC, &GRHcheck);
    1043       43105 :     if (DEBUGLEVEL>2) timer_printf(&T, "factor base");
    1044       43105 :     BQ.subFB = subFBquad(&BQ, QFR.D, lim + 0.5, minSFB);
    1045       43105 :     if (DEBUGLEVEL>2) timer_printf(&T, "subFBquad = %Ps",
    1046             :                                  vecpermute(BQ.FB, BQ.subFB));
    1047       43105 :     nsubFB = lg(BQ.subFB) - 1;
    1048             :   }
    1049       43105 :   while (nsubFB < (expi(D) > 15 ? 3 : 2));
    1050             :   /* invhr = 2^r1 (2pi)^r2 / sqrt(D) w ~ L(chi,1) / hR */
    1051       40942 :   invhr = gmul(dbltor((BQ.PRECREG?2.:M_PI)/sdrc), compute_invresquad(&GRHcheck));
    1052       40942 :   BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1053       40942 :   if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1054       40942 :   BQ.limhash = (LIMC & HIGHMASK)? (HIGHBIT>>1): LIMC*LIMC;
    1055             : 
    1056       40942 :   need = BQ.KC + RELSUP - 2;
    1057       40942 :   current = 0;
    1058       40942 :   W = NULL;
    1059       40942 :   sfb_trials = nreldep = nrelsup = 0;
    1060       40942 :   s = nsubFB + RELSUP;
    1061       40942 :   av2 = avma;
    1062             : 
    1063             :   do
    1064             :   {
    1065       50201 :     if ((nreldep & 3) == 1 || (nrelsup & 7) == 1) {
    1066        8256 :       if (DEBUGLEVEL>2) err_printf("*** Changing sub factor base\n");
    1067        8256 :       gunclone(BQ.subFB);
    1068        8256 :       gunclone(BQ.powsubFB);
    1069        8256 :       BQ.subFB = gclone(vecslice(BQ.vperm, 1, nsubFB));
    1070        8256 :       BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1071        8256 :       if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1072        8256 :       clearhash(BQ.hashtab);
    1073             :     }
    1074       50201 :     need += 2;
    1075       50201 :     mat    = cgetg(need+1, t_MAT);
    1076       50201 :     extraC = cgetg(need+1, t_VEC);
    1077       50201 :     if (!W) { /* first time */
    1078       40942 :       C = extraC;
    1079       40942 :       triv = trivial_relations(&BQ, mat, C);
    1080       40942 :       if (DEBUGLEVEL>2) err_printf("KC = %ld, need %ld relations\n", BQ.KC, need);
    1081             :     } else {
    1082        9259 :       triv = 0;
    1083        9259 :       if (DEBUGLEVEL>2) err_printf("...need %ld more relations\n", need);
    1084             :     }
    1085       50201 :     if (BQ.PRECREG) {
    1086      189378 :       for (i = triv+1; i<=need; i++) {
    1087      184254 :         gel(mat,i) = zero_zv(BQ.KC);
    1088      184254 :         gel(extraC,i) = cgetr(BQ.PRECREG);
    1089             :       }
    1090        5124 :       real_relations(&BQ, need - triv, &current, s,LIMC,mat + triv,extraC + triv);
    1091             :     } else {
    1092      620858 :       for (i = triv+1; i<=need; i++) {
    1093      575781 :         gel(mat,i) = zero_zv(BQ.KC);
    1094      575781 :         gel(extraC,i) = gen_0;
    1095             :       }
    1096       45077 :       imag_relations(&BQ, need - triv, &current, LIMC,mat + triv);
    1097             :     }
    1098             : 
    1099       50201 :     if (!W)
    1100       40942 :       W = hnfspec_i(mat,BQ.vperm,&dep,&B,&C,nsubFB);
    1101             :     else
    1102        9259 :       W = hnfadd_i(W,BQ.vperm,&dep,&B,&C, mat,extraC);
    1103       50201 :     gerepileall(av2, 4, &W,&C,&B,&dep);
    1104       50201 :     need = BQ.KC - (lg(W)-1) - (lg(B)-1);
    1105       50201 :     if (need)
    1106             :     {
    1107        4055 :       if (++nreldep > 15 && cbach < 1) goto START;
    1108        4055 :       continue;
    1109             :     }
    1110             : 
    1111       46146 :     h = ZM_det_triangular(W);
    1112       46146 :     if (DEBUGLEVEL>2) err_printf("\n#### Tentative class number: %Ps\n", h);
    1113             : 
    1114       46146 :     switch(get_R(&BQ, C, (lg(C)-1) - (lg(B)-1) - (lg(W)-1), mulir(h,invhr), &R))
    1115             :     {
    1116             :       case fupb_PRECI:
    1117           0 :         BQ.PRECREG = precdbl(BQ.PRECREG);
    1118           0 :         FIRST = 1; goto START;
    1119             : 
    1120             :       case fupb_RELAT:
    1121        5204 :         if (++nrelsup > MAXRELSUP)
    1122             :         {
    1123          21 :           if (++sfb_trials > SFB_MAX && cbach <= 1) goto START;
    1124          21 :           if (nsubFB < minss(10,BQ.KC)) nsubFB++;
    1125             :         }
    1126        5204 :         need = minss(BQ.KC, nrelsup);
    1127             :     }
    1128             :   }
    1129       50201 :   while (need);
    1130             :   /* DONE */
    1131       40942 :   if (!quad_be_honest(&BQ)) goto START;
    1132       40942 :   if (DEBUGLEVEL>2) timer_printf(&T, "be honest");
    1133       40942 :   clearhash(BQ.hashtab);
    1134       40942 :   free_GRHcheck(&GRHcheck);
    1135             : 
    1136       40942 :   gen = get_clgp(&BQ,W,&cyc,prec);
    1137       40942 :   gunclone(BQ.subFB);
    1138       40942 :   gunclone(BQ.powsubFB);
    1139       40942 :   res = cgetg(5,t_VEC);
    1140       40942 :   gel(res,1) = h;
    1141       40942 :   gel(res,2) = cyc;
    1142       40942 :   gel(res,3) = gen;
    1143       40942 :   gel(res,4) = R; return gerepilecopy(av0,res);
    1144             : }
    1145             : 
    1146             : GEN
    1147           0 : buchimag(GEN D, GEN c, GEN c2, GEN REL)
    1148           0 : { (void)REL; return Buchquad(D,gtodouble(c),gtodouble(c2),0); }
    1149             : 
    1150             : GEN
    1151           0 : buchreal(GEN D, GEN flag, GEN c, GEN c2, GEN REL, long prec) {
    1152           0 :   if (signe(flag)) pari_err_IMPL("narrow class group");
    1153           0 :   (void)REL; return Buchquad(D,gtodouble(c),gtodouble(c2),prec);
    1154             : }
    1155             : 
    1156             : GEN
    1157       40949 : quadclassunit0(GEN x, long flag, GEN data, long prec)
    1158             : {
    1159             :   long lx;
    1160       40949 :   double c1 = 0.0, c2 = 0.0;
    1161             : 
    1162       40949 :   if (!data) lx=1;
    1163             :   else
    1164             :   {
    1165          21 :     lx = lg(data);
    1166          21 :     if (typ(data)!=t_VEC) pari_err_TYPE("quadclassunit", data);
    1167          21 :     if (lx > 7) pari_err_DIM("quadclassunit [tech vector]");
    1168          21 :     if (lx > 3) lx = 3;
    1169             :   }
    1170       40949 :   switch(lx)
    1171             :   {
    1172          14 :     case 3: c2 = gtodouble(gel(data,2));
    1173          21 :     case 2: c1 = gtodouble(gel(data,1));
    1174             :   }
    1175       40949 :   if (flag) pari_err_IMPL("narrow class group");
    1176       40949 :   return Buchquad(x,c1,c2,prec);
    1177             : }

Generated by: LCOV version 1.11