This is an archive of the discontinued LLVM Phabricator instance.

[libc] Implement ntohl and ntohs
ClosedPublic

Authored by rtenneti on Feb 21 2023, 9:59 AM.

Diff Detail

Event Timeline

rtenneti created this revision.Feb 21 2023, 9:59 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 21 2023, 9:59 AM
rtenneti requested review of this revision.Feb 21 2023, 9:59 AM
sivachandra added inline comments.
libc/src/network/ntohl.cpp
16

Should this always convert to big-endian, or should it convert to the host's endianness?

rtenneti added inline comments.Feb 21 2023, 5:06 PM
libc/src/network/ntohl.cpp
16

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

rtenneti updated this revision to Diff 499333.Feb 21 2023, 5:29 PM

+ Added explicit check for IS_LITTLE to do the swap.
+ Added a test to verify htonl(ntohl(orig)) == orig.

Hi Siva,

Added a htonl(ntohl(...)) test. Did the swap based on host's endianess. PTAL.
sivachandra accepted this revision.Feb 21 2023, 10:02 PM
sivachandra added inline comments.
libc/src/network/ntohl.cpp
16

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;
This revision is now accepted and ready to land.Feb 21 2023, 10:02 PM
rtenneti updated this revision to Diff 499528.Feb 22 2023, 8:51 AM

Fixed comments.

rtenneti marked 2 inline comments as done.Feb 22 2023, 8:52 AM
rtenneti updated this revision to Diff 499544.Feb 22 2023, 9:13 AM

Did a git pull rebase false to merge the latest changes.

Herald added a project: Restricted Project. · View Herald TranscriptFeb 22 2023, 9:13 AM

Not sure what happened here, but the diff is not related to the topic of this change.

rtenneti updated this revision to Diff 499565.Feb 22 2023, 10:02 AM

Ran git restore on llvm files.

Herald added projects: Restricted Project, Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptFeb 22 2023, 10:02 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Herald added a reviewer: Restricted Project. · View Herald Transcript
Herald added a reviewer: Restricted Project. · View Herald Transcript
Herald added subscribers: libcxx-commits, Restricted Project, Enna1. · View Herald Transcript
This revision now requires review to proceed.Feb 22 2023, 10:02 AM
rtenneti updated this revision to Diff 499572.Feb 22 2023, 10:09 AM

Dif a git pull --rebase.

rtenneti updated this revision to Diff 499574.Feb 22 2023, 10:14 AM

Fixed comments.

philnik removed reviewers: Restricted Project, Restricted Project, Restricted Project.Feb 22 2023, 10:15 AM
This revision is now accepted and ready to land.Feb 22 2023, 10:15 AM
rtenneti updated this revision to Diff 499575.Feb 22 2023, 10:20 AM

Fixing Siva's comments.

sivachandra accepted this revision.Feb 22 2023, 10:29 AM
This revision was landed with ongoing or failed builds.Feb 22 2023, 10:40 AM
This revision was automatically updated to reflect the committed changes.
foad removed a subscriber: foad.Feb 23 2023, 2:04 AM