b3d319cbc482332acd1ef6901eec3b4147af7e93
[openwrt/staging/wigyori.git] / target / linux / generic / pending-4.9 / 306-mips_mem_functions_performance.patch
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
3
4 lede-commit: 07e59c7bc7f375f792ec9734be42fe4fa391a8bb
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 ---
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
13
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))
20
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" \
24 + -D__ZBOOT__
25
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);
35
36 +#ifndef __ZBOOT__
37 +#define memset(__s, __c, len) \
38 +({ \
39 + size_t __len = (len); \
40 + void *__ret; \
41 + if (__builtin_constant_p(len) && __len >= 64) \
42 + __ret = memset((__s), (__c), __len); \
43 + else \
44 + __ret = __builtin_memset((__s), (__c), __len); \
45 + __ret; \
46 +})
47 +
48 +#define memcpy(dst, src, len) \
49 +({ \
50 + size_t __len = (len); \
51 + void *__ret; \
52 + if (__builtin_constant_p(len) && __len >= 64) \
53 + __ret = memcpy((dst), (src), __len); \
54 + else \
55 + __ret = __builtin_memcpy((dst), (src), __len); \
56 + __ret; \
57 +})
58 +
59 +#define memmove(dst, src, len) \
60 +({ \
61 + size_t __len = (len); \
62 + void *__ret; \
63 + if (__builtin_constant_p(len) && __len >= 64) \
64 + __ret = memmove((dst), (src), __len); \
65 + else \
66 + __ret = __builtin_memmove((dst), (src), __len); \
67 + __ret; \
68 +})
69 +
70 +#define __HAVE_ARCH_MEMCMP
71 +#define memcmp(src1, src2, len) __builtin_memcmp((src1), (src2), (len))
72 +#endif
73 +
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
79 @@ -4,7 +4,7 @@
80
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
85
86 obj-y += iomap.o
87 obj-$(CONFIG_PCI) += iomap-pci.o
88 diff --git a/arch/mips/lib/memcmp.c b/arch/mips/lib/memcmp.c
89 new file mode 100644
90 index 000000000000..35ef1646286e
91 --- /dev/null
92 +++ b/arch/mips/lib/memcmp.c
93 @@ -0,0 +1,22 @@
94 +/*
95 + * copied from linux/lib/string.c
96 + *
97 + * Copyright (C) 1991, 1992 Linus Torvalds
98 + */
99 +
100 +#include <linux/module.h>
101 +#include <linux/string.h>
102 +
103 +#undef memcmp
104 +int memcmp(const void *cs, const void *ct, size_t count)
105 +{
106 + const unsigned char *su1, *su2;
107 + int res = 0;
108 +
109 + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
110 + if ((res = *su1 - *su2) != 0)
111 + break;
112 + return res;
113 +}
114 +EXPORT_SYMBOL(memcmp);
115 +
116 --
117 2.11.0
118