Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2021 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef TOOLBOX_BM_UTILITY_HPP
17#define TOOLBOX_BM_UTILITY_HPP
18
19namespace toolbox::bm {
20
21// The clobber_memory and do_not_optimise functions below are used to defeat the optimiser.
22// See CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!"
23// And https://github.com/google/benchmark/blob/master/include/benchmark/benchmark.h
24
28{
29 asm volatile("" ::: "memory");
30}
31
35template <typename ValueT>
36inline void do_not_optimise(const ValueT& val) noexcept
37{
38 asm volatile("" : : "r,m"(val) : "memory");
39}
40
44template <typename ValueT>
45inline void do_not_optimise(ValueT& val) noexcept
46{
47#if defined(__clang__)
48 asm volatile("" : "+r,m"(val) : : "memory");
49#else
50 asm volatile("" : "+m,r"(val) : : "memory");
51#endif
52}
53} // namespace toolbox::bm
54
55#endif // TOOLBOX_BM_UTILITY_HPP
void clobber_memory() noexcept
Definition Utility.hpp:27
void do_not_optimise(const ValueT &val) noexcept
Definition Utility.hpp:36
constexpr auto bind() noexcept
Definition Slot.hpp:92