From bb6ac9c6781e2639da8957ccf08957974d6ce697 Mon Sep 17 00:00:00 2001 From: cherierm Date: Wed, 25 Jun 2008 14:47:58 +0000 Subject: [PATCH] Windows version of rand_r() and drand48()functions. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5808 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/surf/random_mgr.c | 184 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 178 insertions(+), 6 deletions(-) diff --git a/src/surf/random_mgr.c b/src/surf/random_mgr.c index bae7a68ca5..90d0b93ef3 100644 --- a/src/surf/random_mgr.c +++ b/src/surf/random_mgr.c @@ -2,17 +2,189 @@ #include "xbt/sysdep.h" #ifdef WIN32 -static double drand48(void) + +static unsigned int _seed = 2147483647; + +typedef unsigned __int64 uint64_t; +typedef unsigned int uint32_t; + +struct drand48_data +{ + unsigned short int __x[3]; /* Current state. */ + unsigned short int __old_x[3]; /* Old state. */ + unsigned short int __c; /* Additive const. in congruential formula. */ + unsigned short int __init; /* Flag for initializing. */ + unsigned long long int __a; /* Factor in congruential formula. */ +}; + +static struct drand48_data __libc_drand48_data = {0}; + +union ieee754_double + { + double d; + + /* This is the IEEE 754 double-precision format. */ + struct + { + /* Together these comprise the mantissa. */ + unsigned int mantissa1:32; + unsigned int mantissa0:20; + unsigned int exponent:11; + unsigned int negative:1; + /* Little endian. */ + } ieee; + + /* This format makes it easier to see if a NaN is a signalling NaN. */ + struct + { + /* Together these comprise the mantissa. */ + unsigned int mantissa1:32; + unsigned int mantissa0:19; + unsigned int quiet_nan:1; + unsigned int exponent:11; + unsigned int negative:1; + + } ieee_nan; +}; + +#define IEEE754_DOUBLE_BIAS 0x3ff /* Added to exponent. */ + +double +drand48 (void); + +int +_drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer); + +int +_erand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, double *result); + + +int +_erand48_r (unsigned short int xsubi[3], struct drand48_data *buffer, double *result) +{ + union ieee754_double temp; + + /* Compute next state. */ + if (_drand48_iterate(xsubi, buffer) < 0) + return -1; + + /* Construct a positive double with the 48 random bits distributed over + its fractional part so the resulting FP number is [0.0,1.0). */ + + temp.ieee.negative = 0; + temp.ieee.exponent = IEEE754_DOUBLE_BIAS; + temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12); + temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4); + + /* Please note the lower 4 bits of mantissa1 are always 0. */ + *result = temp.d - 1.0; + + return 0; +} + +int +_drand48_iterate (unsigned short int xsubi[3], struct drand48_data *buffer) +{ + uint64_t X; + uint64_t result; + + /* Initialize buffer, if not yet done. */ + + if(buffer->__init == 0) + { + buffer->__a = 0x5deece66dull; + buffer->__c = 0xb; + buffer->__init = 1; + } + + /* Do the real work. We choose a data type which contains at least + 48 bits. Because we compute the modulus it does not care how + many bits really are computed. */ + + X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0]; + + result = X * buffer->__a + buffer->__c; + + + xsubi[0] = result & 0xffff; + xsubi[1] = (result >> 16) & 0xffff; + xsubi[2] = (result >> 32) & 0xffff; + + return 0; +} + + +double +_drand48 (void) +{ + double result; + + (void) _erand48_r (__libc_drand48_data.__x, &__libc_drand48_data, &result); + + return result; + } + +void +_srand(unsigned int seed) +{ + _seed = seed; +} + +int +_rand(void) { - THROW_UNIMPLEMENTED; - return -1; + const long a = 16807; + const long m = 2147483647; + const long q = 127773; /* (m/a) */ + const long r = 2836; /* (m%a) */ + + long lo, k, s; + + s = (long)_seed; + + k = (long)(s/q); + + lo = (s - q * k); + + s = a * lo -r * k; + + if(s <= 0) + s += m; + + _seed = (int)(s & RAND_MAX); + + return _seed; } -static double rand_r(unsigned int* seed) +int +_rand_r(unsigned int* pseed) { - THROW_UNIMPLEMENTED; - return -1; + const long a = 16807; + const long m = 2147483647; + const long q = 127773; /* (m/a) */ + const long r = 2836; /* (m%a) */ + + long lo, k, s; + + s = (long)*pseed; + + k = (long)(s/q); + + lo = (s - q * k); + + s = a * lo -r * k; + + if(s <= 0) + s += m; + + return (int)(s & RAND_MAX); + } + + +#define rand_r _rand_r +#define drand48 _drand48 + #endif static double custom_random(Generator generator, long int *seed){ -- 2.20.1