Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/openvic-simulation/core/template/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ namespace OpenVic {
{ typename Case::equal {}(identifier, identifier) } -> std::same_as<bool>;
};

template<typename T>
concept integral_max_size_4 = std::integral<T> && sizeof(T) <= 4;

template<typename T>
concept unary_negatable = requires(T const& a) {
{ -a } -> std::same_as<T>;
Expand Down
39 changes: 20 additions & 19 deletions src/openvic-simulation/types/fixed_point/FixedPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "openvic-simulation/utility/Containers.hpp"
#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/core/Math.hpp"
#include "openvic-simulation/core/template/Concepts.hpp"

/* Sin lookup table */
#include "openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp"
Expand Down Expand Up @@ -107,39 +108,39 @@ namespace OpenVic {
static const fixed_point_t e;

// Standard for constexpr requires this here
template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator/(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value / rhs);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator/(T const& lhs, fixed_point_t const& rhs) {
return parse_raw((static_cast<value_type>(lhs) << (2 * PRECISION)) / rhs.value);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr fixed_point_t& operator/=(T const& obj) {
value /= obj;
return *this;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator*(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value * rhs);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr fixed_point_t& operator*=(T const& obj) {
value *= obj;
return *this;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator+(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value + (static_cast<value_type>(rhs) << PRECISION));
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr std::strong_ordering operator<=>(T const& rhs) const {
return value <=> static_cast<value_type>(rhs) << PRECISION;
}
Expand Down Expand Up @@ -525,7 +526,7 @@ namespace OpenVic {
return parse_raw(lhs.value + rhs.value);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator+(T const& lhs, fixed_point_t const& rhs) {
return parse_raw((static_cast<value_type>(lhs) << PRECISION) + rhs.value);
}
Expand All @@ -535,7 +536,7 @@ namespace OpenVic {
return *this;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr fixed_point_t& operator+=(T const& obj) {
value += (static_cast<value_type>(obj) << PRECISION);
return *this;
Expand All @@ -545,12 +546,12 @@ namespace OpenVic {
return parse_raw(lhs.value - rhs.value);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator-(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value - (static_cast<value_type>(rhs) << PRECISION));
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator-(T const& lhs, fixed_point_t const& rhs) {
return parse_raw((static_cast<value_type>(lhs) << PRECISION) - rhs.value);
}
Expand All @@ -560,7 +561,7 @@ namespace OpenVic {
return *this;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr fixed_point_t& operator-=(T const& obj) {
value -= (static_cast<value_type>(obj) << PRECISION);
return *this;
Expand Down Expand Up @@ -588,12 +589,12 @@ namespace OpenVic {
return old;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator<<(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value << rhs);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator>>(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value >> rhs);
}
Expand All @@ -602,7 +603,7 @@ namespace OpenVic {
return parse_raw(lhs.value * rhs.value >> PRECISION);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator*(T const& lhs, fixed_point_t const& rhs) {
return parse_raw(lhs * rhs.value);
}
Expand Down Expand Up @@ -631,12 +632,12 @@ namespace OpenVic {
return parse_raw(lhs.value % rhs.value);
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator%(fixed_point_t const& lhs, T const& rhs) {
return parse_raw(lhs.value % (static_cast<value_type>(rhs) << PRECISION));
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr friend fixed_point_t operator%(T const& lhs, fixed_point_t const& rhs) {
return parse_raw((static_cast<value_type>(lhs) << PRECISION) % rhs.value);
}
Expand All @@ -646,7 +647,7 @@ namespace OpenVic {
return *this;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr fixed_point_t& operator%=(T const& obj) {
value %= (static_cast<value_type>(obj) << PRECISION);
return *this;
Expand All @@ -656,7 +657,7 @@ namespace OpenVic {
return lhs.value == rhs.value;
}

template<std::integral T>
template<integral_max_size_4 T>
OV_SPEED_INLINE constexpr bool operator==(T const& rhs) const {
return value == static_cast<value_type>(rhs) << PRECISION;
}
Expand Down