|
| 1 | +/** |
| 2 | + * Copyright (c) 2012, 2013, 2014 Gil Tene |
| 3 | + * Copyright (c) 2014 Michael Barker |
| 4 | + * Copyright (c) 2014 Matt Warren |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND 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 COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 27 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#include <sys/isa_defs.h> |
| 31 | +# include <sys/byteorder.h> |
| 32 | + |
| 33 | +# define betoh16(x) BE_16(x) |
| 34 | +# define letoh16(x) LE_16(x) |
| 35 | +# define betoh32(x) BE_32(x) |
| 36 | +# define letoh32(x) LE_32(x) |
| 37 | +# define betoh64(x) BE_64(x) |
| 38 | +# define letoh64(x) LE_64(x) |
| 39 | +#define htobe16(x) BE_16(x) |
| 40 | +#define be16toh(x) BE_16(x) |
| 41 | +#define htobe32(x) BE_32(x) |
| 42 | +#define be32toh(x) BE_32(x) |
| 43 | +#define htobe64(x) BE_64(x) |
| 44 | +#define be64toh(x) BE_64(x) |
| 45 | +// Solaris defines endian by setting _LITTLE_ENDIAN or _BIG_ENDIAN |
| 46 | +# ifdef _BIG_ENDIAN |
| 47 | +# define IS_BIG_ENDIAN |
| 48 | +# endif |
| 49 | +# ifdef _LITTLE_ENDIAN |
| 50 | +# define IS_LITTLE_ENDIAN |
| 51 | +# endif |
| 52 | +// Make sure we got some kind of endian (but not both) |
| 53 | +#if defined(IS_BIG_ENDIAN) == defined(IS_LITTLE_ENDIAN) |
| 54 | +# error "Failed to get endian type for this system" |
| 55 | +#endif |
| 56 | + |
| 57 | +// Define bswap functions if we didn't get any from the system headers |
| 58 | +#ifndef BSWAP_16 |
| 59 | +# define BSWAP_16(x) ( \ |
| 60 | + ((uint16_t)(x) & 0x00ffU) << 8 | \ |
| 61 | + ((uint16_t)(x) & 0xff00U) >> 8) |
| 62 | +#endif |
| 63 | +#ifndef BSWAP_32 |
| 64 | +# define BSWAP_32(x) ( \ |
| 65 | + ((uint32_t)(x) & 0x000000ffU) << 24 | \ |
| 66 | + ((uint32_t)(x) & 0x0000ff00U) << 8 | \ |
| 67 | + ((uint32_t)(x) & 0x00ff0000U) >> 8 | \ |
| 68 | + ((uint32_t)(x) & 0xff000000U) >> 24) |
| 69 | +#endif |
| 70 | +#ifndef BSWAP_64 |
| 71 | +# define BSWAP_64(x) ( \ |
| 72 | + ((uint64_t)(x) & 0x00000000000000ffULL) << 56 | \ |
| 73 | + ((uint64_t)(x) & 0x000000000000ff00ULL) << 40 | \ |
| 74 | + ((uint64_t)(x) & 0x0000000000ff0000ULL) << 24 | \ |
| 75 | + ((uint64_t)(x) & 0x00000000ff000000ULL) << 8 | \ |
| 76 | + ((uint64_t)(x) & 0x000000ff00000000ULL) >> 8 | \ |
| 77 | + ((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24 | \ |
| 78 | + ((uint64_t)(x) & 0x00ff000000000000ULL) >> 40 | \ |
| 79 | + ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) |
| 80 | +#endif |
| 81 | + |
| 82 | +// Define conversion functions if we didn't get any from the system headers |
| 83 | +#ifndef BE_16 |
| 84 | +// Big endian system, swap when converting to/from little endian |
| 85 | +# if defined IS_BIG_ENDIAN |
| 86 | +# define BE_16(x) (x) |
| 87 | +# define BE_32(x) (x) |
| 88 | +# define BE_64(x) (x) |
| 89 | +# define LE_16(x) BSWAP_16(x) |
| 90 | +# define LE_32(x) BSWAP_32(x) |
| 91 | +# define LE_64(x) BSWAP_64(x) |
| 92 | +// Little endian system, swap when converting to/from big endian |
| 93 | +# elif defined IS_LITTLE_ENDIAN |
| 94 | +# define BE_16(x) BSWAP_16(x) |
| 95 | +# define BE_32(x) BSWAP_32(x) |
| 96 | +# define BE_64(x) BSWAP_64(x) |
| 97 | +# define LE_16(x) (x) |
| 98 | +# define LE_32(x) (x) |
| 99 | +# define LE_64(x) (x) |
| 100 | +#endif |
| 101 | +#endif |
0 commit comments