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.8.0 lcov report (development 19207-2ed2f69) Lines: 133 136 97.8 %
Date: 2016-07-25 07:10:32 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             :   static THREAD ulong state[64];
      32             :   #define u64state(i)      ((u64)state[(i)])
      33             :   #define u64stateset(i,x) state[(i)] = (ulong) (x);
      34             : #else
      35             :   typedef unsigned long long u64;
      36             :   static THREAD ulong state[128];
      37             :   #define u64state(i)      _32to64(state[2*(i)],state[2*(i)+1])
      38             :   #define u64stateset(i,x) _64to32(x,state+2*(i),state+2*(i)+1)
      39             : static u64
      40    12489838 : _32to64(ulong a, ulong b) { u64 v = a; return (v<<32)|b; }
      41             : static void
      42     6355261 : _64to32(u64 v, ulong *a, ulong *b) { *a = v>>32; *b = v&0xFFFFFFFF; }
      43             : #endif
      44             : static THREAD u64 xorgen_w;
      45             : static THREAD int xorgen_i;
      46             : /* weyl = odd approximation to 2^64*(sqrt(5)-1)/2. */
      47             : static const u64 weyl = (((u64)0x61c88646U)<<32)|((u64)0x80b583ebU);
      48             : 
      49             : static u64
      50    79366339 : block(void)
      51             : {
      52    79366339 :   const int r = 64;
      53    79366339 :   const int a = 33, b = 26, c = 27, d = 29, s = 53;
      54             :   u64 t, v, w;
      55    79366339 :   xorgen_i = (xorgen_i+1)&(r-1);
      56    79366339 :   t = u64state(xorgen_i);
      57    79366339 :   v = u64state((xorgen_i+(r-s))&(r-1));   /* index is (i-s) mod r */
      58    79366339 :   t ^= t<<a; t ^= t>>b;                   /* (I + L^a)(I + R^b) */
      59    79366339 :   v ^= v<<c; v ^= v>>d;                   /* (I + L^c)(I + R^d) */
      60    79366339 :   w = t^v;
      61    79366339 :   u64stateset(xorgen_i, w);               /* update circular array */
      62    79366339 :   return w;
      63             : }
      64             : 
      65             : static void
      66      629941 : init_xor4096i(ulong seed)
      67             : {
      68      629941 :   const int r = 64;
      69      629941 :   u64 v = seed; /* v must be nonzero */
      70             :   int k;
      71             : 
      72    13235276 :   for (k = r; k > 0; k--) {/* avoid correlations for close seeds */
      73    12605335 :     v ^= v<<10; v ^= v>>15; /* recurrence has period 2**64-1 */
      74    12605335 :     v ^= v<<4;  v ^= v>>13;
      75             :   }
      76    13412869 :   for (xorgen_w = v, k = 0; k < r; k++) { /* initialise circular array */
      77    12782928 :     v ^= v<<10; v ^= v>>15;
      78    12782928 :     v ^= v<<4;  v ^= v>>13;
      79    12782928 :     u64stateset(k, v + (xorgen_w+=weyl));
      80             :   }
      81             :   /* discard first 4*r results */
      82      629941 :   for (xorgen_i = r-1, k = 4*r; k > 0; k--) (void)block();
      83      206594 : }
      84             : 
      85      199441 : void pari_init_rand(void) { init_xor4096i(1); }
      86             : 
      87             : static u64
      88    35877193 : rand64(void)
      89             : {
      90    35877193 :   u64 v = block();
      91    35877249 :   xorgen_w += weyl; /* update Weyl generator */
      92    35877249 :   return v + (xorgen_w ^ (xorgen_w>>27));
      93             : }
      94             : 
      95             : /* One random number uniformly distributed in [0..2**BIL) is returned, where
      96             :  * BIL = 8*sizeof(ulong) = 32 or 64. */
      97             : ulong
      98          20 : pari_rand(void) { return rand64(); }
      99             : 
     100             : void
     101        4563 : setrand(GEN x)
     102             : {
     103        4563 :   const int r2 = numberof(state);
     104             :   ulong useed;
     105             :   long i;
     106             :   GEN xp;
     107        4563 :   if (typ(x)!=t_INT)
     108           0 :     pari_err_TYPE("setrand",x);
     109        4563 :   if (signe(x) <= 0) return;
     110        4563 :   useed = itou_or_0(x);
     111        4563 :   if (useed > 0) { init_xor4096i(useed); return; }
     112          62 :   if (lgefint(x)!=2+r2+2+(r2==128))
     113           0 :     pari_err_TYPE("setrand",x);
     114          62 :   xp = int_LSW(x);
     115          62 :   for (i = 0; i < r2; i++) { state[i] = *xp; xp = int_nextW(xp); }
     116             : #ifdef LONG_IS_64BIT
     117          54 :   xorgen_w = *xp; xp = int_nextW(xp);
     118             : #else
     119           8 :   xorgen_w = _32to64(*xp, *int_nextW(xp)); xp = int_nextW(int_nextW(xp));
     120             : #endif
     121          62 :   xorgen_i =  (*xp) & 63;
     122             : }
     123             : 
     124             : GEN
     125      256597 : getrand(void)
     126             : {
     127      256597 :   const int r2 = numberof(state);
     128             :   GEN x;
     129             :   ulong *xp;
     130             :   long i;
     131      256597 :   if (xorgen_i < 0) init_xor4096i(1);
     132             : 
     133      256597 :   x = cgetipos(2+r2+2+(r2==128)); xp = (ulong *) int_LSW(x);
     134      256597 :   for (i = 0; i < r2; i++) { *xp = state[i]; xp = int_nextW(xp); }
     135             : #ifdef LONG_IS_64BIT
     136      219678 :   *xp = xorgen_w; xp = int_nextW(xp);
     137             : #else
     138       36919 :   _64to32(xorgen_w, xp, int_nextW(xp)); xp = int_nextW(int_nextW(xp));
     139             : #endif
     140      256597 :   *xp = xorgen_i? xorgen_i: 64; return x;
     141             : }
     142             : 
     143             : /* assume 0 <= k <= BITS_IN_LONG. Return uniform random 0 <= x < (1<<k) */
     144             : long
     145     9858291 : random_bits(long k) { return rand64() >> (64-k); }
     146             : 
     147             : /********************************************************************/
     148             : /*                                                                  */
     149             : /*                         GENERIC ROUTINES                         */
     150             : /*                                                                  */
     151             : /********************************************************************/
     152             : 
     153             : /* assume n > 0 */
     154             : ulong
     155    18814933 : random_Fl(ulong n)
     156             : {
     157             :   ulong d;
     158             :   int shift;
     159             : #ifdef LONG_IS_64BIT
     160    15508240 :   int SHIFT = 0;
     161             : #else
     162     3306693 :   int SHIFT = 32;
     163             : #endif
     164             : 
     165    18814933 :   if (n == 1) return 0;
     166             : 
     167    18814506 :   shift = bfffo(n); /* 2^(BIL-shift) > n >= 2^(BIL-shift-1)*/
     168             :   /* if N a power of 2, increment shift. No reject */
     169    18814506 :   if ((n << shift) == HIGHBIT) return rand64() >> (SHIFT+shift+1);
     170             :   for (;;) {
     171    25149668 :     d = rand64() >> (SHIFT+shift); /* d < 2^(64-shift) uniformly distributed */
     172             :     /* reject strategy: proba success = n 2^(shift-64), in [1/2, 1[ */
     173    25149729 :     if (d < n) return d;
     174     7014001 :   }
     175             : }
     176             : 
     177             : /* assume N > 0, see random_Fl() for algorithm. Make sure that 32-bit and
     178             :  * 64-bit architectures produce the same integers (consuming random bits
     179             :  * by packets of 64) */
     180             : GEN
     181     2975173 : randomi(GEN N)
     182             : {
     183     2975173 :   long lx = lgefint(N);
     184             :   GEN x, d;
     185             :   int shift;
     186             : 
     187     2975173 :   if (lx == 3) return utoi( random_Fl(N[2]) );
     188             : 
     189       28325 :   shift = bfffo(*int_MSW(N));
     190             :   /* if N a power of 2, increment shift */
     191       28325 :   if (Z_ispow2(N) && ++shift == BITS_IN_LONG) { shift = 0; lx--; }
     192       28325 :   x = cgetipos(lx);
     193             :   for (;;) {
     194       54250 :     GEN y, MSW = int_MSW(x), STOP = MSW;
     195             : #ifdef LONG_IS_64BIT
     196       40836 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d)) *d = rand64();
     197       40836 :     *d = rand64() >> shift;
     198             : #else
     199       13414 :     if (!odd(lx)) STOP = int_precW(STOP);
     200             :     /* STOP points to where MSW would in 64-bit */
     201       20243 :     for (d = int_LSW(x); d != STOP; d = int_nextW(d))
     202             :     {
     203        6829 :       ulong a, b; _64to32(rand64(), &a,&b);
     204        6829 :       *d = b; d = int_nextW(d);
     205        6829 :       *d = a;
     206             :     }
     207             :     {
     208       13414 :       ulong a, b; _64to32(rand64() >> shift, &a,&b);
     209       13414 :       if (d == MSW) /* 32 bits needed */
     210        5297 :         *d = a;
     211             :       else
     212             :       { /* 64 bits needed */
     213        8117 :         *d = b; d = int_nextW(d);
     214        8117 :         *d = a;
     215             :       }
     216             :     }
     217             : #endif
     218       54250 :     y = int_normalize(x, 0);
     219       54250 :     if (absi_cmp(y, N) < 0) return y;
     220       25925 :   }
     221             : }
     222             : 
     223             : GEN
     224       87570 : random_F2x(long d, long vs)
     225             : {
     226       87570 :   ulong db, dl = dvmduBIL(d,&db);
     227       87570 :   long i, l = 2 + dl + !!db;
     228       87570 :   GEN y = cgetg(l,t_VECSMALL); y[1] = vs;
     229             : #ifdef LONG_IS_64BIT
     230       75060 :   for (i=2; i<l; i++) uel(y,i) = rand64();
     231             : #else
     232       12672 :   for (i=2; i<l-1; i+=2)
     233             :   {
     234         162 :     u64 v = rand64();
     235         162 :     uel(y,i)   = (ulong) v;
     236         162 :     uel(y,i+1) = (ulong) (v>>32);
     237             :   }
     238       12510 :   if (i<l) uel(y,i) = (ulong) rand64();
     239             : #endif
     240       87570 :   if (db) uel(y,l-1) &= ((1UL<<db)-1UL);
     241       87570 :   return F2x_renormalize(y,l);
     242             : }
     243             : 
     244             : GEN
     245           7 : randomr(long prec)
     246             : {
     247             :   pari_sp av;
     248             :   long b;
     249             :   GEN x, y;
     250           7 :   if (prec <= 2) return real_0_bit(0);
     251           7 :   x = cgetr(prec); av = avma;
     252           7 :   b = prec2nbits(prec);
     253           7 :   y = randomi(int2n(b));
     254           7 :   if (!signe(y)) return real_0_bit(b);
     255           7 :   affir(y, x); shiftr_inplace(x, - b);
     256           7 :   avma = av; return x;
     257             : }
     258             : 
     259             : static GEN
     260          77 : polrandom(GEN N) /* assume N!=0 */
     261             : {
     262          77 :   long i, d = lg(N);
     263          77 :   GEN z = leading_coeff(N);
     264          77 :   GEN y = cgetg(d,t_POL);
     265          77 :   y[1] = evalsigne(1) | evalvarn(varn(N));
     266          77 :   for (i=2; i<d; i++) gel(y,i) = genrand(z);
     267          77 :   return normalizepol_lg(y,d);
     268             : }
     269             : 
     270             : GEN
     271     2853515 : genrand(GEN N)
     272             : {
     273             :   GEN z;
     274     2853515 :   if (!N) return utoi( random_bits(31) );
     275     2852843 :   switch(typ(N))
     276             :   {
     277             :     case t_INT:
     278     2183629 :       if (signe(N)<=0) pari_err_DOMAIN("random","N","<=",gen_0,gen_0);
     279     2183622 :       return randomi(N);
     280             :     case t_REAL:
     281           7 :       return randomr(realprec(N));
     282             :     case t_INTMOD:
     283      560217 :       z = cgetg(3, t_INTMOD);
     284      560217 :       gel(z,1) = icopy(gel(N,1));
     285      560217 :       gel(z,2) = randomi(gel(N,1)); return z;
     286             :     case t_FFELT:
     287       15204 :       return ffrandom(N);
     288             :     case t_POL:
     289          77 :       if (signe(N)==0) return pol_0(varn(N));
     290          77 :       return polrandom(N);
     291             :     case t_VEC:
     292       93702 :       if (lg(N) == 3)
     293             :       {
     294          70 :         pari_sp av = avma;
     295          70 :         GEN a = gel(N,1), b = gel(N,2), d;
     296          70 :         if (typ(a) != t_INT) a = gceil(a);
     297          70 :         if (typ(b) != t_INT) b = gfloor(b);
     298          70 :         if (typ(a) != t_INT || typ(b) != t_INT) pari_err_TYPE("random", N);
     299          70 :         d = subii(b,a);
     300          70 :         if (signe(d) < 0) pari_err_TYPE("random([a,b]) (a > b)", N);
     301          70 :         return gerepileuptoint(av, addii(a, randomi(addis(d,1))));
     302             :       }
     303       93632 :       return ellrandom(N);
     304             :     default:
     305           7 :       pari_err_TYPE("genrand",N);
     306           0 :       return NULL;/*not reached*/
     307             :   }
     308             : }

Generated by: LCOV version 1.11