HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bitUtils.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_BIT_UTILS_H
8 #define PXR_BASE_TF_BIT_UTILS_H
9 
10 /// \file tf/bitUtils.h
11 /// \ingroup group_tf_BasicMath
12 
13 #include "pxr/pxr.h"
14 
15 #include <cstddef>
16 #include <type_traits>
17 
19 
20 /// Compute the number of bits required to store the given number of values.
21 ///
22 /// Note that the computed result for a number smaller or equal to zero is
23 /// undefined. The input number can be any compile-time constant.
24 ///
25 /// \ingroup group_tf_BasicMath
26 /// \hideinitializer
27 #define TF_BITS_FOR_VALUES(n) \
28  Tf_NumBits<n-1>::type::value
29 
30 template <size_t N, size_t SUM=0, size_t BIT=sizeof(N)*8/2>
31 struct Tf_NumBits
32 {
33  // The result is computed by divide and conquer; for a given word N the
34  // bit at position BIT divides the word in an upper and a lower half.
35  // If the upper half contain any ones, then the result is SUM plus BIT
36  // plus the result for the upper half. If not, the result is SUM plus
37  // the result for the lower half.
38  typedef typename std::conditional<N >= (1ULL<<BIT),
39  Tf_NumBits<(N>>BIT), SUM+BIT, BIT/2>,
40  Tf_NumBits<N, SUM, BIT/2> >::type _func;
41  typedef typename _func::type type;
42 };
43 
44 template <size_t N, size_t SUM>
46 {
47  typedef std::integral_constant<size_t, SUM+1> type;
48 };
49 
50 /// Compute the number of bits required to store the given number of (signed)
51 /// enum values.
52 ///
53 /// \note This is intended to be used when storing enum values in a bitfield
54 /// without casting the enum type to an unsigned integer. (At least GCC
55 /// considers enums to be signed and hence wastes one bit when all enumerants
56 /// are non-negative).
57 ///
58 /// \ingroup group_tf_BasicMath
59 /// \hideinitializer
60 #define TF_BITS_FOR_ENUM_VALUES(n) \
61  (TF_BITS_FOR_VALUES(n) + 1)
62 
64 
65 #endif /* PXR_BASE_TF_BIT_UTILS_H */
type
Definition: core.h:556
_func::type type
Definition: bitUtils.h:41
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::integral_constant< size_t, SUM+1 > type
Definition: bitUtils.h:47
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GA_API const UT_StringHolder N
Tf_NumBits< N, SUM, BIT/2 >::type _func
Definition: bitUtils.h:38