Last active
May 25, 2020 13:42
-
-
Save nmoinvaz/1bf9e4098fe6399a14f829957c2123f8 to your computer and use it in GitHub Desktop.
Google benchmark for testing compare258 variants.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* compare258_benchmark.cc -- test compare258 variants | |
* Copyright (C) 2020 Nathan Moinvaziri | |
* For conditions of distribution and use, see copyright notice in zlib.h | |
*/ | |
/* | |
cmake . -A Win32 -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL -DWITH_GZFILEOP=ON -DBUILD_SHARED_LIBS=OFF | |
cmake_minimum_required(VERSION 3.17) | |
cmake_policy(SET CMP0091 NEW) | |
enable_language(CXX) | |
add_executable(compare258_benchmark) | |
target_sources(compare258_benchmark PRIVATE compare258_benchmark.cc) | |
target_include_directories(compare258_benchmark PRIVATE benchmark/include) | |
target_link_directories(compare258_benchmark PRIVATE benchmark/build/src/Release) | |
configure_test_executable(compare258_benchmark) | |
target_link_libraries(compare258_benchmark benchmark shlwapi zlib) | |
*/ | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include "zbuild.h" | |
#include "zutil.h" | |
#include "fallback_builtins.h" | |
#include <benchmark/benchmark.h> | |
#include "compare258.c" | |
#ifdef X86_SSE42_CMP_STR | |
# include "arch/x86/compare258_sse.c" | |
#endif | |
#if defined(X86_AVX2) && defined(HAVE_BUILTIN_CTZ) | |
# include "arch/x86/compare258_avx.c" | |
#endif | |
#define X2 1 | |
static void compare258_c_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_c_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_c_bench); | |
#if X2 | |
static void compare258_c_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_c_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_c_x2_bench); | |
#endif | |
static void compare258_unaligned_16_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_16_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_16_bench); | |
#if X2 | |
static void compare258_unaligned_16_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_16_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_16_x2_bench); | |
#endif | |
static void compare258_unaligned_32_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_32_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_32_bench); | |
#if X2 | |
static void compare258_unaligned_32_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_32_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_32_x2_bench); | |
#endif | |
#ifdef HAVE_BUILTIN_CTZLL | |
static void compare258_unaligned_64_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_64_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_64_bench); | |
#if X2 | |
static void compare258_unaligned_64_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_64_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_64_x2_bench); | |
#endif | |
#endif | |
#ifdef X86_SSE42_CMP_STR | |
static void compare258_unaligned_sse4_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_sse4_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_sse4_bench); | |
#if X2 | |
static void compare258_unaligned_sse4_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_sse4_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_sse4_x2_bench); | |
#endif | |
#endif | |
#if defined(X86_AVX2) && defined(HAVE_BUILTIN_CTZ) | |
static void compare258_unaligned_avx2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_avx2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_avx2_bench); | |
#if X2 | |
static void compare258_unaligned_avx2_x2_bench(benchmark::State& state) { | |
int32_t x = 0; | |
unsigned char cmp1[258]; | |
unsigned char cmp2[258]; | |
memset(cmp1, 'a', sizeof(cmp1)); | |
memset(cmp2, 'a', sizeof(cmp2)); | |
while (state.KeepRunning()) { | |
cmp2[x] = 0; | |
uint32_t len = compare258_unaligned_avx2_x2_static((const unsigned char *)cmp1, (const unsigned char *)cmp2); | |
benchmark::DoNotOptimize(len); | |
cmp2[x] = 'a'; | |
if (++x > 256) { x = 0; } | |
} | |
} | |
BENCHMARK(compare258_unaligned_avx2_x2_bench); | |
#endif | |
#endif | |
int main(int argc, char** argv) | |
{ | |
::benchmark::Initialize(&argc, argv); | |
::benchmark::RunSpecifiedBenchmarks(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment