Commit | Line | Data |
---|---|---|
8bdd5c12 MD |
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 | ||
d04d4903 MD |
29 | #ifndef _TGIF_ENDIAN_H |
30 | #define _TGIF_ENDIAN_H | |
8bdd5c12 | 31 | |
d04d4903 | 32 | #include <tgif/macros.h> |
8bdd5c12 MD |
33 | #include <math.h> |
34 | ||
35 | #if (defined(__linux__) || defined(__CYGWIN__)) | |
36 | #include <endian.h> | |
37 | #include <byteswap.h> | |
38 | ||
d04d4903 MD |
39 | #define tgif_bswap_16(x) bswap_16(x) |
40 | #define tgif_bswap_32(x) bswap_32(x) | |
41 | #define tgif_bswap_64(x) bswap_64(x) | |
8bdd5c12 | 42 | |
d04d4903 MD |
43 | #define TGIF_BYTE_ORDER __BYTE_ORDER |
44 | #define TGIF_LITTLE_ENDIAN __LITTLE_ENDIAN | |
45 | #define TGIF_BIG_ENDIAN __BIG_ENDIAN | |
8bdd5c12 MD |
46 | |
47 | #ifdef __FLOAT_WORD_ORDER | |
d04d4903 | 48 | #define TGIF_FLOAT_WORD_ORDER __FLOAT_WORD_ORDER |
8bdd5c12 | 49 | #else /* __FLOAT_WORD_ORDER */ |
d04d4903 | 50 | #define TGIF_FLOAT_WORD_ORDER __BYTE_ORDER |
8bdd5c12 MD |
51 | #endif /* __FLOAT_WORD_ORDER */ |
52 | ||
53 | #elif defined(__FreeBSD__) | |
54 | ||
55 | #include <sys/endian.h> | |
56 | ||
d04d4903 MD |
57 | #define tgif_bswap_16(x) bswap16(x) |
58 | #define tgif_bswap_32(x) bswap32(x) | |
59 | #define tgif_bswap_64(x) bswap64(x) | |
8bdd5c12 | 60 | |
d04d4903 MD |
61 | #define TGIF_BYTE_ORDER BYTE_ORDER |
62 | #define TGIF_LITTLE_ENDIAN LITTLE_ENDIAN | |
63 | #define TGIF_BIG_ENDIAN BIG_ENDIAN | |
64 | #define TGIF_FLOAT_WORD_ORDER BYTE_ORDER | |
8bdd5c12 MD |
65 | |
66 | #else | |
67 | #error "Please add support for your OS." | |
68 | #endif | |
69 | ||
084105d5 MD |
70 | #ifdef __cplusplus |
71 | extern "C" { | |
72 | #endif | |
73 | ||
8bdd5c12 MD |
74 | #ifdef __HAVE_FLOAT128 |
75 | static inline | |
d04d4903 | 76 | void tgif_bswap_128p(char *p) |
8bdd5c12 MD |
77 | { |
78 | int i; | |
79 | ||
80 | for (i = 0; i < 8; i++) | |
81 | p[i] = p[15 - i]; | |
82 | } | |
83 | #endif | |
84 | ||
084105d5 MD |
85 | #ifdef __cplusplus |
86 | } | |
87 | #endif | |
88 | ||
d04d4903 MD |
89 | #if TGIF_BITS_PER_LONG == 64 |
90 | # define tgif_bswap_pointer(x) tgif_bswap_64(x) | |
f5e650d7 | 91 | #else |
d04d4903 | 92 | # define tgif_bswap_pointer(x) tgif_bswap_32(x) |
f5e650d7 MD |
93 | #endif |
94 | ||
d04d4903 | 95 | #endif /* _TGIF_ENDIAN_H */ |