2 Copyright 2005-2014 Intel Corporation. All Rights Reserved.
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
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.
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.
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"
29 #define __TBB_machine_icc_generic_H
31 //ICC mimics the "native" target compiler
33 #include "msvc_ia32_common.h"
35 #include "gcc_ia32_common.h"
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.
42 #define __TBB_WORDSIZE 4
44 #define __TBB_WORDSIZE 8
46 #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
48 //__TBB_compiler_fence() defined just in case, as it seems not to be used on its own anywhere else
50 //TODO: any way to use same intrinsics on windows and linux?
51 #pragma intrinsic(_ReadWriteBarrier)
52 #define __TBB_compiler_fence() _ReadWriteBarrier()
54 #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
57 #ifndef __TBB_full_memory_fence
59 //TODO: any way to use same intrinsics on windows and linux?
60 #pragma intrinsic(_mm_mfence)
61 #define __TBB_full_memory_fence() _mm_mfence()
63 #define __TBB_full_memory_fence() __asm__ __volatile__("mfence": : :"memory")
67 #define __TBB_control_consistency_helper() __TBB_compiler_fence()
69 namespace tbb { namespace internal {
70 //TODO: is there any way to reuse definition of memory_order enum from ICC instead of copy paste.
71 //however it seems unlikely that ICC will silently change exact enum values, as they are defined
72 //in the ISO exactly like this.
73 //TODO: add test that exact values of the enum are same as in the ISO C++11
74 typedef enum memory_order {
75 memory_order_relaxed, memory_order_consume, memory_order_acquire,
76 memory_order_release, memory_order_acq_rel, memory_order_seq_cst
79 namespace icc_intrinsics_port {
81 T convert_argument(T value){
84 //The overload below is needed to have explicit conversion of pointer to void* in argument list.
86 //TODO: add according broken macro and recheck with ICC 13.0 if the overload is still needed
88 void* convert_argument(T* value){
92 //TODO: code below is a bit repetitive, consider simplifying it
93 template <typename T, size_t S>
94 struct machine_load_store {
95 static T load_with_acquire ( const volatile T& location ) {
96 return __atomic_load_explicit(&location, memory_order_acquire);
98 static void store_with_release ( volatile T &location, T value ) {
99 __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_release);
103 template <typename T, size_t S>
104 struct machine_load_store_relaxed {
105 static inline T load ( const T& location ) {
106 return __atomic_load_explicit(&location, memory_order_relaxed);
108 static inline void store ( T& location, T value ) {
109 __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_relaxed);
113 template <typename T, size_t S>
114 struct machine_load_store_seq_cst {
115 static T load ( const volatile T& location ) {
116 return __atomic_load_explicit(&location, memory_order_seq_cst);
119 static void store ( volatile T &location, T value ) {
120 __atomic_store_explicit(&location, value, memory_order_seq_cst);
124 }} // namespace tbb::internal
126 namespace tbb{ namespace internal { namespace icc_intrinsics_port{
127 typedef enum memory_order_map {
128 relaxed = memory_order_relaxed,
129 acquire = memory_order_acquire,
130 release = memory_order_release,
131 full_fence= memory_order_seq_cst
133 }}}// namespace tbb::internal
135 #define __TBB_MACHINE_DEFINE_ATOMICS(S,T,M) \
136 inline T __TBB_machine_cmpswp##S##M( volatile void *ptr, T value, T comparand ) { \
137 __atomic_compare_exchange_strong_explicit( \
141 , tbb::internal::icc_intrinsics_port::M \
142 , tbb::internal::icc_intrinsics_port::M); \
146 inline T __TBB_machine_fetchstore##S##M(volatile void *ptr, T value) { \
147 return __atomic_exchange_explicit((T*)ptr, value, tbb::internal::icc_intrinsics_port::M); \
150 inline T __TBB_machine_fetchadd##S##M(volatile void *ptr, T value) { \
151 return __atomic_fetch_add_explicit((T*)ptr, value, tbb::internal::icc_intrinsics_port::M); \
154 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, full_fence)
155 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, acquire)
156 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, release)
157 __TBB_MACHINE_DEFINE_ATOMICS(1,tbb::internal::int8_t, relaxed)
159 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, full_fence)
160 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, acquire)
161 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, release)
162 __TBB_MACHINE_DEFINE_ATOMICS(2,tbb::internal::int16_t, relaxed)
164 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, full_fence)
165 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, acquire)
166 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, release)
167 __TBB_MACHINE_DEFINE_ATOMICS(4,tbb::internal::int32_t, relaxed)
169 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, full_fence)
170 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, acquire)
171 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, release)
172 __TBB_MACHINE_DEFINE_ATOMICS(8,tbb::internal::int64_t, relaxed)
175 #undef __TBB_MACHINE_DEFINE_ATOMICS
177 #define __TBB_USE_FENCED_ATOMICS 1
179 namespace tbb { namespace internal {
180 #if __TBB_FORCE_64BIT_ALIGNMENT_BROKEN
181 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(full_fence)
182 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(full_fence)
184 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(acquire)
185 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(release)
187 __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(relaxed)
188 __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(relaxed)
190 template <typename T>
191 struct machine_load_store<T,8> {
192 static T load_with_acquire ( const volatile T& location ) {
193 if( tbb::internal::is_aligned(&location,8)) {
194 return __atomic_load_explicit(&location, memory_order_acquire);
196 return __TBB_machine_generic_load8acquire(&location);
199 static void store_with_release ( volatile T &location, T value ) {
200 if( tbb::internal::is_aligned(&location,8)) {
201 __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_release);
203 return __TBB_machine_generic_store8release(&location,value);
208 template <typename T>
209 struct machine_load_store_relaxed<T,8> {
210 static T load( const volatile T& location ) {
211 if( tbb::internal::is_aligned(&location,8)) {
212 return __atomic_load_explicit(&location, memory_order_relaxed);
214 return __TBB_machine_generic_load8relaxed(&location);
217 static void store( volatile T &location, T value ) {
218 if( tbb::internal::is_aligned(&location,8)) {
219 __atomic_store_explicit(&location, icc_intrinsics_port::convert_argument(value), memory_order_relaxed);
221 return __TBB_machine_generic_store8relaxed(&location,value);
226 template <typename T >
227 struct machine_load_store_seq_cst<T,8> {
228 static T load ( const volatile T& location ) {
229 if( tbb::internal::is_aligned(&location,8)) {
230 return __atomic_load_explicit(&location, memory_order_seq_cst);
232 return __TBB_machine_generic_load8full_fence(&location);
237 static void store ( volatile T &location, T value ) {
238 if( tbb::internal::is_aligned(&location,8)) {
239 __atomic_store_explicit(&location, value, memory_order_seq_cst);
241 return __TBB_machine_generic_store8full_fence(&location,value);
248 }} // namespace tbb::internal
249 template <typename T>
250 inline void __TBB_machine_OR( T *operand, T addend ) {
251 __atomic_fetch_or_explicit(operand, addend, tbb::internal::memory_order_seq_cst);
254 template <typename T>
255 inline void __TBB_machine_AND( T *operand, T addend ) {
256 __atomic_fetch_and_explicit(operand, addend, tbb::internal::memory_order_seq_cst);