/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_common.h Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_common.h Source File#

API library: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-rocrand/checkouts/develop/projects/rocrand/library/include/rocrand/rocrand_common.h Source File
rocrand_common.h
1 // Copyright (c) 2017-2025 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCRAND_COMMON_H_
22 #define ROCRAND_COMMON_H_
23 
24 #define ROCRAND_2POW16_INV (1.5258789e-05f)
25 #define ROCRAND_2POW16_INV_2PI (9.58738e-05f)
26 #define ROCRAND_2POW32_INV (2.3283064e-10f)
27 #define ROCRAND_2POW32_INV_DOUBLE (2.3283064365386963e-10)
28 #define ROCRAND_2POW64_INV (5.4210109e-20f)
29 #define ROCRAND_2POW64_INV_DOUBLE (5.4210108624275221700372640043497e-20)
30 #define ROCRAND_2POW32_INV_2PI (1.46291807e-09f)
31 #define ROCRAND_2POW53_INV_DOUBLE (1.1102230246251565e-16)
32 #define ROCRAND_PI (3.141592653f)
33 #define ROCRAND_PI_DOUBLE (3.1415926535897932)
34 #define ROCRAND_2PI (6.2831855f)
35 #define ROCRAND_SQRT2 (1.4142135f)
36 #define ROCRAND_SQRT2_DOUBLE (1.4142135623730951)
37 
38 #include <hip/hip_runtime.h>
39 #include <utility>
40 
41 #define ROCRAND_KERNEL __global__ static
42 
43 #if __HIP_DEVICE_COMPILE__ \
44  && (defined(__HIP_PLATFORM_AMD__) \
45  || (defined(__HIP_PLATFORM_NVCC__) && (__CUDA_ARCH__ >= 530)))
46  #define ROCRAND_HALF_MATH_SUPPORTED
47 #endif
48 
49 // Copyright 2001 John Maddock.
50 // Copyright 2017 Peter Dimov.
51 //
52 // Distributed under the Boost Software License, Version 1.0.
53 //
54 // See http://www.boost.org/LICENSE_1_0.txt
55 //
56 // BOOST_STRINGIZE(X)
57 #define ROCRAND_STRINGIZE(X) ROCRAND_DO_STRINGIZE(X)
58 #define ROCRAND_DO_STRINGIZE(X) #X
59 
60 // Copyright 2017 Peter Dimov.
61 //
62 // Distributed under the Boost Software License, Version 1.0.
63 //
64 // See http://www.boost.org/LICENSE_1_0.txt
65 //
66 // BOOST_PRAGMA_MESSAGE("message")
67 //
68 // Expands to the equivalent of #pragma message("message")
69 #if defined(__INTEL_COMPILER)
70  #define ROCRAND_PRAGMA_MESSAGE(x) \
71  __pragma(message(__FILE__ "(" ROCRAND_STRINGIZE(__LINE__) "): note: " x))
72 #elif defined(__GNUC__)
73  #define ROCRAND_PRAGMA_MESSAGE(x) _Pragma(ROCRAND_STRINGIZE(message(x)))
74 #elif defined(_MSC_VER)
75  #define ROCRAND_PRAGMA_MESSAGE(x) \
76  __pragma(message(__FILE__ "(" ROCRAND_STRINGIZE(__LINE__) "): note: " x))
77 #else
78  #define ROCRAND_PRAGMA_MESSAGE(x)
79 #endif
80 
81 #if __cplusplus >= 201402L
82  #define ROCRAND_DEPRECATED(msg) [[deprecated(msg)]]
83 #elif defined(_MSC_VER) && !defined(__clang__)
84  #define ROCRAND_DEPRECATED(msg) __declspec(deprecated(msg))
85 #elif defined(__clang__) || defined(__GNUC__)
86  #define ROCRAND_DEPRECATED(msg) __attribute__((deprecated(msg)))
87 #else
88  #define ROCRAND_DEPRECATED(msg)
89 #endif
90 
91 // This is an accessor macro for HIP vector types (eg. int2, float4, etc.).
92 // Prior to HIP 7.0, individual elements could be accessed through the
93 // data member:
94 //
95 // int2 vec;
96 // vec.data[0] = 1;
97 //
98 // Beginning with HIP 7.0, the data member is hidden, and individual
99 // elements must be accessed like this:
100 //
101 // int2 vec;
102 // vec[0] = 1;
103 //
104 // You can use the macro like this:
105 //
106 // int2 vec;
107 // ROCRAND_HIPVEC_ACCESS(vec)[0];
108 //
109 #if defined(__HIP_PLATFORM_AMD__)
110  #if HIP_VERSION_MAJOR < 7
111  #define ROCRAND_HIPVEC_ACCESS(x) x.data
112  #else
113  #define ROCRAND_HIPVEC_ACCESS(x) x
114  #endif
115 #endif
116 
117 namespace rocrand_device {
118 namespace detail {
119 
120 __forceinline__ __device__ __host__
121 unsigned long long
122  mad_u64_u32(const unsigned int x, const unsigned int y, const unsigned long long z)
123 {
124  return static_cast<unsigned long long>(x) * static_cast<unsigned long long>(y) + z;
125 }
126 
127 __forceinline__ __device__ __host__
128 unsigned long long mul_u64_u32(const unsigned int x, const unsigned int y)
129 {
130  return static_cast<unsigned long long>(x) * static_cast<unsigned long long>(y);
131 }
132 
133 // This helps access fields of engine's internal state which
134 // saves floats and doubles generated using the Box–Muller transform
135 template<typename Engine>
136 struct engine_boxmuller_helper
137 {
138  static __forceinline__ __device__ __host__ bool has_float(const Engine* engine)
139  {
140  return engine->m_state.boxmuller_float_state != 0;
141  }
142 
143  static __forceinline__ __device__ __host__ float get_float(Engine* engine)
144  {
145  engine->m_state.boxmuller_float_state = 0;
146  return engine->m_state.boxmuller_float;
147  }
148 
149  static __forceinline__ __device__ __host__ void save_float(Engine* engine, float f)
150  {
151  engine->m_state.boxmuller_float_state = 1;
152  engine->m_state.boxmuller_float = f;
153  }
154 
155  static __forceinline__ __device__ __host__ bool has_double(const Engine* engine)
156  {
157  return engine->m_state.boxmuller_double_state != 0;
158  }
159 
160  static __forceinline__ __device__ __host__ float get_double(Engine* engine)
161  {
162  engine->m_state.boxmuller_double_state = 0;
163  return engine->m_state.boxmuller_double;
164  }
165 
166  static __forceinline__ __device__ __host__ void save_double(Engine* engine, double d)
167  {
168  engine->m_state.boxmuller_double_state = 1;
169  engine->m_state.boxmuller_double = d;
170  }
171 };
172 
173 template<typename T>
174 __forceinline__ __device__ __host__ void split_ull(T& lo, T& hi, unsigned long long int val);
175 
176 template<>
177 __forceinline__ __device__ __host__ void
178  split_ull(unsigned int& lo, unsigned int& hi, unsigned long long int val)
179 {
180  lo = val & 0xFFFFFFFF;
181  hi = (val >> 32) & 0xFFFFFFFF;
182 }
183 
184 template<>
185 __forceinline__ __device__ __host__ void
186  split_ull(unsigned long long int& lo, unsigned long long int& hi, unsigned long long int val)
187 {
188  lo = val;
189  hi = 0;
190 }
191 
192 } // end namespace detail
193 } // end namespace rocrand_device
194 
195 #endif // ROCRAND_COMMON_H_