Mercurial > hg > th-libs
annotate th_endian.h @ 129:aa2d608fb3f3
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 22 Jun 2014 07:23:54 +0300 |
parents | 34d58b0f2d52 |
children | 4094fcfd4783 |
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 |
66 | 4 * (C) Copyright 2002-2012 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 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
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 #include <stdio.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
5 | 17 #ifdef __cplusplus |
18 extern "C" { | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
52 | 21 /* Check endianess |
22 */ | |
23 #ifndef TH_BYTEORDER | |
24 # error Undefined byteorder (TH_BYTEORDER not set.) | |
25 #endif | |
26 | |
27 #define TH_BIG_ENDIAN 1234 | |
28 #define TH_LITTLE_ENDIAN 4321 | |
29 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 |
5 | 31 /* Endianess swapping macros |
32 */ | |
10 | 33 #define TH_SWAP_16_LE_BE(value) ((uint16_t) ( \ |
5 | 34 (uint16_t) ((uint16_t) (value) >> 8) | \ |
35 (uint16_t) ((uint16_t) (value) << 8)) ) | |
36 | |
37 | |
38 #define TH_SWAP_32_LE_BE(value) ((uint32_t) ( \ | |
39 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \ | |
40 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \ | |
41 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \ | |
42 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24))) | |
43 | |
44 #ifdef TH_HAVE_64BIT | |
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))) | |
54 #endif | |
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 #ifdef TH_HAVE_64BIT | |
71 #define TH_LE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value) | |
72 #define TH_NATIVE_TO_LE64(value) TH_SWAP_64_LE_BE(value) | |
73 #define TH_BE64_TO_NATIVE(value) ((uint64_t) (value)) | |
74 #define TH_NATIVE_TO_BE64(value) ((uint64_t) (value)) | |
75 #endif | |
76 | |
129 | 77 // !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
|
78 #elif (TH_BYTEORDER == TH_LITTLE_ENDIAN) |
5 | 79 |
80 #define TH_LE16_TO_NATIVE(value) ((uint16_t) (value)) | |
81 #define TH_LE32_TO_NATIVE(value) ((uint32_t) (value)) | |
82 #define TH_NATIVE_TO_LE16(value) ((uint16_t) (value)) | |
83 #define TH_NATIVE_TO_LE32(value) ((uint32_t) (value)) | |
84 | |
85 #define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value) | |
86 #define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value) | |
87 #define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value) | |
88 #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
|
89 |
5 | 90 #ifdef TH_HAVE_64BIT |
91 #define TH_LE64_TO_NATIVE(value) ((uint64_t) (value)) | |
92 #define TH_NATIVE_TO_LE64(value) ((uint64_t) (value)) | |
93 #define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value) | |
94 #define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value) | |
95 #endif | |
96 | |
51
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
97 #else |
1607c0691dee
Change byteorder support. Now TH_BYTEORDER must be defined to either
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
98 # error Unsupported byte order! |
5 | 99 #endif |
100 | |
101 | |
102 /* Endian-handling file read/write routines | |
103 */ | |
104 BOOL th_read_str(FILE *f, uint8_t *s, size_t l); | |
105 BOOL th_write_str(FILE *f, uint8_t *s, size_t l); | |
63
5ab4c28b85cd
Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
106 BOOL th_read_byte(FILE *f, uint8_t *b); |
5ab4c28b85cd
Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
107 BOOL th_write_byte(FILE *f, uint8_t b); |
5 | 108 |
109 #define TH_DEFINE_HEADER(xname, xtype) \ | |
110 BOOL th_read_ ## xname (FILE *f, xtype *v); \ | |
111 BOOL th_write_ ## xname (FILE *f, xtype v); | |
112 | |
113 TH_DEFINE_HEADER(le16, uint16_t) | |
114 TH_DEFINE_HEADER(le32, uint32_t) | |
115 | |
116 TH_DEFINE_HEADER(be16, uint16_t) | |
117 TH_DEFINE_HEADER(be32, uint32_t) | |
118 | |
119 #ifdef TH_HAVE_64BIT | |
120 TH_DEFINE_HEADER(be64, uint64_t) | |
121 TH_DEFINE_HEADER(le64, uint64_t) | |
122 #endif | |
123 | |
124 #undef TH_DEFINE_HEADER | |
125 | |
129 | 126 // Cause warnings for old functions |
5 | 127 #define TH_READ_LE16 fdksajlkfdsljf lol |
128 #define TH_READ_LE32 fdksajlkfdsljf lol | |
129 #define TH_READ_BE16 fdksajlkfdsljf lol | |
130 #define TH_READ_BE32 fdksajlkfdsljf lol | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 #endif |
129 | 135 #endif // TH_ENDIAN_H |