File: BitField.h

package info (click to toggle)
dolphin-emu 2503%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 111,624 kB
  • sloc: cpp: 787,747; ansic: 217,914; xml: 31,400; python: 4,226; yacc: 3,985; javascript: 2,430; makefile: 777; asm: 726; sh: 281; pascal: 257; perl: 97; objc: 75
file content (499 lines) | stat: -rw-r--r-- 19,223 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

// Copyright 2014 Tony Wasserka
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the owner nor the names of its contributors may
//       be used to endorse or promote products derived from this software
//       without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <cstddef>
#include <fmt/format.h>
#include <iterator>
#include <limits>
#include <type_traits>

#include "Common/Inline.h"

/*
 * Abstract bitfield class
 *
 * Allows endianness-independent access to individual bitfields within some raw
 * integer value. The assembly generated by this class is identical to the
 * usage of raw bitfields, so it's a perfectly fine replacement.
 *
 * For BitField<X,Y,Z>, X is the distance of the bitfield to the LSB of the
 * raw value, Y is the length in bits of the bitfield. Z is an integer type
 * which determines the sign of the bitfield. Z must have the same size as the
 * raw integer.
 *
 *
 * General usage:
 *
 * Create a new union with the raw integer value as a member.
 * Then for each bitfield you want to expose, add a BitField member
 * in the union. The template parameters are the bit offset and the number
 * of desired bits.
 *
 * Changes in the bitfield members will then get reflected in the raw integer
 * value and vice-versa.
 *
 *
 * Sample usage:
 *
 * union SomeRegister
 * {
 *     u32 hex;
 *
 *     BitField<0,7,u32> first_seven_bits;     // unsigned
 *     BitField<7,8,u32> next_eight_bits;      // unsigned
 *     BitField<3,15,s32> some_signed_fields;  // signed
 * };
 *
 * This is equivalent to the little-endian specific code:
 *
 * union SomeRegister
 * {
 *     u32 hex;
 *
 *     struct
 *     {
 *         u32 first_seven_bits : 7;
 *         u32 next_eight_bits : 8;
 *     };
 *     struct
 *     {
 *         u32 : 3; // padding
 *         s32 some_signed_fields : 15;
 *     };
 * };
 *
 *
 * Caveats:
 *
 * 1)
 * BitField provides automatic casting from and to the storage type where
 * appropriate. However, when using non-typesafe functions like printf, an
 * explicit cast must be performed on the BitField object to make sure it gets
 * passed correctly, e.g.:
 * printf("Value: %d", (s32)some_register.some_signed_fields);
 * Note that this does not apply when using fmt, as a formatter is provided that
 * handles this conversion automatically.
 *
 * 2)
 * Not really a caveat, but potentially irritating: This class is used in some
 * packed structures that do not guarantee proper alignment. Therefore we have
 * to use #pragma pack here not to pack the members of the class, but instead
 * to break GCC's assumption that the members of the class are aligned on
 * sizeof(StorageType).
 * TODO(neobrain): Confirm that this is a proper fix and not just masking
 * symptoms.
 */
#pragma pack(1)
template <std::size_t position, std::size_t bits, typename T,
          // StorageType is T for non-enum types and the underlying type of T if
          // T is an enumeration. Note that T is wrapped within an enable_if in the
          // former case to workaround compile errors which arise when using
          // std::underlying_type<T>::type directly.
          typename StorageType = typename std::conditional_t<
              std::is_enum<T>::value, std::underlying_type<T>, std::enable_if<true, T>>::type>
struct BitField
{
private:
  // This constructor might be considered ambiguous:
  // Would it initialize the storage or just the bitfield?
  // Hence, delete it. Use the assignment operator to set bitfield values!
  BitField(T val) = delete;

public:
  // Force default constructor to be created
  // so that we can use this within unions
  constexpr BitField() = default;

  // We explicitly delete the copy assignment operator here, because the
  // default copy assignment would copy the full storage value, rather than
  // just the bits relevant to this particular bit field.
  // Ideally, we would just implement the copy assignment to copy only the
  // relevant bits, but we're prevented from doing that because the savestate
  // code expects that this class is trivially copyable.
  BitField& operator=(const BitField&) = delete;

