Skip to content

Commit

Permalink
use patch
Browse files Browse the repository at this point in the history
  • Loading branch information
bettio committed Oct 21, 2023
1 parent 5ccdf98 commit 47a7784
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/libAtomVM/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -102,23 +102,50 @@
#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] )

#define WRITE_16_UNALIGNED(ptr, val) \
*((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);
Expand Down

0 comments on commit 47a7784

Please sign in to comment.