summaryrefslogtreecommitdiff
path: root/libc/src/strings
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src/strings')
-rw-r--r--libc/src/strings/CMakeLists.txt97
-rw-r--r--libc/src/strings/bcmp.cpp21
-rw-r--r--libc/src/strings/bcmp.h21
-rw-r--r--libc/src/strings/bcopy.cpp20
-rw-r--r--libc/src/strings/bcopy.h21
-rw-r--r--libc/src/strings/bzero.cpp20
-rw-r--r--libc/src/strings/bzero.h21
-rw-r--r--libc/src/strings/index.cpp21
-rw-r--r--libc/src/strings/index.h20
-rw-r--r--libc/src/strings/rindex.cpp21
-rw-r--r--libc/src/strings/rindex.h20
-rw-r--r--libc/src/strings/strcasecmp.cpp26
-rw-r--r--libc/src/strings/strcasecmp.h20
-rw-r--r--libc/src/strings/strncasecmp.cpp27
-rw-r--r--libc/src/strings/strncasecmp.h21
15 files changed, 397 insertions, 0 deletions
diff --git a/libc/src/strings/CMakeLists.txt b/libc/src/strings/CMakeLists.txt
new file mode 100644
index 000000000000..5e84c7be1f7d
--- /dev/null
+++ b/libc/src/strings/CMakeLists.txt
@@ -0,0 +1,97 @@
+function(add_bcmp bcmp_name)
+ add_implementation(bcmp ${bcmp_name}
+ SRCS ${LIBC_SOURCE_DIR}/src/strings/bcmp.cpp
+ HDRS ${LIBC_SOURCE_DIR}/src/strings/bcmp.h
+ DEPENDS
+ libc.src.string.memory_utils.memory_utils
+ ${ARGN}
+ )
+endfunction()
+
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
+ add_bcmp(bcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
+ add_bcmp(bcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
+ add_bcmp(bcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
+ add_bcmp(bcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
+ add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bcmp(bcmp)
+elseif(LIBC_TARGET_OS_IS_GPU)
+ add_bcmp(bcmp)
+else()
+ add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bcmp(bcmp)
+endif()
+
+function(add_bzero bzero_name)
+ add_implementation(bzero ${bzero_name}
+ SRCS ${LIBC_SOURCE_DIR}/src/strings/bzero.cpp
+ HDRS ${LIBC_SOURCE_DIR}/src/strings/bzero.h
+ DEPENDS
+ libc.src.string.memory_utils.inline_memset
+ ${ARGN}
+ )
+endfunction()
+
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
+ add_bzero(bzero_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
+ add_bzero(bzero_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
+ add_bzero(bzero_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
+ add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
+ add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bzero(bzero)
+elseif(LIBC_TARGET_OS_IS_GPU)
+ add_bzero(bzero)
+else()
+ add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bzero(bzero)
+endif()
+
+add_entrypoint_object(
+ bcopy
+ SRCS
+ bcopy.cpp
+ HDRS
+ bcopy.h
+)
+
+add_entrypoint_object(
+ index
+ SRCS
+ index.cpp
+ HDRS
+ index.h
+ DEPENDS
+ libc.src.string.string_utils
+)
+
+add_entrypoint_object(
+ rindex
+ SRCS
+ rindex.cpp
+ HDRS
+ rindex.h
+ DEPENDS
+ libc.src.string.string_utils
+)
+
+add_entrypoint_object(
+ strcasecmp
+ SRCS
+ strcasecmp.cpp
+ HDRS
+ strcasecmp.h
+ DEPENDS
+ libc.src.__support.ctype_utils
+ libc.src.string.memory_utils.inline_strcmp
+)
+
+add_entrypoint_object(
+ strncasecmp
+ SRCS
+ strncasecmp.cpp
+ HDRS
+ strncasecmp.h
+ DEPENDS
+ libc.src.__support.ctype_utils
+ libc.src.string.memory_utils.inline_strcmp
+)
diff --git a/libc/src/strings/bcmp.cpp b/libc/src/strings/bcmp.cpp
new file mode 100644
index 000000000000..083f13d3f95d
--- /dev/null
+++ b/libc/src/strings/bcmp.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of bcmp --------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/bcmp.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_bcmp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, bcmp,
+ (const void *lhs, const void *rhs, size_t count)) {
+ return inline_bcmp(lhs, rhs, count);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/bcmp.h b/libc/src/strings/bcmp.h
new file mode 100644
index 000000000000..46b4d7163b52
--- /dev/null
+++ b/libc/src/strings/bcmp.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for bzero -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_BCMP_H
+#define LLVM_LIBC_SRC_STRINGS_BCMP_H
+
+#include "src/__support/macros/config.h"
+#include <stddef.h> // size_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+int bcmp(const void *lhs, const void *rhs, size_t count);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_BCMP_H
diff --git a/libc/src/strings/bcopy.cpp b/libc/src/strings/bcopy.cpp
new file mode 100644
index 000000000000..eb0358c18747
--- /dev/null
+++ b/libc/src/strings/bcopy.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of bcopy -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/bcopy.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memmove.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, bcopy, (const void *src, void *dst, size_t count)) {
+ return inline_memmove(dst, src, count);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/bcopy.h b/libc/src/strings/bcopy.h
new file mode 100644
index 000000000000..d8ee77e83c6d
--- /dev/null
+++ b/libc/src/strings/bcopy.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for bcopy -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_BCOPY_H
+#define LLVM_LIBC_SRC_STRINGS_BCOPY_H
+
+#include "src/__support/macros/config.h"
+#include <stddef.h> // size_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+void bcopy(const void *src, void *dest, size_t count);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_BCOPY_H
diff --git a/libc/src/strings/bzero.cpp b/libc/src/strings/bzero.cpp
new file mode 100644
index 000000000000..9c2e9700442a
--- /dev/null
+++ b/libc/src/strings/bzero.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of bzero -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/bzero.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_bzero.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, bzero, (void *ptr, size_t count)) {
+ inline_bzero(reinterpret_cast<char *>(ptr), count);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/bzero.h b/libc/src/strings/bzero.h
new file mode 100644
index 000000000000..3e270fe41f6c
--- /dev/null
+++ b/libc/src/strings/bzero.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for bzero -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_BZERO_H
+#define LLVM_LIBC_SRC_STRINGS_BZERO_H
+
+#include "src/__support/macros/config.h"
+#include <stddef.h> // size_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+void bzero(void *ptr, size_t count);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_BZERO_H
diff --git a/libc/src/strings/index.cpp b/libc/src/strings/index.cpp
new file mode 100644
index 000000000000..33b2be9f33e0
--- /dev/null
+++ b/libc/src/strings/index.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of index -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/index.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(char *, index, (const char *src, int c)) {
+ return internal::strchr_implementation(src, c);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/index.h b/libc/src/strings/index.h
new file mode 100644
index 000000000000..d382cdd249aa
--- /dev/null
+++ b/libc/src/strings/index.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for index -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_INDEX_H
+#define LLVM_LIBC_SRC_STRINGS_INDEX_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+char *index(const char *src, int c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_INDEX_H
diff --git a/libc/src/strings/rindex.cpp b/libc/src/strings/rindex.cpp
new file mode 100644
index 000000000000..1242e0faf85f
--- /dev/null
+++ b/libc/src/strings/rindex.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of rindex ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/rindex.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/string/string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(char *, rindex, (const char *src, int c)) {
+ return internal::strrchr_implementation(src, c);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/rindex.h b/libc/src/strings/rindex.h
new file mode 100644
index 000000000000..f8aa7b9be28f
--- /dev/null
+++ b/libc/src/strings/rindex.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for rindex ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_RINDEX_H
+#define LLVM_LIBC_SRC_STRINGS_RINDEX_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+char *rindex(const char *src, int c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_RINDEX_H
diff --git a/libc/src/strings/strcasecmp.cpp b/libc/src/strings/strcasecmp.cpp
new file mode 100644
index 000000000000..4bbe2909df1e
--- /dev/null
+++ b/libc/src/strings/strcasecmp.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of strcasecmp --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/strcasecmp.h"
+
+#include "src/__support/common.h"
+#include "src/__support/ctype_utils.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_strcmp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, strcasecmp, (const char *left, const char *right)) {
+ auto case_cmp = [](char a, char b) {
+ return LIBC_NAMESPACE::internal::tolower(a) -
+ LIBC_NAMESPACE::internal::tolower(b);
+ };
+ return inline_strcmp(left, right, case_cmp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/strcasecmp.h b/libc/src/strings/strcasecmp.h
new file mode 100644
index 000000000000..b02fb3161587
--- /dev/null
+++ b/libc/src/strings/strcasecmp.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for strcasecmp --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
+#define LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int strcasecmp(const char *left, const char *right);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
diff --git a/libc/src/strings/strncasecmp.cpp b/libc/src/strings/strncasecmp.cpp
new file mode 100644
index 000000000000..9c2f0ab13126
--- /dev/null
+++ b/libc/src/strings/strncasecmp.cpp
@@ -0,0 +1,27 @@
+//===-- Implementation of strncasecmp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/strings/strncasecmp.h"
+
+#include "src/__support/common.h"
+#include "src/__support/ctype_utils.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_strcmp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, strncasecmp,
+ (const char *left, const char *right, size_t n)) {
+ auto case_cmp = [](char a, char b) {
+ return LIBC_NAMESPACE::internal::tolower(a) -
+ LIBC_NAMESPACE::internal::tolower(b);
+ };
+ return inline_strncmp(left, right, n, case_cmp);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/strings/strncasecmp.h b/libc/src/strings/strncasecmp.h
new file mode 100644
index 000000000000..59df51705891
--- /dev/null
+++ b/libc/src/strings/strncasecmp.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for strcasecmp --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H
+#define LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H
+
+#include "src/__support/macros/config.h"
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int strncasecmp(const char *left, const char *right, size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H