From ce49dcf8b9da3202be5972f0ceb5a064aa063399 Mon Sep 17 00:00:00 2001 From: ewarchul Date: Sat, 29 Jun 2024 11:54:25 +0200 Subject: [PATCH] Introduce shorter numeric type aliases and resolve implicit conversion issues --- .../cecxx/benchmark/detail/cec2017/consts.hpp | 2 +- .../benchmark/detail/cec2017/evaluate.hpp | 6 +-- include/cecxx/benchmark/detail/context.hpp | 23 +++++------ include/cecxx/benchmark/detail/func_types.hpp | 8 ++-- include/cecxx/benchmark/detail/geom.hpp | 4 +- .../detail/legacy/affine_transformation.hpp | 16 ++++---- .../functions/multimodal/bi_rastrigin.hpp | 26 ++++++------- .../legacy/functions/multimodal/schaffer.hpp | 14 +++---- .../cecxx/benchmark/detail/problem/hybrid.hpp | 4 +- include/cecxx/benchmark/detail/types.hpp | 7 +++- include/cecxx/benchmark/detail/util.hpp | 2 +- include/cecxx/benchmark/evaluator.hpp | 6 +-- include/cecxx/executor/main.cpp | 10 +++++ include/cecxx/functions/multimodal/ackley.hpp | 12 +++--- .../functions/multimodal/diff_powers.hpp | 4 +- include/cecxx/functions/multimodal/discus.hpp | 4 +- .../cecxx/functions/multimodal/escaffer.hpp | 6 ++- .../cecxx/functions/multimodal/grie_rosen.hpp | 8 ++-- .../cecxx/functions/multimodal/griewank.hpp | 4 +- .../cecxx/functions/multimodal/happycat.hpp | 11 ++++-- include/cecxx/functions/multimodal/hgbat.hpp | 8 ++-- .../cecxx/functions/multimodal/katsuura.hpp | 10 +++-- include/cecxx/functions/multimodal/levy.hpp | 12 +++--- .../cecxx/functions/multimodal/rastrigin.hpp | 4 +- .../cecxx/functions/multimodal/rosenbrock.hpp | 6 ++- .../cecxx/functions/multimodal/schwefel.hpp | 10 +++-- .../functions/multimodal/step_rastrigin.hpp | 6 ++- .../functions/multimodal/weierstrass.hpp | 10 +++-- .../cecxx/functions/multimodal/zakharov.hpp | 6 ++- .../cecxx/functions/unimodal/bent_cigar.hpp | 4 +- include/cecxx/functions/unimodal/ellips.hpp | 6 ++- include/cecxx/functions/unimodal/sphere.hpp | 4 +- .../cecxx/functions/unimodal/sum_diff_pow.hpp | 4 +- include/cecxx/types.hpp | 18 +++++++++ src/context.cpp | 11 +++--- src/evaluator.cpp | 2 +- src/legacy/affine_trans.cpp | 38 +++++++++---------- src/table_io.cpp | 27 ++++++------- src/table_io.hpp | 28 +++++++------- src/util.hpp | 7 ++-- 40 files changed, 237 insertions(+), 161 deletions(-) create mode 100644 include/cecxx/executor/main.cpp create mode 100644 include/cecxx/types.hpp diff --git a/include/cecxx/benchmark/detail/cec2017/consts.hpp b/include/cecxx/benchmark/detail/cec2017/consts.hpp index 8c98616..fa33cef 100644 --- a/include/cecxx/benchmark/detail/cec2017/consts.hpp +++ b/include/cecxx/benchmark/detail/cec2017/consts.hpp @@ -6,7 +6,7 @@ #include "cecxx/benchmark/detail/types.hpp" namespace cecxx::benchmark::detail::cec2017 { -const auto CEC2017_OFFSET = std::unordered_map{ +const auto CEC2017_OFFSET = std::unordered_map{ {1, 100.0}, {2, 200.0}, {3, 300.0}, {4, 400.0}, {5, 500.0}, {6, 600.0}, {7, 700.0}, {8, 800.0}, {9, 900.0}, {10, 1000.0}, {11, 1100.0}, {12, 1200.0}, {13, 1300.0}, {14, 1400.0}, {15, 1500.0}, {16, 1600.0}, {17, 1700.0}, {18, 1800.0}, {19, 1900.0}, {20, 2000.0}, {21, 2100.0}, {22, 2200.0}, {23, 2300.0}, {24, 2400.0}, diff --git a/include/cecxx/benchmark/detail/cec2017/evaluate.hpp b/include/cecxx/benchmark/detail/cec2017/evaluate.hpp index e32b3f1..eb42567 100644 --- a/include/cecxx/benchmark/detail/cec2017/evaluate.hpp +++ b/include/cecxx/benchmark/detail/cec2017/evaluate.hpp @@ -8,7 +8,7 @@ namespace cecxx::benchmark::detail::cec2017 { -auto dispatch_problem(problem_context_view ctx, const int fn, const auto& input) { +auto dispatch_problem(problem_context_view ctx, const u8 fn, const auto& input) { switch (fn) { case 1: return bent_cigar(input, ctx); @@ -75,14 +75,14 @@ auto dispatch_problem(problem_context_view ctx, const int fn, const auto& input) throw std::runtime_error{"Unknown CEC problem"}; } -auto evaluate(problem_context_view ctx, const int fn_num, const auto& input) { +auto evaluate(problem_context_view ctx, const u8 fn_num, const auto& input) { const auto ncol = input.size(); const auto nrow = input.at(0).size(); if (not cec2017::VALID_DIMENSIONS.contains(nrow)) { throw std::runtime_error{"Invalid problem dimension."}; } - auto output = std::vector(ncol); + auto output = std::vector(ncol); for (auto col{0u}; col < output.size(); ++col) { output[col] = dispatch_problem(ctx, fn_num, input.at(col)) + cec2017::CEC2017_OFFSET.at(fn_num); } diff --git a/include/cecxx/benchmark/detail/context.hpp b/include/cecxx/benchmark/detail/context.hpp index 263de6e..c96ac6d 100644 --- a/include/cecxx/benchmark/detail/context.hpp +++ b/include/cecxx/benchmark/detail/context.hpp @@ -6,31 +6,32 @@ #include "cecxx/benchmark/detail/types.hpp" #include "cecxx/benchmark/edition.hpp" +#include "cecxx/types.hpp" namespace cecxx::benchmark::detail { template using context_table_t = std::unordered_map>; struct problem_context_view { - std::span shift{}; - std::span rotate{}; - std::span shuffle{}; + std::span shift{}; + std::span rotate{}; + std::span shuffle{}; }; class context_t { public: - context_t(const cec_edition_t edition, const std::filesystem::path& storage, const int dim); + context_t(const cec_edition_t edition, const std::filesystem::path& storage, const u8 dim); - auto shift(const int fn) const { return shift_.at(fn); } - auto rotate(const int fn) const { return rotate_.at(fn); } - auto shuffle(const int fn) const { return shuffle_.at(fn); } - auto problem_context(const int fn) const { + auto shift(const u8 fn) const { return shift_.at(fn); } + auto rotate(const u8 fn) const { return rotate_.at(fn); } + auto shuffle(const u8 fn) const { return shuffle_.at(fn); } + auto problem_context(const u8 fn) const { return problem_context_view{shift_.at(fn), rotate_.at(fn), shuffle_.at(fn)}; } private: - context_table_t rotate_{}; - context_table_t shuffle_{}; - context_table_t shift_{}; + context_table_t rotate_{}; + context_table_t shuffle_{}; + context_table_t shift_{}; }; } // namespace cecxx::benchmark::detail diff --git a/include/cecxx/benchmark/detail/func_types.hpp b/include/cecxx/benchmark/detail/func_types.hpp index 74706fa..56c763c 100644 --- a/include/cecxx/benchmark/detail/func_types.hpp +++ b/include/cecxx/benchmark/detail/func_types.hpp @@ -7,9 +7,9 @@ namespace cecxx::benchmark::detail { -using stateless_eval_func = double (*)(std::span); -using local_statefull_eval_func = double (*)(std::span, problem_context_view, affine_mask_t); -using nonlocal_statefull_eval_func = double (*)(std::span, problem_context_view, affine_mask_t, - std::vector); +using stateless_eval_func = double (*)(std::span); +using local_statefull_eval_func = double (*)(std::span, problem_context_view, affine_mask_t); +using nonlocal_statefull_eval_func = double (*)(std::span, problem_context_view, affine_mask_t, + std::vector); } // namespace cecxx::benchmark::detail diff --git a/include/cecxx/benchmark/detail/geom.hpp b/include/cecxx/benchmark/detail/geom.hpp index 68b67cc..88f11b2 100644 --- a/include/cecxx/benchmark/detail/geom.hpp +++ b/include/cecxx/benchmark/detail/geom.hpp @@ -8,8 +8,8 @@ namespace cecxx::benchmark::detail { -inline auto apply_geom_transformations(std::span input, problem_context_view ctx, affine_mask_t mask, - double scale_mul = 1.0) { +inline auto apply_geom_transformations(std::span input, problem_context_view ctx, affine_mask_t mask, + f64 scale_mul = 1.0) { const auto nrow = input.size(); auto y_out = std::vector(nrow); auto z_out = std::vector(nrow); diff --git a/include/cecxx/benchmark/detail/legacy/affine_transformation.hpp b/include/cecxx/benchmark/detail/legacy/affine_transformation.hpp index 29f6dab..cb7df88 100644 --- a/include/cecxx/benchmark/detail/legacy/affine_transformation.hpp +++ b/include/cecxx/benchmark/detail/legacy/affine_transformation.hpp @@ -5,12 +5,12 @@ #include "cecxx/benchmark/detail/types.hpp" namespace cecxx::benchmark::detail { -void shufflefunc(std::span input, std::span output, std::span shuffle_vec); -void shiftfunc(std::span input, std::span output, std::span shift_vec); -void rotatefunc(std::span input, std::span output, std::span rotate_mat); -void sr_func(std::span input, std::span sr_x, std::span shit_vec, - std::span rot_mat, const double sh_rate, const do_affine_trans shift, - const do_affine_trans rotate, std::span output); -void cf_cal(std::span input, std::span output, std::span shift_vec, - std::span delta, std::span bias, std::span fit, int cf_num); +void shufflefunc(std::span input, std::span output, std::span shuffle_vec); +void shiftfunc(std::span input, std::span output, std::span shift_vec); +void rotatefunc(std::span input, std::span output, std::span rotate_mat); +void sr_func(std::span input, std::span sr_x, std::span shit_vec, + std::span rot_mat, const f64 sh_rate, const do_affine_trans shift, const do_affine_trans rotate, + std::span output); +void cf_cal(std::span input, std::span output, std::span shift_vec, + std::span delta, std::span bias, std::span fit, u8 cf_num); } // namespace cecxx::benchmark::detail diff --git a/include/cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp b/include/cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp index 5e620ca..b026858 100644 --- a/include/cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp +++ b/include/cecxx/benchmark/detail/legacy/functions/multimodal/bi_rastrigin.hpp @@ -8,13 +8,13 @@ namespace cecxx::functions::multimodal::legacy { -inline auto bi_rastrigin(std::span input, benchmark::detail::problem_context_view ctx, - benchmark::detail::affine_mask_t mask) -> double { +inline auto bi_rastrigin(std::span input, benchmark::detail::problem_context_view ctx, + benchmark::detail::affine_mask_t mask) -> f64 { using namespace cecxx::benchmark; const auto nrow = input.size(); - auto y = std::vector(nrow); - auto z = std::vector(nrow); - auto tmpx = std::vector(nrow); + auto y = std::vector(nrow); + auto z = std::vector(nrow); + auto tmpx = std::vector(nrow); if (to_underlying(mask.shift) == 1) { detail::shiftfunc(input, y, ctx.shift); @@ -41,11 +41,11 @@ inline auto bi_rastrigin(std::span input, benchmark::detail::probl tmpx[i] += mu0; } - double tmp{}; - double tmp1{}; - double tmp2{}; + f64 tmp{}; + f64 tmp1{}; + f64 tmp2{}; constexpr auto d = 1.0; - auto s = 1.0 - 1.0 / (2.0 * std::pow(static_cast(nrow) + 20.0, 0.5) - 8.2); + auto s = 1.0 - 1.0 / (2.0 * std::pow(static_cast(nrow) + 20.0, 0.5) - 8.2); auto mu1 = -std::pow((mu0 * mu0 - d) / s, 0.5); for (auto i = 0u; i < nrow; i++) { tmp = tmpx[i] - mu0; @@ -54,7 +54,7 @@ inline auto bi_rastrigin(std::span input, benchmark::detail::probl tmp2 += tmp * tmp; } tmp2 *= s; - tmp2 += d * static_cast(nrow); + tmp2 += d * static_cast(nrow); tmp = 0.0; auto output{0.0}; @@ -68,7 +68,7 @@ inline auto bi_rastrigin(std::span input, benchmark::detail::probl } else { output = tmp2; } - output += 10.0 * (static_cast(nrow) - tmp); + output += 10.0 * (static_cast(nrow) - tmp); } else { for (auto i = 0u; i < nrow; i++) { tmp += std::cos(2.0 * M_PI * z[i]); @@ -78,10 +78,10 @@ inline auto bi_rastrigin(std::span input, benchmark::detail::probl } else { output = tmp2; } - output += 10.0 * (static_cast(nrow) - tmp); + output += 10.0 * (static_cast(nrow) - tmp); } return output; } -} // namespace cecxx::problem::multimodal::legacy +} // namespace cecxx::functions::multimodal::legacy diff --git a/include/cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp b/include/cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp index 992b529..81498ca 100644 --- a/include/cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp +++ b/include/cecxx/benchmark/detail/legacy/functions/multimodal/schaffer.hpp @@ -7,27 +7,27 @@ #include "cecxx/benchmark/detail/legacy/affine_transformation.hpp" namespace cecxx::functions::multimodal::legacy { -inline auto schaffer(std::span input, benchmark::detail::problem_context_view ctx, - benchmark::detail::affine_mask_t mask, std::vector acc = {}) -> double { +inline auto schaffer(std::span input, benchmark::detail::problem_context_view ctx, + benchmark::detail::affine_mask_t mask, std::vector acc = {}) -> f64 { const auto nrow = input.size(); - std::vector ys{}; + std::vector ys{}; if (acc.empty()) { ys.resize(nrow); } else { ys = acc; } - auto z = std::vector(nrow); + auto z = std::vector(nrow); auto output{0.0}; sr_func(input, z, ctx.shift, ctx.rotate, 1.0, mask.shift, mask.rot, ys); - double tmp{}; + f64 tmp{}; for (auto i = 0u; i < nrow - 1; ++i) { z[i] = std::pow(ys[i] * ys[i] + ys[i + 1] * ys[i + 1], 0.5); tmp = std::sin(50.0 * std::pow(z[i], 0.2)); output += std::pow(z[i], 0.5) + std::pow(z[i], 0.5) * tmp * tmp; } - return output * output / (static_cast(nrow) - 1) / (static_cast(nrow) - 1); + return output * output / (static_cast(nrow) - 1) / (static_cast(nrow) - 1); } -} // namespace cecxx::problem::multimodal::legacy +} // namespace cecxx::functions::multimodal::legacy diff --git a/include/cecxx/benchmark/detail/problem/hybrid.hpp b/include/cecxx/benchmark/detail/problem/hybrid.hpp index 8c5a490..3ca12dd 100644 --- a/include/cecxx/benchmark/detail/problem/hybrid.hpp +++ b/include/cecxx/benchmark/detail/problem/hybrid.hpp @@ -13,10 +13,10 @@ auto calc_hybrid_chunks(std::ranges::range auto&& mix_ratios, const std::integra auto chunk_size = std::vector(fn_num); double acc{}; for (auto i = 0u; i < fn_num - 1; ++i) { - chunk_size[i] = std::ceil(mix_ratios[i] * dim); + chunk_size[i] = std::ceil(mix_ratios[i] * static_cast(dim)); acc += chunk_size[i]; } - chunk_size[fn_num - 1] = dim - acc; + chunk_size[fn_num - 1] = static_cast(dim) - acc; auto chunk_offset = std::vector(fn_num); for (auto i = 1u; i < fn_num; i++) { chunk_offset[i] = chunk_offset[i - 1] + chunk_size[i - 1]; diff --git a/include/cecxx/benchmark/detail/types.hpp b/include/cecxx/benchmark/detail/types.hpp index 73aaf61..2007d22 100644 --- a/include/cecxx/benchmark/detail/types.hpp +++ b/include/cecxx/benchmark/detail/types.hpp @@ -3,13 +3,16 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::benchmark::detail { template constexpr auto to_underlying(Enum e) noexcept -> std::underlying_type_t { return static_cast>(e); } -using fn_num = int; +using fn_num = u8; + template using table_data = std::vector; enum class table_type_t { rotate, shift, shuffle }; @@ -20,7 +23,7 @@ struct affine_mask_t { do_affine_trans rot{}; do_affine_trans shift{}; do_affine_trans shuffle{}; - double rate{1.0}; + f64 rate{1.0}; }; } // namespace cecxx::benchmark::detail diff --git a/include/cecxx/benchmark/detail/util.hpp b/include/cecxx/benchmark/detail/util.hpp index 9c611ca..b9a3b1e 100644 --- a/include/cecxx/benchmark/detail/util.hpp +++ b/include/cecxx/benchmark/detail/util.hpp @@ -5,7 +5,7 @@ #include "cecxx/benchmark/detail/cec2017/evaluate.hpp" namespace cecxx::benchmark::detail { -auto dispatch_cec(cec_edition_t edition, problem_context_view ctx, const int fn_num, const auto& input) { +auto dispatch_cec(cec_edition_t edition, problem_context_view ctx, const u8 fn_num, const auto& input) { switch (edition) { case cec_edition_t::cec2017: return cec2017::evaluate(ctx, fn_num, input); diff --git a/include/cecxx/benchmark/evaluator.hpp b/include/cecxx/benchmark/evaluator.hpp index 01f58e6..acb209d 100644 --- a/include/cecxx/benchmark/evaluator.hpp +++ b/include/cecxx/benchmark/evaluator.hpp @@ -13,15 +13,15 @@ namespace cecxx::benchmark { class evaluator { public: - evaluator(const cec_edition_t edition, const int dimension, + evaluator(const cec_edition_t edition, const u8 dimension, const std::filesystem::path& storage = detail::default_benchmark_datadir()); - auto operator()(const int fn, const matrix auto& input) const -> std::vector { + auto operator()(const u8 fn, const matrix auto& input) const -> std::vector { return detail::dispatch_cec(edition_, ctx_.problem_context(fn), fn, input); } private: - int dim_{}; + u8 dim_{}; detail::context_t ctx_; cec_edition_t edition_{}; }; diff --git a/include/cecxx/executor/main.cpp b/include/cecxx/executor/main.cpp new file mode 100644 index 0000000..59b88f0 --- /dev/null +++ b/include/cecxx/executor/main.cpp @@ -0,0 +1,10 @@ +// +// +// auto main() -> int { +// +// +// auto cec17_runner = cecxx::benchmark::runner(cec_2017, +// +// +// return EXIT_SUCCESS; +// } diff --git a/include/cecxx/functions/multimodal/ackley.hpp b/include/cecxx/functions/multimodal/ackley.hpp index bde1a38..fbedbf1 100644 --- a/include/cecxx/functions/multimodal/ackley.hpp +++ b/include/cecxx/functions/multimodal/ackley.hpp @@ -3,19 +3,21 @@ #include #include +#include "cecxx/types.hpp" + #define E 2.7182818284590452353602874713526625 namespace cecxx::functions::multimodal { -constexpr auto ackley(std::span input) -> double { +constexpr auto ackley(std::span input) -> f64 { const auto nrow = input.size(); - double sum1{}; - double sum2{}; + f64 sum1{}; + f64 sum2{}; for (auto i = 0u; i < nrow; ++i) { sum1 += input[i] * input[i]; sum2 += std::cos(2.0 * M_PI * input[i]); } - sum1 = -0.2 * std::sqrt(sum1 / static_cast(nrow)); - sum2 /= static_cast(nrow); + sum1 = -0.2 * std::sqrt(sum1 / static_cast(nrow)); + sum2 /= static_cast(nrow); return E - 20.0 * std::exp(sum1) - std::exp(sum2) + 20.0; } } // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/diff_powers.hpp b/include/cecxx/functions/multimodal/diff_powers.hpp index ccc48d5..fb5e15a 100644 --- a/include/cecxx/functions/multimodal/diff_powers.hpp +++ b/include/cecxx/functions/multimodal/diff_powers.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto diff_powers(std::span input) -> double { +constexpr auto diff_powers(std::span input) -> f64 { auto output{0.0}; for (auto i = 0u; i < input.size(); i++) { output += std::pow(std::fabs(input[i]), 2 + 4 * i / (input.size() - 1)); diff --git a/include/cecxx/functions/multimodal/discus.hpp b/include/cecxx/functions/multimodal/discus.hpp index a5dabd3..6cdfdfa 100644 --- a/include/cecxx/functions/multimodal/discus.hpp +++ b/include/cecxx/functions/multimodal/discus.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -inline auto discus(std::span input) -> double { +inline auto discus(std::span input) -> f64 { auto output = std::pow(10.0, 6.0) * input[0] * input[0]; for (auto i = 1u; i < input.size(); i++) { output += input[i] * input[i]; diff --git a/include/cecxx/functions/multimodal/escaffer.hpp b/include/cecxx/functions/multimodal/escaffer.hpp index 9097a7e..a567dbc 100644 --- a/include/cecxx/functions/multimodal/escaffer.hpp +++ b/include/cecxx/functions/multimodal/escaffer.hpp @@ -3,11 +3,13 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto escaffer(std::span input) -> double { +constexpr auto escaffer(std::span input) -> f64 { const auto nrow = input.size(); auto output{0.0}; - double temp1{}, temp2{}; + f64 temp1{}, temp2{}; for (auto i = 0u; i < nrow - 1; i++) { temp1 = std::sin(sqrt(input[i] * input[i] + input[i + 1] * input[i + 1])); temp1 = temp1 * temp1; diff --git a/include/cecxx/functions/multimodal/grie_rosen.hpp b/include/cecxx/functions/multimodal/grie_rosen.hpp index 1428cb0..0fdeff5 100644 --- a/include/cecxx/functions/multimodal/grie_rosen.hpp +++ b/include/cecxx/functions/multimodal/grie_rosen.hpp @@ -3,10 +3,12 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto grie_rosen(std::span input) -> double { +constexpr auto grie_rosen(std::span input) -> f64 { const auto nrow = input.size(); - double temp{}, tmp1{}; + f64 temp{}, tmp1{}; auto output{0.0}; for (auto i = 0u; i < nrow - 1; i++) { tmp1 = (input[i] + 1.0) * (input[i] + 1.0) - (input[i + 1] + 1.0); @@ -14,7 +16,7 @@ constexpr auto grie_rosen(std::span input) -> double { output += (temp * temp) / 4000.0 - std::cos(temp) + 1.0; } tmp1 = (input[nrow - 1] + 1.0) * (input[nrow - 1] + 1.0) - (input[0] + 1.0); - temp = 100.0 * tmp1 * tmp1 + input[nrow-1] * input[nrow -1]; + temp = 100.0 * tmp1 * tmp1 + input[nrow - 1] * input[nrow - 1]; output += (temp * temp) / 4000.0 - std::cos(temp) + 1.0; return output; diff --git a/include/cecxx/functions/multimodal/griewank.hpp b/include/cecxx/functions/multimodal/griewank.hpp index 5243bda..2f9a48c 100644 --- a/include/cecxx/functions/multimodal/griewank.hpp +++ b/include/cecxx/functions/multimodal/griewank.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto griewank(std::span input) -> double { +constexpr auto griewank(std::span input) -> f64 { auto sum = 0.0; auto tmp = 1.0; for (auto i = 0u; i < input.size(); i++) { diff --git a/include/cecxx/functions/multimodal/happycat.hpp b/include/cecxx/functions/multimodal/happycat.hpp index 92fc0d4..a60fbf2 100644 --- a/include/cecxx/functions/multimodal/happycat.hpp +++ b/include/cecxx/functions/multimodal/happycat.hpp @@ -3,16 +3,19 @@ #include #include +#include "cecxx/types.hpp" + + namespace cecxx::functions::multimodal { -constexpr auto happycat(std::span input) -> double { +constexpr auto happycat(std::span input) -> f64 { const auto nrow = input.size(); - double r2{}, sum_z{}; + f64 r2{}, sum_z{}; constexpr auto alpha = 1.0 / 8.0; for (auto i = 0u; i < nrow; i++) { r2 += (input[i] - 1.0) * (input[i] - 1.0); sum_z += input[i]; } - return std::pow(std::fabs(r2 - static_cast(nrow)), 2 * alpha) + - (0.5 * r2 + sum_z) / static_cast(nrow) + 0.5; + return std::pow(std::fabs(r2 - static_cast(nrow)), 2 * alpha) + (0.5 * r2 + sum_z) / static_cast(nrow) + + 0.5; } } // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/hgbat.hpp b/include/cecxx/functions/multimodal/hgbat.hpp index 1f7e7a0..e1497c5 100644 --- a/include/cecxx/functions/multimodal/hgbat.hpp +++ b/include/cecxx/functions/multimodal/hgbat.hpp @@ -3,16 +3,18 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto hgbat(std::span input) -> double { +constexpr auto hgbat(std::span input) -> f64 { const auto nrow = input.size(); - double r2{}, sum_z{}; + f64 r2{}, sum_z{}; constexpr auto alpha = 1.0 / 4.0; for (auto i = 0u; i < nrow; i++) { r2 += (input[i] - 1.0) * (input[i] - 1.0); sum_z += (input[i] - 1.0); } return std::pow(std::fabs(std::pow(r2, 2.0) - std::pow(sum_z, 2.0)), 2 * alpha) + - (0.5 * r2 + sum_z) / static_cast(nrow) + 0.5; + (0.5 * r2 + sum_z) / static_cast(nrow) + 0.5; } } // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/katsuura.hpp b/include/cecxx/functions/multimodal/katsuura.hpp index 0f0c7e6..d30c457 100644 --- a/include/cecxx/functions/multimodal/katsuura.hpp +++ b/include/cecxx/functions/multimodal/katsuura.hpp @@ -3,12 +3,14 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto katsuura(std::span input) -> double { +constexpr auto katsuura(std::span input) -> f64 { const auto nrow = input.size(); auto output{1.0}; - double temp{}, tmp1{}, tmp2{}, tmp3{}; - tmp3 = std::pow(1.0 * static_cast(nrow), 1.2); + f64 temp{}, tmp1{}, tmp2{}, tmp3{}; + tmp3 = std::pow(1.0 * static_cast(nrow), 1.2); for (auto i = 0u; i < nrow; i++) { temp = 0.0; for (auto j = 1u; j <= 32; j++) { @@ -18,7 +20,7 @@ constexpr auto katsuura(std::span input) -> double { } output *= std::pow(1.0 + (i + 1) * temp, 10.0 / tmp3); } - tmp1 = 10.0 / static_cast(nrow) / static_cast(nrow); + tmp1 = 10.0 / static_cast(nrow) / static_cast(nrow); return output * tmp1 - tmp1; } diff --git a/include/cecxx/functions/multimodal/levy.hpp b/include/cecxx/functions/multimodal/levy.hpp index e0b6df0..cbce734 100644 --- a/include/cecxx/functions/multimodal/levy.hpp +++ b/include/cecxx/functions/multimodal/levy.hpp @@ -4,16 +4,18 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto levy(std::span input) -> double { +constexpr auto levy(std::span input) -> f64 { const auto nrow = input.size(); - auto w = std::vector(nrow); + auto w = std::vector(nrow); for (auto i = 0u; i < nrow; i++) { w[i] = 1.0 + (input[i] - 1.0) / 4.0; } - double term1 = std::pow((std::sin(M_PI * w[0])), 2); - double term2 = std::pow((w[nrow - 1] - 1), 2) * (1 + std::pow((std::sin(2 * M_PI * w[nrow - 1])), 2)); - double sum = 0.0; + f64 term1 = std::pow((std::sin(M_PI * w[0])), 2); + f64 term2 = std::pow((w[nrow - 1] - 1), 2) * (1 + std::pow((std::sin(2 * M_PI * w[nrow - 1])), 2)); + f64 sum = 0.0; for (auto i = 0u; i < nrow - 1; i++) { sum += std::pow((w[i] - 1), 2) * (1 + 10 * std::pow((std::sin(M_PI * w[i] + 1)), 2)); } diff --git a/include/cecxx/functions/multimodal/rastrigin.hpp b/include/cecxx/functions/multimodal/rastrigin.hpp index 0deb032..6244471 100644 --- a/include/cecxx/functions/multimodal/rastrigin.hpp +++ b/include/cecxx/functions/multimodal/rastrigin.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto rastrigin(std::span input) -> double { +constexpr auto rastrigin(std::span input) -> f64 { auto output{0.0}; for (const auto& x : input) { output += (x * x - 10.0 * std::cos(2.0 * M_PI * x) + 10.0); diff --git a/include/cecxx/functions/multimodal/rosenbrock.hpp b/include/cecxx/functions/multimodal/rosenbrock.hpp index 12c7f76..d4eb173 100644 --- a/include/cecxx/functions/multimodal/rosenbrock.hpp +++ b/include/cecxx/functions/multimodal/rosenbrock.hpp @@ -2,10 +2,12 @@ #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto rosenbrock(std::span input) -> double { +constexpr auto rosenbrock(std::span input) -> f64 { auto output{0.0}; - double tmp1{}; + f64 tmp1{}; for (auto i = 0u; i < input.size() - 1; ++i) { tmp1 = (input[i] + 1.0) * (input[i] + 1.0) - (input[i + 1] + 1.0); output += 100.0 * tmp1 * tmp1 + input[i] * input[i]; diff --git a/include/cecxx/functions/multimodal/schwefel.hpp b/include/cecxx/functions/multimodal/schwefel.hpp index bbbce96..950e903 100644 --- a/include/cecxx/functions/multimodal/schwefel.hpp +++ b/include/cecxx/functions/multimodal/schwefel.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto schwefel(std::span input) -> double { +constexpr auto schwefel(std::span input) -> f64 { const auto nrow = input.size(); auto tmp{0.0}; auto output{0.0}; @@ -14,17 +16,17 @@ constexpr auto schwefel(std::span input) -> double { output -= (500.0 - std::fmod((input[i] + shift), 500)) * std::sin(std::pow(500.0 - std::fmod((input[i] + shift), 500), 0.5)); tmp = ((input[i] + shift) - 500.0) / 100; - output += tmp * tmp / static_cast(nrow); + output += tmp * tmp / static_cast(nrow); } else if ((input[i] + shift) < -500) { output -= (-500.0 + std::fmod(std::fabs((input[i]) + shift), 500)) * std::sin(std::pow(500.0 - std::fmod(std::fabs((input[i] + shift)), 500), 0.5)); tmp = ((input[i] + shift) + 500.0) / 100; - output += tmp * tmp / static_cast(nrow); + output += tmp * tmp / static_cast(nrow); } else { output -= (input[i] + shift) * std::sin(std::pow(std::fabs((input[i] + shift)), 0.5)); } } - output += 4.189828872724338e+002 * static_cast(nrow); + output += 4.189828872724338e+002 * static_cast(nrow); return output; } } // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/step_rastrigin.hpp b/include/cecxx/functions/multimodal/step_rastrigin.hpp index e474343..1caebc9 100644 --- a/include/cecxx/functions/multimodal/step_rastrigin.hpp +++ b/include/cecxx/functions/multimodal/step_rastrigin.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto step_rastrigin(std::span input) -> double { +constexpr auto step_rastrigin(std::span input) -> f64 { auto output{0.0}; for (const auto& x : input) { output += (x * x - 10.0 * std::cos(2.0 * M_PI * x) + 10.0); @@ -12,4 +14,4 @@ constexpr auto step_rastrigin(std::span input) -> double { return output; } -} // namespace cecxx::functions::unimodal +} // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/weierstrass.hpp b/include/cecxx/functions/multimodal/weierstrass.hpp index b995862..f68e50a 100644 --- a/include/cecxx/functions/multimodal/weierstrass.hpp +++ b/include/cecxx/functions/multimodal/weierstrass.hpp @@ -3,16 +3,18 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto weierstrass(std::span input) -> double { +constexpr auto weierstrass(std::span input) -> f64 { const auto nrow = input.size(); constexpr auto a{0.5}; constexpr auto b{3.0}; constexpr auto k_max{20}; auto output{0.0}; - double sum{}; - double sum2{}; + f64 sum{}; + f64 sum2{}; for (auto i = 0u; i < nrow; i++) { sum = 0.0; sum2 = 0.0; @@ -23,6 +25,6 @@ constexpr auto weierstrass(std::span input) -> double { output += sum; } - return output - static_cast(nrow) * sum2; + return output - static_cast(nrow) * sum2; } } // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/multimodal/zakharov.hpp b/include/cecxx/functions/multimodal/zakharov.hpp index a341639..9c47d4b 100644 --- a/include/cecxx/functions/multimodal/zakharov.hpp +++ b/include/cecxx/functions/multimodal/zakharov.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::multimodal { -constexpr auto zakharov(std::span input) -> double { +constexpr auto zakharov(std::span input) -> f64 { auto sum1{0.0}; auto sum2{0.0}; for (auto i = 0u; i < input.size(); i++) { @@ -13,4 +15,4 @@ constexpr auto zakharov(std::span input) -> double { } return sum1 + std::pow(sum2, 2) + std::pow(sum2, 4); } -} // namespace cecxx::functions::unimodal +} // namespace cecxx::functions::multimodal diff --git a/include/cecxx/functions/unimodal/bent_cigar.hpp b/include/cecxx/functions/unimodal/bent_cigar.hpp index f343f6f..b0cab34 100644 --- a/include/cecxx/functions/unimodal/bent_cigar.hpp +++ b/include/cecxx/functions/unimodal/bent_cigar.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::unimodal { -constexpr auto bent_cigar(std::span input) -> double { +constexpr auto bent_cigar(std::span input) -> f64 { auto output{input[0] * input[0]}; for (auto i = 1u; i < input.size(); ++i) { output += std::pow(10.0, 6.0) * input[i] * input[i]; diff --git a/include/cecxx/functions/unimodal/ellips.hpp b/include/cecxx/functions/unimodal/ellips.hpp index be392a2..3653f65 100644 --- a/include/cecxx/functions/unimodal/ellips.hpp +++ b/include/cecxx/functions/unimodal/ellips.hpp @@ -3,12 +3,14 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::unimodal { -constexpr auto ellips(std::span input) -> double { +constexpr auto ellips(std::span input) -> f64 { auto output{0.0}; for (auto i = 0u; i < input.size(); ++i) { - output += std::pow(10.0, 6.0 * i / (static_cast(input.size()) - 1)) * input[i] * input[i]; + output += std::pow(10.0, 6.0 * i / (static_cast(input.size()) - 1)) * input[i] * input[i]; } return output; diff --git a/include/cecxx/functions/unimodal/sphere.hpp b/include/cecxx/functions/unimodal/sphere.hpp index 605fb77..3de32b8 100644 --- a/include/cecxx/functions/unimodal/sphere.hpp +++ b/include/cecxx/functions/unimodal/sphere.hpp @@ -2,9 +2,11 @@ #include +#include "cecxx/types.hpp" + namespace cecxx::functions::unimodal { -constexpr auto sphere(std::span input) -> double { +constexpr auto sphere(std::span input) -> f64 { auto output{0.0}; for (const auto& x : input) { output += x * x; diff --git a/include/cecxx/functions/unimodal/sum_diff_pow.hpp b/include/cecxx/functions/unimodal/sum_diff_pow.hpp index 3fb1d9c..3d82134 100644 --- a/include/cecxx/functions/unimodal/sum_diff_pow.hpp +++ b/include/cecxx/functions/unimodal/sum_diff_pow.hpp @@ -3,8 +3,10 @@ #include #include +#include "cecxx/types.hpp" + namespace cecxx::functions::unimodal { -constexpr auto sum_diff_pow(std::span input) -> double { +constexpr auto sum_diff_pow(std::span input) -> f64 { auto output{0.0}; for (auto i = 0u; i < input.size(); i++) { output += std::pow(std::fabs(input[i]), i + 1); diff --git a/include/cecxx/types.hpp b/include/cecxx/types.hpp new file mode 100644 index 0000000..bc9d3dc --- /dev/null +++ b/include/cecxx/types.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +namespace cecxx { +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; + +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; + +using f32 = float; +using f64 = double; + +} // namespace cecxx diff --git a/src/context.cpp b/src/context.cpp index d73f50b..47ff0fa 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -20,12 +20,13 @@ inline auto to_str(const cec_edition_t edition) -> std::string_view { } } // namespace -context_t::context_t(const cec_edition_t edition, const std::filesystem::path& storage, const int dim) { +context_t::context_t(const cec_edition_t edition, const std::filesystem::path& storage, const u8 dim) { const auto datadir = storage / to_str(edition); - for (const auto& fn_idx : std::views::iota(1, total_problem_num(edition) + 1)) { - rotate_[fn_idx] = load_table_data(datadir, dim, fn_idx); - shuffle_[fn_idx] = load_table_data(datadir, dim, fn_idx); - shift_[fn_idx] = load_table_data(datadir, dim, fn_idx); + for (const auto& fn : std::views::iota(1, total_problem_num(edition) + 1)) { + auto fn_idx = static_cast(fn); + rotate_[fn_idx] = load_table_data(datadir, dim, fn_idx); + shuffle_[fn_idx] = load_table_data(datadir, dim, fn_idx); + shift_[fn_idx] = load_table_data(datadir, dim, fn_idx); } } diff --git a/src/evaluator.cpp b/src/evaluator.cpp index 906a8ce..48bc4ed 100644 --- a/src/evaluator.cpp +++ b/src/evaluator.cpp @@ -2,7 +2,7 @@ namespace cecxx::benchmark { -evaluator::evaluator(const cec_edition_t edition, const int dimension, const std::filesystem::path& storage) +evaluator::evaluator(const cec_edition_t edition, const u8 dimension, const std::filesystem::path& storage) : dim_{dimension}, ctx_{edition, storage, dim_}, edition_{edition} {} } // namespace cecxx diff --git a/src/legacy/affine_trans.cpp b/src/legacy/affine_trans.cpp index c073810..7c9dd4b 100644 --- a/src/legacy/affine_trans.cpp +++ b/src/legacy/affine_trans.cpp @@ -1,25 +1,25 @@ -#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp" - #include #include +#include "cecxx/benchmark/detail/legacy/affine_transformation.hpp" #include "cecxx/benchmark/detail/util.hpp" #include "consts.h" namespace cecxx::benchmark::detail { -void shufflefunc(std::span input, std::span output, std::span shuffle_vec) { +void shufflefunc(std::span input, std::span output, std::span shuffle_vec) { for (auto i = 0u; i < output.size(); i++) { - output[i] = input[shuffle_vec[i] - 1]; + auto idx = static_cast(std::min(shuffle_vec[i] - 1, 1L)); + output[i] = input[idx]; } } -void shiftfunc(std::span input, std::span output, std::span shift_vec) { +void shiftfunc(std::span input, std::span output, std::span shift_vec) { for (auto i = 0u; i < output.size(); i++) { output[i] = input[i] - shift_vec[i]; } } -void rotatefunc(std::span input, std::span output, std::span rotate_mat) { +void rotatefunc(std::span input, std::span output, std::span rotate_mat) { const auto nrow = output.size(); for (auto i = 0u; i < nrow; i++) { output[i] = 0; @@ -29,9 +29,9 @@ void rotatefunc(std::span input, std::span output, std::sp } } -void sr_func(std::span input, std::span sr_x, std::span shit_vec, - std::span rot_mat, const double sh_rate, const do_affine_trans shift, - const do_affine_trans rotate, std::span output) { +void sr_func(std::span input, std::span sr_x, std::span shit_vec, + std::span rot_mat, const f64 sh_rate, const do_affine_trans shift, const do_affine_trans rotate, + std::span output) { const auto nrow = input.size(); if (to_underlying(shift) == 1) { if (to_underlying(rotate) == 1) { @@ -59,19 +59,19 @@ void sr_func(std::span input, std::span sr_x, std::span input, std::span output, std::span shift_vec, - std::span delta, std::span bias, std::span fit, int cf_num) { +void cf_cal(std::span input, std::span output, std::span shift_vec, + std::span delta, std::span bias, std::span fit, u8 cf_num) { const auto nrow = input.size(); - auto w = std::vector(cf_num); - double w_max = 0, w_sum = 0; - for (auto i = 0; i < cf_num; i++) { + auto w = std::vector(cf_num); + f64 w_max = 0, w_sum = 0; + for (auto i = 0u; i < cf_num; i++) { fit[i] += bias[i]; w[i] = 0; for (auto j = 0u; j < nrow; j++) { w[i] += pow(input[j] - shift_vec[i * nrow + j], 2.0); } if (w[i] != 0) { - w[i] = pow(1.0 / w[i], 0.5) * exp(-w[i] / 2.0 / static_cast(nrow) / pow(delta[i], 2.0)); + w[i] = pow(1.0 / w[i], 0.5) * exp(-w[i] / 2.0 / static_cast(nrow) / pow(delta[i], 2.0)); } else { w[i] = INF; } @@ -80,18 +80,18 @@ void cf_cal(std::span input, std::span output, std::span bool { return (fn - 1) >= 21; } +constexpr auto has_extended_size(const u8 fn) -> bool { return (fn - 1) >= 21; } } // namespace -auto ROT_TABLE_FILENAME(const std::filesystem::path& datadir, const int dim, const int fn) -> std::string { +auto ROT_TABLE_FILENAME(const std::filesystem::path& datadir, const u8 dim, const u8 fn) -> std::string { return datadir / ("M_"s + std::to_string(fn) + "_D"s + std::to_string(dim) + ".txt"); } -auto SHUFFLE_TABLE_FILENAME(const std::filesystem::path& datadir, const int dim, const int fn) -> std::string { +auto SHUFFLE_TABLE_FILENAME(const std::filesystem::path& datadir, const u8 dim, const u8 fn) -> std::string { return datadir / ("shuffle_data_"s + std::to_string(fn) + "_D"s + std::to_string(dim) + ".txt"); } -auto SHIFT_TABLE_FILENAME(const std::filesystem::path& datadir, const int, const int fn) -> std::string { +auto SHIFT_TABLE_FILENAME(const std::filesystem::path& datadir, const u8 dim, const u8 fn) -> std::string { + std::ignore = dim; return datadir / ("shift_data_"s + std::to_string(fn) + ".txt"); } -auto ROT_TABLE_SIZE(const int dim, const int fn) -> table_size_t { - constexpr auto funcTreshold = 20; - constexpr auto coeff = 10; +auto ROT_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t { + constexpr auto funcTreshold = 20u; + constexpr auto coeff = 10u; if (fn - 1 < funcTreshold) { - return {.size = dim * dim, .scaler = coeff, .scaler_applied = false}; + return {.size = static_cast(dim * dim), .scaler = coeff, .scaler_applied = false}; } return {.size = dim * dim * coeff, .scaler = coeff, .scaler_applied = true}; } -auto SHIFT_TABLE_SIZE(const int dim, const int fn) -> table_size_t { - constexpr int funcTreshold = 20; - constexpr int coeff = 10; +auto SHIFT_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t { + constexpr auto funcTreshold = 20u; + constexpr auto coeff = 10u; if (fn - 1 < funcTreshold) { return {.size = dim, .scaler = coeff, .scaler_applied = false}; } return {.size = dim * coeff, .scaler = coeff, .scaler_applied = true}; } -auto SHUFFLE_TABLE_SIZE(const int dim, const int fn) -> table_size_t { - constexpr auto coeff = 10; +auto SHUFFLE_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t { + constexpr auto coeff = 10u; if (not has_extended_size(fn)) { return {.size = dim, .scaler = coeff, .scaler_applied = false}; } diff --git a/src/table_io.hpp b/src/table_io.hpp index 4ec60f9..61c4007 100644 --- a/src/table_io.hpp +++ b/src/table_io.hpp @@ -15,18 +15,18 @@ struct FILE_deleter_t { }; struct table_size_t { - int size{}; - int scaler{}; + u64 size{}; + i64 scaler{}; bool scaler_applied{false}; }; -auto ROT_TABLE_FILENAME(const std::filesystem::path& datadir, const int dim, const int fn) -> std::string; -auto SHUFFLE_TABLE_FILENAME(const std::filesystem::path& datadir, const int dim, const int fn) -> std::string; -auto SHIFT_TABLE_FILENAME(const std::filesystem::path& datadir, const int, const int fn) -> std::string; +auto ROT_TABLE_FILENAME(const std::filesystem::path& datadir, const u8 dim, const u8 fn) -> std::string; +auto SHUFFLE_TABLE_FILENAME(const std::filesystem::path& datadir, const u8 dim, const u8 fn) -> std::string; +auto SHIFT_TABLE_FILENAME(const std::filesystem::path& datadir, const u8, const u8 fn) -> std::string; -auto ROT_TABLE_SIZE(const int dim, const int fn) -> table_size_t; -auto SHIFT_TABLE_SIZE(const int dim, const int fn) -> table_size_t; -auto SHUFFLE_TABLE_SIZE(const int dim, const int fn) -> table_size_t; +auto ROT_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t; +auto SHIFT_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t; +auto SHUFFLE_TABLE_SIZE(const u8 dim, const u8 fn) -> table_size_t; template constexpr auto get_table_name(const std::filesystem::path& datapath, Args... args) { @@ -42,7 +42,7 @@ constexpr auto get_table_name(const std::filesystem::path& datapath, Args... arg throw std::runtime_error("Unknown table type."); } -template constexpr auto get_table_size(const int dim, const int fn) { +template constexpr auto get_table_size(const u8 dim, const u8 fn) { switch (Table) { case table_type_t::rotate: return ROT_TABLE_SIZE(dim, fn); @@ -55,13 +55,13 @@ template constexpr auto get_table_size( throw std::runtime_error("Unknown table type."); } -template auto scan_table(auto* fs, const int dim, const int fn) -> std::vector { +template auto scan_table(auto* fs, const u8 dim, const u8 fn) -> std::vector { const auto sz_info = get_table_size(dim, fn); auto table = std::vector(sz_info.size); const auto scan_scaled_shift_table = [&]() { - for (auto i = 0; i < sz_info.scaler - 1; ++i) { - for (auto j = 0; j < dim; ++j) { + for (auto i = 0u; i < sz_info.scaler - 1; ++i) { + for (auto j = 0u; j < dim; ++j) { if (std::fscanf(fs, number_formatter().data(), &table[i * dim + j]) == -1) { throw std::runtime_error{"Failed to parse table data from file."}; } @@ -75,7 +75,7 @@ template auto scan_table(auto* fs, const int dim return table; } - for (auto i = 0; i < sz_info.size; ++i) { + for (auto i = 0u; i < sz_info.size; ++i) { if (std::fscanf(fs, number_formatter().data(), &table[i]) == -1) { throw std::runtime_error{"Failed to parse table data from file."}; } @@ -84,7 +84,7 @@ template auto scan_table(auto* fs, const int dim } template -auto load_table_data(const std::filesystem::path& datapath, const int dim, const int fn) { +auto load_table_data(const std::filesystem::path& datapath, const u8 dim, const u8 fn) { const auto filename = get_table_name(datapath, dim, fn); auto fs_ptr = std::unique_ptr{std::fopen(filename.data(), "r")}; if (not fs_ptr) { diff --git a/src/util.hpp b/src/util.hpp index 19de5bc..a8b92a8 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -4,18 +4,19 @@ #include #include "cecxx/benchmark/edition.hpp" +#include "cecxx/types.hpp" namespace cecxx::benchmark::detail { template constexpr auto number_formatter() -> std::string_view { if (std::same_as) { return "%lf"; - } else if (std::same_as) { - return "%d"; + } else if (std::same_as) { + return "%lu"; } } -constexpr auto total_problem_num(const cec_edition_t edition) -> int { +constexpr auto total_problem_num(const cec_edition_t edition) -> u8 { switch (edition) { case cec_edition_t::cec2017: return 30;