endian.h revision 1.16 1 /* $NetBSD: endian.h,v 1.16 1995/06/01 17:19:18 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
5 * Copyright (c) 1987, 1991 Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)endian.h 7.8 (Berkeley) 4/3/91
37 */
38
39 #ifndef _I386_ENDIAN_H_
40 #define _I386_ENDIAN_H_
41
42 /*
43 * Define the order of 32-bit words in 64-bit words.
44 */
45 #define _QUAD_HIGHWORD 1
46 #define _QUAD_LOWWORD 0
47
48
49 #ifndef _POSIX_SOURCE
50 /*
51 * Definitions for byte order, according to byte significance from low
52 * address to high.
53 */
54 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
55 #define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
56 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
57
58 #define BYTE_ORDER LITTLE_ENDIAN
59
60 #include <sys/cdefs.h>
61
62 __BEGIN_DECLS
63 unsigned long htonl __P((unsigned long));
64 unsigned short htons __P((unsigned short));
65 unsigned long ntohl __P((unsigned long));
66 unsigned short ntohs __P((unsigned short));
67 __END_DECLS
68
69
70 #ifdef __GNUC__
71
72 #if defined(_KERNEL) && !defined(I386_CPU)
73 #define __byte_swap_long_variable(x) \
74 ({ register unsigned long __x = (x); \
75 __asm ("bswap %1" \
76 : "=r" (__x) \
77 : "0" (__x)); \
78 __x; })
79 #else
80 #define __byte_swap_long_variable(x) \
81 ({ register unsigned long __x = (x); \
82 __asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
83 : "=r" (__x) \
84 : "0" (__x)); \
85 __x; })
86 #endif /* _KERNEL && ... */
87
88 #define __byte_swap_word_variable(x) \
89 ({ register unsigned short __x = (x); \
90 __asm ("rorw $8, %w1" \
91 : "=r" (__x) \
92 : "0" (__x)); \
93 __x; })
94
95 #ifdef __OPTIMIZE__
96
97 #define __byte_swap_long_constant(x) \
98 ((((x) & 0xff000000) >> 24) | \
99 (((x) & 0x00ff0000) >> 8) | \
100 (((x) & 0x0000ff00) << 8) | \
101 (((x) & 0x000000ff) << 24))
102 #define __byte_swap_word_constant(x) \
103 ((((x) & 0xff00) >> 8) | \
104 (((x) & 0x00ff) << 8))
105 #define __byte_swap_long(x) \
106 (__builtin_constant_p((x)) ? \
107 __byte_swap_long_constant(x) : __byte_swap_long_variable(x))
108 #define __byte_swap_word(x) \
109 (__builtin_constant_p((x)) ? \
110 __byte_swap_word_constant(x) : __byte_swap_word_variable(x))
111
112 #else /* __OPTIMIZE__ */
113
114 #define __byte_swap_long(x) __byte_swap_long_variable(x)
115 #define __byte_swap_word(x) __byte_swap_word_variable(x)
116
117 #endif /* __OPTIMIZE__ */
118
119 #define ntohl(x) __byte_swap_long(x)
120 #define ntohs(x) __byte_swap_word(x)
121 #define htonl(x) __byte_swap_long(x)
122 #define htons(x) __byte_swap_word(x)
123
124 #endif /* __GNUC__ */
125
126
127 /*
128 * Macros for network/external number representation conversion.
129 */
130 #define NTOHL(x) (x) = ntohl((unsigned long)(x))
131 #define NTOHS(x) (x) = ntohs((unsigned short)(x))
132 #define HTONL(x) (x) = htonl((unsigned long)(x))
133 #define HTONS(x) (x) = htons((unsigned short)(x))
134
135 #endif /* _POSIX_SOURCE */
136
137 #endif /* !_I386_ENDIAN_H_ */
138