bit_field.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Licensed under GPLv2
  2. // Refer to the license.txt file included.
  3. // Copyright 2014 Tony Wasserka
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above copyright
  12. // notice, this list of conditions and the following disclaimer in the
  13. // documentation and/or other materials provided with the distribution.
  14. // * Neither the name of the owner nor the names of its contributors may
  15. // be used to endorse or promote products derived from this software
  16. // without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #pragma once
  30. #include <limits>
  31. #include <type_traits>
  32. #include "common/common.h"
  33. /*
  34. * Abstract bitfield class
  35. *
  36. * Allows endianness-independent access to individual bitfields within some raw
  37. * integer value. The assembly generated by this class is identical to the
  38. * usage of raw bitfields, so it's a perfectly fine replacement.
  39. *
  40. * For BitField<X,Y,Z>, X is the distance of the bitfield to the LSB of the
  41. * raw value, Y is the length in bits of the bitfield. Z is an integer type
  42. * which determines the sign of the bitfield. Z must have the same size as the
  43. * raw integer.
  44. *
  45. *
  46. * General usage:
  47. *
  48. * Create a new union with the raw integer value as a member.
  49. * Then for each bitfield you want to expose, add a BitField member
  50. * in the union. The template parameters are the bit offset and the number
  51. * of desired bits.
  52. *
  53. * Changes in the bitfield members will then get reflected in the raw integer
  54. * value and vice-versa.
  55. *
  56. *
  57. * Sample usage:
  58. *
  59. * union SomeRegister
  60. * {
  61. * u32 hex;
  62. *
  63. * BitField<0,7,u32> first_seven_bits; // unsigned
  64. * BitField<7,8,32> next_eight_bits; // unsigned
  65. * BitField<3,15,s32> some_signed_fields; // signed
  66. * };
  67. *
  68. * This is equivalent to the little-endian specific code:
  69. *
  70. * union SomeRegister
  71. * {
  72. * u32 hex;
  73. *
  74. * struct
  75. * {
  76. * u32 first_seven_bits : 7;
  77. * u32 next_eight_bits : 8;
  78. * };
  79. * struct
  80. * {
  81. * u32 : 3; // padding
  82. * s32 some_signed_fields : 15;
  83. * };
  84. * };
  85. *
  86. *
  87. * Caveats:
  88. *
  89. * 1)
  90. * BitField provides automatic casting from and to the storage type where
  91. * appropriate. However, when using non-typesafe functions like printf, an
  92. * explicit cast must be performed on the BitField object to make sure it gets
  93. * passed correctly, e.g.:
  94. * printf("Value: %d", (s32)some_register.some_signed_fields);
  95. *
  96. * 2)
  97. * Not really a caveat, but potentially irritating: This class is used in some
  98. * packed structures that do not guarantee proper alignment. Therefore we have
  99. * to use #pragma pack here not to pack the members of the class, but instead
  100. * to break GCC's assumption that the members of the class are aligned on
  101. * sizeof(StorageType).
  102. * TODO(neobrain): Confirm that this is a proper fix and not just masking
  103. * symptoms.
  104. */
  105. #pragma pack(1)
  106. template<std::size_t position, std::size_t bits, typename T>
  107. struct BitField
  108. {
  109. private:
  110. // This constructor might be considered ambiguous:
  111. // Would it initialize the storage or just the bitfield?
  112. // Hence, delete it. Use the assignment operator to set bitfield values!
  113. BitField(T val) = delete;
  114. public:
  115. // Force default constructor to be created
  116. // so that we can use this within unions
  117. BitField() = default;
  118. __forceinline BitField& operator=(T val)
  119. {
  120. storage = (storage & ~GetMask()) | ((val << position) & GetMask());
  121. return *this;
  122. }
  123. __forceinline operator T() const
  124. {
  125. if (std::numeric_limits<T>::is_signed)
  126. {
  127. std::size_t shift = 8 * sizeof(T)-bits;
  128. return (T)(((storage & GetMask()) << (shift - position)) >> shift);
  129. }
  130. else
  131. {
  132. return (T)((storage & GetMask()) >> position);
  133. }
  134. }
  135. private:
  136. // StorageType is T for non-enum types and the underlying type of T if
  137. // T is an enumeration. Note that T is wrapped within an enable_if in the
  138. // former case to workaround compile errors which arise when using
  139. // std::underlying_type<T>::type directly.
  140. typedef typename std::conditional < std::is_enum<T>::value,
  141. std::underlying_type<T>,
  142. std::enable_if < true, T >> ::type::type StorageType;
  143. // Unsigned version of StorageType
  144. typedef typename std::make_unsigned<StorageType>::type StorageTypeU;
  145. __forceinline StorageType GetMask() const
  146. {
  147. return ((~(StorageTypeU)0) >> (8 * sizeof(T)-bits)) << position;
  148. }
  149. StorageType storage;
  150. static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range");
  151. // And, you know, just in case people specify something stupid like bits=position=0x80000000
  152. static_assert(position < 8 * sizeof(T), "Invalid position");
  153. static_assert(bits <= 8 * sizeof(T), "Invalid number of bits");
  154. static_assert(bits > 0, "Invalid number of bits");
  155. };
  156. #pragma pack()