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