gcc_generic.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #if !defined(__TBB_machine_H) || defined(__TBB_machine_gcc_generic_H)
  14. #error Do not #include this internal file directly; use public TBB headers instead.
  15. #endif
  16. #define __TBB_machine_gcc_generic_H
  17. #include <stdint.h>
  18. #include <unistd.h>
  19. #define __TBB_WORDSIZE __SIZEOF_POINTER__
  20. #if __TBB_GCC_64BIT_ATOMIC_BUILTINS_BROKEN
  21. #define __TBB_64BIT_ATOMICS 0
  22. #endif
  23. /** FPU control setting not available for non-Intel architectures on Android **/
  24. #if __ANDROID__ && __TBB_generic_arch
  25. #define __TBB_CPU_CTL_ENV_PRESENT 0
  26. #endif
  27. // __BYTE_ORDER__ is used in accordance with http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html,
  28. // but __BIG_ENDIAN__ or __LITTLE_ENDIAN__ may be more commonly found instead.
  29. #if __BIG_ENDIAN__ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__)
  30. #define __TBB_ENDIANNESS __TBB_ENDIAN_BIG
  31. #elif __LITTLE_ENDIAN__ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)
  32. #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
  33. #elif defined(__BYTE_ORDER__)
  34. #define __TBB_ENDIANNESS __TBB_ENDIAN_UNSUPPORTED
  35. #else
  36. #define __TBB_ENDIANNESS __TBB_ENDIAN_DETECT
  37. #endif
  38. #if __TBB_GCC_VERSION < 40700
  39. // Use __sync_* builtins
  40. /** As this generic implementation has absolutely no information about underlying
  41. hardware, its performance most likely will be sub-optimal because of full memory
  42. fence usages where a more lightweight synchronization means (or none at all)
  43. could suffice. Thus if you use this header to enable TBB on a new platform,
  44. consider forking it and relaxing below helpers as appropriate. **/
  45. #define __TBB_acquire_consistency_helper() __sync_synchronize()
  46. #define __TBB_release_consistency_helper() __sync_synchronize()
  47. #define __TBB_full_memory_fence() __sync_synchronize()
  48. #define __TBB_control_consistency_helper() __sync_synchronize()
  49. #define __TBB_MACHINE_DEFINE_ATOMICS(S,T) \
  50. inline T __TBB_machine_cmpswp##S( volatile void *ptr, T value, T comparand ) { \
  51. return __sync_val_compare_and_swap(reinterpret_cast<volatile T *>(ptr),comparand,value); \
  52. } \
  53. inline T __TBB_machine_fetchadd##S( volatile void *ptr, T value ) { \
  54. return __sync_fetch_and_add(reinterpret_cast<volatile T *>(ptr),value); \
  55. }
  56. #define __TBB_USE_GENERIC_FETCH_STORE 1
  57. #else
  58. // __TBB_GCC_VERSION >= 40700; use __atomic_* builtins available since gcc 4.7
  59. #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
  60. // Acquire and release fence intrinsics in GCC might miss compiler fence.
  61. // Adding it at both sides of an intrinsic, as we do not know what reordering can be made.
  62. #define __TBB_acquire_consistency_helper() __TBB_compiler_fence(); __atomic_thread_fence(__ATOMIC_ACQUIRE); __TBB_compiler_fence()
  63. #define __TBB_release_consistency_helper() __TBB_compiler_fence(); __atomic_thread_fence(__ATOMIC_RELEASE); __TBB_compiler_fence()
  64. #define __TBB_full_memory_fence() __atomic_thread_fence(__ATOMIC_SEQ_CST)
  65. #define __TBB_control_consistency_helper() __TBB_acquire_consistency_helper()
  66. #define __TBB_MACHINE_DEFINE_ATOMICS(S,T) \
  67. inline T __TBB_machine_cmpswp##S( volatile void *ptr, T value, T comparand ) { \
  68. (void)__atomic_compare_exchange_n(reinterpret_cast<volatile T *>(ptr), &comparand, value, \
  69. false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
  70. return comparand; \
  71. } \
  72. inline T __TBB_machine_fetchadd##S( volatile void *ptr, T value ) { \
  73. return __atomic_fetch_add(reinterpret_cast<volatile T *>(ptr), value, __ATOMIC_SEQ_CST); \
  74. } \
  75. inline T __TBB_machine_fetchstore##S( volatile void *ptr, T value ) { \
  76. return __atomic_exchange_n(reinterpret_cast<volatile T *>(ptr), value, __ATOMIC_SEQ_CST); \
  77. }
  78. #endif // __TBB_GCC_VERSION < 40700
  79. __TBB_MACHINE_DEFINE_ATOMICS(1,int8_t)
  80. __TBB_MACHINE_DEFINE_ATOMICS(2,int16_t)
  81. __TBB_MACHINE_DEFINE_ATOMICS(4,int32_t)
  82. __TBB_MACHINE_DEFINE_ATOMICS(8,int64_t)
  83. #undef __TBB_MACHINE_DEFINE_ATOMICS
  84. typedef unsigned char __TBB_Flag;
  85. typedef __TBB_atomic __TBB_Flag __TBB_atomic_flag;
  86. #if __TBB_GCC_VERSION < 40700
  87. // Use __sync_* builtins
  88. // Use generic machine_load_store functions if there are no builtin atomics
  89. #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
  90. #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
  91. #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1
  92. static inline void __TBB_machine_or( volatile void *ptr, uintptr_t addend ) {
  93. __sync_fetch_and_or(reinterpret_cast<volatile uintptr_t *>(ptr),addend);
  94. }
  95. static inline void __TBB_machine_and( volatile void *ptr, uintptr_t addend ) {
  96. __sync_fetch_and_and(reinterpret_cast<volatile uintptr_t *>(ptr),addend);
  97. }
  98. inline bool __TBB_machine_try_lock_byte( __TBB_atomic_flag &flag ) {
  99. return __sync_lock_test_and_set(&flag,1)==0;
  100. }
  101. inline void __TBB_machine_unlock_byte( __TBB_atomic_flag &flag ) {
  102. __sync_lock_release(&flag);
  103. }
  104. #else
  105. // __TBB_GCC_VERSION >= 40700; use __atomic_* builtins available since gcc 4.7
  106. static inline void __TBB_machine_or( volatile void *ptr, uintptr_t addend ) {
  107. __atomic_fetch_or(reinterpret_cast<volatile uintptr_t *>(ptr),addend,__ATOMIC_SEQ_CST);
  108. }
  109. static inline void __TBB_machine_and( volatile void *ptr, uintptr_t addend ) {
  110. __atomic_fetch_and(reinterpret_cast<volatile uintptr_t *>(ptr),addend,__ATOMIC_SEQ_CST);
  111. }
  112. inline bool __TBB_machine_try_lock_byte( __TBB_atomic_flag &flag ) {
  113. return !__atomic_test_and_set(&flag,__ATOMIC_ACQUIRE);
  114. }
  115. inline void __TBB_machine_unlock_byte( __TBB_atomic_flag &flag ) {
  116. __atomic_clear(&flag,__ATOMIC_RELEASE);
  117. }
  118. namespace tbb { namespace internal {
  119. /** GCC atomic operation intrinsics might miss compiler fence.
  120. Adding it after load-with-acquire, before store-with-release, and
  121. on both sides of sequentially consistent operations is sufficient for correctness. **/
  122. template <typename T, int MemOrder>
  123. inline T __TBB_machine_atomic_load( const volatile T& location) {
  124. if (MemOrder == __ATOMIC_SEQ_CST) __TBB_compiler_fence();
  125. T value = __atomic_load_n(&location, MemOrder);
  126. if (MemOrder != __ATOMIC_RELAXED) __TBB_compiler_fence();
  127. return value;
  128. }
  129. template <typename T, int MemOrder>
  130. inline void __TBB_machine_atomic_store( volatile T& location, T value) {
  131. if (MemOrder != __ATOMIC_RELAXED) __TBB_compiler_fence();
  132. __atomic_store_n(&location, value, MemOrder);
  133. if (MemOrder == __ATOMIC_SEQ_CST) __TBB_compiler_fence();
  134. }
  135. template <typename T, size_t S>
  136. struct machine_load_store {
  137. static T load_with_acquire ( const volatile T& location ) {
  138. return __TBB_machine_atomic_load<T, __ATOMIC_ACQUIRE>(location);
  139. }
  140. static void store_with_release ( volatile T &location, T value ) {
  141. __TBB_machine_atomic_store<T, __ATOMIC_RELEASE>(location, value);
  142. }
  143. };
  144. template <typename T, size_t S>
  145. struct machine_load_store_relaxed {
  146. static inline T load ( const volatile T& location ) {
  147. return __TBB_machine_atomic_load<T, __ATOMIC_RELAXED>(location);
  148. }
  149. static inline void store ( volatile T& location, T value ) {
  150. __TBB_machine_atomic_store<T, __ATOMIC_RELAXED>(location, value);
  151. }
  152. };
  153. template <typename T, size_t S>
  154. struct machine_load_store_seq_cst {
  155. static T load ( const volatile T& location ) {
  156. return __TBB_machine_atomic_load<T, __ATOMIC_SEQ_CST>(location);
  157. }
  158. static void store ( volatile T &location, T value ) {
  159. __TBB_machine_atomic_store<T, __ATOMIC_SEQ_CST>(location, value);
  160. }
  161. };
  162. }} // namespace tbb::internal
  163. #endif // __TBB_GCC_VERSION < 40700
  164. // Machine specific atomic operations
  165. #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
  166. #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
  167. #define __TBB_TryLockByte __TBB_machine_try_lock_byte
  168. #define __TBB_UnlockByte __TBB_machine_unlock_byte
  169. // __builtin_clz counts the number of leading zeroes
  170. namespace tbb{ namespace internal { namespace gcc_builtins {
  171. inline int clz(unsigned int x){ return __builtin_clz(x); }
  172. inline int clz(unsigned long int x){ return __builtin_clzl(x); }
  173. inline int clz(unsigned long long int x){ return __builtin_clzll(x); }
  174. }}}
  175. // logarithm is the index of the most significant non-zero bit
  176. static inline intptr_t __TBB_machine_lg( uintptr_t x ) {
  177. // If P is a power of 2 and x<P, then (P-1)-x == (P-1) XOR x
  178. return (sizeof(x)*8 - 1) ^ tbb::internal::gcc_builtins::clz(x);
  179. }
  180. #define __TBB_Log2(V) __TBB_machine_lg(V)
  181. #if __TBB_WORDSIZE==4
  182. #define __TBB_USE_GENERIC_DWORD_LOAD_STORE 1
  183. #endif
  184. #if __TBB_x86_32 || __TBB_x86_64
  185. #include "gcc_ia32_common.h"
  186. #endif