Clone of PhatAC @ https://github.com/floaterxk/PhatAC

my_compiler.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef MY_COMPILER_INCLUDED
  2. #define MY_COMPILER_INCLUDED
  3. /* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. /**
  15. Header for compiler-dependent features.
  16. Intended to contain a set of reusable wrappers for preprocessor
  17. macros, attributes, pragmas, and any other features that are
  18. specific to a target compiler.
  19. */
  20. #include <stddef.h> /* size_t */
  21. #if defined __GNUC__
  22. /*
  23. Convenience macro to test the minimum required GCC version.
  24. These should be used with care as Clang also sets __GNUC__ and
  25. __GNUC_MINOR__ (currently to 4.2). Prefer using feature specific
  26. CMake checks in configure.cmake instead.
  27. */
  28. # define MY_GNUC_PREREQ(maj, min) \
  29. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  30. # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  31. #else
  32. # define MY_GNUC_PREREQ(maj, min) (0)
  33. #endif
  34. /*
  35. The macros below are borrowed from include/linux/compiler.h in the
  36. Linux kernel. Use them to indicate the likelyhood of the truthfulness
  37. of a condition. This serves two purposes - newer versions of gcc will be
  38. able to optimize for branch predication, which could yield siginficant
  39. performance gains in frequently executed sections of the code, and the
  40. other reason to use them is for documentation
  41. */
  42. #ifdef HAVE_BUILTIN_EXPECT
  43. // likely/unlikely are likely to clash with other symbols, do not #define
  44. #if defined(__cplusplus)
  45. inline bool likely(bool expr)
  46. {
  47. return __builtin_expect(expr, true);
  48. }
  49. inline bool unlikely(bool expr)
  50. {
  51. return __builtin_expect(expr, false);
  52. }
  53. #else
  54. # define likely(x) __builtin_expect((x),1)
  55. # define unlikely(x) __builtin_expect((x),0)
  56. #endif
  57. #else /* HAVE_BUILTIN_EXPECT */
  58. #if defined(__cplusplus)
  59. inline bool likely(bool expr)
  60. {
  61. return expr;
  62. }
  63. inline bool unlikely(bool expr)
  64. {
  65. return expr;
  66. }
  67. #else
  68. # define likely(x) (x)
  69. # define unlikely(x) (x)
  70. #endif
  71. #endif /* HAVE_BUILTIN_EXPECT */
  72. /* Comunicate to the compiler the unreachability of the code. */
  73. #ifdef HAVE_BUILTIN_UNREACHABLE
  74. # define MY_ASSERT_UNREACHABLE() __builtin_unreachable()
  75. #else
  76. # define MY_ASSERT_UNREACHABLE() do { assert(0); } while (0)
  77. #endif
  78. #if defined __GNUC__ || defined __SUNPRO_C || defined __SUNPRO_CC
  79. /* Specifies the minimum alignment of a type. */
  80. # define MY_ALIGNOF(type) __alignof__(type)
  81. /* Determine the alignment requirement of a type. */
  82. # define MY_ALIGNED(n) __attribute__((__aligned__((n))))
  83. /* Microsoft Visual C++ */
  84. #elif defined _MSC_VER
  85. # define MY_ALIGNOF(type) __alignof(type)
  86. # define MY_ALIGNED(n) __declspec(align(n))
  87. #else /* Make sure they are defined for other compilers. */
  88. # define MY_ALIGNOF(type)
  89. # define MY_ALIGNED(size)
  90. #endif
  91. /* Visual Studio requires '__inline' for C code */
  92. #if !defined(__cplusplus) && defined(_MSC_VER)
  93. # define inline __inline
  94. #endif
  95. /* Provide __func__ macro definition for Visual Studio. */
  96. #if defined(_MSC_VER)
  97. # define __func__ __FUNCTION__
  98. #endif
  99. /**
  100. C++ Type Traits
  101. */
  102. #ifdef __cplusplus
  103. /**
  104. Opaque storage with a particular alignment.
  105. Partial specialization used due to MSVC++.
  106. */
  107. template<size_t alignment> struct my_alignment_imp;
  108. template<> struct MY_ALIGNED(1) my_alignment_imp<1> {};
  109. template<> struct MY_ALIGNED(2) my_alignment_imp<2> {};
  110. template<> struct MY_ALIGNED(4) my_alignment_imp<4> {};
  111. template<> struct MY_ALIGNED(8) my_alignment_imp<8> {};
  112. template<> struct MY_ALIGNED(16) my_alignment_imp<16> {};
  113. /**
  114. A POD type with a given size and alignment.
  115. @remark If the compiler does not support a alignment attribute
  116. (MY_ALIGN macro), the default alignment of a double is
  117. used instead.
  118. @tparam size The minimum size.
  119. @tparam alignment The desired alignment: 1, 2, 4, 8 or 16.
  120. */
  121. template <size_t size, size_t alignment>
  122. struct my_aligned_storage
  123. {
  124. union
  125. {
  126. char data[size];
  127. my_alignment_imp<alignment> align;
  128. };
  129. };
  130. #endif /* __cplusplus */
  131. /*
  132. Disable MY_ATTRIBUTE for Sun Studio and Visual Studio.
  133. Note that Sun Studio supports some __attribute__ variants,
  134. but not format or unused which we use quite a lot.
  135. */
  136. #ifndef MY_ATTRIBUTE
  137. #if defined(__GNUC__)
  138. # define MY_ATTRIBUTE(A) __attribute__(A)
  139. #else
  140. # define MY_ATTRIBUTE(A)
  141. #endif
  142. #endif
  143. #ifndef __has_attribute
  144. # define __has_attribute(x) 0
  145. #endif
  146. #if __has_attribute(no_sanitize_undefined)
  147. # define SUPPRESS_UBSAN __attribute__((no_sanitize_undefined))
  148. #else
  149. # define SUPPRESS_UBSAN
  150. #endif
  151. #endif /* MY_COMPILER_INCLUDED */