libcopp  1.2.0
compile_optimize.h
Go to the documentation of this file.
1 #ifndef UTIL_CONFIG_COMPILE_OPTIMIZE_H
2 #define UTIL_CONFIG_COMPILE_OPTIMIZE_H
3 
4 #pragma once
5 
6 
7 // ================ branch prediction information ================
8 #ifndef likely
9 #ifdef __GNUC__
10 #define likely(x) __builtin_expect(!!(x), 1)
11 #else
12 #define likely(x) !!(x)
13 #endif
14 #endif
15 
16 #ifndef unlikely
17 #ifdef __GNUC__
18 #define unlikely(x) __builtin_expect(!!(x), 0)
19 #else
20 #define unlikely(x) !!(x)
21 #endif
22 #endif
23 
24 #ifndef unreachable
25 #ifdef __GNUC__
26 #ifdef __clang__
27 #if __has_builtin(__builtin_unreachable)
28 #define unreachable() __builtin_unreachable()
29 #else
30 #define unreachable() abort()
31 #endif
32 #else
33 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
34 #define unreachable() __builtin_unreachable()
35 #else
36 #define unreachable() abort()
37 #endif
38 #endif
39 #else
40 #define unreachable() abort()
41 #endif
42 #endif
43 // ---------------- branch prediction information ----------------
44 
45 // ================ import/export ================
46 // 不支持 borland/sunpro_cc/xlcpp
47 
48 // ================ import/export: for compilers ================
49 #if defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)
50 // Intel
51 //
52 // Dynamic shared object (DSO) and dynamic-link library (DLL) support
53 //
54 #if defined(__GNUC__) && (__GNUC__ >= 4)
55 #define UTIL_SYMBOL_EXPORT __attribute__((visibility("default")))
56 #define UTIL_SYMBOL_IMPORT
57 #define UTIL_SYMBOL_VISIBLE __attribute__((visibility("default")))
58 #endif
59 
60 #elif defined __clang__ && !defined(__CUDACC__) && !defined(__ibmxl__)
61 // when using clang and cuda at same time, you want to appear as gcc
62 // Clang C++ emulates GCC, so it has to appear early.
63 //
64 
65 // Dynamic shared object (DSO) and dynamic-link library (DLL) support
66 //
67 #if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32)
68 #define UTIL_SYMBOL_EXPORT __attribute__((__visibility__("default")))
69 #define UTIL_SYMBOL_IMPORT
70 #define UTIL_SYMBOL_VISIBLE __attribute__((__visibility__("default")))
71 #endif
72 
73 #elif defined(__GNUC__) && !defined(__ibmxl__)
74 // GNU C++:
75 //
76 // Dynamic shared object (DSO) and dynamic-link library (DLL) support
77 //
78 #if __GNUC__ >= 4
79 #if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) && !defined(__CYGWIN__)
80 // All Win32 development environments, including 64-bit Windows and MinGW, define
81 // _WIN32 or one of its variant spellings. Note that Cygwin is a POSIX environment,
82 // so does not define _WIN32 or its variants.
83 #define BOOST_HAS_DECLSPEC
84 #define UTIL_SYMBOL_EXPORT __attribute__((__dllexport__))
85 #define UTIL_SYMBOL_IMPORT __attribute__((__dllimport__))
86 #else
87 #define UTIL_SYMBOL_EXPORT __attribute__((__visibility__("default")))
88 #define UTIL_SYMBOL_IMPORT
89 #endif
90 #define UTIL_SYMBOL_VISIBLE __attribute__((__visibility__("default")))
91 #else
92 // config/platform/win32.hpp will define UTIL_SYMBOL_EXPORT, etc., unless already defined
93 #define UTIL_SYMBOL_EXPORT
94 #endif
95 
96 #elif defined _MSC_VER
97 // Microsoft Visual C++
98 //
99 // Must remain the last #elif since some other vendors (Metrowerks, for
100 // example) also #define _MSC_VER
101 #else
102 #endif
103 // ---------------- import/export: for compilers ----------------
104 
105 // ================ import/export: for platform ================
106 // Default defines for UTIL_SYMBOL_EXPORT and UTIL_SYMBOL_IMPORT
107 // If a compiler doesn't support __declspec(dllexport)/__declspec(dllimport),
108 // its boost/config/compiler/ file must define UTIL_SYMBOL_EXPORT and
109 // UTIL_SYMBOL_IMPORT
110 #ifndef UTIL_SYMBOL_EXPORT
111 #define UTIL_SYMBOL_EXPORT __declspec(dllexport)
112 #define UTIL_SYMBOL_IMPORT __declspec(dllimport)
113 #endif
114 // ---------------- import/export: for platform ----------------
115 
116 #ifndef UTIL_SYMBOL_EXPORT
117 #define UTIL_SYMBOL_EXPORT
118 #endif
119 #ifndef UTIL_SYMBOL_IMPORT
120 #define UTIL_SYMBOL_IMPORT
121 #endif
122 #ifndef UTIL_SYMBOL_VISIBLE
123 #define UTIL_SYMBOL_VISIBLE
124 #endif
125 
126 // ---------------- import/export ----------------
127 
128 // ================ __cdecl ================
129 #if defined(__GNUC__)
130 #ifndef __cdecl
131 // see https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html
132 // Intel x86 architecture specific calling conventions
133 #ifdef _M_IX86
134 #define __cdecl __attribute__((__cdecl__))
135 #else
136 #define __cdecl
137 #endif
138 #endif
139 #endif
140 // ---------------- __cdecl ----------------
141 
142 #endif