Mercurial > hg > th-libs
annotate th_endian.h @ 726:29e44a58bc73
Bump copyrights.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 01 Jan 2022 16:17:31 +0200 |
parents | 4ca6a3b30fe8 |
children |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Endianess handling |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
726 | 4 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
622
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
8 /// @file |
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
9 /// @brief Endianess handling functionality for data type conversions, etc. |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
10 #ifndef TH_ENDIAN_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
11 #define TH_ENDIAN_H |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 #ifdef HAVE_CONFIG_H |
180 | 14 # include "config.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 #endif |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
16 #include "th_types.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 |
5 | 18 #ifdef __cplusplus |
19 extern "C" { | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
52 | 22 /* Check endianess |
23 */ | |
24 #ifndef TH_BYTEORDER | |
25 # error Undefined byteorder (TH_BYTEORDER not set.) | |
26 #endif | |
27 | |
28 #define TH_BIG_ENDIAN 1234 | |
29 #define TH_LITTLE_ENDIAN 4321 | |
30 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 |
5 | 32 /* Endianess swapping macros |
33 */ | |
10 | 34 #define TH_SWAP_16_LE_BE(value) ((uint16_t) ( \ |
5 | 35 (uint16_t) ((uint16_t) (value) >> 8) | \ |
36 (uint16_t) ((uint16_t) (value) << 8)) ) | |
37 | |
38 | |
39 #define TH_SWAP_32_LE_BE(value) ((uint32_t) ( \ | |
40 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \ | |
41 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \ | |
42 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \ | |
43 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24))) | |
44 | |
45 #define TH_SWAP_64_LE_BE(value) ((uint64_t) ( \ | |
46 (((uint64_t) (value) & (uint64_t) 0x00000000000000ffULL) << 56) | \ | |
47 (((uint64_t) (value) & (uint64_t) 0x000000000000ff00ULL) << 40) | \ | |
48 (((uint64_t) (value) & (uint64_t) 0x0000000000ff0000ULL) << 24) | \ | |
49 (((uint64_t) (value) & (uint64_t) 0x00000000ff000000ULL) << 8) | \ | |
50 (((uint64_t) (value) & (uint64_t) 0x000000ff00000000ULL) >> 8) | \ | |
51 (((uint64_t) (value) & (uint64_t) 0x0000ff0000000000ULL) >> 24) | \ | |
52 (((uint64_t) (value) & (uint64_t) 0x00ff000000000000ULL) >> 40) | \ | |
53 (((uint64_t) (value) & (uint64_t) 0xff00000000000000ULL) >> 56))) | |
212 | 54 |
5 | 55 |
56 /* Macros that swap only when needed ... | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 */ |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
58 #if (TH_BYTEORDER == TH_BIG_ENDIAN) |
5 | 59 |
60 #define TH_LE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value) | |
61 #define TH_LE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value) | |
62 #define TH_NATIVE_TO_LE16(value) TH_SWAP_16_LE_BE(value) | |
63 #define TH_NATIVE_TO_LE32(value) TH_SWAP_32_LE_BE(value) | |
64 | |
65 #define TH_BE16_TO_NATIVE(value) ((uint16_t) (value)) | |
66 #define TH_BE32_TO_NATIVE(value) ((uint32_t) (value)) | |
67 #define TH_NATIVE_TO_BE16(value) ((uint16_t) (value)) | |
68 #define TH_NATIVE_TO_BE32(value) ((uint32_t) (value)) | |
69 | |
70 #define TH_LE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value) | |
71 #define TH_NATIVE_TO_LE64(value) TH_SWAP_64_LE_BE(value) | |
72 #define TH_BE64_TO_NATIVE(value) ((uint64_t) (value)) | |
73 #define TH_NATIVE_TO_BE64(value) ((uint64_t) (value)) | |
74 | |
129 | 75 // !TH_BIG_ENDIAN |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
76 #elif (TH_BYTEORDER == TH_LITTLE_ENDIAN) |
5 | 77 |
78 #define TH_LE16_TO_NATIVE(value) ((uint16_t) (value)) | |
79 #define TH_LE32_TO_NATIVE(value) ((uint32_t) (value)) | |
80 #define TH_NATIVE_TO_LE16(value) ((uint16_t) (value)) | |
81 #define TH_NATIVE_TO_LE32(value) ((uint32_t) (value)) | |
82 | |
83 #define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value) | |
84 #define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value) | |
85 #define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value) | |
86 #define TH_NATIVE_TO_BE32(value) TH_SWAP_32_LE_BE(value) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 |
5 | 88 #define TH_LE64_TO_NATIVE(value) ((uint64_t) (value)) |
89 #define TH_NATIVE_TO_LE64(value) ((uint64_t) (value)) | |
90 #define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value) | |
91 #define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value) | |
92 | |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
93 #else |
200 | 94 # error Unsupported byte order! |
5 | 95 #endif |
96 | |
213
592bf6da0f1f
Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
97 // |
216
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
98 // NE = Native Endian, aka same as native |
213
592bf6da0f1f
Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
99 // Provided for completeness |
592bf6da0f1f
Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
100 // |
216
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
101 #define TH_NE16_TO_NATIVE(value) (value) |
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
102 #define TH_NE32_TO_NATIVE(value) (value) |
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
103 #define TH_NE64_TO_NATIVE(value) (value) |
213
592bf6da0f1f
Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
104 |
216
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
105 #define TH_NATIVE_TO_NE16(value) (value) |
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
106 #define TH_NATIVE_TO_NE32(value) (value) |
4fee16cb28c1
Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents:
214
diff
changeset
|
107 #define TH_NATIVE_TO_NE64(value) (value) |
213
592bf6da0f1f
Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
108 |
5 | 109 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 #endif |
129 | 113 #endif // TH_ENDIAN_H |