GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
bit_utils.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// File containing a basic set of bit manipulation utilities used within the
16// Draco library.
17
18#ifndef DRACO_CORE_BIT_UTILS_H_
19#define DRACO_CORE_BIT_UTILS_H_
20
21#include <inttypes.h>
22#include <stdint.h>
23
24#include <type_traits>
25
26#if defined(_MSC_VER)
27#include <intrin.h>
28#endif // defined(_MSC_VER)
29
30namespace draco {
31
32// Returns the number of '1' bits within the input 32 bit integer.
33inline int CountOneBits32(uint32_t n) {
34 n -= ((n >> 1) & 0x55555555);
35 n = ((n >> 2) & 0x33333333) + (n & 0x33333333);
36 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
37}
38
39inline uint32_t ReverseBits32(uint32_t n) {
40 n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
41 n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
42 n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
43 n = ((n >> 8) & 0x00FF00FF) | ((n & 0x00FF00FF) << 8);
44 return (n >> 16) | (n << 16);
45}
46
47// Copies the |nbits| from the src integer into the |dst| integer using the
48// provided bit offsets |dst_offset| and |src_offset|.
49inline void CopyBits32(uint32_t *dst, int dst_offset, uint32_t src,
50 int src_offset, int nbits) {
51 const uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - nbits)
52 << dst_offset;
53 *dst = (*dst & (~mask)) | (((src >> src_offset) << dst_offset) & mask);
54}
55
56// Returns the location of the most significant bit in the input integer |n|.
57// The functionality is not defined for |n == 0|.
58inline int MostSignificantBit(uint32_t n) {
59#if defined(__GNUC__)
60 return 31 ^ __builtin_clz(n);
61#elif defined(_MSC_VER)
62
63 unsigned long where;
64 _BitScanReverse(&where, n);
65 return (int)where;
66#else
67 // TODO(fgalligan): Optimize this code.
68 int msb = -1;
69 while (n != 0) {
70 msb++;
71 n >>= 1;
72 }
73 return msb;
74#endif
75}
76
77// Helper function that converts signed integer values into unsigned integer
78// symbols that can be encoded using an entropy encoder.
79void ConvertSignedIntsToSymbols(const int32_t *in, int in_values,
80 uint32_t *out);
81
82// Converts unsigned integer symbols encoded with an entropy encoder back to
83// signed values.
84void ConvertSymbolsToSignedInts(const uint32_t *in, int in_values,
85 int32_t *out);
86
87// Helper function that converts a single signed integer value into an unsigned
88// integer symbol that can be encoded using an entropy encoder.
89template <class IntTypeT>
90typename std::make_unsigned<IntTypeT>::type ConvertSignedIntToSymbol(
91 IntTypeT val) {
92 typedef typename std::make_unsigned<IntTypeT>::type UnsignedType;
93 static_assert(std::is_integral<IntTypeT>::value, "IntTypeT is not integral.");
94 // Early exit if val is positive.
95 if (val >= 0) {
96 return static_cast<UnsignedType>(val) << 1;
97 }
98 val = -(val + 1); // Map -1 to 0, -2 to -1, etc..
99 UnsignedType ret = static_cast<UnsignedType>(val);
100 ret <<= 1;
101 ret |= 1;
102 return ret;
103}
104
105// Converts a single unsigned integer symbol encoded with an entropy encoder
106// back to a signed value.
107template <class IntTypeT>
108typename std::make_signed<IntTypeT>::type ConvertSymbolToSignedInt(
109 IntTypeT val) {
110 static_assert(std::is_integral<IntTypeT>::value, "IntTypeT is not integral.");
111 typedef typename std::make_signed<IntTypeT>::type SignedType;
112 const bool is_positive = !static_cast<bool>(val & 1);
113 val >>= 1;
114 if (is_positive) {
115 return static_cast<SignedType>(val);
116 }
117 SignedType ret = static_cast<SignedType>(val);
118 ret = -ret - 1;
119 return ret;
120}
121
122} // namespace draco
123
124#endif // DRACO_CORE_BIT_UTILS_H_
ret($return)
Definition mesh_login_result.php:5
tag src
Definition meshpage.php:1786
Definition attribute_octahedron_transform.cc:21
int MostSignificantBit(uint32_t n)
Definition bit_utils.h:58
std::make_unsigned< IntTypeT >::type ConvertSignedIntToSymbol(IntTypeT val)
Definition bit_utils.h:90
void CopyBits32(uint32_t *dst, int dst_offset, uint32_t src, int src_offset, int nbits)
Definition bit_utils.h:49
void ConvertSignedIntsToSymbols(const int32_t *in, int in_values, uint32_t *out)
Definition bit_utils.cc:19
int CountOneBits32(uint32_t n)
Definition bit_utils.h:33
std::make_signed< IntTypeT >::type ConvertSymbolToSignedInt(IntTypeT val)
Definition bit_utils.h:108
void ConvertSymbolsToSignedInts(const uint32_t *in, int in_values, int32_t *out)
Definition bit_utils.cc:29
uint32_t ReverseBits32(uint32_t n)
Definition bit_utils.h:39