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)
37#if __cplusplus >= 202002L
47#ifdef NANOVDB_USE_IOSTREAMS
53#define NANOVDB_ASSERT(x) assert(x)
55#define NANOVDB_ASSERT(x)
58#if defined(NANOVDB_USE_INTRINSICS) && defined(_MSC_VER)
60#pragma intrinsic(_BitScanReverse)
61#pragma intrinsic(_BitScanForward)
62#pragma intrinsic(_BitScanReverse64)
63#pragma intrinsic(_BitScanForward64)
68#if defined(__CUDACC__) || defined(__HIP__)
71#define __hostdev__ __host__ __device__
94#define NANOVDB_RESTRICT __restrict
96#define NANOVDB_RESTRICT __restrict__
101#if defined(_MSC_VER) && defined(__CUDACC__)
102#define NANOVDB_HOSTDEV_DISABLE_WARNING __pragma("hd_warning_disable")
103#elif defined(__GNUC__) && defined(__CUDACC__)
104#define NANOVDB_HOSTDEV_DISABLE_WARNING _Pragma("hd_warning_disable")
106#define NANOVDB_HOSTDEV_DISABLE_WARNING
125#define NANOVDB_PRAGMA(X) _Pragma(#X)
126#define NANOVDB_DEPRECATED_HEADER(MSG) NANOVDB_PRAGMA(GCC warning MSG)
127#elif defined(_MSC_VER)
128#define NANOVDB_STRINGIZE_(MSG) #MSG
129#define NANOVDB_STRINGIZE(MSG) NANOVDB_STRINGIZE_(MSG)
130#define NANOVDB_DEPRECATED_HEADER(MSG) \
131 __pragma(message(__FILE__ "(" NANOVDB_STRINGIZE(__LINE__) ") : Warning: " MSG))
138#if defined(__NVCC__) && defined(_MSC_VER)
139#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN __pragma("nv_diag_suppress 1444") __pragma("nv_diag_suppress 20199")
140#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_END __pragma("nv_diag_default 1444") __pragma("nv_diag_default 20199")
141#elif defined(__NVCC__)
142#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN _Pragma("nv_diagnostic push") _Pragma("nv_diag_suppress 1444") _Pragma("nv_diag_suppress 20199")
143#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_END _Pragma("nv_diagnostic pop")
145#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN
146#define NANOVDB_SUPPRESS_DEPRECATION_NVCC_END
148#if defined(__GNUC__) || defined(__clang__)
149#define NANOVDB_SUPPRESS_DEPRECATION_BEGIN \
150 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN
151#define NANOVDB_SUPPRESS_DEPRECATION_END NANOVDB_SUPPRESS_DEPRECATION_NVCC_END _Pragma("GCC diagnostic pop")
152#elif defined(_MSC_VER)
153#define NANOVDB_SUPPRESS_DEPRECATION_BEGIN __pragma(warning(push)) __pragma(warning(disable : 4996)) NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN
154#define NANOVDB_SUPPRESS_DEPRECATION_END NANOVDB_SUPPRESS_DEPRECATION_NVCC_END __pragma(warning(pop))
156#define NANOVDB_SUPPRESS_DEPRECATION_BEGIN NANOVDB_SUPPRESS_DEPRECATION_NVCC_BEGIN
157#define NANOVDB_SUPPRESS_DEPRECATION_END NANOVDB_SUPPRESS_DEPRECATION_NVCC_END
163#define NANOVDB_OFFSETOF(CLASS, MEMBER) ((int)__builtin_offsetof(CLASS, MEMBER))
165#define NANOVDB_OFFSETOF(CLASS, MEMBER) ((int)offsetof(CLASS, MEMBER))
212 for (
char *p = dst; (*p++ = *src) !=
'\0'; ++src);
232 for (; i < max && src[i] !=
'\0'; ++i) dst[i] = src[i];
233 for (; i < max; ++i) dst[i] =
'\0';
247 if (num == 0) dst[len++] =
'0';
248 for (
int abs = num < 0 && bas == 10 ? -num : num; abs; abs /= bas) {
249 const int rem = abs % bas;
250 dst[len++] =
static_cast<char>(rem > 9 ? rem - 10 +
'a' : rem +
'0');
252 if (num < 0) dst[len++] =
'-';
253 for (
char *a = dst, *b = a + len - 1; a < b; ++a, --b) {
272 while (*p !=
'\0') ++p;
286 while (*p !=
'\0') ++p;
300 while(*lhs !=
'\0' && (*lhs == *rhs)){
304 return *(
const unsigned char*)lhs - *(
const unsigned char*)rhs;
313 return strcmp(lhs, rhs) == 0;
320template <
typename T,
typename... Types>
328template <
typename T,
typename... Types>
343 const size_t wordCount = byteCount >> 3;
344 if (wordCount << 3 == byteCount) {
345 for (
auto *d = (uint64_t*)dst, *e = d + wordCount; d != e; ++d) *d = 0ULL;
347 for (
auto *d = (
char*)dst, *e = d + byteCount; d != e; ++d) *d =
'\0';
356template<
typename T0,
typename T1,
typename ...T>
362template<
typename T0,
typename T1>
368template<
typename T0,
typename T1,
typename ...T>
383template <
bool,
typename T =
void>
389template<
bool Test,
typename T =
void>
394template<
bool,
typename T =
void>
400template<
bool Test,
typename T =
void>
433template<
bool,
class TrueT,
class FalseT>
439template<
class TrueT,
class FalseT>
442template<
bool Test,
class TrueT,
class FalseT>
503template<
typename T,
typename ReferenceT>
511template<
typename T,
typename ReferenceT>
514template<
typename T,
typename ReferenceT>
526template<
typename AnyType,
template<
typename...>
class TemplateType>
529template<
typename... Args,
template<
typename...>
class TemplateType>
544 return reinterpret_cast<const char*
>(p) -
reinterpret_cast<const char*
>(q);
554template<
typename DstT =
void>
558 return reinterpret_cast<DstT*
>(
reinterpret_cast<char*
>(p) + offset);
566template<
typename DstT =
void>
570 return reinterpret_cast<const DstT*
>(
reinterpret_cast<const char*
>(p) + offset);
582#if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS)
584#elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS)
586 _BitScanForward(&index, v);
587 return static_cast<uint32_t
>(index);
588#elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS)
589 return static_cast<uint32_t
>(__builtin_ctzl(v));
592 static const unsigned char DeBruijn[32] = {
593 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};
595#if defined(_MSC_VER) && !defined(__NVCC__)
597#pragma warning(disable : 4146)
599 return DeBruijn[uint32_t((v & -v) * 0x077CB531U) >> 27];
600#if defined(_MSC_VER) && !defined(__NVCC__)
614#if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS)
615 return __ffsll(
static_cast<unsigned long long int>(v)) - 1;
616#elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS)
618 _BitScanForward64(&index, v);
619 return static_cast<uint32_t
>(index);
620#elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS)
621 return static_cast<uint32_t
>(__builtin_ctzll(v));
624 static const unsigned char DeBruijn[64] = {
625 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
626 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
627 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
628 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12,
631#if defined(_MSC_VER) && !defined(__NVCC__)
633#pragma warning(disable : 4146)
635 return DeBruijn[uint64_t((v & -v) * UINT64_C(0x022FDD63CC95386D)) >> 58];
636#if defined(_MSC_VER) && !defined(__NVCC__)
652#if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS)
653 return sizeof(uint32_t) * 8 - 1 - __clz(v);
654#elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS)
656 _BitScanReverse(&index, v);
657 return static_cast<uint32_t
>(index);
658#elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS)
659 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzl(v);
662 static const unsigned char DeBruijn[32] = {
663 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
664 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31};
670 return DeBruijn[uint32_t(v * 0x07C4ACDDU) >> 27];
681#if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS)
682 return sizeof(
unsigned long) * 8 - 1 - __clzll(
static_cast<unsigned long long int>(v));
683#elif defined(_MSC_VER) && defined(NANOVDB_USE_INTRINSICS)
685 _BitScanReverse64(&index, v);
686 return static_cast<uint32_t
>(index);
687#elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS)
688 return sizeof(
unsigned long) * 8 - 1 - __builtin_clzll(v);
690 const uint32_t* p =
reinterpret_cast<const uint32_t*
>(&v);
701#if (defined(__CUDA_ARCH__) || defined(__HIP__)) && defined(NANOVDB_USE_INTRINSICS)
705#elif defined(_MSC_VER) && defined(_M_X64) && (_MSC_VER >= 1928) && defined(NANOVDB_USE_INTRINSICS)
707 return uint32_t(__popcnt64(v));
708#elif (defined(__GNUC__) || defined(__clang__)) && defined(NANOVDB_USE_INTRINSICS)
710 return __builtin_popcountll(v);
713 v = v - ((v >> 1) & uint64_t(0x5555555555555555));
714 v = (v & uint64_t(0x3333333333333333)) + ((v >> 2) & uint64_t(0x3333333333333333));
715 return (((v + (v >> 4)) & uint64_t(0xF0F0F0F0F0F0F0F)) * uint64_t(0x101010101010101)) >> 56;
726#if defined(__CUDA_ARCH__) || defined(__HIP__)
727 return static_cast<uint64_t
>(
::atomicOr(
reinterpret_cast<unsigned long long int*
>(target),
728 static_cast<unsigned long long int>(mask)));
729#elif __cplusplus >= 202002L
730 return std::atomic_ref<uint64_t>(*target).fetch_or(mask, std::memory_order_relaxed);
731#elif defined(__GNUC__) || defined(__clang__)
732 return __atomic_fetch_or(target, mask, __ATOMIC_RELAXED);
733#elif defined(_MSC_VER)
734 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"Unexpected long long size");
735 return static_cast<uint64_t
>(_InterlockedOr64(
736 reinterpret_cast<volatile long long*
>(target),
737 static_cast<long long>(mask)));
739#error "util::atomicOr: no implementation for this compiler"
750#if defined(__CUDA_ARCH__) || defined(__HIP__)
751 return static_cast<uint64_t
>(
::atomicAnd(
reinterpret_cast<unsigned long long int*
>(target),
752 static_cast<unsigned long long int>(mask)));
753#elif __cplusplus >= 202002L
754 return std::atomic_ref<uint64_t>(*target).fetch_and(mask, std::memory_order_relaxed);
755#elif defined(__GNUC__) || defined(__clang__)
756 return __atomic_fetch_and(target, mask, __ATOMIC_RELAXED);
757#elif defined(_MSC_VER)
758 static_assert(
sizeof(
long long) ==
sizeof(uint64_t),
"Unexpected long long size");
759 return static_cast<uint64_t
>(_InterlockedAnd64(
760 reinterpret_cast<volatile long long*
>(target),
761 static_cast<long long>(mask)));
763#error "util::atomicAnd: no implementation for this compiler"
#define __hostdev__
Definition SampleFromVoxels.h:29
char * sprint(char *dst)
Definition Util.h:318
static constexpr bool is_const_v
Definition Util.h:412
uint32_t countOn(uint64_t v)
Definition Util.h:699
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:649
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:228
int strcmp(const char *lhs, const char *rhs)
Compares two null-terminated byte strings lexicographically.
Definition Util.h:298
bool streq(const char *lhs, const char *rhs)
Test if two null-terminated byte strings are the same.
Definition Util.h:311
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:555
static constexpr bool is_pointer_v
Definition Util.h:428
static void * memzero(void *dst, size_t byteCount)
Zero initialization of memory.
Definition Util.h:340
char * strcpy(char *dst, const char *src)
Copy characters from src to dst.
Definition Util.h:209
typename remove_const< T >::type remove_const_t
Definition Util.h:460
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition Util.h:187
typename remove_pointer< T >::type remove_pointer_t
Definition Util.h:494
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:579
static constexpr bool is_floating_point_v
Definition Util.h:378
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:268
static int64_t PtrDiff(const void *p, const void *q)
Compute the distance, in bytes, between two pointers, dist = p - q.
Definition Util.h:541
typename conditional< Test, TrueT, FalseT >::type conditional_t
Definition Util.h:443
uint64_t atomicOr(uint64_t *target, uint64_t mask)
Atomically ORs mask into the 64-bit word at target (relaxed ordering). Returns the old value....
Definition Util.h:724
char * sprint(char *dst, T var1, Types... var2)
prints a variable number of string and/or numbers to a destination string
Definition Util.h:329
uint64_t atomicAnd(uint64_t *target, uint64_t mask)
Atomically ANDs mask into the 64-bit word at target (relaxed ordering). Returns the old value....
Definition Util.h:748
typename match_const< T, ReferenceT >::type match_const_t
Definition Util.h:515
typename remove_const< T >::type remove_reference_t
Definition Util.h:477
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:401
static constexpr bool is_same_v
Definition Util.h:369
typename enable_if< Test, T >::type enable_if_t
Definition Util.h:390
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:34
constexpr uint32_t strlen()
return the number of characters (including null termination) required to convert enum type to a strin...
Definition NanoVDB.h:213
#define NANOVDB_HOSTDEV_DISABLE_WARNING
Definition Util.h:106
#define NANOVDB_ASSERT(x)
Definition Util.h:53
FalseT type
Definition Util.h:440
C++11 implementation of std::conditional.
Definition Util.h:434
TrueT type
Definition Util.h:434
T type
Definition Util.h:395
T type
Definition Util.h:387
C++11 implementation of std::enable_if.
Definition Util.h:384
static constexpr bool value
Definition Util.h:409
static constexpr bool value
Definition Util.h:406
C++11 implementation of std::is_floating_point.
Definition Util.h:375
static constexpr bool value
Definition Util.h:375
static constexpr bool value
Definition Util.h:425
Trait used to identify template parameter that are pointers.
Definition Util.h:419
static constexpr bool value
Definition Util.h:419
static constexpr bool value
Definition Util.h:363
static constexpr bool value
Definition Util.h:366
C++11 implementation of std::is_same.
Definition Util.h:358
static constexpr bool value
Definition Util.h:359
static const bool value
Definition Util.h:532
Metafunction used to determine if the first template parameter is a specialization of the class templ...
Definition Util.h:527
static const bool value
Definition Util.h:527
const typename remove_const< T >::type type
Definition Util.h:512
Trait used to transfer the const-ness of a reference type to another type.
Definition Util.h:504
typename remove_const< T >::type type
Definition Util.h:504
T type
Definition Util.h:457
Trait use to const from type. Default implementation is just a pass-through.
Definition Util.h:451
T type
Definition Util.h:451
T type
Definition Util.h:491
Trait use to remove pointer, i.e. "*", qualifier from a type. Default implementation is just a pass-t...
Definition Util.h:485
T type
Definition Util.h:485
T type
Definition Util.h:474
Trait use to remove reference, i.e. "&", qualifier from a type. Default implementation is just a pass...
Definition Util.h:468
T type
Definition Util.h:468