14 #ifndef NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED 19 typedef signed char int8_t;
20 typedef short int16_t;
22 typedef long long int64_t;
23 typedef unsigned char uint8_t;
24 typedef unsigned int uint32_t;
25 typedef unsigned short uint16_t;
26 typedef unsigned long long uint64_t;
28 #define NANOVDB_ASSERT(x) 31 #define UINT64_C(x) (x ## ULL) 34 #else // !__CUDACC_RTC__ 44 #ifdef NANOVDB_USE_IOSTREAMS 46 #endif// ifdef NANOVDB_USE_IOSTREAMS 50 #define NANOVDB_ASSERT(x) assert(x) 52 #define NANOVDB_ASSERT(x) 55 #if defined(NANOVDB_USE_INTRINSICS) && defined(_MSC_VER) 57 #pragma intrinsic(_BitScanReverse) 58 #pragma intrinsic(_BitScanForward) 59 #pragma intrinsic(_BitScanReverse64) 60 #pragma intrinsic(_BitScanForward64) 63 #endif // __CUDACC_RTC__ 65 #if defined(__CUDACC__) || defined(__HIP__) 68 #define __hostdev__ __host__ __device__ // Runs on the CPU and GPU, called from the CPU or the GPU 73 #define __hostdev__ // Runs on the CPU and GPU, called from the CPU or the GPU 76 #define __global__ // Runs on the GPU, called from the CPU or the GPU 79 #define __device__ // Runs on the GPU, called from the GPU 82 #define __host__ // Runs on the CPU, called from the CPU 85 #endif // if defined(__CUDACC__) || defined(__HIP__) 89 #if defined(_MSC_VER) && defined(__CUDACC__) 90 #define NANOVDB_HOSTDEV_DISABLE_WARNING __pragma("hd_warning_disable") 91 #elif defined(__GNUC__) && defined(__CUDACC__) 92 #define NANOVDB_HOSTDEV_DISABLE_WARNING _Pragma("hd_warning_disable") 94 #define NANOVDB_HOSTDEV_DISABLE_WARNING 113 #define NANOVDB_PRAGMA(X) _Pragma(#X) 114 #define NANOVDB_DEPRECATED_HEADER(MSG) NANOVDB_PRAGMA(GCC warning MSG) 115 #elif defined(_MSC_VER) 116 #define NANOVDB_STRINGIZE_(MSG) #MSG 117 #define NANOVDB_STRINGIZE(MSG) NANOVDB_STRINGIZE_(MSG) 118 #define NANOVDB_DEPRECATED_HEADER(MSG) \ 119 __pragma(message(__FILE__ "(" NANOVDB_STRINGIZE(__LINE__) ") : Warning: " MSG)) 123 #define NANOVDB_OFFSETOF(CLASS, MEMBER) ((int)(size_t)((char*)&((CLASS*)0)->MEMBER - (char*)0)) 169 for (
char *p = dst; (*p++ = *src) !=
'\0'; ++src);
189 for (; i < max && src[i] !=
'\0'; ++i) dst[i] = src[i];
190 for (; i <
max; ++i) dst[i] =
'\0';
204 if (num == 0) dst[len++] =
'0';
205 for (
int abs = num < 0 && bas == 10 ? -num : num; abs; abs /= bas) {
206 const int rem = abs % bas;
207 dst[len++] = rem > 9 ? rem - 10 +
'a' : rem +
'0';
209 if (num < 0) dst[len++] =
'-';
210 for (
char *a = dst, *b = a + len - 1; a < b; ++a, --b) {
229 while (*p !=
'\0') ++p;
243 while (*p !=
'\0') ++p;
257 while(*lhs !=
'\0' && (*lhs == *rhs)){
261 return *(
const unsigned char*)lhs - *(
const unsigned char*)rhs;
270 return strcmp(lhs, rhs) == 0;
277 template <
typename T,
typename... Types>
285 template <
typename T,
typename... Types>
300 const size_t wordCount = byteCount >> 3;
301 if (wordCount << 3 == byteCount) {
302 for (
auto *d = (uint64_t*)dst, *e = d + wordCount; d != e; ++d) *d = 0ULL;
304 for (
auto *d = (
char*)dst, *e = d + byteCount; d != e; ++d) *d =
'\0';
313 template<
typename T0,
typename T1,
typename ...T>
319 template<
typename T0,
typename T1>
320 struct is_same<T0, T1> {
static constexpr
bool value =
false;};
323 struct is_same<T, T> {
static constexpr
bool value =
true;};
334 template <
bool,
typename T =
void>
337 template <
typename T>
342 template<
bool,
typename T =
void>
351 struct is_const {
static constexpr
bool value =
false;};
354 struct is_const<const T> {
static constexpr
bool value =
true;};
372 template<
bool,
class TrueT,
class FalseT>
378 template<
class TrueT,
class FalseT>
400 template <
typename T>
406 template <
typename T>
414 template <
typename T>
420 template <
typename T>
430 template<
typename T,
typename ReferenceT>
438 template<
typename T,
typename ReferenceT>
450 template<
typename AnyType,
template<
typename...>
class TemplateType>
452 template<
typename... Args,
template<
typename...>
class TemplateType>
455 static const bool value =
true;
467 return reinterpret_cast<const char*
>(p) - reinterpret_cast<const char*>(q);
477 template<
typename DstT =
void>
481 return reinterpret_cast<DstT*
>(
reinterpret_cast<char*
>(p) + offset);
489 template<
typename DstT =
void>
493 return reinterpret_cast<const DstT*
>(
reinterpret_cast<const char*
>(p) + offset);
505 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 507 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 509 _BitScanForward(&index, v);
510 return static_cast<uint32_t
>(index);
511 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 512 return static_cast<uint32_t
>(__builtin_ctzl(v));
515 static const unsigned char DeBruijn[32] = {
516 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
518 #if defined(_MSC_VER) && !defined(__NVCC__) 519 #pragma warning(push) 520 #pragma warning(disable : 4146) 522 return DeBruijn[uint32_t((v & -v) * 0x077CB531U) >> 27];
523 #if defined(_MSC_VER) && !defined(__NVCC__) 537 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 538 return __ffsll(static_cast<unsigned long long int>(v)) - 1;
539 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 541 _BitScanForward64(&index, v);
542 return static_cast<uint32_t
>(index);
543 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 544 return static_cast<uint32_t
>(__builtin_ctzll(v));
547 static const unsigned char DeBruijn[64] = {
548 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
549 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
550 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
551 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12,
554 #if defined(_MSC_VER) && !defined(__NVCC__) 555 #pragma warning(push) 556 #pragma warning(disable : 4146) 558 return DeBruijn[uint64_t((v & -v) * UINT64_C(0x022FDD63CC95386D)) >> 58];
559 #if defined(_MSC_VER) && !defined(__NVCC__) 575 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 576 return sizeof(uint32_t) * 8 - 1 - __clz(v);
577 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 579 _BitScanReverse(&index, v);
580 return static_cast<uint32_t
>(index);
581 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 582 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzl(v);
585 static const unsigned char DeBruijn[32] = {
586 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
587 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
593 return DeBruijn[uint32_t(v * 0x07C4ACDDU) >> 27];
604 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 605 return sizeof(
unsigned long) * 8 - 1 - __clzll(static_cast<unsigned long long int>(v));
606 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 608 _BitScanReverse64(&index, v);
609 return static_cast<uint32_t
>(index);
610 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 611 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzll(v);
613 const uint32_t* p =
reinterpret_cast<const uint32_t*
>(&v);
624 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 628 #elif defined(_MSC_VER) && defined(_M_X64) && (_MSC_VER >= 1928) && defined(NANOVDB_USE_INTRINSICS) 630 return uint32_t(__popcnt64(v));
631 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 633 return __builtin_popcountll(v);
634 #else // use software implementation 636 v = v - ((v >> 1) & uint64_t(0x5555555555555555));
637 v = (v & uint64_t(0x3333333333333333)) + ((v >> 2) & uint64_t(0x3333333333333333));
638 return (((v + (v >> 4)) & uint64_t(0xF0F0F0F0F0F0F0F)) * uint64_t(0x101010101010101)) >> 56;
644 [[deprecated(
"Use nanovdb::util::findLowestOn instead")]]
646 [[deprecated(
"Use nanovdb::util::findLowestOn instead")]]
648 [[deprecated(
"Use nanovdb::util::findHighestOn instead")]]
650 [[deprecated(
"Use nanovdb::util::findHighestOn instead")]]
652 [[deprecated(
"Use nanovdb::util::countOn instead")]]
657 #endif // end of NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED
C++11 implementation of std::enable_if.
Definition: Util.h:335
Trait used to identify template parameter that are pointers.
Definition: Util.h:361
T type
Definition: Util.h:343
T type
Definition: Util.h:407
static int64_t PtrDiff(const void *p, const void *q)
Compute the distance, in bytes, between two pointers, dist = p - q.
Definition: Util.h:464
Trait use to const from type. Default implementation is just a pass-through.
Definition: Util.h:387
char * strcpy(char *dst, const char *src)
Copy characters from src to dst.
Definition: Util.h:166
Trait use to remove reference, i.e. "&", qualifier from a type. Default implementation is just a pass...
Definition: Util.h:401
char * strncpy(char *dst, const char *src, size_t max)
Copies the first num characters of src to dst. If the end of the source C string (which is signaled b...
Definition: Util.h:185
uint32_t FindLowestOn(uint32_t v)
Definition: Util.h:645
FalseT type
Definition: Util.h:379
char * strcat(char *dst, const char *src)
Appends a copy of the character string pointed to by src to the end of the character string pointed t...
Definition: Util.h:225
#define __hostdev__
Definition: Util.h:73
char * sprint(char *dst)
Definition: Util.h:275
Metafunction used to determine if the first template parameter is a specialization of the class templ...
Definition: Util.h:451
Definition: GridHandle.h:27
C++11 implementation of std::conditional.
Definition: Util.h:373
size_t strlen(const char *str)
length of a c-sting, excluding '\0'.
Definition: Util.h:153
T type
Definition: Util.h:401
uint32_t countOn(uint64_t v)
Definition: Util.h:622
T && declval() noexcept
Minimal implementation of std::declval, which converts any type T to.
int strcmp(const char *lhs, const char *rhs)
Compares two null-terminated byte strings lexicographically.
Definition: Util.h:255
uint32_t CountOn(uint64_t v)
Definition: Util.h:653
Trait use to remove pointer, i.e. "*", qualifier from a type. Default implementation is just a pass-t...
Definition: Util.h:415
uint32_t FindHighestOn(uint32_t v)
Definition: Util.h:649
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition: Util.h:144
#define NANOVDB_HOSTDEV_DISABLE_WARNING
Definition: Util.h:94
uint32_t findLowestOn(uint32_t v)
Returns the index of the lowest, i.e. least significant, on bit in the specified 32 bit word...
Definition: Util.h:502
bool streq(const char *lhs, const char *rhs)
Test if two null-terminated byte strings are the same.
Definition: Util.h:268
T type
Definition: Util.h:415
T type
Definition: Util.h:393
#define NANOVDB_ASSERT(x)
Definition: Util.h:50
C++11 implementation of std::is_floating_point.
Definition: Util.h:329
static void * memzero(void *dst, size_t byteCount)
Zero initialization of memory.
Definition: Util.h:297
static DstT * PtrAdd(void *p, int64_t offset)
Adds a byte offset to a non-const pointer to produce another non-const pointer.
Definition: Util.h:478
C++11 implementation of std::is_same.
Definition: Util.h:314
T type
Definition: Util.h:387
T type
Definition: Util.h:338
Trait used to transfer the const-ness of a reference type to another type.
Definition: Util.h:431
uint32_t findHighestOn(uint32_t v)
Returns the index of the highest, i.e. most significant, on bit in the specified 32 bit word...
Definition: Util.h:572
T type
Definition: Util.h:421
TrueT type
Definition: Util.h:373
typename remove_const< T >::type type
Definition: Util.h:431
const typename remove_const< T >::type type
Definition: Util.h:439