benchmark 1.10.0
Loading...
Searching...
No Matches
macros.h
1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef BENCHMARK_MACROS_H_
16#define BENCHMARK_MACROS_H_
17
18#if defined(_MSC_VER)
19#include <intrin.h>
20#endif
21
22#include <stdint.h>
23
24#define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
25 TypeName(const TypeName&) = delete; \
26 TypeName& operator=(const TypeName&) = delete
27
28#ifdef BENCHMARK_HAS_CXX17
29#define BENCHMARK_UNUSED [[maybe_unused]]
30#elif defined(__GNUC__) || defined(__clang__)
31#define BENCHMARK_UNUSED __attribute__((unused))
32#else
33#define BENCHMARK_UNUSED
34#endif
35
36#if defined(__clang__)
37#define BENCHMARK_DONT_OPTIMIZE __attribute__((optnone))
38#elif defined(__GNUC__) || defined(__GNUG__)
39#define BENCHMARK_DONT_OPTIMIZE __attribute__((optimize(0)))
40#else
41#define BENCHMARK_DONT_OPTIMIZE
42#endif
43
44#if defined(__GNUC__) || defined(__clang__)
45#define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
46#elif defined(_MSC_VER) && !defined(__clang__)
47#define BENCHMARK_ALWAYS_INLINE __forceinline
48#define __func__ __FUNCTION__
49#else
50#define BENCHMARK_ALWAYS_INLINE
51#endif
52
53#define BENCHMARK_INTERNAL_TOSTRING2(x) #x
54#define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
55
56#if (defined(__GNUC__) && !defined(__NVCC__) && !defined(__NVCOMPILER)) || \
57 defined(__clang__)
58#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
59#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
60#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
61 _Pragma("GCC diagnostic push") \
62 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
63#define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("GCC diagnostic pop")
64#elif defined(__NVCOMPILER)
65#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
66#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
67#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
68 _Pragma("diagnostic push") \
69 _Pragma("diag_suppress deprecated_entity_with_custom_message")
70#define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("diagnostic pop")
71#elif defined(_MSC_VER)
72#define BENCHMARK_BUILTIN_EXPECT(x, y) x
73#define BENCHMARK_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
74#define BENCHMARK_WARNING_MSG(msg) \
75 __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
76 __LINE__) ") : warning note: " msg))
77#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
78 __pragma(warning(push)) __pragma(warning(disable : 4996))
79#define BENCHMARK_RESTORE_DEPRECATED_WARNING __pragma(warning(pop))
80#else
81#define BENCHMARK_BUILTIN_EXPECT(x, y) x
82#define BENCHMARK_DEPRECATED_MSG(msg)
83#define BENCHMARK_WARNING_MSG(msg) \
84 __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
85 __LINE__) ") : warning note: " msg))
86#define BENCHMARK_DISABLE_DEPRECATED_WARNING
87#define BENCHMARK_RESTORE_DEPRECATED_WARNING
88#endif
89
90#if defined(__GNUC__) && !defined(__clang__)
91#define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
92#endif
93
94#ifndef __has_builtin
95#define __has_builtin(x) 0
96#endif
97
98#if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
99#define BENCHMARK_UNREACHABLE() __builtin_unreachable()
100#elif defined(_MSC_VER)
101#define BENCHMARK_UNREACHABLE() __assume(false)
102#else
103#define BENCHMARK_UNREACHABLE() ((void)0)
104#endif
105
106#if defined(__GNUC__)
107#if defined(__i386__) || defined(__x86_64__)
108#define BENCHMARK_INTERNAL_CACHELINE_SIZE 64
109#elif defined(__powerpc64__)
110#define BENCHMARK_INTERNAL_CACHELINE_SIZE 128
111#elif defined(__aarch64__)
112#define BENCHMARK_INTERNAL_CACHELINE_SIZE 64
113#elif defined(__arm__)
114#if defined(__ARM_ARCH_5T__)
115#define BENCHMARK_INTERNAL_CACHELINE_SIZE 32
116#elif defined(__ARM_ARCH_7A__)
117#define BENCHMARK_INTERNAL_CACHELINE_SIZE 64
118#endif
119#endif
120#endif
121
122#ifndef BENCHMARK_INTERNAL_CACHELINE_SIZE
123#define BENCHMARK_INTERNAL_CACHELINE_SIZE 64
124#endif
125
126#if defined(__GNUC__)
127#define BENCHMARK_INTERNAL_CACHELINE_ALIGNED \
128 __attribute__((aligned(BENCHMARK_INTERNAL_CACHELINE_SIZE)))
129#elif defined(_MSC_VER)
130#define BENCHMARK_INTERNAL_CACHELINE_ALIGNED \
131 __declspec(align(BENCHMARK_INTERNAL_CACHELINE_SIZE))
132#else
133#define BENCHMARK_INTERNAL_CACHELINE_ALIGNED
134#endif
135
136#endif // BENCHMARK_MACROS_H_