00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _SYS_BYTEORDER_H
00011 #define _SYS_BYTEORDER_H
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if !defined (__GNUC__) && !defined (__GNUG__)
00026 #error You lose! This file is only useful with GNU compilers.
00027 #endif
00028
00029 #ifndef __BYTE_ORDER__
00030
00031
00032 #define __LITTLE_ENDIAN__ 1234
00033 #define __BIG_ENDIAN__ 4321
00034 #define __PDP_ENDIAN__ 3412
00035 #endif
00036
00037 #ifdef __STDC__
00038 static __inline__ unsigned long htonl (unsigned long);
00039 static __inline__ unsigned short htons (unsigned int);
00040 static __inline__ unsigned long ntohl (unsigned long);
00041 static __inline__ unsigned short ntohs (unsigned int);
00042 #endif
00043
00044 #if defined (__i386__)
00045
00046 #ifndef __BYTE_ORDER__
00047 #define __BYTE_ORDER__ __LITTLE_ENDIAN__
00048 #endif
00049
00050
00051
00052
00053
00054 static __inline__ unsigned long
00055 htonl (unsigned long __arg)
00056 {
00057 register unsigned long __result;
00058
00059 __asm__ ("xchg%B0 %b0,%h0
00060 ror%L0 $16,%0
00061 xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
00062 return __result;
00063 }
00064
00065
00066
00067 static __inline__ unsigned short
00068 htons (unsigned int __arg)
00069 {
00070 register unsigned short __result;
00071
00072 __asm__ ("xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
00073 return __result;
00074 }
00075
00076 #elif ((defined (__i860__) && !defined (__i860_big_endian__)) \
00077 || defined (__ns32k__) || defined (__vax__) \
00078 || defined (__spur__) || defined (__arm__))
00079
00080 #ifndef __BYTE_ORDER__
00081 #define __BYTE_ORDER__ __LITTLE_ENDIAN__
00082 #endif
00083
00084
00085
00086
00087
00088
00089 static __inline__ unsigned long
00090 htonl (unsigned long __arg)
00091 {
00092 register unsigned long __result;
00093
00094 __result = (__arg >> 24) & 0x000000ff;
00095 __result |= (__arg >> 8) & 0x0000ff00;
00096 __result |= (__arg << 8) & 0x00ff0000;
00097 __result |= (__arg << 24) & 0xff000000;
00098 return __result;
00099 }
00100
00101
00102
00103 static __inline__ unsigned short
00104 htons (unsigned int __arg)
00105 {
00106 register unsigned short __result;
00107
00108 __result = (__arg << 8) & 0xff00;
00109 __result |= (__arg >> 8) & 0x00ff;
00110 return __result;
00111 }
00112
00113 #else
00114
00115 #ifndef __BYTE_ORDER__
00116 #define __BYTE_ORDER__ __BIG_ENDIAN__
00117 #endif
00118
00119
00120
00121 static __inline__ unsigned long
00122 htonl (unsigned long __arg)
00123 {
00124 return __arg;
00125 }
00126
00127
00128
00129 static __inline__ unsigned short
00130 htons (unsigned int __arg)
00131 {
00132 return __arg;
00133 }
00134
00135 #endif
00136
00137
00138
00139 static __inline__ unsigned long
00140 ntohl (unsigned long __arg)
00141 {
00142 return htonl (__arg);
00143 }
00144
00145
00146
00147 static __inline__ unsigned short
00148 ntohs (unsigned int __arg)
00149 {
00150 return htons (__arg);
00151 }
00152 #endif