1 /* Implementation of the RANDOM intrinsics
2 Copyright 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Lars Segerlund <seger@linuxmail.org>
5 The algorithm was taken from the paper :
7 Mersenne Twister: 623-dimensionally equidistributed
8 uniform pseudorandom generator.
13 Which appeared in the: ACM Transactions on Modelling and Computer
14 Simulations: Special Issue on Uniform Random Number
15 Generation. ( Early in 1998 ).
17 This file is part of the GNU Fortran 95 runtime library (libgfortran).
19 Libgfortran is free software; you can redistribute it and/or
20 modify it under the terms of the GNU Lesser General Public
21 License as published by the Free Software Foundation; either
22 version 2.1 of the License, or (at your option) any later version.
24 Ligbfortran is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU Lesser General Public License for more details.
29 You should have received a copy of the GNU Lesser General Public
30 License along with libgfor; see the file COPYING.LIB. If not,
31 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
32 Boston, MA 02111-1307, USA. */
37 #include <sys/types.h>
45 #include "libgfortran.h"
47 /*Use the 'big' generator by default ( period -> 2**19937 ). */
51 /* Define the necessary constants for the algorithm. */
56 N = 624, M = 397, R = 19, TU = 11, TS = 7, TT = 15, TL = 17
58 #define M_A 0x9908B0DF
59 #define T_B 0x9D2C5680
60 #define T_C 0xEFC60000
64 N = 351, M = 175, R = 19, TU = 11, TS = 7, TT = 15, TL = 17
66 #define M_A 0xE4BD75F5
67 #define T_B 0x655E5280
68 #define T_C 0xFFD58000
72 static unsigned int seed[N];
74 /* This is the routine which handles the seeding of the generator,
75 and also reading and writing of the seed. */
78 random_seed (GFC_INTEGER_4 * size, const gfc_array_i4 * put,
79 const gfc_array_i4 * get)
81 /* Initialize the seed in system dependent manner. */
82 if (get == NULL && put == NULL && size == NULL)
85 fd = open ("/dev/urandom", O_RDONLY);
88 /* We dont have urandom. */
89 GFC_UINTEGER_4 s = (GFC_UINTEGER_4) seed;
90 for (i = 0; i < N; i++)
98 /* Using urandom, might have a length issue. */
99 read (fd, &seed[0], sizeof (GFC_UINTEGER_4) * N);
105 /* Return the size of the seed */
112 /* if we have gotten to this pount we have a get or put
113 * now we check it the array fulfills the demands in the standard .
116 /* Set the seed to PUT data */
119 /* if the rank of the array is not 1 abort */
120 if (GFC_DESCRIPTOR_RANK (put) != 1)
123 /* if the array is too small abort */
124 if (((put->dim[0].ubound + 1 - put->dim[0].lbound)) < N)
127 /* If this is the case the array is a temporary */
128 if (put->dim[0].stride == 0)
131 /* This code now should do correct strides. */
132 for (i = 0; i < N; i++)
133 seed[i] = put->data[i * put->dim[0].stride];
136 /* Return the seed to GET data */
139 /* if the rank of the array is not 1 abort */
140 if (GFC_DESCRIPTOR_RANK (get) != 1)
143 /* if the array is too small abort */
144 if (((get->dim[0].ubound + 1 - get->dim[0].lbound)) < N)
147 /* If this is the case the array is a temporary */
148 if (get->dim[0].stride == 0)
151 /* This code now should do correct strides. */
152 for (i = 0; i < N; i++)
153 get->data[i * get->dim[0].stride] = seed[i];
157 /* Here is the internal routine which generates the random numbers
158 in 'batches' based upon the need for a new batch.
159 It's an integer based routine known as 'Mersenne Twister'.
160 This implementation still lacks 'tempering' and a good verification,
161 but gives very good metrics. */
164 random_generate (void)
169 /* Generate batch of N. */
171 for (k = 0, m = M; k < N - 1; k++)
173 y = (seed[k] & (-1 << R)) | (seed[k + 1] & ((1u << R) - 1));
174 seed[k] = seed[m] ^ (y >> 1) ^ (-(GFC_INTEGER_4) (y & 1) & M_A);
179 y = (seed[N - 1] & (-1 << R)) | (seed[0] & ((1u << R) - 1));
180 seed[N - 1] = seed[M - 1] ^ (y >> 1) ^ (-(GFC_INTEGER_4) (y & 1) & M_A);
184 /* A routine to return a REAL(KIND=4). */
186 #define random_r4 prefix(random_r4)
188 random_r4 (GFC_REAL_4 * harv)
190 /* Regenerate if we need to. */
194 /* Convert uint32 to REAL(KIND=4). */
195 *harv = (GFC_REAL_4) ((GFC_REAL_4) (GFC_UINTEGER_4) seed[i++] /
196 (GFC_REAL_4) (~(GFC_UINTEGER_4) 0));
199 /* A routine to return a REAL(KIND=8). */
201 #define random_r8 prefix(random_r8)
203 random_r8 (GFC_REAL_8 * harv)
205 /* Regenerate if we need to, may waste one 32-bit value. */
209 /* Convert two uint32 to a REAL(KIND=8). */
210 *harv = ((GFC_REAL_8) ((((GFC_UINTEGER_8) seed[i+1]) << 32) + seed[i])) /
211 (GFC_REAL_8) (~(GFC_UINTEGER_8) 0);
215 /* Code to handle arrays will follow here. */
217 /* REAL(KIND=4) REAL array. */
219 #define arandom_r4 prefix(arandom_r4)
221 arandom_r4 (gfc_array_r4 * harv)
223 index_type count[GFC_MAX_DIMENSIONS - 1];
224 index_type extent[GFC_MAX_DIMENSIONS - 1];
225 index_type stride[GFC_MAX_DIMENSIONS - 1];
233 if (harv->dim[0].stride == 0)
234 harv->dim[0].stride = 1;
236 dim = GFC_DESCRIPTOR_RANK (harv);
238 for (n = 0; n < dim; n++)
241 stride[n] = harv->dim[n].stride;
242 extent[n] = harv->dim[n].ubound + 1 - harv->dim[n].lbound;
251 /* Set the elements. */
253 /* regenerate if we need to */
257 /* Convert uint32 to float in a hopefully g95 compiant manner */
258 *dest = (GFC_REAL_4) ((GFC_REAL_4) (GFC_UINTEGER_4) seed[i++] /
259 (GFC_REAL_4) (~(GFC_UINTEGER_4) 0));
261 /* Advance to the next element. */
264 /* Advance to the next source element. */
266 while (count[n] == extent[n])
268 /* When we get to the end of a dimension,
269 reset it and increment
270 the next dimension. */
272 /* We could precalculate these products,
274 frequently used path so proabably not worth it. */
275 dest -= stride[n] * extent[n];
291 /* REAL(KIND=8) array. */
293 #define arandom_r8 prefix(arandom_r8)
295 arandom_r8 (gfc_array_r8 * harv)
297 index_type count[GFC_MAX_DIMENSIONS - 1];
298 index_type extent[GFC_MAX_DIMENSIONS - 1];
299 index_type stride[GFC_MAX_DIMENSIONS - 1];
307 if (harv->dim[0].stride == 0)
308 harv->dim[0].stride = 1;
310 dim = GFC_DESCRIPTOR_RANK (harv);
312 for (n = 0; n < dim; n++)
315 stride[n] = harv->dim[n].stride;
316 extent[n] = harv->dim[n].ubound + 1 - harv->dim[n].lbound;
325 /* Set the elements. */
327 /* regenerate if we need to, may waste one 32-bit value */
331 /* Convert two uint32 to a REAL(KIND=8). */
332 *dest = ((GFC_REAL_8) ((((GFC_UINTEGER_8) seed[i+1]) << 32) + seed[i])) /
333 (GFC_REAL_8) (~(GFC_UINTEGER_8) 0);
336 /* Advance to the next element. */
339 /* Advance to the next source element. */
341 while (count[n] == extent[n])
343 /* When we get to the end of a dimension,
344 reset it and increment
345 the next dimension. */
347 /* We could precalculate these products,
349 frequently used path so proabably not worth it. */
350 dest -= stride[n] * extent[n];