| 1 |
27 |
unneback |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// include/sys/endian.h
|
| 4 |
|
|
//
|
| 5 |
|
|
//==========================================================================
|
| 6 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
| 7 |
|
|
//
|
| 8 |
|
|
// -------------------------------------------
|
| 9 |
|
|
//
|
| 10 |
|
|
// Portions of this software may have been derived from OpenBSD,
|
| 11 |
|
|
// FreeBSD or other sources, and are covered by the appropriate
|
| 12 |
|
|
// copyright disclaimers included herein.
|
| 13 |
|
|
//
|
| 14 |
|
|
// Portions created by Red Hat are
|
| 15 |
|
|
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
| 16 |
|
|
//
|
| 17 |
|
|
// -------------------------------------------
|
| 18 |
|
|
//
|
| 19 |
|
|
//####BSDCOPYRIGHTEND####
|
| 20 |
|
|
//==========================================================================
|
| 21 |
|
|
|
| 22 |
|
|
//==========================================================================
|
| 23 |
|
|
//
|
| 24 |
|
|
// include/sys/endian.h
|
| 25 |
|
|
//
|
| 26 |
|
|
//
|
| 27 |
|
|
//
|
| 28 |
|
|
//==========================================================================
|
| 29 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
| 30 |
|
|
//
|
| 31 |
|
|
// -------------------------------------------
|
| 32 |
|
|
//
|
| 33 |
|
|
// Portions of this software may have been derived from OpenBSD or other sources,
|
| 34 |
|
|
// and are covered by the appropriate copyright disclaimers included herein.
|
| 35 |
|
|
//
|
| 36 |
|
|
// -------------------------------------------
|
| 37 |
|
|
//
|
| 38 |
|
|
//####BSDCOPYRIGHTEND####
|
| 39 |
|
|
//==========================================================================
|
| 40 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 41 |
|
|
//
|
| 42 |
|
|
// Author(s): gthomas
|
| 43 |
|
|
// Contributors: gthomas
|
| 44 |
|
|
// Date: 2000-01-10
|
| 45 |
|
|
// Purpose:
|
| 46 |
|
|
// Description:
|
| 47 |
|
|
//
|
| 48 |
|
|
//
|
| 49 |
|
|
//####DESCRIPTIONEND####
|
| 50 |
|
|
//
|
| 51 |
|
|
//==========================================================================
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
/* $OpenBSD: endian.h,v 1.4 1999/07/21 05:58:25 csapuntz Exp $ */
|
| 55 |
|
|
|
| 56 |
|
|
/*-
|
| 57 |
|
|
* Copyright (c) 1997 Niklas Hallqvist. All rights reserved.
|
| 58 |
|
|
*
|
| 59 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 60 |
|
|
* modification, are permitted provided that the following conditions
|
| 61 |
|
|
* are met:
|
| 62 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
| 63 |
|
|
* notice, this list of conditions and the following disclaimer.
|
| 64 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
| 65 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
| 66 |
|
|
* documentation and/or other materials provided with the distribution.
|
| 67 |
|
|
* 3. All advertising materials mentioning features or use of this software
|
| 68 |
|
|
* must display the following acknowledgement:
|
| 69 |
|
|
* This product includes software developed by Niklas Hallqvist.
|
| 70 |
|
|
* 4. The name of the author may not be used to endorse or promote products
|
| 71 |
|
|
* derived from this software without specific prior written permission.
|
| 72 |
|
|
*
|
| 73 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
| 74 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
| 75 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
| 76 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 77 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
| 78 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 79 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 80 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 81 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
| 82 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 83 |
|
|
*/
|
| 84 |
|
|
|
| 85 |
|
|
/*
|
| 86 |
|
|
* Generic definitions for little- and big-endian systems. Other endianesses
|
| 87 |
|
|
* has to be dealt with in the specific machine/endian.h file for that port.
|
| 88 |
|
|
*
|
| 89 |
|
|
* This file is meant to be included from a little- or big-endian port's
|
| 90 |
|
|
* machine/endian.h after setting BYTE_ORDER to either 1234 for little endian
|
| 91 |
|
|
* or 4321 for big..
|
| 92 |
|
|
*/
|
| 93 |
|
|
|
| 94 |
|
|
#ifndef _SYS_ENDIAN_H_
|
| 95 |
|
|
#define _SYS_ENDIAN_H_
|
| 96 |
|
|
|
| 97 |
|
|
#include <cyg/hal/basetype.h>
|
| 98 |
|
|
|
| 99 |
|
|
#if CYG_BYTEORDER == CYG_MSBFIRST
|
| 100 |
|
|
#define BYTE_ORDER BIG_ENDIAN
|
| 101 |
|
|
#else
|
| 102 |
|
|
#define BYTE_ORDER LITTLE_ENDIAN
|
| 103 |
|
|
#endif
|
| 104 |
|
|
|
| 105 |
|
|
#ifndef _POSIX_SOURCE
|
| 106 |
|
|
|
| 107 |
|
|
#define LITTLE_ENDIAN 1234
|
| 108 |
|
|
#define BIG_ENDIAN 4321
|
| 109 |
|
|
#define PDP_ENDIAN 3412
|
| 110 |
|
|
|
| 111 |
|
|
#ifdef __GNUC__
|
| 112 |
|
|
|
| 113 |
|
|
#define __swap16gen(x) ({ \
|
| 114 |
|
|
cyg_uint16 __swap16gen_x = (x); \
|
| 115 |
|
|
\
|
| 116 |
|
|
(cyg_uint16)((__swap16gen_x & 0xff) << 8 | \
|
| 117 |
|
|
(__swap16gen_x & 0xff00) >> 8); \
|
| 118 |
|
|
})
|
| 119 |
|
|
|
| 120 |
|
|
#define __swap32gen(x) ({ \
|
| 121 |
|
|
cyg_uint32 __swap32gen_x = (x); \
|
| 122 |
|
|
\
|
| 123 |
|
|
(cyg_uint32)((__swap32gen_x & 0xff) << 24 | \
|
| 124 |
|
|
(__swap32gen_x & 0xff00) << 8 | \
|
| 125 |
|
|
(__swap32gen_x & 0xff0000) >> 8 | \
|
| 126 |
|
|
(__swap32gen_x & 0xff000000) >> 24); \
|
| 127 |
|
|
})
|
| 128 |
|
|
|
| 129 |
|
|
#else /* __GNUC__ */
|
| 130 |
|
|
|
| 131 |
|
|
/* Note that these macros evaluate their arguments several times. */
|
| 132 |
|
|
#define __swap16gen(x) \
|
| 133 |
|
|
(cyg_uint16)(((cyg_uint16)(x) & 0xff) << 8 | ((cyg_uint16)(x) & 0xff00) >> 8)
|
| 134 |
|
|
|
| 135 |
|
|
#define __swap32gen(x) \
|
| 136 |
|
|
(cyg_uint32)(((cyg_uint32)(x) & 0xff) << 24 | \
|
| 137 |
|
|
((cyg_uint32)(x) & 0xff00) << 8 | ((cyg_uint32)(x) & 0xff0000) >> 8 | \
|
| 138 |
|
|
((cyg_uint32)(x) & 0xff000000) >> 24)
|
| 139 |
|
|
|
| 140 |
|
|
#endif /* __GNUC__ */
|
| 141 |
|
|
|
| 142 |
|
|
/*
|
| 143 |
|
|
* Define MD_SWAP if you provide swap{16,32}md functions/macros that are
|
| 144 |
|
|
* optimized for your architecture, These will be used for swap{16,32}
|
| 145 |
|
|
* unless the argument is a constant and we are using GCC, where we can
|
| 146 |
|
|
* take advantage of the CSE phase much better by using the generic version.
|
| 147 |
|
|
*/
|
| 148 |
|
|
#ifdef MD_SWAP
|
| 149 |
|
|
#if __GNUC__
|
| 150 |
|
|
|
| 151 |
|
|
#define swap16(x) ({ \
|
| 152 |
|
|
cyg_uint16 __swap16_x = (x); \
|
| 153 |
|
|
\
|
| 154 |
|
|
__builtin_constant_p(x) ? __swap16gen(__swap16_x) : \
|
| 155 |
|
|
__swap16md(__swap16_x); \
|
| 156 |
|
|
})
|
| 157 |
|
|
|
| 158 |
|
|
#define swap32(x) ({ \
|
| 159 |
|
|
cyg_uint32 __swap32_x = (x); \
|
| 160 |
|
|
\
|
| 161 |
|
|
__builtin_constant_p(x) ? __swap32gen(__swap32_x) : \
|
| 162 |
|
|
__swap32md(__swap32_x); \
|
| 163 |
|
|
})
|
| 164 |
|
|
|
| 165 |
|
|
#endif /* __GNUC__ */
|
| 166 |
|
|
|
| 167 |
|
|
#else /* MD_SWAP */
|
| 168 |
|
|
#define swap16 __swap16gen
|
| 169 |
|
|
#define swap32 __swap32gen
|
| 170 |
|
|
#endif /* MD_SWAP */
|
| 171 |
|
|
|
| 172 |
|
|
#define swap16_multi(v, n) do { \
|
| 173 |
|
|
size_t __swap16_multi_n = (n); \
|
| 174 |
|
|
cyg_uint16 *__swap16_multi_v = (v); \
|
| 175 |
|
|
\
|
| 176 |
|
|
while (__swap16_multi_n) { \
|
| 177 |
|
|
*__swap16_multi_v = swap16(*__swap16_multi_v); \
|
| 178 |
|
|
__swap16_multi_v++; \
|
| 179 |
|
|
__swap16_multi_n--; \
|
| 180 |
|
|
} \
|
| 181 |
|
|
} while (0)
|
| 182 |
|
|
|
| 183 |
|
|
cyg_uint32 htobe32 __P((cyg_uint32));
|
| 184 |
|
|
cyg_uint16 htobe16 __P((cyg_uint16));
|
| 185 |
|
|
cyg_uint32 betoh32 __P((cyg_uint32));
|
| 186 |
|
|
cyg_uint16 betoh16 __P((cyg_uint16));
|
| 187 |
|
|
|
| 188 |
|
|
cyg_uint32 htole32 __P((cyg_uint32));
|
| 189 |
|
|
cyg_uint16 htole16 __P((cyg_uint16));
|
| 190 |
|
|
cyg_uint32 letoh32 __P((cyg_uint32));
|
| 191 |
|
|
cyg_uint16 letoh16 __P((cyg_uint16));
|
| 192 |
|
|
|
| 193 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
| 194 |
|
|
|
| 195 |
|
|
/* Can be overridden by machine/endian.h before inclusion of this file. */
|
| 196 |
|
|
#ifndef _QUAD_HIGHWORD
|
| 197 |
|
|
#define _QUAD_HIGHWORD 1
|
| 198 |
|
|
#endif
|
| 199 |
|
|
#ifndef _QUAD_LOWWORD
|
| 200 |
|
|
#define _QUAD_LOWWORD 0
|
| 201 |
|
|
#endif
|
| 202 |
|
|
|
| 203 |
|
|
#define htobe16 swap16
|
| 204 |
|
|
#define htobe32 swap32
|
| 205 |
|
|
#define betoh16 swap16
|
| 206 |
|
|
#define betoh32 swap32
|
| 207 |
|
|
|
| 208 |
|
|
#define htole16(x) (x)
|
| 209 |
|
|
#define htole32(x) (x)
|
| 210 |
|
|
#define letoh16(x) (x)
|
| 211 |
|
|
#define letoh32(x) (x)
|
| 212 |
|
|
|
| 213 |
|
|
#endif /* BYTE_ORDER */
|
| 214 |
|
|
|
| 215 |
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
| 216 |
|
|
|
| 217 |
|
|
/* Can be overridden by machine/endian.h before inclusion of this file. */
|
| 218 |
|
|
#ifndef _QUAD_HIGHWORD
|
| 219 |
|
|
#define _QUAD_HIGHWORD 0
|
| 220 |
|
|
#endif
|
| 221 |
|
|
#ifndef _QUAD_LOWWORD
|
| 222 |
|
|
#define _QUAD_LOWWORD 1
|
| 223 |
|
|
#endif
|
| 224 |
|
|
|
| 225 |
|
|
#define htole16 swap16
|
| 226 |
|
|
#define htole32 swap32
|
| 227 |
|
|
#define letoh16 swap16
|
| 228 |
|
|
#define letoh32 swap32
|
| 229 |
|
|
|
| 230 |
|
|
#define htobe16(x) (x)
|
| 231 |
|
|
#define htobe32(x) (x)
|
| 232 |
|
|
#define betoh16(x) (x)
|
| 233 |
|
|
#define betoh32(x) (x)
|
| 234 |
|
|
|
| 235 |
|
|
#endif /* BYTE_ORDER */
|
| 236 |
|
|
|
| 237 |
|
|
#define htons htobe16
|
| 238 |
|
|
#define htonl htobe32
|
| 239 |
|
|
#define ntohs betoh16
|
| 240 |
|
|
#define ntohl betoh32
|
| 241 |
|
|
|
| 242 |
|
|
#define NTOHL(x) (x) = ntohl((cyg_uint32)(x))
|
| 243 |
|
|
#define NTOHS(x) (x) = ntohs((cyg_uint16)(x))
|
| 244 |
|
|
#define HTONL(x) (x) = htonl((cyg_uint32)(x))
|
| 245 |
|
|
#define HTONS(x) (x) = htons((cyg_uint16)(x))
|
| 246 |
|
|
|
| 247 |
|
|
#endif /* _POSIX_SOURCE */
|
| 248 |
|
|
#endif /* _SYS_ENDIAN_H_ */
|