Utilities\BitField.h: Try to fix some warnings

Cleanup and make bitfield extraction u32 if possible
This commit is contained in:
Eladash 2023-12-31 21:56:55 +02:00 committed by Elad Ashkenazi
parent a4ab6126b0
commit c2076373e8
3 changed files with 50 additions and 61 deletions

View File

@ -15,6 +15,9 @@ struct bf_base
using vtype = std::common_type_t<type>;
using utype = typename std::make_unsigned<vtype>::type;
static constexpr bool can_be_packed = N < (sizeof(int) * 8 + (std::is_unsigned_v<vtype> ? 1 : 0)) && sizeof(vtype) > sizeof(int);
using compact_type = std::conditional_t<can_be_packed, std::conditional_t<std::is_unsigned_v<vtype>, uint, int>, vtype>;
// Datatype bitsize
static constexpr uint bitmax = sizeof(T) * 8; static_assert(N - 1 < bitmax, "bf_base<> error: N out of bounds");
@ -38,6 +41,7 @@ struct bf_t : bf_base<T, N>
using type = typename bf_t::type;
using vtype = typename bf_t::vtype;
using utype = typename bf_t::utype;
using compact_type = typename bf_t::compact_type;
// Field offset
static constexpr uint bitpos = I; static_assert(bitpos + N <= bf_t::bitmax, "bf_t<> error: I out of bounds");
@ -48,47 +52,29 @@ struct bf_t : bf_base<T, N>
return static_cast<utype>(static_cast<utype>(bf_t::mask1 >> (bf_t::bitmax - bf_t::bitsize)) << bitpos);
}
// Bitfield extraction helper
template<typename T2, typename = void>
struct extract_impl
// Bitfield extraction
static constexpr compact_type extract(const T& data) noexcept
{
static_assert(!sizeof(T2), "bf_t<> error: Invalid type");
};
template<typename T2>
struct extract_impl<T2, std::enable_if_t<std::is_unsigned<T2>::value>>
{
// Load unsigned value
static constexpr T2 extract(const T& data)
{
return static_cast<T2>((static_cast<utype>(data) >> bitpos) & bf_t::vmask);
}
};
template<typename T2>
struct extract_impl<T2, std::enable_if_t<std::is_signed<T2>::value>>
if constexpr (std::is_signed_v<T>)
{
// Load signed value (sign-extended)
static constexpr T2 extract(const T& data)
{
return static_cast<T2>(static_cast<vtype>(static_cast<utype>(data) << (bf_t::bitmax - bitpos - N)) >> (bf_t::bitmax - N));
return static_cast<compact_type>(static_cast<vtype>(static_cast<utype>(data) << (bf_t::bitmax - bitpos - N)) >> (bf_t::bitmax - N));
}
};
// Bitfield extraction
static constexpr vtype extract(const T& data)
else
{
return extract_impl<vtype>::extract(data);
// Load unsigned value
return static_cast<compact_type>((static_cast<utype>(data) >> bitpos) & bf_t::vmask);
}
}
// Bitfield insertion
static constexpr vtype insert(vtype value)
static constexpr vtype insert(compact_type value)
{
return static_cast<vtype>((value & bf_t::vmask) << bitpos);
}
// Load bitfield value
constexpr operator vtype() const
constexpr operator compact_type() const noexcept
{
return extract(this->m_data);
}
@ -100,72 +86,72 @@ struct bf_t : bf_base<T, N>
}
// Optimized bool conversion (must be removed if inappropriate)
explicit constexpr operator bool() const
explicit constexpr operator bool() const noexcept
{
return unshifted() != 0u;
}
// Store bitfield value
bf_t& operator =(vtype value)
bf_t& operator =(compact_type value) noexcept
{
this->m_data = static_cast<vtype>((this->m_data & ~data_mask()) | insert(value));
return *this;
}
vtype operator ++(int)
compact_type operator ++(int)
{
utype result = *this;
*this = static_cast<vtype>(result + 1);
compact_type result = *this;
*this = static_cast<compact_type>(result + 1u);
return result;
}
bf_t& operator ++()
{
return *this = *this + 1;
return *this = static_cast<compact_type>(*this + 1u);
}
vtype operator --(int)
compact_type operator --(int)
{
utype result = *this;
*this = static_cast<vtype>(result - 1);
compact_type result = *this;
*this = static_cast<compact_type>(result - 1u);
return result;
}
bf_t& operator --()
{
return *this = *this - 1;
return *this = static_cast<compact_type>(*this - 1u);
}
bf_t& operator +=(vtype right)
bf_t& operator +=(compact_type right)
{
return *this = *this + right;
return *this = static_cast<compact_type>(*this + right);
}
bf_t& operator -=(vtype right)
bf_t& operator -=(compact_type right)
{
return *this = *this - right;
return *this = static_cast<compact_type>(*this - right);
}
bf_t& operator *=(vtype right)
bf_t& operator *=(compact_type right)
{
return *this = *this * right;
return *this = static_cast<compact_type>(*this * right);
}
bf_t& operator &=(vtype right)
bf_t& operator &=(compact_type right)
{
this->m_data &= static_cast<vtype>(((static_cast<utype>(right) & bf_t::vmask) << bitpos) | ~(bf_t::vmask << bitpos));
this->m_data &= static_cast<vtype>(((static_cast<utype>(right + 0u) & bf_t::vmask) << bitpos) | ~(bf_t::vmask << bitpos));
return *this;
}
bf_t& operator |=(vtype right)
bf_t& operator |=(compact_type right)
{
this->m_data |= static_cast<vtype>((static_cast<utype>(right) & bf_t::vmask) << bitpos);
this->m_data |= static_cast<vtype>((static_cast<utype>(right + 0u) & bf_t::vmask) << bitpos);
return *this;
}
bf_t& operator ^=(vtype right)
bf_t& operator ^=(compact_type right)
{
this->m_data ^= static_cast<vtype>((static_cast<utype>(right) & bf_t::vmask) << bitpos);
this->m_data ^= static_cast<vtype>((static_cast<utype>(right + 0u) & bf_t::vmask) << bitpos);
return *this;
}
};
@ -177,6 +163,7 @@ struct cf_t : bf_base<typename F::type, F::bitsize + cf_t<Fields...>::bitsize>
using type = typename cf_t::type;
using vtype = typename cf_t::vtype;
using utype = typename cf_t::utype;
using compact_type = typename cf_t::compact_type;
// Get disjunction of all "data" masks of concatenated values
static constexpr vtype data_mask()
@ -185,25 +172,25 @@ struct cf_t : bf_base<typename F::type, F::bitsize + cf_t<Fields...>::bitsize>
}
// Extract all bitfields and concatenate
static constexpr vtype extract(const type& data)
static constexpr compact_type extract(const type& data)
{
return static_cast<vtype>(static_cast<utype>(F::extract(data)) << cf_t<Fields...>::bitsize | cf_t<Fields...>::extract(data));
return static_cast<compact_type>(static_cast<utype>(F::extract(data)) << cf_t<Fields...>::bitsize | cf_t<Fields...>::extract(data));
}
// Split bitfields and insert them
static constexpr vtype insert(vtype value)
static constexpr vtype insert(compact_type value)
{
return static_cast<vtype>(F::insert(value >> cf_t<Fields...>::bitsize) | cf_t<Fields...>::insert(value));
}
// Load value
constexpr operator vtype() const
constexpr operator compact_type() const noexcept
{
return extract(this->m_data);
}
// Store value
cf_t& operator =(vtype value)
cf_t& operator =(compact_type value) noexcept
{
this->m_data = (this->m_data & ~data_mask()) | insert(value);
return *this;
@ -249,7 +236,7 @@ struct ff_t : bf_base<T, N>
}
// Get value
operator vtype() const
constexpr operator vtype() const noexcept
{
return V;
}

View File

@ -570,7 +570,7 @@ namespace vm
}
addr2 += size3;
size2 -= size3;
size2 -= static_cast<u32>(size3);
}
return 0;

View File

@ -339,10 +339,12 @@ namespace rsx
max_result_by_division = std::max<u32>(max_result_by_division, max);
// Discard lower frequencies because it has been proven that there are indices higher than them
freq_count -= frequencies + freq_count - std::remove_if(frequencies, frequencies + freq_count, [&max_result_by_division](u32 freq)
const usz discard_cnt = frequencies + freq_count - std::remove_if(frequencies, frequencies + freq_count, [&max_result_by_division](u32 freq)
{
return freq <= max_result_by_division;
});
freq_count -= static_cast<u32>(discard_cnt);
}
}
}