Mail Archives: djgpp/1996/06/17/00:44:06
davis AT space DOT mit DOT edu (John E. Davis) wrote:
>I have found the following to be very fast without sacrificing too much
>portability:
>char *jed_memchr(register char *p, register char c, register int n)
>{
> int n2;
> register char *pmax;
>
> pmax = p + (n - 32);
>
> while (p <= pmax)
> {
instead do this:
if(*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c ||
*p++==c || *p++==c || *p++==c || *p++==c)
return p-1;
> }
>
> n2 = n % 32;
I'd put n2=n&31, but that's just me (yes I'm an ex-Borland user :).
> while (n2--)
> {
> if (*p == c) return p;
> p++;
> }
> return(NULL);
>}
This may turn out to be faster. I haven't tried, and gcc's optimizer
may give you exactly the same results.
Malcolm
- Raw text -