RE: generic strncpy - off-by-one error

From: Peter Kjellerstedt
Date: Sat Aug 16 2003 - 03:23:45 EST


> -----Original Message-----
> From: Timothy Miller [mailto:miller@xxxxxxxxxxxxxx]
> Sent: Friday, August 15, 2003 19:52
> To: Peter Kjellerstedt
> Cc: 'Willy Tarreau'; linux-kernel mailing list
> Subject: Re: generic strncpy - off-by-one error
>
> Peter Kjellerstedt wrote:
> > Timothy's 3.125003 6.128571 9.147905 33.325337
>
> Which of mine did you test? The one with the single-byte
> fill, or the one with the multiple fill loops that does
> words for the bulk of the fills?

Stupid me. I only tested the single byte fill version.

> With some minor tweaks to eliminate compiler stupidity which compares
> against -1, that might win on the fill phase. No?

Here are the numbers for my for loop version and your multi
byte fill version for CRIS:

For loops 2.867568 5.620561 8.128734 28.286289
Multi byte fill 2.868031 5.670782 6.312027 11.336015

And here are the numbers for my P4:

For loops 3.060262 5.927378 8.796814 30.659818
Multi byte fill 3.126607 5.898459 7.096685 13.135379

So there is no doubt that the multi byte version is a clear
winner (which was expected, I suppose).

Here is the code that I used:

char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;

while (count && *src) {
*tmp++ = *src++;
count--;
}

if (count) {
size_t count2;

while (count & (sizeof(long) - 1)) {
*tmp++ = '\0';
count--;
}

count2 = count / sizeof(long);
while (count2) {
*((long *)tmp)++ = '\0';
count2--;
}

count &= (sizeof(long) - 1);
while (count) {
*tmp++ = '\0';
count--;
}
}

return dest;
}

//Peter
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/