endian.h (636f2ebf670b685ad40327f75ceb90b60594d738) endian.h (fa561e1e20629340c0ae693f5d6e7fe8fe3c602b)
1/*-
1/*
2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)endian.h 8.1 (Berkeley) 6/10/93
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)endian.h 8.1 (Berkeley) 6/10/93
30 * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
34 * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
31 * $FreeBSD$
32 */
33
35 * $FreeBSD$
36 */
37
34#ifndef _MACHINE_ENDIAN_H_
35#define _MACHINE_ENDIAN_H_
38#ifndef _ENDIAN_H_
39#define _ENDIAN_H_
36
40
37#include <sys/cdefs.h>
38#include <sys/_types.h>
39
40/*
41 * Define the order of 32-bit words in 64-bit words.
42 */
43#define _QUAD_HIGHWORD 0
44#define _QUAD_LOWWORD 1
45
41/*
42 * Define the order of 32-bit words in 64-bit words.
43 */
44#define _QUAD_HIGHWORD 0
45#define _QUAD_LOWWORD 1
46
47#ifndef _POSIX_SOURCE
46/*
48/*
47 * GCC defines _BIG_ENDIAN and _LITTLE_ENDIAN equal to __BIG_ENDIAN__
48 * and __LITTLE_ENDIAN__ (resp).
49 */
50#ifdef _BIG_ENDIAN
51#undef _BIG_ENDIAN
52#endif
53#ifdef _LITTLE_ENDIAN
54#undef _LITTLE_ENDIAN
55#endif
56
57/*
58 * Definitions for byte order, according to byte significance from low
59 * address to high.
60 */
49 * Definitions for byte order, according to byte significance from low
50 * address to high.
51 */
61#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
62#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
63#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
52#define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
53#define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
54#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
64
55
65#ifdef __LITTLE_ENDIAN__
66#define _BYTE_ORDER _LITTLE_ENDIAN
67#else
68#define _BYTE_ORDER _BIG_ENDIAN
69#endif
56#define BYTE_ORDER BIG_ENDIAN
70
57
71/*
72 * Deprecated variants that don't have enough underscores to be useful in more
73 * strict namespaces.
74 */
75#if __BSD_VISIBLE
76#define LITTLE_ENDIAN _LITTLE_ENDIAN
77#define BIG_ENDIAN _BIG_ENDIAN
78#define PDP_ENDIAN _PDP_ENDIAN
79#define BYTE_ORDER _BYTE_ORDER
80#endif
58#include <sys/cdefs.h>
59#include <sys/types.h>
81
60
82#ifdef __CC_SUPPORTS___INLINE
61typedef u_int32_t in_addr_t;
62typedef u_int16_t in_port_t;
83
63
84static __inline __uint16_t
85__bswap16(__uint16_t _x)
86{
64__BEGIN_DECLS
65in_addr_t htonl __P((in_addr_t)) __attribute__((__const__));
66in_port_t htons __P((in_port_t)) __attribute__((__const__));
67in_addr_t ntohl __P((in_addr_t)) __attribute__((__const__));
68in_port_t ntohs __P((in_port_t)) __attribute__((__const__));
69u_int16_t bswap16 __P((u_int16_t)) __attribute__((__const__));
70u_int32_t bswap32 __P((u_int32_t)) __attribute__((__const__));
71u_int64_t bswap64 __P((u_int64_t)) __attribute__((__const__));
72__END_DECLS
87
73
88 return ((_x >> 8) | ((_x << 8) & 0xff00));
89}
90
91static __inline __uint32_t
92__bswap32(__uint32_t _x)
93{
94
95 return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
96 ((_x << 24) & 0xff000000));
97}
98
99static __inline __uint64_t
100__bswap64(__uint64_t _x)
101{
102
103 return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
104 ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
105 ((_x << 24) & ((__uint64_t)0xff << 40)) |
106 ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
107}
108
109#define __htonl(x) ((__uint32_t)(x))
110#define __htons(x) ((__uint16_t)(x))
111#define __ntohl(x) ((__uint32_t)(x))
112#define __ntohs(x) ((__uint16_t)(x))
113
114#else /* !__CC_SUPPORTS___INLINE */
115
116/*
74/*
117 * No optimizations are available for this compiler. Fall back to
118 * non-optimized functions by defining the constant usually used to prevent
119 * redefinition.
75 * Macros for network/external number representation conversion.
120 */
76 */
121#define _BYTEORDER_FUNC_DEFINED
77#if BYTE_ORDER == BIG_ENDIAN && !defined(lint)
78#define ntohl(x) (x)
79#define ntohs(x) (x)
80#define htonl(x) (x)
81#define htons(x) (x)
122
82
123#endif /* __CC_SUPPORTS___INLINE */
83#define NTOHL(x) (x)
84#define NTOHS(x) (x)
85#define HTONL(x) (x)
86#define HTONS(x) (x)
124
87
125#endif /* !_MACHINE_ENDIAN_H_ */
88#else
89
90#define NTOHL(x) (x) = ntohl((in_addr_t)(x))
91#define NTOHS(x) (x) = ntohs((in_port_t)(x))
92#define HTONL(x) (x) = htonl((in_addr_t)(x))
93#define HTONS(x) (x) = htons((in_port_t)(x))
94#endif
95
96#endif /* !_POSIX_SOURCE */
97#endif /* !_ENDIAN_H_ */