1
0
Fork 0

Update for 32-bit systems.

This commit is contained in:
ehofman 2005-09-21 09:17:32 +00:00
parent 0f704b0a6e
commit 7d0daddfcc
2 changed files with 15 additions and 4 deletions

View file

@ -5,8 +5,8 @@
int main() int main()
{ {
uint32_t ui32 = 0xFFAACCEE; uint32_t ui32 = 0x01234567;
uint64_t ui64 = 0x00BBAADDFFAACCEELL; uint64_t ui64 = 0x0123456789ABCDEFLL;
printf("UI32: (normal) %x\nUI32: (swapped) %x\n\n", ui32, bswap_32(ui32) ); printf("UI32: (normal) %x\nUI32: (swapped) %x\n\n", ui32, bswap_32(ui32) );
printf("UI64: (normal) %llx\nUI64: (swapped) %llx\n\n", ui64, bswap_64(ui64) ); printf("UI64: (normal) %llx\nUI64: (swapped) %llx\n\n", ui64, bswap_64(ui64) );

View file

@ -57,11 +57,22 @@ inline uint32_t bswap_32(unsigned int b) {
} }
inline uint64_t bswap_64(unsigned long long b) { inline uint64_t bswap_64(unsigned long long b) {
#if __WORDSIZE == 64
uint64_t x = b; uint64_t x = b;
x = ((x >> 32) & 0x00000000FFFFFFFFLL) | ((x << 32) & 0xFFFFFFFF00000000LL);
x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL); x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL);
x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
x = (x >> 32) | (x << 32);
return x; return x;
#else
union {
uint64_t ll;
uint32_t l[2];
} w, r;
w.ll = b;
r.l[0] = bswap_32 (w.l[1]);
r.l[1] = bswap_32 (w.l[0]);
return r.ll;
#endif
} }
#if BYTE_ORDER == BIG_ENDIAN #if BYTE_ORDER == BIG_ENDIAN