[libc-commits] [libc] d9f14ca - [libc] Add Endian::from_{big, little}_endian (#203895)

via libc-commits libc-commits at lists.llvm.org
Mon Jun 15 06:46:51 PDT 2026


Author: Pavel Labath
Date: 2026-06-15T15:46:47+02:00
New Revision: d9f14cafbbe7e8a4e34a48128539c15099f28563

URL: https://github.com/llvm/llvm-project/commit/d9f14cafbbe7e8a4e34a48128539c15099f28563
DIFF: https://github.com/llvm/llvm-project/commit/d9f14cafbbe7e8a4e34a48128539c15099f28563.diff

LOG: [libc] Add Endian::from_{big,little}_endian (#203895)

This is actually the same function as its to_ counterpart, but it lets
us correctly express the intent.

The functions are already useful for implementing ntoh?, but I'll add
other uses of them soon.

Added: 
    

Modified: 
    libc/src/__support/endian_internal.h
    libc/src/arpa/inet/ntohl.cpp
    libc/src/arpa/inet/ntohs.cpp
    libc/test/src/__support/endian_internal_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/endian_internal.h b/libc/src/__support/endian_internal.h
index 8d81329fd243d..0173cb5748b58 100644
--- a/libc/src/__support/endian_internal.h
+++ b/libc/src/__support/endian_internal.h
@@ -41,6 +41,16 @@ template <unsigned ORDER> struct Endian {
   static constexpr const bool IS_BIG = ORDER == __ORDER_BIG_ENDIAN__;
   template <typename T> LIBC_INLINE static T to_big_endian(T value);
   template <typename T> LIBC_INLINE static T to_little_endian(T value);
+
+  // Converting "to" and "from" a given endianness is actually the same
+  // operation, but we provide dedicated functions to let the user express their
+  // intent clearly.
+  template <typename T> LIBC_INLINE static T from_big_endian(T value) {
+    return to_big_endian(value);
+  }
+  template <typename T> LIBC_INLINE static T from_little_endian(T value) {
+    return to_little_endian(value);
+  }
 };
 
 // Little Endian specializations

diff  --git a/libc/src/arpa/inet/ntohl.cpp b/libc/src/arpa/inet/ntohl.cpp
index d472107a1988c..255c24dcff81a 100644
--- a/libc/src/arpa/inet/ntohl.cpp
+++ b/libc/src/arpa/inet/ntohl.cpp
@@ -14,10 +14,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) {
-  if constexpr (Endian::IS_LITTLE)
-    return __builtin_bswap32(netlong);
-  else
-    return netlong;
+  return Endian::from_big_endian(netlong);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/arpa/inet/ntohs.cpp b/libc/src/arpa/inet/ntohs.cpp
index 9082ed928778e..79c9fb9e436da 100644
--- a/libc/src/arpa/inet/ntohs.cpp
+++ b/libc/src/arpa/inet/ntohs.cpp
@@ -14,10 +14,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(uint16_t, ntohs, (uint16_t netshort)) {
-  if constexpr (Endian::IS_LITTLE)
-    return __builtin_bswap16(netshort);
-  else
-    return netshort;
+  return Endian::from_big_endian(netshort);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/test/src/__support/endian_internal_test.cpp b/libc/test/src/__support/endian_internal_test.cpp
index eab0f376e7495..a8a1df2262012 100644
--- a/libc/test/src/__support/endian_internal_test.cpp
+++ b/libc/test/src/__support/endian_internal_test.cpp
@@ -16,11 +16,15 @@ struct LlvmLibcEndian : testing::Test {
   template <typename T> void check(const T original, const T swapped) {
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
     EXPECT_EQ(Endian::to_little_endian(original), original);
+    EXPECT_EQ(Endian::from_little_endian(original), original);
     EXPECT_EQ(Endian::to_big_endian(original), swapped);
+    EXPECT_EQ(Endian::from_big_endian(swapped), original);
 #endif
 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
     EXPECT_EQ(Endian::to_big_endian(original), original);
+    EXPECT_EQ(Endian::from_big_endian(original), original);
     EXPECT_EQ(Endian::to_little_endian(original), swapped);
+    EXPECT_EQ(Endian::from_little_endian(swapped), original);
 #endif
   }
 };


        


More information about the libc-commits mailing list