summaryrefslogtreecommitdiff
path: root/numpy/random/mtrand/randomkit.c
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2010-05-26 02:26:12 +0000
committerCharles Harris <charlesr.harris@gmail.com>2010-05-26 02:26:12 +0000
commit6be6945afdc31270eaf4463d47c271f701c7f688 (patch)
treeee5933fe857154a79ecad4318bf3d0670003d734 /numpy/random/mtrand/randomkit.c
parentb056b23a27fe4f56f923168bb9931429765084d1 (diff)
downloadnumpy-6be6945afdc31270eaf4463d47c271f701c7f688.tar.gz
STY: Some c coding style cleanups.
Diffstat (limited to 'numpy/random/mtrand/randomkit.c')
-rw-r--r--numpy/random/mtrand/randomkit.c91
1 files changed, 50 insertions, 41 deletions
diff --git a/numpy/random/mtrand/randomkit.c b/numpy/random/mtrand/randomkit.c
index 1426566b2..09bc234ab 100644
--- a/numpy/random/mtrand/randomkit.c
+++ b/numpy/random/mtrand/randomkit.c
@@ -3,7 +3,7 @@
/*
* Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
*
- * The rk_random and rk_seed functions algorithms and the original design of
+ * The rk_random and rk_seed functions algorithms and the original design of
* the Mersenne Twister RNG:
*
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
@@ -12,7 +12,7 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
@@ -35,13 +35,13 @@
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
+ *
* Original algorithm for the implementation of rk_interval function from
* Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by
* Magnus Jonsson.
*
* Constants used in the rk_double implementation by Isaku Wada.
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
@@ -49,10 +49,10 @@
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -72,16 +72,23 @@
#include <math.h>
#ifdef _WIN32
-/* Windows */
-/* XXX: we have to use this ugly defined(__GNUC__) because it is not easy to
- * detect the compiler used in distutils itself */
+/*
+ * Windows
+ * XXX: we have to use this ugly defined(__GNUC__) because it is not easy to
+ * detect the compiler used in distutils itself
+ */
#if (defined(__GNUC__) && defined(NPY_NEEDS_MINGW_TIME_WORKAROUND))
-/* FIXME: ideally, we should set this to the real version of MSVCRT. We need
- * something higher than 0x601 to enable _ftime64 and co */
+
+/*
+ * FIXME: ideally, we should set this to the real version of MSVCRT. We need
+ * something higher than 0x601 to enable _ftime64 and co
+ */
#define __MSVCRT_VERSION__ 0x0700
#include <time.h>
#include <sys/timeb.h>
-/* mingw msvcr lib import wrongly export _ftime, which does not exist in the
+
+/*
+ * mingw msvcr lib import wrongly export _ftime, which does not exist in the
* actual msvc runtime for version >= 8; we make it an alias to _ftime64, which
* is available in those versions of the runtime
*/
@@ -131,7 +138,7 @@ void rk_seed(unsigned long seed, rk_state *state)
seed &= 0xffffffffUL;
/* Knuth's PRNG as used in the Mersenne Twister reference implementation */
- for (pos=0; pos<RK_STATE_LEN; pos++) {
+ for (pos = 0; pos < RK_STATE_LEN; pos++) {
state->key[pos] = seed;
seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
}
@@ -157,20 +164,21 @@ rk_error rk_randomseed(rk_state *state)
#ifndef _WIN32
struct timeval tv;
#else
- struct _timeb tv;
+ struct _timeb tv;
#endif
int i;
if (rk_devfill(state->key, sizeof(state->key), 0) == RK_NOERR) {
- state->key[0] |= 0x80000000UL; /* ensures non-zero key */
- state->pos = RK_STATE_LEN;
- state->has_gauss = 0;
- state->has_binomial = 0;
-
- for (i = 0; i < 624; i++) {
- state->key[i] &= 0xffffffffUL;
- }
- return RK_NOERR;
+ /* ensures non-zero key */
+ state->key[0] |= 0x80000000UL;
+ state->pos = RK_STATE_LEN;
+ state->has_gauss = 0;
+ state->has_binomial = 0;
+
+ for (i = 0; i < 624; i++) {
+ state->key[i] &= 0xffffffffUL;
+ }
+ return RK_NOERR;
}
#ifndef _WIN32
@@ -196,10 +204,10 @@ rk_error rk_randomseed(rk_state *state)
unsigned long rk_random(rk_state *state)
{
unsigned long y;
-
+
if (state->pos == RK_STATE_LEN) {
int i;
-
+
for (i = 0; i < N - M; i++) {
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
@@ -208,19 +216,19 @@ unsigned long rk_random(rk_state *state)
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
}
- y = (state->key[N-1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
- state->key[N-1] = state->key[M-1] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
-
+ y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
+ state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
+
state->pos = 0;
}
y = state->key[state->pos++];
-
+
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
-
+
return y;
}
@@ -242,8 +250,9 @@ unsigned long rk_interval(unsigned long max, rk_state *state)
{
unsigned long mask = max, value;
- if (max == 0) return 0;
-
+ if (max == 0) {
+ return 0;
+ }
/* Smallest bit mask >= max */
mask |= mask >> 1;
mask |= mask >> 2;
@@ -265,7 +274,6 @@ unsigned long rk_interval(unsigned long max, rk_state *state)
#else
while ((value = (rk_ulong(state) & mask)) > max);
#endif
-
return value;
}
@@ -280,7 +288,7 @@ void rk_fill(void *buffer, size_t size, rk_state *state)
{
unsigned long r;
unsigned char *buf = buffer;
-
+
for (; size >= 4; size -= 4) {
r = rk_random(state);
*(buf++) = r & 0xFF;
@@ -288,7 +296,7 @@ void rk_fill(void *buffer, size_t size, rk_state *state)
*(buf++) = (r >> 16) & 0xFF;
*(buf++) = (r >> 24) & 0xFF;
}
-
+
if (!size) {
return;
}
@@ -298,7 +306,8 @@ void rk_fill(void *buffer, size_t size, rk_state *state)
}
}
-rk_error rk_devfill(void *buffer, size_t size, int strong)
+rk_error
+rk_devfill(void *buffer, size_t size, int strong)
{
#ifndef _WIN32
FILE *rfile;
@@ -339,7 +348,8 @@ rk_error rk_devfill(void *buffer, size_t size, int strong)
return RK_ENODEV;
}
-rk_error rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
+rk_error
+rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
{
rk_error err;
@@ -350,7 +360,8 @@ rk_error rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
return err;
}
-double rk_gauss(rk_state *state)
+double
+rk_gauss(rk_state *state)
{
if (state->has_gauss) {
state->has_gauss = 0;
@@ -365,7 +376,7 @@ double rk_gauss(rk_state *state)
r2 = x1*x1 + x2*x2;
}
while (r2 >= 1.0 || r2 == 0.0);
-
+
/* Box-Muller transform */
f = sqrt(-2.0*log(r2)/r2);
state->has_gauss = 1;
@@ -374,5 +385,3 @@ double rk_gauss(rk_state *state)
return f*x2;
}
}
-
-