Fermat
types.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <assert.h>
31 #include <vector_types.h>
32 #include <vector_functions.h>
33 #include <thrust/iterator/detail/host_system_tag.h>
34 #include <thrust/iterator/detail/device_system_tag.h>
35 #include <thrust/iterator/detail/any_system_tag.h>
36 
37 #ifdef __CUDACC__
38  #define CUGAR_HOST_DEVICE __host__ __device__
39  #define CUGAR_HOST __host__
40  #define CUGAR_DEVICE __device__
41 #else
42  #define CUGAR_HOST_DEVICE
43  #define CUGAR_HOST
44  #define CUGAR_DEVICE
45 #endif
46 
47 #ifdef __CUDA_ARCH__
48 #define CUGAR_RESTRICT __restrict__
49 #define CUGAR_SHARED __shared__
50 #else
51 #define CUGAR_RESTRICT
52 #define CUGAR_SHARED
53 #endif
54 
55 #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ > 0
56 #define CUGAR_DEVICE_COMPILATION
57 #endif
58 
59 #define CUGAR_API_CS
60 #define CUGAR_API_SS
61 
62 #if defined(WIN32)
63  #if defined(CUGAR_EXPORTS)
64  #define CUGAR_API __declspec(dllexport)
65  #elif defined(CUGAR_IMPORTS)
66  #define CUGAR_API __declspec(dllimport)
67  #else
68  #define CUGAR_API
69  #endif
70 #else
71  #define CUGAR_API
72 #endif
73 
74 #ifdef WIN32
75 #define CUGAR_FORCEINLINE __forceinline
76 #else
77 #define CUGAR_FORCEINLINE __inline__
78 #endif
79 
80 #ifdef WIN32
81 #define CUGAR_ALIGN_BEGIN(n) __declspec( align( n ) )
82 #define CUGAR_ALIGN_END(n)
83 #elif defined(__GNUC__)
84 #define CUGAR_ALIGN_BEGIN(n)
85 #define CUGAR_ALIGN_END(n) __attribute__ ((aligned(n)));
86 #else
87 #define CUGAR_ALIGN_BEGIN(n)
88 #define CUGAR_ALIGN_END(n)
89 #endif
90 
91 #if defined(CUGAR_CUDA_DEBUG)
92 #define CUGAR_CUDA_DEBUG_STATEMENT(x) x
93 #else
94 #define CUGAR_CUDA_DEBUG_STATEMENT(x)
95 #endif
96 
97 #if defined(CUGAR_CUDA_ASSERTS)
98  // the following trickery eliminates the "controlling expression is constant" warning from nvcc when doing assert(!"some string")
99  #define CUGAR_CUDA_ASSERT(x) { const bool __yes = true; assert(x && __yes); }
100  #define CUGAR_CUDA_ASSERT_IF(cond, x, ...) if ((cond) && !(x)) {printf(__VA_ARGS__); CUGAR_CUDA_ASSERT(x); }
101  #define CUGAR_CUDA_DEBUG_ASSERT(x,...) if (!(x)) { printf(__VA_ARGS__); CUGAR_CUDA_ASSERT(x); }
102 #elif defined(CUGAR_CUDA_NON_BLOCKING_ASSERTS) // !defined(CUGAR_CUDA_ASSERTS)
103  #define CUGAR_CUDA_ASSERT(x)
104  #define CUGAR_CUDA_ASSERT_IF(cond, x, ...) if ((cond) && !(x)) { printf(__VA_ARGS__); }
105  #define CUGAR_CUDA_DEBUG_ASSERT(x,...) if (!(x)) { printf(__VA_ARGS__); }
106 #else // !defined(CUGAR_NON_BLOCKING_ASSERTS) && !defined(CUGAR_CUDA_ASSERTS)
107  #define CUGAR_CUDA_ASSERT(x)
108  #define CUGAR_CUDA_ASSERT_IF(cond, x, ...)
109  #define CUGAR_CUDA_DEBUG_ASSERT(x,...)
110 #endif
111 
112 #if defined(CUGAR_CUDA_DEBUG)
113  #define CUGAR_CUDA_DEBUG_PRINT(...) printf(__VA_ARGS__)
114  #define CUGAR_CUDA_DEBUG_PRINT_IF(cond,...) if (cond) printf(__VA_ARGS__)
115  #define CUGAR_CUDA_DEBUG_SELECT(debug_val,normal_val) (debug_val)
116 #else // !defined(CUGAR_CUDA_DEBUG)
117  #define CUGAR_CUDA_DEBUG_PRINT(...)
118  #define CUGAR_CUDA_DEBUG_PRINT_IF(cond,...)
119  #define CUGAR_CUDA_DEBUG_SELECT(debug_val,normal_val) (normal_val)
120 #endif
121 
122 #if defined(CUGAR_CUDA_DEBUG)
123  #if defined(CUGAR_CUDA_ASSERTS)
124  #define CUGAR_CUDA_DEBUG_CHECK_IF(cond, check,...) if ((cond) && (!(check))) { printf(__VA_ARGS__); assert(check); }
125  #else // !defined(CUGAR_CUDA_ASSERTS)
126  #define CUGAR_CUDA_DEBUG_CHECK_IF(cond, check,...) if ((cond) && (!(check))) printf(__VA_ARGS__)
127  #endif
128 #else // !defined(CUGAR_CUDA_DEBUG)
129 #define CUGAR_CUDA_DEBUG_CHECK_IF(cond, check,...)
130 #endif
131 
132 #if defined(__CUDACC__)
133 #define CUGAR_HOST_DEVICE_TEMPLATE \
134 #pragma hd_warning_disable
135 #else
136 #define CUGAR_HOST_DEVICE_TEMPLATE
137 #endif
138 
139 #ifdef WIN32
140 #define WINONLY(x) x
141 #else
142 #define WINONLY(x)
143 #endif
144 
145 #ifdef _MSC_VER
146 #ifndef _CRT_SECURE_NO_WARNINGS
147 #define _CRT_SECURE_NO_WARNINGS 1
148 #endif
149 #endif
150 
151 // CUGAR_VAR_UNUSED can be prepended to a variable to turn off unused variable warnings
152 // this should only be used when the variable actually is used and the warning is wrong
153 // (e.g., variables which are used only as template parameters for kernel launches)
154 #if defined(__GNUC__)
155 #define CUGAR_VAR_UNUSED __attribute__((unused))
156 #else
157 #define CUGAR_VAR_UNUSED
158 #endif
159 
160 namespace cugar {
161 
164 
165 typedef unsigned long long uint64;
166 typedef unsigned int uint32;
167 typedef unsigned short uint16;
168 typedef unsigned char uint8;
169 typedef long long int64;
170 typedef int int32;
171 typedef short int16;
172 typedef signed char int8;
173 
178 
181 struct host_tag : public thrust::host_system_tag {};
182 
185 struct device_tag : public thrust::device_system_tag {};
186 
188 
191 struct null_type {};
192 
195 
200 
203 
206 template <typename T> struct to_const { typedef T type; };
207 template <typename T> struct to_const<T&> { typedef const T& type; };
208 template <typename T> struct to_const<T*> { typedef const T* type; };
209 template <typename T> struct to_const<const T&> { typedef const T& type; };
210 template <typename T> struct to_const<const T*> { typedef const T* type; };
211 
214 template <typename T> struct reference_subtype { typedef typename T::reference type; };
215 template <typename T> struct reference_subtype<T*> { typedef T& type; };
216 template <typename T> struct reference_subtype<const T*> { typedef const T& type; };
217 template <> struct reference_subtype<null_type> { typedef null_type type; };
218 
221 template <typename T> struct plain_view_subtype { typedef typename T::plain_view_type type; };
222 template <typename T> struct plain_view_subtype<const T> { typedef typename T::const_plain_view_type type; };
223 template <> struct plain_view_subtype<null_type> { typedef null_type type; };
224 template <typename T> struct plain_view_subtype<const T*> { typedef const T* type; };
225 template <typename T> struct plain_view_subtype<T*> { typedef T* type; };
226 
229 template <typename T> struct signed_type {};
230 template <> struct signed_type<uint32> { typedef int32 type; };
231 template <> struct signed_type<uint64> { typedef int64 type; };
232 template <> struct signed_type<int32> { typedef int32 type; };
233 template <> struct signed_type<int64> { typedef int64 type; };
234 
237 template <typename T> struct unsigned_type {};
238 template <> struct unsigned_type<uint32> { typedef uint32 type; };
239 template <> struct unsigned_type<uint64> { typedef uint64 type; };
240 template <> struct unsigned_type<int32> { typedef uint32 type; };
241 template <> struct unsigned_type<int64> { typedef uint64 type; };
242 
245 template <typename T1, typename T2> struct same_type { static const bool pred = false; };
246 template <typename T> struct same_type<T,T> { static const bool pred = true; };
247 
250 template <typename A, typename B>
251 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool equal() { return same_type<A,B>::pred; }
252 
255 template <bool predicate, typename T, typename F> struct if_true {};
256 template <typename T, typename F> struct if_true<true,T,F> { typedef T type; };
257 template <typename T, typename F> struct if_true<false,T,F> { typedef F type; };
258 
261 template <typename A, typename B, typename T, typename F> struct if_equal
262 {
263  typedef typename if_true< same_type<A,B>::pred, T, F >::type type;
264 };
265 
268 template <typename A, typename B, uint32 N> struct binary_switch { typedef B type; };
269 
272 template <typename A, typename B> struct binary_switch<A,B,0> { typedef A type; };
273 
275 
278 template <typename Out, typename In>
280 {
281  In in;
282  Out out;
283 };
284 
287 template <typename Out, typename In>
288 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Out binary_cast(const In in)
289 {
290 #if defined(__CUDA_ARCH__)
291  return reinterpret_cast<const Out&>(in);
292 #else
293  BinaryCast<Out,In> inout;
294  inout.in = in;
295  return inout.out;
296 #endif
297 }
298 
299 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool is_pow2(const uint32 C) { return (C & (C - 1)) == 0u; }
300 
301 template <uint32 C>
302 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool is_pow2_static() { return (C & (C-1)) == 0u; }
303 
304 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE uint32 next_power_of_two(uint32 v)
305 {
306  --v;
307  v |= v >> 1;
308  v |= v >> 2;
309  v |= v >> 4;
310  v |= v >> 8;
311  v |= v >> 16;
312  return ++v;
313 }
314 
315 // round up to next multiple of N, where N is a power of 2.
316 template <uint32 N, typename I> CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
317 I align(const I a) { return (N > 1) ? I(a + N-1) & I(~(N-1)) : a; }
318 
319 // round down to previous multiple of N, where N is a power of 2.
320 template <uint32 N, typename I> CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
321 I align_down(const I a) { return (N > 1) ? I((a / N) * N) : a; }
322 
325 
326 } // namespace cugar
327 
328 //
329 // add basic C++ operators to CUDA vector types - it's not nice, but we need it to apply
330 // basic algorithms requiring their presence
331 //
332 
335 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
336 bool operator==(const uint2 op1, const uint2 op2) { return op1.x == op2.x && op1.y == op2.y; }
337 
340 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
341 bool operator!=(const uint2 op1, const uint2 op2) { return op1.x != op2.x || op1.y != op2.y; }
342 
345 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
346 bool operator==(const uint3 op1, const uint3 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z; }
347 
350 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
351 bool operator!=(const uint3 op1, const uint3 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z; }
352 
355 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
356 bool operator==(const uint4 op1, const uint4 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z && op1.w == op2.w; }
357 
360 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
361 bool operator!=(const uint4 op1, const uint4 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z || op1.w != op2.w; }
362 
365 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
366 bool operator==(const int2 op1, const int2 op2) { return op1.x == op2.x && op1.y == op2.y; }
367 
370 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
371 bool operator!=(const int2 op1, const int2 op2) { return op1.x != op2.x || op1.y != op2.y; }
372 
375 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
376 bool operator==(const int3 op1, const int3 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z; }
377 
380 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
381 bool operator!=(const int3 op1, const int3 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z; }
382 
385 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
386 bool operator==(const int4 op1, const int4 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z && op1.w == op2.w; }
387 
390 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
391 bool operator!=(const int4 op1, const int4 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z || op1.w != op2.w; }
392 
395 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
396 bool operator==(const float2 op1, const float2 op2) { return op1.x == op2.x && op1.y == op2.y; }
397 
400 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
401 bool operator!=(const float2 op1, const float2 op2) { return op1.x != op2.x || op1.y != op2.y; }
402 
405 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
406 bool operator==(const float3 op1, const float3 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z; }
407 
410 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
411 bool operator!=(const float3 op1, const float3 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z; }
412 
415 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
416 bool operator==(const float4 op1, const float4 op2) { return op1.x == op2.x && op1.y == op2.y && op1.z == op2.z && op1.w == op2.w; }
417 
420 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
421 bool operator!=(const float4 op1, const float4 op2) { return op1.x != op2.x || op1.y != op2.y || op1.z != op2.z || op1.w != op2.w; }
Definition: types.h:279
Definition: types.h:181
Definition: types.h:229
Definition: types.h:206
Definition: types.h:221
Definition: types.h:261
Definition: types.h:214
Definition: types.h:237
Definition: types.h:185
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Out binary_cast(const In in)
Definition: types.h:288
Definition: types.h:245
Definition: types.h:255
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool equal()
Definition: types.h:251
Definition: types.h:191
Definition: types.h:268