1 /* DO NOT EDIT THIS FILE. 2 3 It has been auto-edited by fixincludes from: 4 5 "fixinc/tests/inc/sys/byteorder.h" 6 7 This had to be done to correct non-standard usages in the 8 original, manufacturer supplied header file. */ 9 10 #ifndef _SYS_BYTEORDER_H 11 #define _SYS_BYTEORDER_H 12 13 /* Functions to convert `short' and `long' quantities from host byte order 14 to (internet) network byte order (i.e. big-endian). 15 16 Written by Ron Guilmette (rfg@ncd.com). 17 18 This isn't actually used by GCC. It is installed by fixinc.svr4. 19 20 For big-endian machines these functions are essentially no-ops. 21 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). */ 24 25 #if !defined (__GNUC__) && !defined (__GNUG__) 26 #error You lose! This file is only useful with GNU compilers. 27 #endif 28 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 35 #endif 36 37 #ifdef __STDC__ 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__) */ 43 44 #if defined (__i386__) 45 46 #ifndef __BYTE_ORDER__ 47 #define __BYTE_ORDER__ __LITTLE_ENDIAN__ 48 #endif 49 50 /* Convert a host long to a network long. */ 51 52 /* We must use a new-style function definition, so that this will also 53 be valid for C++. */ 54 static __inline__ unsigned long htonl(unsigned long __arg)55htonl (unsigned long __arg) 56 { 57 register unsigned long __result; 58 59 __asm__ ("xchg%B0 %b0,%h0 ; ror%L0 $16,%0 ; xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg)); 60 return __result; 61 } 62 63 /* Convert a host short to a network short. */ 64 65 static __inline__ unsigned short htons(unsigned int __arg)66htons (unsigned int __arg) 67 { 68 register unsigned short __result; 69 70 __asm__ ("xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg)); 71 return __result; 72 } 73 74 #elif ((defined (__i860__) && !defined (__i860_big_endian__)) \ 75 || defined (__ns32k__) || defined (__vax__) \ 76 || defined (__spur__) || defined (__arm__)) 77 78 #ifndef __BYTE_ORDER__ 79 #define __BYTE_ORDER__ __LITTLE_ENDIAN__ 80 #endif 81 82 /* For other little-endian machines, using C code is just as efficient as 83 using assembly code. */ 84 85 /* Convert a host long to a network long. */ 86 87 static __inline__ unsigned long htonl(unsigned long __arg)88htonl (unsigned long __arg) 89 { 90 register unsigned long __result; 91 92 __result = (__arg >> 24) & 0x000000ff; 93 __result |= (__arg >> 8) & 0x0000ff00; 94 __result |= (__arg << 8) & 0x00ff0000; 95 __result |= (__arg << 24) & 0xff000000; 96 return __result; 97 } 98 99 /* Convert a host short to a network short. */ 100 101 static __inline__ unsigned short htons(unsigned int __arg)102htons (unsigned int __arg) 103 { 104 register unsigned short __result; 105 106 __result = (__arg << 8) & 0xff00; 107 __result |= (__arg >> 8) & 0x00ff; 108 return __result; 109 } 110 111 #else /* must be a big-endian machine */ 112 113 #ifndef __BYTE_ORDER__ 114 #define __BYTE_ORDER__ __BIG_ENDIAN__ 115 #endif 116 117 /* Convert a host long to a network long. */ 118 119 static __inline__ unsigned long htonl(unsigned long __arg)120htonl (unsigned long __arg) 121 { 122 return __arg; 123 } 124 125 /* Convert a host short to a network short. */ 126 127 static __inline__ unsigned short htons(unsigned int __arg)128htons (unsigned int __arg) 129 { 130 return __arg; 131 } 132 133 #endif /* big-endian */ 134 135 /* Convert a network long to a host long. */ 136 137 static __inline__ unsigned long ntohl(unsigned long __arg)138ntohl (unsigned long __arg) 139 { 140 return htonl (__arg); 141 } 142 143 /* Convert a network short to a host short. */ 144 145 static __inline__ unsigned short ntohs(unsigned int __arg)146ntohs (unsigned int __arg) 147 { 148 return htons (__arg); 149 } 150 #endif 151