rand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Implementation of the IRAND, RAND, and SRAND intrinsics.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Steven G. Kargl <kargls@comcast.net>.
  4. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* Simple multiplicative congruent algorithm.
  21. The period of this generator is approximately 2^31-1, which means that
  22. it should not be used for anything serious. The implementation here
  23. is based of an algorithm from S.K. Park and K.W. Miller, Comm. ACM,
  24. 31, 1192-1201 (1988). It is also provided solely for compatibility
  25. with G77. */
  26. #include "libgfortran.h"
  27. #include <gthr.h>
  28. #define GFC_RAND_A 16807
  29. #define GFC_RAND_M 2147483647
  30. #define GFC_RAND_M1 (GFC_RAND_M - 1)
  31. static GFC_UINTEGER_8 rand_seed = 1;
  32. #ifdef __GTHREAD_MUTEX_INIT
  33. static __gthread_mutex_t rand_seed_lock = __GTHREAD_MUTEX_INIT;
  34. #else
  35. static __gthread_mutex_t rand_seed_lock;
  36. #endif
  37. /* Set the seed of the irand generator. Note 0 is a bad seed. */
  38. static void
  39. srand_internal (GFC_INTEGER_8 i)
  40. {
  41. rand_seed = i ? i : 123459876;
  42. }
  43. extern void PREFIX(srand) (GFC_INTEGER_4 *i);
  44. export_proto_np(PREFIX(srand));
  45. void
  46. PREFIX(srand) (GFC_INTEGER_4 *i)
  47. {
  48. __gthread_mutex_lock (&rand_seed_lock);
  49. srand_internal (*i);
  50. __gthread_mutex_unlock (&rand_seed_lock);
  51. }
  52. /* Return an INTEGER in the range [1,GFC_RAND_M-1]. */
  53. extern GFC_INTEGER_4 irand (GFC_INTEGER_4 *);
  54. iexport_proto(irand);
  55. GFC_INTEGER_4
  56. irand (GFC_INTEGER_4 *i)
  57. {
  58. GFC_INTEGER_4 j;
  59. if (i)
  60. j = *i;
  61. else
  62. j = 0;
  63. __gthread_mutex_lock (&rand_seed_lock);
  64. switch (j)
  65. {
  66. /* Return the next RN. */
  67. case 0:
  68. break;
  69. /* Reset the RN sequence to system-dependent sequence and return the
  70. first value. */
  71. case 1:
  72. srand_internal (0);
  73. break;
  74. /* Seed the RN sequence with j and return the first value. */
  75. default:
  76. srand_internal (j);
  77. break;
  78. }
  79. rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
  80. j = (GFC_INTEGER_4) rand_seed;
  81. __gthread_mutex_unlock (&rand_seed_lock);
  82. return j;
  83. }
  84. iexport(irand);
  85. /* Return a random REAL in the range [0,1). */
  86. extern GFC_REAL_4 PREFIX(rand) (GFC_INTEGER_4 *i);
  87. export_proto_np(PREFIX(rand));
  88. GFC_REAL_4
  89. PREFIX(rand) (GFC_INTEGER_4 *i)
  90. {
  91. GFC_UINTEGER_4 mask;
  92. #if GFC_REAL_4_RADIX == 2
  93. mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS + 1);
  94. #elif GFC_REAL_4_RADIX == 16
  95. mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4 + 1);
  96. #else
  97. #error "GFC_REAL_4_RADIX has unknown value"
  98. #endif
  99. return ((GFC_UINTEGER_4) (irand(i) -1) & mask) * (GFC_REAL_4) 0x1.p-31f;
  100. }
  101. #ifndef __GTHREAD_MUTEX_INIT
  102. static void __attribute__((constructor))
  103. init (void)
  104. {
  105. __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock);
  106. }
  107. #endif