From 779cbf35527d33c4073a3c8929e284e9a16b0e50 Mon Sep 17 00:00:00 2001 From: Elizabeth Smith Date: Fri, 18 Apr 2014 08:18:54 -0700 Subject: [PATCH] macro for cross platform x64 detection Summary: Another cross platform fun macro - this one detects x64 status and requires some portability.h includes (which should only define portability stuff and not actually include anything) Test Plan: fbmake runtests Reviewed By: delong.j@fb.com FB internal diff: D1282140 --- folly/Baton.h | 2 +- folly/Checksum.cpp | 2 +- folly/CpuId.h | 3 ++- folly/DiscriminatedPtr.h | 3 ++- folly/GroupVarint.h | 6 ++++-- folly/PackedSyncPtr.h | 4 +++- folly/Portability.h | 7 +++++++ folly/RWSpinLock.h | 2 +- folly/SmallLocks.h | 5 ++--- folly/build/generate_varint_tables.py | 6 ++++-- folly/detail/AtomicHashUtils.h | 2 +- folly/detail/MemoryIdler.cpp | 2 +- folly/experimental/EliasFanoCoding.h | 2 +- folly/io/async/EventFDWrapper.h | 26 ++++++++++++-------------- folly/small_vector.h | 6 +++--- folly/test/small_vector_test.cpp | 6 +++--- 16 files changed, 48 insertions(+), 36 deletions(-) diff --git a/folly/Baton.h b/folly/Baton.h index c53afa35..86b2e013 100644 --- a/folly/Baton.h +++ b/folly/Baton.h @@ -135,7 +135,7 @@ struct Baton : boost::noncopyable { return; } assert(before == INIT); -#ifdef __x86_64__ +#if FOLLY_X64 // The pause instruction is the polite way to spin, but it doesn't // actually affect correctness to omit it if we don't have it. // Pausing donates the full capabilities of the current core to diff --git a/folly/Checksum.cpp b/folly/Checksum.cpp index 28ed8392..ba32d687 100644 --- a/folly/Checksum.cpp +++ b/folly/Checksum.cpp @@ -24,7 +24,7 @@ namespace folly { namespace detail { -#if defined(__x86_64__) && defined(__GNUC__) && defined(__GNUC_MINOR__) && \ +#if FOLLY_X64 && defined(__GNUC__) && defined(__GNUC_MINOR__) && \ (((__GNUC__ * 100) + __GNUC_MINOR__) >= 407) // Fast SIMD implementation of CRC-32C for x86 with SSE 4.2 diff --git a/folly/CpuId.h b/folly/CpuId.h index 12e9b074..facabe18 100644 --- a/folly/CpuId.h +++ b/folly/CpuId.h @@ -18,6 +18,7 @@ #define FOLLY_CPUID_H_ #include +#include "folly/Portability.h" namespace folly { @@ -29,7 +30,7 @@ namespace folly { class CpuId { public: CpuId() { -#if defined(__x86_64__) || defined(__i386__) +#if FOLLY_X64 || defined(__i386__) __asm__("cpuid" : "=c"(c_), "=d"(d_) : "a"(1) : "ebx"); #else // On non-Intel, none of these features exist; at least not in the same form diff --git a/folly/DiscriminatedPtr.h b/folly/DiscriminatedPtr.h index af669549..b2e363eb 100644 --- a/folly/DiscriminatedPtr.h +++ b/folly/DiscriminatedPtr.h @@ -31,9 +31,10 @@ #include #include #include "folly/Likely.h" +#include "folly/Portability.h" #include "folly/detail/DiscriminatedPtrDetail.h" -#ifndef __x86_64__ +#if !FOLLY_X64 # error "DiscriminatedPtr is x64-specific code." #endif diff --git a/folly/GroupVarint.h b/folly/GroupVarint.h index cbfce4b0..5c506a2f 100644 --- a/folly/GroupVarint.h +++ b/folly/GroupVarint.h @@ -21,7 +21,9 @@ #error GroupVarint.h requires GCC #endif -#if defined(__x86_64__) || defined(__i386__) +#include "folly/Portability.h" + +#if FOLLY_X64 || defined(__i386__) #define HAVE_GROUP_VARINT 1 #include @@ -607,6 +609,6 @@ typedef GroupVarintDecoder GroupVarint64Decoder; } // namespace folly -#endif /* defined(__x86_64__) || defined(__i386__) */ +#endif /* FOLLY_X64 || defined(__i386__) */ #endif /* FOLLY_GROUPVARINT_H_ */ diff --git a/folly/PackedSyncPtr.h b/folly/PackedSyncPtr.h index 0dfa1e98..fa9fbd5e 100644 --- a/folly/PackedSyncPtr.h +++ b/folly/PackedSyncPtr.h @@ -17,7 +17,9 @@ #ifndef FOLLY_PACKEDSYNCPTR_H_ #define FOLLY_PACKEDSYNCPTR_H_ -#ifndef __x86_64__ +#include "folly/Portability.h" + +#if !FOLLY_X64 # error "PackedSyncPtr is x64-specific code." #endif diff --git a/folly/Portability.h b/folly/Portability.h index d3edde9e..43953f93 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -72,6 +72,13 @@ struct MaxAlign { char c; } __attribute__((aligned)); # define FOLLY_ALWAYS_INLINE #endif +// detection for 64 bit +#if defined(__x86_64__) || defined(_M_X64) +# define FOLLY_X64 1 +#else +# define FOLLY_X64 0 +#endif + // portable version check #ifndef __GNUC_PREREQ # if defined __GNUC__ && defined __GNUC_MINOR__ diff --git a/folly/RWSpinLock.h b/folly/RWSpinLock.h index 33bce2cd..c52f7e8b 100644 --- a/folly/RWSpinLock.h +++ b/folly/RWSpinLock.h @@ -107,7 +107,7 @@ pthread_rwlock_t Read 728698 24us 101ns 7.28ms 194us */ #if defined(__GNUC__) && !defined(__clang__) && \ - (defined(__i386) || defined(__x86_64__) || \ + (defined(__i386) || FOLLY_X64 || \ defined(ARCH_K8)) #define RW_SPINLOCK_USE_X86_INTRINSIC_ #include diff --git a/folly/SmallLocks.h b/folly/SmallLocks.h index 8bd1900b..b8f081a6 100644 --- a/folly/SmallLocks.h +++ b/folly/SmallLocks.h @@ -44,13 +44,12 @@ #include #include +#include "folly/Portability.h" -#ifndef __x86_64__ +#if !FOLLY_X64 # error "SmallLocks.h is currently x64-only." #endif -#include "folly/Portability.h" - namespace folly { ////////////////////////////////////////////////////////////////////// diff --git a/folly/build/generate_varint_tables.py b/folly/build/generate_varint_tables.py index 19589edc..60fe683e 100755 --- a/folly/build/generate_varint_tables.py +++ b/folly/build/generate_varint_tables.py @@ -52,7 +52,9 @@ OUTPUT_FILE = "GroupVarintTables.cpp" def generate(f): f.write(""" -#if defined(__x86_64__) || defined(__i386__) +#include "folly/Portability.h" + +#if FOLLY_X64 || defined(__i386__) #include #include @@ -96,7 +98,7 @@ extern const __m128i groupVarintSSEMasks[] = { } // namespace detail } // namespace folly -#endif /* defined(__x86_64__) || defined(__i386__) */ +#endif /* FOLLY_X64 || defined(__i386__) */ """) def main(): diff --git a/folly/detail/AtomicHashUtils.h b/folly/detail/AtomicHashUtils.h index 6ce2125e..a06db36b 100644 --- a/folly/detail/AtomicHashUtils.h +++ b/folly/detail/AtomicHashUtils.h @@ -19,7 +19,7 @@ // Note: no include guard; different -inl.h files include this and // undef it more than once in a translation unit. -#if !(defined(__x86__) || defined(__i386__) || defined(__x86_64__)) +#if !(defined(__x86__) || defined(__i386__) || FOLLY_X64) #define FOLLY_SPIN_WAIT(condition) \ for (int counter = 0; condition; ++counter) { \ if (counter < 10000) continue; \ diff --git a/folly/detail/MemoryIdler.cpp b/folly/detail/MemoryIdler.cpp index 979d9329..3e01ee44 100644 --- a/folly/detail/MemoryIdler.cpp +++ b/folly/detail/MemoryIdler.cpp @@ -87,7 +87,7 @@ void MemoryIdler::flushLocalMallocCaches() { } -#ifdef __x86_64__ +#if FOLLY_X64 static const size_t s_pageSize = sysconf(_SC_PAGESIZE); static FOLLY_TLS uintptr_t tls_stackLimit; diff --git a/folly/experimental/EliasFanoCoding.h b/folly/experimental/EliasFanoCoding.h index c8e8e3cf..f5222f8c 100644 --- a/folly/experimental/EliasFanoCoding.h +++ b/folly/experimental/EliasFanoCoding.h @@ -28,7 +28,7 @@ #error EliasFanoCoding.h requires GCC #endif -#if !defined(__x86_64__) +#if !FOLLY_X64 #error EliasFanoCoding.h requires x86_64 #endif diff --git a/folly/io/async/EventFDWrapper.h b/folly/io/async/EventFDWrapper.h index 5df88fcb..294d9d37 100644 --- a/folly/io/async/EventFDWrapper.h +++ b/folly/io/async/EventFDWrapper.h @@ -1,21 +1,19 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Copyright 2014 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + /** * Work around the lack of on glibc 2.5.1 which we still * need to support, sigh. @@ -38,7 +36,7 @@ // Values from the Linux kernel source: // arch/x86/include/asm/unistd_{32,64}.h #ifndef __NR_eventfd2 -#if defined(__x86_64__) +#if FOLLY_X64 #define __NR_eventfd2 290 #elif defined(__i386__) #define __NR_eventfd2 328 diff --git a/folly/small_vector.h b/folly/small_vector.h index 03c90a38..06d2a305 100644 --- a/folly/small_vector.h +++ b/folly/small_vector.h @@ -48,7 +48,7 @@ #include "folly/Malloc.h" -#if defined(__GNUC__) && defined(__x86_64__) +#if defined(__GNUC__) && FOLLY_X64 # include "folly/SmallLocks.h" # define FB_PACKED __attribute__((packed)) #else @@ -268,7 +268,7 @@ namespace detail { SizeType size_; }; -#ifdef __x86_64__ +#if FOLLY_X64 template struct OneBitMutexImpl { typedef SizeType InternalSizeType; @@ -1100,7 +1100,7 @@ private: } } FB_PACKED; -#if defined(__x86_64_) +#if FOLLY_X64 typedef unsigned char InlineStorageType[sizeof(value_type) * MaxInline]; #else typedef typename std::aligned_storage< diff --git a/folly/test/small_vector_test.cpp b/folly/test/small_vector_test.cpp index 57b84a4e..5c55a416 100644 --- a/folly/test/small_vector_test.cpp +++ b/folly/test/small_vector_test.cpp @@ -29,7 +29,7 @@ using folly::small_vector; using namespace folly::small_vector_policy; -#if defined(__x86_64__) +#if FOLLY_X64 static_assert(sizeof(small_vector) == 16, "Object size is not what we expect for small_vector"); @@ -602,14 +602,14 @@ TEST(small_vector, AllHeap) { TEST(small_vector, Basic) { typedef folly::small_vector Vector; Vector a; -#ifdef __x86_64__ +#if FOLLY_X64 a.lock(); a.unlock(); #endif -- 2.34.1