| 1 | // SPDX-License-Identifier: MIT |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2012,2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This header defines the following endian macros based on the current |
| 9 | * platform endian headers: |
| 10 | * |
| 11 | * BYTE_ORDER this macro shall have a value equal to one |
| 12 | * of the *_ENDIAN macros in this header. |
| 13 | * FLOAT_WORD_ORDER this macro shall have a value equal to one |
| 14 | * of the *_ENDIAN macros in this header. |
| 15 | * LITTLE_ENDIAN if BYTE_ORDER == LITTLE_ENDIAN, the host |
| 16 | * byte order is from least significant to |
| 17 | * most significant. |
| 18 | * BIG_ENDIAN if BYTE_ORDER == BIG_ENDIAN, the host byte |
| 19 | * order is from most significant to least |
| 20 | * significant. |
| 21 | * |
| 22 | * Direct byte swapping interfaces: |
| 23 | * |
| 24 | * uint16_t bswap_16(uint16_t x); (* swap bytes 16-bit word *) |
| 25 | * uint32_t bswap_32(uint32_t x); (* swap bytes 32-bit word *) |
| 26 | * uint64_t bswap_64(uint32_t x); (* swap bytes 64-bit word *) |
| 27 | */ |
| 28 | |
| 29 | #ifndef _SIDE_ENDIAN_H |
| 30 | #define _SIDE_ENDIAN_H |
| 31 | |
| 32 | #include <math.h> |
| 33 | |
| 34 | #if defined(__SIZEOF_LONG__) |
| 35 | # define SIDE_BITS_PER_LONG (__SIZEOF_LONG__ * 8) |
| 36 | #elif defined(_LP64) |
| 37 | # define SIDE_BITS_PER_LONG 64 |
| 38 | #else |
| 39 | # define SIDE_BITS_PER_LONG 32 |
| 40 | #endif |
| 41 | |
| 42 | #if (defined(__linux__) || defined(__CYGWIN__)) |
| 43 | #include <endian.h> |
| 44 | #include <byteswap.h> |
| 45 | |
| 46 | #define side_bswap_16(x) bswap_16(x) |
| 47 | #define side_bswap_32(x) bswap_32(x) |
| 48 | #define side_bswap_64(x) bswap_64(x) |
| 49 | |
| 50 | #define SIDE_BYTE_ORDER __BYTE_ORDER |
| 51 | #define SIDE_LITTLE_ENDIAN __LITTLE_ENDIAN |
| 52 | #define SIDE_BIG_ENDIAN __BIG_ENDIAN |
| 53 | |
| 54 | #ifdef __FLOAT_WORD_ORDER |
| 55 | #define SIDE_FLOAT_WORD_ORDER __FLOAT_WORD_ORDER |
| 56 | #else /* __FLOAT_WORD_ORDER */ |
| 57 | #define SIDE_FLOAT_WORD_ORDER __BYTE_ORDER |
| 58 | #endif /* __FLOAT_WORD_ORDER */ |
| 59 | |
| 60 | #elif defined(__FreeBSD__) |
| 61 | |
| 62 | #include <sys/endian.h> |
| 63 | |
| 64 | #define side_bswap_16(x) bswap16(x) |
| 65 | #define side_bswap_32(x) bswap32(x) |
| 66 | #define side_bswap_64(x) bswap64(x) |
| 67 | |
| 68 | #define SIDE_BYTE_ORDER BYTE_ORDER |
| 69 | #define SIDE_LITTLE_ENDIAN LITTLE_ENDIAN |
| 70 | #define SIDE_BIG_ENDIAN BIG_ENDIAN |
| 71 | #define SIDE_FLOAT_WORD_ORDER BYTE_ORDER |
| 72 | |
| 73 | #else |
| 74 | #error "Please add support for your OS." |
| 75 | #endif |
| 76 | |
| 77 | #ifdef __cplusplus |
| 78 | extern "C" { |
| 79 | #endif |
| 80 | |
| 81 | #ifdef __HAVE_FLOAT128 |
| 82 | static inline |
| 83 | void side_bswap_128p(char *p) |
| 84 | { |
| 85 | int i; |
| 86 | |
| 87 | for (i = 0; i < 8; i++) |
| 88 | p[i] = p[15 - i]; |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #ifdef __cplusplus |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #if SIDE_BITS_PER_LONG == 64 |
| 97 | # define side_bswap_pointer(x) side_bswap_64(x) |
| 98 | #else |
| 99 | # define side_bswap_pointer(x) side_bswap_32(x) |
| 100 | #endif |
| 101 | |
| 102 | #endif /* _SIDE_ENDIAN_H */ |