Skip to content

Commit 26205ac

Browse files
authored
Merge pull request #682 from OpenVicProject/restrict_fixed_point_operators_to_int32
Restrict fixed_point_t operators to integral_max_size_4
2 parents 55c064f + 04c27ac commit 26205ac

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/openvic-simulation/core/template/Concepts.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ namespace OpenVic {
142142
{ typename Case::equal {}(identifier, identifier) } -> std::same_as<bool>;
143143
};
144144

145+
template<typename T>
146+
concept integral_max_size_4 = std::integral<T> && sizeof(T) <= 4;
147+
145148
template<typename T>
146149
concept unary_negatable = requires(T const& a) {
147150
{ -a } -> std::same_as<T>;

src/openvic-simulation/types/fixed_point/FixedPoint.hpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "openvic-simulation/utility/Containers.hpp"
2323
#include "openvic-simulation/core/Typedefs.hpp"
2424
#include "openvic-simulation/core/Math.hpp"
25+
#include "openvic-simulation/core/template/Concepts.hpp"
2526

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

109110
// Standard for constexpr requires this here
110-
template<std::integral T>
111+
template<integral_max_size_4 T>
111112
OV_SPEED_INLINE constexpr friend fixed_point_t operator/(fixed_point_t const& lhs, T const& rhs) {
112113
return parse_raw(lhs.value / rhs);
113114
}
114115

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

120-
template<std::integral T>
121+
template<integral_max_size_4 T>
121122
OV_SPEED_INLINE constexpr fixed_point_t& operator/=(T const& obj) {
122123
value /= obj;
123124
return *this;
124125
}
125126

126-
template<std::integral T>
127+
template<integral_max_size_4 T>
127128
OV_SPEED_INLINE constexpr friend fixed_point_t operator*(fixed_point_t const& lhs, T const& rhs) {
128129
return parse_raw(lhs.value * rhs);
129130
}
130131

131-
template<std::integral T>
132+
template<integral_max_size_4 T>
132133
OV_SPEED_INLINE constexpr fixed_point_t& operator*=(T const& obj) {
133134
value *= obj;
134135
return *this;
135136
}
136137

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

142-
template<std::integral T>
143+
template<integral_max_size_4 T>
143144
OV_SPEED_INLINE constexpr std::strong_ordering operator<=>(T const& rhs) const {
144145
return value <=> static_cast<value_type>(rhs) << PRECISION;
145146
}
@@ -525,7 +526,7 @@ namespace OpenVic {
525526
return parse_raw(lhs.value + rhs.value);
526527
}
527528

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

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

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

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

563-
template<std::integral T>
564+
template<integral_max_size_4 T>
564565
OV_SPEED_INLINE constexpr fixed_point_t& operator-=(T const& obj) {
565566
value -= (static_cast<value_type>(obj) << PRECISION);
566567
return *this;
@@ -588,12 +589,12 @@ namespace OpenVic {
588589
return old;
589590
}
590591

591-
template<std::integral T>
592+
template<integral_max_size_4 T>
592593
OV_SPEED_INLINE constexpr friend fixed_point_t operator<<(fixed_point_t const& lhs, T const& rhs) {
593594
return parse_raw(lhs.value << rhs);
594595
}
595596

596-
template<std::integral T>
597+
template<integral_max_size_4 T>
597598
OV_SPEED_INLINE constexpr friend fixed_point_t operator>>(fixed_point_t const& lhs, T const& rhs) {
598599
return parse_raw(lhs.value >> rhs);
599600
}
@@ -602,7 +603,7 @@ namespace OpenVic {
602603
return parse_raw(lhs.value * rhs.value >> PRECISION);
603604
}
604605

605-
template<std::integral T>
606+
template<integral_max_size_4 T>
606607
OV_SPEED_INLINE constexpr friend fixed_point_t operator*(T const& lhs, fixed_point_t const& rhs) {
607608
return parse_raw(lhs * rhs.value);
608609
}
@@ -631,12 +632,12 @@ namespace OpenVic {
631632
return parse_raw(lhs.value % rhs.value);
632633
}
633634

634-
template<std::integral T>
635+
template<integral_max_size_4 T>
635636
OV_SPEED_INLINE constexpr friend fixed_point_t operator%(fixed_point_t const& lhs, T const& rhs) {
636637
return parse_raw(lhs.value % (static_cast<value_type>(rhs) << PRECISION));
637638
}
638639

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

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

659-
template<std::integral T>
660+
template<integral_max_size_4 T>
660661
OV_SPEED_INLINE constexpr bool operator==(T const& rhs) const {
661662
return value == static_cast<value_type>(rhs) << PRECISION;
662663
}

0 commit comments

Comments
 (0)