gwnavruntime/containers/bitfieldutils.h Source File

bitfieldutils.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
11 
12 namespace Kaim
13 {
14 
15 class BitFieldUtils
16 {
17 public:
18  static KY_INLINE KyUInt32 GetWordIndex(KyUInt32 bitIndex) { return bitIndex >> 5; } // / 32
19  static KY_INLINE KyUInt32 GetBitIndexInWord(KyUInt32 bitIndex) { return bitIndex & 0x1F; } // % 32
20  static KY_INLINE KyUInt32 GetWordsCount(KyUInt32 bitCount) { return (bitCount + 31) >> 5; } // / 32
21  static KY_INLINE KyUInt32 GetWordMaskFromBitIndex(KyUInt32 bitIndex) { return GetWordMask(GetBitIndexInWord(bitIndex)); }
22 
23  static KY_INLINE void SetBit(KyUInt32& word, KyUInt32 bitIndex) { word |= GetWordMaskFromBitIndex(bitIndex); }
24  static KY_INLINE void UnsetBit(KyUInt32& word, KyUInt32 bitIndex) { word &= ~GetWordMaskFromBitIndex(bitIndex); }
25  static KY_INLINE bool IsBitSet(KyUInt32 word, KyUInt32 bitIndex) { return (word & GetWordMaskFromBitIndex(bitIndex)) != 0; }
26 
27  static KY_INLINE void SetBit(KyUInt32* words, KyUInt32 bitIndex) { SetBit(words[GetWordIndex(bitIndex)], bitIndex); }
28  static KY_INLINE void UnsetBit(KyUInt32* words, KyUInt32 bitIndex) { UnsetBit(words[GetWordIndex(bitIndex)], bitIndex); }
29  static KY_INLINE bool IsBitSet(const KyUInt32* words, KyUInt32 bitIndex) { return IsBitSet(words[GetWordIndex(bitIndex)], bitIndex); }
30 
31  static KY_INLINE KyUInt32 GetWordMask(KyUInt32 bitIndexInWord)
32  {
33  // Note that 1 << non_constant may generate microcoded instructions and impact performance
34  // as on powerPC (see http://cellperformance.beyond3d.com/articles/2006/04/avoiding-microcoded-instructions-on-the-ppu.html)
35  // That's why we preprocess the values here. Hmm we need some profiling to confirm that on different platforms.
36  static const KyUInt32 s_maskToIsolateBit[32] =
37  {
38  1, 1U << 1, 1U << 2, 1U << 3, 1U << 4, 1U << 5, 1U << 6, 1U << 7,
39  1U << 8, 1U << 9, 1U << 10, 1U << 11, 1U << 12, 1U << 13, 1U << 14, 1U << 15,
40  1U << 16, 1U << 17, 1U << 18, 1U << 19, 1U << 20, 1U << 21, 1U << 22, 1U << 23,
41  1U << 24, 1U << 25, 1U << 26, 1U << 27, 1U << 28, 1U << 29, 1U << 30, 1U << 31
42  };
43 
44  return s_maskToIsolateBit[bitIndexInWord];
45  }
46 };
47 
48 
49 }
50 
51 
52 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17