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)
{
char *tmp = dest;
while (count && (*dest++ = *src++) != '\0')
count--;
while (count > 1) {
*dest++ = 0;
while (count) {
if ((*tmp = *src) != 0) src++;
tmp++;
count--;
}
return tmp;
return dest;
}
#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