Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / stdlib / rand_util.h
blobcadd6b5cdcbb8c05827517cfb5eb3757df8aed15
1 //===-- Implementation header for rand utilities ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
10 #define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
12 #include "src/__support/GPU/utils.h"
13 #include "src/__support/macros/attributes.h"
15 namespace LIBC_NAMESPACE {
17 #ifdef LIBC_TARGET_ARCH_IS_GPU
18 // Implement thread local storage on the GPU using local memory. Each thread
19 // gets its slot in the local memory array and is private to the group.
20 // TODO: We need to implement the 'thread_local' keyword on the GPU. This is an
21 // inefficient and incomplete stand-in until that is done.
22 template <typename T> class ThreadLocal {
23 private:
24 static constexpr long MAX_THREADS = 1024;
25 [[clang::loader_uninitialized]] static inline gpu::Local<T>
26 storage[MAX_THREADS];
28 public:
29 LIBC_INLINE operator T() const { return storage[gpu::get_thread_id()]; }
30 LIBC_INLINE void operator=(const T &value) {
31 storage[gpu::get_thread_id()] = value;
35 extern ThreadLocal<unsigned long> rand_next;
36 #else
37 extern LIBC_THREAD_LOCAL unsigned long rand_next;
38 #endif
40 } // namespace LIBC_NAMESPACE
42 #endif // LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H