]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/machine/xbox360_ppc.h
2.0. Updated tbb library.
[casparcg] / tbb / include / tbb / machine / xbox360_ppc.h
1 /*
2     Copyright 2005-2011 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 // TODO: revise by comparing with mac_ppc.h
30
31 #if !defined(__TBB_machine_H) || defined(__TBB_machine_xbox360_ppc_H)
32 #error Do not include this file directly; include tbb_machine.h instead
33 #endif
34
35 #define __TBB_machine_xbox360_ppc_H
36
37 #define NONET
38 #define NOD3D
39 #include "xtl.h"    
40 #include "ppcintrinsics.h"
41
42 #if _MSC_VER >= 1300
43 extern "C" void _MemoryBarrier();
44 #pragma intrinsic(_MemoryBarrier)
45 #define __TBB_control_consistency_helper() __isync()
46 #define __TBB_acquire_consistency_helper() _MemoryBarrier()
47 #define __TBB_release_consistency_helper() _MemoryBarrier()
48 #endif
49
50 #define __TBB_full_memory_fence() __sync()
51
52 #define __TBB_WORDSIZE 4
53 #define __TBB_BIG_ENDIAN 1
54
55 //todo: define __TBB_USE_FENCED_ATOMICS and define acquire/release primitives to maximize performance
56
57 inline __int32 __TBB_machine_cmpswp4(volatile void *ptr, __int32 value, __int32 comparand ) {                               
58  __sync();
59  __int32 result = InterlockedCompareExchange((volatile LONG*)ptr, value, comparand);
60  __isync();
61  return result;
62 }
63
64 inline __int64 __TBB_machine_cmpswp8(volatile void *ptr, __int64 value, __int64 comparand )
65 {
66  __sync();
67  __int64 result = InterlockedCompareExchange64((volatile LONG64*)ptr, value, comparand);
68  __isync();
69  return result;
70 }
71
72 #define __TBB_USE_GENERIC_PART_WORD_CAS             1
73 #define __TBB_USE_GENERIC_FETCH_ADD                 1
74 #define __TBB_USE_GENERIC_FETCH_STORE               1
75 #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE    1
76 #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE        1
77 #define __TBB_USE_GENERIC_DWORD_LOAD_STORE          1
78
79 #pragma optimize( "", off )
80 inline void __TBB_machine_pause (__int32 delay ) 
81 {
82  for (__int32 i=0; i<delay; i++) {;};
83 }
84 #pragma optimize( "", on ) 
85
86 #define __TBB_Yield()  Sleep(0)
87 #define __TBB_Pause(V) __TBB_machine_pause(V)
88
89 // This port uses only 2 hardware threads for TBB on XBOX 360. 
90 // Others are left to sound etc.
91 // Change the following mask to allow TBB use more HW threads.
92 static const int __TBB_XBOX360_HARDWARE_THREAD_MASK = 0x0C;
93
94 static inline int __TBB_XBOX360_DetectNumberOfWorkers() 
95 {
96      char a[__TBB_XBOX360_HARDWARE_THREAD_MASK];  //compile time assert - at least one bit should be set always
97      a[0]=0;
98
99      return ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 0) & 1) +
100             ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 1) & 1) +
101             ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 2) & 1) +
102             ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 3) & 1) +
103             ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 4) & 1) +
104             ((__TBB_XBOX360_HARDWARE_THREAD_MASK >> 5) & 1) + 1;  // +1 accomodates for the master thread
105 }
106
107 static inline int __TBB_XBOX360_GetHardwareThreadIndex(int workerThreadIndex)
108 {
109     workerThreadIndex %= __TBB_XBOX360_DetectNumberOfWorkers()-1;
110     int m = __TBB_XBOX360_HARDWARE_THREAD_MASK;
111     int index = 0;
112     int skipcount = workerThreadIndex;
113     while (true)
114     {
115         if ((m & 1)!=0) 
116         {
117             if (skipcount==0) break;
118             skipcount--;
119         }
120         m >>= 1;
121        index++;
122     }
123     return index; 
124 }
125
126 #define __TBB_HardwareConcurrency() __TBB_XBOX360_DetectNumberOfWorkers()