Watt-32 tcp/ip  2.2 dev-rel.10
swap.h
Go to the documentation of this file.
1 
5 #ifndef __SYS_SWAP_BYTES_H
6 #define __SYS_SWAP_BYTES_H
7 
8 #ifndef __SYS_W32API_H
9 #include <sys/w32api.h>
10 #endif
11 
12 #ifndef __SYS_CDEFS_H
13 #include <sys/cdefs.h>
14 #endif
15 
16 #ifndef __NETINET_IN_H
17 #include <netinet/in.h>
18 #endif
19 
20 #if defined(__dj_include_netinet_in_h_)
21 #error "You are using the wrong version of <netinet/in.h>. Ref. point 10 of the INSTALL file"
22 #endif
23 
24 __BEGIN_DECLS
25 
26 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
27  /* Provide some real versions too
28  */
29  W32_FUNC unsigned short W32_CALL ntohs (unsigned short);
30  W32_FUNC unsigned short W32_CALL htons (unsigned short);
31  W32_FUNC unsigned long W32_CALL ntohl (unsigned long);
32  W32_FUNC unsigned long W32_CALL htonl (unsigned long);
33  W32_FUNC unsigned long cdecl _w32_intel (unsigned long x);
34  W32_FUNC unsigned short cdecl _w32_intel16 (unsigned short x);
35 #endif
36 
37 #undef ntohs
38 #undef htons
39 #undef ntohl
40 #undef htonl
41 #define ntohs(x) intel16(x)
42 #define htons(x) intel16(x)
43 #define ntohl(x) intel(x)
44 #define htonl(x) intel(x)
45 
46 
47 /*
48  * Hard to believe, but someone uses Watt-32 on a
49  * Motorola/PowerPC embedded target.
50  */
51 #if defined(BIG_ENDIAN_MACHINE) || defined(USE_BIGENDIAN)
52  #define intel(x) x
53  #define intel16(x) x
54 
55 #elif defined(__i386__) && defined(__GNUC__) && !defined(__NO_INLINE__) /* -O0 */
56  #define intel(x) __ntohl(x)
57  #define intel16(x) __ntohs(x)
58 
59  /*
60  * Ripped (and adapted) from <linux/include/asm-386/byteorder.h>
61  */
62  /*@unused@*/ extern __inline__ unsigned long __ntohl (unsigned long x)
63  {
64  __asm__ __volatile (
65  "xchgb %b0, %h0\n\t" /* swap lower bytes */
66  "rorl $16, %0\n\t" /* swap words */
67  "xchgb %b0, %h0" /* swap higher bytes */
68  : "=q" (x) : "0" (x));
69  return (x);
70  }
71 
72  /*@unused@*/ extern __inline__ unsigned short __ntohs (unsigned short x)
73  {
74  __asm__ __volatile__ (
75  "xchgb %b0, %h0" /* swap bytes */
76  : "=q" (x) : "0" (x));
77  return (x);
78  }
79 
80 #elif defined(__GNUC__) && defined(__x86_64__)
81  #define intel(x) __ntohl(x)
82  #define intel16(x) __ntohs(x)
83 
84  extern __inline__ unsigned short __ntohs (unsigned short x)
85  {
86  x = ((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8);
87  return (x);
88  }
89 
90  extern __inline__ unsigned long __ntohl (unsigned long x)
91  {
92  x = ((x & 0x000000FF) << 24) |
93  ((x & 0x0000FF00) << 8) |
94  ((x & 0x00FF0000) >> 8) |
95  ((x & 0xFF000000) >> 24);
96  return (x);
97  }
98 
99 #elif defined(__POCC__) /* PellesC */
100  #define intel(x) __ntohl(x)
101  #define intel16(x) __ntohs(x)
102 
103  __declspec(naked) inline unsigned long __fastcall __ntohl (unsigned long x)
104  {
105  __asm xchg cl, ch /* 'x' is in ecx */
106  __asm ror ecx, 16
107  __asm xchg cl, ch
108  __asm mov eax, ecx
109  __asm ret
110  }
111 
112  __declspec(naked) inline unsigned short __fastcall __ntohs (unsigned short x)
113  {
114  __asm xchg cl, ch /* 'x' is in ecx */
115  __asm movzx eax, cx
116  __asm ret
117  }
118 
119 #elif (defined(_MSC_VER) && (_MSC_VER >= 1200)) && /* MSVC 6+ */ \
120  !defined(__POCC__) /* "pocc -Ze" sets _MSC_VER */
121  #define intel(x) __ntohl(x)
122  #define intel16(x) __ntohs(x)
123 
124  /* Is it safe to write these as __fastcall?
125  */
126  #if 0
127  __declspec(naked) static unsigned long __cdecl __ntohl (unsigned long x)
128  {
129  __asm mov eax, [esp+4]
130  __asm xchg al, ah
131  __asm ror eax, 16
132  __asm xchg al, ah
133  __asm ret
134  }
135  __declspec(naked) static unsigned short __cdecl __ntohs (unsigned short x)
136  {
137  __asm mov ax, [esp+4]
138  __asm xchg al, ah
139  __asm ret
140  }
141  #else
142  __declspec(naked) static unsigned long __fastcall __ntohl (unsigned long x)
143  {
144  __asm xchg cl, ch /* 'x' is in ecx */
145  __asm ror ecx, 16
146  __asm xchg cl, ch
147  __asm mov eax, ecx
148  __asm ret
149  }
150  __declspec(naked) static unsigned short __fastcall __ntohs (unsigned short x)
151  {
152  __asm xchg cl, ch /* 'x' is in ecx */
153  __asm movzx eax, cx
154  __asm ret
155  }
156  #endif
157 
158 #elif defined(__WATCOMC__) && defined(__FLAT__) /* Watcom 32-bit */
159  #define intel(x) __ntohl(x)
160  #define intel16(x) __ntohs(x)
161 
162  extern unsigned long __ntohl (unsigned long x);
163  #pragma aux __ntohl = \
164  "xchg al, ah" \
165  "ror eax, 16" \
166  "xchg al, ah" \
167  parm [eax] \
168  modify [eax];
169 
170  extern unsigned short __ntohs (unsigned short x);
171  #pragma aux __ntohs = \
172  "xchg al, ah" \
173  parm [ax] \
174  modify [ax];
175 
176 #elif defined(__WATCOMC__) && !defined(__FLAT__) /* Watcom 16-bit */
177  #define intel(x) __ntohl(x)
178  #define intel16(x) __ntohs(x)
179 
180  extern unsigned long __ntohl (unsigned long x);
181  #pragma aux __ntohl = \
182  "xchg al, dh" \
183  "xchg ah, dl" \
184  parm [dx ax] \
185  modify [dx ax];
186 
187  extern unsigned short __ntohs (unsigned short x);
188  #pragma aux __ntohs = \
189  "xchg al, ah" \
190  parm [ax] \
191  modify [ax];
192 
193 #elif (defined(__BORLANDC__) && defined(__FLAT__)) || /* bcc32 */ \
194  (defined(__DMC__) && (__INTSIZE==4)) /* dmc -mx */
195  #include <dos.h>
196 
197  #define intel(x) __ntohl(x)
198  #define intel16(x) __ntohs(x)
199 
200  #define __ntohs(x) (_AX = x, \
201  __emit__(0x86,0xC4), /* xchg al, ah */ \
202  _AX)
203 
204  #define __ntohl(x) (_EAX = x, \
205  __emit__(0x86,0xC4), /* xchg al, ah */ \
206  __emit__(0xC1,0xC8,0x10), /* ror eax, 16 */ \
207  __emit__(0x86,0xC4), /* xchg al, ah */ \
208  _EAX)
209 
210 #elif defined(__CCDL__) && defined(__386__) /* LadSoft 386 */
211  #define intel(x) __ntohl(x)
212  #define intel16(x) __ntohs(x)
213 
214  static unsigned long __ntohl (unsigned long x)
215  {
216  asm { mov eax, [x]
217  xchg al, ah
218  ror eax, 16
219  xchg al, ah
220  }
221  return (_EAX);
222  }
223  static unsigned short __ntohs (unsigned short x)
224  {
225  asm { mov ax, [x]
226  xchg al, ah
227  }
228  return (unsigned short)_EAX; /* doesn't have _AX */
229  }
230 
231  /* This crashes mysteriously if we use _bswap()
232  */
233 #elif defined(__LCC__) && 0 /* LCC-Win32 */
234  #define intel(x) __ntohl(x)
235  #define intel16(x) __ntohs(x)
236  #if 0
237  #include <intrinsics.h>
238  #define W32_LCC_INTRINSICS_INCLUDED /* header guard is missing */
239  #define __ntohl(x) (unsigned long) _bswap(x)
240  #define __ntohs(x) ((unsigned short) (_bswap(x) >> 16))
241  #else
242  unsigned long inline __declspec(naked) __ntohl (unsigned long x)
243  {
244  _asm ("movl (%esp), %eax");
245  _asm ("xchg %ah, %al");
246  _asm ("rorl $16, %eax");
247  _asm ("xchg %ah, %al");
248  }
249  unsigned short inline __declspec(naked) __ntohs (unsigned short x)
250  {
251  _asm ("movs (%esp), %ax");
252  _asm ("xchg %ah, %al");
253  }
254  #endif
255 
256 #else /* no inlining possible (or worth the effort) */
257  #define intel W32_NAMESPACE (intel)
258  #define intel16 W32_NAMESPACE (intel16)
259  #define WATT32_NO_INLINE_INTEL
260 
261  W32_FUNC unsigned long cdecl intel (unsigned long x);
262  W32_FUNC unsigned short cdecl intel16 (unsigned short x);
263 #endif
264 
265 __END_DECLS
266 
267 #endif /* __SYS_SWAP_BYTES_H */
W32_FUNC unsigned long cdecl _w32_intel(unsigned long x)
Convert 32-bit big-endian (network order) to intel (host order) format.
Definition: misc.c:147
Definition: esp.h:48
Definition: ah.h:48
W32_FUNC unsigned short cdecl _w32_intel16(unsigned short x)
Convert 16-bit big-endian (network order) to intel (host order) format.
Definition: misc.c:159