Skip to content

Commit 6ac01d1

Browse files
authored
Reapply "[libc] Enable wide-read memory operations by default on Linux (#154602)" (#154640)
Reland afterr the sanitizer and arm32 builds complained.
1 parent 2b1dcf5 commit 6ac01d1

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

libc/config/linux/arm/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"string": {
3+
"LIBC_CONF_STRING_UNSAFE_WIDE_READ": {
4+
"value": false
5+
}
6+
}
7+
}

libc/config/linux/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"string": {
3+
"LIBC_CONF_STRING_UNSAFE_WIDE_READ": {
4+
"value": true
5+
}
6+
}
7+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818

1919
namespace neon {
20-
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
20+
[[gnu::no_sanitize_address]] [[maybe_unused]] LIBC_INLINE static size_t
21+
string_length(const char *src) {
2122
using Vector __attribute__((may_alias)) = uint8x8_t;
2223

2324
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector);
24-
Vector *block_ptr = reinterpret_cast<Vector *>(src - misalign_bytes);
25+
const Vector *block_ptr =
26+
reinterpret_cast<const Vector *>(src - misalign_bytes);
2527
Vector v = *block_ptr;
2628
Vector vcmp = vceqz_u8(v);
27-
uint64x1_t cmp_mask = vreinterpret_u64_s8(vcmp);
29+
uint64x1_t cmp_mask = vreinterpret_u64_u8(vcmp);
2830
uint64_t cmp = vget_lane_u64(cmp_mask, 0);
2931
cmp = cmp >> (misalign_bytes << 3);
3032
if (cmp)
@@ -34,7 +36,7 @@ namespace neon {
3436
++block_ptr;
3537
v = *block_ptr;
3638
vcmp = vceqz_u8(v);
37-
cmp_mask = vreinterpret_u64_s8(vcmp);
39+
cmp_mask = vreinterpret_u64_u8(vcmp);
3840
cmp = vget_lane_u64(cmp_mask, 0);
3941
if (cmp)
4042
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) -

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@ namespace LIBC_NAMESPACE_DECL {
1818
namespace string_length_internal {
1919
// Return a bit-mask with the nth bit set if the nth-byte in block_ptr is zero.
2020
template <typename Vector, typename Mask>
21-
Mask CompareAndMask(const Vector *block_ptr);
21+
LIBC_INLINE static Mask compare_and_mask(const Vector *block_ptr);
2222

2323
template <typename Vector, typename Mask,
24-
decltype(CompareAndMask<Vector, Mask>)>
25-
size_t string_length_vector(const char *src) {
24+
decltype(compare_and_mask<Vector, Mask>)>
25+
[[gnu::no_sanitize_address]] LIBC_INLINE static size_t
26+
string_length_vector(const char *src) {
2627
uintptr_t misalign_bytes = reinterpret_cast<uintptr_t>(src) % sizeof(Vector);
2728

2829
const Vector *block_ptr =
2930
reinterpret_cast<const Vector *>(src - misalign_bytes);
30-
auto cmp = CompareAndMask<Vector, Mask>(block_ptr) >> misalign_bytes;
31+
auto cmp = compare_and_mask<Vector, Mask>(block_ptr) >> misalign_bytes;
3132
if (cmp)
3233
return cpp::countr_zero(cmp);
3334

3435
while (true) {
3536
block_ptr++;
36-
cmp = CompareAndMask<Vector, Mask>(block_ptr);
37+
cmp = compare_and_mask<Vector, Mask>(block_ptr);
3738
if (cmp)
3839
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) -
3940
reinterpret_cast<uintptr_t>(src) +
@@ -42,7 +43,8 @@ size_t string_length_vector(const char *src) {
4243
}
4344

4445
template <>
45-
uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) {
46+
LIBC_INLINE uint32_t
47+
compare_and_mask<__m128i, uint32_t>(const __m128i *block_ptr) {
4648
__m128i v = _mm_load_si128(block_ptr);
4749
__m128i z = _mm_setzero_si128();
4850
__m128i c = _mm_cmpeq_epi8(z, v);
@@ -52,13 +54,14 @@ uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) {
5254
namespace sse2 {
5355
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
5456
return string_length_vector<__m128i, uint32_t,
55-
CompareAndMask<__m128i, uint32_t>>(src);
57+
compare_and_mask<__m128i, uint32_t>>(src);
5658
}
5759
} // namespace sse2
5860

5961
#if defined(__AVX2__)
6062
template <>
61-
uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) {
63+
LIBC_INLINE uint32_t
64+
compare_and_mask<__m256i, uint32_t>(const __m256i *block_ptr) {
6265
__m256i v = _mm256_load_si256(block_ptr);
6366
__m256i z = _mm256_setzero_si256();
6467
__m256i c = _mm256_cmpeq_epi8(z, v);
@@ -68,22 +71,23 @@ uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) {
6871
namespace avx2 {
6972
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
7073
return string_length_vector<__m256i, uint32_t,
71-
CompareAndMask<__m256i, uint32_t>>(src);
74+
compare_and_mask<__m256i, uint32_t>>(src);
7275
}
7376
} // namespace avx2
7477
#endif
7578

7679
#if defined(__AVX512F__)
7780
template <>
78-
__mmask64 CompareAndMask<__m512i, __mmask64>(const __m512i *block_ptr) {
81+
LIBC_INLINE __mmask64
82+
compare_and_mask<__m512i, __mmask64>(const __m512i *block_ptr) {
7983
__m512i v = _mm512_load_si512(block_ptr);
8084
__m512i z = _mm512_setzero_si512();
8185
return _mm512_cmp_epu8_mask(z, v, _MM_CMPINT_EQ);
8286
}
8387
namespace avx512 {
8488
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
8589
return string_length_vector<__m512i, __mmask64,
86-
CompareAndMask<__m512i, __mmask64>>(src);
90+
compare_and_mask<__m512i, __mmask64>>(src);
8791
}
8892
} // namespace avx512
8993
#endif

0 commit comments

Comments
 (0)