-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swab implementation is added to libc/src/unistd. Differential Revision: https://reviews.llvm.org/D147970
- Loading branch information
Showing
10 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |