Skip to content

Commit b2fee04

Browse files
committed
Apply clang-tidy fixes
Move fixed point LUT generator scripts to misc/scripts
1 parent 5751583 commit b2fee04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+599
-1382
lines changed

misc/scripts/exp_lut_generator.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python
2+
import os
3+
from argparse import ArgumentParser
4+
from math import e
5+
from typing import List
6+
7+
BIT_COUNT = 64
8+
MAX_VALUE = 2**BIT_COUNT
9+
10+
DEFAULT_DIVISOR_BASE = 2
11+
DEFAULT_DIVISOR_POWER = 16
12+
DEFAULT_EXP_BASE = e
13+
14+
15+
def generate_exp_lut(divisor_base: int, divisor_power: int, exp_base: float):
16+
divisor: int = divisor_base**divisor_power
17+
18+
exp_lut: List[int] = []
19+
20+
for index in range(BIT_COUNT):
21+
exponent = (2**index) / divisor
22+
value = int((exp_base**exponent) * divisor + 0.5)
23+
if value > MAX_VALUE:
24+
break
25+
exp_lut.append(value)
26+
27+
lut_identifier: str = (
28+
f"LUT_{divisor_base}_{divisor_power}_EXP_{'e' if exp_base == e else ('%g' % exp_base).replace('.', 'p')}"
29+
)
30+
lut_size: int = len(exp_lut)
31+
32+
generated_options = ""
33+
if (
34+
DEFAULT_DIVISOR_BASE is not divisor_base
35+
or DEFAULT_DIVISOR_POWER is not divisor_power
36+
or DEFAULT_EXP_BASE is not exp_base
37+
):
38+
generated_options += " with `"
39+
40+
if DEFAULT_DIVISOR_BASE is not divisor_base:
41+
generated_options += f"-b {divisor_base}"
42+
if DEFAULT_DIVISOR_POWER is not divisor_power or DEFAULT_EXP_BASE is not exp_base:
43+
generated_options += " "
44+
45+
if DEFAULT_DIVISOR_POWER is not divisor_power:
46+
generated_options += f"-p {divisor_power}"
47+
if DEFAULT_EXP_BASE is not exp_base:
48+
generated_options += " "
49+
50+
if DEFAULT_EXP_BASE is not exp_base:
51+
generated_options += f"-e {exp_base:g}"
52+
53+
generated_options += "`"
54+
55+
source: str = f"""// This file was generated using the `misc/scripts/exp_lut_generator.py` script{generated_options}.
56+
57+
#pragma once
58+
59+
#include <array>
60+
#include <cstddef>
61+
#include <cstdint>
62+
63+
static constexpr int64_t {lut_identifier}_DIVISOR = {divisor};
64+
static constexpr size_t {lut_identifier}_SIZE = {lut_size};
65+
66+
static constexpr std::array<int64_t, {lut_identifier}_SIZE> {lut_identifier} {{
67+
"""
68+
69+
for value in exp_lut[:-1]:
70+
source += f"\t{value},\n"
71+
72+
source += f"\t{exp_lut[-1]}\n"
73+
source += "};\n"
74+
75+
fixed_point_lut_path: str = os.path.join(
76+
os.path.dirname(__file__), f"../../src/openvic-simulation/types/fixed_point/FixedPoint{lut_identifier}.hpp"
77+
)
78+
with open(fixed_point_lut_path, "w", newline="\n") as file:
79+
file.write(source)
80+
81+
print(f"`FixedPoint{lut_identifier}.hpp` generated successfully.")
82+
83+
84+
if __name__ == "__main__":
85+
parser = ArgumentParser(
86+
prog="Fixed Point Exp LUT Generator", description="Fixed-Point Exponential Look-Up Table generator"
87+
)
88+
parser.add_argument(
89+
"-b",
90+
"--base",
91+
type=int,
92+
default=DEFAULT_DIVISOR_BASE,
93+
choices=range(2, 65),
94+
help="The base of the fixed point divisor",
95+
)
96+
parser.add_argument(
97+
"-p",
98+
"--power",
99+
type=int,
100+
default=DEFAULT_DIVISOR_POWER,
101+
choices=range(1, 65),
102+
help="The power of the fixed point divisor",
103+
)
104+
parser.add_argument(
105+
"-e",
106+
"--exp",
107+
type=float,
108+
default=DEFAULT_EXP_BASE,
109+
help="The base of the exponential function the look-up table represents",
110+
)
111+
args = parser.parse_args()
112+
113+
generate_exp_lut(args.base, args.power, args.exp)
114+
exit(0)

