[libc-commits] [libc] d0d6d78 - [libc] Implement ntohl and ntohs
Raman Tenneti via libc-commits
libc-commits at lists.llvm.org
Wed Feb 22 10:40:43 PST 2023
Author: Raman Tenneti
Date: 2023-02-22T10:40:38-08:00
New Revision: d0d6d78bbda7cea3408ab2686f67e38b6fa52c6e
URL: https://github.com/llvm/llvm-project/commit/d0d6d78bbda7cea3408ab2686f67e38b6fa52c6e
DIFF: https://github.com/llvm/llvm-project/commit/d0d6d78bbda7cea3408ab2686f67e38b6fa52c6e.diff
LOG: [libc] Implement ntohl and ntohs
Per spec:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohs.html
Co-authored-by: Jeff Bailey <jbailey at google.com>
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D144506
Added:
libc/src/network/ntohl.cpp
libc/src/network/ntohl.h
libc/src/network/ntohs.cpp
libc/src/network/ntohs.h
libc/test/src/network/ntohl_test.cpp
libc/test/src/network/ntohs_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/spec/posix.td
libc/src/network/CMakeLists.txt
libc/src/network/htons.h
libc/test/src/network/CMakeLists.txt
libc/test/src/network/htonl_test.cpp
libc/test/src/network/htons_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 82de499789577..6865133826cb3 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -321,6 +321,8 @@ if(LLVM_LIBC_FULL_BUILD)
# network.h entrypoints
libc.src.network.htonl
libc.src.network.htons
+ libc.src.network.ntohl
+ libc.src.network.ntohs
# pthread.h entrypoints
libc.src.pthread.pthread_atfork
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6d5aba734c709..c90ec052b459b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -334,6 +334,8 @@ if(LLVM_LIBC_FULL_BUILD)
# network.h entrypoints
libc.src.network.htonl
libc.src.network.htons
+ libc.src.network.ntohl
+ libc.src.network.ntohs
# pthread.h entrypoints
libc.src.pthread.pthread_atfork
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index a7e50ca107f2f..fc3fe421ca919 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -814,6 +814,16 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<UInt16Type>,
[ArgSpec<UInt16Type>]
>,
+ FunctionSpec<
+ "ntohl",
+ RetValSpec<UInt32Type>,
+ [ArgSpec<UInt32Type>]
+ >,
+ FunctionSpec<
+ "ntohs",
+ RetValSpec<UInt16Type>,
+ [ArgSpec<UInt16Type>]
+ >,
]
>;
diff --git a/libc/src/network/CMakeLists.txt b/libc/src/network/CMakeLists.txt
index ea5101ecbaa9b..cdd53e9150348 100644
--- a/libc/src/network/CMakeLists.txt
+++ b/libc/src/network/CMakeLists.txt
@@ -19,3 +19,25 @@ add_entrypoint_object(
libc.include.arpa_inet
libc.src.__support.common
)
+
+add_entrypoint_object(
+ ntohl
+ SRCS
+ ntohl.cpp
+ HDRS
+ ntohl.h
+ DEPENDS
+ libc.include.arpa_inet
+ libc.src.__support.common
+)
+
+add_entrypoint_object(
+ ntohs
+ SRCS
+ ntohs.cpp
+ HDRS
+ ntohs.h
+ DEPENDS
+ libc.include.arpa_inet
+ libc.src.__support.common
+)
diff --git a/libc/src/network/htons.h b/libc/src/network/htons.h
index 7b61006ffb113..a3abaf6ec6df7 100644
--- a/libc/src/network/htons.h
+++ b/libc/src/network/htons.h
@@ -13,7 +13,7 @@
namespace __llvm_libc {
-uint16_t htons(uint16_t hostlong);
+uint16_t htons(uint16_t hostshort);
} // namespace __llvm_libc
diff --git a/libc/src/network/ntohl.cpp b/libc/src/network/ntohl.cpp
new file mode 100644
index 0000000000000..c525f722ec5b3
--- /dev/null
+++ b/libc/src/network/ntohl.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of ntohl function ----------------------------------===//
+//
+// 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/network/ntohl.h"
+#include "src/__support/common.h"
+#include "src/__support/endian.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) {
+ if constexpr (Endian::IS_LITTLE)
+ return __builtin_bswap32(netlong);
+ else
+ return netlong;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/network/ntohl.h b/libc/src/network/ntohl.h
new file mode 100644
index 0000000000000..bec626f5e4cec
--- /dev/null
+++ b/libc/src/network/ntohl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of ntohl --------------------------*- 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_NETWORK_NTOHL_H
+#define LLVM_LIBC_SRC_NETWORK_NTOHL_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+uint32_t ntohl(uint32_t netlong);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_NETWORK_NTOHL_H
diff --git a/libc/src/network/ntohs.cpp b/libc/src/network/ntohs.cpp
new file mode 100644
index 0000000000000..8556b3e28cdc3
--- /dev/null
+++ b/libc/src/network/ntohs.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of ntohs function ----------------------------------===//
+//
+// 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/network/ntohs.h"
+#include "src/__support/common.h"
+#include "src/__support/endian.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(uint16_t, ntohs, (uint16_t netshort)) {
+ if constexpr (Endian::IS_LITTLE)
+ return __builtin_bswap16(netshort);
+ else
+ return netshort;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/network/ntohs.h b/libc/src/network/ntohs.h
new file mode 100644
index 0000000000000..b10aaa9c8891b
--- /dev/null
+++ b/libc/src/network/ntohs.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of ntohs --------------------------*- 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_NETWORK_NTOHS_H
+#define LLVM_LIBC_SRC_NETWORK_NTOHS_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+uint16_t ntohs(uint16_t netshort);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_NETWORK_NTOHS_H
diff --git a/libc/test/src/network/CMakeLists.txt b/libc/test/src/network/CMakeLists.txt
index ed75dc4e77d72..7bff6da77ed09 100644
--- a/libc/test/src/network/CMakeLists.txt
+++ b/libc/test/src/network/CMakeLists.txt
@@ -10,6 +10,7 @@ add_libc_unittest(
20
DEPENDS
libc.src.network.htonl
+ libc.src.network.ntohl
)
add_libc_unittest(
@@ -22,4 +23,31 @@ add_libc_unittest(
20
DEPENDS
libc.src.network.htons
+ libc.src.network.ntohs
+)
+
+add_libc_unittest(
+ ntohl
+ SUITE
+ libc_network_unittests
+ SRCS
+ ntohl_test.cpp
+ CXX_STANDARD
+ 20
+ DEPENDS
+ libc.src.network.htonl
+ libc.src.network.ntohl
+)
+
+add_libc_unittest(
+ ntohs
+ SUITE
+ libc_network_unittests
+ SRCS
+ ntohs_test.cpp
+ CXX_STANDARD
+ 20
+ DEPENDS
+ libc.src.network.htons
+ libc.src.network.ntohs
)
diff --git a/libc/test/src/network/htonl_test.cpp b/libc/test/src/network/htonl_test.cpp
index 66ffc8b17232d..a8363d20d9767 100644
--- a/libc/test/src/network/htonl_test.cpp
+++ b/libc/test/src/network/htonl_test.cpp
@@ -8,6 +8,7 @@
#include "src/__support/endian.h"
#include "src/network/htonl.h"
+#include "src/network/ntohl.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcHtonl, SmokeTest) {
@@ -20,3 +21,8 @@ TEST(LlvmLibcHtonl, SmokeTest) {
EXPECT_EQ(__llvm_libc::htonl(original), original);
#endif
}
+
+TEST(LlvmLibcHtonl, CompleteTest) {
+ uint32_t original = 0x01234567;
+ EXPECT_EQ(__llvm_libc::htonl(__llvm_libc::ntohl(original)), original);
+}
diff --git a/libc/test/src/network/htons_test.cpp b/libc/test/src/network/htons_test.cpp
index 786f488c98a29..f9bd77bad25d8 100644
--- a/libc/test/src/network/htons_test.cpp
+++ b/libc/test/src/network/htons_test.cpp
@@ -8,6 +8,7 @@
#include "src/__support/endian.h"
#include "src/network/htons.h"
+#include "src/network/ntohs.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcHtons, SmokeTest) {
@@ -20,3 +21,8 @@ TEST(LlvmLibcHtons, SmokeTest) {
EXPECT_EQ(__llvm_libc::htons(original), original);
#endif
}
+
+TEST(LlvmLibcHtons, CompleteTest) {
+ uint16_t original = 0x0123;
+ EXPECT_EQ(__llvm_libc::htons(__llvm_libc::ntohs(original)), original);
+}
diff --git a/libc/test/src/network/ntohl_test.cpp b/libc/test/src/network/ntohl_test.cpp
new file mode 100644
index 0000000000000..bc52a97c87672
--- /dev/null
+++ b/libc/test/src/network/ntohl_test.cpp
@@ -0,0 +1,28 @@
+//===-- Unittests for ntohl -----------------------------------------------===//
+//
+// 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/__support/endian.h"
+#include "src/network/htonl.h"
+#include "src/network/ntohl.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcNtohl, SmokeTest) {
+ uint32_t original = 0x67452301;
+ uint32_t swapped = 0x01234567;
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ EXPECT_EQ(__llvm_libc::ntohl(original), swapped);
+#endif
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ EXPECT_EQ(__llvm_libc::ntohl(original), original);
+#endif
+}
+
+TEST(LlvmLibcNtohl, CompleteTest) {
+ uint32_t original = 0x01234567;
+ EXPECT_EQ(__llvm_libc::ntohl(__llvm_libc::htonl(original)), original);
+}
diff --git a/libc/test/src/network/ntohs_test.cpp b/libc/test/src/network/ntohs_test.cpp
new file mode 100644
index 0000000000000..f10c246553932
--- /dev/null
+++ b/libc/test/src/network/ntohs_test.cpp
@@ -0,0 +1,28 @@
+//===-- Unittests for ntohs -----------------------------------------------===//
+//
+// 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/__support/endian.h"
+#include "src/network/htons.h"
+#include "src/network/ntohs.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcNtohs, SmokeTest) {
+ uint16_t original = 0x2301;
+ uint16_t swapped = 0x0123;
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ EXPECT_EQ(__llvm_libc::ntohs(original), swapped);
+#endif
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ EXPECT_EQ(__llvm_libc::ntohs(original), original);
+#endif
+}
+
+TEST(LlvmLibcNtohs, CompleteTest) {
+ uint16_t original = 0x0123;
+ EXPECT_EQ(__llvm_libc::ntohs(__llvm_libc::htons(original)), original);
+}
More information about the libc-commits
mailing list