ia32intrin.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* Copyright (C) 2009-2019 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #ifndef _X86INTRIN_H_INCLUDED
  19. # error "Never use <ia32intrin.h> directly; include <x86intrin.h> instead."
  20. #endif
  21. /* 32bit bsf */
  22. extern __inline int
  23. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  24. __bsfd (int __X)
  25. {
  26. return __builtin_ctz (__X);
  27. }
  28. /* 32bit bsr */
  29. extern __inline int
  30. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  31. __bsrd (int __X)
  32. {
  33. return __builtin_ia32_bsrsi (__X);
  34. }
  35. /* 32bit bswap */
  36. extern __inline int
  37. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  38. __bswapd (int __X)
  39. {
  40. return __builtin_bswap32 (__X);
  41. }
  42. #ifndef __iamcu__
  43. #ifndef __SSE4_2__
  44. #pragma GCC push_options
  45. #pragma GCC target("sse4.2")
  46. #define __DISABLE_SSE4_2__
  47. #endif /* __SSE4_2__ */
  48. /* 32bit accumulate CRC32 (polynomial 0x11EDC6F41) value. */
  49. extern __inline unsigned int
  50. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  51. __crc32b (unsigned int __C, unsigned char __V)
  52. {
  53. return __builtin_ia32_crc32qi (__C, __V);
  54. }
  55. extern __inline unsigned int
  56. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  57. __crc32w (unsigned int __C, unsigned short __V)
  58. {
  59. return __builtin_ia32_crc32hi (__C, __V);
  60. }
  61. extern __inline unsigned int
  62. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  63. __crc32d (unsigned int __C, unsigned int __V)
  64. {
  65. return __builtin_ia32_crc32si (__C, __V);
  66. }
  67. #ifdef __DISABLE_SSE4_2__
  68. #undef __DISABLE_SSE4_2__
  69. #pragma GCC pop_options
  70. #endif /* __DISABLE_SSE4_2__ */
  71. #endif /* __iamcu__ */
  72. /* 32bit popcnt */
  73. extern __inline int
  74. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  75. __popcntd (unsigned int __X)
  76. {
  77. return __builtin_popcount (__X);
  78. }
  79. #ifndef __iamcu__
  80. /* rdpmc */
  81. extern __inline unsigned long long
  82. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  83. __rdpmc (int __S)
  84. {
  85. return __builtin_ia32_rdpmc (__S);
  86. }
  87. #endif /* __iamcu__ */
  88. /* rdtsc */
  89. extern __inline unsigned long long
  90. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  91. __rdtsc (void)
  92. {
  93. return __builtin_ia32_rdtsc ();
  94. }
  95. #ifndef __iamcu__
  96. /* rdtscp */
  97. extern __inline unsigned long long
  98. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  99. __rdtscp (unsigned int *__A)
  100. {
  101. return __builtin_ia32_rdtscp (__A);
  102. }
  103. #endif /* __iamcu__ */
  104. /* 8bit rol */
  105. extern __inline unsigned char
  106. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  107. __rolb (unsigned char __X, int __C)
  108. {
  109. return __builtin_ia32_rolqi (__X, __C);
  110. }
  111. /* 16bit rol */
  112. extern __inline unsigned short
  113. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  114. __rolw (unsigned short __X, int __C)
  115. {
  116. return __builtin_ia32_rolhi (__X, __C);
  117. }
  118. /* 32bit rol */
  119. extern __inline unsigned int
  120. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  121. __rold (unsigned int __X, int __C)
  122. {
  123. __C &= 31;
  124. return (__X << __C) | (__X >> (-__C & 31));
  125. }
  126. /* 8bit ror */
  127. extern __inline unsigned char
  128. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  129. __rorb (unsigned char __X, int __C)
  130. {
  131. return __builtin_ia32_rorqi (__X, __C);
  132. }
  133. /* 16bit ror */
  134. extern __inline unsigned short
  135. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  136. __rorw (unsigned short __X, int __C)
  137. {
  138. return __builtin_ia32_rorhi (__X, __C);
  139. }
  140. /* 32bit ror */
  141. extern __inline unsigned int
  142. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  143. __rord (unsigned int __X, int __C)
  144. {
  145. __C &= 31;
  146. return (__X >> __C) | (__X << (-__C & 31));
  147. }
  148. /* Pause */
  149. extern __inline void
  150. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  151. __pause (void)
  152. {
  153. __builtin_ia32_pause ();
  154. }
  155. #ifdef __x86_64__
  156. /* 64bit bsf */
  157. extern __inline int
  158. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  159. __bsfq (long long __X)
  160. {
  161. return __builtin_ctzll (__X);
  162. }
  163. /* 64bit bsr */
  164. extern __inline int
  165. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  166. __bsrq (long long __X)
  167. {
  168. return __builtin_ia32_bsrdi (__X);
  169. }
  170. /* 64bit bswap */
  171. extern __inline long long
  172. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  173. __bswapq (long long __X)
  174. {
  175. return __builtin_bswap64 (__X);
  176. }
  177. #ifndef __SSE4_2__
  178. #pragma GCC push_options
  179. #pragma GCC target("sse4.2")
  180. #define __DISABLE_SSE4_2__
  181. #endif /* __SSE4_2__ */
  182. /* 64bit accumulate CRC32 (polynomial 0x11EDC6F41) value. */
  183. extern __inline unsigned long long
  184. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  185. __crc32q (unsigned long long __C, unsigned long long __V)
  186. {
  187. return __builtin_ia32_crc32di (__C, __V);
  188. }
  189. #ifdef __DISABLE_SSE4_2__
  190. #undef __DISABLE_SSE4_2__
  191. #pragma GCC pop_options
  192. #endif /* __DISABLE_SSE4_2__ */
  193. /* 64bit popcnt */
  194. extern __inline long long
  195. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  196. __popcntq (unsigned long long __X)
  197. {
  198. return __builtin_popcountll (__X);
  199. }
  200. /* 64bit rol */
  201. extern __inline unsigned long long
  202. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  203. __rolq (unsigned long long __X, int __C)
  204. {
  205. __C &= 63;
  206. return (__X << __C) | (__X >> (-__C & 63));
  207. }
  208. /* 64bit ror */
  209. extern __inline unsigned long long
  210. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  211. __rorq (unsigned long long __X, int __C)
  212. {
  213. __C &= 63;
  214. return (__X >> __C) | (__X << (-__C & 63));
  215. }
  216. /* Read flags register */
  217. extern __inline unsigned long long
  218. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  219. __readeflags (void)
  220. {
  221. return __builtin_ia32_readeflags_u64 ();
  222. }
  223. /* Write flags register */
  224. extern __inline void
  225. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  226. __writeeflags (unsigned long long __X)
  227. {
  228. __builtin_ia32_writeeflags_u64 (__X);
  229. }
  230. #define _bswap64(a) __bswapq(a)
  231. #define _popcnt64(a) __popcntq(a)
  232. #else
  233. /* Read flags register */
  234. extern __inline unsigned int
  235. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  236. __readeflags (void)
  237. {
  238. return __builtin_ia32_readeflags_u32 ();
  239. }
  240. /* Write flags register */
  241. extern __inline void
  242. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  243. __writeeflags (unsigned int __X)
  244. {
  245. __builtin_ia32_writeeflags_u32 (__X);
  246. }
  247. #endif
  248. /* On LP64 systems, longs are 64-bit. Use the appropriate rotate
  249. * function. */
  250. #ifdef __LP64__
  251. #define _lrotl(a,b) __rolq((a), (b))
  252. #define _lrotr(a,b) __rorq((a), (b))
  253. #else
  254. #define _lrotl(a,b) __rold((a), (b))
  255. #define _lrotr(a,b) __rord((a), (b))
  256. #endif
  257. #define _bit_scan_forward(a) __bsfd(a)
  258. #define _bit_scan_reverse(a) __bsrd(a)
  259. #define _bswap(a) __bswapd(a)
  260. #define _popcnt32(a) __popcntd(a)
  261. #ifndef __iamcu__
  262. #define _rdpmc(a) __rdpmc(a)
  263. #define _rdtscp(a) __rdtscp(a)
  264. #endif /* __iamcu__ */
  265. #define _rdtsc() __rdtsc()
  266. #define _rotwl(a,b) __rolw((a), (b))
  267. #define _rotwr(a,b) __rorw((a), (b))
  268. #define _rotl(a,b) __rold((a), (b))
  269. #define _rotr(a,b) __rord((a), (b))