diff --git a/src/libAtomVM/utils.h b/src/libAtomVM/utils.h index 260adccfdf..6a102d1947 100644 --- a/src/libAtomVM/utils.h +++ b/src/libAtomVM/utils.h @@ -28,7 +28,7 @@ #ifndef _UTILS_H_ #define _UTILS_H_ -#ifdef __ORDER_LITTLE_ENDIAN__ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #ifdef __GNUC__ #define READ_32_ALIGNED(ptr) \ __builtin_bswap32(*((uint32_t *) (ptr))) @@ -102,16 +102,40 @@ #define ENDIAN_SWAP_32(value) ((((value) & 0xFF) << 24) | (((value) & 0xFF00) << 8) | (((value) & 0xFF0000) >> 8) | (((value) & 0xFF000000) >> 24)) #endif -#else +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + #define READ_64_UNALIGNED(ptr) \ + ((uint64_t) ( (((uint8_t *)(ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *)(ptr))[2] << 8) | ((uint8_t *)(ptr))[3] ) << 32 | \ + (((uint8_t *)(ptr))[4] << 24) | (((uint8_t *) (ptr))[5] << 16) | (((uint8_t *)(ptr))[6] << 8) | ((uint8_t *)(ptr))[7] ) + + #ifdef __GNUC__ + #define WRITE_64_UNALIGNED(ptr, val) \ + { \ + uint64_t val_arg = val; \ + __builtin_memcpy(ptr, (void *) &val_arg, sizeof(uint64_t)); \ + } + + #define WRITE_32_UNALIGNED(ptr, val) \ + { \ + uint32_t val_arg = val; \ + __builtin_memcpy(ptr, (void *) &val_arg, sizeof(uint32_t)); \ + } + #else + #error Create implementations of WRITE_64_UNALIGNED + #define WRITE_32_UNALIGNED(ptr, val) \ + { \ + uint32_t val_arg = val; \ + *((uint32_t *) (ptr)) = ( (((uint8_t *)(&val_arg))[0] << 24) | (((uint8_t *) (&val_arg))[1] << 16) | (((uint8_t *)(&val_arg))[2] << 8) | ((uint8_t *)(&val_arg))[3] ) \ + } + + #endif + + #define READ_32_ALIGNED(ptr) \ (*((uint32_t *) (ptr))) #define READ_32_UNALIGNED(ptr) \ ( (((uint8_t *)(ptr))[0] << 24) | (((uint8_t *) (ptr))[1] << 16) | (((uint8_t *)(ptr))[2] << 8) | ((uint8_t *)(ptr))[3] ) - #define WRITE_32_UNALIGNED(ptr, val) \ - *((uint32_t *) (ptr)) = ( (((uint8_t *)(&val))[0] << 24) | (((uint8_t *) (&val))[1] << 16) | (((uint8_t *)(&val))[2] << 8) | ((uint8_t *)(&val))[3] ) - #define READ_16_UNALIGNED(ptr) \ ( (((uint8_t *)(ptr))[0] << 8) | ((uint8_t *)(ptr))[1] ) @@ -119,6 +143,9 @@ *((uint16_t *) (ptr)) = ( (((uint8_t *)(&val))[0] << 8) | ((uint8_t *)(&val))[1] ) #define ENDIAN_SWAP_32(value) (value) + +#else +#error "unsupported endianness." #endif #define UNUSED(x) (void) (x);