diff --git a/libc/src/network/ntohl.cpp b/libc/src/network/ntohl.cpp --- a/libc/src/network/ntohl.cpp +++ b/libc/src/network/ntohl.cpp @@ -13,7 +13,7 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) { - return Endian::to_big_endian(netlong); + return Endian::IS_LITTLE ? __builtin_bswap32(netlong) : netlong; } } // namespace __llvm_libc diff --git a/libc/src/network/ntohs.cpp b/libc/src/network/ntohs.cpp --- a/libc/src/network/ntohs.cpp +++ b/libc/src/network/ntohs.cpp @@ -13,7 +13,7 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(uint16_t, ntohs, (uint16_t netshort)) { - return Endian::to_big_endian(netshort); + return Endian::IS_LITTLE ? __builtin_bswap16(netshort) : netshort; } } // namespace __llvm_libc diff --git a/libc/test/src/network/CMakeLists.txt b/libc/test/src/network/CMakeLists.txt --- a/libc/test/src/network/CMakeLists.txt +++ b/libc/test/src/network/CMakeLists.txt @@ -10,6 +10,7 @@ 20 DEPENDS libc.src.network.htonl + libc.src.network.ntohl ) add_libc_unittest( @@ -22,6 +23,7 @@ 20 DEPENDS libc.src.network.htons + libc.src.network.ntohs ) add_libc_unittest( @@ -33,6 +35,7 @@ CXX_STANDARD 20 DEPENDS + libc.src.network.htonl libc.src.network.ntohl ) @@ -45,5 +48,6 @@ 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 --- 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 @@ 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 --- 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 @@ 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 --- a/libc/test/src/network/ntohl_test.cpp +++ b/libc/test/src/network/ntohl_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/__support/endian.h" +#include "src/network/htonl.h" #include "src/network/ntohl.h" #include "test/UnitTest/Test.h" @@ -20,3 +21,8 @@ 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 --- a/libc/test/src/network/ntohs_test.cpp +++ b/libc/test/src/network/ntohs_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/__support/endian.h" +#include "src/network/htons.h" #include "src/network/ntohs.h" #include "test/UnitTest/Test.h" @@ -20,3 +21,8 @@ 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); +}