-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add vector-based strlen implementation for x86_64 and aarch64 #152389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sterling-Augustine
merged 10 commits into
llvm:main
from
Sterling-Augustine:strlen_avx2
Aug 19, 2025
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a4760a
Add vector-based strlen implementation for x86_64 and aarch64
Sterling-Augustine 6a1f6dd
Merge branch 'main' into strlen_avx2
Sterling-Augustine fd74316
Fix formatting. Undo config changed for testing.
Sterling-Augustine 863ad74
Address some comments
Sterling-Augustine d9889cd
Merge branch 'main' into strlen_avx2
Sterling-Augustine ac36e4a
Address comments
Sterling-Augustine 61e0181
Fix formatting
Sterling-Augustine d616a95
Refactor to template-based implementation
Sterling-Augustine 6d23ff3
Fix formatting
Sterling-Augustine 9c0d577
Update function modifiers for LLVM-libc style
Sterling-Augustine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===-- Strlen implementation for aarch64 ---------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H | ||
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H | ||
|
||
#if defined(__ARM_NEON) | ||
#include "src/__support/CPP/bit.h" // countr_zero | ||
|
||
#include <arm_neon.h> | ||
#include <stddef.h> // size_t | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
namespace neon { | ||
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) { | ||
using Vector __attribute__((may_alias)) = uint8x8_t; | ||
|
||
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector); | ||
Vector *block_ptr = reinterpret_cast<Vector *>(src - misalign_bytes); | ||
Vector v = *block_ptr; | ||
Vector vcmp = vceqz_u8(v); | ||
uint64x1_t cmp_mask = vreinterpret_u64_s8(vcmp); | ||
uint64_t cmp = vget_lane_u64(cmp_mask, 0); | ||
cmp = cmp >> (misalign_bytes << 3); | ||
if (cmp) | ||
return cpp::countr_zero(cmp) >> 3; | ||
|
||
while (true) { | ||
++block_ptr; | ||
v = *block_ptr; | ||
vcmp = vceqz_u8(v); | ||
cmp_mask = vreinterpret_u64_s8(vcmp); | ||
cmp = vget_lane_u64(cmp_mask, 0); | ||
if (cmp) | ||
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) - | ||
reinterpret_cast<uintptr_t>(src) + | ||
(cpp::countr_zero(cmp) >> 3)); | ||
} | ||
} | ||
} // namespace neon | ||
|
||
namespace string_length_impl = neon; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
#endif // __ARM_NEON | ||
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//===-- Strlen implementation for x86_64 ----------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H | ||
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H | ||
|
||
#include "src/__support/CPP/bit.h" // countr_zero | ||
|
||
#include <immintrin.h> | ||
#include <stddef.h> // size_t | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
namespace string_length_internal { | ||
// Return a bit-mask with the nth bit set if the nth-byte in block_ptr is zero. | ||
template <typename Vector, typename Mask> | ||
Mask CompareAndMask(const Vector *block_ptr); | ||
|
||
template <typename Vector, typename Mask, | ||
decltype(CompareAndMask<Vector, Mask>)> | ||
size_t string_length_vector(const char *src) { | ||
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector); | ||
|
||
const Vector *block_ptr = | ||
reinterpret_cast<const Vector *>(src - misalign_bytes); | ||
auto cmp = CompareAndMask<Vector, Mask>(block_ptr) >> misalign_bytes; | ||
if (cmp) | ||
return cpp::countr_zero(cmp); | ||
|
||
while (true) { | ||
block_ptr++; | ||
cmp = CompareAndMask<Vector, Mask>(block_ptr); | ||
if (cmp) | ||
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) - | ||
reinterpret_cast<uintptr_t>(src) + | ||
cpp::countr_zero(cmp)); | ||
} | ||
} | ||
|
||
template <> | ||
uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) { | ||
__m128i v = _mm_load_si128(block_ptr); | ||
__m128i z = _mm_setzero_si128(); | ||
__m128i c = _mm_cmpeq_epi8(z, v); | ||
return _mm_movemask_epi8(c); | ||
} | ||
|
||
namespace sse2 { | ||
size_t string_length(const char *src) { | ||
return string_length_vector<__m128i, uint32_t, | ||
CompareAndMask<__m128i, uint32_t>>(src); | ||
} | ||
} // namespace sse2 | ||
|
||
#if defined(__AVX2__) | ||
template <> | ||
uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) { | ||
__m256i v = _mm256_load_si256(block_ptr); | ||
__m256i z = _mm256_setzero_si256(); | ||
__m256i c = _mm256_cmpeq_epi8(z, v); | ||
return _mm256_movemask_epi8(c); | ||
} | ||
|
||
namespace avx2 { | ||
size_t string_length(const char *src) { | ||
return string_length_vector<__m256i, uint32_t, | ||
CompareAndMask<__m256i, uint32_t>>(src); | ||
} | ||
} // namespace avx2 | ||
#endif | ||
|
||
#if defined(__AVX512F__) | ||
template <> | ||
__mmask64 CompareAndMask<__m512i, __mmask64>(const __m512i *block_ptr) { | ||
__m512i v = _mm512_load_si512(block_ptr); | ||
__m512i z = _mm512_setzero_si512(); | ||
return _mm512_cmp_epu8_mask(z, v, _MM_CMPINT_EQ); | ||
} | ||
namespace avx512 { | ||
size_t string_length(const char *src) { | ||
return string_length_vector<__m512i, __mmask64, | ||
CompareAndMask<__m512i, __mmask64>>(src); | ||
} | ||
} // namespace avx512 | ||
#endif | ||
} // namespace string_length_internal | ||
|
||
#if defined(__AVX512F__) | ||
namespace string_length_impl = string_length_internal::avx512; | ||
#elif defined(__AVX2__) | ||
namespace string_length_impl = string_length_internal::avx2; | ||
#else | ||
namespace string_length_impl = string_length_internal::sse2; | ||
#endif | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.