From 056c318e8670c7295f9d8a0a4014047a5564dad8 Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Thu, 19 Nov 2020 16:33:40 +0100 Subject: [PATCH] Make memcmp more compiler friendly The Thumb ISA allows incrementing a pointer right after dereferencing it. This commit attempts to make it more obvious to the compiler to use this instruction in memcmp(). --- lib/string.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/string.c b/lib/string.c index 0cbdf9c..0cf9c04 100644 --- a/lib/string.c +++ b/lib/string.c @@ -10,18 +10,9 @@ int memcmp(const void *s1, const void *s2, size_t n) int delta = 0; while (n-- > 0) { - /* - * TODO - * See if avr-gcc is smart enough to combine the pointer - * dereferencing and increment into a single `ld Rd,X+` - * instruction even if there is an if statement in between - */ - delta = *(const unsigned char *)s1 - *(const unsigned char *)s2; + delta = *(const unsigned char *)s1++ - *(const unsigned char *)s2++; if (delta != 0) break; - - s1++; - s2++; } return delta;