Skip to content

Commit 9e5ebea

Browse files
committed
Reapply "[libc] Enable wide-read memory operations by default on Linux (llvm#154602)"
This reverts commit 27fc967.
1 parent 27fc967 commit 9e5ebea

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-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: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ 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
22+
compare_and_mask(const Vector *block_ptr);
2223

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

2830
const Vector *block_ptr =
2931
reinterpret_cast<const Vector *>(src - misalign_bytes);
30-
auto cmp = CompareAndMask<Vector, Mask>(block_ptr) >> misalign_bytes;
32+
auto cmp = compare_and_mask<Vector, Mask>(block_ptr) >> misalign_bytes;
3133
if (cmp)
3234
return cpp::countr_zero(cmp);
3335

3436
while (true) {
3537
block_ptr++;
36-
cmp = CompareAndMask<Vector, Mask>(block_ptr);
38+
cmp = compare_and_mask<Vector, Mask>(block_ptr);
3739
if (cmp)
3840
return static_cast<size_t>(reinterpret_cast<uintptr_t>(block_ptr) -
3941
reinterpret_cast<uintptr_t>(src) +
@@ -42,7 +44,8 @@ size_t string_length_vector(const char *src) {
4244
}
4345

4446
template <>
45-
uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) {
47+
LIBC_INLINE uint32_t
48+
compare_and_mask<__m128i, uint32_t>(const __m128i *block_ptr) {
4649
__m128i v = _mm_load_si128(block_ptr);
4750
__m128i z = _mm_setzero_si128();
4851
__m128i c = _mm_cmpeq_epi8(z, v);
@@ -52,13 +55,14 @@ uint32_t CompareAndMask<__m128i, uint32_t>(const __m128i *block_ptr) {
5255
namespace sse2 {
5356
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
5457
return string_length_vector<__m128i, uint32_t,
55-
CompareAndMask<__m128i, uint32_t>>(src);
58+
compare_and_mask<__m128i, uint32_t>>(src);
5659
}
5760
} // namespace sse2
5861

5962
#if defined(__AVX2__)
6063
template <>
61-
uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) {
64+
LIBC_INLINE uint32_t
65+
compare_and_mask<__m256i, uint32_t>(const __m256i *block_ptr) {
6266
__m256i v = _mm256_load_si256(block_ptr);
6367
__m256i z = _mm256_setzero_si256();
6468
__m256i c = _mm256_cmpeq_epi8(z, v);
@@ -68,22 +72,23 @@ uint32_t CompareAndMask<__m256i, uint32_t>(const __m256i *block_ptr) {
6872
namespace avx2 {
6973
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
7074
return string_length_vector<__m256i, uint32_t,
71-
CompareAndMask<__m256i, uint32_t>>(src);
75+
compare_and_mask<__m256i, uint32_t>>(src);
7276
}
7377
} // namespace avx2
7478
#endif
7579

7680
#if defined(__AVX512F__)
7781
template <>
78-
__mmask64 CompareAndMask<__m512i, __mmask64>(const __m512i *block_ptr) {
82+
LIBC_INLINE __mmask64
83+
compare_and_mask<__m512i, __mmask64>(const __m512i *block_ptr) {
7984
__m512i v = _mm512_load_si512(block_ptr);
8085
__m512i z = _mm512_setzero_si512();
8186
return _mm512_cmp_epu8_mask(z, v, _MM_CMPINT_EQ);
8287
}
8388
namespace avx512 {
8489
[[maybe_unused]] LIBC_INLINE size_t string_length(const char *src) {
8590
return string_length_vector<__m512i, __mmask64,
86-
CompareAndMask<__m512i, __mmask64>>(src);
91+
compare_and_mask<__m512i, __mmask64>>(src);
8792
}
8893
} // namespace avx512
8994
#endif

0 commit comments

Comments
 (0)