Shadowrun: Awakened 29 September 2011 - Build 871
sunos_sparc.h
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 
00030 #ifndef __TBB_machine_H
00031 #error Do not include this file directly; include tbb_machine.h instead
00032 #endif
00033 
00034 #include <stdint.h>
00035 #include <unistd.h>
00036 
00037 #include <sched.h> // sched_yield
00038 
00039 #define __TBB_WORDSIZE 8
00040 #define __TBB_BIG_ENDIAN 1
00041 
00042 #define __TBB_release_consistency_helper() __asm__ __volatile__ ("": : :"memory")
00043 
00044 inline void __TBB_rel_acq_fence() { __asm__ __volatile__("membar #LoadLoad|#LoadStore|#StoreStore|#StoreLoad": : : "memory"); }
00045 
00046 //--------------------------------------------------
00047 // Compare and swap
00048 //--------------------------------------------------
00049 
00057 static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand ){
00058   int32_t result;
00059   __asm__ __volatile__(
00060                        "cas\t[%5],%4,%1"
00061                        : "=m"(*(int32_t *)ptr), "=r"(result)
00062                        : "m"(*(int32_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
00063                        : "memory");
00064   return result;
00065 }
00066 
00074 static inline int64_t __TBB_machine_cmpswp8(volatile void *ptr, int64_t value, int64_t comparand ){
00075   int64_t result;
00076   __asm__ __volatile__(
00077                        "casx\t[%5],%4,%1"
00078                : "=m"(*(int64_t *)ptr), "=r"(result)
00079                : "m"(*(int64_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
00080                : "memory");
00081   return result;
00082 }
00083 
00084 //---------------------------------------------------
00085 // Fetch and add
00086 //---------------------------------------------------
00087 
00094 static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend){
00095   int32_t result;
00096   __asm__ __volatile__ (                                 
00097                         "0:\t add\t %3, %4, %0\n"    // do addition
00098                         "\t cas\t [%2], %3, %0\n"        // cas to store result in memory
00099                         "\t cmp\t %3, %0\n"            // check if value from memory is original
00100                         "\t bne,a,pn\t %%icc, 0b\n"        // if not try again
00101                         "\t mov %0, %3\n"            // use branch delay slot to move new value in memory to be added
00102                : "=&r"(result), "=m"(*(int32_t *)ptr)
00103                : "r"(ptr), "r"(*(int32_t *)ptr), "r"(addend), "m"(*(int32_t *)ptr)
00104                : "ccr", "memory");
00105   return result;
00106 }
00107 
00114 static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend){
00115   int64_t result;
00116   __asm__ __volatile__ (
00117                         "0:\t add\t %3, %4, %0\n"    // do addition
00118                         "\t casx\t [%2], %3, %0\n"        // cas to store result in memory
00119                         "\t cmp\t %3, %0\n"            // check if value from memory is original
00120                         "\t bne,a,pn\t %%xcc, 0b\n"        // if not try again
00121                         "\t mov %0, %3\n"            // use branch delay slot to move new value in memory to be added
00122                 : "=&r"(result), "=m"(*(int64_t *)ptr)
00123                 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
00124                 : "ccr", "memory");
00125   return result;
00126 }
00127 
00128 //--------------------------------------------------------
00129 // Logarithm (base two, integer)
00130 //--------------------------------------------------------
00131 
00132 static inline int64_t __TBB_machine_lg( uint64_t x ) {
00133     uint64_t count;
00134     // one hot encode
00135     x |= (x >> 1);
00136     x |= (x >> 2);
00137     x |= (x >> 4);
00138     x |= (x >> 8);
00139     x |= (x >> 16);
00140     x |= (x >> 32);
00141     // count 1's
00142     __asm__ ("popc %1, %0" : "=r"(count) : "r"(x) );
00143     return count-1;
00144 }
00145 
00146 //--------------------------------------------------------
00147 
00148 static inline void __TBB_machine_or( volatile void *ptr, uint64_t addend ) {
00149   __asm__ __volatile__ (
00150                         "0:\t or\t %2, %3, %%g1\n" // do addition
00151                         "\t casx\t [%1], %2, %%g1\n"            // cas to store result in memory
00152                         "\t cmp\t %2, %%g1\n"                   // check if value from memory is original
00153                         "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
00154                         "\t mov %%g1, %2\n"                     // use branch delay slot to move new value in memory to be added
00155                 : "=m"(*(int64_t *)ptr)
00156                 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
00157                 : "ccr", "g1", "memory");
00158 }
00159 
00160 static inline void __TBB_machine_and( volatile void *ptr, uint64_t addend ) {
00161   __asm__ __volatile__ (
00162                         "0:\t and\t %2, %3, %%g1\n"        // do addition
00163                         "\t casx\t [%1], %2, %%g1\n"            // cas to store result in memory
00164                         "\t cmp\t %2, %%g1\n"                   // check if value from memory is original
00165                         "\t bne,a,pn\t %%xcc, 0b\n"         // if not try again
00166                         "\t mov %%g1, %2\n"                     // use branch delay slot to move new value in memory to be added
00167                 : "=m"(*(int64_t *)ptr)
00168                 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
00169                 : "ccr", "g1", "memory");
00170 }
00171 
00172 
00173 static inline void __TBB_machine_pause( int32_t delay ) {
00174     // do nothing, inlined, doesnt matter
00175 }
00176 
00177 // put 0xff in memory location, return memory value,
00178 //  generic trylockbyte puts 0x01, however this is fine
00179 //  because all that matters is that 0 is unlocked
00180 static inline bool __TBB_machine_trylockbyte(unsigned char &flag){
00181     unsigned char result;
00182     __asm__ __volatile__ (
00183             "ldstub\t [%2], %0\n"
00184         : "=r"(result), "=m"(flag)
00185         : "r"(&flag), "m"(flag)
00186         : "memory");
00187     return result == 0;
00188 }
00189 
00190 
00191 // Machine specific atomic operations
00192 
00193 //#define __TBB_CompareAndSwap1(P,V,C) __TBB_machine_cmpswp1(P,V,C)  // use generic version in tbb_machine.h
00194 //#define __TBB_CompareAndSwap2(P,V,C) __TBB_machine_cmpswp2(P,V,C)  // use generic version in tbb_machine.h
00195 #define __TBB_CompareAndSwap4(P,V,C) __TBB_machine_cmpswp4(P,V,C)
00196 #define __TBB_CompareAndSwap8(P,V,C) __TBB_machine_cmpswp8(P,V,C)
00197 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp8(P,V,C)
00198 
00199 //#define __TBB_FetchAndAdd1(P,V) __TBB_machine_fetchadd1(P,V)       // use generic version in tbb_machine.h
00200 //#define __TBB_FetchAndAdd2(P,V) __TBB_machine_fetchadd2(P,V)       // use generic version in tbb_machine.h
00201 #define __TBB_FetchAndAdd4(P,V) __TBB_machine_fetchadd4(P,V)
00202 #define __TBB_FetchAndAdd8(P,V)  __TBB_machine_fetchadd8(P,V)
00203 #define __TBB_FetchAndAddW(P,V)  __TBB_machine_fetchadd8(P,V)
00204 
00205 // use generic version in tbb_machine.h
00206 //#define __TBB_FetchAndStore1(P,V) __TBB_machine_fetchstore1(P,V)  
00207 //#define __TBB_FetchAndStore2(P,V) __TBB_machine_fetchstore2(P,V)
00208 //#define __TBB_FetchAndStore4(P,V) __TBB_machine_fetchstore4(P,V)
00209 //#define __TBB_FetchAndStore8(P,V)  __TBB_machine_fetchstore8(P,V)
00210 //#define __TBB_FetchAndStoreW(P,V)  __TBB_machine_fetchstore8(P,V)
00211 
00212 #define __TBB_Store8(P,V) (*P = V)
00213 #define __TBB_Load8(P)    (*P)
00214 
00215 #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
00216 #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
00217 
00218 // Definition of other functions
00219 #define __TBB_Pause(V) __TBB_machine_pause(V)
00220 #define __TBB_Log2(V)    __TBB_machine_lg(V)
00221 
00222 // Special atomic functions
00223 #define __TBB_FetchAndAddWrelease(P,V) __TBB_FetchAndAddW(P,V)
00224 #define __TBB_FetchAndIncrementWacquire(P) __TBB_FetchAndAddW(P,1)
00225 #define __TBB_FetchAndDecrementWrelease(P) __TBB_FetchAndAddW(P,-1)
00226 
00227 // Definition of Lock functions
00228 // Repeatedly runs TryLockByte, no need to implement
00229 #undef __TBB_LockByte
00230 
00231 #define __TBB_TryLockByte(P) __TBB_machine_trylockbyte(P)
00232 
00233 #define __TBB_Yield() sched_yield()

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net