Skip to content

Commit 86c8236

Browse files
authored
Merge pull request #683 from OpenVicProject/restrict_fixed_point_t_constructor_to_integral_max_size_4
Restrict fixed_point_t constructor to integral_max_size_4
2 parents 26205ac + 54dd70c commit 86c8236

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <algorithm>
66
#include <cstddef>
77
#include <cstdint>
8+
#include <limits>
89

910
#include <type_safe/strong_typedef.hpp>
1011

@@ -1839,7 +1840,8 @@ void CountryInstance::update_gamestate(const Date today, MapInstance& map_instan
18391840
}
18401841

18411842
if (occupied_provinces_proportion != 0) {
1842-
occupied_provinces_proportion /= owned_provinces.size();
1843+
assert(owned_provinces.size() <= std::numeric_limits<int32_t>::max());
1844+
occupied_provinces_proportion /= static_cast<int32_t>(owned_provinces.size());
18431845
}
18441846

18451847
if (capital != nullptr) {

src/openvic-simulation/economy/BuildingInstance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void BuildingInstance::update_gamestate(Date today) {
3232
end_date = start_date + building_type.get_build_time();
3333
break;
3434
case ExpansionState::Expanding:
35-
expansion_progress = fixed_point_t { static_cast<int32_t>((today - start_date).to_int()) } / (end_date - start_date).to_int();
35+
expansion_progress = fixed_point_t { static_cast<int32_t>((today - start_date).to_int()) }
36+
/ static_cast<int32_t>((end_date - start_date).to_int());
3637
break;
3738
default: expansion_state = _can_expand() ? ExpansionState::CanExpand : ExpansionState::CannotExpand;
3839
}

src/openvic-simulation/economy/production/ArtisanalProducer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ArtisanalProducerDeps.hpp"
33

44
#include <cstddef>
5+
#include <limits>
56

67
#include <type_safe/strong_typedef.hpp>
78

@@ -521,6 +522,7 @@ ProductionType const* ArtisanalProducer::pick_production_type(
521522
);
522523

523524
fixed_point_t relative_score = ranked_artisanal_production_types.empty() ? fixed_point_t::_1 : fixed_point_t::_0;
525+
assert(ranked_artisanal_production_types.size() < std::numeric_limits<int32_t>::max());
524526
for (auto it = ranked_artisanal_production_types.begin(); it < ranked_artisanal_production_types.end(); ++it) {
525527
auto const& [production_type, score_estimate] = *it;
526528
size_t i = it - ranked_artisanal_production_types.begin();
@@ -532,14 +534,15 @@ ProductionType const* ArtisanalProducer::pick_production_type(
532534
relative_score = (
533535
(current_score - score_estimate)
534536
/ (previous_score_estimate - score_estimate)
535-
+ ranked_artisanal_production_types.size() - i
536-
) / (1 + ranked_artisanal_production_types.size());
537+
+ static_cast<int32_t>(ranked_artisanal_production_types.size() - i)
538+
) / static_cast<int32_t>(1 + ranked_artisanal_production_types.size());
537539
}
538540
break;
539541
}
540542

541543
if (current_score == score_estimate) {
542-
relative_score = fixed_point_t::parse(ranked_artisanal_production_types.size() - i) / (1 + ranked_artisanal_production_types.size());
544+
relative_score = fixed_point_t::parse(ranked_artisanal_production_types.size() - i)
545+
/ static_cast<int32_t>(1 + ranked_artisanal_production_types.size());
543546
}
544547
}
545548

src/openvic-simulation/economy/production/ArtisanalProducer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <cstdint>
34
#include <optional>
45

56
#include "openvic-simulation/types/IndexedFlatMap.hpp"
@@ -91,7 +92,7 @@ namespace OpenVic {
9192
private:
9293
CountryInstance* const country_to_report_economy_nullable;
9394
memory::vector<fixed_point_t>& demand_per_input;
94-
size_t distinct_goods_to_buy = 0;
95+
int32_t distinct_goods_to_buy = 0;
9596
Fraction inputs_bought_fraction;
9697
MarketInstance const& market_instance;
9798
memory::vector<fixed_point_t>& max_price_per_input;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <cassert>
4-
#include <cctype>
54
#include <charconv>
65
#include <concepts>
76
#include <cstddef>
@@ -78,7 +77,8 @@ namespace OpenVic {
7877

7978
public:
8079
OV_ALWAYS_INLINE constexpr fixed_point_t() : value { 0 } {}
81-
OV_ALWAYS_INLINE constexpr fixed_point_t(int32_t new_value) : value { static_cast<value_type>(new_value) << PRECISION } {}
80+
template<integral_max_size_4 T>
81+
OV_ALWAYS_INLINE constexpr fixed_point_t(T new_value) : value { static_cast<value_type>(new_value) << PRECISION } {}
8282

8383
static const fixed_point_t max;
8484
static const fixed_point_t min;
@@ -238,7 +238,7 @@ namespace OpenVic {
238238
return truncate();
239239
}
240240

241-
return floor<value_type>();
241+
return floor<int32_t>();
242242
}
243243

244244
template<std::integral T>
@@ -255,7 +255,7 @@ namespace OpenVic {
255255
return truncate();
256256
}
257257

258-
return ceil<value_type>();
258+
return ceil<int32_t>();
259259
}
260260

261261
OV_SPEED_INLINE constexpr fixed_point_t round() const {

tests/src/core/random/WeightedSampling.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ TEST_CASE("WeightedSampling limits", "[WeightedSampling]") {
1818
const fixed_point_t weights_sum = fixed_point_t::usable_max;
1919
constexpr size_t perfect_divisor = 257; //4294967295 is perfectly divisible by 257
2020
std::array<fixed_point_t, perfect_divisor> weights {};
21+
assert(weights.size() < std::numeric_limits<int32_t>::max());
2122
constexpr uint32_t step_size = max_random_value / weights.size();
22-
weights.fill(weights_sum / weights.size());
23+
weights.fill(weights_sum / static_cast<int32_t>(weights.size()));
2324
for (size_t i = 0; i < weights.size(); ++i) {
2425
CHECK(sample_weighted_index(
2526
i * step_size,
@@ -33,8 +34,9 @@ TEST_CASE("WeightedSampling weights", "[WeightedSampling]") {
3334
std::array<fixed_point_t, max_length> weights {};
3435

3536
fixed_point_t weights_sum = 0;
37+
assert(weights.size() < std::numeric_limits<int32_t>::max());
3638
for (size_t i = 0; i < weights.size(); ++i) {
37-
const fixed_point_t weight = 1+i;
39+
const fixed_point_t weight = static_cast<int32_t>(1+i);
3840
weights_sum += weight;
3941
weights[i] = weight;
4042
}

0 commit comments

Comments
 (0)