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;};
325 template<
typename T0,
typename T1,
typename ...T>
340 template <
bool,
typename T =
void>
343 template <
typename T>
346 template<
bool Test,
typename T =
void>
351 template<
bool,
typename T =
void>
357 template<
bool Test,
typename T =
void>
363 struct is_const {
static constexpr
bool value =
false;};
366 struct is_const<const T> {
static constexpr
bool value =
true;};
390 template<
bool,
class TrueT,
class FalseT>
396 template<
class TrueT,
class FalseT>
399 template<
bool Test,
class TrueT,
class FalseT>
424 template <
typename T>
430 template <
typename T>
433 template <
typename T>
441 template <
typename T>
447 template <
typename T>
450 template <
typename T>
460 template<
typename T,
typename ReferenceT>
468 template<
typename T,
typename ReferenceT>
471 template<
typename T,
typename ReferenceT>
483 template<
typename AnyType,
template<
typename...>
class TemplateType>
486 template<
typename... Args,
template<
typename...>
class TemplateType>
489 static const bool value =
true;
501 return reinterpret_cast<const char*
>(p) - reinterpret_cast<const char*>(q);
511 template<
typename DstT =
void>
515 return reinterpret_cast<DstT*
>(
reinterpret_cast<char*
>(p) + offset);
523 template<
typename DstT =
void>
527 return reinterpret_cast<const DstT*
>(
reinterpret_cast<const char*
>(p) + offset);
539 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 541 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 543 _BitScanForward(&index, v);
544 return static_cast<uint32_t
>(index);
545 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 546 return static_cast<uint32_t
>(__builtin_ctzl(v));
549 static const unsigned char DeBruijn[32] = {
550 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};
552 #if defined(_MSC_VER) && !defined(__NVCC__) 553 #pragma warning(push) 554 #pragma warning(disable : 4146) 556 return DeBruijn[uint32_t((v & -v) * 0x077CB531U) >> 27];
557 #if defined(_MSC_VER) && !defined(__NVCC__) 571 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 572 return __ffsll(static_cast<unsigned long long int>(v)) - 1;
573 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 575 _BitScanForward64(&index, v);
576 return static_cast<uint32_t
>(index);
577 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 578 return static_cast<uint32_t
>(__builtin_ctzll(v));
581 static const unsigned char DeBruijn[64] = {
582 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
583 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
584 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
585 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12,
588 #if defined(_MSC_VER) && !defined(__NVCC__) 589 #pragma warning(push) 590 #pragma warning(disable : 4146) 592 return DeBruijn[uint64_t((v & -v) * UINT64_C(0x022FDD63CC95386D)) >> 58];
593 #if defined(_MSC_VER) && !defined(__NVCC__) 609 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 610 return sizeof(uint32_t) * 8 - 1 - __clz(v);
611 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 613 _BitScanReverse(&index, v);
614 return static_cast<uint32_t
>(index);
615 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 616 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzl(v);
619 static const unsigned char DeBruijn[32] = {
620 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
621 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
627 return DeBruijn[uint32_t(v * 0x07C4ACDDU) >> 27];
638 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 639 return sizeof(
unsigned long) * 8 - 1 - __clzll(static_cast<unsigned long long int>(v));
640 #elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS) 642 _BitScanReverse64(&index, v);
643 return static_cast<uint32_t
>(index);
644 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 645 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzll(v);
647 const uint32_t* p =
reinterpret_cast<const uint32_t*
>(&v);
658 #if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS) 662 #elif defined(_MSC_VER) && defined(_M_X64) && (_MSC_VER >= 1928) && defined(NANOVDB_USE_INTRINSICS) 664 return uint32_t(__popcnt64(v));
665 #elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS) 667 return __builtin_popcountll(v);
668 #else // use software implementation 670 v = v - ((v >> 1) & uint64_t(0x5555555555555555));
671 v = (v & uint64_t(0x3333333333333333)) + ((v >> 2) & uint64_t(0x3333333333333333));
672 return (((v + (v >> 4)) & uint64_t(0xF0F0F0F0F0F0F0F)) * uint64_t(0x101010101010101)) >> 56;
678 [[deprecated(
"Use nanovdb::util::findLowestOn instead")]]
680 [[deprecated(
"Use nanovdb::util::findLowestOn instead")]]
682 [[deprecated(
"Use nanovdb::util::findHighestOn instead")]]
684 [[deprecated(
"Use nanovdb::util::findHighestOn instead")]]
686 [[deprecated(
"Use nanovdb::util::countOn instead")]]
691 #endif // end of NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED typename enable_if< Test, T >::type enable_if_t
Definition: Util.h:347
C++11 implementation of std::enable_if.
Definition: Util.h:341
Trait used to identify template parameter that are pointers.
Definition: Util.h:376
static constexpr bool is_floating_point_v
Definition: Util.h:335
T type
Definition: Util.h:352
T type
Definition: Util.h:431
static int64_t PtrDiff(const void *p, const void *q)
Compute the distance, in bytes, between two pointers, dist = p - q.
Definition: Util.h:498
Trait use to const from type. Default implementation is just a pass-through.
Definition: Util.h:408
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:425
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:679
FalseT type
Definition: Util.h:397
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
typename disable_if< Test, T >::type disable_if_t
Definition: Util.h:358
Metafunction used to determine if the first template parameter is a specialization of the class templ...
Definition: Util.h:484
Definition: GridHandle.h:27
typename conditional< Test, TrueT, FalseT >::type conditional_t
Definition: Util.h:400
C++11 implementation of std::conditional.
Definition: Util.h:391
size_t strlen(const char *str)
length of a c-sting, excluding '\0'.
Definition: Util.h:153
T type
Definition: Util.h:425
typename remove_const< T >::type remove_const_t
Definition: Util.h:417
uint32_t countOn(uint64_t v)
Definition: Util.h:656
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:687
Trait use to remove pointer, i.e. "*", qualifier from a type. Default implementation is just a pass-t...
Definition: Util.h:442
uint32_t FindHighestOn(uint32_t v)
Definition: Util.h:683
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:536
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:442
T type
Definition: Util.h:414
static constexpr bool is_const_v
Definition: Util.h:369
#define NANOVDB_ASSERT(x)
Definition: Util.h:50
C++11 implementation of std::is_floating_point.
Definition: Util.h:332
typename remove_const< T >::type remove_reference_t
Definition: Util.h:434
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:512
typename match_const< T, ReferenceT >::type match_const_t
Definition: Util.h:472
static constexpr bool is_pointer_v
Definition: Util.h:385
typename remove_pointer< T >::type remove_pointer_t
Definition: Util.h:451
C++11 implementation of std::is_same.
Definition: Util.h:314
T type
Definition: Util.h:408
T type
Definition: Util.h:344
Trait used to transfer the const-ness of a reference type to another type.
Definition: Util.h:461
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:606
T type
Definition: Util.h:448
TrueT type
Definition: Util.h:391
static constexpr bool is_same_v
Definition: Util.h:326
typename remove_const< T >::type type
Definition: Util.h:461
const typename remove_const< T >::type type
Definition: Util.h:469