  DOLPHIN_FORCE_INLINE BitField& operator=(T val)
  {
    storage = (storage & ~GetMask()) | ((static_cast<StorageType>(val) << position) & GetMask());
    return *this;
  }

  constexpr T Value() const { return Value(std::is_signed<T>()); }
  constexpr operator T() const { return Value(); }
  static constexpr bool IsSigned() { return std::is_signed<T>(); }
  static constexpr std::size_t StartBit() { return position; }
  static constexpr std::size_t NumBits() { return bits; }

private:
  // Unsigned version of StorageType
  using StorageTypeU = std::make_unsigned_t<StorageType>;

  constexpr T Value(std::true_type) const
  {
    const size_t shift_amount = 8 * sizeof(StorageType) - bits;
    return static_cast<T>((storage << (shift_amount - position)) >> shift_amount);
  }

  constexpr T Value(std::false_type) const
  {
    return static_cast<T>((storage & GetMask()) >> position);
  }

  static constexpr StorageType GetMask()
  {
    return (std::numeric_limits<StorageTypeU>::max() >> (8 * sizeof(StorageType) - bits))
           << position;
  }

  StorageType storage;

  static_assert(bits + position <= 8 * sizeof(StorageType), "Bitfield out of range");
  static_assert(sizeof(T) <= sizeof(StorageType), "T must fit in StorageType");

  // And, you know, just in case people specify something stupid like bits=position=0x80000000
  static_assert(position < 8 * sizeof(StorageType), "Invalid position");
  static_assert(bits <= 8 * sizeof(T), "Invalid number of bits");
  static_assert(bits > 0, "Invalid number of bits");
};
#pragma pack()

// Use the underlying type's formatter for BitFields, if one exists
template <std::size_t position, std::size_t bits, typename T, typename S>
struct fmt::formatter<BitField<position, bits, T, S>>
{
  fmt::formatter<T> m_formatter;
  constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); }
  template <typename FormatContext>
  auto format(const BitField<position, bits, T, S>& bitfield, FormatContext& ctx) const
  {
    return m_formatter.format(bitfield.Value(), ctx);
  }
};

// Language limitations require the following to make these formattable
// (formatter<BitFieldArray<position, bits, size, T>::Ref> is not legal)
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstRef;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayRef;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstIterator;
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayIterator;

#pragma pack(1)
template <std::size_t position, std::size_t bits, std::size_t size, typename T,
          // StorageType is T for non-enum types and the underlying type of T if
          // T is an enumeration. Note that T is wrapped within an enable_if in the
          // former case to workaround compile errors which arise when using
          // std::underlying_type<T>::type directly.
          typename StorageType = typename std::conditional_t<
              std::is_enum<T>::value, std::underlying_type<T>, std::enable_if<true, T>>::type>
struct BitFieldArray
{
  using Ref = BitFieldArrayRef<position, bits, size, T, StorageType>;
  using ConstRef = BitFieldArrayConstRef<position, bits, size, T, StorageType>;
  using Iterator = BitFieldArrayIterator<position, bits, size, T, StorageType>;
  using ConstIterator = BitFieldArrayConstIterator<position, bits, size, T, StorageType>;

private:
  // This constructor might be considered ambiguous:
  // Would it initialize the storage or just the bitfield?
  // Hence, delete it. Use the assignment operator to set bitfield values!
  BitFieldArray(T val) = delete;

public:
  // Force default constructor to be created
  // so that we can use this within unions
  constexpr BitFieldArray() = default;

  // We explicitly delete the copy assignment operator here, because the
  // default copy assignment would copy the full storage value, rather than
  // just the bits relevant to this particular bit field.
  // Ideally, we would just implement the copy assignment to copy only the
  // relevant bits, but we're prevented from doing that because the savestate
  // code expects that this class is trivially copyable.
  BitFieldArray& operator=(const BitFieldArray&) = delete;

public:
  constexpr bool IsSigned() const { return std::is_signed<T>(); }
  constexpr std::size_t StartBit() const { return position; }
  constexpr std::size_t NumBits() const { return bits; }
  constexpr std::size_t Size() const { return size; }
  constexpr std::size_t TotalNumBits() const { return bits * size; }

