Skip to content

Instantly share code, notes, and snippets.

@churchofthought
Last active December 17, 2015 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save churchofthought/5520574 to your computer and use it in GitHub Desktop.
Save churchofthought/5520574 to your computer and use it in GitHub Desktop.
improved 8-byte strrev
void strrev(char *strStart, const int len){
char* strEnd = strStart + len;
while (strStart <= (strEnd -= 8)){
__uint64_t buf = *(__uint64_t*)strStart;
*(__uint64_t*)strStart = *(__uint64_t*)strEnd;
__asm__ ("bswap %0" : "=r" (*(__uint64_t*)strStart) : "0" (*(__uint64_t*)strStart));
*(__uint64_t*)strEnd = buf;
__asm__ ("bswap %0" : "=r" (*(__uint64_t*)strEnd) : "0" (*(__uint64_t*)strEnd));
strStart += 8;
}
strEnd += (8 > len ? 7 : 8);
// do the rest of the modulo 8 bytes
while (strStart < strEnd) {
char charBuf = *strStart;
*strStart = *strEnd;
*strEnd = charBuf;
--strEnd;
++strStart;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment