Skip to content

Commit

Permalink
calculate max name length at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
wx257osn2 committed Feb 4, 2024
1 parent 8d6b476 commit 61c5c0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 8 additions & 1 deletion benchmark/include/implementation_macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
#define OPERATOR_PLUS_EQUAL_IMPLEMENTATION_II_END
#define OPERATOR_PLUS_EQUAL_IMPLEMENTATION(impls) CAT(OPERATOR_PLUS_EQUAL_IMPLEMENTATION_I impls, _END)

#define MAX_NAME_LENGTH_(name, _0, _1) detail::max{std::string_view{name}.size()} +
#define MAX_NAME_LENGTH_I(name, _0, _1) MAX_NAME_LENGTH_(name, _0, _1) MAX_NAME_LENGTH_II
#define MAX_NAME_LENGTH_II(name, _0, _1) MAX_NAME_LENGTH_(name, _0, _1) MAX_NAME_LENGTH_I
#define MAX_NAME_LENGTH_I_END detail::max<std::size_t>{3/*qoi*/}
#define MAX_NAME_LENGTH_II_END detail::max<std::size_t>{3/*qoi*/}
#define MAX_NAME_LENGTH(impls) (CAT(MAX_NAME_LENGTH_I impls, _END)).t

#define OUTPUT_IMPLEMENTATION_(name, ident, _) \
if(printer.opt->CAT(run_, ident)) \
output_lib(os, name, res, res.ident);
Expand Down Expand Up @@ -74,7 +81,7 @@
#define BENCHMARK_ENCODE_CALL_II_END
#define BENCHMARK_ENCODE_CALL(impls) CAT(BENCHMARK_ENCODE_CALL_I impls, _END)

#define HELP_(name, _0, _1) " --no" name " " << std::string(9+1-std::strlen(name), '.') << " don't execute " name "\n"
#define HELP_(name, _0, _1) " --no" name " " << std::string(benchmark_result_t::printer::max_name_length+1-std::strlen(name), '.') << " don't execute " name "\n"
#define HELP_I(name, _0, _1) HELP_(name, _0, _1) HELP_II
#define HELP_II(name, _0, _1) HELP_(name, _0, _1) HELP_I
#define HELP_I_END
Expand Down
14 changes: 13 additions & 1 deletion benchmark/src/qoibench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ struct options{
}
};

namespace detail{

template<typename T>
struct max{
T t;
constexpr max operator+(const max<T>& rhs)const{
return max{std::max(this->t, rhs.t)};
}
};

}

using nanosec = std::chrono::duration<double, std::nano>;
struct benchmark_result_t{
struct lib_t{
Expand Down Expand Up @@ -124,7 +136,7 @@ struct benchmark_result_t{
return os;
}
};
static constexpr std::size_t max_name_length = 9;
static constexpr std::size_t max_name_length = MAX_NAME_LENGTH(IMPLEMENTATIONS);
static std::ostream& output_lib(std::ostream& os, std::string_view name, const benchmark_result_t& res, const lib_t& lib){
if(!lib.valid)
return os << name << " outputs invalid content\n";
Expand Down

0 comments on commit 61c5c0f

Please sign in to comment.