aboutsummaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-09-14 01:45:31 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit35200031e88c65da6a0bde563f20d95c1dd4f464 (patch)
treeb1159960ca7ba78a42f6ef203f99d2b1a1c26641 /src/std
parent7d663615b2a44715e7447a40cae467d7d4e38b9c (diff)
Use struct for bytecode header instead of pointer arithmetic
Diffstat (limited to 'src/std')
-rw-r--r--src/std/misc.c54
1 files changed, 54 insertions, 0 deletions
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