misc/scripts/sin_lut_generator.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
import os
3+
from argparse import ArgumentParser
4+
from math import pi, sin
5+
6+
DEFAULT_PRECISION = 16
7+
DEFAULT_COUNT = 9
8+
9+
10+
def generate_sin_lut(precision: int, count_log2: int):
11+
one = 1 << precision
12+
count = 1 << count_log2
13+
14+
SinLut = []
15+
16+
for i in range(count):
17+
angle = 2 * pi * i / count
18+
19+
sin_value = sin(angle)
20+
moved_sin = sin_value * one
21+
rounded_sin = int(moved_sin + 0.5) if moved_sin > 0 else int(moved_sin - 0.5)
22+
SinLut.append(rounded_sin)
23+
24+
SinLut.append(SinLut[0])
25+
26+
generated_options = ""
27+
if DEFAULT_PRECISION is not precision or DEFAULT_COUNT is not count_log2:
28+
generated_options += " with `"
29+
30+
if DEFAULT_PRECISION is not precision:
31+
generated_options += f"-p {precision}"
32+
if DEFAULT_COUNT is not count_log2:
33+
generated_options += " "
34+
35+
if DEFAULT_COUNT is not count_log2:
36+
generated_options += f"-c {count_log2}"
37+
38+
generated_options += "`"
39+
40+
source = f"""// This file was generated using the `misc/scripts/sin_lut_generator.py` script{generated_options}.
41+
42+
#pragma once
43+
44+
#include <cstdint>
45+
46+
static constexpr int32_t SIN_LUT_PRECISION = {precision};
47+
static constexpr int32_t SIN_LUT_COUNT_LOG2 = {count_log2};
48+
static constexpr int32_t SIN_LUT_SHIFT = SIN_LUT_PRECISION - SIN_LUT_COUNT_LOG2;
49+
50+
static constexpr int64_t SIN_LUT[(1 << SIN_LUT_COUNT_LOG2) + 1] = {{
51+
"""
52+
53+
VALS_PER_LINE = 16
54+
55+
lines = [SinLut[i : i + VALS_PER_LINE] for i in range(0, len(SinLut), VALS_PER_LINE)]
56+
57+
for line in lines[:-1]:
58+
source += f"\t{', '.join(str(value) for value in line)},\n"
59+
60+
source += f"\t{', '.join(str(value) for value in lines[-1])}\n"
61+
source += "};\n"
62+
63+
fixed_point_sin_path: str = os.path.join(
64+
os.path.dirname(__file__), "../../src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp"
65+
)
66+
with open(fixed_point_sin_path, "w", newline="\n") as file:
67+
file.write(source)
68+
69+
print("`FixedPointLUT_sin.hpp` generated successfully.")
70+
71+
72+
if __name__ == "__main__":
73+
parser = ArgumentParser(prog="Fixed Point Sin LUT Generator", description="Fixed-Point Sin Look-Up Table generator")
74+
parser.add_argument(
75+
"-p",
76+
"--precision",
77+
type=int,
78+
default=DEFAULT_PRECISION,
79+
choices=range(1, 65),
80+
help="The number of bits after the point (fractional bits)",
81+
)
82+
parser.add_argument(
83+
"-c",
84+
"--count",
85+
type=int,
86+
default=DEFAULT_COUNT,
87+
choices=range(1, 65),
88+
help="The base 2 log of the number of values in the look-up table (must be <= precision)",
89+
)
90+
args = parser.parse_args()
91+
92+
if args.precision < args.count:
93+
print("ERROR: invalid count ", args.count, " - can't be greater than precision (", args.precision, ")")
94+
exit(-1)
95+
else:
96+
generate_sin_lut(args.precision, args.count)
97+
exit(0)

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Dataloader::path_vector_t Dataloader::_lookup_files_in_dir(
108108
path_vector_t ret;
109109
struct file_entry_t {
110110
fs::path file;
111-
fs::path const* root;
111+
fs::path const* root = nullptr;
112112
};
113113
string_map_t<file_entry_t> found_files;
114114
for (fs::path const& root : roots) {

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ using namespace std::string_view_literals;
236236
ONE_OR_MORE = _MUST_APPEAR | _CAN_REPEAT
237237
} expected_count;
238238
node_callback_t callback;
239-
size_t count;
239+
size_t count = 0;
240240

241241
dictionary_entry_t(expected_count_t new_expected_count, node_callback_t&& new_callback)
242-
: expected_count { new_expected_count }, callback { MOV(new_callback) }, count { 0 } {}
242+
: expected_count { new_expected_count }, callback { MOV(new_callback) } {}
243243

244244
constexpr bool must_appear() const {
245245
return static_cast<uint8_t>(expected_count) & static_cast<uint8_t>(expected_count_t::_MUST_APPEAR);

src/openvic-simulation/dataloader/Vic2PathSearch_Windows.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#ifdef _WIN32
23

34
#include <concepts>
45
#pragma comment(lib, "advapi32.lib")
@@ -149,7 +150,7 @@ namespace OpenVic::Windows {
149150
};
150151

151152
template<either_char_type RCHAR_T, either_char_type CHAR_T, either_char_type CHAR_T2>
152-
std::basic_string<RCHAR_T> ReadRegValue(
153+
std::basic_string<RCHAR_T> ReadRegValue( //
153154
HKEY root, std::basic_string_view<CHAR_T> key, std::basic_string_view<CHAR_T2> name
154155
) {
155156
RegistryKey registry_key(root, key, name);
@@ -168,3 +169,4 @@ namespace OpenVic::Windows {
168169
return ReadRegValue<RCHAR_T>(root, key_sv, name_sv);
169170
}
170171
}
172+
#endif

src/openvic-simulation/defines/AIDefines.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,7 @@
33
using namespace OpenVic;
44
using namespace OpenVic::NodeTools;
55

6-
AIDefines::AIDefines()
7-
: colony_weight {},
8-
administrator_weight {},
9-
industryworker_weight {},
10-
educator_weight {},
11-
soldier_weight {},
12-
soldier_fraction {},
13-
capitalist_fraction {},
14-
production_weight {},
15-
spam_penalty {},
16-
one_side_max_warscore {},
17-
pop_project_investment_max_budget_factor {},
18-
relation_limit_no_alliance_offer {},
19-
naval_supply_penalty_limit {},
20-
chance_build_railroad {},
21-
chance_build_naval_base {},
22-
chance_build_fort {},
23-
chance_invest_pop_proj {},
24-
chance_foreign_invest {},
25-
tws_awareness_score_low_cap {},
26-
tws_awareness_score_aspect {},
27-
peace_base_reluctance {},
28-
peace_time_duration {},
29-
peace_time_factor {},
30-
peace_time_factor_no_goals {},
31-
peace_war_exhaustion_factor {},
32-
peace_war_direction_factor {},
33-
peace_war_direction_winning_mult {},
34-
peace_force_balance_factor {},
35-
peace_ally_base_reluctance_mult {},
36-
peace_ally_time_mult {},
37-
peace_ally_war_exhaustion_mult {},
38-
peace_ally_war_direction_mult {},
39-
peace_ally_force_balance_mult {},
40-
aggression_base {},
41-
aggression_unciv_bonus {},
42-
fleet_size {},
43-
min_fleets {},
44-
max_fleets {},
45-
time_before_disband {} {}
6+
AIDefines::AIDefines() {}
467

478
std::string_view AIDefines::get_name() const {
489
return "ai";

src/openvic-simulation/defines/AIDefines.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ namespace OpenVic {
4747
fixed_point_t PROPERTY(peace_ally_force_balance_mult);
4848
fixed_point_t PROPERTY(aggression_base);
4949
fixed_point_t PROPERTY(aggression_unciv_bonus);
50-
size_t PROPERTY(fleet_size);
51-
size_t PROPERTY(min_fleets);
52-
size_t PROPERTY(max_fleets);
50+
size_t PROPERTY(fleet_size, 0);
51+
size_t PROPERTY(min_fleets, 0);
52+
size_t PROPERTY(max_fleets, 0);
5353
Timespan PROPERTY(time_before_disband);
5454

5555
AIDefines();

0 commit comments

Comments
 (0)