BitField.h 6.8 KB

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