xref: /openbmc/qemu/util/bufferiszero.c (revision cbe3d5264631aa193fd2705820cbde6c5a602abb)
188ca8e80SRichard Henderson /*
288ca8e80SRichard Henderson  * Simple C functions to supplement the C library
388ca8e80SRichard Henderson  *
488ca8e80SRichard Henderson  * Copyright (c) 2006 Fabrice Bellard
588ca8e80SRichard Henderson  *
688ca8e80SRichard Henderson  * Permission is hereby granted, free of charge, to any person obtaining a copy
788ca8e80SRichard Henderson  * of this software and associated documentation files (the "Software"), to deal
888ca8e80SRichard Henderson  * in the Software without restriction, including without limitation the rights
988ca8e80SRichard Henderson  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1088ca8e80SRichard Henderson  * copies of the Software, and to permit persons to whom the Software is
1188ca8e80SRichard Henderson  * furnished to do so, subject to the following conditions:
1288ca8e80SRichard Henderson  *
1388ca8e80SRichard Henderson  * The above copyright notice and this permission notice shall be included in
1488ca8e80SRichard Henderson  * all copies or substantial portions of the Software.
1588ca8e80SRichard Henderson  *
1688ca8e80SRichard Henderson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1788ca8e80SRichard Henderson  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1888ca8e80SRichard Henderson  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1988ca8e80SRichard Henderson  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2088ca8e80SRichard Henderson  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2188ca8e80SRichard Henderson  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2288ca8e80SRichard Henderson  * THE SOFTWARE.
2388ca8e80SRichard Henderson  */
2488ca8e80SRichard Henderson #include "qemu/osdep.h"
2588ca8e80SRichard Henderson #include "qemu/cutils.h"
265e33a872SRichard Henderson #include "qemu/bswap.h"
2751f4d916SRichard Henderson #include "host/cpuinfo.h"
2888ca8e80SRichard Henderson 
29*cbe3d526SAlexander Monakov static bool (*buffer_is_zero_accel)(const void *, size_t);
30*cbe3d526SAlexander Monakov 
31*cbe3d526SAlexander Monakov static bool buffer_is_zero_integer(const void *buf, size_t len)
325e33a872SRichard Henderson {
335e33a872SRichard Henderson     if (unlikely(len < 8)) {
345e33a872SRichard Henderson         /* For a very small buffer, simply accumulate all the bytes.  */
355e33a872SRichard Henderson         const unsigned char *p = buf;
365e33a872SRichard Henderson         const unsigned char *e = buf + len;
375e33a872SRichard Henderson         unsigned char t = 0;
385e33a872SRichard Henderson 
395e33a872SRichard Henderson         do {
405e33a872SRichard Henderson             t |= *p++;
415e33a872SRichard Henderson         } while (p < e);
425e33a872SRichard Henderson 
435e33a872SRichard Henderson         return t == 0;
445e33a872SRichard Henderson     } else {
455e33a872SRichard Henderson         /* Otherwise, use the unaligned memory access functions to
465e33a872SRichard Henderson            handle the beginning and end of the buffer, with a couple
475e33a872SRichard Henderson            of loops handling the middle aligned section.  */
485e33a872SRichard Henderson         uint64_t t = ldq_he_p(buf);
495e33a872SRichard Henderson         const uint64_t *p = (uint64_t *)(((uintptr_t)buf + 8) & -8);
505e33a872SRichard Henderson         const uint64_t *e = (uint64_t *)(((uintptr_t)buf + len) & -8);
515e33a872SRichard Henderson 
525e33a872SRichard Henderson         for (; p + 8 <= e; p += 8) {
535e33a872SRichard Henderson             __builtin_prefetch(p + 8);
545e33a872SRichard Henderson             if (t) {
555e33a872SRichard Henderson                 return false;
565e33a872SRichard Henderson             }
575e33a872SRichard Henderson             t = p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7];
585e33a872SRichard Henderson         }
595e33a872SRichard Henderson         while (p < e) {
605e33a872SRichard Henderson             t |= *p++;
615e33a872SRichard Henderson         }
625e33a872SRichard Henderson         t |= ldq_he_p(buf + len - 8);
635e33a872SRichard Henderson 
645e33a872SRichard Henderson         return t == 0;
655e33a872SRichard Henderson     }
665e33a872SRichard Henderson }
675e33a872SRichard Henderson 
68d018425cSAlexander Monakov #if defined(CONFIG_AVX2_OPT) || defined(__SSE2__)
69701ea587SRichard Henderson #include <immintrin.h>
70d9911d14SRichard Henderson 
71d9911d14SRichard Henderson /* Note that each of these vectorized functions require len >= 64.  */
72d9911d14SRichard Henderson 
73701ea587SRichard Henderson static bool __attribute__((target("sse2")))
74d9911d14SRichard Henderson buffer_zero_sse2(const void *buf, size_t len)
75d9911d14SRichard Henderson {
76d9911d14SRichard Henderson     __m128i t = _mm_loadu_si128(buf);
77d9911d14SRichard Henderson     __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16);
78d9911d14SRichard Henderson     __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16);
79d9911d14SRichard Henderson     __m128i zero = _mm_setzero_si128();
80d9911d14SRichard Henderson 
81d9911d14SRichard Henderson     /* Loop over 16-byte aligned blocks of 64.  */
82d9911d14SRichard Henderson     while (likely(p <= e)) {
83d9911d14SRichard Henderson         __builtin_prefetch(p);
84d9911d14SRichard Henderson         t = _mm_cmpeq_epi8(t, zero);
85d9911d14SRichard Henderson         if (unlikely(_mm_movemask_epi8(t) != 0xFFFF)) {
86d9911d14SRichard Henderson             return false;
87d9911d14SRichard Henderson         }
88d9911d14SRichard Henderson         t = p[-4] | p[-3] | p[-2] | p[-1];
89d9911d14SRichard Henderson         p += 4;
90d9911d14SRichard Henderson     }
91d9911d14SRichard Henderson 
92d9911d14SRichard Henderson     /* Finish the aligned tail.  */
93d9911d14SRichard Henderson     t |= e[-3];
94d9911d14SRichard Henderson     t |= e[-2];
95d9911d14SRichard Henderson     t |= e[-1];
96d9911d14SRichard Henderson 
97d9911d14SRichard Henderson     /* Finish the unaligned tail.  */
98d9911d14SRichard Henderson     t |= _mm_loadu_si128(buf + len - 16);
99d9911d14SRichard Henderson 
100d9911d14SRichard Henderson     return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF;
101d9911d14SRichard Henderson }
10288ca8e80SRichard Henderson 
1035e33a872SRichard Henderson #ifdef CONFIG_AVX2_OPT
104701ea587SRichard Henderson static bool __attribute__((target("avx2")))
105d9911d14SRichard Henderson buffer_zero_avx2(const void *buf, size_t len)
106d9911d14SRichard Henderson {
107d9911d14SRichard Henderson     /* Begin with an unaligned head of 32 bytes.  */
108d9911d14SRichard Henderson     __m256i t = _mm256_loadu_si256(buf);
109d9911d14SRichard Henderson     __m256i *p = (__m256i *)(((uintptr_t)buf + 5 * 32) & -32);
110d9911d14SRichard Henderson     __m256i *e = (__m256i *)(((uintptr_t)buf + len) & -32);
111d9911d14SRichard Henderson 
112d9911d14SRichard Henderson     /* Loop over 32-byte aligned blocks of 128.  */
1138f13a39dSRobert Hoo     while (p <= e) {
114d9911d14SRichard Henderson         __builtin_prefetch(p);
115d9911d14SRichard Henderson         if (unlikely(!_mm256_testz_si256(t, t))) {
116d9911d14SRichard Henderson             return false;
117d9911d14SRichard Henderson         }
118d9911d14SRichard Henderson         t = p[-4] | p[-3] | p[-2] | p[-1];
119d9911d14SRichard Henderson         p += 4;
1208f13a39dSRobert Hoo     } ;
121d9911d14SRichard Henderson 
122d9911d14SRichard Henderson     /* Finish the last block of 128 unaligned.  */
123d9911d14SRichard Henderson     t |= _mm256_loadu_si256(buf + len - 4 * 32);
124d9911d14SRichard Henderson     t |= _mm256_loadu_si256(buf + len - 3 * 32);
125d9911d14SRichard Henderson     t |= _mm256_loadu_si256(buf + len - 2 * 32);
126d9911d14SRichard Henderson     t |= _mm256_loadu_si256(buf + len - 1 * 32);
127d9911d14SRichard Henderson 
128d9911d14SRichard Henderson     return _mm256_testz_si256(t, t);
129d9911d14SRichard Henderson }
130d9911d14SRichard Henderson #endif /* CONFIG_AVX2_OPT */
131d9911d14SRichard Henderson 
13251f4d916SRichard Henderson static unsigned __attribute__((noinline))
13351f4d916SRichard Henderson select_accel_cpuinfo(unsigned info)
134d9911d14SRichard Henderson {
13551f4d916SRichard Henderson     /* Array is sorted in order of algorithm preference. */
13651f4d916SRichard Henderson     static const struct {
13751f4d916SRichard Henderson         unsigned bit;
13851f4d916SRichard Henderson         bool (*fn)(const void *, size_t);
13951f4d916SRichard Henderson     } all[] = {
14051f4d916SRichard Henderson #ifdef CONFIG_AVX2_OPT
141*cbe3d526SAlexander Monakov         { CPUINFO_AVX2,    buffer_zero_avx2 },
14251f4d916SRichard Henderson #endif
143*cbe3d526SAlexander Monakov         { CPUINFO_SSE2,    buffer_zero_sse2 },
144*cbe3d526SAlexander Monakov         { CPUINFO_ALWAYS,  buffer_is_zero_integer },
14551f4d916SRichard Henderson     };
14651f4d916SRichard Henderson 
14751f4d916SRichard Henderson     for (unsigned i = 0; i < ARRAY_SIZE(all); ++i) {
14851f4d916SRichard Henderson         if (info & all[i].bit) {
149*cbe3d526SAlexander Monakov             buffer_is_zero_accel = all[i].fn;
15051f4d916SRichard Henderson             return all[i].bit;
15151f4d916SRichard Henderson         }
15251f4d916SRichard Henderson     }
15351f4d916SRichard Henderson     return 0;
154d9911d14SRichard Henderson }
1555e33a872SRichard Henderson 
156*cbe3d526SAlexander Monakov static unsigned used_accel;
157*cbe3d526SAlexander Monakov 
15851f4d916SRichard Henderson static void __attribute__((constructor)) init_accel(void)
15988ca8e80SRichard Henderson {
16051f4d916SRichard Henderson     used_accel = select_accel_cpuinfo(cpuinfo_init());
1615e33a872SRichard Henderson }
162*cbe3d526SAlexander Monakov 
163*cbe3d526SAlexander Monakov #define INIT_ACCEL NULL
1645e33a872SRichard Henderson 
165efad6682SRichard Henderson bool test_buffer_is_zero_next_accel(void)
166efad6682SRichard Henderson {
16751f4d916SRichard Henderson     /*
16851f4d916SRichard Henderson      * Accumulate the accelerators that we've already tested, and
16951f4d916SRichard Henderson      * remove them from the set to test this round.  We'll get back
17051f4d916SRichard Henderson      * a zero from select_accel_cpuinfo when there are no more.
17151f4d916SRichard Henderson      */
17251f4d916SRichard Henderson     unsigned used = select_accel_cpuinfo(cpuinfo & ~used_accel);
17351f4d916SRichard Henderson     used_accel |= used;
17451f4d916SRichard Henderson     return used;
175efad6682SRichard Henderson }
1765e33a872SRichard Henderson #else
177efad6682SRichard Henderson bool test_buffer_is_zero_next_accel(void)
178efad6682SRichard Henderson {
179efad6682SRichard Henderson     return false;
180efad6682SRichard Henderson }
181*cbe3d526SAlexander Monakov 
182*cbe3d526SAlexander Monakov #define INIT_ACCEL buffer_is_zero_integer
183efad6682SRichard Henderson #endif
184efad6682SRichard Henderson 
185*cbe3d526SAlexander Monakov static bool (*buffer_is_zero_accel)(const void *, size_t) = INIT_ACCEL;
186*cbe3d526SAlexander Monakov 
187*cbe3d526SAlexander Monakov bool buffer_is_zero_ool(const void *buf, size_t len)
18888ca8e80SRichard Henderson {
1895e33a872SRichard Henderson     if (unlikely(len == 0)) {
19088ca8e80SRichard Henderson         return true;
19188ca8e80SRichard Henderson     }
192*cbe3d526SAlexander Monakov     if (!buffer_is_zero_sample3(buf, len)) {
193*cbe3d526SAlexander Monakov         return false;
194*cbe3d526SAlexander Monakov     }
195*cbe3d526SAlexander Monakov     /* All bytes are covered for any len <= 3.  */
196*cbe3d526SAlexander Monakov     if (unlikely(len <= 3)) {
197*cbe3d526SAlexander Monakov         return true;
198*cbe3d526SAlexander Monakov     }
19988ca8e80SRichard Henderson 
200*cbe3d526SAlexander Monakov     if (likely(len >= 256)) {
201*cbe3d526SAlexander Monakov         return buffer_is_zero_accel(buf, len);
202*cbe3d526SAlexander Monakov     }
203*cbe3d526SAlexander Monakov     return buffer_is_zero_integer(buf, len);
204*cbe3d526SAlexander Monakov }
205083d012aSRichard Henderson 
206*cbe3d526SAlexander Monakov bool buffer_is_zero_ge256(const void *buf, size_t len)
207*cbe3d526SAlexander Monakov {
208*cbe3d526SAlexander Monakov     return buffer_is_zero_accel(buf, len);
2095e33a872SRichard Henderson }
210