Branch data Line data Source code
1 : : /* asm/byteorder.h
2 : :
3 : : Copyright 1996, 1998, 2001, 2006 Red Hat, Inc.
4 : :
5 : : This file is part of Cygwin.
6 : :
7 : : This software is a copyrighted work licensed under the terms of the
8 : : Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 : : details. */
10 : :
11 : : #ifndef _I386_BYTEORDER_H
12 : : #define _I386_BYTEORDER_H
13 : :
14 : : #include <stdint.h>
15 : :
16 : : #ifdef __cplusplus
17 : : extern "C" {
18 : : #endif
19 : :
20 : : #ifndef __LITTLE_ENDIAN
21 : : #define __LITTLE_ENDIAN 1234
22 : : #endif
23 : :
24 : : #ifndef __LITTLE_ENDIAN_BITFIELD
25 : : #define __LITTLE_ENDIAN_BITFIELD
26 : : #endif
27 : :
28 : : extern uint32_t ntohl(uint32_t);
29 : : extern uint16_t ntohs(uint16_t);
30 : : extern uint32_t htonl(uint32_t);
31 : : extern uint16_t htons(uint16_t);
32 : :
33 : : extern __inline__ uint32_t __ntohl(uint32_t);
34 : : extern __inline__ uint16_t __ntohs(uint16_t);
35 : : extern __inline__ uint32_t __constant_ntohl(uint32_t);
36 : : extern __inline__ uint16_t __constant_ntohs(uint16_t);
37 : :
38 : : extern __inline__ uint32_t
39 : : __ntohl(uint32_t x)
40 : : {
41 : : __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
42 : : "rorl $16,%0\n\t" /* swap words */
43 : : "xchgb %b0,%h0" /* swap higher bytes */
44 : : :"=q" (x)
45 : 0 : : "0" (x));
46 : 0 : return x;
47 : : }
48 : :
49 : : #define __constant_ntohl(x) \
50 : : ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
51 : : (((uint32_t)(x) & 0x0000ff00U) << 8) | \
52 : : (((uint32_t)(x) & 0x00ff0000U) >> 8) | \
53 : : (((uint32_t)(x) & 0xff000000U) >> 24)))
54 : :
55 : : extern __inline__ uint16_t
56 : : __ntohs(uint16_t x)
57 : : {
58 : : __asm__("xchgb %b0,%h0" /* swap bytes */
59 : : : "=q" (x)
60 : 0 : : "0" (x));
61 : 0 : return x;
62 : : }
63 : :
64 : : #define __constant_ntohs(x) \
65 : : ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
66 : : (((uint16_t)(x) & 0xff00) >> 8))) \
67 : :
68 : : #define __htonl(x) __ntohl(x)
69 : : #define __htons(x) __ntohs(x)
70 : : #define __constant_htonl(x) __constant_ntohl(x)
71 : : #define __constant_htons(x) __constant_ntohs(x)
72 : :
73 : : #if defined (__OPTIMIZE__) && !defined (__NO_INLINE__)
74 : : # define ntohl(x) \
75 : : (__builtin_constant_p((long)(x)) ? \
76 : : __constant_ntohl((x)) : \
77 : : __ntohl((x)))
78 : : # define ntohs(x) \
79 : : (__builtin_constant_p((short)(x)) ? \
80 : : __constant_ntohs((x)) : \
81 : : __ntohs((x)))
82 : : # define htonl(x) \
83 : : (__builtin_constant_p((long)(x)) ? \
84 : : __constant_htonl((x)) : \
85 : : __htonl((x)))
86 : : # define htons(x) \
87 : : (__builtin_constant_p((short)(x)) ? \
88 : : __constant_htons((x)) : \
89 : : __htons((x)))
90 : : #endif
91 : :
92 : : #ifdef __cplusplus
93 : : }
94 : : #endif
95 : :
96 : : #endif
|