Header And Logo

PostgreSQL
| The world's most advanced open source database.

erand48.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * erand48.c
00004  *
00005  * This file supplies pg_erand48(), pg_lrand48(), and pg_srand48(), which
00006  * are just like erand48(), lrand48(), and srand48() except that we use
00007  * our own implementation rather than the one provided by the operating
00008  * system.  We used to test for an operating system version rather than
00009  * unconditionally using our own, but (1) some versions of Cygwin have a
00010  * buggy erand48() that always returns zero and (2) as of 2011, glibc's
00011  * erand48() is strangely coded to be almost-but-not-quite thread-safe,
00012  * which doesn't matter for the backend but is important for pgbench.
00013  *
00014  *
00015  * Copyright (c) 1993 Martin Birgmeier
00016  * All rights reserved.
00017  *
00018  * You may redistribute unmodified or modified versions of this source
00019  * code provided that the above copyright notice and this and the
00020  * following conditions are retained.
00021  *
00022  * This software is provided ``as is'', and comes with no warranties
00023  * of any kind. I shall in no event be liable for anything that happens
00024  * to anyone/anything when using this software.
00025  *
00026  * IDENTIFICATION
00027  *    src/port/erand48.c
00028  *
00029  *-------------------------------------------------------------------------
00030  */
00031 
00032 #include "c.h"
00033 
00034 #include <math.h>
00035 
00036 #define RAND48_SEED_0   (0x330e)
00037 #define RAND48_SEED_1   (0xabcd)
00038 #define RAND48_SEED_2   (0x1234)
00039 #define RAND48_MULT_0   (0xe66d)
00040 #define RAND48_MULT_1   (0xdeec)
00041 #define RAND48_MULT_2   (0x0005)
00042 #define RAND48_ADD      (0x000b)
00043 
00044 static unsigned short _rand48_seed[3] = {
00045     RAND48_SEED_0,
00046     RAND48_SEED_1,
00047     RAND48_SEED_2
00048 };
00049 static unsigned short _rand48_mult[3] = {
00050     RAND48_MULT_0,
00051     RAND48_MULT_1,
00052     RAND48_MULT_2
00053 };
00054 static unsigned short _rand48_add = RAND48_ADD;
00055 
00056 
00057 static void
00058 _dorand48(unsigned short xseed[3])
00059 {
00060     unsigned long accu;
00061     unsigned short temp[2];
00062 
00063     accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
00064         (unsigned long) _rand48_add;
00065     temp[0] = (unsigned short) accu;    /* lower 16 bits */
00066     accu >>= sizeof(unsigned short) * 8;
00067     accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
00068         (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
00069     temp[1] = (unsigned short) accu;    /* middle 16 bits */
00070     accu >>= sizeof(unsigned short) * 8;
00071     accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
00072     xseed[0] = temp[0];
00073     xseed[1] = temp[1];
00074     xseed[2] = (unsigned short) accu;
00075 }
00076 
00077 
00078 double
00079 pg_erand48(unsigned short xseed[3])
00080 {
00081     _dorand48(xseed);
00082     return ldexp((double) xseed[0], -48) +
00083         ldexp((double) xseed[1], -32) +
00084         ldexp((double) xseed[2], -16);
00085 }
00086 
00087 long
00088 pg_lrand48(void)
00089 {
00090     _dorand48(_rand48_seed);
00091     return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1);
00092 }
00093 
00094 void
00095 pg_srand48(long seed)
00096 {
00097     _rand48_seed[0] = RAND48_SEED_0;
00098     _rand48_seed[1] = (unsigned short) seed;
00099     _rand48_seed[2] = (unsigned short) (seed >> 16);
00100     _rand48_mult[0] = RAND48_MULT_0;
00101     _rand48_mult[1] = RAND48_MULT_1;
00102     _rand48_mult[2] = RAND48_MULT_2;
00103     _rand48_add = RAND48_ADD;
00104 }