brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 862aeec Raw
80 lines · c
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-exception6//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_H13#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BYTE_PER_BYTE_H14 15#include "src/__support/macros/attributes.h"   // LIBC_INLINE16#include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL17#include "src/string/memory_utils/utils.h"     // Ptr, CPtr18 19#include <stddef.h> // size_t20 21namespace LIBC_NAMESPACE_DECL {22 23[[maybe_unused]] LIBC_INLINE void24inline_memcpy_byte_per_byte(Ptr dst, CPtr src, size_t count,25                            size_t offset = 0) {26  LIBC_LOOP_NOUNROLL27  for (; offset < count; ++offset)28    dst[offset] = src[offset];29}30 31[[maybe_unused]] LIBC_INLINE void32inline_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_NOUNROLL37    for (size_t offset = 0; offset < count; ++offset)38      dst[offset] = src[offset];39  } else {40    LIBC_LOOP_NOUNROLL41    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 void48inline_memset_byte_per_byte(Ptr dst, uint8_t value, size_t count,49                            size_t offset = 0) {50  LIBC_LOOP_NOUNROLL51  for (; offset < count; ++offset)52    dst[offset] = static_cast<cpp::byte>(value);53}54 55[[maybe_unused]] LIBC_INLINE BcmpReturnType56inline_bcmp_byte_per_byte(CPtr p1, CPtr p2, size_t count, size_t offset = 0) {57  LIBC_LOOP_NOUNROLL58  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 MemcmpReturnType65inline_memcmp_byte_per_byte(CPtr p1, CPtr p2, size_t count, size_t offset = 0) {66  LIBC_LOOP_NOUNROLL67  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_DECL78 79#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BYTE_PER_BYTE_H80