Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13