This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH 2/4] Improve generic strspn performance
- From: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- To: Adhemerval Zanella <adhemerval dot zanella at linaro dot org>, "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>
- Cc: nd <nd at arm dot com>
- Date: Tue, 29 Mar 2016 13:02:16 +0000
- Subject: Re: [PATCH 2/4] Improve generic strspn performance
- Authentication-results: sourceware.org; auth=none
- Nodisclaimer: True
- References: <1459178389-14133-1-git-send-email-adhemerval dot zanella at linaro dot org>,<1459178389-14133-2-git-send-email-adhemerval dot zanella at linaro dot org>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
Adhemerval Zanella wrote:
> + if (accept[0] == '\0')
> + return 0;
> + if (accept[1] == '\0')
> + {
GCC doesn't get the static branch prediction correct for the 2nd if,
so it would be useful to add __glibc_unlikely given that single-character
accepts are rare.
> + s = (unsigned char *) ((size_t)(s) & ~3);
> + unsigned int c0, c1, c2, c3;
> + do {
> + s += 4;
> + c0 = p[s[0]];
> + c1 = p[s[1]];
> + c2 = p[s[2]];
> + c3 = p[s[3]];
> + } while ((c0 && c1 && c2 && c3) == 1);
That should use '&' rather than '&&' and '!= 0' similar to how I did it in strcspn.
This will use 3 AND(S) instructions and a single branch.
> +
> + size_t count = s - (unsigned char *) str;
> + return (c0 && c1) == 0 ? count - !c0 + 1 : count - !c2 + 3;
Again, c0 & c1 is better and allows CSE with the while expression above.
Also -!c0 +1 is equivalent to c0, -!c2 + 3 is equivalent to c2 + 2 - this is simpler
and faster.
Otherwise it looks good, and thanks for doing this one too!
Cheers,
Wilco