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 20459-9710128) Lines: 623 671 92.8 %
Date: 2017-04-29 05:33:44 Functions: 43 47 91.5 %
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     1338866 : 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      997998 : qfr3_canon(GEN x, struct qfr_data *S)
      56             : {
      57      997998 :   GEN a = gel(x,1), c = gel(x,3);
      58      997998 :   if (signe(a) < 0) {
      59      430434 :     if (absequalii(a,c)) return qfr3_rho(x, S);
      60      430428 :     setsigne(a, 1);
      61      430428 :     setsigne(c,-1);
      62             :   }
      63      997992 :   return x;
      64             : }
      65             : static GEN
      66        3180 : qfr3_canon_safe(GEN x, struct qfr_data *S)
      67             : {
      68        3180 :   GEN a = gel(x,1), c = gel(x,3);
      69        3180 :   if (signe(a) < 0) {
      70         162 :     if (absequalii(a,c)) return qfr3_rho(x, S);
      71         162 :     gel(x,1) = negi(a);
      72         162 :     gel(x,3) = negi(c);
      73             :   }
      74        3180 :   return x;
      75             : }
      76             : static GEN
      77     1669098 : qfr5_canon(GEN x, struct qfr_data *S)
      78             : {
      79     1669098 :   GEN a = gel(x,1), c = gel(x,3);
      80     1669098 :   if (signe(a) < 0) {
      81      744366 :     if (absequalii(a,c)) return qfr5_rho(x, S);
      82      744360 :     setsigne(a, 1);
      83      744360 :     setsigne(c,-1);
      84             :   }
      85     1669092 :   return x;
      86             : }
      87             : static GEN
      88     1483146 : QFR5_comp(GEN x,GEN y, struct qfr_data *S)
      89     1483146 : { return qfr5_canon(qfr5_comp(x,y,S), S); }
      90             : static GEN
      91      861732 : QFR3_comp(GEN x, GEN y, struct qfr_data *S)
      92      861732 : { return qfr3_canon(qfr3_comp(x,y,S), S); }
      93             : 
      94             : /* compute rho^n(x) */
      95             : static GEN
      96      189072 : qfr5_rho_pow(GEN x, long n, struct qfr_data *S)
      97             : {
      98             :   long i;
      99      189072 :   pari_sp av = avma;
     100     1899558 :   for (i=1; i<=n; i++)
     101             :   {
     102     1710486 :     x = qfr5_rho(x,S);
     103     1710486 :     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      189072 :   return gerepilecopy(av, x);
     110             : }
     111             : 
     112             : static GEN
     113      185952 : qfr5_pf(struct qfr_data *S, long p, long prec)
     114             : {
     115      185952 :   GEN y = primeform_u(S->D,p);
     116      185952 :   return qfr5_canon(qfr5_red(qfr_to_qfr5(y,prec), S), S);
     117             : }
     118             : 
     119             : static GEN
     120      136248 : qfr3_pf(struct qfr_data *S, long p)
     121             : {
     122      136248 :   GEN y = primeform_u(S->D,p);
     123      136248 :   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     1730274 : init_form(struct buch_quad *B, GEN ex,
     131             :           GEN (*comp)(GEN,GEN,struct qfr_data *S))
     132             : {
     133     1730274 :   long i, l = lg(B->powsubFB);
     134     1730274 :   GEN F = NULL;
     135    11474498 :   for (i=1; i<l; i++)
     136     9744224 :     if (ex[i])
     137             :     {
     138     9136134 :       GEN t = gmael(B->powsubFB,i,ex[i]);
     139     9136134 :       F = F? comp(F, t, B->QFR): t;
     140             :     }
     141     1730274 :   return F;
     142             : }
     143             : static GEN
     144      169236 : qfr5_factorback(struct buch_quad *B, GEN ex) { return init_form(B, ex, &QFR5_comp); }
     145             : 
     146             : static GEN
     147     5792512 : QFI_comp(GEN x, GEN y, struct qfr_data *S) { (void)S; return qficomp(x,y); }
     148             : 
     149             : static GEN
     150       41189 : qfi_factorback(struct buch_quad *B, GEN ex) { return init_form(B, ex, &QFI_comp); }
     151             : 
     152             : static GEN
     153     1519179 : random_form(struct buch_quad *B, GEN ex,
     154             :             GEN (*comp)(GEN,GEN, struct qfr_data *S))
     155             : {
     156     1519179 :   long i, l = lg(ex);
     157     1519179 :   pari_sp av = avma;
     158             :   GEN F;
     159             :   for(;;)
     160             :   {
     161     1519849 :     for (i=1; i<l; i++) ex[i] = random_bits(RANDOM_BITS);
     162     3039028 :     if ((F = init_form(B, ex, comp))) return F;
     163         670 :     avma = av;
     164         670 :   }
     165             : }
     166             : static GEN
     167      138114 : qfr3_random(struct buch_quad *B,GEN ex){ return random_form(B, ex, &QFR3_comp); }
     168             : static GEN
     169     1381065 : 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        1980 : bnf_increase_LIMC(long LIMC, long LIMCMAX)
     178             : {
     179        1980 :   if (LIMC >= LIMCMAX) pari_err_BUG("Buchmann's algorithm");
     180        1980 :   if (LIMC <= LIMCMAX/40) /* cbach <= 0.3 */
     181          12 :     LIMC *= 2;
     182        1968 :   else if (LIMCMAX < 60) /* \Delta_K <= 9 */
     183           0 :     LIMC++;
     184             :   else
     185        1968 :     LIMC += LIMCMAX / 60; /* cbach += 0.2 */
     186        1980 :   if (LIMC > LIMCMAX) LIMC = LIMCMAX;
     187        1980 :   return LIMC;
     188             : }
     189             : 
     190             : /* Is |q| <= p ? */
     191             : static int
     192           0 : isless_iu(GEN q, ulong p) {
     193           0 :   long l = lgefint(q);
     194           0 :   return l==2 || (l == 3 && uel(q,2) <= p);
     195             : }
     196             : 
     197             : static long
     198     2565584 : factorquad(struct buch_quad *B, GEN f, long nFB, ulong limp)
     199             : {
     200             :   ulong X;
     201     2565584 :   long i, lo = 0;
     202     2565584 :   GEN x = gel(f,1), FB = B->FB, P = B->primfact, E = B->exprimfact;
     203             : 
     204     5131168 :   for (i=1; lgefint(x) > 3; i++)
     205             :   {
     206           0 :     ulong p = uel(FB,i), r;
     207           0 :     GEN q = diviu_rem(x, p, &r);
     208           0 :     if (!r)
     209             :     {
     210           0 :       long k = 0;
     211           0 :       do { k++; x = q; q = diviu_rem(x, p, &r); } while (!r);
     212           0 :       lo++; P[lo] = p; E[lo] = k;
     213             :     }
     214           0 :     if (isless_iu(q,p)) {
     215           0 :       if (lgefint(x) == 3) { X = uel(x,2); goto END; }
     216           0 :       return 0;
     217             :     }
     218           0 :     if (i == nFB) return 0;
     219             :   }
     220     2565584 :   X = uel(x,2);
     221     2565584 :   if (X == 1) { P[0] = 0; return 1; }
     222    99966553 :   for (;; i++)
     223             :   { /* single precision affair, split for efficiency */
     224   102440629 :     ulong p = uel(FB,i);
     225   102440629 :     ulong q = X / p, r = X % p; /* gcc makes a single div */
     226   102440629 :     if (!r)
     227             :     {
     228     3833523 :       long k = 0;
     229     4661124 :       do { k++; X = q; q = X / p; r = X % p; } while (!r);
     230     3833523 :       lo++; P[lo] = p; E[lo] = k;
     231             :     }
     232   102440629 :     if (q <= p) break;
     233   100542775 :     if (i == nFB) return 0;
     234    99966553 :   }
     235             : END:
     236     1897854 :   if (X > B->limhash) return 0;
     237     1897830 :   if (X != 1 && X <= limp)
     238             :   {
     239      391787 :     if (B->badprim && ugcd(X, umodiu(B->badprim,X)) > 1) return 0;
     240      391085 :     lo++; P[lo] = X; E[lo] = 1;
     241      391085 :     X = 1;
     242             :   }
     243     1897128 :   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     1338866 : largeprime(struct buch_quad *B, long q, GEN ex, long np, long nrho)
     249             : {
     250     1338866 :   const long hashv = hash(q);
     251     1338866 :   long *pt, i, l = lg(B->subFB);
     252             : 
     253     1527944 :   for (pt = B->hashtab[hashv]; ; pt = (long*) pt[0])
     254             :   {
     255     1527944 :     if (!pt)
     256             :     {
     257     1263381 :       pt = (long*) pari_malloc((l+3) * sizeof(long));
     258     1263381 :       *pt++ = nrho; /* nrho = pt[-3] */
     259     1263381 :       *pt++ = np;   /* np   = pt[-2] */
     260     1263381 :       *pt++ = q;    /* q    = pt[-1] */
     261     1263381 :       pt[0] = (long)B->hashtab[hashv];
     262     1263381 :       for (i=1; i<l; i++) pt[i]=ex[i];
     263     1263381 :       B->hashtab[hashv]=pt; return NULL;
     264             :     }
     265      264563 :     if (pt[-1] == q) break;
     266      189078 :   }
     267      105658 :   for(i=1; i<l; i++)
     268      102502 :     if (pt[i] != ex[i]) return pt;
     269        3156 :   return (pt[-2]==np)? NULL: pt;
     270             : }
     271             : 
     272             : static void
     273       78689 : clearhash(long **hash)
     274             : {
     275             :   long *pt;
     276             :   long i;
     277    80656225 :   for (i=0; i<HASHT; i++) {
     278   162418453 :     for (pt = hash[i]; pt; ) {
     279     1263381 :       void *z = (void*)(pt - 3);
     280     1263381 :       pt = (long*) pt[0]; pari_free(z);
     281             :     }
     282    80577536 :     hash[i] = NULL;
     283             :   }
     284       78689 : }
     285             : 
     286             : /* last prime stored */
     287             : ulong
     288       34884 : 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       46098 : GRH_ensure(GRHcheck_t *S, long nb)
     292             : {
     293       46098 :   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       46098 : }
     300             : /* cache data for all primes up to the LIM */
     301             : static void
     302      417588 : cache_prime_quad(GRHcheck_t *S, ulong LIM, GEN D)
     303             : {
     304             :   GRHprime_t *pr;
     305             :   long nb;
     306             : 
     307      835176 :   if (S->limp >= LIM) return;
     308       36300 :   nb = (long)primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     309       36300 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     310       36300 :   for (pr = S->primes + S->nprimes;;)
     311             :   {
     312     2529276 :     ulong p = u_forprime_next(&(S->P));
     313     2529276 :     pr->p = p;
     314     2529276 :     pr->logp = log((double)p);
     315     2529276 :     pr->dec = (GEN)kroiu(D,p);
     316     2529276 :     S->nprimes++;
     317     2529276 :     pr++;
     318             :     /* store up to nextprime(LIM) included */
     319     2529276 :     if (p >= LIM) { S->limp = p; break; }
     320     2492976 :   }
     321             : }
     322             : 
     323             : static GEN
     324       34884 : compute_invresquad(GRHcheck_t *S)
     325             : {
     326       34884 :   pari_sp av = avma;
     327       34884 :   GEN invres = real_1(DEFAULTPREC);
     328       34884 :   GRHprime_t *pr = S->primes;
     329       34884 :   long i = S->nprimes, LIMC = GRH_last_prime(S)+diffptr[i]-1; /* nextprime(p+1)-1*/
     330       34884 :   double limp = log((double)LIMC) / 2;
     331     2564160 :   for (; i > 0; pr++, i--)
     332             :   {
     333     2529276 :     long s = (long)pr->dec;
     334     2529276 :     if (s)
     335             :     {
     336     2473524 :       ulong p = pr->p;
     337     2473524 :       if (s>0 || pr->logp <= limp)
     338             :         /* Both p and P contribute */
     339     1309374 :         invres = mulur(p - s, divru(invres, p));
     340     1164150 :       else if (s<0)
     341             :         /* Only p contributes */
     342     1164150 :         invres = mulur(p, divru(invres, p - 1));
     343             :     }
     344             :   }
     345       34884 :   return gerepileuptoleaf(av, invres);
     346             : }
     347             : 
     348             : /* p | conductor of order of disc D ? */
     349             : static int
     350      182400 : is_bad(GEN D, ulong p)
     351             : {
     352      182400 :   pari_sp av = avma;
     353             :   int r;
     354      182400 :   if (p == 2)
     355             :   {
     356       46176 :     r = mod16(D) >> 1;
     357       46176 :     if (r && signe(D) < 0) r = 8-r;
     358       46176 :     return (r < 4);
     359             :   }
     360      136224 :   r = (remii(D, sqru(p)) == gen_0); /* p^2 | D ? */
     361      136224 :   avma = av; return r;
     362             : }
     363             : 
     364             : /* returns the n-th suitable ideal for the factorbase */
     365             : static long
     366       34884 : nthidealquad(GEN D, long n)
     367             : {
     368       34884 :   pari_sp av = avma;
     369             :   forprime_t S;
     370             :   ulong p;
     371       34884 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     372      201390 :   while ((p = u_forprime_next(&S)) && n > 0)
     373      131622 :     if (!is_bad(D, p) && kroiu(D, p) >= 0) n--;
     374       34884 :   avma = av; return p;
     375             : }
     376             : 
     377             : static int
     378      345966 : quadGRHchk(GEN D, GRHcheck_t *S, ulong LIMC)
     379             : {
     380      345966 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     381             :   long i;
     382             : 
     383      345966 :   cache_prime_quad(S, LIMC, D);
     384     9911538 :   for (i = 0;; i++)
     385             :   {
     386     9911538 :     GRHprime_t *pr = S->primes+i;
     387     9911538 :     ulong p = pr->p;
     388             :     long M;
     389             :     double logNP, q, A, B;
     390     9911538 :     if (p > LIMC) break;
     391     9565572 :     if ((long)pr->dec < 0)
     392             :     {
     393     4723488 :       logNP = 2 * pr->logp;
     394     4723488 :       q = 1/(double)p;
     395             :     }
     396             :     else
     397             :     {
     398     4842084 :       logNP = pr->logp;
     399     4842084 :       q = 1/sqrt((double)p);
     400             :     }
     401     9565572 :     A = logNP * q; B = logNP * A; M = (long)(logC/logNP);
     402     9565572 :     if (M > 1)
     403             :     {
     404      647766 :       double inv1_q = 1 / (1-q);
     405      647766 :       A *= (1 - pow(q, (double) M)) * inv1_q;
     406      647766 :       B *= (1 - pow(q, (double) M)*(M+1 - M*q)) * inv1_q * inv1_q;
     407             :     }
     408     9565572 :     if ((long)pr->dec>0) { SA += 2*A;SB += 2*B; } else { SA += A; SB += B; }
     409     9565572 :   }
     410      345966 :   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       36738 : FBquad(struct buch_quad *B, ulong C2, ulong C1, GRHcheck_t *S)
     416             : {
     417       36738 :   GEN D = B->QFR->D;
     418             :   long i;
     419             :   pari_sp av;
     420             :   GRHprime_t *pr;
     421             : 
     422       36738 :   cache_prime_quad(S, C2, D);
     423       36738 :   pr = S->primes;
     424       36738 :   B->numFB = cgetg(C2+1, t_VECSMALL);
     425       36738 :   B->FB    = cgetg(C2+1, t_VECSMALL);
     426       36738 :   av = avma;
     427       36738 :   B->KC = 0; i = 0;
     428       36738 :   B->badprim = gen_1;
     429      969894 :   for (;; pr++) /* p <= C2 */
     430             :   {
     431     1006632 :     ulong p = pr->p;
     432     1006632 :     if (!B->KC && p > C1) B->KC = i;
     433     1006632 :     if (p > C2) break;
     434      969894 :     switch ((long)pr->dec)
     435             :     {
     436      473406 :       case -1: break; /* inert */
     437             :       case  0: /* ramified */
     438       50778 :         if (is_bad(D, p)) { B->badprim = muliu(B->badprim, p); break; }
     439             :         /* fall through */
     440             :       default:  /* split */
     441      496296 :         i++; B->numFB[p] = i; B->FB[i] = p; break;
     442             :     }
     443      969894 :   }
     444       36738 :   B->KC2 = i;
     445       36738 :   setlg(B->FB, B->KC2+1);
     446       36738 :   if (B->badprim != gen_1)
     447         168 :     B->badprim = gerepileuptoint(av, B->badprim);
     448             :   else
     449             :   {
     450       36570 :     B->badprim = NULL; avma = av;
     451             :   }
     452       36738 : }
     453             : 
     454             : /* create B->vperm, return B->subFB */
     455             : static GEN
     456       36738 : subFBquad(struct buch_quad *B, GEN D, double PROD, long minSFB)
     457             : {
     458       36738 :   long i, j, lgsub = 1, ino = 1, lv = B->KC+1;
     459       36738 :   double prod = 1.;
     460             :   pari_sp av;
     461             :   GEN no;
     462             : 
     463       36738 :   B->vperm = cgetg(lv, t_VECSMALL);
     464       36738 :   av = avma;
     465       36738 :   no    = cgetg(lv, t_VECSMALL);
     466      143844 :   for (j = 1; j < lv; j++)
     467             :   {
     468      141966 :     ulong p = uel(B->FB,j);
     469      141966 :     if (!umodiu(D, p)) no[ino++] = j; /* ramified */
     470             :     else
     471             :     {
     472      100362 :       B->vperm[lgsub++] = j;
     473      100362 :       prod *= p;
     474      100362 :       if (lgsub > minSFB && prod > PROD) break;
     475             :     }
     476             :   }
     477             :   /* lgsub >= 1 otherwise quadGRHchk is false */
     478       36738 :   i = lgsub;
     479       36738 :   for (j = 1; j < ino;i++,j++) B->vperm[i] = no[j];
     480       36738 :   for (     ; i < lv; i++)     B->vperm[i] = i;
     481       36738 :   no = gclone(vecslice(B->vperm, 1, lgsub-1));
     482       36738 :   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       41951 : powsubFBquad(struct buch_quad *B, long n)
     488             : {
     489       41951 :   pari_sp av = avma;
     490       41951 :   long i,j, l = lg(B->subFB);
     491       41951 :   GEN F, y, x = cgetg(l, t_VEC), D = B->QFR->D;
     492             : 
     493       41951 :   if (B->PRECREG) /* real */
     494             :   {
     495       33504 :     for (i=1; i<l; i++)
     496             :     {
     497       29190 :       F = qfr5_pf(B->QFR, B->FB[B->subFB[i]], B->PRECREG);
     498       29190 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     499       29190 :       gel(y,1) = F;
     500       29190 :       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      141146 :     for (i=1; i<l; i++)
     506             :     {
     507      103509 :       F = qfi_pf(D, B->FB[B->subFB[i]]);
     508      103509 :       y = cgetg(n+1, t_VEC); gel(x,i) = y;
     509      103509 :       gel(y,1) = F;
     510      103509 :       for (j=2; j<=n; j++) gel(y,j) = qficomp(gel(y,j-1), F);
     511             :     }
     512             :   }
     513       41951 :   x = gclone(x); avma = av; return x;
     514             : }
     515             : 
     516             : static void
     517       36366 : sub_fact(struct buch_quad *B, GEN col, GEN F)
     518             : {
     519       36366 :   GEN b = gel(F,2);
     520             :   long i;
     521      110790 :   for (i=1; i<=B->primfact[0]; i++)
     522             :   {
     523       74424 :     ulong p = B->primfact[i], k = B->numFB[p];
     524       74424 :     long e = B->exprimfact[i];
     525       74424 :     if (umodiu(b, p<<1) > p) e = -e;
     526       74424 :     col[k] -= e;
     527             :   }
     528       36366 : }
     529             : static void
     530      685265 : add_fact(struct buch_quad *B, GEN col, GEN F)
     531             : {
     532      685265 :   GEN b = gel(F,2);
     533             :   long i;
     534     2253507 :   for (i=1; i<=B->primfact[0]; i++)
     535             :   {
     536     1568242 :     ulong p = B->primfact[i], k = B->numFB[p];
     537     1568242 :     long e = B->exprimfact[i];
     538     1568242 :     if (umodiu(b, p<<1) > p) e = -e;
     539     1568242 :     col[k] += e;
     540             :   }
     541      685265 : }
     542             : 
     543             : static GEN
     544       34884 : get_clgp(struct buch_quad *B, GEN W, GEN *ptD, long prec)
     545             : {
     546       34884 :   GEN res, init, u1, D = ZM_snf_group(W,NULL,&u1), Z = prec? real_0(prec): NULL;
     547       34884 :   long i, j, l = lg(W), c = lg(D);
     548             : 
     549       34884 :   res=cgetg(c,t_VEC); init = cgetg(l,t_VEC);
     550       34884 :   for (i=1; i<l; i++) gel(init,i) = primeform_u(B->QFR->D, B->FB[B->vperm[i]]);
     551       76044 :   for (j=1; j<c; j++)
     552             :   {
     553       41160 :     GEN g = NULL;
     554       41160 :     if (prec)
     555             :     {
     556       11394 :       for (i=1; i<l; i++)
     557             :       {
     558        8214 :         GEN t, u = gcoeff(u1,i,j);
     559        8214 :         if (!signe(u)) continue;
     560        3762 :         t = qfr3_pow(gel(init,i), u, B->QFR);
     561        3762 :         g = g? qfr3_comp(g, t, B->QFR): t;
     562             :       }
     563        3180 :       g = qfr3_to_qfr(qfr3_canon_safe(qfr3_red(g, B->QFR), B->QFR), Z);
     564             :     }
     565             :     else
     566             :     {
     567      113292 :       for (i=1; i<l; i++)
     568             :       {
     569       75312 :         GEN t, u = gcoeff(u1,i,j);
     570       75312 :         if (!signe(u)) continue;
     571       52144 :         t = powgi(gel(init,i), u);
     572       52144 :         g = g? qficomp(g, t): t;
     573             :       }
     574             :     }
     575       41160 :     gel(res,j) = g;
     576             :   }
     577       34884 :   *ptD = D; return res;
     578             : }
     579             : 
     580             : static long
     581       34884 : trivial_relations(struct buch_quad *B, GEN mat, GEN C)
     582             : {
     583       34884 :   long i, j = 0;
     584       34884 :   GEN col, D = B->QFR->D;
     585      524274 :   for (i = 1; i <= B->KC; i++)
     586             :   { /* ramified prime ==> trivial relation */
     587      489390 :     if (umodiu(D, B->FB[i])) continue;
     588       45966 :     col = zero_zv(B->KC);
     589       45966 :     col[i] = 2; j++;
     590       45966 :     gel(mat,j) = col;
     591       45966 :     gel(C,j) = gen_0;
     592             :   }
     593       34884 :   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       38419 : imag_relations(struct buch_quad *B, long need, long *pc, ulong LIMC, GEN mat)
     607             : {
     608             :   pari_timer T;
     609       38419 :   long lgsub = lg(B->subFB), current = *pc, nbtest = 0, s = 0;
     610             :   long i, fpc;
     611             :   pari_sp av;
     612       38419 :   GEN col, form, ex = cgetg(lgsub, t_VECSMALL);
     613             : 
     614       38419 :   if (!current) current = 1;
     615       38419 :   if (DEBUGLEVEL>2) timer_start(&T);
     616       38419 :   av = avma;
     617             :   for(;;)
     618             :   {
     619     1419082 :     if (s >= need) break;
     620     1380663 :     avma = av;
     621     1380663 :     form = qfi_random(B,ex);
     622     1380663 :     form = qficomp(form, qfi_pf(B->QFR->D, B->FB[current]));
     623     1380663 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     624     1380663 :     if (!fpc)
     625             :     {
     626      246510 :       if (DEBUGLEVEL>3) err_printf(".");
     627      246510 :       if ((nbtest & 0xff) == 0 && ++current > B->KC) current = 1;
     628      246510 :       continue;
     629             :     }
     630     1134153 :     if (fpc > 1)
     631             :     {
     632      683912 :       long *fpd = largeprime(B,fpc,ex,current,0);
     633             :       ulong b1, b2, p;
     634             :       GEN form2;
     635      683912 :       if (!fpd)
     636             :       {
     637      642723 :         if (DEBUGLEVEL>3) err_printf(".");
     638      642723 :         continue;
     639             :       }
     640       41189 :       form2 = qficomp(qfi_factorback(B,fpd), qfi_pf(B->QFR->D, B->FB[fpd[-2]]));
     641       41189 :       p = fpc << 1;
     642       41189 :       b1 = umodiu(gel(form2,2), p);
     643       41189 :       b2 = umodiu(gel(form,2),  p);
     644       41189 :       if (b1 != b2 && b1+b2 != p) continue;
     645             : 
     646       41159 :       col = gel(mat,++s);
     647       41159 :       add_fact(B,col, form);
     648       41159 :       (void)factorquad(B,form2,B->KC,LIMC);
     649       41159 :       if (b1==b2)
     650             :       {
     651       20736 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     652       20736 :         sub_fact(B, col, form2); col[fpd[-2]]++;
     653             :       }
     654             :       else
     655             :       {
     656       20423 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     657       20423 :         add_fact(B, col, form2); col[fpd[-2]]--;
     658             :       }
     659       41159 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     660             :     }
     661             :     else
     662             :     {
     663      450241 :       col = gel(mat,++s);
     664      450241 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     665      450241 :       add_fact(B, col, form);
     666      450241 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     667             :     }
     668      491400 :     col[current]--;
     669      491400 :     if (++current > B->KC) current = 1;
     670     1380663 :   }
     671       38419 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     672       38419 :   *pc = current;
     673       38419 : }
     674             : 
     675             : static int
     676           6 : imag_be_honest(struct buch_quad *B)
     677             : {
     678           6 :   long p, fpc, s = B->KC, nbtest = 0;
     679           6 :   GEN F, ex = cgetg(lg(B->subFB), t_VECSMALL);
     680           6 :   pari_sp av = avma;
     681             : 
     682         414 :   while (s<B->KC2)
     683             :   {
     684         402 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     685         402 :     F = qficomp(qfi_pf(B->QFR->D, p), qfi_random(B, ex));
     686         402 :     fpc = factorquad(B,F,s,p-1);
     687         402 :     if (fpc == 1) { nbtest=0; s++; }
     688             :     else
     689         276 :       if (++nbtest > 40) return 0;
     690         402 :     avma = av;
     691             :   }
     692           6 :   return 1;
     693             : }
     694             : 
     695             : /* Real Quadratic fields */
     696             : 
     697             : static void
     698        4392 : real_relations(struct buch_quad *B, long need, long *pc, long lim, ulong LIMC, GEN mat, GEN C)
     699             : {
     700             :   pari_timer T;
     701        4392 :   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        4392 :   int first = (current == 0);
     706             :   pari_sp av, av1;
     707        4392 :   GEN d, col, form, form0, form1, ex = cgetg(lgsub, t_VECSMALL);
     708             : 
     709        4392 :   if (DEBUGLEVEL>2) timer_start(&T);
     710        4392 :   if (!current) current = 1;
     711        4392 :   if (lim > need) lim = need;
     712        4392 :   av = avma;
     713             :   for(;;)
     714             :   {
     715      142488 :     if (s >= need) break;
     716      138096 :     if (first && s >= lim) {
     717        1836 :       first = 0;
     718        1836 :       if (DEBUGLEVEL>2) dbg_all(&T, "initial", s, nbtest);
     719             :     }
     720      138096 :     avma = av; form = qfr3_random(B, ex);
     721      138096 :     if (!first)
     722      136230 :       form = QFR3_comp(form, qfr3_pf(B->QFR, B->FB[current]), B->QFR);
     723      138096 :     av1 = avma;
     724      138096 :     form0 = form; form1 = NULL;
     725      138096 :     endcycle = rhoacc = 0;
     726      138096 :     rho = -1;
     727             : 
     728             : CYCLE:
     729     1112202 :     if (endcycle || rho > 5000)
     730             :     {
     731          18 :       if (++current > B->KC) current = 1;
     732          18 :       continue;
     733             :     }
     734     1112184 :     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     1112184 :     if (rho < 0) rho = 0; /* first time in */
     740             :     else
     741             :     {
     742      974088 :       form = qfr3_rho(form, B->QFR); rho++;
     743      974088 :       rhoacc++;
     744      974088 :       if (first)
     745      380940 :         endcycle = (absequalii(gel(form,1),gel(form0,1))
     746      190470 :              && equalii(gel(form,2),gel(form0,2)));
     747             :       else
     748             :       {
     749      783618 :         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      783618 :           { setsigne(form[1],1); setsigne(form[3],-1); }
     758      783666 :         if (equalii(gel(form,1),gel(form0,1)) &&
     759          48 :             equalii(gel(form,2),gel(form0,2))) continue;
     760             :       }
     761             :     }
     762     1112184 :     nbtest++; fpc = factorquad(B,form,B->KC,LIMC);
     763     1112184 :     if (!fpc)
     764             :     {
     765      330438 :       if (DEBUGLEVEL>3) err_printf(".");
     766      330438 :       goto CYCLE;
     767             :     }
     768      781746 :     if (fpc > 1)
     769             :     { /* look for Large Prime relation */
     770      654954 :       long *fpd = largeprime(B,fpc,ex,first? 0: current,rhoacc);
     771             :       ulong b1, b2, p;
     772             :       GEN form2;
     773      654954 :       if (!fpd)
     774             :       {
     775      623814 :         if (DEBUGLEVEL>3) err_printf(".");
     776      623814 :         goto CYCLE;
     777             :       }
     778       31140 :       if (!form1)
     779             :       {
     780       31140 :         form1 = qfr5_factorback(B,ex);
     781       31140 :         if (!first)
     782       31140 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     783             :       }
     784       31140 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     785       31140 :       rho = 0;
     786             : 
     787       31140 :       form2 = qfr5_factorback(B,fpd);
     788       31140 :       if (fpd[-2])
     789       20532 :         form2 = QFR5_comp(form2, qfr5_pf(B->QFR, B->FB[fpd[-2]], prec), B->QFR);
     790       31140 :       form2 = qfr5_rho_pow(form2, fpd[-3], B->QFR);
     791       31140 :       if (!absequalii(gel(form2,1),gel(form2,3)))
     792             :       {
     793       31140 :         setsigne(form2[1], 1);
     794       31140 :         setsigne(form2[3],-1);
     795             :       }
     796       31140 :       p = fpc << 1;
     797       31140 :       b1 = umodiu(gel(form2,2), p);
     798       31140 :       b2 = umodiu(gel(form1,2), p);
     799       31140 :       if (b1 != b2 && b1+b2 != p) goto CYCLE;
     800             : 
     801       31140 :       col = gel(mat,++s);
     802       31140 :       add_fact(B, col, form1);
     803       31140 :       (void)factorquad(B,form2,B->KC,LIMC);
     804       31140 :       if (b1==b2)
     805             :       {
     806       15630 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += fpd[i]-ex[i];
     807       15630 :         sub_fact(B,col, form2);
     808       15630 :         if (fpd[-2]) col[fpd[-2]]++;
     809       31260 :         d = qfr5_dist(subii(gel(form1,4),gel(form2,4)),
     810       31260 :                       divrr(gel(form1,5),gel(form2,5)), prec);
     811             :       }
     812             :       else
     813             :       {
     814       15510 :         for (i=1; i<lgsub; i++) col[B->subFB[i]] += -fpd[i]-ex[i];
     815       15510 :         add_fact(B, col, form2);
     816       15510 :         if (fpd[-2]) col[fpd[-2]]--;
     817       31020 :         d = qfr5_dist(addii(gel(form1,4),gel(form2,4)),
     818       31020 :                       mulrr(gel(form1,5),gel(form2,5)), prec);
     819             :       }
     820       31140 :       if (DEBUGLEVEL>2) err_printf(" %ldP",s);
     821             :     }
     822             :     else
     823             :     { /* standard relation */
     824      126792 :       if (!form1)
     825             :       {
     826      106956 :         form1 = qfr5_factorback(B, ex);
     827      106956 :         if (!first)
     828      105090 :           form1 = QFR5_comp(form1, qfr5_pf(B->QFR, B->FB[current], prec), B->QFR);
     829             :       }
     830      126792 :       form1 = qfr5_rho_pow(form1, rho, B->QFR);
     831      126792 :       rho = 0;
     832             : 
     833      126792 :       col = gel(mat,++s);
     834      126792 :       for (i=1; i<lgsub; i++) col[B->subFB[i]] = -ex[i];
     835      126792 :       add_fact(B, col, form1);
     836      126792 :       d = qfr5_dist(gel(form1,4), gel(form1,5), prec);
     837      126792 :       if (DEBUGLEVEL>2) err_printf(" %ld",s);
     838             :     }
     839      157932 :     affrr(d, gel(C,s));
     840      157932 :     if (first)
     841             :     {
     842       21702 :       if (s >= lim) continue;
     843       19854 :       goto CYCLE;
     844             :     }
     845             :     else
     846             :     {
     847      136230 :       col[current]--;
     848      136230 :       if (++current > B->KC) current = 1;
     849             :     }
     850      138096 :   }
     851        4392 :   if (DEBUGLEVEL>2) dbg_all(&T, "random", s, nbtest);
     852        4392 :   *pc = current;
     853        4392 : }
     854             : 
     855             : static int
     856           6 : real_be_honest(struct buch_quad *B)
     857             : {
     858           6 :   long p, fpc, s = B->KC, nbtest = 0;
     859           6 :   GEN F,F0, ex = cgetg(lg(B->subFB), t_VECSMALL);
     860           6 :   pari_sp av = avma;
     861             : 
     862          30 :   while (s<B->KC2)
     863             :   {
     864          18 :     p = B->FB[s+1]; if (DEBUGLEVEL>2) err_printf(" %ld",p);
     865          18 :     F = QFR3_comp(qfr3_random(B, ex), qfr3_pf(B->QFR, p), B->QFR);
     866          18 :     for (F0 = F;;)
     867             :     {
     868          36 :       fpc = factorquad(B,F,s,p-1);
     869          36 :       if (fpc == 1) { nbtest=0; s++; break; }
     870          18 :       if (++nbtest > 40) return 0;
     871          18 :       F = qfr3_canon(qfr3_rho(F, B->QFR), B->QFR);
     872          18 :       if (equalii(gel(F,1),gel(F0,1))
     873           0 :        && equalii(gel(F,2),gel(F0,2))) break;
     874          18 :     }
     875          18 :     avma = av;
     876             :   }
     877           6 :   return 1;
     878             : }
     879             : 
     880             : static GEN
     881       27450 : gcdreal(GEN a,GEN b)
     882             : {
     883       27450 :   if (!signe(a)) return mpabs(b);
     884       26892 :   if (!signe(b)) return mpabs(a);
     885       26760 :   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       26760 :   else if (typ(b)==t_INT)
     891           0 :     b = itor(b, lg(a));
     892       26760 :   if (expo(a)<-5) return absr(b);
     893        9690 :   if (expo(b)<-5) return absr(a);
     894        7248 :   a = absr(a); b = absr(b);
     895       23244 :   while (expo(b) >= -5  && signe(b))
     896             :   {
     897             :     long e;
     898        8748 :     GEN r, q = gcvtoi(divrr(a,b),&e);
     899        8748 :     if (e > 0) return NULL;
     900        8748 :     r = subrr(a, mulir(q,b)); a = b; b = r;
     901             :   }
     902        7248 :   return absr(a);
     903             : }
     904             : 
     905             : static int
     906       39335 : get_R(struct buch_quad *B, GEN C, long sreg, GEN z, GEN *ptR)
     907             : {
     908       39335 :   GEN R = gen_1;
     909             :   double c;
     910             :   long i;
     911             : 
     912       39335 :   if (B->PRECREG)
     913             :   {
     914        2466 :     R = mpabs(gel(C,1));
     915       29916 :     for (i=2; i<=sreg; i++)
     916             :     {
     917       27450 :       R = gcdreal(gel(C,i), R);
     918       27450 :       if (!R) return fupb_PRECI;
     919             :     }
     920        2466 :     if (gexpo(R) <= -3)
     921             :     {
     922           0 :       if (DEBUGLEVEL>2) err_printf("regulator is zero.\n");
     923           0 :       return fupb_RELAT;
     924             :     }
     925        2466 :     if (DEBUGLEVEL>2) err_printf("#### Tentative regulator: %Ps\n",R);
     926             :   }
     927       39335 :   c = gtodouble(gmul(z, R));
     928       39335 :   if (c < 0.8 || c > 1.3) return fupb_RELAT;
     929       34884 :   *ptR = R; return fupb_NONE;
     930             : }
     931             : 
     932             : static int
     933       34884 : quad_be_honest(struct buch_quad *B)
     934             : {
     935             :   int r;
     936       34884 :   if (B->KC2 <= B->KC) return 1;
     937          12 :   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          12 :   r = B->PRECREG? real_be_honest(B): imag_be_honest(B);
     940          12 :   if (DEBUGLEVEL>2) err_printf("\n");
     941          12 :   return r;
     942             : }
     943             : 
     944             : 
     945             : GEN
     946       34890 : Buchquad(GEN D, double cbach, double cbach2, long prec)
     947             : {
     948       34890 :   const long MAXRELSUP = 7, SFB_MAX = 3;
     949             :   pari_timer T;
     950       34890 :   pari_sp av0 = avma, av, av2;
     951       34890 :   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       34890 :   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       34890 :   int FIRST = 1;
     960             : 
     961       34890 :   check_quaddisc(D, &s, /*junk*/&i, "Buchquad");
     962       34890 :   R = NULL; /* -Wall */
     963       34890 :   BQ.QFR = &QFR;
     964       34890 :   QFR.D = D;
     965       34890 :   if (s < 0)
     966             :   {
     967       33042 :     if (abscmpiu(QFR.D,4) <= 0)
     968             :     {
     969           6 :       GEN z = cgetg(5,t_VEC);
     970           6 :       gel(z,1) = gel(z,4) = gen_1; gel(z,2) = gel(z,3) = cgetg(1,t_VEC);
     971           6 :       return z;
     972             :     }
     973       33036 :     prec = BQ.PRECREG = 0;
     974             :   } else {
     975        1848 :     BQ.PRECREG = maxss(prec+EXTRAPRECWORD, nbits2prec(2*expi(QFR.D) + 128));
     976             :   }
     977       34884 :   if (DEBUGLEVEL>2) timer_start(&T);
     978       34884 :   BQ.primfact   = new_chunk(100);
     979       34884 :   BQ.exprimfact = new_chunk(100);
     980       34884 :   BQ.hashtab = (long**) new_chunk(HASHT);
     981       34884 :   for (i=0; i<HASHT; i++) BQ.hashtab[i] = NULL;
     982             : 
     983       34884 :   drc = fabs(gtodouble(QFR.D));
     984       34884 :   LOGD = log(drc);
     985       34884 :   LOGD2 = LOGD * LOGD;
     986             : 
     987       34884 :   sdrc = lim = sqrt(drc);
     988       34884 :   if (!BQ.PRECREG) lim /= sqrt(3.);
     989       34884 :   cp = (ulong)exp(sqrt(LOGD*log(LOGD)/8.0));
     990       34884 :   if (cp < 20) cp = 20;
     991       34884 :   if (cbach > 6.) {
     992           0 :     if (cbach2 < cbach) cbach2 = cbach;
     993           0 :     cbach = 6.;
     994             :   }
     995       34884 :   if (cbach < 0.)
     996           0 :     pari_err_DOMAIN("Buchquad","Bach constant","<",gen_0,dbltor(cbach));
     997       34884 :   av = avma;
     998       34884 :   BQ.powsubFB = BQ.subFB = NULL;
     999       34884 :   minSFB = (expi(D) > 15)? 3: 2;
    1000       34884 :   init_GRHcheck(&GRHcheck, 2, BQ.PRECREG? 2: 0, LOGD);
    1001       34884 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    1002       34884 :   LIMCMAX = (long)(6.*LOGD2);
    1003             :   /* 97/1223 below to ensure a good enough approximation of residue */
    1004       34884 :   cache_prime_quad(&GRHcheck, expi(D) < 16 ? 97: 1223, D);
    1005      242724 :   while (!quadGRHchk(D, &GRHcheck, high))
    1006             :   {
    1007      172956 :     low = high;
    1008      172956 :     high *= 2;
    1009             :   }
    1010      207894 :   while (high - low > 1)
    1011             :   {
    1012      138126 :     long test = (low+high)/2;
    1013      138126 :     if (quadGRHchk(D, &GRHcheck, test))
    1014       76332 :       high = test;
    1015             :     else
    1016       61794 :       low = test;
    1017             :   }
    1018       34884 :   if (high == LIMC0+1 && quadGRHchk(D, &GRHcheck, LIMC0))
    1019           0 :     LIMC2 = LIMC0;
    1020             :   else
    1021       34884 :     LIMC2 = high;
    1022       34884 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    1023       34884 :   LIMC0 = (long)(cbach*LOGD2);
    1024       34884 :   LIMC = cbach ? LIMC0 : LIMC2;
    1025       34884 :   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       36738 :     if (!FIRST) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    1032       36738 :     if (DEBUGLEVEL>2 && LIMC > LIMC0)
    1033           0 :       err_printf("%s*** Bach constant: %f\n", FIRST?"":"\n", LIMC/LOGD2);
    1034       36738 :     FIRST = 0; avma = av;
    1035       36738 :     if (BQ.subFB) gunclone(BQ.subFB);
    1036       36738 :     if (BQ.powsubFB) gunclone(BQ.powsubFB);
    1037       36738 :     clearhash(BQ.hashtab);
    1038       36738 :     if (LIMC < cp) LIMC = cp;
    1039       36738 :     if (LIMC2 < LIMC) LIMC2 = LIMC;
    1040       36738 :     if (BQ.PRECREG) qfr_data_init(QFR.D, BQ.PRECREG, &QFR);
    1041             : 
    1042       36738 :     FBquad(&BQ, LIMC2, LIMC, &GRHcheck);
    1043       36738 :     if (DEBUGLEVEL>2) timer_printf(&T, "factor base");
    1044       36738 :     BQ.subFB = subFBquad(&BQ, QFR.D, lim + 0.5, minSFB);
    1045       36738 :     if (DEBUGLEVEL>2) timer_printf(&T, "subFBquad = %Ps",
    1046             :                                  vecpermute(BQ.FB, BQ.subFB));
    1047       36738 :     nsubFB = lg(BQ.subFB) - 1;
    1048             :   }
    1049       36738 :   while (nsubFB < (expi(D) > 15 ? 3 : 2));
    1050             :   /* invhr = 2^r1 (2pi)^r2 / sqrt(D) w ~ L(chi,1) / hR */
    1051       34884 :   invhr = gmul(dbltor((BQ.PRECREG?2.:M_PI)/sdrc), compute_invresquad(&GRHcheck));
    1052       34884 :   BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1053       34884 :   if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1054       34884 :   BQ.limhash = (LIMC & HIGHMASK)? (HIGHBIT>>1): LIMC*LIMC;
    1055             : 
    1056       34884 :   need = BQ.KC + RELSUP - 2;
    1057       34884 :   current = 0;
    1058       34884 :   W = NULL;
    1059       34884 :   sfb_trials = nreldep = nrelsup = 0;
    1060       34884 :   s = nsubFB + RELSUP;
    1061       34884 :   av2 = avma;
    1062             : 
    1063             :   do
    1064             :   {
    1065       42811 :     if ((nreldep & 3) == 1 || (nrelsup & 7) == 1) {
    1066        7067 :       if (DEBUGLEVEL>2) err_printf("*** Changing sub factor base\n");
    1067        7067 :       gunclone(BQ.subFB);
    1068        7067 :       gunclone(BQ.powsubFB);
    1069        7067 :       BQ.subFB = gclone(vecslice(BQ.vperm, 1, nsubFB));
    1070        7067 :       BQ.powsubFB = powsubFBquad(&BQ,CBUCH+1);
    1071        7067 :       if (DEBUGLEVEL>2) timer_printf(&T, "powsubFBquad");
    1072        7067 :       clearhash(BQ.hashtab);
    1073             :     }
    1074       42811 :     need += 2;
    1075       42811 :     mat    = cgetg(need+1, t_MAT);
    1076       42811 :     extraC = cgetg(need+1, t_VEC);
    1077       42811 :     if (!W) { /* first time */
    1078       34884 :       C = extraC;
    1079       34884 :       triv = trivial_relations(&BQ, mat, C);
    1080       34884 :       if (DEBUGLEVEL>2) err_printf("KC = %ld, need %ld relations\n", BQ.KC, need);
    1081             :     } else {
    1082        7927 :       triv = 0;
    1083        7927 :       if (DEBUGLEVEL>2) err_printf("...need %ld more relations\n", need);
    1084             :     }
    1085       42811 :     if (BQ.PRECREG) {
    1086      162324 :       for (i = triv+1; i<=need; i++) {
    1087      157932 :         gel(mat,i) = zero_zv(BQ.KC);
    1088      157932 :         gel(extraC,i) = cgetr(BQ.PRECREG);
    1089             :       }
    1090        4392 :       real_relations(&BQ, need - triv, &current, s,LIMC,mat + triv,extraC + triv);
    1091             :     } else {
    1092      529819 :       for (i = triv+1; i<=need; i++) {
    1093      491400 :         gel(mat,i) = zero_zv(BQ.KC);
    1094      491400 :         gel(extraC,i) = gen_0;
    1095             :       }
    1096       38419 :       imag_relations(&BQ, need - triv, &current, LIMC,mat + triv);
    1097             :     }
    1098             : 
    1099       42811 :     if (!W)
    1100       34884 :       W = hnfspec_i(mat,BQ.vperm,&dep,&B,&C,nsubFB);
    1101             :     else
    1102        7927 :       W = hnfadd_i(W,BQ.vperm,&dep,&B,&C, mat,extraC);
    1103       42811 :     gerepileall(av2, 4, &W,&C,&B,&dep);
    1104       42811 :     need = BQ.KC - (lg(W)-1) - (lg(B)-1);
    1105       42811 :     if (need)
    1106             :     {
    1107        3476 :       if (++nreldep > 15 && cbach < 1) goto START;
    1108        3476 :       continue;
    1109             :     }
    1110             : 
    1111       39335 :     h = ZM_det_triangular(W);
    1112       39335 :     if (DEBUGLEVEL>2) err_printf("\n#### Tentative class number: %Ps\n", h);
    1113             : 
    1114       39335 :     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        4451 :         if (++nrelsup > MAXRELSUP)
    1122             :         {
    1123          18 :           if (++sfb_trials > SFB_MAX && cbach <= 1) goto START;
    1124          18 :           if (nsubFB < minss(10,BQ.KC)) nsubFB++;
    1125             :         }
    1126        4451 :         need = minss(BQ.KC, nrelsup);
    1127             :     }
    1128             :   }
    1129       42811 :   while (need);
    1130             :   /* DONE */
    1131       34884 :   if (!quad_be_honest(&BQ)) goto START;
    1132       34884 :   if (DEBUGLEVEL>2) timer_printf(&T, "be honest");
    1133       34884 :   clearhash(BQ.hashtab);
    1134       34884 :   free_GRHcheck(&GRHcheck);
    1135             : 
    1136       34884 :   gen = get_clgp(&BQ,W,&cyc,prec);
    1137       34884 :   gunclone(BQ.subFB);
    1138       34884 :   gunclone(BQ.powsubFB);
    1139       34884 :   res = cgetg(5,t_VEC);
    1140       34884 :   gel(res,1) = h;
    1141       34884 :   gel(res,2) = cyc;
    1142       34884 :   gel(res,3) = gen;
    1143       34884 :   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       34890 : quadclassunit0(GEN x, long flag, GEN data, long prec)
    1158             : {
    1159             :   long lx;
    1160       34890 :   double c1 = 0.0, c2 = 0.0;
    1161             : 
    1162       34890 :   if (!data) lx=1;
    1163             :   else
    1164             :   {
    1165          18 :     lx = lg(data);
    1166          18 :     if (typ(data)!=t_VEC) pari_err_TYPE("quadclassunit", data);
    1167          18 :     if (lx > 7) pari_err_DIM("quadclassunit [tech vector]");
    1168          18 :     if (lx > 3) lx = 3;
    1169             :   }
    1170       34890 :   switch(lx)
    1171             :   {
    1172          12 :     case 3: c2 = gtodouble(gel(data,2));
    1173          18 :     case 2: c1 = gtodouble(gel(data,1));
    1174             :   }
    1175       34890 :   if (flag) pari_err_IMPL("narrow class group");
    1176       34890 :   return Buchquad(x,c1,c2,prec);
    1177             : }

Generated by: LCOV version 1.11