Normalize SSE support detection
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2015 Facebook, Inc.
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
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #ifndef FOLLY_NO_CONFIG
21 #include <folly/folly-config.h>
22 #endif
23
24 #ifdef FOLLY_PLATFORM_CONFIG
25 #include FOLLY_PLATFORM_CONFIG
26 #endif
27
28 #if FOLLY_HAVE_FEATURES_H
29 #include <features.h>
30 #endif
31
32 #include <folly/CPortability.h>
33
34 #ifdef __APPLE__
35 # include <malloc/malloc.h>
36 #endif
37
38 #if FOLLY_HAVE_SCHED_H
39  #include <sched.h>
40  #ifndef FOLLY_HAVE_PTHREAD_YIELD
41   #define pthread_yield sched_yield
42  #endif
43 #endif
44
45 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
46 // MAP_ANON on OSX/BSD.
47 #if defined(__APPLE__) || defined(__FreeBSD__)
48   #include <sys/mman.h>
49   #ifndef MAP_ANONYMOUS
50     #ifdef MAP_ANON
51       #define MAP_ANONYMOUS MAP_ANON
52     #endif
53   #endif
54 #endif
55
56 // MaxAlign: max_align_t isn't supported by gcc
57 #ifdef __GNUC__
58 struct MaxAlign { char c; } __attribute__((__aligned__));
59 #else /* !__GNUC__ */
60 # error Cannot define MaxAlign on this platform
61 #endif
62
63 // compiler specific attribute translation
64 // msvc should come first, so if clang is in msvc mode it gets the right defines
65
66 // NOTE: this will only do checking in msvc with versions that support /analyze
67 #if _MSC_VER
68 # ifdef _USE_ATTRIBUTES_FOR_SAL
69 #    undef _USE_ATTRIBUTES_FOR_SAL
70 # endif
71 /* nolint */
72 # define _USE_ATTRIBUTES_FOR_SAL 1
73 # include <sal.h>
74 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
75 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
76 #else
77 # define FOLLY_PRINTF_FORMAT /**/
78 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
79   __attribute__((__format__(__printf__, format_param, dots_param)))
80 #endif
81
82 // deprecated
83 #if defined(__clang__) || defined(__GNUC__)
84 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
85 #else
86 # define FOLLY_DEPRECATED
87 #endif
88
89 // noreturn
90 #if defined(_MSC_VER)
91 # define FOLLY_NORETURN __declspec(noreturn)
92 #elif defined(__clang__) || defined(__GNUC__)
93 # define FOLLY_NORETURN __attribute__((__noreturn__))
94 #else
95 # define FOLLY_NORETURN
96 #endif
97
98 // noinline
99 #ifdef _MSC_VER
100 # define FOLLY_NOINLINE __declspec(noinline)
101 #elif defined(__clang__) || defined(__GNUC__)
102 # define FOLLY_NOINLINE __attribute__((__noinline__))
103 #else
104 # define FOLLY_NOINLINE
105 #endif
106
107 // always inline
108 #ifdef _MSC_VER
109 # define FOLLY_ALWAYS_INLINE __forceinline
110 #elif defined(__clang__) || defined(__GNUC__)
111 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
112 #else
113 # define FOLLY_ALWAYS_INLINE
114 #endif
115
116 // detection for 64 bit
117 #if defined(__x86_64__) || defined(_M_X64)
118 # define FOLLY_X64 1
119 #else
120 # define FOLLY_X64 0
121 #endif
122
123 #if defined(__aarch64__)
124 # define FOLLY_A64 1
125 #else
126 # define FOLLY_A64 0
127 #endif
128
129 // packing is very ugly in msvc
130 #ifdef _MSC_VER
131 # define FOLLY_PACK_ATTR /**/
132 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
133 # define FOLLY_PACK_POP __pragma(pack(pop))
134 #elif defined(__clang__) || defined(__GNUC__)
135 # define FOLLY_PACK_ATTR __attribute__((__packed__))
136 # define FOLLY_PACK_PUSH /**/
137 # define FOLLY_PACK_POP /**/
138 #else
139 # define FOLLY_PACK_ATTR /**/
140 # define FOLLY_PACK_PUSH /**/
141 # define FOLLY_PACK_POP /**/
142 #endif
143
144 // portable version check
145 #ifndef __GNUC_PREREQ
146 # if defined __GNUC__ && defined __GNUC_MINOR__
147 /* nolint */
148 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
149                                    ((maj) << 16) + (min))
150 # else
151 /* nolint */
152 #  define __GNUC_PREREQ(maj, min) 0
153 # endif
154 #endif
155
156
157 /* Define macro wrappers for C++11's "final" and "override" keywords, which
158  * are supported in gcc 4.7 but not gcc 4.6. */
159 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
160 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
161 #  define FOLLY_FINAL final
162 #  define FOLLY_OVERRIDE override
163 # elif defined(_MSC_VER) && _MSC_VER >= 1600
164 #  define FOLLY_FINAL final
165 #  define FOLLY_OVERRIDE override
166 # else
167 #  define FOLLY_FINAL /**/
168 #  define FOLLY_OVERRIDE /**/
169 # endif
170 #endif
171
172 /* Platform specific TLS support
173  * gcc implements __thread
174  * msvc implements __declspec(thread)
175  * the semantics are the same
176  * (but remember __thread has different semantics when using emutls (ex. apple))
177  */
178 #if defined(_MSC_VER)
179 # define FOLLY_TLS __declspec(thread)
180 #elif defined(__GNUC__) || defined(__clang__)
181 # define FOLLY_TLS __thread
182 #else
183 # error cannot define platform specific thread local storage
184 #endif
185
186 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
187 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
188 # if defined(__GLIBC_PREREQ)
189 #  if __GLIBC_PREREQ(2, 10)
190 #   define FOLLY_HAVE_PREADV 1
191 #   define FOLLY_HAVE_PWRITEV 1
192 #  endif
193 # endif
194 #endif
195
196 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
197 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
198 // up in a macro to make forward-declarations easier.
199 #if FOLLY_USE_LIBCPP
200 #include <__config>
201 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
202 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
203 #else
204 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
205 #define FOLLY_NAMESPACE_STD_END       }
206 #endif
207
208 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
209 // versions of these into the global namespace.
210 #if FOLLY_HAVE_CLOCK_GETTIME
211 #include <time.h>
212 #else
213 #include <folly/detail/Clock.h>
214 #endif
215
216 // Provide our own std::__throw_* wrappers for platforms that don't have them
217 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
218 #include <bits/functexcept.h>
219 #else
220 #include <folly/detail/FunctionalExcept.h>
221 #endif
222
223 #if defined(__cplusplus)
224 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
225 // usage of __has_trivial_copy(), so we can't use it as a
226 // least-common-denominator for C++11 implementations that don't support
227 // std::is_trivially_copyable<T>.
228 //
229 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
230 //
231 // As a result, use std::is_trivially_copyable() where it exists, and fall back
232 // to Boost otherwise.
233 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
234 #include <type_traits>
235 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
236   (std::is_trivially_copyable<T>::value)
237 #else
238 #include <boost/type_traits.hpp>
239 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
240   (boost::has_trivial_copy<T>::value &&                  \
241    boost::has_trivial_destructor<T>::value)
242 #endif
243 #endif // __cplusplus
244
245 // MSVC specific defines
246 // mainly for posix compat
247 #ifdef _MSC_VER
248
249 // this definition is in a really silly place with a silly name
250 // and ifdefing it every time we want it is painful
251 #include <basetsd.h>
252 typedef SSIZE_T ssize_t;
253
254 // sprintf semantics are not exactly identical
255 // but current usage is not a problem
256 # define snprintf _snprintf
257
258 // semantics here are identical
259 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
260
261 // compiler specific to compiler specific
262 // nolint
263 # define __PRETTY_FUNCTION__ __FUNCSIG__
264
265 // Hide a GCC specific thing that breaks MSVC if left alone.
266 # define __extension__
267
268 #ifdef _M_IX86_FP
269 # define FOLLY_SSE _M_IX86_FP
270 #endif
271
272 #endif
273
274 #ifndef FOLLY_SSE
275 # if defined(__SSE4_2__)
276 #  define FOLLY_SSE 4.2
277 # elif defined(__SSE4_1__)
278 #  define FOLLY_SSE 4.1
279 # elif defined(__SSE4__)
280 #  define FOLLY_SSE 4
281 # elif defined(__SSE3__)
282 #  define FOLLY_SSE 3
283 # elif defined(__SSE2__)
284 #  define FOLLY_SSE 2
285 # elif defined(__SSE__)
286 #  define FOLLY_SSE 1
287 # else
288 #  define FOLLY_SSE 0
289 # endif
290 #endif
291
292 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
293 namespace FOLLY_GFLAGS_NAMESPACE { }
294 namespace gflags {
295 using namespace FOLLY_GFLAGS_NAMESPACE;
296 }  // namespace gflags
297 #endif
298
299 // for TARGET_OS_IPHONE
300 #ifdef __APPLE__
301 #include <TargetConditionals.h>
302 #endif
303
304 // MacOS doesn't have malloc_usable_size()
305 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
306 inline size_t malloc_usable_size(void* ptr) {
307   return malloc_size(ptr);
308 }
309 #endif
310
311 // RTTI may not be enabled for this compilation unit.
312 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
313     (defined(_MSC_VER) && defined(_CPPRTTI))
314 # define FOLLY_HAS_RTTI 1
315 #endif
316
317 #ifdef _MSC_VER
318 # include <intrin.h>
319 #endif
320
321 namespace folly {
322
323 inline void asm_volatile_pause() {
324 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
325   ::_mm_pause();
326 #elif defined(__i386__) || FOLLY_X64
327   asm volatile ("pause");
328 #elif FOLLY_A64
329   asm volatile ("wfe");
330 #endif
331 }
332 inline void asm_pause() {
333 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
334   ::_mm_pause();
335 #elif defined(__i386__) || FOLLY_X64
336   asm ("pause");
337 #elif FOLLY_A64
338   asm ("wfe");
339 #endif
340 }
341
342 }
343
344 #endif // FOLLY_PORTABILITY_H_