Warning: This file is not a C or C++ file. It does not have highlighting.

1//===-- Trivial byte per byte implementations ----------------------------===//
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// Straightforward implementations targeting the smallest code size possible.
9// This needs to be compiled with '-Os' or '-Oz'.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BYTE_PER_BYTE_H
13#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BYTE_PER_BYTE_H
14
15#include "src/__support/macros/attributes.h" // LIBC_INLINE
16#include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL
17#include "src/string/memory_utils/utils.h" // Ptr, CPtr
18
19#include <stddef.h> // size_t
20
21namespace LIBC_NAMESPACE_DECL {
22
23[[maybe_unused]] LIBC_INLINE void
24inline_memcpy_byte_per_byte(Ptr dst, CPtr src, size_t count,
25 size_t offset = 0) {
26 LIBC_LOOP_NOUNROLL
27 for (; offset < count; ++offset)
28 dst[offset] = src[offset];
29}
30
31[[maybe_unused]] LIBC_INLINE void
32inline_memmove_byte_per_byte(Ptr dst, CPtr src, size_t count) {
33 if (count == 0 || dst == src)
34 return;
35 if (dst < src) {
36 LIBC_LOOP_NOUNROLL
37 for (size_t offset = 0; offset < count; ++offset)
38 dst[offset] = src[offset];
39 } else {
40 LIBC_LOOP_NOUNROLL
41 for (ptrdiff_t offset = static_cast<ptrdiff_t>(count - 1); offset >= 0;
42 --offset)
43 dst[offset] = src[offset];
44 }
45}
46
47[[maybe_unused]] LIBC_INLINE static void
48inline_memset_byte_per_byte(Ptr dst, uint8_t value, size_t count,
49 size_t offset = 0) {
50 LIBC_LOOP_NOUNROLL
51 for (; offset < count; ++offset)
52 dst[offset] = static_cast<cpp::byte>(value);
53}
54
55[[maybe_unused]] LIBC_INLINE BcmpReturnType
56inline_bcmp_byte_per_byte(CPtr p1, CPtr p2, size_t count, size_t offset = 0) {
57 LIBC_LOOP_NOUNROLL
58 for (; offset < count; ++offset)
59 if (p1[offset] != p2[offset])
60 return BcmpReturnType::nonzero();
61 return BcmpReturnType::zero();
62}
63
64[[maybe_unused]] LIBC_INLINE MemcmpReturnType
65inline_memcmp_byte_per_byte(CPtr p1, CPtr p2, size_t count, size_t offset = 0) {
66 LIBC_LOOP_NOUNROLL
67 for (; offset < count; ++offset) {
68 const int32_t a = static_cast<int32_t>(p1[offset]);
69 const int32_t b = static_cast<int32_t>(p2[offset]);
70 const int32_t diff = a - b;
71 if (diff)
72 return diff;
73 }
74 return MemcmpReturnType::zero();
75}
76
77} // namespace LIBC_NAMESPACE_DECL
78
79#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BYTE_PER_BYTE_H
80

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libc/src/string/memory_utils/generic/byte_per_byte.h