BRE12
gcc_generic.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #if !defined(__TBB_machine_H) || defined(__TBB_machine_gcc_generic_H)
22 #error Do not #include this internal file directly; use public TBB headers instead.
23 #endif
24 
25 #define __TBB_machine_gcc_generic_H
26 
27 #include <stdint.h>
28 #include <unistd.h>
29 
30 #define __TBB_WORDSIZE __SIZEOF_POINTER__
31 
32 #if __TBB_GCC_64BIT_ATOMIC_BUILTINS_BROKEN
33  #define __TBB_64BIT_ATOMICS 0
34 #endif
35 
37 #if __ANDROID__ && __TBB_generic_arch
38  #define __TBB_CPU_CTL_ENV_PRESENT 0
39 #endif
40 
41 // __BYTE_ORDER__ is used in accordance with http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html,
42 // but __BIG_ENDIAN__ or __LITTLE_ENDIAN__ may be more commonly found instead.
43 #if __BIG_ENDIAN__ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__)
44  #define __TBB_ENDIANNESS __TBB_ENDIAN_BIG
45 #elif __LITTLE_ENDIAN__ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)
46  #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
47 #elif defined(__BYTE_ORDER__)
48  #define __TBB_ENDIANNESS __TBB_ENDIAN_UNSUPPORTED
49 #else
50  #define __TBB_ENDIANNESS __TBB_ENDIAN_DETECT
51 #endif
52 
53 #if __TBB_GCC_VERSION < 40700
54 // Use __sync_* builtins
55 
61 #define __TBB_acquire_consistency_helper() __sync_synchronize()
62 #define __TBB_release_consistency_helper() __sync_synchronize()
63 #define __TBB_full_memory_fence() __sync_synchronize()
64 #define __TBB_control_consistency_helper() __sync_synchronize()
65 
66 #define __TBB_MACHINE_DEFINE_ATOMICS(S,T) \
67 inline T __TBB_machine_cmpswp##S( volatile void *ptr, T value, T comparand ) { \
68  return __sync_val_compare_and_swap(reinterpret_cast<volatile T *>(ptr),comparand,value); \
69 } \
70 inline T __TBB_machine_fetchadd##S( volatile void *ptr, T value ) { \
71  return __sync_fetch_and_add(reinterpret_cast<volatile T *>(ptr),value); \
72 }
73 
74 #define __TBB_USE_GENERIC_FETCH_STORE 1
75 
76 #else
77 // __TBB_GCC_VERSION >= 40700; use __atomic_* builtins available since gcc 4.7
78 
79 #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
80 // Acquire and release fence intrinsics in GCC might miss compiler fence.
81 // Adding it at both sides of an intrinsic, as we do not know what reordering can be made.
82 #define __TBB_acquire_consistency_helper() __TBB_compiler_fence(); __atomic_thread_fence(__ATOMIC_ACQUIRE); __TBB_compiler_fence()
83 #define __TBB_release_consistency_helper() __TBB_compiler_fence(); __atomic_thread_fence(__ATOMIC_RELEASE); __TBB_compiler_fence()
84 #define __TBB_full_memory_fence() __atomic_thread_fence(__ATOMIC_SEQ_CST)
85 #define __TBB_control_consistency_helper() __TBB_acquire_consistency_helper()
86 
87 #define __TBB_MACHINE_DEFINE_ATOMICS(S,T) \
88 inline T __TBB_machine_cmpswp##S( volatile void *ptr, T value, T comparand ) { \
89  (void)__atomic_compare_exchange_n(reinterpret_cast<volatile T *>(ptr), &comparand, value, \
90  false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
91  return comparand; \
92 } \
93 inline T __TBB_machine_fetchadd##S( volatile void *ptr, T value ) { \
94  return __atomic_fetch_add(reinterpret_cast<volatile T *>(ptr), value, __ATOMIC_SEQ_CST); \
95 } \
96 inline T __TBB_machine_fetchstore##S( volatile void *ptr, T value ) { \
97  return __atomic_exchange_n(reinterpret_cast<volatile T *>(ptr), value, __ATOMIC_SEQ_CST); \
98 }
99 
100 #endif // __TBB_GCC_VERSION < 40700
101 
102 __TBB_MACHINE_DEFINE_ATOMICS(1,int8_t)
103 __TBB_MACHINE_DEFINE_ATOMICS(2,int16_t)
104 __TBB_MACHINE_DEFINE_ATOMICS(4,int32_t)
105 __TBB_MACHINE_DEFINE_ATOMICS(8,int64_t)
106 
107 #undef __TBB_MACHINE_DEFINE_ATOMICS
108 
109 namespace tbb{ namespace internal { namespace gcc_builtins {
110  inline int clz(unsigned int x){ return __builtin_clz(x);};
111  inline int clz(unsigned long int x){ return __builtin_clzl(x);};
112  inline int clz(unsigned long long int x){ return __builtin_clzll(x);};
113 }}}
114 //gcc __builtin_clz builtin count _number_ of leading zeroes
115 static inline intptr_t __TBB_machine_lg( uintptr_t x ) {
116  return sizeof(x)*8 - tbb::internal::gcc_builtins::clz(x) -1 ;
117 }
118 
119 
120 typedef unsigned char __TBB_Flag;
121 typedef __TBB_atomic __TBB_Flag __TBB_atomic_flag;
122 
123 #if __TBB_GCC_VERSION < 40700
124 // Use __sync_* builtins
125 
126 static inline void __TBB_machine_or( volatile void *ptr, uintptr_t addend ) {
127  __sync_fetch_and_or(reinterpret_cast<volatile uintptr_t *>(ptr),addend);
128 }
129 
130 static inline void __TBB_machine_and( volatile void *ptr, uintptr_t addend ) {
131  __sync_fetch_and_and(reinterpret_cast<volatile uintptr_t *>(ptr),addend);
132 }
133 
134 inline bool __TBB_machine_try_lock_byte( __TBB_atomic_flag &flag ) {
135  return __sync_lock_test_and_set(&flag,1)==0;
136 }
137 
138 inline void __TBB_machine_unlock_byte( __TBB_atomic_flag &flag ) {
139  __sync_lock_release(&flag);
140 }
141 
142 #else
143 // __TBB_GCC_VERSION >= 40700; use __atomic_* builtins available since gcc 4.7
144 
145 static inline void __TBB_machine_or( volatile void *ptr, uintptr_t addend ) {
146  __atomic_fetch_or(reinterpret_cast<volatile uintptr_t *>(ptr),addend,__ATOMIC_SEQ_CST);
147 }
148 
149 static inline void __TBB_machine_and( volatile void *ptr, uintptr_t addend ) {
150  __atomic_fetch_and(reinterpret_cast<volatile uintptr_t *>(ptr),addend,__ATOMIC_SEQ_CST);
151 }
152 
153 inline bool __TBB_machine_try_lock_byte( __TBB_atomic_flag &flag ) {
154  return !__atomic_test_and_set(&flag,__ATOMIC_ACQUIRE);
155 }
156 
157 inline void __TBB_machine_unlock_byte( __TBB_atomic_flag &flag ) {
158  __atomic_clear(&flag,__ATOMIC_RELEASE);
159 }
160 
161 #endif // __TBB_GCC_VERSION < 40700
162 
163 // Machine specific atomic operations
164 #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
165 #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
166 
167 #define __TBB_TryLockByte __TBB_machine_try_lock_byte
168 #define __TBB_UnlockByte __TBB_machine_unlock_byte
169 
170 // Definition of other functions
171 #define __TBB_Log2(V) __TBB_machine_lg(V)
172 
173 // TODO: implement with __atomic_* builtins where available
174 #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
175 #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
176 #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1
177 
178 #if __TBB_WORDSIZE==4
179  #define __TBB_USE_GENERIC_DWORD_LOAD_STORE 1
180 #endif
181 
182 #if __TBB_x86_32 || __TBB_x86_64
183 #include "gcc_itsx.h"
184 #endif
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44