2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
#include <nall/serializer.hpp>
|
|
|
|
#include <nall/traits.hpp>
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
|
|
|
struct Boolean {
|
|
|
|
inline Boolean() : data(false) {}
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
template<typename T> inline Boolean(const T& value) : data(value) {}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline operator bool() const { return data; }
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
template<typename T> inline auto& operator=(const T& value) { data = value; return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline auto serialize(serializer& s) { s(data); }
|
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
private:
|
2015-12-06 21:11:41 +00:00
|
|
|
bool data;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<uint Bits> struct Natural {
|
|
|
|
using type =
|
|
|
|
type_if<expression<Bits <= 8>, uint8_t,
|
|
|
|
type_if<expression<Bits <= 16>, uint16_t,
|
|
|
|
type_if<expression<Bits <= 32>, uint32_t,
|
|
|
|
type_if<expression<Bits <= 64>, uint64_t,
|
|
|
|
void>>>>;
|
|
|
|
|
|
|
|
enum : type { Mask = ~0ull >> (64 - Bits) };
|
|
|
|
|
|
|
|
inline Natural() : data(0) {}
|
2016-02-16 09:32:49 +00:00
|
|
|
template<typename T> inline Natural(const T& value) { assign(value); }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline operator type() const { return data; }
|
2016-02-16 09:32:49 +00:00
|
|
|
template<typename T> inline auto& operator=(const T& value) { assign(value); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto operator++(int) { type value = data; assign(data + 1); return value; }
|
|
|
|
inline auto operator--(int) { type value = data; assign(data - 1); return value; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto& operator++() { assign(data + 1); return *this; }
|
|
|
|
inline auto& operator--() { assign(data - 1); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto& operator &=(const type value) { assign(data & value); return *this; }
|
|
|
|
inline auto& operator |=(const type value) { assign(data | value); return *this; }
|
|
|
|
inline auto& operator ^=(const type value) { assign(data ^ value); return *this; }
|
|
|
|
inline auto& operator<<=(const type value) { assign(data << value); return *this; }
|
|
|
|
inline auto& operator>>=(const type value) { assign(data >> value); return *this; }
|
|
|
|
inline auto& operator +=(const type value) { assign(data + value); return *this; }
|
|
|
|
inline auto& operator -=(const type value) { assign(data - value); return *this; }
|
|
|
|
inline auto& operator *=(const type value) { assign(data * value); return *this; }
|
|
|
|
inline auto& operator /=(const type value) { assign(data / value); return *this; }
|
|
|
|
inline auto& operator %=(const type value) { assign(data % value); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline auto serialize(serializer& s) { s(data); }
|
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
struct Reference {
|
2016-02-16 09:32:49 +00:00
|
|
|
inline Reference(Natural& source, uint lo, uint hi) : source(source), Lo(lo), Hi(hi) {}
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
|
|
|
|
inline operator type() const {
|
|
|
|
const type RangeBits = Hi - Lo + 1;
|
|
|
|
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
|
|
|
|
return (source & RangeMask) >> Lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto& operator=(const type value) {
|
|
|
|
const type RangeBits = Hi - Lo + 1;
|
|
|
|
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
|
|
|
|
source = (source & ~RangeMask) | ((value << Lo) & RangeMask);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Natural& source;
|
2016-02-16 09:32:49 +00:00
|
|
|
const type Lo;
|
|
|
|
const type Hi;
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
};
|
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto bits(uint lo, uint hi) -> Reference { return {*this, lo < hi ? lo : hi, hi > lo ? hi : lo}; }
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
inline auto bit(uint index) -> Reference { return {*this, index, index}; }
|
|
|
|
inline auto byte(uint index) -> Reference { return {*this, index * 8 + 0, index * 8 + 7}; }
|
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto clamp(uint bits) -> uintmax {
|
|
|
|
const uintmax b = 1ull << (bits - 1);
|
|
|
|
const uintmax m = b * 2 - 1;
|
|
|
|
return data < m ? data : m;
|
|
|
|
}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto clip(uint bits) -> uintmax {
|
|
|
|
const uintmax b = 1ull << (bits - 1);
|
|
|
|
const uintmax m = b * 2 - 1;
|
|
|
|
return data & m;
|
|
|
|
}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
private:
|
2016-02-16 09:32:49 +00:00
|
|
|
auto assign(type value) -> void {
|
|
|
|
data = value & Mask;
|
2015-12-06 21:11:41 +00:00
|
|
|
}
|
2016-02-16 09:32:49 +00:00
|
|
|
|
|
|
|
type data;
|
2015-12-06 21:11:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<uint Bits> struct Integer {
|
|
|
|
using type =
|
|
|
|
type_if<expression<Bits <= 8>, int8_t,
|
|
|
|
type_if<expression<Bits <= 16>, int16_t,
|
|
|
|
type_if<expression<Bits <= 32>, int32_t,
|
|
|
|
type_if<expression<Bits <= 64>, int64_t,
|
|
|
|
void>>>>;
|
|
|
|
using utype = typename Natural<Bits>::type;
|
|
|
|
|
|
|
|
enum : utype { Mask = ~0ull >> (64 - Bits), Sign = 1ull << (Bits - 1) };
|
|
|
|
|
|
|
|
inline Integer() : data(0) {}
|
2016-02-16 09:32:49 +00:00
|
|
|
template<typename T> inline Integer(const T& value) { assign(value); }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline operator type() const { return data; }
|
2016-02-16 09:32:49 +00:00
|
|
|
template<typename T> inline auto& operator=(const T& value) { assign(value); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto operator++(int) { type value = data; assign(data + 1); return value; }
|
|
|
|
inline auto operator--(int) { type value = data; assign(data - 1); return value; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto& operator++() { assign(data + 1); return *this; }
|
|
|
|
inline auto& operator--() { assign(data - 1); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto& operator &=(const type value) { assign(data & value); return *this; }
|
|
|
|
inline auto& operator |=(const type value) { assign(data | value); return *this; }
|
|
|
|
inline auto& operator ^=(const type value) { assign(data ^ value); return *this; }
|
|
|
|
inline auto& operator<<=(const type value) { assign(data << value); return *this; }
|
|
|
|
inline auto& operator>>=(const type value) { assign(data >> value); return *this; }
|
|
|
|
inline auto& operator +=(const type value) { assign(data + value); return *this; }
|
|
|
|
inline auto& operator -=(const type value) { assign(data - value); return *this; }
|
|
|
|
inline auto& operator *=(const type value) { assign(data * value); return *this; }
|
|
|
|
inline auto& operator /=(const type value) { assign(data / value); return *this; }
|
|
|
|
inline auto& operator %=(const type value) { assign(data % value); return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline auto serialize(serializer& s) { s(data); }
|
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
struct Reference {
|
2016-02-16 09:32:49 +00:00
|
|
|
inline Reference(Integer& source, uint lo, uint hi) : source(source), Lo(lo), Hi(hi) {}
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
|
|
|
|
inline operator utype() const {
|
|
|
|
const type RangeBits = Hi - Lo + 1;
|
|
|
|
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
|
|
|
|
return ((utype)source & RangeMask) >> Lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto& operator=(const utype value) {
|
|
|
|
const type RangeBits = Hi - Lo + 1;
|
|
|
|
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
|
|
|
|
source = ((utype)source & ~RangeMask) | ((value << Lo) & RangeMask);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Integer& source;
|
2016-02-16 09:32:49 +00:00
|
|
|
const uint Lo;
|
|
|
|
const uint Hi;
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline auto bits(uint lo, uint hi) -> Reference { return {*this, lo, hi}; }
|
|
|
|
inline auto bit(uint index) -> Reference { return {*this, index, index}; }
|
|
|
|
inline auto byte(uint index) -> Reference { return {*this, index * 8 + 0, index * 8 + 7}; }
|
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto clamp(uint bits) -> intmax {
|
|
|
|
const intmax b = 1ull << (bits - 1);
|
|
|
|
const intmax m = b - 1;
|
|
|
|
return data > m ? m : data < -b ? -b : data;
|
|
|
|
}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
2016-02-16 09:32:49 +00:00
|
|
|
inline auto clip(uint bits) -> intmax {
|
|
|
|
const uintmax b = 1ull << (bits - 1);
|
|
|
|
const uintmax m = b * 2 - 1;
|
|
|
|
return ((data & m) ^ b) - b;
|
|
|
|
}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
private:
|
2016-02-16 09:32:49 +00:00
|
|
|
auto assign(type value) -> void {
|
|
|
|
data = ((value & Mask) ^ Sign) - Sign;
|
2015-12-06 21:11:41 +00:00
|
|
|
}
|
2016-02-16 09:32:49 +00:00
|
|
|
|
|
|
|
type data;
|
2015-12-06 21:11:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<uint Bits> struct Real {
|
|
|
|
using type =
|
|
|
|
type_if<expression<Bits == 32>, float32_t,
|
|
|
|
type_if<expression<Bits == 64>, float64_t,
|
|
|
|
type_if<expression<Bits == 80>, float80_t,
|
|
|
|
void>>>;
|
|
|
|
|
|
|
|
inline Real() : data(0.0) {}
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
template<typename T> inline Real(const T& value) : data((type)value) {}
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline operator type() const { return data; }
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
template<typename T> inline auto& operator=(const T& value) { data = (type)value; return *this; }
|
2015-12-06 21:11:41 +00:00
|
|
|
|
|
|
|
inline auto operator++(int) { type value = data; ++data; return value; }
|
|
|
|
inline auto operator--(int) { type value = data; --data; return value; }
|
|
|
|
|
|
|
|
inline auto& operator++() { data++; return *this; }
|
|
|
|
inline auto& operator--() { data--; return *this; }
|
|
|
|
|
|
|
|
inline auto& operator+=(const type value) { data = data + value; return *this; }
|
|
|
|
inline auto& operator-=(const type value) { data = data - value; return *this; }
|
|
|
|
inline auto& operator*=(const type value) { data = data * value; return *this; }
|
|
|
|
inline auto& operator/=(const type value) { data = data / value; return *this; }
|
|
|
|
inline auto& operator%=(const type value) { data = data % value; return *this; }
|
|
|
|
|
|
|
|
inline auto serialize(serializer& s) { s(data); }
|
|
|
|
|
|
|
|
type data;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|