BRE12
icc_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_icc_generic_H)
22 #error Do not #include this internal file directly; use public TBB headers instead.
23 #endif
24 
25 #if ! __TBB_ICC_BUILTIN_ATOMICS_PRESENT
26  #error "Intel C++ Compiler of at least 12.0 version is needed to use ICC intrinsics port"
27 #endif
28 
29 #define __TBB_machine_icc_generic_H
30 
31 //ICC mimics the "native" target compiler
32 #if _MSC_VER
33  #include "msvc_ia32_common.h"
34 #else
35  #include "gcc_ia32_common.h"
36 #endif
37 
38 //TODO: Make __TBB_WORDSIZE macro optional for ICC intrinsics port.
39 //As compiler intrinsics are used for all the operations it is possible to do.
40 
41 #if __TBB_x86_32
42  #define __TBB_WORDSIZE 4
43 #else
44  #define __TBB_WORDSIZE 8
45 #endif
46 #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
47 
48 //__TBB_compiler_fence() defined just in case, as it seems not to be used on its own anywhere else
49 #ifndef __TBB_compiler_fence
50 #if _MSC_VER
51  //TODO: any way to use same intrinsics on windows and linux?
52  #pragma intrinsic(_ReadWriteBarrier)
53  #define __TBB_compiler_fence() _ReadWriteBarrier()
54 #else
55  #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
56 #endif
57 #endif
58 
59 #ifndef __TBB_full_memory_fence
60 #if _MSC_VER
61  //TODO: any way to use same intrinsics on windows and linux?
62  #pragma intrinsic(_mm_mfence)
63  #define __TBB_full_memory_fence() _mm_mfence()
64 #else
65  #define __TBB_full_memory_fence() __asm__ __volatile__("mfence": : :"memory")
66 #endif
67 #endif
68 
69 #ifndef __TBB_control_consistency_helper
70 #define __TBB_control_consistency_helper() __TBB_compiler_fence()
71 #endif
72 
73 namespace tbb { namespace internal {
74 //TODO: is there any way to reuse definition of memory_order enum from ICC instead of copy paste.
75 //however it seems unlikely that ICC will silently change exact enum values, as they are defined
76 //in the ISO exactly like this.
77 //TODO: add test that exact values of the enum are same as in the ISO C++11
78 typedef enum memory_order {
79  memory_order_relaxed, memory_order_consume, memory_order_acquire,
80  memory_order_release, memory_order_acq_rel, memory_order_seq_cst
81 } memory_order;
82 
83 namespace icc_intrinsics_port {
84  template <typename T>
85  T convert_argument(T value){
86  return value;
87  }
88  //The overload below is needed to have explicit conversion of pointer to void* in argument list.
89  //compiler bug?
90  //TODO: add according broken macro and recheck with ICC 13.0 if the overload is still needed
91  template <typename T>
92  void* convert_argument(T* value){
93  return (void*)value;
94  }
95 }
96 //TODO: code below is a bit repetitive, consider simplifying it
97 template <typename T, size_t S>
99  static T load_with_acquire ( const volatile T& location ) {
100  return __atomic_load_explicit(&location, memory_order_acquire);
101  }
102  static void store_with_release ( volatile T &location, T value ) {
103  __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_release);
104  }
105 };
106 
107 template <typename T, size_t S>
109  static inline T load ( const T& location ) {
110  return __atomic_load_explicit(&location, memory_order_relaxed);
111  }
112  static inline void store ( T& location, T value ) {
113  __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_relaxed);
114  }
115 };
116 
117 template <typename T, size_t S>
119  static T load ( const volatile T& location ) {
120  return __atomic_load_explicit(&location, memory_order_seq_cst);
121  }
122 
123  static void store ( volatile T &location, T value ) {
124  __atomic_store_explicit(&location, value, memory_order_seq_cst);
125  }
126 };
127 
128 }} // namespace tbb::internal
129 
130 namespace tbb{ namespace internal { namespace icc_intrinsics_port{
131  typedef enum memory_order_map {
132  relaxed = memory_order_relaxed,
133  acquire = memory_order_acquire,
134  release = memory_order_release,
135  full_fence= memory_order_seq_cst
136  } memory_order_map;
137 }}}// namespace tbb::internal
138 
139 #define __TBB_MACHINE_DEFINE_ATOMICS(S,T,M) \
140 inline T __TBB_machine_cmpswp##S##M( volatile void *ptr, T value, T comparand ) { \
141  __atomic_compare_exchange_strong_explicit( \
142  (T*)ptr \
143  ,&comparand \
144  ,value \
145  , tbb::internal::icc_intrinsics_port::M \
146  , tbb::internal::icc_intrinsics_port::M); \
147  return comparand; \
148 } \
149  \
150 inline T __TBB_machine_fetchstore##S##M(volatile void *ptr, T value) { \
151  return __atomic_exchange_explicit((T*)ptr, value, tbb::internal::icc_intrinsics_port::M); \
152 } \
153  \
154 inline T __TBB_machine_fetchadd##S##M(volatile void *ptr, T value) { \
155  return __atomic_fetch_add_explicit((T*)ptr, value, tbb::internal::icc_intrinsics_port::M); \
156 } \
157 
158 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, full_fence)
159 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, acquire)
160 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, release)
161 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, relaxed)
162 
163 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, full_fence)
164 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, acquire)
165 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, release)
166 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, relaxed)
167 
168 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, full_fence)
169 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, acquire)
170 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, release)
171 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, relaxed)
172 
173 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, full_fence)
174 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, acquire)
175 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, release)
176 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, relaxed)
177 
178 
179 #undef __TBB_MACHINE_DEFINE_ATOMICS
180 
181 #define __TBB_USE_FENCED_ATOMICS 1
182 
183 namespace tbb { namespace internal {
184 #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
185 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(full_fence)
186 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(full_fence)
187 
188 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(acquire)
189 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(release)
190 
191 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(relaxed)
192 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(relaxed)
193 
194 template <typename T>
195 struct machine_load_store<T,8> {
196  static T load_with_acquire ( const volatile T& location ) {
197  if( tbb::internal::is_aligned(&location,8)) {
198  return __atomic_load_explicit(&location, memory_order_acquire);
199  } else {
200  return __TBB_machine_generic_load8acquire(&location);
201  }
202  }
203  static void store_with_release ( volatile T &location, T value ) {
204  if( tbb::internal::is_aligned(&location,8)) {
205  __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_release);
206  } else {
207  return __TBB_machine_generic_store8release(&location,value);
208  }
209  }
210 };
211 
212 template <typename T>
213 struct machine_load_store_relaxed<T,8> {
214  static T load( const volatile T& location ) {
215  if( tbb::internal::is_aligned(&location,8)) {
216  return __atomic_load_explicit(&location, memory_order_relaxed);
217  } else {
218  return __TBB_machine_generic_load8relaxed(&location);
219  }
220  }
221  static void store( volatile T &location, T value ) {
222  if( tbb::internal::is_aligned(&location,8)) {
223  __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_relaxed);
224  } else {
225  return __TBB_machine_generic_store8relaxed(&location,value);
226  }
227  }
228 };
229 
230 template <typename T >
231 struct machine_load_store_seq_cst<T,8> {
232  static T load ( const volatile T& location ) {
233  if( tbb::internal::is_aligned(&location,8)) {
234  return __atomic_load_explicit(&location, memory_order_seq_cst);
235  } else {
236  return __TBB_machine_generic_load8full_fence(&location);
237  }
238 
239  }
240 
241  static void store ( volatile T &location, T value ) {
242  if( tbb::internal::is_aligned(&location,8)) {
243  __atomic_store_explicit(&location, value, memory_order_seq_cst);
244  } else {
245  return __TBB_machine_generic_store8full_fence(&location,value);
246  }
247 
248  }
249 };
250 
251 #endif
252 }} // namespace tbb::internal
253 template <typename T>
254 inline void __TBB_machine_OR( T *operand, T addend ) {
255  __atomic_fetch_or_explicit(operand, addend, tbb::internal::memory_order_seq_cst);
256 }
257 
258 template <typename T>
259 inline void __TBB_machine_AND( T *operand, T addend ) {
260  __atomic_fetch_and_explicit(operand, addend, tbb::internal::memory_order_seq_cst);
261 }
262 
Definition: icc_generic.h:118
Acquire.
Definition: atomic.h:47
Definition: icc_generic.h:98
No ordering.
Definition: atomic.h:51
Sequential consistency.
Definition: atomic.h:45
Definition: _flow_graph_async_msg_impl.h:32
Release.
Definition: atomic.h:49
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44