BitOps.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file BitOps.h
  13. @brief Provides bit operation utilities on bitfields. */
  14. #include <cassert>
  15. #include "Types.h"
  16. namespace kNet
  17. {
  18. /// Returns the u32 representation of the given string that consists only of characters '0' and '1'.
  19. u32 BinaryStringToValue(const char *str);
  20. /// Returns the number of bits that are set (ones) in the given value.
  21. inline int CountBitsSet(u32 value)
  22. {
  23. int bits = 0;
  24. while(value)
  25. {
  26. value &= value - 1;
  27. ++bits;
  28. }
  29. return bits;
  30. }
  31. // Clears the LSB bit and returns the zero-based index of where the bit was set.
  32. inline int ExtractLSB(unsigned long *value)
  33. {
  34. for(int i = 0; i < 32; ++i)
  35. if ((*value & (1 << i)) != 0)
  36. {
  37. *value &= ~(1 << i);
  38. return i;
  39. }
  40. return -1;
  41. }
  42. /** @brief Template-evaluated enum that gives a mask with the Bits LSB bits set.
  43. For example,
  44. LSBT<0>::val = 0,
  45. LSBT<1>::val = 1,
  46. LSBT<2>::val = 11,
  47. LSBT<24>::val = 11111111 11111111 11111111.
  48. LSBT<33> and greater are undefined. */
  49. template<int Bits>
  50. class LSBT
  51. {
  52. public:
  53. enum { val = (1 << Bits) - 1 };
  54. };
  55. /// @cond FULL
  56. /** Specialization for LSBT<32> to avoid the undefined result of 1 << 32. */
  57. template<>
  58. class LSBT<32>
  59. {
  60. public:
  61. enum { val = 0xFFFFFFFF };
  62. };
  63. template<>
  64. class LSBT<31>
  65. {
  66. public:
  67. enum { val = 0x7FFFFFFF };
  68. };
  69. /// @endcond
  70. /** @return A mask with the given amount of LSB bits set. This is the runtime-equivalent of LSBT.
  71. LSB(0)==0, LSB(1)==1, LSB(8)=11111111, etc.
  72. LSB(32) and above are undefined. */
  73. inline u32 LSB(u32 bits)
  74. {
  75. assert(bits <= 32);
  76. if (bits >= 32)
  77. return 0xFFFFFFFF;
  78. return (1 << bits) - 1;
  79. }
  80. /** @brief A template-computed enum to create a mask of given amount of bits at given position of a u32 variable.
  81. For example,
  82. BitMaskT<0,0>::val == 0,
  83. BitMaskT<0,8>::val == 11111111,
  84. BitMaskT<4, 12>::val == 11111111 11110000, etc.
  85. If Pos+Bits > 32, the result is undefined. */
  86. template<int Pos, int Bits>
  87. class BitMaskT
  88. {
  89. public:
  90. // enum { val = (Pow<2, Bits>::val - 1) << Pos }; // Alternate way to calculate this value.
  91. enum { val = LSBT<Pos+Bits>::val & ~LSBT<Pos>::val }; // But this is nicer.
  92. };
  93. /** @return A mask with the given number of bits set at the given position of a u32 variable. This is
  94. the runtime-equivalent of BitMaskT.
  95. For example, BitMask(4,4)==11110000. */
  96. inline u32 BitMask(u32 pos, u32 bits)
  97. {
  98. return LSB(pos + bits) & ~LSB(pos);
  99. }
  100. /** @return The given 4 bit fields packed into a single larger bitfield.
  101. Aggressively templatized version. */
  102. template<typename ResultType, typename InputType,
  103. int APos, int ABits, int RPos, int RBits,
  104. int GPos, int GBits, int BPos, int BBits>
  105. ResultType PackBits(InputType a, InputType r, InputType g, InputType b)
  106. {
  107. return (ResultType)(
  108. (ResultType)((a << APos) & BitMaskT<APos, ABits>::val) |
  109. (ResultType)((r << RPos) & BitMaskT<RPos, RBits>::val) |
  110. (ResultType)((g << GPos) & BitMaskT<GPos, GBits>::val) |
  111. (ResultType)((b << BPos) & BitMaskT<BPos, BBits>::val));
  112. }
  113. /** @return The given 4 bit fields packed into a single larger bitfield. */
  114. template<typename ResultType, typename InputType>
  115. ResultType PackBits(int APos, int ABits, int RPos, int RBits,
  116. int GPos, int GBits, int BPos, int BBits,
  117. InputType a, InputType r, InputType g, InputType b)
  118. {
  119. return (ResultType)(
  120. (ResultType)((a << APos) & BitMask(APos, ABits)) |
  121. (ResultType)((r << RPos) & BitMask(RPos, RBits)) |
  122. (ResultType)((g << GPos) & BitMask(GPos, GBits)) |
  123. (ResultType)((b << BPos) & BitMask(BPos, BBits)));
  124. }
  125. /** Extracts the given adjacent bits from a larger bitfield. Aggressively templatized version.
  126. For example, ExtractBits<u8, u8, 4, 2> gets the bits 00110000 from a byte,
  127. shifts them right 4 places and returns the two bits. */
  128. template<typename ResultType, typename InputType, int Pos, int Bits>
  129. void ExtractBits(ResultType &out, const InputType &in)
  130. {
  131. out = (ResultType)(in >> Pos & BitMaskT<0, Bits>::val);
  132. }
  133. /** Extracts the given adjacent bits from a larger bitfield.
  134. For example, ExtractBits<u8, u8>(3, 4) gets the bits 01111000 from a byte,
  135. shifts them right 3 places and returns the four bits. */
  136. template<typename ResultType, typename InputType>
  137. void ExtractBits(int pos, int bits, ResultType &out, const InputType &in)
  138. {
  139. out = (ResultType)(in >> pos & BitMask(0, bits));
  140. }
  141. } // ~kNet