old-nall/primitives.hpp

269 lines
11 KiB
C++
Raw Normal View History

2016-05-15 10:42:10 +00:00
#pragma once
#include <nall/serializer.hpp>
#include <nall/traits.hpp>
namespace nall {
struct Boolean {
inline Boolean() : data(false) {}
template<typename T> inline Boolean(const T& value) : data(value) {}
inline operator bool() const { return data; }
template<typename T> inline auto& operator=(const T& value) { data = value; return *this; }
2016-08-09 18:27:40 +00:00
inline auto flip() { return data ^= 1; }
inline auto raise() { return data == 0 ? data = 1, true : false; }
inline auto lower() { return data == 1 ? data = 0, true : false; }
2016-05-15 10:42:10 +00:00
inline auto serialize(serializer& s) { s(data); }
private:
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-07-03 11:35:22 +00:00
template<typename T> inline Natural(const T& value) { set(value); }
2016-05-15 10:42:10 +00:00
inline operator type() const { return data; }
2016-07-03 11:35:22 +00:00
template<typename T> inline auto& operator=(const T& value) { set(value); return *this; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto operator++(int) { type value = data; set(data + 1); return value; }
inline auto operator--(int) { type value = data; set(data - 1); return value; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto& operator++() { set(data + 1); return *this; }
inline auto& operator--() { set(data - 1); return *this; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto& operator &=(const type value) { set(data & value); return *this; }
inline auto& operator |=(const type value) { set(data | value); return *this; }
inline auto& operator ^=(const type value) { set(data ^ value); return *this; }
inline auto& operator<<=(const type value) { set(data << value); return *this; }
inline auto& operator>>=(const type value) { set(data >> value); return *this; }
inline auto& operator +=(const type value) { set(data + value); return *this; }
inline auto& operator -=(const type value) { set(data - value); return *this; }
inline auto& operator *=(const type value) { set(data * value); return *this; }
inline auto& operator /=(const type value) { set(data / value); return *this; }
inline auto& operator %=(const type value) { set(data % value); return *this; }
2016-05-15 10:42:10 +00:00
inline auto serialize(serializer& s) { s(data); }
struct Reference {
inline Reference(Natural& source, uint lo, uint hi) : source(source), Lo(lo), Hi(hi) {}
2016-09-03 15:58:38 +00:00
inline auto& operator=(Reference& source) { return set(source.get()); }
2016-05-15 10:42:10 +00:00
2016-08-09 18:27:40 +00:00
inline auto get() const -> type {
2016-05-15 10:42:10 +00:00
const type RangeBits = Hi - Lo + 1;
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
return (source & RangeMask) >> Lo;
}
2016-08-09 18:27:40 +00:00
inline auto& set(const type value) {
2016-05-15 10:42:10 +00:00
const type RangeBits = Hi - Lo + 1;
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
source = (source & ~RangeMask) | ((value << Lo) & RangeMask);
return *this;
}
2016-08-09 18:27:40 +00:00
inline operator type() const { return get(); }
inline auto& operator =(const type value) { return set( value); }
inline auto& operator &=(const type value) { return set(get() & value); }
inline auto& operator |=(const type value) { return set(get() | value); }
inline auto& operator ^=(const type value) { return set(get() ^ value); }
inline auto& operator<<=(const type value) { return set(get() << value); }
inline auto& operator>>=(const type value) { return set(get() >> value); }
inline auto& operator +=(const type value) { return set(get() + value); }
inline auto& operator -=(const type value) { return set(get() - value); }
inline auto& operator *=(const type value) { return set(get() * value); }
inline auto& operator /=(const type value) { return set(get() / value); }
inline auto& operator %=(const type value) { return set(get() % value); }
2016-09-03 15:58:38 +00:00
inline auto operator++(int) { auto value = get(); set(value + 1); return value; }
inline auto operator--(int) { auto value = get(); set(value - 1); return value; }
2016-08-09 18:27:40 +00:00
inline auto& operator++() { return set(get() + 1); }
inline auto& operator--() { return set(get() - 1); }
2016-05-15 10:42:10 +00:00
private:
Natural& source;
const type Lo;
const type Hi;
};
inline auto bits(uint lo, uint hi) -> Reference { return {*this, lo < hi ? lo : hi, hi > lo ? hi : lo}; }
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-08-09 18:27:40 +00:00
inline auto bits(uint lo, uint hi) const -> const Reference { return {(Natural&)*this, lo < hi ? lo : hi, hi > lo ? hi : lo}; }
inline auto bit(uint index) const -> const Reference { return {(Natural&)*this, index, index}; }
inline auto byte(uint index) const -> const Reference { return {(Natural&)*this, index * 8 + 0, index * 8 + 7}; }
inline auto clamp(uint bits) -> uintmax {
const uintmax b = 1ull << (bits - 1);
const uintmax m = b * 2 - 1;
2016-05-15 10:42:10 +00:00
return data < m ? data : m;
}
2016-08-09 18:27:40 +00:00
inline auto clip(uint bits) -> uintmax {
const uintmax b = 1ull << (bits - 1);
const uintmax m = b * 2 - 1;
2016-05-15 10:42:10 +00:00
return data & m;
}
private:
2016-07-03 11:35:22 +00:00
auto set(type value) -> void {
2016-05-15 10:42:10 +00:00
data = value & Mask;
}
type data;
};
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-07-03 11:35:22 +00:00
template<typename T> inline Integer(const T& value) { set(value); }
2016-05-15 10:42:10 +00:00
inline operator type() const { return data; }
2016-07-03 11:35:22 +00:00
template<typename T> inline auto& operator=(const T& value) { set(value); return *this; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto operator++(int) { type value = data; set(data + 1); return value; }
inline auto operator--(int) { type value = data; set(data - 1); return value; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto& operator++() { set(data + 1); return *this; }
inline auto& operator--() { set(data - 1); return *this; }
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
inline auto& operator &=(const type value) { set(data & value); return *this; }
inline auto& operator |=(const type value) { set(data | value); return *this; }
inline auto& operator ^=(const type value) { set(data ^ value); return *this; }
inline auto& operator<<=(const type value) { set(data << value); return *this; }
inline auto& operator>>=(const type value) { set(data >> value); return *this; }
inline auto& operator +=(const type value) { set(data + value); return *this; }
inline auto& operator -=(const type value) { set(data - value); return *this; }
inline auto& operator *=(const type value) { set(data * value); return *this; }
inline auto& operator /=(const type value) { set(data / value); return *this; }
inline auto& operator %=(const type value) { set(data % value); return *this; }
2016-05-15 10:42:10 +00:00
inline auto serialize(serializer& s) { s(data); }
struct Reference {
inline Reference(Integer& source, uint lo, uint hi) : source(source), Lo(lo), Hi(hi) {}
2016-09-03 15:58:38 +00:00
inline auto& operator=(const Reference& source) { return set(source.get()); }
2016-05-15 10:42:10 +00:00
2016-08-09 18:27:40 +00:00
inline auto get() const -> utype {
2016-05-15 10:42:10 +00:00
const type RangeBits = Hi - Lo + 1;
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
return ((utype)source & RangeMask) >> Lo;
}
2016-08-09 18:27:40 +00:00
inline auto& set(const utype value) {
2016-05-15 10:42:10 +00:00
const type RangeBits = Hi - Lo + 1;
const type RangeMask = (((1ull << RangeBits) - 1) << Lo) & Mask;
source = ((utype)source & ~RangeMask) | ((value << Lo) & RangeMask);
return *this;
}
2016-08-09 18:27:40 +00:00
inline operator utype() const { return get(); }
inline auto& operator =(const utype value) { return set( value); }
inline auto& operator &=(const utype value) { return set(get() & value); }
inline auto& operator |=(const utype value) { return set(get() | value); }
inline auto& operator ^=(const utype value) { return set(get() ^ value); }
inline auto& operator<<=(const utype value) { return set(get() << value); }
inline auto& operator>>=(const utype value) { return set(get() >> value); }
inline auto& operator +=(const utype value) { return set(get() + value); }
inline auto& operator -=(const utype value) { return set(get() - value); }
inline auto& operator *=(const utype value) { return set(get() * value); }
inline auto& operator /=(const utype value) { return set(get() / value); }
inline auto& operator %=(const utype value) { return set(get() % value); }
2016-09-03 15:58:38 +00:00
inline auto operator++(int) { auto value = get(); set(value + 1); return value; }
inline auto operator--(int) { auto value = get(); set(value - 1); return value; }
2016-08-09 18:27:40 +00:00
inline auto& operator++() { return set(get() + 1); }
inline auto& operator--() { return set(get() - 1); }
2016-05-15 10:42:10 +00:00
private:
Integer& source;
const uint Lo;
const uint Hi;
};
2016-08-09 18:27:40 +00:00
inline auto bits(uint lo, uint hi) -> Reference { return {*this, lo < hi ? lo : hi, hi > lo ? hi : lo}; }
2016-05-15 10:42:10 +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-08-09 18:27:40 +00:00
inline auto bits(uint lo, uint hi) const -> const Reference { return {(Integer&)*this, lo < hi ? lo : hi, hi > lo ? hi : lo}; }
inline auto bit(uint index) const -> const Reference { return {(Integer&)*this, index, index}; }
inline auto byte(uint index) const -> const Reference { return {(Integer&)*this, index * 8 + 0, index * 8 + 7}; }
inline auto clamp(uint bits) -> intmax {
const intmax b = 1ull << (bits - 1);
const intmax m = b - 1;
2016-05-15 10:42:10 +00:00
return data > m ? m : data < -b ? -b : data;
}
2016-08-09 18:27:40 +00:00
inline auto clip(uint bits) -> intmax {
const uintmax b = 1ull << (bits - 1);
const uintmax m = b * 2 - 1;
2016-05-15 10:42:10 +00:00
return ((data & m) ^ b) - b;
}
private:
2016-07-03 11:35:22 +00:00
auto set(type value) -> void {
2016-05-15 10:42:10 +00:00
data = ((value & Mask) ^ Sign) - Sign;
}
type data;
};
template<uint Bits> struct Real {
using type =
type_if<expression<Bits == 32>, float32_t,
type_if<expression<Bits == 64>, float64_t,
2016-07-09 07:51:02 +00:00
//type_if<expression<Bits == 80>, float80_t,
void>>;
2016-05-15 10:42:10 +00:00
inline Real() : data(0.0) {}
template<typename T> inline Real(const T& value) : data((type)value) {}
inline operator type() const { return data; }
template<typename T> inline auto& operator=(const T& value) { data = (type)value; return *this; }
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); }
2016-07-03 11:35:22 +00:00
private:
2016-05-15 10:42:10 +00:00
type data;
};
using boolean = nall::Boolean;
2016-07-09 07:51:02 +00:00
using natural = nall::Natural<sizeof(uint) * 8>;
2016-09-03 15:58:38 +00:00
using integer = nall::Integer<sizeof(int) * 8>;
2016-07-09 07:51:02 +00:00
using real = nall::Real<sizeof(double) * 8>;
2016-05-15 10:42:10 +00:00
2016-08-09 18:27:40 +00:00
}