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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_BIT_UTILS_H
25 #define PXR_BASE_TF_BIT_UTILS_H
26 
27 /// \file tf/bitUtils.h
28 /// \ingroup group_tf_BasicMath
29 
30 #include "pxr/pxr.h"
31 
32 #include <cstddef>
33 #include <type_traits>
34 
36 
37 /// Compute the number of bits required to store the given number of values.
38 ///
39 /// Note that the computed result for a number smaller or equal to zero is
40 /// undefined. The input number can be any compile-time constant.
41 ///
42 /// \ingroup group_tf_BasicMath
43 /// \hideinitializer
44 #define TF_BITS_FOR_VALUES(n) \
45  Tf_NumBits<n-1>::type::value
46 
47 template <size_t N, size_t SUM=0, size_t BIT=sizeof(N)*8/2>
48 struct Tf_NumBits
49 {
50  // The result is computed by divide and conquer; for a given word N the
51  // bit at position BIT divides the word in an upper and a lower half.
52  // If the upper half contain any ones, then the result is SUM plus BIT
53  // plus the result for the upper half. If not, the result is SUM plus
54  // the result for the lower half.
55  typedef typename std::conditional<N >= (1ULL<<BIT),
56  Tf_NumBits<(N>>BIT), SUM+BIT, BIT/2>,
57  Tf_NumBits<N, SUM, BIT/2> >::type _func;
58  typedef typename _func::type type;
59 };
60 
61 template <size_t N, size_t SUM>
63 {
64  typedef std::integral_constant<size_t, SUM+1> type;
65 };
66 
67 /// Compute the number of bits required to store the given number of (signed)
68 /// enum values.
69 ///
70 /// \note This is intended to be used when storing enum values in a bitfield
71 /// without casting the enum type to an unsigned integer. (At least GCC
72 /// considers enums to be signed and hence wastes one bit when all enumerants
73 /// are non-negative).
74 ///
75 /// \ingroup group_tf_BasicMath
76 /// \hideinitializer
77 #define TF_BITS_FOR_ENUM_VALUES(n) \
78  (TF_BITS_FOR_VALUES(n) + 1)
79 
81 
82 #endif /* PXR_BASE_TF_BIT_UTILS_H */
_func::type type
Definition: bitUtils.h:58
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
std::integral_constant< size_t, SUM+1 > type
Definition: bitUtils.h:64
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GA_API const UT_StringHolder N
type
Definition: core.h:1059
Tf_NumBits< N, SUM, BIT/2 >::type _func
Definition: bitUtils.h:55