From a3169750b5cfcdc950b9e2bdd85cdf65341db307 Mon Sep 17 00:00:00 2001 From: Roy Oursler Date: Thu, 13 Dec 2018 15:29:05 -0700 Subject: [PATCH] mem: Remove unaligned loads in base function Change-Id: I8fb0f2e2e372485c864d5c60f816b661a865b707 Signed-off-by: Roy Oursler --- include/unaligned.h | 5 +++++ mem/mem_zero_detect_base.c | 18 +++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/unaligned.h b/include/unaligned.h index d048655..f9437df 100644 --- a/include/unaligned.h +++ b/include/unaligned.h @@ -51,5 +51,10 @@ static inline uint64_t load_u64(uint8_t * buf) { return ret; } +static inline uintmax_t load_umax(uint8_t * buf) { + uintmax_t ret; + memcpy(&ret, buf, sizeof(ret)); + return ret; +} #endif diff --git a/mem/mem_zero_detect_base.c b/mem/mem_zero_detect_base.c index ae11553..2353016 100644 --- a/mem/mem_zero_detect_base.c +++ b/mem/mem_zero_detect_base.c @@ -29,22 +29,22 @@ #include #include +#include "unaligned.h" int mem_zero_detect_base(void *buf, size_t n) { - unsigned char *c; - uintmax_t a = 0, *p = buf; + uint8_t *c = buf; + uintmax_t a = 0; // Check buffer in native machine width comparisons - while (n >= sizeof(*p)) { - n -= sizeof(*p); - if (*p++ != 0) + while (n >= sizeof(uintmax_t)) { + n -= sizeof(uintmax_t); + if (load_umax(c) != 0) return -1; + c += sizeof(uintmax_t); } // Check remaining bytes - c = (unsigned char *)p; - switch (n) { case 7: a |= *c++; // fall through to case 6,5,4 @@ -53,12 +53,12 @@ int mem_zero_detect_base(void *buf, size_t n) case 5: a |= *c++; // fall through to case 4 case 4: - a |= *((unsigned int *)c); + a |= load_u32(c); break; case 3: a |= *c++; // fall through to case 2 case 2: - a |= *((unsigned short *)c); + a |= load_u16(c); break; case 1: a |= *c;