  constexpr T Value(size_t index) const { return Value(std::is_signed<T>(), index); }
  void SetValue(size_t index, T value)
  {
    const size_t pos = position + bits * index;
    storage = (storage & ~GetElementMask(index)) |
              ((static_cast<StorageType>(value) << pos) & GetElementMask(index));
  }
  Ref operator[](size_t index) { return Ref(this, index); }
  constexpr const ConstRef operator[](size_t index) const { return ConstRef(this, index); }

  constexpr Iterator begin() { return Iterator(this, 0); }
  constexpr Iterator end() { return Iterator(this, size); }
  constexpr ConstIterator begin() const { return ConstIterator(this, 0); }
  constexpr ConstIterator end() const { return ConstIterator(this, size); }
  constexpr ConstIterator cbegin() const { return begin(); }
  constexpr ConstIterator cend() const { return end(); }

private:
  // Unsigned version of StorageType
  using StorageTypeU = std::make_unsigned_t<StorageType>;

  constexpr T Value(std::true_type, size_t index) const
  {
    const size_t pos = position + bits * index;
    const size_t shift_amount = 8 * sizeof(StorageType) - bits;
    return static_cast<T>((storage << (shift_amount - pos)) >> shift_amount);
  }

  constexpr T Value(std::false_type, size_t index) const
  {
    const size_t pos = position + bits * index;
    return static_cast<T>((storage & GetElementMask(index)) >> pos);
  }

  static constexpr StorageType GetElementMask(size_t index)
  {
    const size_t pos = position + bits * index;
    return (std::numeric_limits<StorageTypeU>::max() >> (8 * sizeof(StorageType) - bits)) << pos;
  }

  StorageType storage;

  static_assert(bits * size + position <= 8 * sizeof(StorageType), "Bitfield array out of range");
  static_assert(sizeof(T) <= sizeof(StorageType), "T must fit in StorageType");

  // And, you know, just in case people specify something stupid like bits=position=0x80000000
  static_assert(position < 8 * sizeof(StorageType), "Invalid position");
  static_assert(bits <= 8 * sizeof(T), "Invalid number of bits");
  static_assert(bits > 0, "Invalid number of bits");
  static_assert(size <= 8 * sizeof(StorageType), "Invalid size");
  static_assert(size > 0, "Invalid size");
};
#pragma pack()

template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstRef
{
  friend struct BitFieldArray<position, bits, size, T, S>;
  friend class BitFieldArrayConstIterator<position, bits, size, T, S>;

public:
  constexpr T Value() const { return m_array->Value(m_index); }
  constexpr operator T() const { return Value(); }

private:
  constexpr BitFieldArrayConstRef(const BitFieldArray<position, bits, size, T, S>* array,
                                  size_t index)
      : m_array(array), m_index(index)
  {
  }

  const BitFieldArray<position, bits, size, T, S>* const m_array;
  const size_t m_index;
};

template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayRef
{
  friend struct BitFieldArray<position, bits, size, T, S>;
  friend class BitFieldArrayIterator<position, bits, size, T, S>;

public:
  constexpr T Value() const { return m_array->Value(m_index); }
  constexpr operator T() const { return Value(); }
  T operator=(const BitFieldArrayRef<position, bits, size, T, S>& value) const
  {
    m_array->SetValue(m_index, value);
    return value;
  }
  T operator=(T value) const
  {
    m_array->SetValue(m_index, value);
    return value;
  }

private:
  constexpr BitFieldArrayRef(BitFieldArray<position, bits, size, T, S>* array, size_t index)
      : m_array(array), m_index(index)
  {
  }

  BitFieldArray<position, bits, size, T, S>* const m_array;
  const size_t m_index;
};

