From a36efcd88841f1952bbabd73642c1bfe4485104d Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 18 Feb 2019 10:46:21 +0100 Subject: [PATCH] Create new helper compiler_barrier to prevent compiler from over-optimizing bench. --- bench/common.c | 32 ++++++++++++++++++++++++++++++++ bench/common.h | 23 ++++++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 bench/common.c diff --git a/bench/common.c b/bench/common.c new file mode 100644 index 00000000..e05f3d8d --- /dev/null +++ b/bench/common.c @@ -0,0 +1,32 @@ +/* + * M*LIB - common functions for bench module + * + * Copyright (c) 2017-2019, Patrick Pelissier + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "common.h" + +void compiler_barrier(void *p) +{ + // Do nothing, but let the compiler thinks that we did. + (void) p; +} diff --git a/bench/common.h b/bench/common.h index ff9aba47..a3289387 100644 --- a/bench/common.h +++ b/bench/common.h @@ -25,11 +25,14 @@ #ifndef __COMMON_BENCH__H #define __COMMON_BENCH__H +#include +#include + #if defined(_WIN32) /* WINDOWS variant */ #include -static unsigned long long +static inline unsigned long long cputime (void) { LARGE_INTEGER freq, val; @@ -44,7 +47,7 @@ cputime (void) #include #include -static unsigned long long +static inline unsigned long long cputime (void) { #if defined(MULTI_THREAD_MEASURE) @@ -62,23 +65,24 @@ cputime (void) #endif -unsigned long g_result; +static unsigned long g_result; #if defined(MULTI_THREAD_MEASURE) M_THREAD_ATTR #endif -unsigned int randValue = 0; +static unsigned int randValue = 0; -static void rand_init(void) +static inline void rand_init(void) { randValue = 0; } -static unsigned int rand_get(void) + +static inline unsigned int rand_get(void) { randValue = randValue * 31421U + 6927U; return randValue; } -static void test_function(const char str[], size_t n, void (*func)(size_t)) +static inline void test_function(const char str[], size_t n, void (*func)(size_t)) { unsigned long long start, end; rand_init(); @@ -123,4 +127,9 @@ static inline int get_cpu_count(void) #endif } +#ifdef __cplusplus +extern "C" +#endif +void compiler_barrier(void *p); + #endif