/*
The latest version of this library is available on GitHub;
https://github.com/sheredom/ubench.h
*/
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to
*/
#ifndef SHEREDOM_UBENCH_H_INCLUDED
#define SHEREDOM_UBENCH_H_INCLUDED
#ifdef _MSC_VER
/*
Disable warning about not inlining 'inline' functions.
TODO: We'll fix this later by not using fprintf within our macros, and
instead use snprintf to a realloc'ed buffer.
*/
#pragma warning(disable : 4710)
/*
Disable warning about inlining functions that are not marked 'inline'.
TODO: add a UBENCH_NOINLINE onto the macro generated functions to fix this.
*/
#pragma warning(disable : 4711)
/*
Disable warning about replacing undefined preprocessor macro '__cplusplus' with
0 emitted from microsofts own headers.
See: https://developercommunity.visualstudio.com/t/issue-in-corecrth-header-results-in-an-undefined-m/433021
*/
#pragma warning(disable : 4668)
/*
Disabled warning about dangerous use of section.
section '.CRT$XCU' is reserved for C++ dynamic initialization. Manually
creating the section will interfere with C++ dynamic initialization and may lead to undefined behavior
*/
#if defined(_MSC_FULL_VER)
#if _MSC_FULL_VER >= 192930100 // this warning was introduced in Visual Studio 2019 version 16.11
#pragma warning(disable : 5247)
#pragma warning(disable : 5248)
#endif
#endif
#pragma warning(push, 1)
#endif
#if defined(__cplusplus)
#define UBENCH_C_FUNC extern "C"
#else
#define UBENCH_C_FUNC
#endif
#if defined(__cplusplus)
#define UBENCH_NULL NULL
#else
#define UBENCH_NULL 0
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1920)
typedef __int64 ubench_int64_t;
typedef unsigned __int64 ubench_uint64_t;
#else
#include
typedef int64_t ubench_int64_t;
typedef uint64_t ubench_uint64_t;
#endif
#include
#include
#include
#include
#include
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
typedef union {
struct {
unsigned long LowPart;
long HighPart;
} DUMMYSTRUCTNAME;
struct {
unsigned long LowPart;
long HighPart;
} u;
ubench_int64_t QuadPart;
} ubench_large_integer;
UBENCH_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceCounter(ubench_large_integer *);
UBENCH_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency(ubench_large_integer *);
#elif defined(__linux__)
/*
slightly obscure include here - we need to include glibc's features.h, but
we don't want to just include a header that might not be defined for other
c libraries like musl. Instead we include limits.h, which we know on all
glibc distributions includes features.h
*/
#include
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
#include
#if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__)))
/* glibc is version 2.17 or above, so we can just use clock_gettime */
#define UBENCH_USE_CLOCKGETTIME
#else
#include
#include
#endif
#endif
#elif defined(__APPLE__)
#include
#endif
#if defined(__cplusplus)
#define UBENCH_C_FUNC extern "C"
#else
#define UBENCH_C_FUNC
#endif
#if defined(__cplusplus) && (__cplusplus >= 201103L)
#define UBENCH_NOEXCEPT noexcept
#else
#define UBENCH_NOEXCEPT
#endif
#if defined(__cplusplus) && defined(_MSC_VER)
#define UBENCH_NOTHROW __declspec(nothrow)
#else
#define UBENCH_NOTHROW
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1920)
#define UBENCH_PRId64 "I64d"
#define UBENCH_PRIu64 "I64u"
#else
#include
#define UBENCH_PRId64 PRId64
#define UBENCH_PRIu64 PRIu64
#endif
#if defined(_MSC_VER)
#define UBENCH_INLINE __forceinline
#define UBENCH_NOINLINE __declspec(noinline)
#if defined(_WIN64)
#define UBENCH_SYMBOL_PREFIX
#else
#define UBENCH_SYMBOL_PREFIX "_"
#endif
#if defined(__clang__)
#define UBENCH_INITIALIZER_BEGIN_DISABLE_WARNINGS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wmissing-variable-declarations\"")
#define UBENCH_INITIALIZER_END_DISABLE_WARNINGS _Pragma("clang diagnostic pop")
#else
#define UBENCH_INITIALIZER_BEGIN_DISABLE_WARNINGS
#define UBENCH_INITIALIZER_END_DISABLE_WARNINGS
#endif
#pragma section(".CRT$XCU", read)
#define UBENCH_INITIALIZER(f) \
static void __cdecl f(void); \
UBENCH_INITIALIZER_BEGIN_DISABLE_WARNINGS __pragma( \
comment(linker, "/include:" UBENCH_SYMBOL_PREFIX #f "_")) \
UBENCH_C_FUNC __declspec(allocate(".CRT$XCU")) void(__cdecl * \
f##_)(void) = f; \
UBENCH_INITIALIZER_END_DISABLE_WARNINGS static void __cdecl f(void)
#else
#if defined(__linux__)
#if defined(__clang__)
#if __has_warning("-Wreserved-id-macro")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
#endif
#define __STDC_FORMAT_MACROS 1
#if defined(__clang__)
#if __has_warning("-Wreserved-id-macro")
#pragma clang diagnostic pop
#endif
#endif
#endif
#define UBENCH_INLINE inline
#define UBENCH_NOINLINE __attribute__((noinline))
#define UBENCH_INITIALIZER(f) \
static void f(void) __attribute__((constructor)); \
static void f(void)
#endif
#if defined(__cplusplus)
#define UBENCH_CAST(type, x) static_cast(x)
#define UBENCH_PTR_CAST(type, x) reinterpret_cast(x)
#define UBENCH_EXTERN extern "C"
#else
#define UBENCH_CAST(type, x) ((type)(x))
#define UBENCH_PTR_CAST(type, x) ((type)(x))
#define UBENCH_EXTERN extern
#endif
#ifdef _MSC_VER
/*
io.h contains definitions for some structures with natural padding. This is
uninteresting, but for some reason MSVC's behaviour is to warn about
including this system header. That *is* interesting
*/
#pragma warning(disable : 4820)
#pragma warning(push, 1)
#include
#pragma warning(pop)
#define UBENCH_COLOUR_OUTPUT() (_isatty(_fileno(stdout)))
#else
#include
#define UBENCH_COLOUR_OUTPUT() (isatty(STDOUT_FILENO))
#endif
static UBENCH_INLINE ubench_int64_t ubench_ns(void) {
#ifdef _MSC_VER
ubench_large_integer counter;
ubench_large_integer frequency;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&frequency);
return UBENCH_CAST(ubench_int64_t,
(counter.QuadPart * 1000000000) / frequency.QuadPart);
#elif defined(__linux)
struct timespec ts;
const clockid_t cid = CLOCK_REALTIME;
#if defined(UBENCH_USE_CLOCKGETTIME)
clock_gettime(cid, &ts);
#else
syscall(SYS_clock_gettime, cid, &ts);
#endif
return UBENCH_CAST(ubench_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 +
ts.tv_nsec;
#elif __APPLE__
return UBENCH_CAST(ubench_int64_t, mach_absolute_time());
#endif
}
struct ubench_run_state_s {
ubench_int64_t* ns;
ubench_int64_t size;
ubench_int64_t sample;
};
typedef void (*ubench_benchmark_t)(struct ubench_run_state_s* ubs);
struct ubench_benchmark_state_s {
ubench_benchmark_t func;
char *name;
};
struct ubench_state_s {
struct ubench_benchmark_state_s *benchmarks;
size_t benchmarks_length;
FILE *output;
double confidence;
};
/* extern to the global state ubench needs to execute */
UBENCH_EXTERN struct ubench_state_s ubench_state;
#if defined(_MSC_VER)
#define UBENCH_UNUSED
#else
#define UBENCH_UNUSED __attribute__((unused))
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvariadic-macros"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif
#define UBENCH_PRINTF(...) \
if (ubench_state.output) { \
fprintf(ubench_state.output, __VA_ARGS__); \
} \
printf(__VA_ARGS__)
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvariadic-macros"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif
#ifdef _MSC_VER
#define UBENCH_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__)
#else
#define UBENCH_SNPRINTF(...) snprintf(__VA_ARGS__)
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s* ubs)
{
ubench_int64_t curr_sample = ubs->sample++;
ubs->ns[curr_sample] = ubench_ns();
return curr_sample < ubs->size ? 1 : 0;
}
#define UBENCH_DO_BENCHMARK() \
while(ubench_do_benchmark(ubench_run_state) > 0)
#define UBENCH_EX(SET, NAME) \
UBENCH_EXTERN struct ubench_state_s ubench_state; \
static void ubench_##SET##_##NAME(struct ubench_run_state_s* ubs); \
UBENCH_INITIALIZER(ubench_register_##SET##_##NAME) { \
const size_t index = ubench_state.benchmarks_length++; \
const char *name_part = #SET "." #NAME; \
const size_t name_size = strlen(name_part) + 1; \
char *name = UBENCH_PTR_CAST(char *, malloc(name_size)); \
ubench_state.benchmarks = UBENCH_PTR_CAST( \
struct ubench_benchmark_state_s *, \
realloc(UBENCH_PTR_CAST(void *, ubench_state.benchmarks), \
sizeof(struct ubench_benchmark_state_s) * \
ubench_state.benchmarks_length)); \
ubench_state.benchmarks[index].func = &ubench_##SET##_##NAME; \
ubench_state.benchmarks[index].name = name; \
UBENCH_SNPRINTF(name, name_size, "%s", name_part); \
} \
void ubench_##SET##_##NAME(struct ubench_run_state_s* ubench_run_state)
#define UBENCH(SET, NAME) \
static void ubench_run_##SET##_##NAME(void); \
UBENCH_EX(SET, NAME) { \
UBENCH_DO_BENCHMARK() { \
ubench_run_##SET##_##NAME(); \
} \
} \
void ubench_run_##SET##_##NAME(void)
#define UBENCH_F_SETUP(FIXTURE) \
static void ubench_f_setup_##FIXTURE(struct FIXTURE *ubench_fixture)
#define UBENCH_F_TEARDOWN(FIXTURE) \
static void ubench_f_teardown_##FIXTURE(struct FIXTURE *ubench_fixture)
#define UBENCH_EX_F(FIXTURE, NAME) \
UBENCH_EXTERN struct ubench_state_s ubench_state; \
static void ubench_f_setup_##FIXTURE(struct FIXTURE *); \
static void ubench_f_teardown_##FIXTURE(struct FIXTURE *); \
static void ubench_run_ex_##FIXTURE##_##NAME(struct FIXTURE *, \
struct ubench_run_state_s*); \
static void ubench_f_##FIXTURE##_##NAME(struct ubench_run_state_s* ubench_run_state) { \
struct FIXTURE fixture; \
memset(&fixture, 0, sizeof(fixture)); \
ubench_f_setup_##FIXTURE(&fixture); \
ubench_run_ex_##FIXTURE##_##NAME(&fixture, ubench_run_state); \
ubench_f_teardown_##FIXTURE(&fixture); \
} \
UBENCH_INITIALIZER(ubench_register_##FIXTURE##_##NAME) { \
const size_t index = ubench_state.benchmarks_length++; \
const char *name_part = #FIXTURE "." #NAME; \
const size_t name_size = strlen(name_part) + 1; \
char *name = UBENCH_PTR_CAST(char *, malloc(name_size)); \
ubench_state.benchmarks = UBENCH_PTR_CAST( \
struct ubench_benchmark_state_s *, \
realloc(UBENCH_PTR_CAST(void *, ubench_state.benchmarks), \
sizeof(struct ubench_benchmark_state_s) * \
ubench_state.benchmarks_length)); \
ubench_state.benchmarks[index].func = &ubench_f_##FIXTURE##_##NAME; \
ubench_state.benchmarks[index].name = name; \
UBENCH_SNPRINTF(name, name_size, "%s", name_part); \
} \
void ubench_run_ex_##FIXTURE##_##NAME(struct FIXTURE *ubench_fixture, \
struct ubench_run_state_s* ubench_run_state)
#define UBENCH_F(FIXTURE, NAME) \
static void ubench_run_##FIXTURE##_##NAME(struct FIXTURE *); \
UBENCH_EX_F(FIXTURE, NAME) { \
UBENCH_DO_BENCHMARK() { \
ubench_run_##FIXTURE##_##NAME(ubench_fixture); \
} \
} \
void ubench_run_##FIXTURE##_##NAME(struct FIXTURE *ubench_fixture)
static UBENCH_INLINE int ubench_should_filter(const char *filter,
const char *benchmark);
int ubench_should_filter(const char *filter, const char *benchmark) {
if (filter) {
const char *filter_cur = filter;
const char *benchmark_cur = benchmark;
const char *filter_wildcard = UBENCH_NULL;
while (('\0' != *filter_cur) && ('\0' != *benchmark_cur)) {
if ('*' == *filter_cur) {
/* store the position of the wildcard */
filter_wildcard = filter_cur;
/* skip the wildcard character */
filter_cur++;
while (('\0' != *filter_cur) && ('\0' != *benchmark_cur)) {
if ('*' == *filter_cur) {
/*
we found another wildcard (filter is something like *foo*) so we
exit the current loop, and return to the parent loop to handle
the wildcard case
*/
break;
} else if (*filter_cur != *benchmark_cur) {
/* otherwise our filter didn't match, so reset it */
filter_cur = filter_wildcard;
}
/* move benchmark along */
benchmark_cur++;
/* move filter along */
filter_cur++;
}
if (('\0' == *filter_cur) && ('\0' == *benchmark_cur)) {
return 0;
}
/* if the benchmarks have been exhausted, we don't have a match! */
if ('\0' == *benchmark_cur) {
return 1;
}
} else {
if (*benchmark_cur != *filter_cur) {
/* benchmark doesn't match filter */
return 1;
} else {
/* move our filter and benchmark forward */
benchmark_cur++;
filter_cur++;
}
}
}
if (('\0' != *filter_cur) ||
(('\0' != *benchmark_cur) &&
((filter == filter_cur) || ('*' != filter_cur[-1])))) {
/* we have a mismatch! */
return 1;
}
}
return 0;
}
static UBENCH_INLINE int ubench_strncmp(const char *a, const char *b,
size_t n) {
/* strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it */
unsigned i;
for (i = 0; i < n; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
static UBENCH_INLINE FILE *ubench_fopen(const char *filename,
const char *mode) {
#ifdef _MSC_VER
FILE *file;
if (0 == fopen_s(&file, filename, mode)) {
return file;
} else {
return UBENCH_NULL;
}
#else
return fopen(filename, mode);
#endif
}
static UBENCH_INLINE int ubench_main(int argc, const char *const argv[]);
int ubench_main(int argc, const char *const argv[]) {
ubench_uint64_t failed = 0;
size_t index = 0;
size_t *failed_benchmarks = UBENCH_NULL;
size_t failed_benchmarks_length = 0;
const char *filter = UBENCH_NULL;
ubench_uint64_t ran_benchmarks = 0;
enum colours { RESET, GREEN, RED };
const int use_colours = UBENCH_COLOUR_OUTPUT();
const char *colours[] = {"\033[0m", "\033[32m", "\033[31m"};
if (!use_colours) {
for (index = 0; index < sizeof colours / sizeof colours[0]; index++) {
colours[index] = "";
}
}
/* loop through all arguments looking for our options */
for (index = 1; index < UBENCH_CAST(size_t, argc); index++) {
/* Informational switches */
const char help_str[] = "--help";
const char list_str[] = "--list-benchmarks";
/* Benchmark config switches */
const char filter_str[] = "--filter=";
const char output_str[] = "--output=";
const char confidence_str[] = "--confidence=";
if (0 == ubench_strncmp(argv[index], help_str, strlen(help_str))) {
printf("ubench.h - the single file benchmarking solution for C/C++!\n"
"Command line Options:\n");
printf(" --help Show this message and exit.\n"
" --filter= Filter the benchmarks to run (EG. "
"MyBench*.a would run MyBenchmark.a but not MyBenchmark.b).\n"
" --list-benchmarks List benchmarks, one per line. "
"Output names can be passed to --filter.\n"
" --output=