1 /* DO NOT EDIT THIS FILE.
3 It has been auto-edited by fixincludes from:
5 "fixinc/tests/inc/sys/byteorder.h"
7 This had to be done to correct non-standard usages in the
8 original, manufacturer supplied header file. */
10 #ifndef _SYS_BYTEORDER_H
11 #define _SYS_BYTEORDER_H
13 /* Functions to convert `short' and `long' quantities from host byte order
14 to (internet) network byte order (i.e. big-endian).
16 Written by Ron Guilmette (rfg@ncd.com).
18 This isn't actually used by GCC. It is installed by fixinc.svr4.
20 For big-endian machines these functions are essentially no-ops.
22 For little-endian machines, we define the functions using specialized
23 asm sequences in cases where doing so yields better code (e.g. i386). */
25 #if !defined (__GNUC__) && !defined (__GNUG__)
26 #error You lose! This file is only useful with GNU compilers.
29 #ifndef __BYTE_ORDER__
30 /* Byte order defines. These are as defined on UnixWare 1.1, but with
31 double underscores added at the front and back. */
32 #define __LITTLE_ENDIAN__ 1234
33 #define __BIG_ENDIAN__ 4321
34 #define __PDP_ENDIAN__ 3412
38 static __inline__ unsigned long htonl (unsigned long);
39 static __inline__ unsigned short htons (unsigned int);
40 static __inline__ unsigned long ntohl (unsigned long);
41 static __inline__ unsigned short ntohs (unsigned int);
42 #endif /* defined (__STDC__) */
44 #if defined (__i386__)
46 #ifndef __BYTE_ORDER__
47 #define __BYTE_ORDER__ __LITTLE_ENDIAN__
50 /* Convert a host long to a network long. */
52 /* We must use a new-style function definition, so that this will also
54 static __inline__ unsigned long
55 htonl (unsigned long __arg)
57 register unsigned long __result;
59 __asm__ ("xchg%B0 %b0,%h0
61 xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
65 /* Convert a host short to a network short. */
67 static __inline__ unsigned short
68 htons (unsigned int __arg)
70 register unsigned short __result;
72 __asm__ ("xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
76 #elif ((defined (__i860__) && !defined (__i860_big_endian__)) \
77 || defined (__ns32k__) || defined (__vax__) \
78 || defined (__spur__) || defined (__arm__))
80 #ifndef __BYTE_ORDER__
81 #define __BYTE_ORDER__ __LITTLE_ENDIAN__
84 /* For other little-endian machines, using C code is just as efficient as
85 using assembly code. */
87 /* Convert a host long to a network long. */
89 static __inline__ unsigned long
90 htonl (unsigned long __arg)
92 register unsigned long __result;
94 __result = (__arg >> 24) & 0x000000ff;
95 __result |= (__arg >> 8) & 0x0000ff00;
96 __result |= (__arg << 8) & 0x00ff0000;
97 __result |= (__arg << 24) & 0xff000000;
101 /* Convert a host short to a network short. */
103 static __inline__ unsigned short
104 htons (unsigned int __arg)
106 register unsigned short __result;
108 __result = (__arg << 8) & 0xff00;
109 __result |= (__arg >> 8) & 0x00ff;
113 #else /* must be a big-endian machine */
115 #ifndef __BYTE_ORDER__
116 #define __BYTE_ORDER__ __BIG_ENDIAN__
119 /* Convert a host long to a network long. */
121 static __inline__ unsigned long
122 htonl (unsigned long __arg)
127 /* Convert a host short to a network short. */
129 static __inline__ unsigned short
130 htons (unsigned int __arg)
135 #endif /* big-endian */
137 /* Convert a network long to a host long. */
139 static __inline__ unsigned long
140 ntohl (unsigned long __arg)
142 return htonl (__arg);
145 /* Convert a network short to a host short. */
147 static __inline__ unsigned short
148 ntohs (unsigned int __arg)
150 return htons (__arg);