Austin Group Defect Tracker

Aardvark Mark III


Viewing Issue Simple Details Jump to Notes ] Issue History ] Print ]
ID Category Severity Type Date Submitted Last Update
0000162 [1003.1(2008)/Issue 7] System Interfaces Comment Enhancement Request 2009-10-07 07:07 2010-01-21 16:12
Reporter Konrad_Schwarz View Status public  
Assigned To ajosey
Priority normal Resolution Open  
Status Under Review  
Name Konrad Schwarz
Organization Siemens AG
User Reference ntohs_endianess
Section ntohs
Page Number
Line Number
Interp Status ---
Final Accepted Text
Summary 0000162: Determining System Endianess during Preprocessing
Description Sometimes one wants to compile conditionally depending on the endianess of the target platform. I am unaware of a portable (standard-based) way of determining endianess at preprocessing time.

However, a minor change to the definition of ntohs() and related functions would achieve this. The man page currently reads:

"On some implementations, these functions are defined as macros."

The BSD document "An Advanced 4.4BSD Interprocess Communication Tutorial" (http://docs.freebsd.org/44doc/psd/21.ipc/paper.pdf), [^] which introduced the ntohs() family of functions, states on page 15,

"On machines where unneeded these routines are defined as null macros."

I would like to have POSIX strengthened so that ntohs() etc. are macros exactly when the network byte order and host byte order are identical; that is, when the host is big-endian. The test for big-endianess could then be implemented as

# ifdef ntohs
/* big-endian specific code */
# else
/* non-big-endian, presumably little endian */
# endif

I realize that some machines, such as the PDP-11, are neither little nor big endian. However, such machines, similar to non-octet byte machines, are so rare that POSIX should not have to cater to them.

Note that it is not possible to implement ntohs() etc. on a little-endian machine using only a macro, as the byte swap must evaluate its argument several times. In standard C, a function is required.
Desired Action Change the sentence

"On some implementations, these functions are defined as macros."

to

"On exactly those implementations where network and host byte order are identical these functions are defined as macros."
Tags c99
Attached Files

- Relationships

-  Notes
(0000248)
eblake (reporter)
2009-10-07 12:19

"Note that it is not possible to implement ntohs() etc. on a little-endian machine using only a macro" - not true. Yes, it is not possible to do so using strictly C99 constructs, but an implementation is free to use extensions as part of implementing a macro, and many implementations DO use a macro on little-endian machines that outputs an assembly directive for a byte swap while evaluating the argument exactly once. For example, on one little-endian system I sampled:

# define ntohs(x) \
(__builtin_constant_p((short)(x)) ? \
 __constant_ntohs((x)) : \
 __ntohs((x)))

where the semantics of __builtin_constant_p are such that side effects of x are not evaluated.

If we are to support endianness detection, it should by standardizing existing implementations, which provide a new header <endian.h> that defines either LITTLE_ENDIAN or BIG_ENDIAN as appropriate.
(0000250)
nick (manager)
2009-10-08 16:29

Forward this to the C99 committee for their consideration.
(0000279)
nick (manager)
2009-11-05 16:19

C99 is considering this, but have so far shown little enthusiasm. No formal proposal to add this has been made to the committee. Next April is the final chance to do so.
(0000312)
jpoehlmann (reporter)
2009-12-02 15:11

If this clause of the Standard would be fullfilled by most Unices, we could already base on it:

   The BSD document "An Advanced 4.4BSD Interprocess Communication
   Tutorial" (http://docs.freebsd.org/44doc/psd/21.ipc/paper.pdf), [^] [^] which
   introduced the ntohs() family of functions, states on page 15,
 
   "On machines where unneeded these routines are defined as null macros."

#ifdef ntohs
  #if ( ntohs ( 0x1234 ) == 0x1234 ) )
    #define BIGENDIAN_FOUND_AT_COMPILETIME = 1
  #endif
#else
  #define ENDIANNESS_UNKNONW_AT_COMPILETIME
#endif

This would also work in the case that ntohs is a macro
on a little endian platform.

Unfortunately on Linux (tested ppc and Intel) ntohs seems not to be a macro
so this test will mostly fail.

It may be preferable to standardize the __BYTE_ORDER, __BIG_ENDIAN and __LITTLE_ENDIAN macros found in glibc and some other UNICes.
(0000372)
Don Cragun (manager)
2010-01-07 17:06

This was discussed during today's conference call.
We do not believe that ntohs() should be required to be implemented in any particular way.

However, if the C Committee does not provide a way to determine endianness in the next revision of the C Standard, we will consider adding a new header and macros to specify implementation endianness for integral and floating point values.
(0000377)
drepper (reporter)
2010-01-21 16:12

The C committee hasn't shown enthusiasm to introduce macros for endianess handling. They are not opposed but somebody needs to do the work and preferably before the April C committee meeting.

If any of the people commenting here has interest, please provide a little paper describing the new feature with justification and changes to the spec. Either post it to the C committee mailing list directly or post it to the Austin Group mailing list and it will be appropriately forwarded.

- Issue History
Date Modified Username Field Change
2009-10-07 07:07 Konrad_Schwarz New Issue
2009-10-07 07:07 Konrad_Schwarz Status New => Under Review
2009-10-07 07:07 Konrad_Schwarz Assigned To => ajosey
2009-10-07 07:07 Konrad_Schwarz Name => Konrad Schwarz
2009-10-07 07:07 Konrad_Schwarz Organization => Siemens AG
2009-10-07 07:07 Konrad_Schwarz User Reference => ntohs_endianess
2009-10-07 07:07 Konrad_Schwarz Section => ntohs
2009-10-07 12:19 eblake Note Added: 0000248
2009-10-08 16:28 nick Tag Attached: c99
2009-10-08 16:29 nick Note Added: 0000250
2009-11-05 16:19 nick Note Added: 0000279
2009-12-02 15:11 jpoehlmann Note Added: 0000312
2010-01-07 17:06 Don Cragun Note Added: 0000372
2010-01-21 16:12 drepper Note Added: 0000377


Mantis 1.1.6[^]
Copyright © 2000 - 2008 Mantis Group
Powered by Mantis Bugtracker