|
|
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:
65: #elif defined (__i860__) || defined (__ns32k__) || defined (__vax__) || defined (__spur__) || defined (__arm__)
66:
67: /* For other little-endian machines, using C code is just as efficient as
68: using assembly code. */
69:
70: /* Convert a host long to a network long. */
71:
72: __STATIC __inline__ unsigned long
73: htonl (__arg)
74: unsigned long __arg;
75: {
76: register unsigned long __result;
77:
78: __result = (__arg >> 24) & 0x000000ff;
79: __result |= (__arg >> 8) & 0x0000ff00;
80: __result |= (__arg << 8) & 0x00ff0000;
81: __result |= (__arg << 24) & 0xff000000;
82: return __result;
83: }
84:
85: /* Convert a host short to a network short. */
86:
87: __STATIC __inline__ unsigned short
88: htons (__arg)
89: unsigned short __arg;
90: {
91: register unsigned short __result;
92:
93: __result = (__arg << 8) & 0xff00;
94: __result |= (__arg >> 8) & 0x00ff;
95: return __result;
96: }
97:
98: #else /* must be a big-endian machine */
99:
100: /* Convert a host long to a network long. */
101:
102: __STATIC __inline__ unsigned long
103: htonl (__arg)
104: unsigned long __arg;
105: {
106: return __arg;
107: }
108:
109: /* Convert a host short to a network short. */
110:
111: __STATIC __inline__ unsigned short
112: htons (__arg)
113: unsigned short __arg;
114: {
115: return __arg;
116: }
117:
118: #endif /* big-endian */
119:
120: /* Convert a network long to a host long. */
121:
122: __STATIC __inline__ unsigned long
123: ntohl (__arg)
124: unsigned long __arg;
125: {
126: return htonl (__arg);
127: }
128:
129: /* Convert a network short to a host short. */
130:
131: __STATIC __inline__ unsigned short
132: ntohs (__arg)
133: unsigned int __arg;
134: {
135: return htons (__arg);
136: }
137:
138:
139: #undef __STATIC
140:
141: #endif /* !defined (_SYS_BYTEORDER_H) */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.