Skip to content

Commit 56d6002

Browse files
authored
Fix tcc build, remove PACK macro (#271)
There was no definition of the macro for compilers that were not gcc, clang or msvc. While it would be easy to add one, a better approach is to switch to memcpy() and avoid type punning altogether. Fixes: #270
1 parent b37627d commit 56d6002

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

cutils.h

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define CUTILS_H
2727

2828
#include <stdlib.h>
29+
#include <string.h>
2930
#include <inttypes.h>
3031

3132
/* set if CPU is big endian */
@@ -82,14 +83,6 @@ static void *__builtin_frame_address(unsigned int level) {
8283
# define FORMAT_STRING(p) p
8384
#endif /* _MSC_VER */
8485

85-
// https://stackoverflow.com/a/3312896
86-
// https://stackoverflow.com/a/3312896
87-
#if defined(__GNUC__) || defined(__clang__) // GCC, clang, clang-cl, and so on but not MSVC
88-
# define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
89-
#elif defined(_MSC_VER) && !defined(__clang__) // MSVC
90-
# define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
91-
#endif
92-
9386
#if defined(_MSC_VER) && !defined(__clang__)
9487
#include <math.h>
9588
#define INF INFINITY
@@ -228,67 +221,61 @@ static inline int ctz64(uint64_t a)
228221
#endif
229222
}
230223

231-
PACK(
232-
struct packed_u64 {
233-
uint64_t v;
234-
}
235-
);
236-
237-
PACK(
238-
struct packed_u32 {
239-
uint32_t v;
240-
}
241-
);
242-
243-
PACK(
244-
struct packed_u16 {
245-
uint16_t v;
246-
}
247-
);
248-
249224
static inline uint64_t get_u64(const uint8_t *tab)
250225
{
251-
return ((const struct packed_u64 *)tab)->v;
226+
uint64_t v;
227+
memcpy(&v, tab, sizeof(v));
228+
return v;
252229
}
253230

254231
static inline int64_t get_i64(const uint8_t *tab)
255232
{
256-
return (int64_t)((const struct packed_u64 *)tab)->v;
233+
int64_t v;
234+
memcpy(&v, tab, sizeof(v));
235+
return v;
257236
}
258237

259238
static inline void put_u64(uint8_t *tab, uint64_t val)
260239
{
261-
((struct packed_u64 *)tab)->v = val;
240+
memcpy(tab, &val, sizeof(val));
262241
}
263242

264243
static inline uint32_t get_u32(const uint8_t *tab)
265244
{
266-
return ((const struct packed_u32 *)tab)->v;
245+
uint32_t v;
246+
memcpy(&v, tab, sizeof(v));
247+
return v;
267248
}
268249

269250
static inline int32_t get_i32(const uint8_t *tab)
270251
{
271-
return (int32_t)((const struct packed_u32 *)tab)->v;
252+
int32_t v;
253+
memcpy(&v, tab, sizeof(v));
254+
return v;
272255
}
273256

274257
static inline void put_u32(uint8_t *tab, uint32_t val)
275258
{
276-
((struct packed_u32 *)tab)->v = val;
259+
memcpy(tab, &val, sizeof(val));
277260
}
278261

279262
static inline uint32_t get_u16(const uint8_t *tab)
280263
{
281-
return ((const struct packed_u16 *)tab)->v;
264+
uint16_t v;
265+
memcpy(&v, tab, sizeof(v));
266+
return v;
282267
}
283268

284269
static inline int32_t get_i16(const uint8_t *tab)
285270
{
286-
return (int16_t)((const struct packed_u16 *)tab)->v;
271+
int16_t v;
272+
memcpy(&v, tab, sizeof(v));
273+
return v;
287274
}
288275

289276
static inline void put_u16(uint8_t *tab, uint16_t val)
290277
{
291-
((struct packed_u16 *)tab)->v = val;
278+
memcpy(tab, &val, sizeof(val));
292279
}
293280

294281
static inline uint32_t get_u8(const uint8_t *tab)

0 commit comments

Comments
 (0)