bufferiszero.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Simple C functions to supplement the C library
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/cutils.h"
  26. #include "qemu/bswap.h"
  27. static bool
  28. buffer_zero_int(const void *buf, size_t len)
  29. {
  30. if (unlikely(len < 8)) {
  31. /* For a very small buffer, simply accumulate all the bytes. */
  32. const unsigned char *p = buf;
  33. const unsigned char *e = buf + len;
  34. unsigned char t = 0;
  35. do {
  36. t |= *p++;
  37. } while (p < e);
  38. return t == 0;
  39. } else {
  40. /* Otherwise, use the unaligned memory access functions to
  41. handle the beginning and end of the buffer, with a couple
  42. of loops handling the middle aligned section. */
  43. uint64_t t = ldq_he_p(buf);
  44. const uint64_t *p = (uint64_t *)(((uintptr_t)buf + 8) & -8);
  45. const uint64_t *e = (uint64_t *)(((uintptr_t)buf + len) & -8);
  46. for (; p + 8 <= e; p += 8) {
  47. __builtin_prefetch(p + 8);
  48. if (t) {
  49. return false;
  50. }
  51. t = p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7];
  52. }
  53. while (p < e) {
  54. t |= *p++;
  55. }
  56. t |= ldq_he_p(buf + len - 8);
  57. return t == 0;
  58. }
  59. }
  60. #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT) || defined(__SSE2__)
  61. #include <immintrin.h>
  62. /* Note that each of these vectorized functions require len >= 64. */
  63. static bool __attribute__((target("sse2")))
  64. buffer_zero_sse2(const void *buf, size_t len)
  65. {
  66. __m128i t = _mm_loadu_si128(buf);
  67. __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16);
  68. __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16);
  69. __m128i zero = _mm_setzero_si128();
  70. /* Loop over 16-byte aligned blocks of 64. */
  71. while (likely(p <= e)) {
  72. __builtin_prefetch(p);
  73. t = _mm_cmpeq_epi8(t, zero);
  74. if (unlikely(_mm_movemask_epi8(t) != 0xFFFF)) {
  75. return false;
  76. }
  77. t = p[-4] | p[-3] | p[-2] | p[-1];
  78. p += 4;
  79. }
  80. /* Finish the aligned tail. */
  81. t |= e[-3];
  82. t |= e[-2];
  83. t |= e[-1];
  84. /* Finish the unaligned tail. */
  85. t |= _mm_loadu_si128(buf + len - 16);
  86. return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF;
  87. }
  88. #ifdef CONFIG_AVX2_OPT
  89. static bool __attribute__((target("sse4")))
  90. buffer_zero_sse4(const void *buf, size_t len)
  91. {
  92. __m128i t = _mm_loadu_si128(buf);
  93. __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16);
  94. __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16);
  95. /* Loop over 16-byte aligned blocks of 64. */
  96. while (likely(p <= e)) {
  97. __builtin_prefetch(p);
  98. if (unlikely(!_mm_testz_si128(t, t))) {
  99. return false;
  100. }
  101. t = p[-4] | p[-3] | p[-2] | p[-1];
  102. p += 4;
  103. }
  104. /* Finish the aligned tail. */
  105. t |= e[-3];
  106. t |= e[-2];
  107. t |= e[-1];
  108. /* Finish the unaligned tail. */
  109. t |= _mm_loadu_si128(buf + len - 16);
  110. return _mm_testz_si128(t, t);
  111. }
  112. static bool __attribute__((target("avx2")))
  113. buffer_zero_avx2(const void *buf, size_t len)
  114. {
  115. /* Begin with an unaligned head of 32 bytes. */
  116. __m256i t = _mm256_loadu_si256(buf);
  117. __m256i *p = (__m256i *)(((uintptr_t)buf + 5 * 32) & -32);
  118. __m256i *e = (__m256i *)(((uintptr_t)buf + len) & -32);
  119. /* Loop over 32-byte aligned blocks of 128. */
  120. while (p <= e) {
  121. __builtin_prefetch(p);
  122. if (unlikely(!_mm256_testz_si256(t, t))) {
  123. return false;
  124. }
  125. t = p[-4] | p[-3] | p[-2] | p[-1];
  126. p += 4;
  127. } ;
  128. /* Finish the last block of 128 unaligned. */
  129. t |= _mm256_loadu_si256(buf + len - 4 * 32);
  130. t |= _mm256_loadu_si256(buf + len - 3 * 32);
  131. t |= _mm256_loadu_si256(buf + len - 2 * 32);
  132. t |= _mm256_loadu_si256(buf + len - 1 * 32);
  133. return _mm256_testz_si256(t, t);
  134. }
  135. #endif /* CONFIG_AVX2_OPT */
  136. #ifdef CONFIG_AVX512F_OPT
  137. static bool __attribute__((target("avx512f")))
  138. buffer_zero_avx512(const void *buf, size_t len)
  139. {
  140. /* Begin with an unaligned head of 64 bytes. */
  141. __m512i t = _mm512_loadu_si512(buf);
  142. __m512i *p = (__m512i *)(((uintptr_t)buf + 5 * 64) & -64);
  143. __m512i *e = (__m512i *)(((uintptr_t)buf + len) & -64);
  144. /* Loop over 64-byte aligned blocks of 256. */
  145. while (p <= e) {
  146. __builtin_prefetch(p);
  147. if (unlikely(_mm512_test_epi64_mask(t, t))) {
  148. return false;
  149. }
  150. t = p[-4] | p[-3] | p[-2] | p[-1];
  151. p += 4;
  152. }
  153. t |= _mm512_loadu_si512(buf + len - 4 * 64);
  154. t |= _mm512_loadu_si512(buf + len - 3 * 64);
  155. t |= _mm512_loadu_si512(buf + len - 2 * 64);
  156. t |= _mm512_loadu_si512(buf + len - 1 * 64);
  157. return !_mm512_test_epi64_mask(t, t);
  158. }
  159. #endif /* CONFIG_AVX512F_OPT */
  160. /* Note that for test_buffer_is_zero_next_accel, the most preferred
  161. * ISA must have the least significant bit.
  162. */
  163. #define CACHE_AVX512F 1
  164. #define CACHE_AVX2 2
  165. #define CACHE_SSE4 4
  166. #define CACHE_SSE2 8
  167. /* Make sure that these variables are appropriately initialized when
  168. * SSE2 is enabled on the compiler command-line, but the compiler is
  169. * too old to support CONFIG_AVX2_OPT.
  170. */
  171. #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT)
  172. # define INIT_CACHE 0
  173. # define INIT_ACCEL buffer_zero_int
  174. #else
  175. # ifndef __SSE2__
  176. # error "ISA selection confusion"
  177. # endif
  178. # define INIT_CACHE CACHE_SSE2
  179. # define INIT_ACCEL buffer_zero_sse2
  180. #endif
  181. static unsigned cpuid_cache = INIT_CACHE;
  182. static bool (*buffer_accel)(const void *, size_t) = INIT_ACCEL;
  183. static int length_to_accel = 64;
  184. static void init_accel(unsigned cache)
  185. {
  186. bool (*fn)(const void *, size_t) = buffer_zero_int;
  187. if (cache & CACHE_SSE2) {
  188. fn = buffer_zero_sse2;
  189. length_to_accel = 64;
  190. }
  191. #ifdef CONFIG_AVX2_OPT
  192. if (cache & CACHE_SSE4) {
  193. fn = buffer_zero_sse4;
  194. length_to_accel = 64;
  195. }
  196. if (cache & CACHE_AVX2) {
  197. fn = buffer_zero_avx2;
  198. length_to_accel = 128;
  199. }
  200. #endif
  201. #ifdef CONFIG_AVX512F_OPT
  202. if (cache & CACHE_AVX512F) {
  203. fn = buffer_zero_avx512;
  204. length_to_accel = 256;
  205. }
  206. #endif
  207. buffer_accel = fn;
  208. }
  209. #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT)
  210. #include "qemu/cpuid.h"
  211. static void __attribute__((constructor)) init_cpuid_cache(void)
  212. {
  213. unsigned max = __get_cpuid_max(0, NULL);
  214. int a, b, c, d;
  215. unsigned cache = 0;
  216. if (max >= 1) {
  217. __cpuid(1, a, b, c, d);
  218. if (d & bit_SSE2) {
  219. cache |= CACHE_SSE2;
  220. }
  221. if (c & bit_SSE4_1) {
  222. cache |= CACHE_SSE4;
  223. }
  224. /* We must check that AVX is not just available, but usable. */
  225. if ((c & bit_OSXSAVE) && (c & bit_AVX) && max >= 7) {
  226. unsigned bv = xgetbv_low(0);
  227. __cpuid_count(7, 0, a, b, c, d);
  228. if ((bv & 0x6) == 0x6 && (b & bit_AVX2)) {
  229. cache |= CACHE_AVX2;
  230. }
  231. /* 0xe6:
  232. * XCR0[7:5] = 111b (OPMASK state, upper 256-bit of ZMM0-ZMM15
  233. * and ZMM16-ZMM31 state are enabled by OS)
  234. * XCR0[2:1] = 11b (XMM state and YMM state are enabled by OS)
  235. */
  236. if ((bv & 0xe6) == 0xe6 && (b & bit_AVX512F)) {
  237. cache |= CACHE_AVX512F;
  238. }
  239. }
  240. }
  241. cpuid_cache = cache;
  242. init_accel(cache);
  243. }
  244. #endif /* CONFIG_AVX2_OPT */
  245. bool test_buffer_is_zero_next_accel(void)
  246. {
  247. /* If no bits set, we just tested buffer_zero_int, and there
  248. are no more acceleration options to test. */
  249. if (cpuid_cache == 0) {
  250. return false;
  251. }
  252. /* Disable the accelerator we used before and select a new one. */
  253. cpuid_cache &= cpuid_cache - 1;
  254. init_accel(cpuid_cache);
  255. return true;
  256. }
  257. static bool select_accel_fn(const void *buf, size_t len)
  258. {
  259. if (likely(len >= length_to_accel)) {
  260. return buffer_accel(buf, len);
  261. }
  262. return buffer_zero_int(buf, len);
  263. }
  264. #else
  265. #define select_accel_fn buffer_zero_int
  266. bool test_buffer_is_zero_next_accel(void)
  267. {
  268. return false;
  269. }
  270. #endif
  271. /*
  272. * Checks if a buffer is all zeroes
  273. */
  274. bool buffer_is_zero(const void *buf, size_t len)
  275. {
  276. if (unlikely(len == 0)) {
  277. return true;
  278. }
  279. /* Fetch the beginning of the buffer while we select the accelerator. */
  280. __builtin_prefetch(buf);
  281. /* Use an optimized zero check if possible. Note that this also
  282. includes a check for an unrolled loop over 64-bit integers. */
  283. return select_accel_fn(buf, len);
  284. }