Skip to content

Commit 56c1951

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

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

+661
-1387
lines changed

misc/scripts/exp_lut_generator.py

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

misc/scripts/sin_lut_generator.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env python
2+
import decimal
3+
import os
4+
from argparse import ArgumentParser
5+
6+
DEFAULT_PRECISION = 16
7+
DEFAULT_COUNT = 9
8+
9+
10+
def decimal_pi() -> decimal.Decimal:
11+
"""Compute Pi to the current precision.
12+
13+
>>> print(pdecimal_pii())
14+
3.141592653589793238462643383
15+
16+
"""
17+
decimal.getcontext().prec += 2 # extra digits for intermediate steps
18+
three: decimal.Decimal = decimal.Decimal(3) # substitute "three=3.0" for regular floats
19+
lasts: decimal.Decimal = decimal.Decimal(0)
20+
t: decimal.Decimal = three
21+
n, na, d, da = 1, 0, 0, 24
22+
s: decimal.Decimal = three
23+
while s != lasts:
24+
lasts = s
25+
n, na = n + na, na + 8
26+
d, da = d + da, da + 32
27+
t = (t * n) / d
28+
s += decimal.Decimal(t)
29+
decimal.getcontext().prec -= 2
30+
return +s # unary plus applies the new precision
31+
32+
33+
def decimal_sin(x: decimal.Decimal) -> decimal.Decimal:
34+
"""Return the sine of x as measured in radians.
35+
36+
The Taylor series approximation works best for a small value of x.
37+
For larger values, first compute x = x % (2 * pi).
38+
39+
>>> print(decimal_sin(Decimal('0.5')))
40+
0.4794255386042030002732879352
41+
>>> print(decimal_sin(0.5))
42+
0.479425538604
43+
>>> print(decimal_sin(0.5+0j))
44+
(0.479425538604+0j)
45+
46+
"""
47+
decimal.getcontext().prec += 2
48+
i, fact, num, sign = 1, 1, x, 1
49+
s: decimal.Decimal = x
50+
lasts: decimal.Decimal = decimal.Decimal(0)
51+
while s != lasts:
52+
lasts = s
53+
i += 2
54+
fact *= i * (i - 1)
55+
num *= x * x
56+
sign *= -1
57+
s += num / fact * sign
58+
decimal.getcontext().prec -= 2
59+
return +s
60+
61+
62+
def generate_sin_lut(precision: int, count_log2: int):
63+
one = 1 << precision
64+
count = 1 << count_log2
65+
66+
SinLut = []
67+
68+
for i in range(count):
69+
angle: decimal.Decimal = 2 * decimal_pi() * i / count
70+
71+
sin_value: decimal.Decimal = decimal_sin(angle) # sin(angle)
72+
moved_sin: decimal.Decimal = sin_value * one
73+
rounded_sin: int = (
74+
int(moved_sin + decimal.Decimal(0.5)) if moved_sin > 0 else int(moved_sin - decimal.Decimal(0.5))
75+
)
76+
SinLut.append(rounded_sin)
77+
78+
SinLut.append(SinLut[0])
79+
80+
generated_options = ""
81+
if DEFAULT_PRECISION is not precision or DEFAULT_COUNT is not count_log2:
82+
generated_options += " with `"
83+
84+
if DEFAULT_PRECISION is not precision:
85+
generated_options += f"-p {precision}"
86+
if DEFAULT_COUNT is not count_log2:
87+
generated_options += " "
88+
89+
if DEFAULT_COUNT is not count_log2:
90+
generated_options += f"-c {count_log2}"
91+
92+
generated_options += "`"
93+
94+
source = f"""// This file was generated using the `misc/scripts/sin_lut_generator.py` script{generated_options}.
95+
96+
#pragma once
97+
98+
#include <array>
99+
#include <cstdint>
100+
101+
static constexpr uint32_t SIN_LUT_PRECISION = {precision};
102+
static constexpr uint32_t SIN_LUT_COUNT_LOG2 = {count_log2};
103+
static constexpr int32_t SIN_LUT_SHIFT = SIN_LUT_PRECISION - SIN_LUT_COUNT_LOG2;
104+
105+
static constexpr std::array<int64_t, (1 << SIN_LUT_COUNT_LOG2) + 1> SIN_LUT = {{
106+
"""
107+
108+
VALS_PER_LINE = 16
109+
110+
lines = [SinLut[i : i + VALS_PER_LINE] for i in range(0, len(SinLut), VALS_PER_LINE)]
111+
112+
for line in lines[:-1]:
113+
source += f"\t{', '.join(str(value) for value in line)},\n"
114+
115+
source += f"\t{', '.join(str(value) for value in lines[-1])}\n"
116+
source += "};\n"
117+
118+
fixed_point_sin_path: str = os.path.join(
119+
os.path.dirname(__file__), "../../src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp"
120+
)
121+
with open(fixed_point_sin_path, "w", newline="\n") as file:
122+
file.write(source)
123+
124+
print("`FixedPointLUT_sin.hpp` generated successfully.")
125+
126+
127+
if __name__ == "__main__":
128+
parser = ArgumentParser(prog="Fixed Point Sin LUT Generator", description="Fixed-Point Sin Look-Up Table generator")
129+
parser.add_argument(
130+
"-p",
131+
"--precision",
132+
type=int,
133+
default=DEFAULT_PRECISION,
134+
choices=range(1, 65),
135+
help="The number of bits after the point (fractional bits)",
136+
)
137+
parser.add_argument(
138+
"-c",
139+
"--count",
140+
type=int,
141+
default=DEFAULT_COUNT,
142+
choices=range(1, 65),
143+
help="The base 2 log of the number of values in the look-up table (must be <= precision)",
144+
)
145+
args = parser.parse_args()
146+
147+
if args.precision < args.count:
148+
print("ERROR: invalid count ", args.count, " - can't be greater than precision (", args.precision, ")")
149+
exit(-1)
150+
else:
151+
generate_sin_lut(args.precision, args.count)
152+
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)