[libc] [llvm] Add vector-based strlen implementation for x86_64 and aarch64 (PR #152389)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 14:24:44 PDT 2025


================
@@ -0,0 +1,50 @@
+//===-- 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
+
+#include <arm_neon.h>
+#include <stddef.h> // size_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t string_length_neon(const char* src) {
+  using Vector __attribute__((may_alias)) = uint8x8_t;
+  uintptr_t misalign_bytes = reinterpret_case<uintptr_t>(src) % sizeof(Vector);
+  Vector *block_ptr = reinterpret_cast<Vector *>(src - misalign_bytes);
+  if (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 __builtin_ctzl(cmp) >> 3;
----------------
jhuber6 wrote:

```suggestion
    if (cmp) return cpp::countr_zero(cmp) >> 3;
```

https://github.com/llvm/llvm-project/pull/152389


More information about the llvm-commits mailing list