linux_ia32.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_linux_ia32_H)
  14. #error Do not #include this internal file directly; use public TBB headers instead.
  15. #endif
  16. #define __TBB_machine_linux_ia32_H
  17. #include <stdint.h>
  18. #include "gcc_ia32_common.h"
  19. #define __TBB_WORDSIZE 4
  20. #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
  21. #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
  22. #define __TBB_control_consistency_helper() __TBB_compiler_fence()
  23. #define __TBB_acquire_consistency_helper() __TBB_compiler_fence()
  24. #define __TBB_release_consistency_helper() __TBB_compiler_fence()
  25. #define __TBB_full_memory_fence() __asm__ __volatile__("mfence": : :"memory")
  26. #if __TBB_ICC_ASM_VOLATILE_BROKEN
  27. #define __TBB_VOLATILE
  28. #else
  29. #define __TBB_VOLATILE volatile
  30. #endif
  31. #define __TBB_MACHINE_DEFINE_ATOMICS(S,T,X,R) \
  32. static inline T __TBB_machine_cmpswp##S (volatile void *ptr, T value, T comparand ) \
  33. { \
  34. T result; \
  35. \
  36. __asm__ __volatile__("lock\ncmpxchg" X " %2,%1" \
  37. : "=a"(result), "=m"(*(__TBB_VOLATILE T*)ptr) \
  38. : "q"(value), "0"(comparand), "m"(*(__TBB_VOLATILE T*)ptr) \
  39. : "memory"); \
  40. return result; \
  41. } \
  42. \
  43. static inline T __TBB_machine_fetchadd##S(volatile void *ptr, T addend) \
  44. { \
  45. T result; \
  46. __asm__ __volatile__("lock\nxadd" X " %0,%1" \
  47. : R (result), "=m"(*(__TBB_VOLATILE T*)ptr) \
  48. : "0"(addend), "m"(*(__TBB_VOLATILE T*)ptr) \
  49. : "memory"); \
  50. return result; \
  51. } \
  52. \
  53. static inline T __TBB_machine_fetchstore##S(volatile void *ptr, T value) \
  54. { \
  55. T result; \
  56. __asm__ __volatile__("lock\nxchg" X " %0,%1" \
  57. : R (result), "=m"(*(__TBB_VOLATILE T*)ptr) \
  58. : "0"(value), "m"(*(__TBB_VOLATILE T*)ptr) \
  59. : "memory"); \
  60. return result; \
  61. } \
  62. __TBB_MACHINE_DEFINE_ATOMICS(1,int8_t,"","=q")
  63. __TBB_MACHINE_DEFINE_ATOMICS(2,int16_t,"","=r")
  64. __TBB_MACHINE_DEFINE_ATOMICS(4,int32_t,"l","=r")
  65. #if __INTEL_COMPILER
  66. #pragma warning( push )
  67. // reference to EBX in a function requiring stack alignment
  68. #pragma warning( disable: 998 )
  69. #endif
  70. #if __TBB_GCC_CAS8_BUILTIN_INLINING_BROKEN
  71. #define __TBB_IA32_CAS8_NOINLINE __attribute__ ((noinline))
  72. #else
  73. #define __TBB_IA32_CAS8_NOINLINE
  74. #endif
  75. static inline __TBB_IA32_CAS8_NOINLINE int64_t __TBB_machine_cmpswp8 (volatile void *ptr, int64_t value, int64_t comparand ) {
  76. //TODO: remove the extra part of condition once __TBB_GCC_BUILTIN_ATOMICS_PRESENT is lowered to gcc version 4.1.2
  77. #if (__TBB_GCC_BUILTIN_ATOMICS_PRESENT || (__TBB_GCC_VERSION >= 40102)) && !__TBB_GCC_64BIT_ATOMIC_BUILTINS_BROKEN
  78. return __sync_val_compare_and_swap( reinterpret_cast<volatile int64_t*>(ptr), comparand, value );
  79. #else /* !__TBB_GCC_BUILTIN_ATOMICS_PRESENT */
  80. //TODO: look like ICC 13.0 has some issues with this code, investigate it more deeply
  81. int64_t result;
  82. union {
  83. int64_t i64;
  84. int32_t i32[2];
  85. };
  86. i64 = value;
  87. #if __PIC__
  88. /* compiling position-independent code */
  89. // EBX register preserved for compliance with position-independent code rules on IA32
  90. int32_t tmp;
  91. __asm__ __volatile__ (
  92. "movl %%ebx,%2\n\t"
  93. "movl %5,%%ebx\n\t"
  94. #if __GNUC__==3
  95. "lock\n\t cmpxchg8b %1\n\t"
  96. #else
  97. "lock\n\t cmpxchg8b (%3)\n\t"
  98. #endif
  99. "movl %2,%%ebx"
  100. : "=A"(result)
  101. , "=m"(*(__TBB_VOLATILE int64_t *)ptr)
  102. , "=m"(tmp)
  103. #if __GNUC__==3
  104. : "m"(*(__TBB_VOLATILE int64_t *)ptr)
  105. #else
  106. : "SD"(ptr)
  107. #endif
  108. , "0"(comparand)
  109. , "m"(i32[0]), "c"(i32[1])
  110. : "memory"
  111. #if __INTEL_COMPILER
  112. ,"ebx"
  113. #endif
  114. );
  115. #else /* !__PIC__ */
  116. __asm__ __volatile__ (
  117. "lock\n\t cmpxchg8b %1\n\t"
  118. : "=A"(result), "=m"(*(__TBB_VOLATILE int64_t *)ptr)
  119. : "m"(*(__TBB_VOLATILE int64_t *)ptr)
  120. , "0"(comparand)
  121. , "b"(i32[0]), "c"(i32[1])
  122. : "memory"
  123. );
  124. #endif /* __PIC__ */
  125. return result;
  126. #endif /* !__TBB_GCC_BUILTIN_ATOMICS_PRESENT */
  127. }
  128. #undef __TBB_IA32_CAS8_NOINLINE
  129. #if __INTEL_COMPILER
  130. #pragma warning( pop )
  131. #endif // warning 998 is back
  132. static inline void __TBB_machine_or( volatile void *ptr, uint32_t addend ) {
  133. __asm__ __volatile__("lock\norl %1,%0" : "=m"(*(__TBB_VOLATILE uint32_t *)ptr) : "r"(addend), "m"(*(__TBB_VOLATILE uint32_t *)ptr) : "memory");
  134. }
  135. static inline void __TBB_machine_and( volatile void *ptr, uint32_t addend ) {
  136. __asm__ __volatile__("lock\nandl %1,%0" : "=m"(*(__TBB_VOLATILE uint32_t *)ptr) : "r"(addend), "m"(*(__TBB_VOLATILE uint32_t *)ptr) : "memory");
  137. }
  138. //TODO: Check if it possible and profitable for IA-32 architecture on (Linux* and Windows*)
  139. //to use of 64-bit load/store via floating point registers together with full fence
  140. //for sequentially consistent load/store, instead of CAS.
  141. #if __clang__
  142. #define __TBB_fildq "fildll"
  143. #define __TBB_fistpq "fistpll"
  144. #else
  145. #define __TBB_fildq "fildq"
  146. #define __TBB_fistpq "fistpq"
  147. #endif
  148. static inline int64_t __TBB_machine_aligned_load8 (const volatile void *ptr) {
  149. __TBB_ASSERT(tbb::internal::is_aligned(ptr,8),"__TBB_machine_aligned_load8 should be used with 8 byte aligned locations only \n");
  150. int64_t result;
  151. __asm__ __volatile__ ( __TBB_fildq " %1\n\t"
  152. __TBB_fistpq " %0" : "=m"(result) : "m"(*(const __TBB_VOLATILE uint64_t*)ptr) : "memory" );
  153. return result;
  154. }
  155. static inline void __TBB_machine_aligned_store8 (volatile void *ptr, int64_t value ) {
  156. __TBB_ASSERT(tbb::internal::is_aligned(ptr,8),"__TBB_machine_aligned_store8 should be used with 8 byte aligned locations only \n");
  157. // Aligned store
  158. __asm__ __volatile__ ( __TBB_fildq " %1\n\t"
  159. __TBB_fistpq " %0" : "=m"(*(__TBB_VOLATILE int64_t*)ptr) : "m"(value) : "memory" );
  160. }
  161. static inline int64_t __TBB_machine_load8 (const volatile void *ptr) {
  162. #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
  163. if( tbb::internal::is_aligned(ptr,8)) {
  164. #endif
  165. return __TBB_machine_aligned_load8(ptr);
  166. #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
  167. } else {
  168. // Unaligned load
  169. return __TBB_machine_cmpswp8(const_cast<void*>(ptr),0,0);
  170. }
  171. #endif
  172. }
  173. //! Handles misaligned 8-byte store
  174. /** Defined in tbb_misc.cpp */
  175. extern "C" void __TBB_machine_store8_slow( volatile void *ptr, int64_t value );
  176. extern "C" void __TBB_machine_store8_slow_perf_warning( volatile void *ptr );
  177. static inline void __TBB_machine_store8(volatile void *ptr, int64_t value) {
  178. #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
  179. if( tbb::internal::is_aligned(ptr,8)) {
  180. #endif
  181. __TBB_machine_aligned_store8(ptr,value);
  182. #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
  183. } else {
  184. // Unaligned store
  185. #if TBB_USE_PERFORMANCE_WARNINGS
  186. __TBB_machine_store8_slow_perf_warning(ptr);
  187. #endif /* TBB_USE_PERFORMANCE_WARNINGS */
  188. __TBB_machine_store8_slow(ptr,value);
  189. }
  190. #endif
  191. }
  192. // Machine specific atomic operations
  193. #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
  194. #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
  195. #define __TBB_USE_GENERIC_DWORD_FETCH_ADD 1
  196. #define __TBB_USE_GENERIC_DWORD_FETCH_STORE 1
  197. #define __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE 1
  198. #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
  199. #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
  200. #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1