Debian Codegraph @

lib/util/intcode.h

raw
1 #ifndef INTCODE_H_INCLUDED
2 #define INTCODE_H_INCLUDED
3
4 #include "pm_config.h" /* For uint32_t, BYTE_ORDER, HAVE_INT64 */
5
6 static unsigned int const pm_byteOrder = BYTE_ORDER;
7
8 typedef struct {
9 /*----------------------------------------------------------------------------
10 This is a big-endian representation of a 16 bit integer. I.e.
11 bytes[0] is the most significant 8 bits; bytes[1] is the least
12 significant 8 bits of the number in pure binary.
13
14 On a big-endian machines, this is bit for bit identical to uint16_t.
15 On a little-endian machine, it isn't.
16
17 This is an important data type because decent file formats use
18 big-endian -- they don't care if some CPU happens to use some other
19 code for its own work.
20 -----------------------------------------------------------------------------*/
21 unsigned char bytes[2];
22 } bigend16;
23
24
25 static __inline__ uint16_t
26 pm_uintFromBigend16(bigend16 const arg) {
27
28 uint16_t retval;
29
30 switch (pm_byteOrder) {
31 case BIG_ENDIAN: {
32 union {
33 bigend16 bigend;
34 uint16_t native;
35 } converter;
36 converter.bigend = arg;
37 retval = converter.native;
38 } break;
39
40 default: {
41 retval =
42 (arg.bytes[0] << 8) |
43 (arg.bytes[1] << 0);
44 }
45 }
46 return retval;
47 }
48
49
50
51 static __inline__ bigend16
52 pm_bigendFromUint16(uint16_t const arg) {
53
54 bigend16 retval;
55
56 switch (pm_byteOrder) {
57 case BIG_ENDIAN: {
58 union {
59 bigend16 bigend;
60 uint16_t native;
61 } converter;
62 converter.native = arg;
63 retval = converter.bigend;
64 } break;
65
66 default: {
67 uint16_t shift;
68 shift = arg;
69 retval.bytes[1] = shift; /* Takes lower 8 bits */
70 shift >>= 8;
71 retval.bytes[0] = shift; /* Takes lower 8 bits */
72 }
73 }
74 return retval;
75 }
76
77 typedef struct {
78 /*----------------------------------------------------------------------------
79 This is a big-endian representation of a 32 bit integer. I.e.
80 bytes[0] is the most significant 8 bits; bytes[3] is the least
81 significant 8 bits of the number in pure binary.
82
83 On a big-endian machines, this is bit for bit identical to uint32_t.
84 On a little-endian machine, it isn't.
85
86 This is an important data type because decent file formats use
87 big-endian -- they don't care if some CPU happens to use some other
88 code for its own work.
89 -----------------------------------------------------------------------------*/
90 unsigned char bytes[4];
91 } bigend32;
92
93
94 static __inline__ uint32_t
95 pm_uintFromBigend32(bigend32 const arg) {
96
97 uint32_t retval;
98
99 switch (pm_byteOrder) {
100 case BIG_ENDIAN: {
101 union {
102 bigend32 bigend;
103 uint32_t native;
104 } converter;
105 converter.bigend = arg;
106 retval = converter.native;
107 } break;
108
109 #if HAVE_GCC_BSWAP
110 case LITTLE_ENDIAN: {
111 /* Use GCC built-in */
112 union {
113 bigend32 bigend;
114 uint32_t native;
115 } converter;
116 converter.bigend = arg;
117 retval = __builtin_bswap32(converter.native);
118 } break;
119 #endif
120
121 default:
122 retval =
123 (arg.bytes[0] << 24) |
124 (arg.bytes[1] << 16) |
125 (arg.bytes[2] << 8) |
126 (arg.bytes[3] << 0);
127 }
128
129 return retval;
130 }
131
132
133
134 static __inline__ bigend32
135 pm_bigendFromUint32(uint32_t const arg) {
136
137 bigend32 retval;
138
139 switch (pm_byteOrder) {
140 case BIG_ENDIAN: {
141 union {
142 bigend32 bigend;
143 uint32_t native;
144 } converter;
145 converter.native = arg;
146 retval = converter.bigend;
147 } break;
148
149 #if HAVE_GCC_BSWAP
150 case LITTLE_ENDIAN: {
151 /* Use GCC built-in */
152 union {
153 bigend32 bigend;
154 uint32_t native;
155 } converter;
156 converter.native = __builtin_bswap32(arg);
157 retval = converter.bigend;
158 } break;
159 #endif
160
161 default: {
162 uint32_t shift;
163 shift = arg;
164 retval.bytes[3] = shift; /* Takes lower 8 bits */
165 shift >>= 8;
166 retval.bytes[2] = shift; /* Takes lower 8 bits */
167 shift >>= 8;
168 retval.bytes[1] = shift; /* Takes lower 8 bits */
169 shift >>= 8;
170 retval.bytes[0] = shift; /* Takes lower 8 bits */
171 }
172 }
173 return retval;
174 }
175
176
177 #if HAVE_INT64
178
179 typedef struct {
180 /*----------------------------------------------------------------------------
181 This is a big-endian representation of a 64 bit integer. I.e.
182 bytes[0] is the most significant 8 bits; bytes[7] is the least
183 significant 8 bits of the number in pure binary.
184
185 On a big-endian machines, this is bit for bit identical to uint64_t.
186 On a little-endian machine, it isn't.
187
188 This is an important data type because decent file formats use
189 big-endian -- they don't care if some CPU happens to use some other
190 code for its own work.
191 -----------------------------------------------------------------------------*/
192 unsigned char bytes[8];
193 } bigend64;
194
195
196 static __inline__ uint64_t
197 pm_uintFromBigend64(bigend64 const arg) {
198
199 uint64_t retval;
200
201 switch (pm_byteOrder) {
202 case BIG_ENDIAN: {
203 union {
204 bigend64 bigend;
205 uint64_t native;
206 } converter;
207 converter.bigend = arg;
208 retval = converter.native;
209 } break;
210
211 #if HAVE_GCC_BSWAP
212 case LITTLE_ENDIAN: {
213 /* Use GCC built-in */
214 union {
215 bigend64 bigend;
216 uint64_t native;
217 } converter;
218 converter.bigend = arg;
219 retval = __builtin_bswap64(converter.native);
220 } break;
221 #endif
222 default:
223 retval =
224 ((uint64_t)arg.bytes[0] << 56) | ((uint64_t)arg.bytes[1] << 48) |
225 ((uint64_t)arg.bytes[2] << 40) | ((uint64_t)arg.bytes[3] << 32) |
226 ((uint64_t)arg.bytes[4] << 24) | ((uint64_t)arg.bytes[5] << 16) |
227 ((uint64_t)arg.bytes[6] << 8) | ((uint64_t)arg.bytes[7] << 0);
228 }
229 return retval;
230 }
231
232
233
234 static __inline__ bigend64
235 pm_bigendFromUint64(uint64_t const arg) {
236
237 bigend64 retval;
238
239 switch (pm_byteOrder) {
240 case BIG_ENDIAN: {
241 union {
242 bigend64 bigend;
243 uint64_t native;
244 } converter;
245 converter.native = arg;
246 retval = converter.bigend;
247 } break;
248
249 #if HAVE_GCC_BSWAP
250 case LITTLE_ENDIAN: {
251
252 /* Use GCC built-in */
253 union {
254 bigend64 bigend;
255 uint64_t native;
256 } converter;
257 converter.native = __builtin_bswap64(arg);
258 retval = converter.bigend;
259 } break;
260 #endif
261
262 default: {
263 uint64_t shift;
264 shift = arg;
265 retval.bytes[7] = shift; /* Takes lower 8 bits */
266 shift >>= 8;
267 retval.bytes[6] = shift; /* Takes lower 8 bits */
268 shift >>= 8;
269 retval.bytes[5] = shift; /* Takes lower 8 bits */
270 shift >>= 8;
271 retval.bytes[4] = shift; /* Takes lower 8 bits */
272 shift >>= 8;
273 retval.bytes[3] = shift; /* Takes lower 8 bits */
274 shift >>= 8;
275 retval.bytes[2] = shift; /* Takes lower 8 bits */
276 shift >>= 8;
277 retval.bytes[1] = shift; /* Takes lower 8 bits */
278 shift >>= 8;
279 retval.bytes[0] = shift; /* Takes lower 8 bits */
280 }
281 }
282 return retval;
283 }
284
285 #endif /* HAVE_INT64 */
286
287 #endif
288
289
290
291
292