// Satisfies LegacyOutputIterator / std::output_iterator.
// Does not satisfy LegacyInputIterator / std::input_iterator as std::output_iterator_tag does not
// extend std::input_iterator_tag.
// Does not satisfy LegacyForwardIterator / std::forward_iterator, as that requires use of real
// references instead of proxy objects.
// This iterator allows use of BitFieldArray in range-based for loops, and with fmt::join.
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayIterator
{
  friend struct BitFieldArray<position, bits, size, T, S>;

public:
  using iterator_category = std::output_iterator_tag;
  using value_type = T;
  using difference_type = ptrdiff_t;
  using pointer = void;
  using reference = BitFieldArrayRef<position, bits, size, T, S>;

private:
  constexpr BitFieldArrayIterator(BitFieldArray<position, bits, size, T, S>* array, size_t index)
      : m_array(array), m_index(index)
  {
  }

public:
  // Required by std::input_or_output_iterator
  constexpr BitFieldArrayIterator() = default;
  // Required by LegacyIterator
  constexpr BitFieldArrayIterator(const BitFieldArrayIterator& other) = default;
  // Required by LegacyIterator
  BitFieldArrayIterator& operator=(const BitFieldArrayIterator& other) = default;
  // Move constructor and assignment operators, explicitly defined for completeness
  constexpr BitFieldArrayIterator(BitFieldArrayIterator&& other) = default;
  BitFieldArrayIterator& operator=(BitFieldArrayIterator&& other) = default;

public:
  BitFieldArrayIterator& operator++()
  {
    m_index++;
    return *this;
  }
  BitFieldArrayIterator operator++(int)
  {
    BitFieldArrayIterator other(*this);
    ++*this;
    return other;
  }
  constexpr reference operator*() const { return reference(m_array, m_index); }
  constexpr bool operator==(BitFieldArrayIterator other) const { return m_index == other.m_index; }
  constexpr bool operator!=(BitFieldArrayIterator other) const { return m_index != other.m_index; }

private:
  BitFieldArray<position, bits, size, T, S>* m_array;
  size_t m_index;
};

// Satisfies LegacyInputIterator / std::input_iterator.
// Does not satisfy LegacyForwardIterator / std::forward_iterator, as that requires use of real
// references instead of proxy objects.
// This iterator allows use of BitFieldArray in range-based for loops, and with fmt::join.
template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
class BitFieldArrayConstIterator
{
  friend struct BitFieldArray<position, bits, size, T, S>;

public:
  using iterator_category = std::input_iterator_tag;
  using value_type = T;
  using difference_type = ptrdiff_t;
  using pointer = void;
  using reference = BitFieldArrayConstRef<position, bits, size, T, S>;

private:
  constexpr BitFieldArrayConstIterator(const BitFieldArray<position, bits, size, T, S>* array,
                                       size_t index)
      : m_array(array), m_index(index)
  {
  }

public:
  // Required by std::input_or_output_iterator
  constexpr BitFieldArrayConstIterator() = default;
  // Required by LegacyIterator
  constexpr BitFieldArrayConstIterator(const BitFieldArrayConstIterator& other) = default;
  // Required by LegacyIterator
  BitFieldArrayConstIterator& operator=(const BitFieldArrayConstIterator& other) = default;
  // Move constructor and assignment operators, explicitly defined for completeness
  constexpr BitFieldArrayConstIterator(BitFieldArrayConstIterator&& other) = default;
  BitFieldArrayConstIterator& operator=(BitFieldArrayConstIterator&& other) = default;

public:
  BitFieldArrayConstIterator& operator++()
  {
    m_index++;
    return *this;
  }
  BitFieldArrayConstIterator operator++(int)
  {
    BitFieldArrayConstIterator other(*this);
    ++*this;
    return other;
  }
  constexpr reference operator*() const { return reference(m_array, m_index); }
  constexpr bool operator==(BitFieldArrayConstIterator other) const
  {
    return m_index == other.m_index;
  }
  constexpr bool operator!=(BitFieldArrayConstIterator other) const
  {
    return m_index != other.m_index;
  }

private:
  const BitFieldArray<position, bits, size, T, S>* m_array;
  size_t m_index;
};

template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
struct fmt::formatter<BitFieldArrayRef<position, bits, size, T, S>>
{
  fmt::formatter<T> m_formatter;
  constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); }
  template <typename FormatContext>
  auto format(const BitFieldArrayRef<position, bits, size, T, S>& ref, FormatContext& ctx) const
  {
    return m_formatter.format(ref.Value(), ctx);
  }
};

template <std::size_t position, std::size_t bits, std::size_t size, typename T, typename S>
struct fmt::formatter<BitFieldArrayConstRef<position, bits, size, T, S>>
{
  fmt::formatter<T> m_formatter;
  constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); }
  template <typename FormatContext>
  auto format(const BitFieldArrayConstRef<position, bits, size, T, S>& ref,
              FormatContext& ctx) const
  {
    return m_formatter.format(ref.Value(), ctx);
  }
};