diff options
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/misc.c | 54 |
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 |