Mercurial > hg > th-libs
annotate th_endian.h @ 201:e6a278f140c4
Remove file helper functions, they will be reintroduced in another form in new I/O contexts.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 11 Feb 2016 21:38:52 +0200 |
parents | d80916aada78 |
children | f99fceb09707 |
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 |
144 | 4 * (C) Copyright 2002-2015 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 | 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 | 16 #ifdef __cplusplus |
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 | 20 /* Check endianess |
21 */ | |
22 #ifndef TH_BYTEORDER | |
23 # error Undefined byteorder (TH_BYTEORDER not set.) | |
24 #endif | |
25 | |
26 #define TH_BIG_ENDIAN 1234 | |
27 #define TH_LITTLE_ENDIAN 4321 | |
28 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 |
5 | 30 /* Endianess swapping macros |
31 */ | |
10 | 32 #define TH_SWAP_16_LE_BE(value) ((uint16_t) ( \ |
5 | 33 (uint16_t) ((uint16_t) (value) >> 8) | \ |
34 (uint16_t) ((uint16_t) (value) << 8)) ) | |
35 | |
36 | |
37 #define TH_SWAP_32_LE_BE(value) ((uint32_t) ( \ | |
38 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \ | |
39 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \ | |
40 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \ | |
41 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24))) | |
42 | |
43 #ifdef TH_HAVE_64BIT | |
44 #define TH_SWAP_64_LE_BE(value) ((uint64_t) ( \ | |
45 (((uint64_t) (value) & (uint64_t) 0x00000000000000ffULL) << 56) | \ | |
46 (((uint64_t) (value) & (uint64_t) 0x000000000000ff00ULL) << 40) | \ | |
47 (((uint64_t) (value) & (uint64_t) 0x0000000000ff0000ULL) << 24) | \ | |
48 (((uint64_t) (value) & (uint64_t) 0x00000000ff000000ULL) << 8) | \ | |
49 (((uint64_t) (value) & (uint64_t) 0x000000ff00000000ULL) >> 8) | \ | |
50 (((uint64_t) (value) & (uint64_t) 0x0000ff0000000000ULL) >> 24) | \ | |
51 (((uint64_t) (value) & (uint64_t) 0x00ff000000000000ULL) >> 40) | \ | |
52 (((uint64_t) (value) & (uint64_t) 0xff00000000000000ULL) >> 56))) | |
53 #endif | |
54 | |
55 /* 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
|
56 */ |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
57 #if (TH_BYTEORDER == TH_BIG_ENDIAN) |
5 | 58 |
59 #define TH_LE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value) | |
60 #define TH_LE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value) | |
61 #define TH_NATIVE_TO_LE16(value) TH_SWAP_16_LE_BE(value) | |
62 #define TH_NATIVE_TO_LE32(value) TH_SWAP_32_LE_BE(value) | |
63 | |
64 #define TH_BE16_TO_NATIVE(value) ((uint16_t) (value)) | |
65 #define TH_BE32_TO_NATIVE(value) ((uint32_t) (value)) | |
66 #define TH_NATIVE_TO_BE16(value) ((uint16_t) (value)) | |
67 #define TH_NATIVE_TO_BE32(value) ((uint32_t) (value)) | |
68 | |
69 #ifdef TH_HAVE_64BIT | |
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 #endif | |
75 | |
129 | 76 // !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
|
77 #elif (TH_BYTEORDER == TH_LITTLE_ENDIAN) |
5 | 78 |
79 #define TH_LE16_TO_NATIVE(value) ((uint16_t) (value)) | |
80 #define TH_LE32_TO_NATIVE(value) ((uint32_t) (value)) | |
81 #define TH_NATIVE_TO_LE16(value) ((uint16_t) (value)) | |
82 #define TH_NATIVE_TO_LE32(value) ((uint32_t) (value)) | |
83 | |
84 #define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value) | |
85 #define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value) | |
86 #define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value) | |
87 #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
|
88 |
5 | 89 #ifdef TH_HAVE_64BIT |
90 #define TH_LE64_TO_NATIVE(value) ((uint64_t) (value)) | |
91 #define TH_NATIVE_TO_LE64(value) ((uint64_t) (value)) | |
92 #define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value) | |
93 #define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value) | |
94 #endif | |
95 | |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
96 #else |
200 | 97 # error Unsupported byte order! |
5 | 98 #endif |
99 | |
100 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 #endif |
129 | 104 #endif // TH_ENDIAN_H |