From befc3d9a6169bae8aa071bc4b7656f2063fa96f6 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Tue, 11 Jan 2022 12:32:03 +0100 Subject: [PATCH] lib: use 32-bit assignments for memset() SOF loves memset(). It takes great care to 0-initialise buffers and objects. And its memset() routine only sets one byte at a time. Optimise it to use 32-bit assignments where possible. Signed-off-by: Guennadi Liakhovetski --- src/lib/lib.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/lib.c b/src/lib/lib.c index f46bbfa18..be8d6c6ae 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -4,6 +4,7 @@ // // Author: Liam Girdwood +#include #include #include @@ -25,9 +26,26 @@ void *memset(void *s, int c, size_t n) { uint8_t *d8 = s; uint8_t v = c; + uint32_t v32 = (uint32_t)v | ((uint32_t)v << 8) | + ((uint32_t)v << 16) | ((uint32_t)v << 24); + uint32_t *d32 = (void *)ALIGN_UP((uintptr_t)s, sizeof(uint32_t)); + /* Don't bother with 32-bit copies for up to 7 bytes */ + size_t prefix_sz = n > 2 * sizeof(v32) - 1 ? (uint8_t *)d32 - d8 : n; int i; - for (i = 0; i < n; i++) + for (i = 0; i < prefix_sz; i++) + d8[i] = v; + + /* This won't be executed if n <= 7 */ + for (i = 0; i < (n - prefix_sz) >> 2; i++) + d32[i] = v32; + + /* + * The starting point now is the prefix plus the number of 32-bit copies + * from the loop above, multiplied by 4. + * This won't be executed if n <= 7 + */ + for (i = prefix_sz + (i << 2); i < n; i++) d8[i] = v; return s;