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.0 lcov report (development 23172-40b229422) Lines: 141 141 100.0 %
Date: 2018-10-22 05:38:26 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        5721 : _32to64(ulong a, ulong b) { u64 v = a; return (v<<32)|b; }
      35             : static void
      36     3207254 : _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    76590637 : block(void)
      46             : {
      47    76590637 :   const int r = 64;
      48    76590637 :   const int a = 33, b = 26, c = 27, d = 29, s = 53;
      49             :   u64 t, v, w;
      50    76590637 :   xorgen_i = (xorgen_i+1)&(r-1);
      51    76590637 :   t = state[xorgen_i];
      52    76590637 :   v = state[(xorgen_i+(r-s))&(r-1)];   /* index is (i-s) mod r */
      53    76590637 :   t ^= t<<a; t ^= t>>b;                   /* (I + L^a)(I + R^b) */
      54    76590637 :   v ^= v<<c; v ^= v>>d;                   /* (I + L^c)(I + R^d) */
      55    76590637 :   w = t^v;
      56    76590637 :   return state[xorgen_i] = w;               /* update circular array */
      57             : }
      58             : 
      59             : /* v > 0 */
      60             : static void
      61      166175 : init_xor4096i(u64 v)
      62             : {
      63      166175 :   const int r = 64;
      64             :   int k;
      65             : 
      66     9389027 :   for (k = r; k > 0; k--) {/* avoid correlations for close seeds */
      67     9222852 :     v ^= v<<10; v ^= v>>15; /* recurrence has period 2**64-1 */
      68     9222852 :     v ^= v<<4;  v ^= v>>13;
      69             :   }
      70     9371611 :   for (xorgen_w = v, k = 0; k < r; k++) { /* initialise circular array */
      71     9205436 :     v ^= v<<10; v ^= v>>15;
      72     9205436 :     v ^= v<<4;  v ^= v>>13;
      73     9205436 :     state[k] = v + (xorgen_w+=weyl);
      74             :   }
      75             :   /* discard first 4*r results */
      76      166175 :   for (xorgen_i = r-1, k = 4*r; k > 0; k--) (void)block();
      77      145415 : }
      78             : 
      79      113261 : void pari_init_rand(void) { init_xor4096i(1UL); }
      80             : 
      81             : static u64
      82    40697943 : rand64(void)
      83             : {
      84    40697943 :   u64 v = block();
      85    40697983 :   xorgen_w += weyl; /* update Weyl generator */
      86    40697983 :   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       32014 : setrand(GEN x)
      96             : {
      97       32014 :   const int r2 = numberof(state);
      98             :   long i, lx;
      99             :   u64 v;
     100             :   GEN xp;
     101       32014 :   if (typ(x)!=t_INT) pari_err_TYPE("setrand",x);
     102       32014 :   if (signe(x) <= 0) pari_err_DOMAIN("setrand","n", "<=", gen_0, x);
     103       32007 :   lx = lgefint(x);
     104       32007 :   if (lx == 3) { v = x[2]; init_xor4096i(v); return; }
     105             : #ifndef LONG_IS_64BIT
     106          90 :   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         605 :   xp = int_LSW(x);
     113             : #ifdef LONG_IS_64BIT
     114         516 :   if (lx != 2 + r2+2)
     115           6 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     116         510 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) state[i] = *xp;
     117         510 :   xorgen_w = *xp; xp = int_nextW(xp);
     118             : #else
     119          89 :   if (lx != 2 + 2*r2+3)
     120           1 :     pari_err_DOMAIN("setrand", "n", "!=", strtoGENstr("getrand()"), x);
     121        5720 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     122        5632 :     state[i] = _32to64(*int_nextW(xp), *xp);
     123          88 :   xorgen_w = _32to64(*int_nextW(xp), *xp); xp = int_nextW(int_nextW(xp));
     124             : #endif
     125         598 :   xorgen_i =  (*xp) & 63;
     126             : }
     127             : 
     128             : GEN
     129      338260 : getrand(void)
     130             : {
     131      338260 :   const int r2 = numberof(state);
     132             :   GEN x;
     133             :   ulong *xp;
     134             :   long i;
     135      338260 :   if (xorgen_i < 0) init_xor4096i(1UL);
     136             : 
     137             : #ifdef LONG_IS_64BIT
     138      289746 :   x = cgetipos(2+r2+2); xp = (ulong *) int_LSW(x);
     139      289746 :   for (i = 0; i < r2; i++, xp = int_nextW(xp)) *xp = state[i];
     140      289746 :   *xp = xorgen_w; xp = int_nextW(xp);
     141             : #else
     142       48514 :   x = cgetipos(2+2*r2+3); xp = (ulong *) int_LSW(x);
     143     3153410 :   for (i = 0; i < r2; i++, xp = int_nextW(int_nextW(xp)))
     144     3104896 :     _64to32(state[i], int_nextW(xp), xp);
     145       48514 :   _64to32(xorgen_w, int_nextW(xp), xp); xp = int_nextW(int_nextW(xp));
     146             : #endif
     147      338260 :   *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     9548589 : random_bits(long k) { return rand64() >> (64-k); }
     153             : 
     154             : /********************************************************************/
     155             : /*                                                                  */
     156             : /*                         GENERIC ROUTINES                         */
     157             : /*                                                                  */
     158             : /********************************************************************/
     159             : 
     160             : /* assume n > 0 */
     161             : ulong
     162    22107538 : random_Fl(ulong n)
     163             : {
     164             :   ulong d;
     165             :   int shift;
     166             : #ifdef LONG_IS_64BIT
     167    18354754 :   int SHIFT = 0;
     168             : #else
     169     3752784 :   int SHIFT = 32;
     170             : #endif
     171             : 
     172    22107538 :   if (n == 1) return 0;
     173             : 
     174    22097052 :   shift = bfffo(n); /* 2^(BIL-shift) > n >= 2^(BIL-shift-1)*/
     175             :   /* if N a power of 2, increment shift. No reject */
     176    22097052 :   if ((n << shift) == HIGHBIT) return rand64() >> (SHIFT+shift+1);
     177             :   for (;;) {
     178    37606657 :     d = rand64() >> (SHIFT+shift); /* d < 2^(64-shift) uniformly distributed */
     179             :     /* reject strategy: proba success = n 2^(shift-64), in [1/2, 1[ */
     180    29423949 :     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     4799730 : randomi(GEN N)
     189             : {
     190     4799730 :   long lx = lgefint(N);
     191             :   GEN x, d;
     192             :   int shift;
     193             : 
     194     4799730 :   if (lx == 3) return utoi( random_Fl(N[2]) );
     195             : 
     196       78236 :   shift = bfffo(*int_MSW(N));
     197             :   /* if N a power of 2, increment shift */
     198       78236 :   if (Z_ispow2(N) && ++shift == BITS_IN_LONG) { shift = 0; lx--; }
     199       78236 :   x = cgetipos(lx);
     200       70938 :   for (;;) {
     201      149174 :     GEN y, MSW = int_MSW(x), STOP = MSW;
     202             : #ifdef LONG_IS_64BIT
     203      121994 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d)) *d = rand64();
     204      121994 :     *d = rand64() >> shift;
     205             : #else
     206       27180 :     if (!odd(lx)) STOP = int_precW(STOP);
     207             :     /* STOP points to where MSW would in 64-bit */
     208       53844 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d))
     209             :     {
     210       26664 :       ulong a, b; _64to32(rand64(), &a,&b);
     211       26664 :       *d = b; d = int_nextW(d);
     212       26664 :       *d = a;
     213             :     }
     214             :     {
     215       27180 :       ulong a, b; _64to32(rand64() >> shift, &a,&b);
     216       27180 :       if (d == MSW) /* 32 bits needed */
     217       17738 :         *d = a;
     218             :       else
     219             :       { /* 64 bits needed */
     220        9442 :         *d = b; d = int_nextW(d);
     221        9442 :         *d = a;
     222             :       }
     223             :     }
     224             : #endif
     225      149174 :     y = int_normalize(x, 0);
     226      149174 :     if (abscmpii(y, N) < 0) return y;
     227             :   }
     228             : }
     229             : 
     230             : GEN
     231      532941 : random_F2x(long d, long vs)
     232             : {
     233      532941 :   ulong db, dl = dvmduBIL(d,&db);
     234      532941 :   long i, l = 2 + dl + !!db;
     235      532941 :   GEN y = cgetg(l,t_VECSMALL); y[1] = vs;
     236             : #ifdef LONG_IS_64BIT
     237      456819 :   for (i=2; i<l; i++) uel(y,i) = rand64();
     238             : #else
     239       76257 :   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       76122 :   if (i<l) uel(y,i) = (ulong) rand64();
     246             : #endif
     247      532941 :   if (db) uel(y,l-1) &= ((1UL<<db)-1UL);
     248      532941 :   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 :   set_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     4871671 : genrand(GEN N)
     279             : {
     280             :   GEN z;
     281     4871671 :   if (!N) return utoi( random_bits(31) );
     282     4870992 :   switch(typ(N))
     283             :   {
     284             :     case t_INT:
     285      279090 :       if (signe(N)<=0) pari_err_DOMAIN("random","N","<=",gen_0,gen_0);
     286      279083 :       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      144438 :       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     3820166 :       if (lg(N) == 3)
     300             :       {
     301     3575824 :         pari_sp av = avma;
     302     3575824 :         GEN a = gel(N,1), b = gel(N,2), d;
     303     3575824 :         if (typ(a) != t_INT) a = gceil(a);
     304     3575824 :         if (typ(b) != t_INT) b = gfloor(b);
     305     3575824 :         if (typ(a) != t_INT || typ(b) != t_INT) pari_err_TYPE("random", N);
     306     3575824 :         d = subii(b,a);
     307     3575824 :         if (signe(d) < 0) pari_err_TYPE("random([a,b]) (a > b)", N);
     308     3575824 :         return gerepileuptoint(av, addii(a, randomi(addiu(d,1))));
     309             :       }
     310      244342 :       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.13