Last active
May 25, 2020 13:44
-
-
Save nmoinvaz/8bdc503804e17164b2d33312764feecc to your computer and use it in GitHub Desktop.
Google benchmark for testing adler32 variants.
This file contains 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
/* adler32_benchmark.cc -- test adler32 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(adler32_benchmark) | |
target_sources(adler32_benchmark PRIVATE adler32_benchmark.cc) | |
target_include_directories(adler32_benchmark PRIVATE benchmark/include) | |
target_link_directories(adler32_benchmark PRIVATE benchmark/build/src/Release) | |
configure_test_executable(adler32_benchmark) | |
target_link_libraries(adler32_benchmark benchmark shlwapi zlib) | |
*/ | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include "zbuild.h" | |
#include <benchmark/benchmark.h> | |
#define MAX_RANDOM_INTS (1024 * 1024) | |
static uint32_t *random_ints = NULL; | |
/* adler32 */ | |
extern "C" uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len); | |
#ifdef ARM_NEON_ADLER32 | |
extern "C" uint32_t adler32_neon(uint32_t adler, const unsigned char *buf, size_t len); | |
#endif | |
#ifdef X86_SSSE3_ADLER32 | |
extern "C" uint32_t adler32_ssse3(uint32_t adler, const unsigned char *buf, size_t len); | |
#endif | |
#ifdef X86_AVX2_ADLER32 | |
extern "C" uint32_t adler32_avx2(uint32_t adler, const unsigned char *buf, size_t len); | |
#endif | |
static void adler32_c_bench(benchmark::State& state) { | |
int32_t j = 0; | |
uint32_t a = 0; | |
while (state.KeepRunning()) { | |
uint32_t hash = adler32_c(a, (const unsigned char *)random_ints, j * sizeof(uint32_t)); | |
benchmark::DoNotOptimize(hash); | |
if (++j >= MAX_RANDOM_INTS) j = 0; | |
} | |
} | |
BENCHMARK(adler32_c_bench); | |
#ifdef ARM_NEON_ADLER32 | |
static void adler32_neon_bench(benchmark::State& state) { | |
int32_t j = 0; | |
uint32_t a = 0; | |
while (state.KeepRunning()) { | |
uint32_t hash = adler32_neon(a, (const unsigned char *)random_ints, j * sizeof(uint32_t)); | |
benchmark::DoNotOptimize(hash); | |
if (++j >= MAX_RANDOM_INTS) j = 0; | |
} | |
} | |
BENCHMARK(adler32_neon_bench); | |
#endif | |
#ifdef X86_SSSE3_ADLER32 | |
static void adler32_ssse3_bench(benchmark::State& state) { | |
int32_t j = 0; | |
uint32_t a = 0; | |
while (state.KeepRunning()) { | |
uint32_t hash = adler32_ssse3(a, (const unsigned char *)random_ints, j * sizeof(uint32_t)); | |
benchmark::DoNotOptimize(hash); | |
if (++j >= MAX_RANDOM_INTS) j = 0; | |
} | |
} | |
BENCHMARK(adler32_ssse3_bench); | |
#endif | |
#ifdef X86_AVX2_ADLER32 | |
static void adler32_avx2_bench(benchmark::State& state) { | |
int32_t j = 0; | |
uint32_t a = 0; | |
while (state.KeepRunning()) { | |
uint32_t hash = adler32_avx2(a, (const unsigned char *)random_ints, j * sizeof(uint32_t)); | |
benchmark::DoNotOptimize(hash); | |
if (++j >= MAX_RANDOM_INTS) j = 0; | |
} | |
} | |
BENCHMARK(adler32_avx2_bench); | |
#endif | |
int main(int argc, char** argv) | |
{ | |
int32_t random_ints_size = MAX_RANDOM_INTS * sizeof(uint32_t); | |
random_ints = (uint32_t *)malloc(random_ints_size); | |
for (int32_t i = 0; i < MAX_RANDOM_INTS; i++) { | |
random_ints[i] = rand(); | |
} | |
::benchmark::Initialize(&argc, argv); | |
::benchmark::RunSpecifiedBenchmarks(); | |
free(random_ints); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment