Skip to content

Commit ac36e4a

Browse files
Address comments
1 parent d9889cd commit ac36e4a

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

libc/src/string/memory_utils/aarch64/inline_strlen.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <stddef.h> // size_t
1616

1717
namespace LIBC_NAMESPACE_DECL {
18-
[[maybe_unused]] LIBC_INLINE size_t string_length_neon(const char *src) {
18+
19+
namespace neon {
20+
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
1921
using Vector __attribute__((may_alias)) = uint8x8_t;
2022

2123
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector);
@@ -40,12 +42,10 @@ namespace LIBC_NAMESPACE_DECL {
4042
(cpp::countr_zero(cmp) >> 3));
4143
}
4244
}
45+
} // namespace neon
4346

44-
template <typename T>
45-
[[maybe_unused]] LIBC_INLINE void string_length_aarch64(const char *src) {
46-
return inline_string_length_neon(src);
47-
}
48-
} // namespace LIBC_NAMESPACE_DECL
47+
namespace string_length_impl = neon;
4948

49+
} // namespace LIBC_NAMESPACE_DECL
5050
#endif // __ARM_NEON
5151
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_STRLEN_H

libc/src/string/memory_utils/x86_64/inline_strlen.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_X86_64_INLINE_STRLEN_H
1010

1111
#include "src/__support/CPP/bit.h" // countr_zero
12-
#include "src/string/memory_utils/op_x86.h" // K_AVX
1312

13+
#include <immintrin.h>
1414
#include <stddef.h> // size_t
1515

1616
namespace LIBC_NAMESPACE_DECL {
1717

18-
[[maybe_unused]] LIBC_INLINE size_t string_length_sse2(const char *src) {
18+
namespace sse2 {
19+
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
1920
using Vector __attribute__((may_alias)) = __m128i;
2021

2122
Vector z = _mm_setzero_si128();
@@ -40,9 +41,11 @@ namespace LIBC_NAMESPACE_DECL {
4041
cpp::countr_zero(cmp));
4142
}
4243
}
44+
} // namespace sse2
4345

4446
#if defined(__AVX2__)
45-
[[maybe_unused]] LIBC_INLINE size_t string_length_avx2(const char *src) {
47+
namespace avx2 {
48+
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
4649
using Vector __attribute__((may_alias)) = __mm256i;
4750

4851
Vector z = _mm256_setzero_si256();
@@ -67,10 +70,12 @@ namespace LIBC_NAMESPACE_DECL {
6770
cpp::countr_zero(cmp));
6871
}
6972
}
70-
#endif // __AVX2__
73+
}
74+
#endif
7175

7276
#if defined(__AVX512F__)
73-
[[maybe_unused]] LIBC_INLINE size_t string_length_avx512(const char *src) {
77+
namespace avx512 {
78+
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
7479
using Vector __attribute__((may_alias)) = __mm512i;
7580

7681
Vector z = _mm512_setzero_si512();
@@ -92,18 +97,16 @@ namespace LIBC_NAMESPACE_DECL {
9297
cpp::countr_zero(cmp));
9398
}
9499
}
95-
#endif // __AVX512F__
100+
} // namespace avx512
101+
#endif
96102

97-
namespace x86 {
98-
template <typename T> LIBC_INLINE size_t string_length_x86_64(const char *src) {
99103
#if defined(__AVX512F__)
100-
return string_length_avx512(src);
104+
namespace string_length_impl = avx512;
101105
#elif defined(__AVX2__)
102-
return string_length_avx2(src);
106+
namespace string_length_impl = avx2;
107+
#else
108+
namespace string_length_impl = sse2;
103109
#endif
104-
return string_length_sse2(src);
105-
}
106-
}
107110

108111
} // namespace LIBC_NAMESPACE_DECL
109112

libc/src/string/string_utils.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
#if defined(LIBC_COPT_STRING_UNSAFE_WIDE_READ)
2626
#if defined(LIBC_TARGET_ARCH_IS_X86)
2727
#include "src/string/memory_utils/x86_64/inline_strlen.h"
28-
namespace wide_read_impl = x86;
2928
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_NEON)
3029
#include "src/string/memory_utils/aarch64/inline_strlen.h"
31-
namespace wide_read_impl = aarch64;
3230
#else
33-
namespace wide_read_impl = default_wide_read;
31+
namespace string_length_impl = LIBC_NAMESPACE::wide_read;
3432
#endif
3533
#endif
3634

@@ -72,7 +70,6 @@ template <typename Word> LIBC_INLINE constexpr bool has_zeroes(Word block) {
7270
return (subtracted & inverted & HIGH_BITS) != 0;
7371
}
7472

75-
namespace default_wide_read {
7673
template <typename Word>
7774
LIBC_INLINE size_t string_length_wide_read(const char *src) {
7875
const char *char_ptr = src;
@@ -93,18 +90,24 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) {
9390
}
9491
return static_cast<size_t>(char_ptr - src);
9592
}
96-
} // namespace default_wide_read
9793

98-
// Returns the length of a string, denoted by the first occurrence
99-
// of a null terminator.
100-
template <typename T> LIBC_INLINE size_t string_length(const T *src) {
101-
#ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
94+
namespace wide_read {
95+
LIBC_INLINE size_t string_length(const char *src) {
10296
// Unsigned int is the default size for most processors, and on x86-64 it
10397
// performs better than larger sizes when the src pointer can't be assumed to
10498
// be aligned to a word boundary, so it's the size we use for reading the
10599
// string a block at a time.
100+
return string_length_wide_read<unsigned int>(src);
101+
}
102+
103+
} // namespace wide_read
104+
105+
// Returns the length of a string, denoted by the first occurrence
106+
// of a null terminator.
107+
template <typename T> LIBC_INLINE size_t string_length(const T *src) {
108+
#ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
106109
if constexpr (cpp::is_same_v<T, char>)
107-
return wide_read_impl::string_length(src);
110+
return string_length_impl::string_length(src);
108111
#endif
109112
size_t length;
110113
for (length = 0; *src; ++src, ++length)

0 commit comments

Comments
 (0)