[libc-commits] [PATCH] D144506: [libc] Implement ntohl and ntohs

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Feb 21 22:02:11 PST 2023


sivachandra accepted this revision.
sivachandra added inline comments.
This revision is now accepted and ready to land.


================
Comment at: libc/src/network/ntohl.cpp:16
+LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) {
+  return Endian::to_big_endian(netlong);
+}
----------------
rtenneti wrote:
> sivachandra wrote:
> > Should this always convert to big-endian, or should it convert to the host's endianness?
> I thought of implementing it as the following:
> 
> LLVM_LIBC_FUNCTION(uint32_t, ntohl, (uint32_t netlong)) {
>   return Endian::IS_LITTLE ? __builtin_bswap32(netlong) : netlong;
> }
> 
> The above seem to be same as Endian::to_big_endian. I am wondering if it is better to implement the above (to be clearer). What do you think? Thanks
I think this is cleaner. One more suggestion - use a `constexpr` `if` statement like this: 

```
if constexpr (Endian::IS_LITTLE)
  return __builtin_bswap(netlong);
else
  return netlong;
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144506/new/

https://reviews.llvm.org/D144506



More information about the libc-commits mailing list