annotate th_endian.h @ 454:347bfd3e017e

Bump copyright years.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Jan 2018 22:57:02 +0200
parents 1b3472ba7b23
children e4ce60239d16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
454
347bfd3e017e Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
4 * (C) Copyright 2002-2018 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 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
8 #ifndef TH_ENDIAN_H
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
9 #define TH_ENDIAN_H
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #ifdef HAVE_CONFIG_H
180
06a375df1787 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 179
diff changeset
12 # include "config.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #endif
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
14 #include "th_types.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
16 #ifdef __cplusplus
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 extern "C" {
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
52
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
20 /* Check endianess
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
21 */
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
22 #ifndef TH_BYTEORDER
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
23 # error Undefined byteorder (TH_BYTEORDER not set.)
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
24 #endif
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
25
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
26 #define TH_BIG_ENDIAN 1234
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
27 #define TH_LITTLE_ENDIAN 4321
2a452b142301 Byteorder changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
28
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
30 /* Endianess swapping macros
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
31 */
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
32 #define TH_SWAP_16_LE_BE(value) ((uint16_t) ( \
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
33 (uint16_t) ((uint16_t) (value) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
34 (uint16_t) ((uint16_t) (value) << 8)) )
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
35
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
36
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
37 #define TH_SWAP_32_LE_BE(value) ((uint32_t) ( \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
38 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
39 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
40 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
41 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24)))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
42
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
43 #define TH_SWAP_64_LE_BE(value) ((uint64_t) ( \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
44 (((uint64_t) (value) & (uint64_t) 0x00000000000000ffULL) << 56) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 (((uint64_t) (value) & (uint64_t) 0x000000000000ff00ULL) << 40) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
46 (((uint64_t) (value) & (uint64_t) 0x0000000000ff0000ULL) << 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
47 (((uint64_t) (value) & (uint64_t) 0x00000000ff000000ULL) << 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
48 (((uint64_t) (value) & (uint64_t) 0x000000ff00000000ULL) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49 (((uint64_t) (value) & (uint64_t) 0x0000ff0000000000ULL) >> 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50 (((uint64_t) (value) & (uint64_t) 0x00ff000000000000ULL) >> 40) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
51 (((uint64_t) (value) & (uint64_t) 0xff00000000000000ULL) >> 56)))
212
f99fceb09707 Get rid of TH_HAVE_64BIT.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
52
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 /* 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
55 */
51
1607c0691dee Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
56 #if (TH_BYTEORDER == TH_BIG_ENDIAN)
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
57
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
58 #define TH_LE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
59 #define TH_LE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
60 #define TH_NATIVE_TO_LE16(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
61 #define TH_NATIVE_TO_LE32(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
62
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63 #define TH_BE16_TO_NATIVE(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 #define TH_BE32_TO_NATIVE(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
65 #define TH_NATIVE_TO_BE16(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
66 #define TH_NATIVE_TO_BE32(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
67
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
68 #define TH_LE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
69 #define TH_NATIVE_TO_LE64(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
70 #define TH_BE64_TO_NATIVE(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
71 #define TH_NATIVE_TO_BE64(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
72
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
73 // !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
74 #elif (TH_BYTEORDER == TH_LITTLE_ENDIAN)
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
75
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
76 #define TH_LE16_TO_NATIVE(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
77 #define TH_LE32_TO_NATIVE(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
78 #define TH_NATIVE_TO_LE16(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
79 #define TH_NATIVE_TO_LE32(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
80
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
81 #define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
82 #define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
83 #define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
84 #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
85
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
86 #define TH_LE64_TO_NATIVE(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
87 #define TH_NATIVE_TO_LE64(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
88 #define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
89 #define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
90
51
1607c0691dee Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
91 #else
200
d80916aada78 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 196
diff changeset
92 # error Unsupported byte order!
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
93 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
94
213
592bf6da0f1f Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
95 //
216
4fee16cb28c1 Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents: 214
diff changeset
96 // 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
97 // Provided for completeness
592bf6da0f1f Add native to / from he* "conversion" macros, which do nothing.
Matti Hamalainen <ccr@tnsp.org>
parents: 212
diff changeset
98 //
216
4fee16cb28c1 Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents: 214
diff changeset
99 #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
100 #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
101 #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
102
216
4fee16cb28c1 Meh, rename he/HE to ne/NE. Native vs Host.
Matti Hamalainen <ccr@tnsp.org>
parents: 214
diff changeset
103 #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
104 #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
105 #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
106
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
107
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 #ifdef __cplusplus
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 #endif
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
111 #endif // TH_ENDIAN_H