LCOV - code coverage report
Current view: top level - src/port - erand48.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 19 34 55.9 %
Date: 2017-09-29 13:40:31 Functions: 2 5 40.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * erand48.c
       4             :  *
       5             :  * This file supplies pg_erand48(), pg_lrand48(), and pg_srand48(), which
       6             :  * are just like erand48(), lrand48(), and srand48() except that we use
       7             :  * our own implementation rather than the one provided by the operating
       8             :  * system.  We used to test for an operating system version rather than
       9             :  * unconditionally using our own, but (1) some versions of Cygwin have a
      10             :  * buggy erand48() that always returns zero and (2) as of 2011, glibc's
      11             :  * erand48() is strangely coded to be almost-but-not-quite thread-safe,
      12             :  * which doesn't matter for the backend but is important for pgbench.
      13             :  *
      14             :  *
      15             :  * Copyright (c) 1993 Martin Birgmeier
      16             :  * All rights reserved.
      17             :  *
      18             :  * You may redistribute unmodified or modified versions of this source
      19             :  * code provided that the above copyright notice and this and the
      20             :  * following conditions are retained.
      21             :  *
      22             :  * This software is provided ``as is'', and comes with no warranties
      23             :  * of any kind. I shall in no event be liable for anything that happens
      24             :  * to anyone/anything when using this software.
      25             :  *
      26             :  * IDENTIFICATION
      27             :  *    src/port/erand48.c
      28             :  *
      29             :  *-------------------------------------------------------------------------
      30             :  */
      31             : 
      32             : #include "c.h"
      33             : 
      34             : #include <math.h>
      35             : 
      36             : #define RAND48_SEED_0   (0x330e)
      37             : #define RAND48_SEED_1   (0xabcd)
      38             : #define RAND48_SEED_2   (0x1234)
      39             : #define RAND48_MULT_0   (0xe66d)
      40             : #define RAND48_MULT_1   (0xdeec)
      41             : #define RAND48_MULT_2   (0x0005)
      42             : #define RAND48_ADD      (0x000b)
      43             : 
      44             : static unsigned short _rand48_seed[3] = {
      45             :     RAND48_SEED_0,
      46             :     RAND48_SEED_1,
      47             :     RAND48_SEED_2
      48             : };
      49             : static unsigned short _rand48_mult[3] = {
      50             :     RAND48_MULT_0,
      51             :     RAND48_MULT_1,
      52             :     RAND48_MULT_2
      53             : };
      54             : static unsigned short _rand48_add = RAND48_ADD;
      55             : 
      56             : 
      57             : static void
      58         757 : _dorand48(unsigned short xseed[3])
      59             : {
      60             :     unsigned long accu;
      61             :     unsigned short temp[2];
      62             : 
      63        1514 :     accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
      64         757 :         (unsigned long) _rand48_add;
      65         757 :     temp[0] = (unsigned short) accu;    /* lower 16 bits */
      66         757 :     accu >>= sizeof(unsigned short) * 8;
      67        1514 :     accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
      68         757 :         (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
      69         757 :     temp[1] = (unsigned short) accu;    /* middle 16 bits */
      70         757 :     accu >>= sizeof(unsigned short) * 8;
      71         757 :     accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
      72         757 :     xseed[0] = temp[0];
      73         757 :     xseed[1] = temp[1];
      74         757 :     xseed[2] = (unsigned short) accu;
      75         757 : }
      76             : 
      77             : 
      78             : double
      79         757 : pg_erand48(unsigned short xseed[3])
      80             : {
      81         757 :     _dorand48(xseed);
      82        2271 :     return ldexp((double) xseed[0], -48) +
      83         757 :         ldexp((double) xseed[1], -32) +
      84         757 :         ldexp((double) xseed[2], -16);
      85             : }
      86             : 
      87             : long
      88           0 : pg_lrand48(void)
      89             : {
      90           0 :     _dorand48(_rand48_seed);
      91           0 :     return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1);
      92             : }
      93             : 
      94             : long
      95           0 : pg_jrand48(unsigned short xseed[3])
      96             : {
      97           0 :     _dorand48(xseed);
      98           0 :     return ((long) xseed[2] << 16) + ((long) xseed[1]);
      99             : }
     100             : 
     101             : void
     102           0 : pg_srand48(long seed)
     103             : {
     104           0 :     _rand48_seed[0] = RAND48_SEED_0;
     105           0 :     _rand48_seed[1] = (unsigned short) seed;
     106           0 :     _rand48_seed[2] = (unsigned short) (seed >> 16);
     107           0 :     _rand48_mult[0] = RAND48_MULT_0;
     108           0 :     _rand48_mult[1] = RAND48_MULT_1;
     109           0 :     _rand48_mult[2] = RAND48_MULT_2;
     110           0 :     _rand48_add = RAND48_ADD;
     111           0 : }

Generated by: LCOV version 1.11