2022-04-28 11:24:11 -05:00
|
|
|
// SPDX-FileCopyrightText: 2014 Tony Wasserka
|
|
|
|
// SPDX-FileCopyrightText: 2014 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0-or-later
|
2014-05-07 17:14:42 -05:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-06-21 07:40:28 -05:00
|
|
|
#include <cstddef>
|
2014-05-07 17:14:42 -05:00
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
2019-01-25 11:16:23 -06:00
|
|
|
#include "common/swap.h"
|
2014-05-07 17:14:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2014-10-25 14:12:24 -05:00
|
|
|
* BitField<7,8,u32> next_eight_bits; // unsigned
|
2014-05-07 17:14:42 -05:00
|
|
|
* 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);
|
|
|
|
*
|
|
|
|
* 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)
|
2019-01-25 11:16:23 -06:00
|
|
|
template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag>
|
2016-09-17 19:38:01 -05:00
|
|
|
struct BitField {
|
2014-05-07 17:14:42 -05:00
|
|
|
private:
|
2018-11-10 22:41:30 -06:00
|
|
|
// UnderlyingType is T for non-enum types and the underlying type of T if
|
2017-05-20 22:40:13 -05:00
|
|
|
// 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.
|
2018-11-10 22:41:30 -06:00
|
|
|
using UnderlyingType = typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>,
|
|
|
|
std::enable_if<true, T>>::type;
|
2017-05-20 22:40:13 -05:00
|
|
|
|
2018-11-10 22:41:30 -06:00
|
|
|
// We store the value as the unsigned type to avoid undefined behaviour on value shifting
|
|
|
|
using StorageType = std::make_unsigned_t<UnderlyingType>;
|
2017-05-20 22:40:13 -05:00
|
|
|
|
2019-01-25 11:16:23 -06:00
|
|
|
using StorageTypeWithEndian = typename AddEndian<StorageType, EndianTag>::type;
|
|
|
|
|
2014-05-07 17:14:42 -05:00
|
|
|
public:
|
2017-05-20 22:40:13 -05:00
|
|
|
/// Constants to allow limited introspection of fields if needed
|
2018-09-15 08:21:06 -05:00
|
|
|
static constexpr std::size_t position = Position;
|
|
|
|
static constexpr std::size_t bits = Bits;
|
2019-11-16 00:29:37 -06:00
|
|
|
static constexpr StorageType mask = (((StorageType)~0) >> (8 * sizeof(T) - bits)) << position;
|
2017-05-20 22:40:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a value by masking and shifting it according to the field parameters. A value
|
|
|
|
* containing several bitfields can be assembled by formatting each of their values and ORing
|
|
|
|
* the results together.
|
|
|
|
*/
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] static constexpr StorageType FormatValue(const T& value) {
|
|
|
|
return (static_cast<StorageType>(value) << position) & mask;
|
2017-05-20 22:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts a value from the passed storage. In most situations prefer use the member functions
|
|
|
|
* (such as Value() or operator T), but this can be used to extract a value from a bitfield
|
|
|
|
* union in a constexpr context.
|
|
|
|
*/
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] static constexpr T ExtractValue(const StorageType& storage) {
|
2018-11-10 22:41:30 -06:00
|
|
|
if constexpr (std::numeric_limits<UnderlyingType>::is_signed) {
|
2017-05-20 22:40:13 -05:00
|
|
|
std::size_t shift = 8 * sizeof(T) - bits;
|
2018-11-10 22:41:30 -06:00
|
|
|
return static_cast<T>(static_cast<UnderlyingType>(storage << (shift - position)) >>
|
|
|
|
shift);
|
2017-05-20 22:40:13 -05:00
|
|
|
} else {
|
2018-11-10 22:41:30 -06:00
|
|
|
return static_cast<T>((storage & mask) >> position);
|
2017-05-20 22:40:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:08:10 -05:00
|
|
|
// This constructor and assignment operator might be considered ambiguous:
|
2016-02-11 11:41:15 -06:00
|
|
|
// Would they initialize the storage or just the bitfield?
|
|
|
|
// Hence, delete them. Use the Assign method to set bitfield values!
|
|
|
|
BitField(T val) = delete;
|
|
|
|
BitField& operator=(T val) = delete;
|
|
|
|
|
2019-03-07 15:32:10 -06:00
|
|
|
constexpr BitField() noexcept = default;
|
|
|
|
|
|
|
|
constexpr BitField(const BitField&) noexcept = default;
|
|
|
|
constexpr BitField& operator=(const BitField&) noexcept = default;
|
|
|
|
|
|
|
|
constexpr BitField(BitField&&) noexcept = default;
|
|
|
|
constexpr BitField& operator=(BitField&&) noexcept = default;
|
2014-05-07 17:14:42 -05:00
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr operator T() const {
|
2014-05-29 15:54:34 -05:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:25:37 -06:00
|
|
|
constexpr void Assign(const T& value) {
|
2020-04-24 02:27:51 -05:00
|
|
|
storage = static_cast<StorageType>((storage & ~mask) | FormatValue(value));
|
2014-12-15 18:18:56 -06:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr T Value() const {
|
2017-05-20 22:40:13 -05:00
|
|
|
return ExtractValue(storage);
|
2014-05-07 17:14:42 -05:00
|
|
|
}
|
|
|
|
|
2020-08-14 08:38:45 -05:00
|
|
|
[[nodiscard]] constexpr explicit operator bool() const {
|
2014-12-20 17:28:17 -06:00
|
|
|
return Value() != 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:14:42 -05:00
|
|
|
private:
|
2019-01-25 11:16:23 -06:00
|
|
|
StorageTypeWithEndian storage;
|
2014-05-07 17:14:42 -05:00
|
|
|
|
|
|
|
static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range");
|
|
|
|
|
|
|
|
// And, you know, just in case people specify something stupid like bits=position=0x80000000
|
|
|
|
static_assert(position < 8 * sizeof(T), "Invalid position");
|
|
|
|
static_assert(bits <= 8 * sizeof(T), "Invalid number of bits");
|
|
|
|
static_assert(bits > 0, "Invalid number of bits");
|
2018-04-17 17:00:18 -05:00
|
|
|
static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField");
|
2014-05-07 17:14:42 -05:00
|
|
|
};
|
|
|
|
#pragma pack()
|
2019-01-25 11:16:23 -06:00
|
|
|
|
|
|
|
template <std::size_t Position, std::size_t Bits, typename T>
|
|
|
|
using BitFieldBE = BitField<Position, Bits, T, BETag>;
|