[Skiboot] [PATCH 09/11] Optimize memset() implementation to do word at a time

Patrick Williams III iawillia at us.ibm.com
Thu May 7 21:57:27 AEST 2015


If you compile with -O3, gcc will already do loop unrolling for naive
implementations. There isn't a good reason to make the code more complex
when you could just enable -O3 for your libc parts.

Patrick

> On May 7, 2015, at 2:12 AM, Stewart Smith <stewart at linux.vnet.ibm.com>
wrote:
>
> Makes booting with gcov enabled take dozens of seconds faster
>
> Signed-off-by: Stewart Smith <stewart at linux.vnet.ibm.com>
> ---
> libc/string/memset.c        |   15 ++++++++++++++-
> libc/test/run-memops-test.c |   16 ++++++++++++++++
> libc/test/run-memops.c      |    3 +++
> 3 files changed, 33 insertions(+), 1 deletion(-)
>
> diff --git a/libc/string/memset.c b/libc/string/memset.c
> index f8dfbf5..e7172b0 100644
> --- a/libc/string/memset.c
> +++ b/libc/string/memset.c
> @@ -15,7 +15,20 @@
> void *
> memset(void *dest, int c, size_t size)
> {
> -    unsigned char *d = (unsigned char *)dest;
> +    unsigned char *d;
> +    unsigned long long *d64 = (unsigned long long*)dest;
> +    unsigned long long c8 = c;
> +
> +    if (size > 8) {
> +        c8 = c8 | c8 << 8ULL | c8 << 16ULL | c8 << 24ULL | c8 << 32ULL
> +            | c8 << 40ULL | c8 << 48ULL | c8 << 56ULL;
> +        while (size > 8) {
> +            *d64++ = c8;
> +            size = size - 8;
> +        }
> +    }
> +
> +    d = (unsigned char *)d64;
>
>    while (size-- > 0) {
>        *d++ = (unsigned char)c;
> diff --git a/libc/test/run-memops-test.c b/libc/test/run-memops-test.c
> index 0979994..6ec5a27 100644
> --- a/libc/test/run-memops-test.c
> +++ b/libc/test/run-memops-test.c
> @@ -41,3 +41,19 @@
> #include "../string/strncpy.c"
> #include "../string/strstr.c"
> #include "../string/strtok.c"
> +
> +char buf[100];
> +
> +int test_memset(void);
> +
> +int test_memset(void)
> +{
> +    int i;
> +    int r= 0;
> +
> +    memset(buf, 0x42, sizeof(buf));
> +    for(i=0; i<sizeof(buf); i++)
> +        if (buf[i] != 0x42)
> +            r = -1;
> +    return r;
> +}
> diff --git a/libc/test/run-memops.c b/libc/test/run-memops.c
> index 15ec9bd..7158b6b 100644
> --- a/libc/test/run-memops.c
> +++ b/libc/test/run-memops.c
> @@ -21,7 +21,10 @@
> #include <string.h>
> #include <stdio.h>
>
> +int test_memset(void);
> +
> int main(void)
> {
> +    assert(test_memset() == 0);
>    return 0;
> }
> --
> 1.7.10.4
>
> _______________________________________________
> Skiboot mailing list
> Skiboot at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/skiboot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/skiboot/attachments/20150507/7f3e8d30/attachment-0001.html>


More information about the Skiboot mailing list