2016-06-08 22:26:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
Update to v106r77 release.
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.
2019-01-13 06:25:14 +00:00
|
|
|
#include <nall/primitives.hpp>
|
|
|
|
|
2016-06-08 22:26:35 +00:00
|
|
|
namespace nall {
|
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
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 {
|
2016-06-08 22:26:35 +00:00
|
|
|
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 };
|
2016-07-01 11:58:12 +00:00
|
|
|
using type = Type;
|
|
|
|
using utype = typename std::make_unsigned<type>::type;
|
2016-06-08 22:26:35 +00:00
|
|
|
static_assert(hi < sizeof(type) * 8, "");
|
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
inline NaturalBitField() = default;
|
|
|
|
inline NaturalBitField(const NaturalBitField& value) { set(value.data); }
|
|
|
|
template<typename T> inline NaturalBitField(const T& value) { set(value << lo); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
|
|
|
inline explicit operator bool() const { return data & mask; }
|
2016-07-01 11:58:12 +00:00
|
|
|
inline operator utype() const { return get(); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
inline auto& operator=(const NaturalBitField& value) { return set(value.data); }
|
Update to v106r77 release.
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.
2019-01-13 06:25:14 +00:00
|
|
|
template<typename T> inline auto& operator=(T value) { return set(value << lo); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
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; }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
|
|
|
inline auto& operator++() { return set(data + (1 << lo)); }
|
|
|
|
inline auto& operator--() { return set(data - (1 << lo)); }
|
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
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); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
|
|
|
private:
|
2016-07-01 11:58:12 +00:00
|
|
|
utype data;
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto get() const -> utype {
|
2016-06-08 22:26:35 +00:00
|
|
|
return (data & mask) >> lo;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto set(utype value) -> NaturalBitField& {
|
2016-06-08 22:26:35 +00:00
|
|
|
return data = (data & ~mask) | (value & mask), *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
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, "");
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline IntegerBitField() = default;
|
|
|
|
inline IntegerBitField(const IntegerBitField& value) { set(value.get()); }
|
|
|
|
template<typename T> inline IntegerBitField(const T& value) { set(value); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline explicit operator bool() const { return data & mask; }
|
|
|
|
inline operator stype() const { return get(); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto& operator=(const IntegerBitField& value) { return set(value.get()); }
|
|
|
|
template<typename T> inline auto& operator=(const T& value) { return set(value); }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto operator++(int) { stype value = get(); set(value + 1); return value; }
|
|
|
|
inline auto operator--(int) { stype value = get(); set(value - 1); return value; }
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
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); }
|
2016-06-27 13:07:57 +00:00
|
|
|
|
2016-06-08 22:26:35 +00:00
|
|
|
private:
|
2016-07-01 11:58:12 +00:00
|
|
|
utype data;
|
2016-06-08 22:26:35 +00:00
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto get() const -> stype {
|
|
|
|
enum : utype { b = 1ull << (bits - 1) };
|
|
|
|
enum : utype { m = b * 2 - 1 };
|
|
|
|
return ((((data & mask) >> lo) & m) ^ b) - b;
|
2016-06-08 22:26:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:58:12 +00:00
|
|
|
inline auto set(utype value) -> IntegerBitField& {
|
|
|
|
return data = (data & ~mask) | ((value << lo) & mask), *this;
|
2016-06-08 22:26:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|