2 * Simple C functions to supplement the C library
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "qemu/cutils.h"
26 #include "qemu/bswap.h"
27 #include "host/cpuinfo.h"
30 buffer_zero_int(const void *buf
, size_t len
)
32 if (unlikely(len
< 8)) {
33 /* For a very small buffer, simply accumulate all the bytes. */
34 const unsigned char *p
= buf
;
35 const unsigned char *e
= buf
+ len
;
44 /* Otherwise, use the unaligned memory access functions to
45 handle the beginning and end of the buffer, with a couple
46 of loops handling the middle aligned section. */
47 uint64_t t
= ldq_he_p(buf
);
48 const uint64_t *p
= (uint64_t *)(((uintptr_t)buf
+ 8) & -8);
49 const uint64_t *e
= (uint64_t *)(((uintptr_t)buf
+ len
) & -8);
51 for (; p
+ 8 <= e
; p
+= 8) {
52 __builtin_prefetch(p
+ 8);
56 t
= p
[0] | p
[1] | p
[2] | p
[3] | p
[4] | p
[5] | p
[6] | p
[7];
61 t
|= ldq_he_p(buf
+ len
- 8);
67 #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT) || defined(__SSE2__)
68 #include <immintrin.h>
70 /* Note that each of these vectorized functions require len >= 64. */
72 static bool __attribute__((target("sse2")))
73 buffer_zero_sse2(const void *buf
, size_t len
)
75 __m128i t
= _mm_loadu_si128(buf
);
76 __m128i
*p
= (__m128i
*)(((uintptr_t)buf
+ 5 * 16) & -16);
77 __m128i
*e
= (__m128i
*)(((uintptr_t)buf
+ len
) & -16);
78 __m128i zero
= _mm_setzero_si128();
80 /* Loop over 16-byte aligned blocks of 64. */
81 while (likely(p
<= e
)) {
82 __builtin_prefetch(p
);
83 t
= _mm_cmpeq_epi8(t
, zero
);
84 if (unlikely(_mm_movemask_epi8(t
) != 0xFFFF)) {
87 t
= p
[-4] | p
[-3] | p
[-2] | p
[-1];
91 /* Finish the aligned tail. */
96 /* Finish the unaligned tail. */
97 t
|= _mm_loadu_si128(buf
+ len
- 16);
99 return _mm_movemask_epi8(_mm_cmpeq_epi8(t
, zero
)) == 0xFFFF;
102 #ifdef CONFIG_AVX2_OPT
103 static bool __attribute__((target("sse4")))
104 buffer_zero_sse4(const void *buf
, size_t len
)
106 __m128i t
= _mm_loadu_si128(buf
);
107 __m128i
*p
= (__m128i
*)(((uintptr_t)buf
+ 5 * 16) & -16);
108 __m128i
*e
= (__m128i
*)(((uintptr_t)buf
+ len
) & -16);
110 /* Loop over 16-byte aligned blocks of 64. */
111 while (likely(p
<= e
)) {
112 __builtin_prefetch(p
);
113 if (unlikely(!_mm_testz_si128(t
, t
))) {
116 t
= p
[-4] | p
[-3] | p
[-2] | p
[-1];
120 /* Finish the aligned tail. */
125 /* Finish the unaligned tail. */
126 t
|= _mm_loadu_si128(buf
+ len
- 16);
128 return _mm_testz_si128(t
, t
);
131 static bool __attribute__((target("avx2")))
132 buffer_zero_avx2(const void *buf
, size_t len
)
134 /* Begin with an unaligned head of 32 bytes. */
135 __m256i t
= _mm256_loadu_si256(buf
);
136 __m256i
*p
= (__m256i
*)(((uintptr_t)buf
+ 5 * 32) & -32);
137 __m256i
*e
= (__m256i
*)(((uintptr_t)buf
+ len
) & -32);
139 /* Loop over 32-byte aligned blocks of 128. */
141 __builtin_prefetch(p
);
142 if (unlikely(!_mm256_testz_si256(t
, t
))) {
145 t
= p
[-4] | p
[-3] | p
[-2] | p
[-1];
149 /* Finish the last block of 128 unaligned. */
150 t
|= _mm256_loadu_si256(buf
+ len
- 4 * 32);
151 t
|= _mm256_loadu_si256(buf
+ len
- 3 * 32);
152 t
|= _mm256_loadu_si256(buf
+ len
- 2 * 32);
153 t
|= _mm256_loadu_si256(buf
+ len
- 1 * 32);
155 return _mm256_testz_si256(t
, t
);
157 #endif /* CONFIG_AVX2_OPT */
159 #ifdef CONFIG_AVX512F_OPT
160 static bool __attribute__((target("avx512f")))
161 buffer_zero_avx512(const void *buf
, size_t len
)
163 /* Begin with an unaligned head of 64 bytes. */
164 __m512i t
= _mm512_loadu_si512(buf
);
165 __m512i
*p
= (__m512i
*)(((uintptr_t)buf
+ 5 * 64) & -64);
166 __m512i
*e
= (__m512i
*)(((uintptr_t)buf
+ len
) & -64);
168 /* Loop over 64-byte aligned blocks of 256. */
170 __builtin_prefetch(p
);
171 if (unlikely(_mm512_test_epi64_mask(t
, t
))) {
174 t
= p
[-4] | p
[-3] | p
[-2] | p
[-1];
178 t
|= _mm512_loadu_si512(buf
+ len
- 4 * 64);
179 t
|= _mm512_loadu_si512(buf
+ len
- 3 * 64);
180 t
|= _mm512_loadu_si512(buf
+ len
- 2 * 64);
181 t
|= _mm512_loadu_si512(buf
+ len
- 1 * 64);
183 return !_mm512_test_epi64_mask(t
, t
);
186 #endif /* CONFIG_AVX512F_OPT */
189 * Make sure that these variables are appropriately initialized when
190 * SSE2 is enabled on the compiler command-line, but the compiler is
191 * too old to support CONFIG_AVX2_OPT.
193 #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT)
195 # define INIT_LENGTH 0
196 # define INIT_ACCEL buffer_zero_int
199 # error "ISA selection confusion"
201 # define INIT_USED CPUINFO_SSE2
202 # define INIT_LENGTH 64
203 # define INIT_ACCEL buffer_zero_sse2
206 static unsigned used_accel
= INIT_USED
;
207 static unsigned length_to_accel
= INIT_LENGTH
;
208 static bool (*buffer_accel
)(const void *, size_t) = INIT_ACCEL
;
210 static unsigned __attribute__((noinline
))
211 select_accel_cpuinfo(unsigned info
)
213 /* Array is sorted in order of algorithm preference. */
214 static const struct {
217 bool (*fn
)(const void *, size_t);
219 #ifdef CONFIG_AVX512F_OPT
220 { CPUINFO_AVX512F
, 256, buffer_zero_avx512
},
222 #ifdef CONFIG_AVX2_OPT
223 { CPUINFO_AVX2
, 128, buffer_zero_avx2
},
224 { CPUINFO_SSE4
, 64, buffer_zero_sse4
},
226 { CPUINFO_SSE2
, 64, buffer_zero_sse2
},
227 { CPUINFO_ALWAYS
, 0, buffer_zero_int
},
230 for (unsigned i
= 0; i
< ARRAY_SIZE(all
); ++i
) {
231 if (info
& all
[i
].bit
) {
232 length_to_accel
= all
[i
].len
;
233 buffer_accel
= all
[i
].fn
;
240 #if defined(CONFIG_AVX512F_OPT) || defined(CONFIG_AVX2_OPT)
241 static void __attribute__((constructor
)) init_accel(void)
243 used_accel
= select_accel_cpuinfo(cpuinfo_init());
245 #endif /* CONFIG_AVX2_OPT */
247 bool test_buffer_is_zero_next_accel(void)
250 * Accumulate the accelerators that we've already tested, and
251 * remove them from the set to test this round. We'll get back
252 * a zero from select_accel_cpuinfo when there are no more.
254 unsigned used
= select_accel_cpuinfo(cpuinfo
& ~used_accel
);
259 static bool select_accel_fn(const void *buf
, size_t len
)
261 if (likely(len
>= length_to_accel
)) {
262 return buffer_accel(buf
, len
);
264 return buffer_zero_int(buf
, len
);
268 #define select_accel_fn buffer_zero_int
269 bool test_buffer_is_zero_next_accel(void)
276 * Checks if a buffer is all zeroes
278 bool buffer_is_zero(const void *buf
, size_t len
)
280 if (unlikely(len
== 0)) {
284 /* Fetch the beginning of the buffer while we select the accelerator. */
285 __builtin_prefetch(buf
);
287 /* Use an optimized zero check if possible. Note that this also
288 includes a check for an unrolled loop over 64-bit integers. */
289 return select_accel_fn(buf
, len
);