[libc-commits] [libc] 1a3e760 - [libc] Add swab implementation
Caslyn Tonelli via libc-commits
libc-commits at lists.llvm.org
Mon Apr 10 16:38:00 PDT 2023
Author: Caslyn Tonelli
Date: 2023-04-10T23:37:51Z
New Revision: 1a3e760eda9713d806ae55688ce3df35ef57ab1e
URL: https://github.com/llvm/llvm-project/commit/1a3e760eda9713d806ae55688ce3df35ef57ab1e
DIFF: https://github.com/llvm/llvm-project/commit/1a3e760eda9713d806ae55688ce3df35ef57ab1e.diff
LOG: [libc] Add swab implementation
Swab implementation is added to libc/src/unistd.
Differential Revision: https://reviews.llvm.org/D147970
Added:
libc/src/unistd/linux/swab.cpp
libc/src/unistd/swab.h
libc/test/src/unistd/swab_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/spec/posix.td
libc/src/unistd/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/test/src/unistd/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 7de897fa3cc5c..6fb32065856d4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -187,6 +187,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.readlink
libc.src.unistd.readlinkat
libc.src.unistd.rmdir
+ libc.src.unistd.swab
libc.src.unistd.symlink
libc.src.unistd.symlinkat
libc.src.unistd.sysconf
diff --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt
index cd41baeaf9dd0..0c59d91cc0c50 100644
--- a/libc/config/linux/riscv64/entrypoints.txt
+++ b/libc/config/linux/riscv64/entrypoints.txt
@@ -189,6 +189,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.readlink
libc.src.unistd.readlinkat
libc.src.unistd.rmdir
+ libc.src.unistd.swab
libc.src.unistd.symlink
libc.src.unistd.symlinkat
libc.src.unistd.sysconf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0b024b1fd486a..13c788df6400e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -191,6 +191,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.readlink
libc.src.unistd.readlinkat
libc.src.unistd.rmdir
+ libc.src.unistd.swab
libc.src.unistd.symlink
libc.src.unistd.symlinkat
libc.src.unistd.sysconf
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index fc3fe421ca919..ccbba46cc2149 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -544,6 +544,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>]
>,
+ FunctionSpec<
+ "swab",
+ RetValSpec<VoidType>,
+ [ArgSpec<ConstVoidRestrictedPtr>, ArgSpec<VoidPtr>, ArgSpec<SSizeTType>]
+ >,
FunctionSpec<
"symlink",
RetValSpec<IntType>,
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index ab05175c25b77..261e16ef8b484 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -191,6 +191,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.rmdir
)
+add_entrypoint_object(
+ swab
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.swab
+)
+
add_entrypoint_object(
symlink
ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 2507e1e9a9387..8ca5d59e6739f 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -353,6 +353,14 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ swab
+ SRCS
+ swab.cpp
+ HDRS
+ ../swab.h
+)
+
add_entrypoint_object(
symlink
SRCS
diff --git a/libc/src/unistd/linux/swab.cpp b/libc/src/unistd/linux/swab.cpp
new file mode 100644
index 0000000000000..44166a623ff42
--- /dev/null
+++ b/libc/src/unistd/linux/swab.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of swab --------------------------------------------===//
+//
+// 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/unistd/swab.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(void, swab,
+ (const void *__restrict from, void *__restrict to,
+ ssize_t n)) {
+ const unsigned char *f = static_cast<const unsigned char *>(from);
+ unsigned char *t = static_cast<unsigned char *>(to);
+ for (ssize_t i = 1; i < n; i += 2) {
+ t[i - 1] = f[i];
+ t[i] = f[i - 1];
+ }
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/unistd/swab.h b/libc/src/unistd/swab.h
new file mode 100644
index 0000000000000..e5e02e1333147
--- /dev/null
+++ b/libc/src/unistd/swab.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for swab --------------------------*- 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_UNISTD_SWAB_H
+#define LLVM_LIBC_SRC_UNISTD_SWAB_H
+
+#include <unistd.h> // For ssize_t
+
+namespace __llvm_libc {
+
+void swab(const void *__restrict from, void *__restrict to, ssize_t n);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_UNISTD_SWAB_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index ea19aba0943ea..1ec29d36c65f9 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -225,6 +225,16 @@ add_libc_unittest(
libc.src.unistd.rmdir
)
+add_libc_unittest(
+ swab_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ swab_test.cpp
+ DEPENDS
+ libc.src.unistd.swab
+)
+
add_libc_unittest(
readlink_test
SUITE
diff --git a/libc/test/src/unistd/swab_test.cpp b/libc/test/src/unistd/swab_test.cpp
new file mode 100644
index 0000000000000..9b94924ee3cf1
--- /dev/null
+++ b/libc/test/src/unistd/swab_test.cpp
@@ -0,0 +1,83 @@
+
+//===-- Unittests for swab ------------------------------------------------===//
+//
+// 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/unistd/swab.h"
+
+#include "src/string/string_utils.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSwabTest, NegativeSizeIsNoOp) {
+ const char *from = "abc";
+ char to[4] = {'x', 'y', 'z', '\0'};
+ __llvm_libc::swab(from, to, -1);
+ ASSERT_STREQ(to, "xyz");
+}
+
+TEST(LlvmLibcSwabTest, ZeroSizeIsNoOp) {
+ const char *from = "abc";
+ char to[4] = {'x', 'y', 'z', '\0'};
+ __llvm_libc::swab(from, to, 0);
+ ASSERT_STREQ(to, "xyz");
+}
+
+TEST(LlvmLibcSwabTest, SingleByteIsNoOp) {
+ char from[] = {'a'};
+ char to[4] = {'x', 'y', 'z', '\0'};
+ __llvm_libc::swab(from, to, sizeof(from));
+ ASSERT_STREQ(to, "xyz");
+}
+
+TEST(LlvmLibcSwabTest, NullPtrsAreNotDeRefedIfNIsLessThanTwo) {
+ // This test passes if a crash does not happen
+ __llvm_libc::swab(nullptr, nullptr, -1);
+ __llvm_libc::swab(nullptr, nullptr, 0);
+ __llvm_libc::swab(nullptr, nullptr, 1);
+}
+
+TEST(LlvmLibcSwabTest, BytesAreSwappedWithEvenN) {
+ {
+ const char *from = "ab";
+ char to[3] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "ba");
+ }
+ {
+ const char *from = "abcd";
+ char to[5] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "badc");
+ }
+ {
+ const char *from = "aAaAaA";
+ char to[7] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "AaAaAa");
+ }
+}
+
+TEST(LlvmLibcSwabTest, LastByteIgnoredWithOddN) {
+ {
+ const char *from = "aba";
+ char to[3] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "ba");
+ }
+ {
+ const char *from = "abcde";
+ char to[5] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "badc");
+ }
+ {
+ const char *from = "aAaAaAx";
+ char to[7] = {};
+ __llvm_libc::swab(from, to, __llvm_libc::internal::string_length(from));
+ ASSERT_STREQ(to, "AaAaAa");
+ }
+}
More information about the libc-commits
mailing list