Skip to content

Commit

Permalink
[libc] Add swab implementation
Browse files Browse the repository at this point in the history
Swab implementation is added to libc/src/unistd.

Differential Revision: https://reviews.llvm.org/D147970
  • Loading branch information
Caslyn committed Apr 10, 2023
1 parent 420526f commit 1a3e760
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
7 changes: 7 additions & 0 deletions libc/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions libc/src/unistd/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions libc/src/unistd/linux/swab.cpp
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions libc/src/unistd/swab.h
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 83 additions & 0 deletions libc/test/src/unistd/swab_test.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 1a3e760

Please sign in to comment.