Skip to content

Instantly share code, notes, and snippets.

@theuni
Created December 16, 2016 18:53
Show Gist options
  • Save theuni/deb0b97c31890bd0a06a65dd3cb51822 to your computer and use it in GitHub Desktop.
Save theuni/deb0b97c31890bd0a06a65dd3cb51822 to your computer and use it in GitHub Desktop.
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define bswap_64(x) OSSwapInt64(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_16(x) OSSwapInt16(x)
#else
#include <byteswap.h>
#endif
#include <cstdint>
extern uint64_t out64;
extern uint32_t out32;
extern uint16_t out16;
void myswaps(uint64_t in64, uint32_t in32, uint16_t in16);
void systemswaps(uint64_t in64, uint32_t in32, uint16_t in16);
inline uint16_t mybswap_16(uint16_t x)
{
return (x >> 8) | ((x & 0x00ff) << 8);
}
inline uint32_t mybswap_32(uint32_t x)
{
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
}
inline uint64_t mybswap_64(uint64_t x)
{
return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
| ((x & 0x000000ff00000000ull) >> 8)
| ((x & 0x00000000ff000000ull) << 8)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
}
void systemswaps(uint64_t in64, uint32_t in32, uint16_t in16)
{
out64 = bswap_64(in64);
out32 = bswap_32(in32);
out16 = bswap_16(in16);
}
void myswaps(uint64_t in64, uint32_t in32, uint16_t in16)
{
out64 = mybswap_64(in64);
out32 = mybswap_32(in32);
out16 = mybswap_16(in16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment