1 /* Copyright 2015 The TensorFlow Authors. 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 16 #ifndef TENSORFLOW_PLATFORM_MACROS_H_ 17 #define TENSORFLOW_PLATFORM_MACROS_H_ 18 19 // Compiler attributes 20 #if (defined(__GNUC__) || defined(__APPLE__)) && !defined(SWIG) 21 // Compiler supports GCC-style attributes 22 #define TF_ATTRIBUTE_NORETURN __attribute__((noreturn)) 23 #define TF_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 24 #define TF_ATTRIBUTE_NOINLINE __attribute__((noinline)) 25 #define TF_ATTRIBUTE_UNUSED __attribute__((unused)) 26 #define TF_ATTRIBUTE_COLD __attribute__((cold)) 27 #define TF_ATTRIBUTE_WEAK __attribute__((weak)) 28 #define TF_PACKED __attribute__((packed)) 29 #define TF_MUST_USE_RESULT __attribute__((warn_unused_result)) 30 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) \ 31 __attribute__((__format__(__printf__, string_index, first_to_check))) 32 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) \ 33 __attribute__((__format__(__scanf__, string_index, first_to_check))) 34 #elif defined(COMPILER_MSVC) 35 // Non-GCC equivalents 36 #define TF_ATTRIBUTE_NORETURN __declspec(noreturn) 37 #define TF_ATTRIBUTE_ALWAYS_INLINE 38 #define TF_ATTRIBUTE_NOINLINE 39 #define TF_ATTRIBUTE_UNUSED 40 #define TF_ATTRIBUTE_COLD 41 #define TF_MUST_USE_RESULT 42 #define TF_PACKED 43 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) 44 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) 45 #else 46 // Non-GCC equivalents 47 #define TF_ATTRIBUTE_NORETURN 48 #define TF_ATTRIBUTE_ALWAYS_INLINE 49 #define TF_ATTRIBUTE_NOINLINE 50 #define TF_ATTRIBUTE_UNUSED 51 #define TF_ATTRIBUTE_COLD 52 #define TF_ATTRIBUTE_WEAK 53 #define TF_MUST_USE_RESULT 54 #define TF_PACKED 55 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) 56 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) 57 #endif 58 59 // Control visiblity outside .so 60 #if defined(COMPILER_MSVC) 61 #ifdef TF_COMPILE_LIBRARY 62 #define TF_EXPORT __declspec(dllexport) 63 #else 64 #define TF_EXPORT __declspec(dllimport) 65 #endif // TF_COMPILE_LIBRARY 66 #else 67 #define TF_EXPORT __attribute__((visibility("default"))) 68 #endif // COMPILER_MSVC 69 70 // GCC can be told that a certain branch is not likely to be taken (for 71 // instance, a CHECK failure), and use that information in static analysis. 72 // Giving it this information can help it optimize for the common case in 73 // the absence of better information (ie. -fprofile-arcs). 74 #if defined(COMPILER_GCC3) 75 #define TF_PREDICT_FALSE(x) (__builtin_expect(x, 0)) 76 #define TF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 77 #else 78 #define TF_PREDICT_FALSE(x) (x) 79 #define TF_PREDICT_TRUE(x) (x) 80 #endif 81 82 // A macro to disallow the copy constructor and operator= functions 83 // This is usually placed in the private: declarations for a class. 84 #define TF_DISALLOW_COPY_AND_ASSIGN(TypeName) \ 85 TypeName(const TypeName&) = delete; \ 86 void operator=(const TypeName&) = delete 87 88 // The TF_ARRAYSIZE(arr) macro returns the # of elements in an array arr. 89 // 90 // The expression TF_ARRAYSIZE(a) is a compile-time constant of type 91 // size_t. 92 #define TF_ARRAYSIZE(a) \ 93 ((sizeof(a) / sizeof(*(a))) / \ 94 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 95 96 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ 97 (defined(_MSC_VER) && _MSC_VER >= 1900) 98 // Define this to 1 if the code is compiled in C++11 mode; leave it 99 // undefined otherwise. Do NOT define it to 0 -- that causes 100 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. 101 #define LANG_CXX11 1 102 #endif 103 104 #if defined(__clang__) && defined(LANG_CXX11) && defined(__has_warning) 105 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") 106 #define TF_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT 107 #endif 108 #endif 109 110 #ifndef TF_FALLTHROUGH_INTENDED 111 #define TF_FALLTHROUGH_INTENDED \ 112 do { \ 113 } while (0) 114 #endif 115 116 #endif // TENSORFLOW_PLATFORM_MACROS_H_ 117