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 - random.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 152 152 100.0 %
Date: 2020-06-04 05:59:24 Functions: 17 17 100.0 %
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             : /********************************************************************/
      14             : /*                                                                  */
      15             : /*                      PSEUDO-RANDOM INTEGERS                      */
      16             : /*                                                                  */
      17             : /********************************************************************/
      18             : #include "pari.h"
      19             : #include "paripriv.h"
      20             : /********************************************************************/
      21             : /*                    XORGEN (Richard P. Brent)                     */
      22             : /*          http://wwwmaths.anu.edu.au/~brent/random.html           */
      23             : /*        (initial adaptation to PARI/GP by Randall Rathbun)        */
      24             : /********************************************************************/
      25             : /* Adapted from xorgens.c version 3.04, Richard P. Brent, 20060628 (GPL).
      26             :  * 32-bit or 64-bit integer random number generator with period at
      27             :  * least 2**4096-1. It is assumed that "ulong" is a 32-bit or 64-bit integer */
      28             : 
      29             : #ifdef LONG_IS_64BIT
      30             :   typedef ulong u64;
      31             : #else
      32             :   typedef unsigned long long u64;
      33             : static u64
      34        1171 : _32to64(ulong a, ulong b) { u64 v = a; return (v<<32)|b; }
      35             : static void
      36     3753119 : _64to32(u64 v, ulong *a, ulong *b) { *a = v>>32; *b = v&0xFFFFFFFF; }
      37             : #endif
      38             : static THREAD u64 state[64];
      39             : static THREAD u64 xorgen_w;
      40             : static THREAD int xorgen_i;
      41             : /* weyl = odd approximation to 2^64*(sqrt(5)-1)/2. */
      42             : static const u64 weyl = (((u64)0x61c88646U)<<32)|((u64)0x80b583ebU);
      43             : 
      44             : static u64
      45    87911420 : block(void)
      46             : {
      47    87911420 :   const int r = 64;
      48    87911420 :   const int a = 33, b = 26, c = 27, d = 29, s = 53;
      49             :   u64 t, v, w;
      50    87911420 :   xorgen_i = (xorgen_i+1)&(r-1);
      51    87911420 :   t = state[xorgen_i];
      52    87911420 :   v = state[(xorgen_i+(r-s))&(r-1)];   /* index is (i-s) mod r */
      53    87911420 :   t ^= t<<a; t ^= t>>b;                   /* (I + L^a)(I + R^b) */
      54    87911420 :   v ^= v<<c; v ^= v>>d;                   /* (I + L^c)(I + R^d) */
      55    87911420 :   w = t^v;
      56    87911420 :   return state[xorgen_i] = w;               /* update circular array */
      57             : }
      58             : 
      59             : /* v > 0 */
      60             : static void
      61      214736 : init_xor4096i(u64 v)
      62             : {
      63      214736 :   const int r = 64;
      64             :   int k;
      65             : 
      66    13624682 :   for (k = r; k > 0; k--) {/* avoid correlations for close seeds */
      67    13409946 :     v ^= v<<10; v ^= v>>15; /* recurrence has period 2**64-1 */
      68    13409946 :     v ^= v<<4;  v ^= v>>13;
      69             :   }
      70    13514177 :   for (xorgen_w = v, k = 0; k < r; k++) { /* initialise circular array */
      71    13299441 :     v ^= v<<10; v ^= v>>15;
      72    13299441 :     v ^= v<<4;  v ^= v>>13;
      73    13299441 :     state[k] = v + (xorgen_w+=weyl);
      74             :   }
      75             :   /* discard first 4*r results */
      76    40980580 :   for (xorgen_i = r-1, k = 4*r; k > 0; k--) (void)block();
      77      223010 : }
      78             : 
      79      162280 : void pari_init_rand(void) { init_xor4096i(1UL); }
      80             : 
      81             : static u64
      82    47146325 : rand64(void)
      83             : {
      84    47146325 :   u64 v = block();
      85    47147293 :   xorgen_w += weyl; /* update Weyl generator */
      86    47147293 :   return v + (xorgen_w ^ (xorgen_w>>27));
      87             : }
      88             : 
      89             : /* One random number uniformly distributed in [0..2**BIL) is returned, where
      90             :  * BIL = 8*sizeof(ulong) = 32 or 64. */
      91             : ulong
      92      106059 : pari_rand(void) { return rand64(); }
      93             : 
      94             : void
      95       52682 : setrand(GEN x)
      96             : {
      97       52682 :   const int r2 = numberof(state);
      98             :   long i, lx;
      99             :   u64 v;
     100             :   GEN xp;
     101       52682 :   if (typ(x)!=t_INT) pari_err_TYPE("setrand",x);
     102       52682 :   if (signe(x) <= 0) pari_err_DOMAIN("setrand","n", "<=", gen_0, x);
     103       52675 :   lx = lgefint(x);
     104       52675 :   if (lx == 3) { v = x[2]; init_xor4096i(v); return; }
     105             : #ifndef LONG_IS_64BIT
     106          20 :   if (lx == 4)
     107             :   {
     108           1 :     v = _32to64(*int_W(x,1),*int_W(x,0));
     109           1 :     init_xor4096i(v); return;
     110             :   }
     111             : #endif
     112         133 :   xp = int_LSW(x);
     113             : #ifdef LONG_IS_64BIT
     114         114 :   if (lx != 2 + r2+2)
     115           6 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     116        7020 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) state[i] = *xp;
     117         108 :   xorgen_w = *xp; xp = int_nextW(xp);
     118             : #else
     119          19 :   if (lx != 2 + 2*r2+3)
     120           1 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     121        1170 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     122        1152 :     state[i] = _32to64(*int_nextW(xp), *xp);
     123          18 :   xorgen_w = _32to64(*int_nextW(xp), *xp); xp = int_nextW(int_nextW(xp));
     124             : #endif
     125         126 :   xorgen_i =  (*xp) & 63;
     126             : }
     127             : 
     128             : GEN
     129      394841 : getrand(void)
     130             : {
     131      394841 :   const int r2 = numberof(state);
     132             :   GEN x;
     133             :   ulong *xp;
     134             :   long i;
     135      394841 :   if (xorgen_i < 0) init_xor4096i(1UL);
     136             : 
     137             : #ifdef LONG_IS_64BIT
     138      338178 :   x = cgetipos(2+r2+2); xp = (ulong *) int_LSW(x);
     139    21981570 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) *xp = state[i];
     140      338178 :   *xp = xorgen_w; xp = int_nextW(xp);
     141             : #else
     142       56663 :   x = cgetipos(2+2*r2+3); xp = (ulong *) int_LSW(x);
     143     3683095 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     144     3626432 :     _64to32(state[i], int_nextW(xp), xp);
     145       56663 :   _64to32(xorgen_w, int_nextW(xp), xp); xp = int_nextW(int_nextW(xp));
     146             : #endif
     147      394841 :   *xp = xorgen_i? xorgen_i: 64; return x;
     148             : }
     149             : 
     150             : /* assume 0 <= k <= BITS_IN_LONG. Return uniform random 0 <= x < (1<<k) */
     151             : long
     152     9522507 : random_bits(long k) { return rand64() >> (64-k); }
     153             : 
     154             : /********************************************************************/
     155             : /*                                                                  */
     156             : /*                         GENERIC ROUTINES                         */
     157             : /*                                                                  */
     158             : /********************************************************************/
     159             : 
     160             : /* assume n > 0 */
     161             : ulong
     162    25693944 : random_Fl(ulong n)
     163             : {
     164             :   ulong d;
     165             :   int shift;
     166             : #ifdef LONG_IS_64BIT
     167    21417650 :   int SHIFT = 0;
     168             : #else
     169     4276294 :   int SHIFT = 32;
     170             : #endif
     171             : 
     172    25693944 :   if (n == 1) return 0;
     173             : 
     174    25683458 :   shift = bfffo(n); /* 2^(BIL-shift) > n >= 2^(BIL-shift-1)*/
     175             :   /* if N a power of 2, increment shift. No reject */
     176    25683458 :   if ((n << shift) == HIGHBIT) return rand64() >> (SHIFT+shift+1);
     177             :   for (;;) {
     178    35757331 :     d = rand64() >> (SHIFT+shift); /* d < 2^(64-shift) uniformly distributed */
     179             :     /* reject strategy: proba success = n 2^(shift-64), in [1/2, 1[ */
     180    35758530 :     if (d < n) return d;
     181             :   }
     182             : }
     183             : 
     184             : /* assume N > 0, see random_Fl() for algorithm. Make sure that 32-bit and
     185             :  * 64-bit architectures produce the same integers (consuming random bits
     186             :  * by packets of 64) */
     187             : GEN
     188     4830794 : randomi(GEN N)
     189             : {
     190     4830794 :   long lx = lgefint(N);
     191             :   GEN x, d;
     192             :   int shift;
     193             : 
     194     4830794 :   if (lx == 3) return utoi( random_Fl(N[2]) );
     195             : 
     196       98604 :   shift = bfffo(*int_MSW(N));
     197             :   /* if N a power of 2, increment shift */
     198       98604 :   if (Z_ispow2(N) && ++shift == BITS_IN_LONG) { shift = 0; lx--; }
     199       98604 :   x = cgetipos(lx);
     200       90889 :   for (;;) {
     201      189493 :     GEN y, MSW = int_MSW(x), STOP = MSW;
     202             : #ifdef LONG_IS_64BIT
     203      330443 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d)) *d = rand64();
     204      149570 :     *d = rand64() >> shift;
     205             : #else
     206       39923 :     if (!odd(lx)) STOP = int_precW(STOP);
     207             :     /* STOP points to where MSW would in 64-bit */
     208       70024 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d))
     209             :     {
     210       30101 :       ulong a, b; _64to32(rand64(), &a,&b);
     211       30101 :       *d = b; d = int_nextW(d);
     212       30101 :       *d = a;
     213             :     }
     214             :     {
     215       39923 :       ulong a, b; _64to32(rand64() >> shift, &a,&b);
     216       39923 :       if (d == MSW) /* 32 bits needed */
     217       21513 :         *d = a;
     218             :       else
     219             :       { /* 64 bits needed */
     220       18410 :         *d = b; d = int_nextW(d);
     221       18410 :         *d = a;
     222             :       }
     223             :     }
     224             : #endif
     225      189492 :     y = int_normalize(x, 0);
     226      189492 :     if (abscmpii(y, N) < 0) return y;
     227             :   }
     228             : }
     229             : 
     230             : GEN
     231      530698 : random_F2x(long d, long vs)
     232             : {
     233      530698 :   ulong db, dl = dvmduBIL(d,&db);
     234      530698 :   long i, l = 2 + dl + !!db;
     235      530698 :   GEN y = cgetg(l,t_VECSMALL); y[1] = vs;
     236             : #ifdef LONG_IS_64BIT
     237      910398 :   for (i=2; i<l; i++) uel(y,i) = rand64();
     238             : #else
     239       75937 :   for (i=2; i<l-1; i+=2)
     240             :   {
     241         135 :     u64 v = rand64();
     242         135 :     uel(y,i)   = (ulong) v;
     243         135 :     uel(y,i+1) = (ulong) (v>>32);
     244             :   }
     245       75802 :   if (i<l) uel(y,i) = (ulong) rand64();
     246             : #endif
     247      530698 :   if (db) uel(y,l-1) &= ((1UL<<db)-1UL);
     248      530698 :   return F2x_renormalize(y,l);
     249             : }
     250             : 
     251             : GEN
     252          80 : random_zv(long n)
     253             : {
     254          80 :   GEN y = cgetg(n+1, t_VECSMALL);
     255             :   long i;
     256      106119 :   for (i=1; i<=n; i++) uel(y,i) = pari_rand();
     257          80 :   return y;
     258             : }
     259             : 
     260             : GEN
     261           7 : randomr(long prec)
     262             : {
     263             :   pari_sp av;
     264             :   long b;
     265             :   GEN x, y;
     266           7 :   if (prec <= 2) return real_0_bit(0);
     267           7 :   x = cgetr(prec); av = avma;
     268           7 :   b = prec2nbits(prec);
     269           7 :   y = randomi(int2n(b));
     270           7 :   if (!signe(y)) return real_0_bit(b);
     271           7 :   affir(y, x); shiftr_inplace(x, - b);
     272           7 :   set_avma(av); return x;
     273             : }
     274             : 
     275             : static GEN
     276       13657 : polrandom(GEN N) /* assume N!=0 */
     277             : {
     278       13657 :   long i, d = lg(N);
     279       13657 :   GEN z = leading_coeff(N);
     280       13657 :   GEN y = cgetg(d,t_POL);
     281       13657 :   y[1] = evalsigne(1) | evalvarn(varn(N));
     282       69559 :   for (i=2; i<d; i++) gel(y,i) = genrand(z);
     283       13657 :   return normalizepol_lg(y,d);
     284             : }
     285             : 
     286             : GEN
     287     4923639 : genrand(GEN N)
     288             : {
     289             :   GEN z;
     290     4923639 :   if (!N) return utoi( random_bits(31) );
     291     4922960 :   switch(typ(N))
     292             :   {
     293      282674 :     case t_INT:
     294      282674 :       if (signe(N)<=0) pari_err_DOMAIN("random","N","<=",gen_0,gen_0);
     295      282667 :       return randomi(N);
     296           7 :     case t_REAL:
     297           7 :       return randomr(realprec(N));
     298      619339 :     case t_INTMOD:
     299      619339 :       z = cgetg(3, t_INTMOD);
     300      619339 :       gel(z,1) = icopy(gel(N,1));
     301      619339 :       gel(z,2) = randomi(gel(N,1)); return z;
     302      187474 :     case t_FFELT:
     303      187474 :       return ffrandom(N);
     304       13657 :     case t_POL:
     305       13657 :       if (signe(N)==0) return pol_0(varn(N));
     306       13657 :       return polrandom(N);
     307     3819802 :     case t_VEC:
     308     3819802 :       if (lg(N) == 3)
     309             :       {
     310     3575824 :         pari_sp av = avma;
     311     3575824 :         GEN a = gel(N,1), b = gel(N,2), d;
     312     3575824 :         if (typ(a) != t_INT) a = gceil(a);
     313     3575824 :         if (typ(b) != t_INT) b = gfloor(b);
     314     3575824 :         if (typ(a) != t_INT || typ(b) != t_INT) pari_err_TYPE("random", N);
     315     3575824 :         d = subii(b,a);
     316     3575824 :         if (signe(d) < 0) pari_err_TYPE("random([a,b]) (a > b)", N);
     317     3575824 :         return gerepileuptoint(av, addii(a, randomi(addiu(d,1))));
     318             :       }
     319      243978 :       return ellrandom(N);
     320           7 :     default:
     321           7 :       pari_err_TYPE("genrand",N);
     322             :       return NULL;/*LCOV_EXCL_LINE*/
     323             :   }
     324             : }

Generated by: LCOV version 1.13