1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: [PATCH] mips: allow the compiler to optimize memset, memcmp, memcpy for better performance and (in some instances) smaller code
4 lede-commit: 07e59c7bc7f375f792ec9734be42fe4fa391a8bb
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 arch/mips/boot/compressed/Makefile | 3 ++-
8 arch/mips/include/asm/string.h | 38 ++++++++++++++++++++++++++++++++++++++
9 arch/mips/lib/Makefile | 2 +-
10 arch/mips/lib/memcmp.c | 22 ++++++++++++++++++++++
11 4 files changed, 63 insertions(+), 2 deletions(-)
12 create mode 100644 arch/mips/lib/memcmp.c
14 diff --git a/arch/mips/boot/compressed/Makefile b/arch/mips/boot/compressed/Makefile
15 index 90aca95fe314..3cd3b391ef49 100644
16 --- a/arch/mips/boot/compressed/Makefile
17 +++ b/arch/mips/boot/compressed/Makefile
18 @@ -23,7 +23,8 @@ KBUILD_CFLAGS := $(shell echo $(KBUILD_CFLAGS) | sed -e "s/-pg//")
19 KBUILD_CFLAGS := $(filter-out -fstack-protector, $(KBUILD_CFLAGS))
21 KBUILD_CFLAGS := $(LINUXINCLUDE) $(KBUILD_CFLAGS) -D__KERNEL__ \
22 - -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) -D"VMLINUX_LOAD_ADDRESS_ULL=$(VMLINUX_LOAD_ADDRESS)ull"
23 + -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) -D"VMLINUX_LOAD_ADDRESS_ULL=$(VMLINUX_LOAD_ADDRESS)ull" \
26 KBUILD_AFLAGS := $(LINUXINCLUDE) $(KBUILD_AFLAGS) -D__ASSEMBLY__ \
27 -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) \
28 diff --git a/arch/mips/include/asm/string.h b/arch/mips/include/asm/string.h
29 index 29030cb398ee..7b737f9b6d58 100644
30 --- a/arch/mips/include/asm/string.h
31 +++ b/arch/mips/include/asm/string.h
32 @@ -140,4 +140,42 @@ extern void *memcpy(void *__to, __const__ void *__from, size_t __n);
33 #define __HAVE_ARCH_MEMMOVE
34 extern void *memmove(void *__dest, __const__ void *__src, size_t __n);
37 +#define memset(__s, __c, len) \
39 + size_t __len = (len); \
41 + if (__builtin_constant_p(len) && __len >= 64) \
42 + __ret = memset((__s), (__c), __len); \
44 + __ret = __builtin_memset((__s), (__c), __len); \
48 +#define memcpy(dst, src, len) \
50 + size_t __len = (len); \
52 + if (__builtin_constant_p(len) && __len >= 64) \
53 + __ret = memcpy((dst), (src), __len); \
55 + __ret = __builtin_memcpy((dst), (src), __len); \
59 +#define memmove(dst, src, len) \
61 + size_t __len = (len); \
63 + if (__builtin_constant_p(len) && __len >= 64) \
64 + __ret = memmove((dst), (src), __len); \
66 + __ret = __builtin_memmove((dst), (src), __len); \
70 +#define __HAVE_ARCH_MEMCMP
71 +#define memcmp(src1, src2, len) __builtin_memcmp((src1), (src2), (len))
74 #endif /* _ASM_STRING_H */
75 diff --git a/arch/mips/lib/Makefile b/arch/mips/lib/Makefile
76 index 0344e575f522..33a0211e954a 100644
77 --- a/arch/mips/lib/Makefile
78 +++ b/arch/mips/lib/Makefile
81 lib-y += bitops.o csum_partial.o delay.o memcpy.o memset.o \
82 mips-atomic.o strlen_user.o strncpy_user.o \
83 - strnlen_user.o uncached.o
84 + strnlen_user.o uncached.o memcmp.o
87 obj-$(CONFIG_PCI) += iomap-pci.o
88 diff --git a/arch/mips/lib/memcmp.c b/arch/mips/lib/memcmp.c
90 index 000000000000..35ef1646286e
92 +++ b/arch/mips/lib/memcmp.c
95 + * copied from linux/lib/string.c
97 + * Copyright (C) 1991, 1992 Linus Torvalds
100 +#include <linux/module.h>
101 +#include <linux/string.h>
104 +int memcmp(const void *cs, const void *ct, size_t count)
106 + const unsigned char *su1, *su2;
109 + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
110 + if ((res = *su1 - *su2) != 0)
114 +EXPORT_SYMBOL(memcmp);