[flang] [llvm] [flang/flang-rt] Adding support of RAND, IRAND and SRAND intrinsics (PR #166780)
Kelvin Li via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 20 13:17:52 PST 2025
================
@@ -398,6 +406,55 @@ std::int64_t RTNAME(time)() { return time(nullptr); }
// MCLOCK: returns accumulated CPU time in ticks
std::int32_t FORTRAN_PROCEDURE_NAME(mclock)() { return std::clock(); }
+static void _internal_srand(int seed) { rand_seed = seed ? seed : 123459876; }
+
+// IRAND(I)
+int RTNAME(Irand)(int *i) {
+ int j;
+ if (i)
+ j = *i;
+ else
+ j = 0;
+
+ rand_seed_lock.Take();
+ switch (j) {
+ case 0:
+ break;
+ case 1:
+ _internal_srand(0);
+ break;
+ default:
+ _internal_srand(j);
+ break;
+ }
+
+ rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
+ j = (int)rand_seed;
+ rand_seed_lock.Drop();
+ return j;
+}
+
+// RAND(I)
+float RTNAME(Rand)(int *i) {
+ unsigned mask = 0;
+ auto radix = std::numeric_limits<float>::radix;
+ auto digits = std::numeric_limits<float>::digits;
+ if (radix == 2)
+ mask = ~(unsigned)0u << (32 - digits + 1);
+ else if (radix == 16)
+ mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);
+ else
+ std::fprintf(stderr, "Radix unknown value");
----------------
kkwli wrote:
I am not sure if it is the way to handle error/warning message here. Need others to chime in.
https://github.com/llvm/llvm-project/pull/166780
More information about the llvm-commits
mailing list