xref: /aosp_15_r20/external/llvm-libc/src/string/memory_utils/op_riscv.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- RISC-V implementation of memory function building blocks ----------===//
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 //
9 // This file provides x86 specific building blocks to compose memory functions.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_OP_RISCV_H
13 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_OP_RISCV_H
14 
15 #include "src/__support/macros/config.h"
16 #include "src/__support/macros/properties/architectures.h"
17 
18 #if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
19 
20 #include "src/__support/common.h"
21 #include "src/string/memory_utils/op_generic.h"
22 
23 namespace LIBC_NAMESPACE_DECL {
24 namespace generic {
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // Specializations for uint16_t
28 template <> struct cmp_is_expensive<uint16_t> : public cpp::false_type {};
29 template <> LIBC_INLINE bool eq<uint16_t>(CPtr p1, CPtr p2, size_t offset) {
30   return load<uint16_t>(p1, offset) == load<uint16_t>(p2, offset);
31 }
32 template <>
33 LIBC_INLINE uint32_t neq<uint16_t>(CPtr p1, CPtr p2, size_t offset) {
34   return load<uint16_t>(p1, offset) ^ load<uint16_t>(p2, offset);
35 }
36 template <>
37 LIBC_INLINE MemcmpReturnType cmp<uint16_t>(CPtr p1, CPtr p2, size_t offset) {
38   return static_cast<int32_t>(load_be<uint16_t>(p1, offset)) -
39          static_cast<int32_t>(load_be<uint16_t>(p2, offset));
40 }
41 template <>
42 LIBC_INLINE MemcmpReturnType cmp_neq<uint16_t>(CPtr p1, CPtr p2, size_t offset);
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 // Specializations for uint32_t
46 template <> struct cmp_is_expensive<uint32_t> : public cpp::false_type {};
47 template <> LIBC_INLINE bool eq<uint32_t>(CPtr p1, CPtr p2, size_t offset) {
48   return load<uint32_t>(p1, offset) == load<uint32_t>(p2, offset);
49 }
50 template <>
51 LIBC_INLINE uint32_t neq<uint32_t>(CPtr p1, CPtr p2, size_t offset) {
52   return load<uint32_t>(p1, offset) ^ load<uint32_t>(p2, offset);
53 }
54 template <>
55 LIBC_INLINE MemcmpReturnType cmp<uint32_t>(CPtr p1, CPtr p2, size_t offset) {
56   const auto a = load_be<uint32_t>(p1, offset);
57   const auto b = load_be<uint32_t>(p2, offset);
58   return cmp_uint32_t(a, b);
59 }
60 template <>
61 LIBC_INLINE MemcmpReturnType cmp_neq<uint32_t>(CPtr p1, CPtr p2, size_t offset);
62 
63 ///////////////////////////////////////////////////////////////////////////////
64 // Specializations for uint64_t
65 template <> struct cmp_is_expensive<uint64_t> : public cpp::true_type {};
66 template <> LIBC_INLINE bool eq<uint64_t>(CPtr p1, CPtr p2, size_t offset) {
67   return load<uint64_t>(p1, offset) == load<uint64_t>(p2, offset);
68 }
69 template <>
70 LIBC_INLINE uint32_t neq<uint64_t>(CPtr p1, CPtr p2, size_t offset) {
71   return !eq<uint64_t>(p1, p2, offset);
72 }
73 template <>
74 LIBC_INLINE MemcmpReturnType cmp<uint64_t>(CPtr p1, CPtr p2, size_t offset);
75 template <>
76 LIBC_INLINE MemcmpReturnType cmp_neq<uint64_t>(CPtr p1, CPtr p2,
77                                                size_t offset) {
78   const auto a = load_be<uint64_t>(p1, offset);
79   const auto b = load_be<uint64_t>(p2, offset);
80   return cmp_neq_uint64_t(a, b);
81 }
82 
83 } // namespace generic
84 } // namespace LIBC_NAMESPACE_DECL
85 
86 #endif // LIBC_TARGET_ARCH_IS_ANY_RISCV
87 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_OP_RISCV_H
88