bufferiszero.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_AVX2_OPT) || defined(__SSE2__)
  61. /* Do not use push_options pragmas unnecessarily, because clang
  62. * does not support them.
  63. */
  64. #ifdef CONFIG_AVX2_OPT
  65. #pragma GCC push_options
  66. #pragma GCC target("sse2")
  67. #endif
  68. #include <emmintrin.h>
  69. /* Note that each of these vectorized functions require len >= 64. */
  70. static bool
  71. buffer_zero_sse2(const void *buf, size_t len)
  72. {
  73. __m128i t = _mm_loadu_si128(buf);
  74. __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16);
  75. __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16);
  76. __m128i zero = _mm_setzero_si128();
  77. /* Loop over 16-byte aligned blocks of 64. */
  78. while (likely(p <= e)) {
  79. __builtin_prefetch(p);
  80. t = _mm_cmpeq_epi8(t, zero);
  81. if (unlikely(_mm_movemask_epi8(t) != 0xFFFF)) {
  82. return false;
  83. }
  84. t = p[-4] | p[-3] | p[-2] | p[-1];
  85. p += 4;
  86. }
  87. /* Finish the aligned tail. */
  88. t |= e[-3];
  89. t |= e[-2];
  90. t |= e[-1];
  91. /* Finish the unaligned tail. */
  92. t |= _mm_loadu_si128(buf + len - 16);
  93. return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF;
  94. }
  95. #ifdef CONFIG_AVX2_OPT
  96. #pragma GCC pop_options
  97. #endif
  98. #ifdef CONFIG_AVX2_OPT
  99. /* Note that due to restrictions/bugs wrt __builtin functions in gcc <= 4.8,
  100. * the includes have to be within the corresponding push_options region, and
  101. * therefore the regions themselves have to be ordered with increasing ISA.
  102. */
  103. #pragma GCC push_options
  104. #pragma GCC target("sse4")
  105. #include <smmintrin.h>
  106. static bool
  107. buffer_zero_sse4(const void *buf, size_t len)
  108. {
  109. __m128i t = _mm_loadu_si128(buf);
  110. __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16);
  111. __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16);
  112. /* Loop over 16-byte aligned blocks of 64. */
  113. while (likely(p <= e)) {
  114. __builtin_prefetch(p);
  115. if (unlikely(!_mm_testz_si128(t, t))) {
  116. return false;
  117. }
  118. t = p[-4] | p[-3] | p[-2] | p[-1];
  119. p += 4;
  120. }
  121. /* Finish the aligned tail. */
  122. t |= e[-3];
  123. t |= e[-2];
  124. t |= e[-1];
  125. /* Finish the unaligned tail. */
  126. t |= _mm_loadu_si128(buf + len - 16);
  127. return _mm_testz_si128(t, t);
  128. }
  129. #pragma GCC pop_options
  130. #pragma GCC push_options
  131. #pragma GCC target("avx2")
  132. #include <immintrin.h>
  133. static bool
  134. buffer_zero_avx2(const void *buf, size_t len)
  135. {
  136. /* Begin with an unaligned head of 32 bytes. */
  137. __m256i t = _mm256_loadu_si256(buf);
  138. __m256i *p = (__m256i *)(((uintptr_t)buf + 5 * 32) & -32);
  139. __m256i *e = (__m256i *)(((uintptr_t)buf + len) & -32);
  140. if (likely(p <= e)) {
  141. /* Loop over 32-byte aligned blocks of 128. */
  142. do {
  143. __builtin_prefetch(p);
  144. if (unlikely(!_mm256_testz_si256(t, t))) {
  145. return false;
  146. }
  147. t = p[-4] | p[-3] | p[-2] | p[-1];
  148. p += 4;
  149. } while (p <= e);
  150. } else {
  151. t |= _mm256_loadu_si256(buf + 32);
  152. if (len <= 128) {
  153. goto last2;
  154. }
  155. }
  156. /* Finish the last block of 128 unaligned. */
  157. t |= _mm256_loadu_si256(buf + len - 4 * 32);
  158. t |= _mm256_loadu_si256(buf + len - 3 * 32);
  159. last2:
  160. t |= _mm256_loadu_si256(buf + len - 2 * 32);
  161. t |= _mm256_loadu_si256(buf + len - 1 * 32);
  162. return _mm256_testz_si256(t, t);
  163. }
  164. #pragma GCC pop_options
  165. #endif /* CONFIG_AVX2_OPT */
  166. /* Note that for test_buffer_is_zero_next_accel, the most preferred
  167. * ISA must have the least significant bit.
  168. */
  169. #define CACHE_AVX2 1
  170. #define CACHE_SSE4 2
  171. #define CACHE_SSE2 4
  172. /* Make sure that these variables are appropriately initialized when
  173. * SSE2 is enabled on the compiler command-line, but the compiler is
  174. * too old to support CONFIG_AVX2_OPT.
  175. */
  176. #ifdef CONFIG_AVX2_OPT
  177. # define INIT_CACHE 0
  178. # define INIT_ACCEL buffer_zero_int
  179. #else
  180. # ifndef __SSE2__
  181. # error "ISA selection confusion"
  182. # endif
  183. # define INIT_CACHE CACHE_SSE2
  184. # define INIT_ACCEL buffer_zero_sse2
  185. #endif
  186. static unsigned cpuid_cache = INIT_CACHE;
  187. static bool (*buffer_accel)(const void *, size_t) = INIT_ACCEL;
  188. static void init_accel(unsigned cache)
  189. {
  190. bool (*fn)(const void *, size_t) = buffer_zero_int;
  191. if (cache & CACHE_SSE2) {
  192. fn = buffer_zero_sse2;
  193. }
  194. #ifdef CONFIG_AVX2_OPT
  195. if (cache & CACHE_SSE4) {
  196. fn = buffer_zero_sse4;
  197. }
  198. if (cache & CACHE_AVX2) {
  199. fn = buffer_zero_avx2;
  200. }
  201. #endif
  202. buffer_accel = fn;
  203. }
  204. #ifdef CONFIG_AVX2_OPT
  205. #include "qemu/cpuid.h"
  206. static void __attribute__((constructor)) init_cpuid_cache(void)
  207. {
  208. int max = __get_cpuid_max(0, NULL);
  209. int a, b, c, d;
  210. unsigned cache = 0;
  211. if (max >= 1) {
  212. __cpuid(1, a, b, c, d);
  213. if (d & bit_SSE2) {
  214. cache |= CACHE_SSE2;
  215. }
  216. if (c & bit_SSE4_1) {
  217. cache |= CACHE_SSE4;
  218. }
  219. /* We must check that AVX is not just available, but usable. */
  220. if ((c & bit_OSXSAVE) && (c & bit_AVX) && max >= 7) {
  221. int bv;
  222. __asm("xgetbv" : "=a"(bv), "=d"(d) : "c"(0));
  223. __cpuid_count(7, 0, a, b, c, d);
  224. if ((bv & 6) == 6 && (b & bit_AVX2)) {
  225. cache |= CACHE_AVX2;
  226. }
  227. }
  228. }
  229. cpuid_cache = cache;
  230. init_accel(cache);
  231. }
  232. #endif /* CONFIG_AVX2_OPT */
  233. bool test_buffer_is_zero_next_accel(void)
  234. {
  235. /* If no bits set, we just tested buffer_zero_int, and there
  236. are no more acceleration options to test. */
  237. if (cpuid_cache == 0) {
  238. return false;
  239. }
  240. /* Disable the accelerator we used before and select a new one. */
  241. cpuid_cache &= cpuid_cache - 1;
  242. init_accel(cpuid_cache);
  243. return true;
  244. }
  245. static bool select_accel_fn(const void *buf, size_t len)
  246. {
  247. if (likely(len >= 64)) {
  248. return buffer_accel(buf, len);
  249. }
  250. return buffer_zero_int(buf, len);
  251. }
  252. #else
  253. #define select_accel_fn buffer_zero_int
  254. bool test_buffer_is_zero_next_accel(void)
  255. {
  256. return false;
  257. }
  258. #endif
  259. /*
  260. * Checks if a buffer is all zeroes
  261. */
  262. bool buffer_is_zero(const void *buf, size_t len)
  263. {
  264. if (unlikely(len == 0)) {
  265. return true;
  266. }
  267. /* Fetch the beginning of the buffer while we select the accelerator. */
  268. __builtin_prefetch(buf);
  269. /* Use an optimized zero check if possible. Note that this also
  270. includes a check for an unrolled loop over 64-bit integers. */
  271. return select_accel_fn(buf, len);
  272. }