1 //===-- Memset implementation for aarch64 -----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_MEMSET_H
9 #define LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_MEMSET_H
10
11 #include "src/__support/macros/attributes.h" // LIBC_INLINE
12 #include "src/__support/macros/config.h"
13 #include "src/string/memory_utils/op_aarch64.h"
14 #include "src/string/memory_utils/op_generic.h"
15 #include "src/string/memory_utils/utils.h" // Ptr, CPtr
16
17 #include <stddef.h> // size_t
18
19 namespace LIBC_NAMESPACE_DECL {
20
21 [[maybe_unused]] LIBC_INLINE static void
inline_memset_aarch64(Ptr dst,uint8_t value,size_t count)22 inline_memset_aarch64(Ptr dst, uint8_t value, size_t count) {
23 static_assert(aarch64::kNeon, "aarch64 supports vector types");
24 using uint128_t = generic_v128;
25 using uint256_t = generic_v256;
26 using uint512_t = generic_v512;
27 if (count == 0)
28 return;
29 if (count <= 3) {
30 generic::Memset<uint8_t>::block(dst, value);
31 if (count > 1)
32 generic::Memset<uint16_t>::tail(dst, value, count);
33 return;
34 }
35 if (count <= 8)
36 return generic::Memset<uint32_t>::head_tail(dst, value, count);
37 if (count <= 16)
38 return generic::Memset<uint64_t>::head_tail(dst, value, count);
39 if (count <= 32)
40 return generic::Memset<uint128_t>::head_tail(dst, value, count);
41 if (count <= (32 + 64)) {
42 generic::Memset<uint256_t>::block(dst, value);
43 if (count <= 64)
44 return generic::Memset<uint256_t>::tail(dst, value, count);
45 generic::Memset<uint256_t>::block(dst + 32, value);
46 generic::Memset<uint256_t>::tail(dst, value, count);
47 return;
48 }
49 if (count >= 448 && value == 0 && aarch64::neon::hasZva()) {
50 generic::Memset<uint512_t>::block(dst, 0);
51 align_to_next_boundary<64>(dst, count);
52 return aarch64::neon::BzeroCacheLine::loop_and_tail(dst, 0, count);
53 } else {
54 generic::Memset<uint128_t>::block(dst, value);
55 align_to_next_boundary<16>(dst, count);
56 return generic::Memset<uint512_t>::loop_and_tail(dst, value, count);
57 }
58 }
59
60 } // namespace LIBC_NAMESPACE_DECL
61
62 #endif // LIBC_SRC_STRING_MEMORY_UTILS_AARCH64_INLINE_MEMSET_H
63