Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:02:48

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 // Adapted from donated nonius code.
0009 
0010 #ifndef CATCH_OPTIMIZER_HPP_INCLUDED
0011 #define CATCH_OPTIMIZER_HPP_INCLUDED
0012 
0013 #if defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC__)
0014 #   include <atomic> // atomic_thread_fence
0015 #endif
0016 
0017 #include <catch2/internal/catch_move_and_forward.hpp>
0018 
0019 #include <type_traits>
0020 
0021 namespace Catch {
0022     namespace Benchmark {
0023 #if defined(__GNUC__) || defined(__clang__)
0024         template <typename T>
0025         inline void keep_memory(T* p) {
0026             asm volatile("" : : "g"(p) : "memory");
0027         }
0028         inline void keep_memory() {
0029             asm volatile("" : : : "memory");
0030         }
0031 
0032         namespace Detail {
0033             inline void optimizer_barrier() { keep_memory(); }
0034         } // namespace Detail
0035 #elif defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC__)
0036 
0037 #if defined(_MSVC_VER)
0038 #pragma optimize("", off)
0039 #elif defined(__IAR_SYSTEMS_ICC__)
0040 // For IAR the pragma only affects the following function
0041 #pragma optimize=disable
0042 #endif
0043         template <typename T>
0044         inline void keep_memory(T* p) {
0045             // thanks @milleniumbug
0046             *reinterpret_cast<char volatile*>(p) = *reinterpret_cast<char const volatile*>(p);
0047         }
0048         // TODO equivalent keep_memory()
0049 #if defined(_MSVC_VER)
0050 #pragma optimize("", on)
0051 #endif
0052 
0053         namespace Detail {
0054             inline void optimizer_barrier() {
0055                 std::atomic_thread_fence(std::memory_order_seq_cst);
0056             }
0057         } // namespace Detail
0058 
0059 #endif
0060 
0061         template <typename T>
0062         inline void deoptimize_value(T&& x) {
0063             keep_memory(&x);
0064         }
0065 
0066         template <typename Fn, typename... Args>
0067         inline auto invoke_deoptimized(Fn&& fn, Args&&... args) -> std::enable_if_t<!std::is_same<void, decltype(fn(args...))>::value> {
0068             deoptimize_value(CATCH_FORWARD(fn) (CATCH_FORWARD(args)...));
0069         }
0070 
0071         template <typename Fn, typename... Args>
0072         inline auto invoke_deoptimized(Fn&& fn, Args&&... args) -> std::enable_if_t<std::is_same<void, decltype(fn(args...))>::value> {
0073             CATCH_FORWARD(fn) (CATCH_FORWARD(args)...);
0074         }
0075     } // namespace Benchmark
0076 } // namespace Catch
0077 
0078 #endif // CATCH_OPTIMIZER_HPP_INCLUDED