aboutsummaryrefslogtreecommitdiff
path: root/libc/src/stdlib
diff options
context:
space:
mode:
authorMichael Jones <michaelrj@google.com>2022-10-04 11:59:55 -0700
committerMichael Jones <michaelrj@google.com>2022-10-04 13:31:26 -0700
commit38b6f58e33bbd8dc0be570f41806d0a9006610d9 (patch)
treebe624ff89eefbb0e10239b5daa4d6ffdd51f8148 /libc/src/stdlib
parent42fead6834ef3a96c765ea545c2d2bac951f7e98 (diff)
downloadllvm-38b6f58e33bbd8dc0be570f41806d0a9006610d9.zip
llvm-38b6f58e33bbd8dc0be570f41806d0a9006610d9.tar.gz
llvm-38b6f58e33bbd8dc0be570f41806d0a9006610d9.tar.bz2
[libc] implement basic rand and srand
This provides the reference implementation of rand and srand. In future this will likely be upgraded to something that supports full ints. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D135187
Diffstat (limited to 'libc/src/stdlib')
-rw-r--r--libc/src/stdlib/CMakeLists.txt30
-rw-r--r--libc/src/stdlib/rand.cpp22
-rw-r--r--libc/src/stdlib/rand.h20
-rw-r--r--libc/src/stdlib/rand_util.cpp15
-rw-r--r--libc/src/stdlib/rand_util.h18
-rw-r--r--libc/src/stdlib/srand.cpp17
-rw-r--r--libc/src/stdlib/srand.h20
7 files changed, 142 insertions, 0 deletions
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 7daecd9..b067e84 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -201,6 +201,36 @@ add_entrypoint_object(
libc.include.stdlib
)
+add_object_library(
+ rand_util
+ SRCS
+ rand_util.cpp
+ HDRS
+ rand_util.h
+)
+
+add_entrypoint_object(
+ rand
+ SRCS
+ rand.cpp
+ HDRS
+ rand.h
+ DEPENDS
+ .rand_util
+ libc.include.stdlib
+)
+
+add_entrypoint_object(
+ srand
+ SRCS
+ srand.cpp
+ HDRS
+ srand.h
+ DEPENDS
+ .rand_util
+ libc.include.stdlib
+)
+
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")
diff --git a/libc/src/stdlib/rand.cpp b/libc/src/stdlib/rand.cpp
new file mode 100644
index 0000000..ef6a721
--- /dev/null
+++ b/libc/src/stdlib/rand.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of rand --------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/rand.h"
+#include "src/__support/common.h"
+#include "src/stdlib/rand_util.h"
+
+namespace __llvm_libc {
+
+// This rand function is the example implementation from the C standard. It is
+// not cryptographically secure.
+LLVM_LIBC_FUNCTION(int, rand, (void)) { // RAND_MAX is assumed to be 32767
+ rand_next = rand_next * 1103515245 + 12345;
+ return static_cast<unsigned int>((rand_next / 65536) % 32768);
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdlib/rand.h b/libc/src/stdlib/rand.h
new file mode 100644
index 0000000..3f37c57
--- /dev/null
+++ b/libc/src/stdlib/rand.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for rand --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdlib.h>
+
+#ifndef LLVM_LIBC_SRC_STDLIB_RAND_H
+#define LLVM_LIBC_SRC_STDLIB_RAND_H
+
+namespace __llvm_libc {
+
+int rand(void);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_RAND_H
diff --git a/libc/src/stdlib/rand_util.cpp b/libc/src/stdlib/rand_util.cpp
new file mode 100644
index 0000000..afa6662
--- /dev/null
+++ b/libc/src/stdlib/rand_util.cpp
@@ -0,0 +1,15 @@
+//===-- Shared utility for rand -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/rand_util.h"
+
+namespace __llvm_libc {
+
+thread_local unsigned long rand_next;
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdlib/rand_util.h b/libc/src/stdlib/rand_util.h
new file mode 100644
index 0000000..6179406
--- /dev/null
+++ b/libc/src/stdlib/rand_util.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for rand utilities ----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
+#define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
+
+namespace __llvm_libc {
+
+extern thread_local unsigned long rand_next;
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
diff --git a/libc/src/stdlib/srand.cpp b/libc/src/stdlib/srand.cpp
new file mode 100644
index 0000000..d93d867
--- /dev/null
+++ b/libc/src/stdlib/srand.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of srand -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/srand.h"
+#include "src/__support/common.h"
+#include "src/stdlib/rand_util.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(void, srand, (unsigned int seed)) { rand_next = seed; }
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdlib/srand.h b/libc/src/stdlib/srand.h
new file mode 100644
index 0000000..86228b7
--- /dev/null
+++ b/libc/src/stdlib/srand.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for srand -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdlib.h>
+
+#ifndef LLVM_LIBC_SRC_STDLIB_SRAND_H
+#define LLVM_LIBC_SRC_STDLIB_SRAND_H
+
+namespace __llvm_libc {
+
+void srand(unsigned int seed);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_SRAND_H