sunos_sparc.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #if !defined(__TBB_machine_H) || defined(__TBB_machine_sunos_sparc_H)
  14. #error Do not #include this internal file directly; use public TBB headers instead.
  15. #endif
  16. #define __TBB_machine_sunos_sparc_H
  17. #include <stdint.h>
  18. #include <unistd.h>
  19. #define __TBB_WORDSIZE 8
  20. // Big endian is assumed for SPARC.
  21. // While hardware may support page-specific bi-endianness, only big endian pages may be exposed to TBB
  22. #define __TBB_ENDIANNESS __TBB_ENDIAN_BIG
  23. /** To those working on SPARC hardware. Consider relaxing acquire and release
  24. consistency helpers to no-op (as this port covers TSO mode only). **/
  25. #define __TBB_compiler_fence() __asm__ __volatile__ ("": : :"memory")
  26. #define __TBB_control_consistency_helper() __TBB_compiler_fence()
  27. #define __TBB_acquire_consistency_helper() __TBB_compiler_fence()
  28. #define __TBB_release_consistency_helper() __TBB_compiler_fence()
  29. #define __TBB_full_memory_fence() __asm__ __volatile__("membar #LoadLoad|#LoadStore|#StoreStore|#StoreLoad": : : "memory")
  30. //--------------------------------------------------
  31. // Compare and swap
  32. //--------------------------------------------------
  33. /**
  34. * Atomic CAS for 32 bit values, if *ptr==comparand, then *ptr=value, returns *ptr
  35. * @param ptr pointer to value in memory to be swapped with value if *ptr==comparand
  36. * @param value value to assign *ptr to if *ptr==comparand
  37. * @param comparand value to compare with *ptr
  38. ( @return value originally in memory at ptr, regardless of success
  39. */
  40. static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand ){
  41. int32_t result;
  42. __asm__ __volatile__(
  43. "cas\t[%5],%4,%1"
  44. : "=m"(*(int32_t *)ptr), "=r"(result)
  45. : "m"(*(int32_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
  46. : "memory");
  47. return result;
  48. }
  49. /**
  50. * Atomic CAS for 64 bit values, if *ptr==comparand, then *ptr=value, returns *ptr
  51. * @param ptr pointer to value in memory to be swapped with value if *ptr==comparand
  52. * @param value value to assign *ptr to if *ptr==comparand
  53. * @param comparand value to compare with *ptr
  54. ( @return value originally in memory at ptr, regardless of success
  55. */
  56. static inline int64_t __TBB_machine_cmpswp8(volatile void *ptr, int64_t value, int64_t comparand ){
  57. int64_t result;
  58. __asm__ __volatile__(
  59. "casx\t[%5],%4,%1"
  60. : "=m"(*(int64_t *)ptr), "=r"(result)
  61. : "m"(*(int64_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
  62. : "memory");
  63. return result;
  64. }
  65. //---------------------------------------------------
  66. // Fetch and add
  67. //---------------------------------------------------
  68. /**
  69. * Atomic fetch and add for 32 bit values, in this case implemented by continuously checking success of atomicity
  70. * @param ptr pointer to value to add addend to
  71. * @param addened value to add to *ptr
  72. * @return value at ptr before addened was added
  73. */
  74. static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend){
  75. int32_t result;
  76. __asm__ __volatile__ (
  77. "0:\t add\t %3, %4, %0\n" // do addition
  78. "\t cas\t [%2], %3, %0\n" // cas to store result in memory
  79. "\t cmp\t %3, %0\n" // check if value from memory is original
  80. "\t bne,a,pn\t %%icc, 0b\n" // if not try again
  81. "\t mov %0, %3\n" // use branch delay slot to move new value in memory to be added
  82. : "=&r"(result), "=m"(*(int32_t *)ptr)
  83. : "r"(ptr), "r"(*(int32_t *)ptr), "r"(addend), "m"(*(int32_t *)ptr)
  84. : "ccr", "memory");
  85. return result;
  86. }
  87. /**
  88. * Atomic fetch and add for 64 bit values, in this case implemented by continuously checking success of atomicity
  89. * @param ptr pointer to value to add addend to
  90. * @param addened value to add to *ptr
  91. * @return value at ptr before addened was added
  92. */
  93. static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend){
  94. int64_t result;
  95. __asm__ __volatile__ (
  96. "0:\t add\t %3, %4, %0\n" // do addition
  97. "\t casx\t [%2], %3, %0\n" // cas to store result in memory
  98. "\t cmp\t %3, %0\n" // check if value from memory is original
  99. "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
  100. "\t mov %0, %3\n" // use branch delay slot to move new value in memory to be added
  101. : "=&r"(result), "=m"(*(int64_t *)ptr)
  102. : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
  103. : "ccr", "memory");
  104. return result;
  105. }
  106. //--------------------------------------------------------
  107. // Logarithm (base two, integer)
  108. //--------------------------------------------------------
  109. static inline int64_t __TBB_machine_lg( uint64_t x ) {
  110. __TBB_ASSERT(x, "__TBB_Log2(0) undefined");
  111. uint64_t count;
  112. // one hot encode
  113. x |= (x >> 1);
  114. x |= (x >> 2);
  115. x |= (x >> 4);
  116. x |= (x >> 8);
  117. x |= (x >> 16);
  118. x |= (x >> 32);
  119. // count 1's
  120. __asm__ ("popc %1, %0" : "=r"(count) : "r"(x) );
  121. return count-1;
  122. }
  123. //--------------------------------------------------------
  124. static inline void __TBB_machine_or( volatile void *ptr, uint64_t value ) {
  125. __asm__ __volatile__ (
  126. "0:\t or\t %2, %3, %%g1\n" // do operation
  127. "\t casx\t [%1], %2, %%g1\n" // cas to store result in memory
  128. "\t cmp\t %2, %%g1\n" // check if value from memory is original
  129. "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
  130. "\t mov %%g1, %2\n" // use branch delay slot to move new value in memory to be added
  131. : "=m"(*(int64_t *)ptr)
  132. : "r"(ptr), "r"(*(int64_t *)ptr), "r"(value), "m"(*(int64_t *)ptr)
  133. : "ccr", "g1", "memory");
  134. }
  135. static inline void __TBB_machine_and( volatile void *ptr, uint64_t value ) {
  136. __asm__ __volatile__ (
  137. "0:\t and\t %2, %3, %%g1\n" // do operation
  138. "\t casx\t [%1], %2, %%g1\n" // cas to store result in memory
  139. "\t cmp\t %2, %%g1\n" // check if value from memory is original
  140. "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
  141. "\t mov %%g1, %2\n" // use branch delay slot to move new value in memory to be added
  142. : "=m"(*(int64_t *)ptr)
  143. : "r"(ptr), "r"(*(int64_t *)ptr), "r"(value), "m"(*(int64_t *)ptr)
  144. : "ccr", "g1", "memory");
  145. }
  146. static inline void __TBB_machine_pause( int32_t delay ) {
  147. // do nothing, inlined, doesn't matter
  148. }
  149. // put 0xff in memory location, return memory value,
  150. // generic trylockbyte puts 0x01, however this is fine
  151. // because all that matters is that 0 is unlocked
  152. static inline bool __TBB_machine_trylockbyte(unsigned char &flag){
  153. unsigned char result;
  154. __asm__ __volatile__ (
  155. "ldstub\t [%2], %0\n"
  156. : "=r"(result), "=m"(flag)
  157. : "r"(&flag), "m"(flag)
  158. : "memory");
  159. return result == 0;
  160. }
  161. #define __TBB_USE_GENERIC_PART_WORD_CAS 1
  162. #define __TBB_USE_GENERIC_PART_WORD_FETCH_ADD 1
  163. #define __TBB_USE_GENERIC_FETCH_STORE 1
  164. #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
  165. #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
  166. #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1
  167. #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
  168. #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
  169. // Definition of other functions
  170. #define __TBB_Pause(V) __TBB_machine_pause(V)
  171. #define __TBB_Log2(V) __TBB_machine_lg(V)
  172. #define __TBB_TryLockByte(P) __TBB_machine_trylockbyte(P)