1 /* Implementation of the RANDOM intrinsics
2 Copyright 2002 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>
41 #include "libgfortran.h"
43 /*Use the 'big' generator by default ( period -> 2**19937 ). */
47 /* Define the necessary constants for the algorithm. */
52 N = 624, M = 397, R = 19, TU = 11, TS = 7, TT = 15, TL = 17
54 #define M_A 0x9908B0DF
55 #define T_B 0x9D2C5680
56 #define T_C 0xEFC60000
60 N = 351, M = 175, R = 19, TU = 11, TS = 7, TT = 15, TL = 17
62 #define M_A 0xE4BD75F5
63 #define T_B 0x655E5280
64 #define T_C 0xFFD58000
68 static unsigned int seed[N];
70 /* This is the routine which handles the seeding of the generator,
71 and also reading and writing of the seed. */
73 #define random_seed prefix(random_seed)
75 random_seed (GFC_INTEGER_4 * size, const gfc_array_i4 * put,
76 const gfc_array_i4 * get)
78 /* Initialize the seed in system dependent manner. */
79 if (get == NULL && put == NULL && size == NULL)
82 fd = open ("/dev/urandom", O_RDONLY);
85 /* We dont have urandom. */
86 GFC_UINTEGER_4 s = (GFC_UINTEGER_4) seed;
87 for (i = 0; i < N; i++)
95 /* Using urandom, might have a length issue. */
96 read (fd, &seed[0], sizeof (GFC_UINTEGER_4) * N);
102 /* Return the size of the seed */
109 /* if we have gotten to this pount we have a get or put
110 * now we check it the array fulfills the demands in the standard .
113 /* Set the seed to PUT data */
116 /* if the rank of the array is not 1 abort */
117 if (GFC_DESCRIPTOR_RANK (put) != 1)
120 /* if the array is too small abort */
121 if (((put->dim[0].ubound + 1 - put->dim[0].lbound)) < N)
124 /* If this is the case the array is a temporary */
125 if (get->dim[0].stride == 0)
128 /* This code now should do correct strides. */
129 for (i = 0; i < N; i++)
130 seed[i] = put->data[i * put->dim[0].stride];
133 /* Return the seed to GET data */
136 /* if the rank of the array is not 1 abort */
137 if (GFC_DESCRIPTOR_RANK (get) != 1)
140 /* if the array is too small abort */
141 if (((get->dim[0].ubound + 1 - get->dim[0].lbound)) < N)
144 /* If this is the case the array is a temporary */
145 if (get->dim[0].stride == 0)
148 /* This code now should do correct strides. */
149 for (i = 0; i < N; i++)
150 get->data[i * get->dim[0].stride] = seed[i];
154 /* Here is the internal routine which generates the random numbers
155 in 'batches' based upon the need for a new batch.
156 It's an integer based routine known as 'Mersenne Twister'.
157 This implementation still lacks 'tempering' and a good verification,
158 but gives very good metrics. */
161 random_generate (void)
166 /* Generate batch of N. */
168 for (k = 0, m = M; k < N - 1; k++)
170 y = (seed[k] & (-1 << R)) | (seed[k + 1] & ((1u << R) - 1));
171 seed[k] = seed[m] ^ (y >> 1) ^ (-(GFC_INTEGER_4) (y & 1) & M_A);
176 y = (seed[N - 1] & (-1 << R)) | (seed[0] & ((1u << R) - 1));
177 seed[N - 1] = seed[M - 1] ^ (y >> 1) ^ (-(GFC_INTEGER_4) (y & 1) & M_A);
181 /* A routine to return a REAL(KIND=4). */
183 #define random_r4 prefix(random_r4)
185 random_r4 (GFC_REAL_4 * harv)
187 /* Regenerate if we need to. */
191 /* Convert uint32 to REAL(KIND=4). */
192 *harv = (GFC_REAL_4) ((GFC_REAL_4) (GFC_UINTEGER_4) seed[i++] /
193 (GFC_REAL_4) (~(GFC_UINTEGER_4) 0));
196 /* A routine to return a REAL(KIND=8). */
198 #define random_r8 prefix(random_r8)
200 random_r8 (GFC_REAL_8 * harv)
202 /* Regenerate if we need to, may waste one 32-bit value. */
206 /* Convert two uint32 to a REAL(KIND=8). */
207 *harv = ((GFC_REAL_8) ((((GFC_UINTEGER_8) seed[i+1]) << 32) + seed[i])) /
208 (GFC_REAL_8) (~(GFC_UINTEGER_8) 0);
212 /* Code to handle arrays will follow here. */
214 /* REAL(KIND=4) REAL array. */
216 #define arandom_r4 prefix(arandom_r4)
218 arandom_r4 (gfc_array_r4 * harv)
220 index_type count[GFC_MAX_DIMENSIONS - 1];
221 index_type extent[GFC_MAX_DIMENSIONS - 1];
222 index_type stride[GFC_MAX_DIMENSIONS - 1];
230 if (harv->dim[0].stride == 0)
231 harv->dim[0].stride = 1;
233 dim = GFC_DESCRIPTOR_RANK (harv);
235 for (n = 0; n < dim; n++)
238 stride[n] = harv->dim[n].stride;
239 extent[n] = harv->dim[n].ubound + 1 - harv->dim[n].lbound;
248 /* Set the elements. */
250 /* regenerate if we need to */
254 /* Convert uint32 to float in a hopefully g95 compiant manner */
255 *dest = (GFC_REAL_4) ((GFC_REAL_4) (GFC_UINTEGER_4) seed[i++] /
256 (GFC_REAL_4) (~(GFC_UINTEGER_4) 0));
258 /* Advance to the next element. */
261 /* Advance to the next source element. */
263 while (count[n] == extent[n])
265 /* When we get to the end of a dimension,
266 reset it and increment
267 the next dimension. */
269 /* We could precalculate these products,
271 frequently used path so proabably not worth it. */
272 dest -= stride[n] * extent[n];
288 /* REAL(KIND=8) array. */
290 #define arandom_r8 prefix(arandom_r8)
292 arandom_r8 (gfc_array_r8 * harv)
294 index_type count[GFC_MAX_DIMENSIONS - 1];
295 index_type extent[GFC_MAX_DIMENSIONS - 1];
296 index_type stride[GFC_MAX_DIMENSIONS - 1];
304 if (harv->dim[0].stride == 0)
305 harv->dim[0].stride = 1;
307 dim = GFC_DESCRIPTOR_RANK (harv);
309 for (n = 0; n < dim; n++)
312 stride[n] = harv->dim[n].stride;
313 extent[n] = harv->dim[n].ubound + 1 - harv->dim[n].lbound;
322 /* Set the elements. */
324 /* regenerate if we need to, may waste one 32-bit value */
328 /* Convert two uint32 to a REAL(KIND=8). */
329 *dest = ((GFC_REAL_8) ((((GFC_UINTEGER_8) seed[i+1]) << 32) + seed[i])) /
330 (GFC_REAL_8) (~(GFC_UINTEGER_8) 0);
333 /* Advance to the next element. */
336 /* Advance to the next source element. */
338 while (count[n] == extent[n])
340 /* When we get to the end of a dimension,
341 reset it and increment
342 the next dimension. */
344 /* We could precalculate these products,
346 frequently used path so proabably not worth it. */
347 dest -= stride[n] * extent[n];