All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
12
13#include "pxr/pxr.h"
14
15#include <cstddef>
16#include <type_traits>
17
18PXR_NAMESPACE_OPEN_SCOPE
19
27#define TF_BITS_FOR_VALUES(n) \
28 Tf_NumBits<n-1>::type::value
29
30template <size_t N, size_t SUM=0, size_t BIT=sizeof(N)*8/2>
31struct 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
44template <size_t N, size_t SUM>
45struct Tf_NumBits<N, SUM, 0>
46{
47 typedef std::integral_constant<size_t, SUM+1> type;
48};
49
60#define TF_BITS_FOR_ENUM_VALUES(n) \
61 (TF_BITS_FOR_VALUES(n) + 1)
62
63PXR_NAMESPACE_CLOSE_SCOPE
64
65#endif /* PXR_BASE_TF_BIT_UTILS_H */