gwnavruntime/containers/bitfieldutils.h Source File

bitfieldutils.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 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 #ifndef Navigation_BitFieldUtils_H
9 #define Navigation_BitFieldUtils_H
10 
12 
13 namespace Kaim
14 {
15 
16 class BitFieldUtils
17 {
18 public:
19  static KY_INLINE KyUInt32 GetWordIndex(KyUInt32 bitIndex) { return bitIndex >> 5; } // / 32
20  static KY_INLINE KyUInt32 GetBitIndexInWord(KyUInt32 bitIndex) { return bitIndex & 0x1F; } // % 32
21  static KY_INLINE KyUInt32 GetWordsCount(KyUInt32 bitCount) { return (bitCount + 31) >> 5; } // / 32
22  static KY_INLINE KyUInt32 GetWordMaskFromBitIndex(KyUInt32 bitIndex) { return GetWordMask(GetBitIndexInWord(bitIndex)); }
23 
24  static KY_INLINE void SetBit(KyUInt32& word, KyUInt32 bitIndex) { word |= GetWordMaskFromBitIndex(bitIndex); }
25  static KY_INLINE void UnsetBit(KyUInt32& word, KyUInt32 bitIndex) { word &= ~GetWordMaskFromBitIndex(bitIndex); }
26  static KY_INLINE bool IsBitSet(KyUInt32 word, KyUInt32 bitIndex) { return (word & GetWordMaskFromBitIndex(bitIndex)) != 0; }
27 
28  static KY_INLINE void SetBit(KyUInt32* words, KyUInt32 bitIndex) { SetBit(words[GetWordIndex(bitIndex)], bitIndex); }
29  static KY_INLINE void UnsetBit(KyUInt32* words, KyUInt32 bitIndex) { UnsetBit(words[GetWordIndex(bitIndex)], bitIndex); }
30  static KY_INLINE bool IsBitSet(const KyUInt32* words, KyUInt32 bitIndex) { return IsBitSet(words[GetWordIndex(bitIndex)], bitIndex); }
31 
32  static KY_INLINE KyUInt32 GetWordMask(KyUInt32 bitIndexInWord)
33  {
34  // note that "1U << bitIndexInWord" may generate microcoded instructions and impacts performances
35  // as on powerPC (see http://cellperformance.beyond3d.com/articles/2006/04/avoiding-microcoded-instructions-on-the-ppu.html)
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 #endif // Navigation_BitFieldUtils_H
53 
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36