Nebula
Loading...
Searching...
No Matches
bit.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/types.h"
13
14namespace Util
15{
16
17//------------------------------------------------------------------------------
20constexpr uint64
22{
23 return mask | (1ULL << bit);
24}
25
26//------------------------------------------------------------------------------
29constexpr bool
31{
32 return mask & (1ULL << bit);
33}
34
35//------------------------------------------------------------------------------
38constexpr uint32
40{
41 return mask | (1 << bit);
42}
43
44//------------------------------------------------------------------------------
47constexpr bool
49{
50 return mask & (1 << bit);
51}
52
53//------------------------------------------------------------------------------
56constexpr uint16
58{
59 return mask | (1 << bit);
60}
61
62//------------------------------------------------------------------------------
65constexpr bool
67{
68 return mask & (1 << bit);
69}
70
71//------------------------------------------------------------------------------
74constexpr int64
75SetBit(int64 mask, uint8 bit)
76{
77 return mask | (1LL << bit);
78}
79
80//------------------------------------------------------------------------------
83constexpr bool
84HasBit(int64 mask, uint8 bit)
85{
86 return mask & (1LL << bit);
87}
88
89//------------------------------------------------------------------------------
92constexpr int32
93SetBit(int32 mask, uint8 bit)
94{
95 return mask | (1 << bit);
96}
97
98//------------------------------------------------------------------------------
101constexpr bool
103{
104 return mask & (1 << bit);
105}
106
107//------------------------------------------------------------------------------
110constexpr int16
112{
113 return mask | (1 << bit);
114}
115
116//------------------------------------------------------------------------------
119constexpr bool
121{
122 return mask & (1 << bit);
123}
124
125//------------------------------------------------------------------------------
128constexpr uint32
130{
131 i = i - ((i >> 1) & 0x55555555);
132 i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
133 return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
134}
135
136//------------------------------------------------------------------------------
140template<typename T>
141inline void HashCombine(uint32_t& s, const T& v)
142{
143 std::hash<T> h;
144 s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
145}
146
147//------------------------------------------------------------------------------
150inline uint
152{
153 return _mm_popcnt_u32(value);
154}
155
156//------------------------------------------------------------------------------
159inline uint64
161{
162 return _mm_popcnt_u64(value);
163}
164
165//------------------------------------------------------------------------------
168inline uint
170{
171#if __WIN32__
172 DWORD count = 0;
173 _BitScanForward(&count, value);
174#else
175 int count = __builtin_ctz(value);
176#endif
177 return count;
178}
179
180//------------------------------------------------------------------------------
183inline uint
185{
186#if __WIN32__
187 DWORD count = 0;
188 _BitScanForward64(&count, value);
189#else
190 int count = __builtin_ctz(value);
191#endif
192 return count;
193}
194
195//------------------------------------------------------------------------------
198inline uint
200{
201#if __WIN32__
202 DWORD count = 0;
203 _BitScanReverse(&count, value);
204#else
205 int count = __builtin_clz(value);
206#endif
207 return count;
208}
209
210//------------------------------------------------------------------------------
213inline uint
215{
216#if __WIN32__
217 DWORD count = 0;
218 _BitScanReverse64(&count, value);
219#else
220 int count = __builtin_clz(value);
221#endif
222 return count;
223}
224
225//------------------------------------------------------------------------------
228inline uint
229Lsb(uint value, byte bit)
230{
231 uint mask = value & ~((1 << bit) - 1);
232#if __WIN32__
233 DWORD count = 0;
234 _BitScanForward(&count, mask);
235#else
236 int count = __builtin_ctz(mask);
237#endif
238 return mask ? count : 0xFFFFFFFF;
239}
240
241//------------------------------------------------------------------------------
244inline uint
245BitmaskConvert(uint mask, const uint* table, const uint numEntries = 0xFFFFFFFF)
246{
247 uint ret = 0x0;
248 uint usageFlags = mask;
249 uint flagIndex = 0;
250 while (usageFlags != 0x0 && flagIndex < numEntries)
251 {
252 if (usageFlags & (1 << flagIndex))
253 {
254 ret |= table[flagIndex];
255 }
256 usageFlags &= ~(1 << flagIndex);
257 flagIndex++;
258 }
259 return ret;
260}
261
262} // namespace Util
263
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
uint LastOne(uint value)
Definition bit.h:199
constexpr uint64 SetBit(uint64 mask, uint8 bit)
Definition bit.h:21
constexpr bool HasBit(uint64 mask, uint8 bit)
Definition bit.h:30
uint BitmaskConvert(uint mask, const uint *table, const uint numEntries=0xFFFFFFFF)
Definition bit.h:245
uint Lsb(uint value, byte bit)
Definition bit.h:229
uint FirstOne(uint value)
Definition bit.h:169
uint PopCnt(uint value)
Definition bit.h:151
constexpr uint32 CountBits(uint32 i)
Definition bit.h:129
void HashCombine(uint32_t &s, const T &v)
Combine hashes.
Definition bit.h:141
uint8_t uint8
Definition types.h:42
int64_t int64
Definition types.h:37
unsigned int uint
Definition types.h:31
int16_t int16
Definition types.h:41
int32_t int32
Definition types.h:39
uint64_t uint64
Definition types.h:36
uint16_t uint16
Definition types.h:40
uint32_t uint32
Definition types.h:38