|
|
1.1 root 1: #ifndef _SYS_BYTEORDER_H
2: #define _SYS_BYTEORDER_H
3:
4: /* Functions to convert `short' and `long' quantities from host byte order
5: to (internet) network byte order (i.e. big-endian).
6:
7: Written by Ron Guilmette ([email protected]).
8:
9: This isn't actually used by GCC. It is installed by fixinc.svr4.
10:
11: For big-endian machines these functions are essentially no-ops.
12:
13: For little-endian machines, we define the functions using specialized
14: asm sequences in cases where doing so yields better code (e.g. i386). */
15:
16: #if !defined (__GNUC__) && !defined (__GNUG__)
17: #error You lose! This file is only useful with GNU compilers.
18: #endif
19:
20: #ifdef __GNUC__
21: #define __STATIC static
22: #else
23: #define __STATIC
24: #endif
25:
26: #ifdef __STDC__
27: __STATIC __inline__ unsigned long htonl (unsigned long);
28: __STATIC __inline__ unsigned short htons (unsigned int);
29: __STATIC __inline__ unsigned long ntohl (unsigned long);
30: __STATIC __inline__ unsigned short ntohs (unsigned int);
31: #endif /* defined (__STDC__) */
32:
33: #if defined (__i386__)
34:
35: /* Convert a host long to a network long. */
36:
37: __STATIC __inline__ unsigned long
38: htonl (__arg)
39: unsigned long __arg;
40: {
41: register unsigned long __result __asm__ ("%eax");
42:
43: __result = __arg;
44: __asm__ ("xchgb %%ah, %%al\n\
45: rorl $16, %%eax\n\
46: xchgb %%ah, %%al\n\
47: clc" : "=r" (__result) : "0" (__result));
48: return __result;
49: }
50:
51: /* Convert a host short to a network short. */
52:
53: __STATIC __inline__ unsigned short
54: htons (__arg)
55: unsigned int __arg;
56: {
57: register unsigned short __result __asm__ ("%eax");
58:
59: __result = __arg;
60: __asm__ ("xchgb %%ah, %%al\n\
61: clc" : "=r" (__result) : "0" (__result));
62: return __result;
63: }
64:
1.1.1.2 ! root 65: #elif ((defined (__i860__) && !defined (__i860_big_endian__)) \
! 66: || defined (__ns32k__) || defined (__vax__) \
! 67: || defined (__spur__) || defined (__arm__))
1.1 root 68:
69: /* For other little-endian machines, using C code is just as efficient as
70: using assembly code. */
71:
72: /* Convert a host long to a network long. */
73:
74: __STATIC __inline__ unsigned long
75: htonl (__arg)
76: unsigned long __arg;
77: {
78: register unsigned long __result;
79:
80: __result = (__arg >> 24) & 0x000000ff;
81: __result |= (__arg >> 8) & 0x0000ff00;
82: __result |= (__arg << 8) & 0x00ff0000;
83: __result |= (__arg << 24) & 0xff000000;
84: return __result;
85: }
86:
87: /* Convert a host short to a network short. */
88:
89: __STATIC __inline__ unsigned short
90: htons (__arg)
1.1.1.2 ! root 91: unsigned int __arg;
1.1 root 92: {
93: register unsigned short __result;
94:
95: __result = (__arg << 8) & 0xff00;
96: __result |= (__arg >> 8) & 0x00ff;
97: return __result;
98: }
99:
100: #else /* must be a big-endian machine */
101:
102: /* Convert a host long to a network long. */
103:
104: __STATIC __inline__ unsigned long
105: htonl (__arg)
106: unsigned long __arg;
107: {
108: return __arg;
109: }
110:
111: /* Convert a host short to a network short. */
112:
113: __STATIC __inline__ unsigned short
114: htons (__arg)
1.1.1.2 ! root 115: unsigned int __arg;
1.1 root 116: {
117: return __arg;
118: }
119:
120: #endif /* big-endian */
121:
122: /* Convert a network long to a host long. */
123:
124: __STATIC __inline__ unsigned long
125: ntohl (__arg)
126: unsigned long __arg;
127: {
128: return htonl (__arg);
129: }
130:
131: /* Convert a network short to a host short. */
132:
133: __STATIC __inline__ unsigned short
134: ntohs (__arg)
135: unsigned int __arg;
136: {
137: return htons (__arg);
138: }
139:
140:
141: #undef __STATIC
142:
143: #endif /* !defined (_SYS_BYTEORDER_H) */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.