1 /* Implementation of the IRAND, RAND, and SRAND intrinsics.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Steven G. Kargl <kargls@comcast.net>.
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* Simple multiplicative congruent algorithm.
27 The period of this generator is approximately 2^31-1, which means that
28 it should not be used for anything serious. The implementation here
29 is based of an algorithm from S.K. Park and K.W. Miller, Comm. ACM,
30 31, 1192-1201 (1988). It is also provided solely for compatibility
33 #include "libgfortran.h"
36 #define GFC_RAND_A 16807
37 #define GFC_RAND_M 2147483647
38 #define GFC_RAND_M1 (GFC_RAND_M - 1)
40 static GFC_UINTEGER_8 rand_seed
= 1;
41 #ifdef __GTHREAD_MUTEX_INIT
42 static __gthread_mutex_t rand_seed_lock
= __GTHREAD_MUTEX_INIT
;
44 static __gthread_mutex_t rand_seed_lock
;
48 /* Set the seed of the irand generator. Note 0 is a bad seed. */
51 srand_internal (GFC_INTEGER_8 i
)
53 rand_seed
= i
? i
: 123459876;
56 extern void PREFIX(srand
) (GFC_INTEGER_4
*i
);
57 export_proto_np(PREFIX(srand
));
60 PREFIX(srand
) (GFC_INTEGER_4
*i
)
62 __gthread_mutex_lock (&rand_seed_lock
);
64 __gthread_mutex_unlock (&rand_seed_lock
);
67 /* Return an INTEGER in the range [1,GFC_RAND_M-1]. */
69 extern GFC_INTEGER_4
irand (GFC_INTEGER_4
*);
73 irand (GFC_INTEGER_4
*i
)
81 __gthread_mutex_lock (&rand_seed_lock
);
85 /* Return the next RN. */
89 /* Reset the RN sequence to system-dependent sequence and return the
95 /* Seed the RN sequence with j and return the first value. */
101 rand_seed
= GFC_RAND_A
* rand_seed
% GFC_RAND_M
;
102 j
= (GFC_INTEGER_4
) rand_seed
;
104 __gthread_mutex_unlock (&rand_seed_lock
);
111 /* Return a random REAL in the range [0,1). */
113 extern GFC_REAL_4
PREFIX(rand
) (GFC_INTEGER_4
*i
);
114 export_proto_np(PREFIX(rand
));
117 PREFIX(rand
) (GFC_INTEGER_4
*i
)
120 #if GFC_REAL_4_RADIX == 2
121 mask
= ~ (GFC_UINTEGER_4
) 0u << (32 - GFC_REAL_4_DIGITS
+ 1);
122 #elif GFC_REAL_4_RADIX == 16
123 mask
= ~ (GFC_UINTEGER_4
) 0u << ((8 - GFC_REAL_4_DIGITS
) * 4 + 1);
125 #error "GFC_REAL_4_RADIX has unknown value"
127 return ((GFC_UINTEGER_4
) (irand(i
) -1) & mask
) * (GFC_REAL_4
) 0x1.p
-31f
;
130 #ifndef __GTHREAD_MUTEX_INIT
131 static void __attribute__((constructor
))
134 __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock
);