[libc-commits] [libc] [libc] Implement generic SIMD helper 'simd.h' and implement strlen (PR #152605)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Wed Aug 27 08:01:11 PDT 2025
================
@@ -0,0 +1,53 @@
+//===-- Strlen for generic SIMD types -------------------------------------===//
+//
+// 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_GENERIC_INLINE_STRLEN_H
+#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_INLINE_STRLEN_H
+
+#include "src/__support/CPP/simd.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+// Exploit the underlying integer representation to do a variable shift.
+LIBC_INLINE constexpr cpp::simd_mask<char> shift_mask(cpp::simd_mask<char> m,
+ size_t shift) {
+ using bitmask_ty = cpp::internal::get_as_integer_type_t<cpp::simd_mask<char>>;
+ bitmask_ty r = cpp::bit_cast<bitmask_ty>(m) >> shift;
+ return cpp::bit_cast<cpp::simd_mask<char>>(r);
+}
+
+[[clang::no_sanitize("address")]] LIBC_INLINE size_t
+string_length(const char *src) {
----------------
SchrodingerZhu wrote:
Ideally, you can use where(idx < len, vector).copy_from, which translates to `maskload` in AVX2/AVX512 and `wherelt` masked load in SVE. With that, fault is suppressed automatically and the whole operation is safe.
See https://www.felixcloutier.com/x86/vpmaskmov
See also
https://github.com/llvm/llvm-project/pull/152605
More information about the libc-commits
mailing list