2015-09-26 20:46:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
#include "types.h"
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
template<typename T, uint N>
|
|
|
|
struct bf_base
|
|
|
|
{
|
|
|
|
using type = T;
|
|
|
|
using vtype = simple_t<type>;
|
2017-03-04 13:54:43 +00:00
|
|
|
using utype = typename std::make_unsigned<vtype>::type;
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Datatype bitsize
|
2016-08-14 00:22:19 +00:00
|
|
|
static constexpr uint bitmax = sizeof(T) * 8; static_assert(N - 1 < bitmax, "bf_base<> error: N out of bounds");
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
// Field bitsize
|
|
|
|
static constexpr uint bitsize = N;
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Value mask
|
2017-03-04 13:54:43 +00:00
|
|
|
static constexpr utype vmask = static_cast<utype>(~utype{} >> (bitmax - bitsize));
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2017-06-30 22:53:16 +00:00
|
|
|
// All ones mask
|
|
|
|
static constexpr utype mask1 = static_cast<utype>(~utype{});
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
protected:
|
2015-09-26 20:46:04 +00:00
|
|
|
type m_data;
|
2016-02-01 21:55:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bitfield accessor (N bits from I position, 0 is LSB)
|
|
|
|
template<typename T, uint I, uint N>
|
|
|
|
struct bf_t : bf_base<T, N>
|
|
|
|
{
|
|
|
|
using type = typename bf_t::type;
|
|
|
|
using vtype = typename bf_t::vtype;
|
2017-03-04 13:54:43 +00:00
|
|
|
using utype = typename bf_t::utype;
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
// Field offset
|
|
|
|
static constexpr uint bitpos = I; static_assert(bitpos + N <= bf_t::bitmax, "bf_t<> error: I out of bounds");
|
|
|
|
|
|
|
|
// Get bitmask of size N, at I pos
|
2017-03-04 13:54:43 +00:00
|
|
|
static constexpr utype data_mask()
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2017-06-30 22:53:16 +00:00
|
|
|
return static_cast<utype>(static_cast<utype>(bf_t::mask1 >> (bf_t::bitmax - bf_t::bitsize)) << bitpos);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Bitfield extraction helper
|
|
|
|
template<typename T2, typename = void>
|
|
|
|
struct extract_impl
|
|
|
|
{
|
|
|
|
static_assert(!sizeof(T2), "bf_t<> error: Invalid type");
|
|
|
|
};
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
template<typename T2>
|
|
|
|
struct extract_impl<T2, std::enable_if_t<std::is_unsigned<T2>::value>>
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
|
|
|
// Load unsigned value
|
2016-02-01 21:55:43 +00:00
|
|
|
static constexpr T2 extract(const T& data)
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<T2>((static_cast<utype>(data) >> bitpos) & bf_t::vmask);
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
template<typename T2>
|
|
|
|
struct extract_impl<T2, std::enable_if_t<std::is_signed<T2>::value>>
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
|
|
|
// Load signed value (sign-extended)
|
2016-02-01 21:55:43 +00:00
|
|
|
static constexpr T2 extract(const T& data)
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<T2>(static_cast<vtype>(static_cast<utype>(data) << (bf_t::bitmax - bitpos - N)) >> (bf_t::bitmax - N));
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Bitfield extraction
|
|
|
|
static constexpr vtype extract(const T& data)
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
return extract_impl<vtype>::extract(data);
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Bitfield insertion
|
|
|
|
static constexpr vtype insert(vtype value)
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<vtype>((value & bf_t::vmask) << bitpos);
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Load bitfield value
|
|
|
|
constexpr operator vtype() const
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
return extract(this->m_data);
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Load raw data with mask applied
|
|
|
|
constexpr T unshifted() const
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<T>(this->m_data & data_mask());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimized bool conversion (must be removed if inappropriate)
|
|
|
|
explicit constexpr operator bool() const
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
|
|
|
return unshifted() != 0;
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Store bitfield value
|
|
|
|
bf_t& operator =(vtype value)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
this->m_data = static_cast<vtype>((this->m_data & ~data_mask()) | insert(value));
|
2016-02-01 21:55:43 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-09-26 20:46:04 +00:00
|
|
|
vtype operator ++(int)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
utype result = *this;
|
|
|
|
*this = static_cast<vtype>(result + 1);
|
2015-09-26 20:46:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator ++()
|
|
|
|
{
|
|
|
|
return *this = *this + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vtype operator --(int)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
utype result = *this;
|
|
|
|
*this = static_cast<vtype>(result - 1);
|
2015-09-26 20:46:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator --()
|
|
|
|
{
|
|
|
|
return *this = *this - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator +=(vtype right)
|
|
|
|
{
|
|
|
|
return *this = *this + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator -=(vtype right)
|
|
|
|
{
|
|
|
|
return *this = *this - right;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator *=(vtype right)
|
|
|
|
{
|
|
|
|
return *this = *this * right;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator &=(vtype right)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
this->m_data &= static_cast<vtype>((static_cast<utype>(right) & bf_t::vmask) << bitpos);
|
2015-09-26 20:46:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator |=(vtype right)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
this->m_data |= static_cast<vtype>((static_cast<utype>(right) & bf_t::vmask) << bitpos);
|
2015-09-26 20:46:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bf_t& operator ^=(vtype right)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
this->m_data ^= static_cast<vtype>((static_cast<utype>(right) & bf_t::vmask) << bitpos);
|
2015-09-26 20:46:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Field pack (concatenated from left to right)
|
|
|
|
template<typename F = void, typename... Fields>
|
|
|
|
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;
|
2017-03-04 13:54:43 +00:00
|
|
|
using utype = typename cf_t::utype;
|
2015-09-26 20:46:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
// Get disjunction of all "data" masks of concatenated values
|
|
|
|
static constexpr vtype data_mask()
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<vtype>(F::data_mask() | cf_t<Fields...>::data_mask());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract all bitfields and concatenate
|
|
|
|
static constexpr vtype extract(const type& data)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<vtype>(static_cast<utype>(F::extract(data)) << cf_t<Fields...>::bitsize | cf_t<Fields...>::extract(data));
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split bitfields and insert them
|
|
|
|
static constexpr vtype insert(vtype value)
|
|
|
|
{
|
2017-03-04 13:54:43 +00:00
|
|
|
return static_cast<vtype>(F::insert(value >> cf_t<Fields...>::bitsize) | cf_t<Fields...>::insert(value));
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load value
|
|
|
|
constexpr operator vtype() const
|
|
|
|
{
|
|
|
|
return extract(this->m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store value
|
|
|
|
cf_t& operator =(vtype value)
|
|
|
|
{
|
|
|
|
this->m_data = (this->m_data & ~data_mask()) | insert(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Empty field pack (recursion terminator)
|
|
|
|
template<>
|
|
|
|
struct cf_t<void>
|
|
|
|
{
|
|
|
|
static constexpr uint bitsize = 0;
|
|
|
|
|
|
|
|
static constexpr uint data_mask()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static constexpr auto extract(const T& data) -> decltype(+T())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static constexpr T insert(T value)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fixed field (provides constant values in field pack)
|
|
|
|
template<typename T, T V, uint N>
|
|
|
|
struct ff_t : bf_base<T, N>
|
|
|
|
{
|
|
|
|
using type = typename ff_t::type;
|
|
|
|
using vtype = typename ff_t::vtype;
|
|
|
|
|
|
|
|
// Return constant value
|
|
|
|
static constexpr vtype extract(const type& data)
|
|
|
|
{
|
|
|
|
static_assert((V & ff_t::vmask) == V, "ff_t<> error: V out of bounds");
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get value
|
|
|
|
operator vtype() const
|
|
|
|
{
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
};
|
2016-08-03 20:51:05 +00:00
|
|
|
|
|
|
|
template<typename T, uint I, uint N>
|
|
|
|
struct fmt_unveil<bf_t<T, I, N>, void>
|
|
|
|
{
|
|
|
|
using type = typename fmt_unveil<simple_t<T>>::type;
|
|
|
|
|
2016-08-07 13:59:46 +00:00
|
|
|
static inline auto get(const bf_t<T, I, N>& bf)
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
return fmt_unveil<type>::get(bf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, typename... Fields>
|
|
|
|
struct fmt_unveil<cf_t<F, Fields...>, void>
|
|
|
|
{
|
|
|
|
using type = typename fmt_unveil<simple_t<typename F::type>>::type;
|
|
|
|
|
2016-08-07 13:59:46 +00:00
|
|
|
static inline auto get(const cf_t<F, Fields...>& cf)
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
return fmt_unveil<type>::get(cf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, T V, uint N>
|
|
|
|
struct fmt_unveil<ff_t<T, V, N>, void>
|
|
|
|
{
|
|
|
|
using type = typename fmt_unveil<simple_t<T>>::type;
|
|
|
|
|
2016-08-07 13:59:46 +00:00
|
|
|
static inline auto get(const ff_t<T, V, N>& ff)
|
2016-08-03 20:51:05 +00:00
|
|
|
{
|
|
|
|
return fmt_unveil<type>::get(ff);
|
|
|
|
}
|
|
|
|
};
|