fsm/src/pack.h

62 lines
2.3 KiB
C++

#pragma once
#include <cstdint>
#if defined(__GNUC__) && !defined(__clang__)
#error "GCC is currently not supported due to volatile union packed struct issue. Use clang instead"
#endif
#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 4)) || defined(__clang__) || defined(__TINYC__)
#if defined(__TINYC__)
#define PACKED(x, s) \
_Pragma("pack(1)") \
union { \
s value; \
x __attribute__((packed)) fields; \
} \
_Pragma("pack(1)")
#define PACKEDM(x, s, n) \
_Pragma("pack(1)") \
union { \
s value[n]; \
x __attribute__((packed)) fields; \
} \
_Pragma("pack(1)")
#else
#define PACKED(x, s) \
union { \
s value; \
x __attribute__((packed, aligned(sizeof(s)))) fields; \
} __attribute__((packed, aligned(sizeof(s))))
#define PACKEDA(n, a, x, s) \
union n { \
static const uint32_t ADDRESS = BASE_ADDRESS + a; \
s value; \
x __attribute__((packed, aligned(sizeof(s)))) fields; \
} __attribute__((packed, aligned(sizeof(s))))
#define PACKEDM(x, s, i) \
union { \
s value[i]; \
x __attribute__((packed, aligned(sizeof(s)))) fields; \
} __attribute__((packed, aligned(sizeof(s))))
#define PACKEDAM(n, a, x, s, i) \
union n { \
static const uint32_t ADDRESS = BASE_ADDRESS + a; \
s value[i]; \
x __attribute__((packed, aligned(sizeof(s)))) fields; \
} __attribute__((packed, aligned(sizeof(s))))
#endif
#else
#error "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4, upgrade to GCC >= 4.4."
#endif