14#ifndef NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED
15#define NANOVDB_UTIL_UTIL_H_HAS_BEEN_INCLUDED
19typedef signed char int8_t;
22typedef long long int64_t;
23typedef unsigned char uint8_t;
24typedef unsigned int uint32_t;
25typedef unsigned short uint16_t;
26typedef unsigned long long uint64_t;
28#define NANOVDB_ASSERT(x)
31#define UINT64_C(x) (x ## ULL)
44#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)
65#if defined(__CUDACC__) || defined(__HIP__)
68#define __hostdev__ __host__ __device__
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;
277template <
typename T,
typename... Types>
285template <
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';
313template<
typename T0,
typename T1,
typename ...T>
319template<
typename T0,
typename T1>
325template<
typename T0,
typename T1,
typename ...T>
340template <
bool,
typename T =
void>
346template<
bool Test,
typename T =
void>
351template<
bool,
typename T =
void>
357template<
bool Test,
typename T =
void>
390template<
bool,
class TrueT,
class FalseT>
396template<
class TrueT,
class FalseT>
399template<
bool Test,
class TrueT,
class FalseT>
460template<
typename T,
typename ReferenceT>
468template<
typename T,
typename ReferenceT>
471template<
typename T,
typename ReferenceT>
483template<
typename AnyType,
template<
typename...>
class TemplateType>
486template<
typename... Args,
template<
typename...>
class TemplateType>
501 return reinterpret_cast<const char*
>(p) -
reinterpret_cast<const char*
>(q);
511template<
typename DstT =
void>
515 return reinterpret_cast<DstT*
>(
reinterpret_cast<char*
>(p) + offset);
523template<
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__)
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__)
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);
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")]]
char * sprint(char *dst)
Definition Util.h:275
static constexpr bool is_const_v
Definition Util.h:369
uint32_t countOn(uint64_t v)
Definition Util.h:656
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
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
int strcmp(const char *lhs, const char *rhs)
Compares two null-terminated byte strings lexicographically.
Definition Util.h:255
bool streq(const char *lhs, const char *rhs)
Test if two null-terminated byte strings are the same.
Definition Util.h:268
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
static constexpr bool is_pointer_v
Definition Util.h:385
static void * memzero(void *dst, size_t byteCount)
Zero initialization of memory.
Definition Util.h:297
char * strcpy(char *dst, const char *src)
Copy characters from src to dst.
Definition Util.h:166
typename remove_const< T >::type remove_const_t
Definition Util.h:417
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition Util.h:144
typename remove_pointer< T >::type remove_pointer_t
Definition Util.h:451
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
static constexpr bool is_floating_point_v
Definition Util.h:335
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
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
typename conditional< Test, TrueT, FalseT >::type conditional_t
Definition Util.h:400
char * sprint(char *dst, T var1, Types... var2)
prints a variable number of string and/or numbers to a destination string
Definition Util.h:286
typename match_const< T, ReferenceT >::type match_const_t
Definition Util.h:472
typename remove_const< T >::type remove_reference_t
Definition Util.h:434
T && declval() noexcept
Minimal implementation of std::declval, which converts any type T to.
typename disable_if< Test, T >::type disable_if_t
Definition Util.h:358
static constexpr bool is_same_v
Definition Util.h:326
typename enable_if< Test, T >::type enable_if_t
Definition Util.h:347
Definition GridHandle.h:27
uint32_t CountOn(uint64_t v)
Definition Util.h:687
__hostdev__ constexpr uint32_t strlen()
return the number of characters (including null termination) required to convert enum type to a strin...
Definition NanoVDB.h:204
uint32_t FindHighestOn(uint32_t v)
Definition Util.h:683
uint32_t FindLowestOn(uint32_t v)
Definition Util.h:679
#define NANOVDB_HOSTDEV_DISABLE_WARNING
Definition Util.h:94
#define __hostdev__
Definition Util.h:73
#define NANOVDB_ASSERT(x)
Definition Util.h:50
FalseT type
Definition Util.h:397
C++11 implementation of std::conditional.
Definition Util.h:391
TrueT type
Definition Util.h:391
T type
Definition Util.h:352
T type
Definition Util.h:344
C++11 implementation of std::enable_if.
Definition Util.h:341
static constexpr bool value
Definition Util.h:366
static constexpr bool value
Definition Util.h:363
C++11 implementation of std::is_floating_point.
Definition Util.h:332
static constexpr bool value
Definition Util.h:332
static constexpr bool value
Definition Util.h:382
Trait used to identify template parameter that are pointers.
Definition Util.h:376
static constexpr bool value
Definition Util.h:376
static constexpr bool value
Definition Util.h:320
static constexpr bool value
Definition Util.h:323
C++11 implementation of std::is_same.
Definition Util.h:315
static constexpr bool value
Definition Util.h:316
static const bool value
Definition Util.h:489
Metafunction used to determine if the first template parameter is a specialization of the class templ...
Definition Util.h:484
static const bool value
Definition Util.h:484
const typename remove_const< T >::type type
Definition Util.h:469
Trait used to transfer the const-ness of a reference type to another type.
Definition Util.h:461
typename remove_const< T >::type type
Definition Util.h:461
T type
Definition Util.h:414
Trait use to const from type. Default implementation is just a pass-through.
Definition Util.h:408
T type
Definition Util.h:408
T type
Definition Util.h:448
Trait use to remove pointer, i.e. "*", qualifier from a type. Default implementation is just a pass-t...
Definition Util.h:442
T type
Definition Util.h:442
T type
Definition Util.h:431
Trait use to remove reference, i.e. "&", qualifier from a type. Default implementation is just a pass...
Definition Util.h:425
T type
Definition Util.h:425