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 - random.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 22303-eb3e11d) Lines: 142 142 100.0 %
Date: 2018-04-21 06:16:28 Functions: 16 16 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         846 : _32to64(ulong a, ulong b) { u64 v = a; return (v<<32)|b; }
      35             : static void
      36     3197959 : _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    67190311 : block(void)
      46             : {
      47    67190311 :   const int r = 64;
      48    67190311 :   const int a = 33, b = 26, c = 27, d = 29, s = 53;
      49             :   u64 t, v, w;
      50    67190311 :   xorgen_i = (xorgen_i+1)&(r-1);
      51    67190311 :   t = state[xorgen_i];
      52    67190311 :   v = state[(xorgen_i+(r-s))&(r-1)];   /* index is (i-s) mod r */
      53    67190311 :   t ^= t<<a; t ^= t>>b;                   /* (I + L^a)(I + R^b) */
      54    67190311 :   v ^= v<<c; v ^= v>>d;                   /* (I + L^c)(I + R^d) */
      55    67190311 :   w = t^v;
      56    67190311 :   return state[xorgen_i] = w;               /* update circular array */
      57             : }
      58             : 
      59             : /* v > 0 */
      60             : static void
      61      221249 : init_xor4096i(u64 v)
      62             : {
      63      221249 :   const int r = 64;
      64             :   int k;
      65             : 
      66     7671649 :   for (k = r; k > 0; k--) {/* avoid correlations for close seeds */
      67     7450400 :     v ^= v<<10; v ^= v>>15; /* recurrence has period 2**64-1 */
      68     7450400 :     v ^= v<<4;  v ^= v>>13;
      69             :   }
      70     7572011 :   for (xorgen_w = v, k = 0; k < r; k++) { /* initialise circular array */
      71     7350762 :     v ^= v<<10; v ^= v>>15;
      72     7350762 :     v ^= v<<4;  v ^= v>>13;
      73     7350762 :     state[k] = v + (xorgen_w+=weyl);
      74             :   }
      75             :   /* discard first 4*r results */
      76      221249 :   for (xorgen_i = r-1, k = 4*r; k > 0; k--) (void)block();
      77      121522 : }
      78             : 
      79       88960 : void pari_init_rand(void) { init_xor4096i(1UL); }
      80             : 
      81             : static u64
      82    40987417 : rand64(void)
      83             : {
      84    40987417 :   u64 v = block();
      85    40987505 :   xorgen_w += weyl; /* update Weyl generator */
      86    40987505 :   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          20 : pari_rand(void) { return rand64(); }
      93             : 
      94             : void
      95       31349 : setrand(GEN x)
      96             : {
      97       31349 :   const int r2 = numberof(state);
      98             :   long i, lx;
      99             :   u64 v;
     100             :   GEN xp;
     101       31349 :   if (typ(x)!=t_INT) pari_err_TYPE("setrand",x);
     102       31349 :   if (signe(x) <= 0) pari_err_DOMAIN("setrand","n", "<=", gen_0, x);
     103       31342 :   lx = lgefint(x);
     104       58198 :   if (lx == 3) { v = x[2]; init_xor4096i(v); return; }
     105             : #ifndef LONG_IS_64BIT
     106          15 :   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          80 :   xp = int_LSW(x);
     113             : #ifdef LONG_IS_64BIT
     114          66 :   if (lx != 2 + r2+2)
     115           6 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     116          60 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) state[i] = *xp;
     117          60 :   xorgen_w = *xp; xp = int_nextW(xp);
     118             : #else
     119          14 :   if (lx != 2 + 2*r2+3)
     120           1 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     121         845 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     122         832 :     state[i] = _32to64(*int_nextW(xp), *xp);
     123          13 :   xorgen_w = _32to64(*int_nextW(xp), *xp); xp = int_nextW(int_nextW(xp));
     124             : #endif
     125          73 :   xorgen_i =  (*xp) & 63;
     126             : }
     127             : 
     128             : GEN
     129      337322 : getrand(void)
     130             : {
     131      337322 :   const int r2 = numberof(state);
     132             :   GEN x;
     133             :   ulong *xp;
     134             :   long i;
     135      337322 :   if (xorgen_i < 0) init_xor4096i(1UL);
     136             : 
     137             : #ifdef LONG_IS_64BIT
     138      288942 :   x = cgetipos(2+r2+2); xp = (ulong *) int_LSW(x);
     139      288942 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) *xp = state[i];
     140      288942 :   *xp = xorgen_w; xp = int_nextW(xp);
     141             : #else
     142       48380 :   x = cgetipos(2+2*r2+3); xp = (ulong *) int_LSW(x);
     143     3144700 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     144     3096320 :     _64to32(state[i], int_nextW(xp), xp);
     145       48380 :   _64to32(xorgen_w, int_nextW(xp), xp); xp = int_nextW(int_nextW(xp));
     146             : #endif
     147      337322 :   *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     9423088 : random_bits(long k) { return rand64() >> (64-k); }
     153             : 
     154             : /********************************************************************/
     155             : /*                                                                  */
     156             : /*                         GENERIC ROUTINES                         */
     157             : /*                                                                  */
     158             : /********************************************************************/
     159             : 
     160             : /* assume n > 0 */
     161             : ulong
     162    22457616 : random_Fl(ulong n)
     163             : {
     164             :   ulong d;
     165             :   int shift;
     166             : #ifdef LONG_IS_64BIT
     167    18665473 :   int SHIFT = 0;
     168             : #else
     169     3792143 :   int SHIFT = 32;
     170             : #endif
     171             : 
     172    22457616 :   if (n == 1) return 0;
     173             : 
     174    22447130 :   shift = bfffo(n); /* 2^(BIL-shift) > n >= 2^(BIL-shift-1)*/
     175             :   /* if N a power of 2, increment shift. No reject */
     176    22447130 :   if ((n << shift) == HIGHBIT) return rand64() >> (SHIFT+shift+1);
     177             :   for (;;) {
     178    29847443 :     d = rand64() >> (SHIFT+shift); /* d < 2^(64-shift) uniformly distributed */
     179             :     /* reject strategy: proba success = n 2^(shift-64), in [1/2, 1[ */
     180    29847532 :     if (d < n) return d;
     181     8251903 :   }
     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     5313321 : randomi(GEN N)
     189             : {
     190     5313321 :   long lx = lgefint(N);
     191             :   GEN x, d;
     192             :   int shift;
     193             : 
     194     5313321 :   if (lx == 3) return utoi( random_Fl(N[2]) );
     195             : 
     196       78031 :   shift = bfffo(*int_MSW(N));
     197             :   /* if N a power of 2, increment shift */
     198       78031 :   if (Z_ispow2(N) && ++shift == BITS_IN_LONG) { shift = 0; lx--; }
     199       78031 :   x = cgetipos(lx);
     200             :   for (;;) {
     201      148644 :     GEN y, MSW = int_MSW(x), STOP = MSW;
     202             : #ifdef LONG_IS_64BIT
     203      121591 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d)) *d = rand64();
     204      121591 :     *d = rand64() >> shift;
     205             : #else
     206       27053 :     if (!odd(lx)) STOP = int_precW(STOP);
     207             :     /* STOP points to where MSW would in 64-bit */
     208       53259 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d))
     209             :     {
     210       26206 :       ulong a, b; _64to32(rand64(), &a,&b);
     211       26206 :       *d = b; d = int_nextW(d);
     212       26206 :       *d = a;
     213             :     }
     214             :     {
     215       27053 :       ulong a, b; _64to32(rand64() >> shift, &a,&b);
     216       27053 :       if (d == MSW) /* 32 bits needed */
     217       17692 :         *d = a;
     218             :       else
     219             :       { /* 64 bits needed */
     220        9361 :         *d = b; d = int_nextW(d);
     221        9361 :         *d = a;
     222             :       }
     223             :     }
     224             : #endif
     225      148644 :     y = int_normalize(x, 0);
     226      148644 :     if (abscmpii(y, N) < 0) return y;
     227       70613 :   }
     228             : }
     229             : 
     230             : GEN
     231      531813 : random_F2x(long d, long vs)
     232             : {
     233      531813 :   ulong db, dl = dvmduBIL(d,&db);
     234      531813 :   long i, l = 2 + dl + !!db;
     235      531813 :   GEN y = cgetg(l,t_VECSMALL); y[1] = vs;
     236             : #ifdef LONG_IS_64BIT
     237      455847 :   for (i=2; i<l; i++) uel(y,i) = rand64();
     238             : #else
     239       76101 :   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       75966 :   if (i<l) uel(y,i) = (ulong) rand64();
     246             : #endif
     247      531813 :   if (db) uel(y,l-1) &= ((1UL<<db)-1UL);
     248      531813 :   return F2x_renormalize(y,l);
     249             : }
     250             : 
     251             : GEN
     252           7 : randomr(long prec)
     253             : {
     254             :   pari_sp av;
     255             :   long b;
     256             :   GEN x, y;
     257           7 :   if (prec <= 2) return real_0_bit(0);
     258           7 :   x = cgetr(prec); av = avma;
     259           7 :   b = prec2nbits(prec);
     260           7 :   y = randomi(int2n(b));
     261           7 :   if (!signe(y)) return real_0_bit(b);
     262           7 :   affir(y, x); shiftr_inplace(x, - b);
     263           7 :   avma = av; return x;
     264             : }
     265             : 
     266             : static GEN
     267       13139 : polrandom(GEN N) /* assume N!=0 */
     268             : {
     269       13139 :   long i, d = lg(N);
     270       13139 :   GEN z = leading_coeff(N);
     271       13139 :   GEN y = cgetg(d,t_POL);
     272       13139 :   y[1] = evalsigne(1) | evalvarn(varn(N));
     273       13139 :   for (i=2; i<d; i++) gel(y,i) = genrand(z);
     274       13139 :   return normalizepol_lg(y,d);
     275             : }
     276             : 
     277             : GEN
     278     5386486 : genrand(GEN N)
     279             : {
     280             :   GEN z;
     281     5386486 :   if (!N) return utoi( random_bits(31) );
     282     5385807 :   switch(typ(N))
     283             :   {
     284             :     case t_INT:
     285     2188025 :       if (signe(N)<=0) pari_err_DOMAIN("random","N","<=",gen_0,gen_0);
     286     2188018 :       return randomi(N);
     287             :     case t_REAL:
     288           7 :       return randomr(realprec(N));
     289             :     case t_INTMOD:
     290      614145 :       z = cgetg(3, t_INTMOD);
     291      614145 :       gel(z,1) = icopy(gel(N,1));
     292      614145 :       gel(z,2) = randomi(gel(N,1)); return z;
     293             :     case t_FFELT:
     294      144466 :       return ffrandom(N);
     295             :     case t_POL:
     296       13139 :       if (signe(N)==0) return pol_0(varn(N));
     297       13139 :       return polrandom(N);
     298             :     case t_VEC:
     299     2426018 :       if (lg(N) == 3)
     300             :       {
     301     2181956 :         pari_sp av = avma;
     302     2181956 :         GEN a = gel(N,1), b = gel(N,2), d;
     303     2181956 :         if (typ(a) != t_INT) a = gceil(a);
     304     2181956 :         if (typ(b) != t_INT) b = gfloor(b);
     305     2181956 :         if (typ(a) != t_INT || typ(b) != t_INT) pari_err_TYPE("random", N);
     306     2181956 :         d = subii(b,a);
     307     2181956 :         if (signe(d) < 0) pari_err_TYPE("random([a,b]) (a > b)", N);
     308     2181956 :         return gerepileuptoint(av, addii(a, randomi(addiu(d,1))));
     309             :       }
     310      244062 :       return ellrandom(N);
     311             :     default:
     312           7 :       pari_err_TYPE("genrand",N);
     313             :       return NULL;/*LCOV_EXCL_LINE*/
     314             :   }
     315             : }

Generated by: LCOV version 1.11