|
| 1 | +//===-- Strlen implementation for x86_64 ----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H |
| 9 | +#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H |
| 10 | + |
| 11 | +#include "src/__support/CPP/bit.h" // countr_zero |
| 12 | + |
| 13 | +#include <immintrin.h> |
| 14 | +#include <stddef.h> // size_t |
| 15 | + |
| 16 | +namespace LIBC_NAMESPACE_DECL { |
| 17 | + |
| 18 | +namespace string_length_internal { |
| 19 | +// Return a bit-mask with the nth bit set if the nth-byte in block_ptr is zero. |
| 20 | +template <typename Vector, typename Mask> |
| 21 | +Mask CompareAndMask(const Vector *block_ptr); |
| 22 | + |
| 23 | +template <typename Vector, typename Mask, |
| 24 | + decltype(CompareAndMask<Vector, Mask>)> |
| 25 | +size_t string_length_vector(const char *src) { |
| 26 | + uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector); |
| 27 | + |
| 28 | + const Vector *block_ptr = |
| 29 | + reinterpret_cast<const Vector *>(src - misalign_bytes); |
| 30 | + auto cmp = CompareAndMask<Vector, Mask>(block_ptr) >> misalign_bytes; |
| 31 | + if (cmp) |
| 32 | + return cpp::countr_zero(cmp); |
| 33 | + |
| 34 | + while (true) { |
| 35 | + block_ptr++; |
| 36 | + cmp = CompareAndMask<Vector, Mask>(block_ptr); |
| 37 | + if (cmp) |
| 38 | + return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) - |
| 39 | + reinterpret_cast<uintptr_t>(src) + |
| 40 | + cpp::countr_zero(cmp)); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +template <> |
| 45 | +uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) { |
| 46 | + __m128i v = _mm_load_si128(block_ptr); |
| 47 | + __m128i z = _mm_setzero_si128(); |
| 48 | + __m128i c = _mm_cmpeq_epi8(z, v); |
| 49 | + return _mm_movemask_epi8(c); |
| 50 | +} |
| 51 | + |
| 52 | +namespace sse2 { |
| 53 | +[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) { |
| 54 | + return string_length_vector<__m128i, uint32_t, |
| 55 | + CompareAndMask<__m128i, uint32_t>>(src); |
| 56 | +} |
| 57 | +} // namespace sse2 |
| 58 | + |
| 59 | +#if defined(__AVX2__) |
| 60 | +template <> |
| 61 | +uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) { |
| 62 | + __m256i v = _mm256_load_si256(block_ptr); |
| 63 | + __m256i z = _mm256_setzero_si256(); |
| 64 | + __m256i c = _mm256_cmpeq_epi8(z, v); |
| 65 | + return _mm256_movemask_epi8(c); |
| 66 | +} |
| 67 | + |
| 68 | +namespace avx2 { |
| 69 | +[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) { |
| 70 | + return string_length_vector<__m256i, uint32_t, |
| 71 | + CompareAndMask<__m256i, uint32_t>>(src); |
| 72 | +} |
| 73 | +} // namespace avx2 |
| 74 | +#endif |
| 75 | + |
| 76 | +#if defined(__AVX512F__) |
| 77 | +template <> |
| 78 | +__mmask64 CompareAndMask<__m512i, __mmask64>(const __m512i *block_ptr) { |
| 79 | + __m512i v = _mm512_load_si512(block_ptr); |
| 80 | + __m512i z = _mm512_setzero_si512(); |
| 81 | + return _mm512_cmp_epu8_mask(z, v, _MM_CMPINT_EQ); |
| 82 | +} |
| 83 | +namespace avx512 { |
| 84 | +[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) { |
| 85 | + return string_length_vector<__m512i, __mmask64, |
| 86 | + CompareAndMask<__m512i, __mmask64>>(src); |
| 87 | +} |
| 88 | +} // namespace avx512 |
| 89 | +#endif |
| 90 | +} // namespace string_length_internal |
| 91 | + |
| 92 | +#if defined(__AVX512F__) |
| 93 | +namespace string_length_impl = string_length_internal::avx512; |
| 94 | +#elif defined(__AVX2__) |
| 95 | +namespace string_length_impl = string_length_internal::avx2; |
| 96 | +#else |
| 97 | +namespace string_length_impl = string_length_internal::sse2; |
| 98 | +#endif |
| 99 | + |
| 100 | +} // namespace LIBC_NAMESPACE_DECL |
| 101 | + |
| 102 | +#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H |
0 commit comments