]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/machine/linux_ia32.h
(no commit message)
[casparcg] / tbb30_20100406oss / include / tbb / machine / linux_ia32.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
31 #endif
32
33 #if !__MINGW32__
34 #include "linux_common.h"
35 #endif
36
37 #define __TBB_WORDSIZE 4
38 #define __TBB_BIG_ENDIAN 0
39
40 #define __TBB_release_consistency_helper() __asm__ __volatile__("": : :"memory")
41
42 inline void __TBB_rel_acq_fence() { __asm__ __volatile__("mfence": : :"memory"); }
43
44 #if __TBB_ICC_ASM_VOLATILE_BROKEN
45 #define __TBB_VOLATILE
46 #else
47 #define __TBB_VOLATILE volatile
48 #endif
49
50 #define __MACHINE_DECL_ATOMICS(S,T,X) \
51 static inline T __TBB_machine_cmpswp##S (volatile void *ptr, T value, T comparand )  \
52 {                                                                                    \
53     T result;                                                                        \
54                                                                                      \
55     __asm__ __volatile__("lock\ncmpxchg" X " %2,%1"                                  \
56                           : "=a"(result), "=m"(*(__TBB_VOLATILE T*)ptr)              \
57                           : "q"(value), "0"(comparand), "m"(*(__TBB_VOLATILE T*)ptr) \
58                           : "memory");                                               \
59     return result;                                                                   \
60 }                                                                                    \
61                                                                                      \
62 static inline T __TBB_machine_fetchadd##S(volatile void *ptr, T addend)              \
63 {                                                                                    \
64     T result;                                                                        \
65     __asm__ __volatile__("lock\nxadd" X " %0,%1"                                     \
66                           : "=r"(result), "=m"(*(__TBB_VOLATILE T*)ptr)              \
67                           : "0"(addend), "m"(*(__TBB_VOLATILE T*)ptr)                \
68                           : "memory");                                               \
69     return result;                                                                   \
70 }                                                                                    \
71                                                                                      \
72 static inline  T __TBB_machine_fetchstore##S(volatile void *ptr, T value)            \
73 {                                                                                    \
74     T result;                                                                        \
75     __asm__ __volatile__("lock\nxchg" X " %0,%1"                                     \
76                           : "=r"(result), "=m"(*(__TBB_VOLATILE T*)ptr)              \
77                           : "0"(value), "m"(*(__TBB_VOLATILE T*)ptr)                 \
78                           : "memory");                                               \
79     return result;                                                                   \
80 }                                                                                    \
81                                                                                      
82 __MACHINE_DECL_ATOMICS(1,int8_t,"")
83 __MACHINE_DECL_ATOMICS(2,int16_t,"")
84 __MACHINE_DECL_ATOMICS(4,int32_t,"l")
85
86 static inline int64_t __TBB_machine_cmpswp8 (volatile void *ptr, int64_t value, int64_t comparand )
87 {
88     int64_t result;
89 #if __PIC__ 
90     /* compiling position-independent code */
91     // EBX register preserved for compliance with position-independent code rules on IA32
92     __asm__ __volatile__ (
93             "pushl %%ebx\n\t"
94             "movl  (%%ecx),%%ebx\n\t"
95             "movl  4(%%ecx),%%ecx\n\t"
96             "lock\n\t cmpxchg8b %1\n\t"
97             "popl  %%ebx"
98              : "=A"(result), "=m"(*(int64_t *)ptr)
99              : "m"(*(int64_t *)ptr)
100              , "0"(comparand)
101              , "c"(&value)
102              : "memory", "esp"
103 #if __INTEL_COMPILER
104              ,"ebx"
105 #endif
106     );
107 #else /* !__PIC__ */
108     union {
109         int64_t i64;
110         int32_t i32[2];
111     };
112     i64 = value;
113     __asm__ __volatile__ (
114             "lock\n\t cmpxchg8b %1\n\t"
115              : "=A"(result), "=m"(*(__TBB_VOLATILE int64_t *)ptr)
116              : "m"(*(__TBB_VOLATILE int64_t *)ptr)
117              , "0"(comparand)
118              , "b"(i32[0]), "c"(i32[1])
119              : "memory"
120     );
121 #endif /* __PIC__ */
122     return result;
123 }
124
125 static inline int32_t __TBB_machine_lg( uint32_t x ) {
126     int32_t j;
127     __asm__ ("bsr %1,%0" : "=r"(j) : "r"(x));
128     return j;
129 }
130
131 static inline void __TBB_machine_or( volatile void *ptr, uint32_t addend ) {
132     __asm__ __volatile__("lock\norl %1,%0" : "=m"(*(__TBB_VOLATILE uint32_t *)ptr) : "r"(addend), "m"(*(__TBB_VOLATILE uint32_t *)ptr) : "memory");
133 }
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
139 static inline void __TBB_machine_pause( int32_t delay ) {
140     for (int32_t i = 0; i < delay; i++) {
141        __asm__ __volatile__("pause;");
142     }
143     return;
144 }   
145
146 static inline int64_t __TBB_machine_load8 (const volatile void *ptr) {
147     int64_t result;
148     if( ((uint32_t)ptr&7u)==0 ) {
149         // Aligned load
150         __asm__ __volatile__ ( "fildq %1\n\t"
151                                "fistpq %0" :  "=m"(result) : "m"(*(const __TBB_VOLATILE uint64_t*)ptr) : "memory" );
152     } else {
153         // Unaligned load
154         result = __TBB_machine_cmpswp8(const_cast<void*>(ptr),0,0);
155     }
156     return result;
157 }
158
159 //! Handles misaligned 8-byte store
160 /** Defined in tbb_misc.cpp */
161 extern "C" void __TBB_machine_store8_slow( volatile void *ptr, int64_t value );
162 extern "C" void __TBB_machine_store8_slow_perf_warning( volatile void *ptr );
163
164 static inline void __TBB_machine_store8(volatile void *ptr, int64_t value) {
165     if( ((uint32_t)ptr&7u)==0 ) {
166         // Aligned store
167         __asm__ __volatile__ ( "fildq %1\n\t"
168                                "fistpq %0" :  "=m"(*(__TBB_VOLATILE int64_t*)ptr) : "m"(value) : "memory" );
169     } else {
170         // Unaligned store
171 #if TBB_USE_PERFORMANCE_WARNINGS
172         __TBB_machine_store8_slow_perf_warning(ptr);
173 #endif /* TBB_USE_PERFORMANCE_WARNINGS */
174         __TBB_machine_store8_slow(ptr,value);
175     }
176 }
177  
178 template <typename T, size_t S>
179 struct __TBB_machine_load_store {
180     static inline T load_with_acquire(const volatile T& location) {
181         T to_return = location;
182         __asm__ __volatile__("" : : : "memory" );   // Compiler fence to keep operations from migrating upwards
183         return to_return;
184     }
185
186     static inline void store_with_release(volatile T &location, T value) {
187         __asm__ __volatile__("" : : : "memory" );   // Compiler fence to keep operations from migrating upwards
188         location = value;
189     }
190 };
191
192 template <typename T>
193 struct __TBB_machine_load_store<T,8> {
194     static inline T load_with_acquire(const volatile T& location) {
195         T to_return = __TBB_machine_load8((const volatile void *)&location);
196         __asm__ __volatile__("" : : : "memory" );   // Compiler fence to keep operations from migrating upwards
197         return to_return;
198     }
199
200     static inline void store_with_release(volatile T &location, T value) {
201         __asm__ __volatile__("" : : : "memory" );   // Compiler fence to keep operations from migrating downwards
202         __TBB_machine_store8((volatile void *)&location,(int64_t)value);
203     }
204 };
205
206 #undef __TBB_VOLATILE
207
208 template<typename T>
209 inline T __TBB_machine_load_with_acquire(const volatile T &location) {
210     return __TBB_machine_load_store<T,sizeof(T)>::load_with_acquire(location);
211 }
212
213 template<typename T, typename V>
214 inline void __TBB_machine_store_with_release(volatile T &location, V value) {
215     __TBB_machine_load_store<T,sizeof(T)>::store_with_release(location,value);
216 }
217
218 #define __TBB_load_with_acquire(L) __TBB_machine_load_with_acquire((L))
219 #define __TBB_store_with_release(L,V) __TBB_machine_store_with_release((L),(V))
220
221 // Machine specific atomic operations
222
223 #define __TBB_CompareAndSwap1(P,V,C) __TBB_machine_cmpswp1(P,V,C)
224 #define __TBB_CompareAndSwap2(P,V,C) __TBB_machine_cmpswp2(P,V,C)
225 #define __TBB_CompareAndSwap4(P,V,C) __TBB_machine_cmpswp4(P,V,C)
226 #define __TBB_CompareAndSwap8(P,V,C) __TBB_machine_cmpswp8(P,V,C)
227 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp4(P,V,C)
228
229 #define __TBB_FetchAndAdd1(P,V) __TBB_machine_fetchadd1(P,V)
230 #define __TBB_FetchAndAdd2(P,V) __TBB_machine_fetchadd2(P,V)
231 #define __TBB_FetchAndAdd4(P,V) __TBB_machine_fetchadd4(P,V)
232 #define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd4(P,V)
233
234 #define __TBB_FetchAndStore1(P,V) __TBB_machine_fetchstore1(P,V)
235 #define __TBB_FetchAndStore2(P,V) __TBB_machine_fetchstore2(P,V)
236 #define __TBB_FetchAndStore4(P,V) __TBB_machine_fetchstore4(P,V)
237 #define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore4(P,V)
238
239 #define __TBB_Store8(P,V) __TBB_machine_store8(P,V)
240 #define __TBB_Load8(P)    __TBB_machine_load8(P)
241
242 #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
243 #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
244
245
246 // Those we chose not to implement (they will be implemented generically using CMPSWP8)
247 #undef __TBB_FetchAndAdd8
248 #undef __TBB_FetchAndStore8
249
250 // Definition of other functions
251 #define __TBB_Pause(V) __TBB_machine_pause(V)
252 #define __TBB_Log2(V)  __TBB_machine_lg(V)
253
254 // Special atomic functions
255 #define __TBB_FetchAndAddWrelease(P,V) __TBB_FetchAndAddW(P,V)
256 #define __TBB_FetchAndIncrementWacquire(P) __TBB_FetchAndAddW(P,1)
257 #define __TBB_FetchAndDecrementWrelease(P) __TBB_FetchAndAddW(P,-1)
258
259 // Use generic definitions from tbb_machine.h
260 #undef __TBB_TryLockByte
261 #undef __TBB_LockByte