Skip to content

Commit 4d7605b

Browse files
authored
Add an LLVM implementation of 'random' to standard library (#1163)
1 parent b2e4c77 commit 4d7605b

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

libraries/common/effekt.effekt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ extern def unsafeSubstring(str: String, from: Int, to: Int) at {}: String =
164164
extern def random() at io: Double =
165165
js "Math.random()"
166166
chez "(random 1.0)"
167+
llvm """
168+
%r = call double @c_io_random()
169+
ret double %r
170+
"""
167171
vm "effekt::random()"
168172

169173
// References and state

libraries/llvm/forward-declare-c.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare %Pos @c_get_arg(i64)
55

66
declare void @c_io_println(%Pos)
77
declare %Pos @c_io_readln()
8+
declare %Double @c_io_random()
89

910
declare void @hole(i8*)
1011
declare void @duplicated_prompt()

libraries/llvm/io.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <uv.h>
55
#include <string.h> // to compare flag names
66

7-
// Println and Readln
8-
// ------------------
7+
// Println and Readln and Random
8+
// -----------------------------
99

1010
void c_io_println(String text) {
1111
for (uint64_t j = 0; j < text.tag; ++j) {
@@ -34,6 +34,10 @@ String c_io_readln() {
3434
return result;
3535
}
3636

37+
double c_io_random(void) {
38+
return (double)rand() / (double)RAND_MAX;
39+
}
40+
3741
// Lib UV Bindings
3842
// ---------------
3943
// Ideas behind the LLVM / libuv implementation.

0 commit comments

Comments
 (0)