• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
10 #define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
11 
12 #include "src/__support/GPU/utils.h"
13 #include "src/__support/macros/attributes.h"
14 
15 namespace LIBC_NAMESPACE {
16 
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];
27 
28 public:
T()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;
32   }
33 };
34 
35 extern ThreadLocal<unsigned long> rand_next;
36 #else
37 extern LIBC_THREAD_LOCAL unsigned long rand_next;
38 #endif
39 
40 } // namespace LIBC_NAMESPACE
41 
42 #endif // LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
43