The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* $OpenBSD: endian.h,v 1.17 2006/01/06 18:53:05 millert Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * Generic definitions for little- and big-endian systems. Other endianesses |
| 29 | * has to be dealt with in the specific machine/endian.h file for that port. |
| 30 | * |
| 31 | * This file is meant to be included from a little- or big-endian port's |
| 32 | * machine/endian.h after setting _BYTE_ORDER to either 1234 for little endian |
| 33 | * or 4321 for big.. |
| 34 | */ |
| 35 | |
| 36 | #ifndef _SYS_ENDIAN_H_ |
| 37 | #define _SYS_ENDIAN_H_ |
| 38 | |
| 39 | #include <sys/cdefs.h> |
| 40 | #include <machine/_types.h> |
| 41 | |
| 42 | #define _LITTLE_ENDIAN 1234 |
| 43 | #define _BIG_ENDIAN 4321 |
| 44 | #define _PDP_ENDIAN 3412 |
| 45 | |
| 46 | #if __BSD_VISIBLE |
| 47 | #define LITTLE_ENDIAN _LITTLE_ENDIAN |
| 48 | #define BIG_ENDIAN _BIG_ENDIAN |
| 49 | #define PDP_ENDIAN _PDP_ENDIAN |
| 50 | #define BYTE_ORDER _BYTE_ORDER |
| 51 | #endif |
| 52 | |
| 53 | #ifdef __GNUC__ |
| 54 | |
| 55 | #define __swap16gen(x) __statement({ \ |
| 56 | __uint16_t __swap16gen_x = (x); \ |
| 57 | \ |
| 58 | (__uint16_t)((__swap16gen_x & 0xff) << 8 | \ |
| 59 | (__swap16gen_x & 0xff00) >> 8); \ |
| 60 | }) |
| 61 | |
| 62 | #define __swap32gen(x) __statement({ \ |
| 63 | __uint32_t __swap32gen_x = (x); \ |
| 64 | \ |
| 65 | (__uint32_t)((__swap32gen_x & 0xff) << 24 | \ |
| 66 | (__swap32gen_x & 0xff00) << 8 | \ |
| 67 | (__swap32gen_x & 0xff0000) >> 8 | \ |
| 68 | (__swap32gen_x & 0xff000000) >> 24); \ |
| 69 | }) |
| 70 | |
| 71 | #define __swap64gen(x) __statement({ \ |
| 72 | __uint64_t __swap64gen_x = (x); \ |
| 73 | \ |
| 74 | (__uint64_t)((__swap64gen_x & 0xff) << 56 | \ |
| 75 | (__swap64gen_x & 0xff00ULL) << 40 | \ |
| 76 | (__swap64gen_x & 0xff0000ULL) << 24 | \ |
| 77 | (__swap64gen_x & 0xff000000ULL) << 8 | \ |
| 78 | (__swap64gen_x & 0xff00000000ULL) >> 8 | \ |
| 79 | (__swap64gen_x & 0xff0000000000ULL) >> 24 | \ |
| 80 | (__swap64gen_x & 0xff000000000000ULL) >> 40 | \ |
| 81 | (__swap64gen_x & 0xff00000000000000ULL) >> 56); \ |
| 82 | }) |
| 83 | |
| 84 | #else /* __GNUC__ */ |
| 85 | |
| 86 | /* Note that these macros evaluate their arguments several times. */ |
| 87 | #define __swap16gen(x) \ |
| 88 | (__uint16_t)(((__uint16_t)(x) & 0xff) << 8 | ((__uint16_t)(x) & 0xff00) >> 8) |
| 89 | |
| 90 | #define __swap32gen(x) \ |
| 91 | (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 | \ |
| 92 | ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\ |
| 93 | ((__uint32_t)(x) & 0xff000000) >> 24) |
| 94 | |
| 95 | #define __swap64gen(x) \ |
| 96 | (__uint64_t)((((__uint64_t)(x) & 0xff) << 56) | \ |
| 97 | ((__uint64_t)(x) & 0xff00ULL) << 40 | \ |
| 98 | ((__uint64_t)(x) & 0xff0000ULL) << 24 | \ |
| 99 | ((__uint64_t)(x) & 0xff000000ULL) << 8 | \ |
| 100 | ((__uint64_t)(x) & 0xff00000000ULL) >> 8 | \ |
| 101 | ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 | \ |
| 102 | ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 | \ |
| 103 | ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) |
| 104 | |
| 105 | #endif /* __GNUC__ */ |
| 106 | |
| 107 | /* |
| 108 | * Define MD_SWAP if you provide swap{16,32}md functions/macros that are |
| 109 | * optimized for your architecture, These will be used for swap{16,32} |
| 110 | * unless the argument is a constant and we are using GCC, where we can |
| 111 | * take advantage of the CSE phase much better by using the generic version. |
| 112 | */ |
| 113 | #ifdef MD_SWAP |
| 114 | #if __GNUC__ |
| 115 | |
| 116 | #define __swap16(x) __statement({ \ |
| 117 | __uint16_t __swap16_x = (x); \ |
| 118 | \ |
| 119 | __builtin_constant_p(x) ? __swap16gen(__swap16_x) : \ |
| 120 | __swap16md(__swap16_x); \ |
| 121 | }) |
| 122 | |
| 123 | #define __swap32(x) __statement({ \ |
| 124 | __uint32_t __swap32_x = (x); \ |
| 125 | \ |
| 126 | __builtin_constant_p(x) ? __swap32gen(__swap32_x) : \ |
| 127 | __swap32md(__swap32_x); \ |
| 128 | }) |
| 129 | |
| 130 | #define __swap64(x) __statement({ \ |
| 131 | __uint64_t __swap64_x = (x); \ |
| 132 | \ |
| 133 | __builtin_constant_p(x) ? __swap64gen(__swap64_x) : \ |
| 134 | __swap64md(__swap64_x); \ |
| 135 | }) |
| 136 | |
| 137 | #endif /* __GNUC__ */ |
| 138 | |
| 139 | #else /* MD_SWAP */ |
| 140 | #define __swap16 __swap16gen |
| 141 | #define __swap32 __swap32gen |
| 142 | #define __swap64 __swap64gen |
| 143 | #endif /* MD_SWAP */ |
| 144 | |
| 145 | #define __swap16_multi(v, n) do { \ |
| 146 | __size_t __swap16_multi_n = (n); \ |
| 147 | __uint16_t *__swap16_multi_v = (v); \ |
| 148 | \ |
| 149 | while (__swap16_multi_n) { \ |
| 150 | *__swap16_multi_v = swap16(*__swap16_multi_v); \ |
| 151 | __swap16_multi_v++; \ |
| 152 | __swap16_multi_n--; \ |
| 153 | } \ |
| 154 | } while (0) |
| 155 | |
| 156 | #if __BSD_VISIBLE |
| 157 | #define swap16 __swap16 |
| 158 | #define swap32 __swap32 |
| 159 | #define swap64 __swap64 |
| 160 | #define swap16_multi __swap16_multi |
| 161 | |
| 162 | __BEGIN_DECLS |
| 163 | __uint64_t htobe64(__uint64_t); |
| 164 | __uint32_t htobe32(__uint32_t); |
| 165 | __uint16_t htobe16(__uint16_t); |
| 166 | __uint64_t betoh64(__uint64_t); |
| 167 | __uint32_t betoh32(__uint32_t); |
| 168 | __uint16_t betoh16(__uint16_t); |
| 169 | |
| 170 | __uint64_t htole64(__uint64_t); |
| 171 | __uint32_t htole32(__uint32_t); |
| 172 | __uint16_t htole16(__uint16_t); |
| 173 | __uint64_t letoh64(__uint64_t); |
| 174 | __uint32_t letoh32(__uint32_t); |
| 175 | __uint16_t letoh16(__uint16_t); |
| 176 | __END_DECLS |
| 177 | #endif /* __BSD_VISIBLE */ |
| 178 | |
| 179 | #if _BYTE_ORDER == _LITTLE_ENDIAN |
| 180 | |
| 181 | /* Can be overridden by machine/endian.h before inclusion of this file. */ |
| 182 | #ifndef _QUAD_HIGHWORD |
| 183 | #define _QUAD_HIGHWORD 1 |
| 184 | #endif |
| 185 | #ifndef _QUAD_LOWWORD |
| 186 | #define _QUAD_LOWWORD 0 |
| 187 | #endif |
| 188 | |
| 189 | #if __BSD_VISIBLE |
| 190 | #define htobe16 __swap16 |
| 191 | #define htobe32 __swap32 |
| 192 | #define htobe64 __swap64 |
| 193 | #define betoh16 __swap16 |
| 194 | #define betoh32 __swap32 |
| 195 | #define betoh64 __swap64 |
| 196 | |
| 197 | #define htole16(x) (x) |
| 198 | #define htole32(x) (x) |
| 199 | #define htole64(x) (x) |
| 200 | #define letoh16(x) (x) |
| 201 | #define letoh32(x) (x) |
| 202 | #define letoh64(x) (x) |
| 203 | #endif /* __BSD_VISIBLE */ |
| 204 | |
| 205 | #define htons(x) __swap16(x) |
| 206 | #define htonl(x) __swap32(x) |
| 207 | #define ntohs(x) __swap16(x) |
| 208 | #define ntohl(x) __swap32(x) |
| 209 | |
| 210 | /* Bionic additions */ |
| 211 | #define ntohq(x) __swap64(x) |
| 212 | #define htonq(x) __swap64(x) |
| 213 | |
| 214 | #define __LITTLE_ENDIAN_BITFIELD |
| 215 | |
| 216 | #endif /* _BYTE_ORDER */ |
| 217 | |
| 218 | #if _BYTE_ORDER == _BIG_ENDIAN |
| 219 | |
| 220 | /* Can be overridden by machine/endian.h before inclusion of this file. */ |
| 221 | #ifndef _QUAD_HIGHWORD |
| 222 | #define _QUAD_HIGHWORD 0 |
| 223 | #endif |
| 224 | #ifndef _QUAD_LOWWORD |
| 225 | #define _QUAD_LOWWORD 1 |
| 226 | #endif |
| 227 | |
| 228 | #if __BSD_VISIBLE |
| 229 | #define htole16 __swap16 |
| 230 | #define htole32 __swap32 |
| 231 | #define htole64 __swap64 |
| 232 | #define letoh16 __swap16 |
| 233 | #define letoh32 __swap32 |
| 234 | #define letoh64 __swap64 |
| 235 | |
| 236 | #define htobe16(x) (x) |
| 237 | #define htobe32(x) (x) |
| 238 | #define htobe64(x) (x) |
| 239 | #define betoh16(x) (x) |
| 240 | #define betoh32(x) (x) |
| 241 | #define betoh64(x) (x) |
| 242 | #endif /* __BSD_VISIBLE */ |
| 243 | |
| 244 | #define htons(x) (x) |
| 245 | #define htonl(x) (x) |
| 246 | #define ntohs(x) (x) |
| 247 | #define ntohl(x) (x) |
| 248 | |
| 249 | /* Bionic additions */ |
| 250 | #define ntohq(x) (x) |
| 251 | #define htonq(x) (x) |
| 252 | |
| 253 | #define __BIG_ENDIAN_BITFIELD |
| 254 | |
| 255 | #endif /* _BYTE_ORDER */ |
| 256 | |
| 257 | #if __BSD_VISIBLE |
| 258 | #define NTOHL(x) (x) = ntohl((u_int32_t)(x)) |
| 259 | #define NTOHS(x) (x) = ntohs((u_int16_t)(x)) |
| 260 | #define HTONL(x) (x) = htonl((u_int32_t)(x)) |
| 261 | #define HTONS(x) (x) = htons((u_int16_t)(x)) |
| 262 | #endif |
| 263 | |
| 264 | |
| 265 | #define __BYTE_ORDER _BYTE_ORDER |
| 266 | #ifndef __LITTLE_ENDIAN |
| 267 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN |
| 268 | #endif |
| 269 | #ifndef __BIG_ENDIAN |
| 270 | #define __BIG_ENDIAN _BIG_ENDIAN |
| 271 | #endif |
| 272 | |
| 273 | #endif /* _SYS_ENDIAN_H_ */ |