| 8 | { |
| 9 | template<typename TValue> |
| 10 | struct SpeedTemplate |
| 11 | { |
| 12 | using ValueType = TValue; |
| 13 | |
| 14 | ValueType value; |
| 15 | constexpr ValueType getRaw() const { return value; } |
| 16 | constexpr SpeedTemplate() = default; |
| 17 | constexpr explicit SpeedTemplate(ValueType val) |
| 18 | : value(val) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | // Conversion function (only valid to more accuracy) |
| 23 | template<typename T> |
| 24 | constexpr operator SpeedTemplate<T>() const |
| 25 | { |
| 26 | static_assert(sizeof(T) > sizeof(ValueType)); |
| 27 | constexpr auto shift = (sizeof(T) - sizeof(ValueType)) * 8; |
| 28 | return SpeedTemplate<T>(getRaw() * (1 << shift)); |
| 29 | } |
| 30 | |
| 31 | // Best accuracy between ThisType and OtherType |
| 32 | template<typename OtherType> |
| 33 | using BestValue = std::conditional_t<(sizeof(ValueType) > sizeof(OtherType)), ValueType, OtherType>; |
| 34 | template<typename OtherType> |
| 35 | using BestType = SpeedTemplate<BestValue<OtherType>>; |
| 36 | |
| 37 | template<typename RhsT> |
| 38 | constexpr bool operator==(SpeedTemplate<RhsT> const& rhs) const |
| 39 | { |
| 40 | return BestType<RhsT>(*this).value == BestType<RhsT>(rhs).value; |
| 41 | } |
| 42 | template<typename RhsT> |
| 43 | constexpr std::strong_ordering operator<=>(SpeedTemplate<RhsT> const& rhs) const |
| 44 | { |
| 45 | return BestType<RhsT>(*this).value <=> BestType<RhsT>(rhs).value; |
| 46 | } |
| 47 | |
| 48 | constexpr SpeedTemplate& operator-() |
| 49 | { |
| 50 | value = -value; |
| 51 | return *this; |
| 52 | } |
| 53 | constexpr SpeedTemplate& operator+=(SpeedTemplate const& rhs) |
| 54 | { |
| 55 | value += rhs.value; |
| 56 | return *this; |
| 57 | } |
| 58 | constexpr SpeedTemplate& operator-=(SpeedTemplate const& rhs) |
| 59 | { |
| 60 | value -= rhs.value; |
| 61 | return *this; |
| 62 | } |
| 63 | template<typename RhsT> |
| 64 | constexpr BestType<RhsT> operator+(SpeedTemplate<RhsT> const& rhs) const |
| 65 | { |
| 66 | return BestType<RhsT>(BestType<RhsT>(*this).value + BestType<RhsT>(rhs).value); |
| 67 | } |