I'm looking at using bswap64 to help speed up string reversing but I'm having issues getting the type system to play nicely.
This is my current test/play code, it takes "abcdefgh" and prints "hgfedcba":
Code:
test.c: In function ‘main’:
test.c:19: warning: cast to pointer from integer of different size
test.c: At top level:
test.c:27: error: conflicting types for ‘storeLong’
test.c:19: note: previous implicit declaration of ‘storeLong’ was here
test.c:40: error: conflicting types for ‘extractLong’
test.c:10: note: previous implicit declaration of ‘extractLong’ was here
This is my current test/play code, it takes "abcdefgh" and prints "hgfedcba":
Code:
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
// Example Word
char* word = "abcdefgh";
// Get long representation
int64_t num = extractLong(word, 0);
// Print String
printf("%s\n", num);
// Perform ByteSwap
num = __builtin_bswap64(num);
// Store Swap back into string
word = (char*) storeLong(word, 0, num);
// Print String
printf("%s\n", num);
return 0;
}
void storeLong(char* array, size_t off, int64_t val)
{
array[off + 0] = (char)((val & 0xff00000000000000L) >> 56);
array[off + 1] = (char)((val & 0x00ff000000000000L) >> 48);
array[off + 2] = (char)((val & 0x0000ff0000000000L) >> 40);
array[off + 3] = (char)((val & 0x000000ff00000000L) >> 32);
array[off + 4] = (char)((val & 0x00000000ff000000L) >> 24);
array[off + 5] = (char)((val & 0x0000000000ff0000L) >> 16);
array[off + 6] = (char)((val & 0x000000000000ff00L) >> 8);
array[off + 7] = (char)((val & 0x00000000000000ffL));
}
int64_t extractLong(char* array, size_t off)
{
int64_t a = array[off+0] & 0xff;
int64_t b = array[off+1] & 0xff;
int64_t c = array[off+2] & 0xff;
int64_t d = array[off+3] & 0xff;
int64_t e = array[off+4] & 0xff;
int64_t f = array[off+5] & 0xff;
int64_t g = array[off+6] & 0xff;
int64_t h = array[off+7] & 0xff;
return (a<<56 | b<<48 | c<<40 | d<<32 | e<<24 | f<<16 | g<<8 | h);
}