]> git.sesse.net Git - casparcg/blob - tbb/include/tbb/machine/mac_ppc.h
2.0.0.2: Updated tbb version.
[casparcg] / tbb / include / tbb / machine / mac_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 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
31 #endif
32
33 #include <stdint.h>
34 #include <unistd.h>
35
36 // This file is for PowerPC with compilers supporting GNU inline-assembler syntax (currently GNU g++ and IBM XL).
37
38 // Motivation for use of "#if defined(__powerpc64__) || defined(__ppc64__)" to detect a 64-bit environment:
39 // IBM XL documents both __powerpc64__ and __PPC64__, and these also appear to work on g++ (documentation?)
40 // Apple documents __ppc64__ (with __ppc__ only 32-bit, which is not portable even to other environments using g++)
41 inline int32_t __TBB_machine_cmpswp4 (volatile void *ptr, int32_t value, int32_t comparand )
42 {
43     int32_t result;
44
45     __asm__ __volatile__("sync\n"
46                          "0: lwarx %0,0,%2\n\t"  /* load w/ reservation */
47                          "cmpw %0,%4\n\t"        /* compare against comparand */
48                          "bne- 1f\n\t"           /* exit if not same */
49                          "stwcx. %3,0,%2\n\t"    /* store new_value */
50                          "bne- 0b\n"             /* retry if reservation lost */
51                          "1: sync"               /* the exit */
52                           : "=&r"(result), "=m"(* (int32_t*) ptr)
53                           : "r"(ptr), "r"(value), "r"(comparand), "m"(* (int32_t*) ptr)
54                           : "cr0", "memory");
55     return result;
56 }
57
58 #if defined(__powerpc64__) || defined(__ppc64__)
59
60 inline int64_t __TBB_machine_cmpswp8 (volatile void *ptr, int64_t value, int64_t comparand )
61 {
62     int64_t result;
63     __asm__ __volatile__("sync\n"
64                          "0: ldarx %0,0,%2\n\t"  /* load w/ reservation */
65                          "cmpd %0,%4\n\t"        /* compare against comparand */
66                          "bne- 1f\n\t"           /* exit if not same */
67                          "stdcx. %3,0,%2\n\t"    /* store new_value */
68                          "bne- 0b\n"             /* retry if reservation lost */
69                          "1: sync"               /* the exit */
70                           : "=&r"(result), "=m"(* (int64_t*) ptr)
71                           : "r"(ptr), "r"(value), "r"(comparand), "m"(* (int64_t*) ptr)
72                           : "cr0", "memory");
73     return result;
74 }
75 #else
76 // Except for special circumstances, 32-bit builds are meant to run on actual 32-bit hardware
77 // A locked implementation would also be a possibility
78 #define __TBB_64BIT_ATOMICS 0
79 #endif /* 64bit CAS */
80
81 #define __TBB_BIG_ENDIAN 1
82
83 #if defined(__powerpc64__) || defined(__ppc64__)
84 #define __TBB_WORDSIZE 8
85 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp8(P,V,C)
86 #else
87 #define __TBB_WORDSIZE 4
88 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp4(P,V,C)
89 #endif
90
91 #define __TBB_CompareAndSwap4(P,V,C) __TBB_machine_cmpswp4(P,V,C)
92 #if __TBB_64BIT_ATOMICS
93 #define __TBB_CompareAndSwap8(P,V,C) __TBB_machine_cmpswp8(P,V,C)
94 #endif
95 #define __TBB_full_memory_fence() __asm__ __volatile__("sync": : :"memory")
96 #define __TBB_release_consistency_helper() __asm__ __volatile__("lwsync": : :"memory")
97
98 #if !__IBMCPP__
99 // "1501-230 (S) Internal compiler error; please contact your Service Representative"
100 static inline intptr_t __TBB_machine_lg( uintptr_t x ) {
101     // TODO: assumes sizeof(uintptr_t)<=8 resp. 4
102     #if defined(__powerpc64__) || defined(__ppc64__)
103     __asm__ __volatile__ ("cntlzd %0,%0" : "+r"(x)); // counting starts at 2^63
104     return 63-static_cast<intptr_t>(x);
105     #else
106     __asm__ __volatile__ ("cntlzw %0,%0" : "+r"(x)); // counting starts at 2^31 (on 64-bit hardware, higher-order bits are ignored)
107     return 31-static_cast<intptr_t>(x);
108     #endif
109 }
110 #define __TBB_Log2(V) __TBB_machine_lg(V)
111 #endif
112
113 #define __TBB_Byte uint32_t // TODO: would this ever not be aligned without an alignment specification?
114
115 inline bool __TBB_machine_trylockbyte( __TBB_Byte &flag ) {
116     return __TBB_machine_cmpswp4(&flag,1,0)==0;
117 }
118 #define __TBB_TryLockByte(P) __TBB_machine_trylockbyte(P)