mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-04-02 10:42:14 -04:00
byuu says: So this turned out to be a rather unproductive ten-hour rabbit hole, but ... I reworked nall/primitives.hpp a lot. And because the changes are massive, testing of this WIP for regressions is critically important. I really can't stress that enough, we're almost certainly going to have some hidden regressions here ... We now have a nall/primitives/ subfolder that splits up the classes into manageable components. The bit-field support is now shared between both Natural and Integer. All of the assignment operator overloads are now templated and take references instead of values. Things like the GSU::Register class are non-copyable on account of the function<> object inside of it, and previously only operator= would work with classes like that. The big change is nall/primitives/operators.hpp, which is a really elaborate system to compute the minimum number of bits needed for any operation, and to return a Natural<T> or Integer<T> when one or both of the arguments are such a type. Unfortunately, it doesn't really work yet ... Kirby's Dream Land 3 breaks if we include operators.hpp. Zelda 3 runs fine with this, but I had to make a huge amount of core changes, including introducing a new ternary(bool, lhs, rhs) function to nall/algorithm to get past Natural<X> and Natural<Y> not being equivalent (is_integral types get a special exemption to ternary ?: type equivalence, yet it's impossible to simulate with our own classes, which is bullshit.) The horrifying part is that ternary() will evaluate both lhs and rhs, unlike ?: I converted some of the functions to test ? uint(x) : uint(y), and others to ternary(test, x, y) ... I don't have a strong preference either way yet. But the part where things may have gotten broken is in the changes to where ternary() was placed. Some cases like in the GBA PPU renderer, it was rather unclear the order of evaluations, so I may have made a mistake somewhere. So again, please please test this if you can. Or even better, look over the diff. Longer-term, I'd really like the enable nall/primitives/operators.hpp, but right now I'm not sure why Kirby's Dream Land 3 is breaking. Help would be appreciated, but ... it's gonna be really complex and difficult to debug, so I'm probably gonna be on my own here ... sigh.
142 lines
5.7 KiB
C++
142 lines
5.7 KiB
C++
#pragma once
|
|
|
|
#include <nall/primitives.hpp>
|
|
|
|
namespace nall {
|
|
|
|
template<typename Type, uint Bit> struct BooleanBitField {
|
|
enum : uint { bit = Bit };
|
|
enum : uint { mask = 1ull << bit };
|
|
using type = Type;
|
|
using utype = typename std::make_unsigned<type>::type;
|
|
static_assert(bit < sizeof(type) * 8, "");
|
|
|
|
inline BooleanBitField() = default;
|
|
inline BooleanBitField(const BooleanBitField& value) { set(value.get()); }
|
|
template<typename T> inline BooleanBitField(const bool value) { set(value); }
|
|
|
|
inline operator bool() const { return get(); }
|
|
|
|
inline auto& operator=(const BooleanBitField& value) { return set(value.get()); }
|
|
inline auto& operator=(const bool value) { return set(value); }
|
|
|
|
inline auto& operator&=(const bool value) { return set(get() & value); }
|
|
inline auto& operator|=(const bool value) { return set(get() | value); }
|
|
inline auto& operator^=(const bool value) { return set(get() ^ value); }
|
|
|
|
inline auto raise() { return get() == 0 ? set(1), true : false; }
|
|
inline auto lower() { return get() == 1 ? set(0), true : false; }
|
|
inline auto& invert() { return set(get() ^ 1); }
|
|
|
|
private:
|
|
utype data;
|
|
|
|
inline auto get() const -> bool {
|
|
return data & mask;
|
|
}
|
|
|
|
inline auto set(bool value) -> BooleanBitField& {
|
|
return data = (data & ~mask) | (value << bit), *this;
|
|
}
|
|
};
|
|
|
|
template<typename Type, uint Lo, uint Hi> struct NaturalBitField {
|
|
enum : uint { lo = Lo <= Hi ? Lo : Hi };
|
|
enum : uint { hi = Hi >= Lo ? Hi : Lo };
|
|
enum : uint { bits = hi - lo + 1 };
|
|
enum : uint { mask = (~0ull >> (64 - bits)) << lo };
|
|
using type = Type;
|
|
using utype = typename std::make_unsigned<type>::type;
|
|
static_assert(hi < sizeof(type) * 8, "");
|
|
|
|
inline NaturalBitField() = default;
|
|
inline NaturalBitField(const NaturalBitField& value) { set(value.data); }
|
|
template<typename T> inline NaturalBitField(const T& value) { set(value << lo); }
|
|
|
|
inline explicit operator bool() const { return data & mask; }
|
|
inline operator utype() const { return get(); }
|
|
|
|
inline auto& operator=(const NaturalBitField& value) { return set(value.data); }
|
|
template<typename T> inline auto& operator=(T value) { return set(value << lo); }
|
|
|
|
inline auto operator++(int) { utype value = get(); set(data + (1 << lo)); return value; }
|
|
inline auto operator--(int) { utype value = get(); set(data - (1 << lo)); return value; }
|
|
|
|
inline auto& operator++() { return set(data + (1 << lo)); }
|
|
inline auto& operator--() { return set(data - (1 << lo)); }
|
|
|
|
inline auto& operator &=(const utype value) { return set(data & (value << lo)); }
|
|
inline auto& operator |=(const utype value) { return set(data | (value << lo)); }
|
|
inline auto& operator ^=(const utype value) { return set(data ^ (value << lo)); }
|
|
inline auto& operator<<=(const utype value) { return set((data & mask) << value); }
|
|
inline auto& operator>>=(const utype value) { return set((data & mask) >> value); }
|
|
inline auto& operator +=(const utype value) { return set(data + (value << lo)); }
|
|
inline auto& operator -=(const utype value) { return set(data - (value << lo)); }
|
|
inline auto& operator *=(const utype value) { return set((get() * value) << lo); }
|
|
inline auto& operator /=(const utype value) { return set((get() / value) << lo); }
|
|
inline auto& operator %=(const utype value) { return set((get() % value) << lo); }
|
|
|
|
private:
|
|
utype data;
|
|
|
|
inline auto get() const -> utype {
|
|
return (data & mask) >> lo;
|
|
}
|
|
|
|
inline auto set(utype value) -> NaturalBitField& {
|
|
return data = (data & ~mask) | (value & mask), *this;
|
|
}
|
|
};
|
|
|
|
template<typename Type, uint Lo, uint Hi> struct IntegerBitField {
|
|
enum : uint { lo = Lo <= Hi ? Lo : Hi };
|
|
enum : uint { hi = Hi >= Lo ? Hi : Lo };
|
|
enum : uint { bits = hi - lo + 1 };
|
|
enum : uint { mask = (~0ull >> (64 - bits)) << lo };
|
|
using type = Type;
|
|
using stype = typename std::make_signed<type>::type;
|
|
using utype = typename std::make_unsigned<type>::type;
|
|
static_assert(hi < sizeof(type) * 8, "");
|
|
|
|
inline IntegerBitField() = default;
|
|
inline IntegerBitField(const IntegerBitField& value) { set(value.get()); }
|
|
template<typename T> inline IntegerBitField(const T& value) { set(value); }
|
|
|
|
inline explicit operator bool() const { return data & mask; }
|
|
inline operator stype() const { return get(); }
|
|
|
|
inline auto& operator=(const IntegerBitField& value) { return set(value.get()); }
|
|
template<typename T> inline auto& operator=(const T& value) { return set(value); }
|
|
|
|
inline auto operator++(int) { stype value = get(); set(value + 1); return value; }
|
|
inline auto operator--(int) { stype value = get(); set(value - 1); return value; }
|
|
|
|
inline auto& operator++() { return set(get() + 1); }
|
|
inline auto& operator--() { return set(get() - 1); }
|
|
|
|
inline auto& operator &=(const stype value) { return set(get() & value); }
|
|
inline auto& operator |=(const stype value) { return set(get() | value); }
|
|
inline auto& operator ^=(const stype value) { return set(get() ^ value); }
|
|
inline auto& operator<<=(const stype value) { return set(get() << value); }
|
|
inline auto& operator>>=(const stype value) { return set(get() >> value); }
|
|
inline auto& operator +=(const stype value) { return set(get() + value); }
|
|
inline auto& operator -=(const stype value) { return set(get() - value); }
|
|
inline auto& operator *=(const stype value) { return set(get() * value); }
|
|
inline auto& operator /=(const stype value) { return set(get() / value); }
|
|
inline auto& operator %=(const stype value) { return set(get() % value); }
|
|
|
|
private:
|
|
utype data;
|
|
|
|
inline auto get() const -> stype {
|
|
enum : utype { b = 1ull << (bits - 1) };
|
|
enum : utype { m = b * 2 - 1 };
|
|
return ((((data & mask) >> lo) & m) ^ b) - b;
|
|
}
|
|
|
|
inline auto set(utype value) -> IntegerBitField& {
|
|
return data = (data & ~mask) | ((value << lo) & mask), *this;
|
|
}
|
|
};
|
|
|
|
}
|