From 35200031e88c65da6a0bde563f20d95c1dd4f464 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 14 Sep 2019 01:45:31 +0200 Subject: Use struct for bytecode header instead of pointer arithmetic --- src/std/misc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/std/misc.c (limited to 'src/std') diff --git a/src/std/misc.c b/src/std/misc.c new file mode 100644 index 0000000..f53797d --- /dev/null +++ b/src/std/misc.c @@ -0,0 +1,54 @@ +#include "../../include/std/misc.h" + +#if defined(_MSC_VER) +u16 byteswap16(u16 value) { + return _byteswap_ushort(value); +} + +u32 byteswap32(u32 value) { + return _byteswap_ulong(value); +} + +u64 byteswap64(u64 value) { + return _byteswap_uint64(value); +} +#elif defined(__GNUC__) +u16 byteswap16(u16 value) { + return __builtin_bswap16(value); +} +u32 byteswap32(u32 value) { + return __builtin_bswap32(value); +} +u64 byteswap64(u64 value) { + return __builtin_bswap64(value); +} +#else +u16 byteswap16(u16 value) { + u16 result = 0; + result |= (value & 0x00FF) << 8; + result |= (value & 0xFF00) >> 8; + return result; +} + +u32 byteswap32(u32 value) { + u32 result = 0; + result |= (value & 0x000000FF) << 24; + result |= (value & 0x0000FF00) << 8; + result |= (value & 0x00FF0000) >> 8; + result |= (value & 0xFF000000) >> 24; + return result; +} + +u64 byteswap64(u64 value) { + u64 result = 0; + result |= (value & 0x00000000000000FF) << 56; + result |= (value & 0x000000000000FF00) << 40; + result |= (value & 0x0000000000FF0000) << 24; + result |= (value & 0x00000000FF000000) << 8; + result |= (value & 0x000000FF00000000) >> 8; + result |= (value & 0x0000FF0000000000) >> 24; + result |= (value & 0x00FF000000000000) >> 40; + result |= (value & 0xFF00000000000000) >> 56; + return result; +} +#endif -- cgit v1.2.3