c++linuxgccglibcgcc4.4

How are the ntoh functions implemented under RHEL/GCC?


A production issue has led our team to the following questions:

  1. Under RHEL6 using GCC 4.4.6, how are ntohs and ntohl implemented?
  2. Are the implementations known to be fast or slow?
  3. How can I actually see the generated assembly code for the functions?

I know the implications behind questions may seem far-fetched and ridiculous, but I have been asked to investigate.

The hardware in question is an Intel box, little endian, 64-bit processor and compiled in 64 bit.


Solution

    1. They're provided by glibc, not GCC, look in /usr/include/bits/byteswap.h for the __bswap_16 and __bswap_32 functions, which are used when optimization is enabled (see <netinet/in.h> for details of how.)
    2. You didn't say what architecture you're using, on a big-endian system they're no-ops, so optimally fast! On little-endian they're architecture-specific hand-optimized assembly code.
    3. Use GCC's -save-temps option to keep the intermediate .s files, or use -S to stop after compilation and before assembling the code, or use http://gcc.godbolt.org/