Last active
December 17, 2015 00:19
-
-
Save churchofthought/5520574 to your computer and use it in GitHub Desktop.
improved 8-byte strrev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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