Commit dd14119a authored by Albert Cahalan's avatar Albert Cahalan Committed by Doug Ledford

[PATCH] fast AND correct strncpy

This is Erik Andersen's excellent strncpy.
It works like magic. That "if" isn't a jump;
gcc uses a few integer instructions to wipe
out all jumps except for the loop itself and
the function call/return.

This has been exhaustively tested against glibc.

The existing code has 5 extra branches and
is over twice as large. (my gcc, etc.)
parent fc132fde
...@@ -87,13 +87,12 @@ char * strncpy(char * dest,const char *src,size_t count) ...@@ -87,13 +87,12 @@ char * strncpy(char * dest,const char *src,size_t count)
{ {
char *tmp = dest; char *tmp = dest;
while (count && (*dest++ = *src++) != '\0') while (count) {
count--; if ((*tmp = *src) != 0) src++;
while (count > 1) { tmp++;
*dest++ = 0;
count--; count--;
} }
return tmp; return dest;
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment