306-mips_mem_functions_performance.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. lede-commit: 07e59c7bc7f375f792ec9734be42fe4fa391a8bb
  4. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  5. ---
  6. arch/mips/boot/compressed/Makefile | 3 ++-
  7. arch/mips/include/asm/string.h | 38 ++++++++++++++++++++++++++++++++++++++
  8. arch/mips/lib/Makefile | 2 +-
  9. arch/mips/lib/memcmp.c | 22 ++++++++++++++++++++++
  10. 4 files changed, 63 insertions(+), 2 deletions(-)
  11. create mode 100644 arch/mips/lib/memcmp.c
  12. --- a/arch/mips/boot/compressed/Makefile
  13. +++ b/arch/mips/boot/compressed/Makefile
  14. @@ -23,7 +23,8 @@ KBUILD_CFLAGS := $(filter-out -pg, $(KBU
  15. KBUILD_CFLAGS := $(filter-out -fstack-protector, $(KBUILD_CFLAGS))
  16. KBUILD_CFLAGS := $(KBUILD_CFLAGS) -D__KERNEL__ \
  17. - -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) -D"VMLINUX_LOAD_ADDRESS_ULL=$(VMLINUX_LOAD_ADDRESS)ull"
  18. + -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) -D"VMLINUX_LOAD_ADDRESS_ULL=$(VMLINUX_LOAD_ADDRESS)ull" \
  19. + -D__ZBOOT__
  20. KBUILD_AFLAGS := $(KBUILD_AFLAGS) -D__ASSEMBLY__ \
  21. -DBOOT_HEAP_SIZE=$(BOOT_HEAP_SIZE) \
  22. --- a/arch/mips/include/asm/string.h
  23. +++ b/arch/mips/include/asm/string.h
  24. @@ -140,4 +140,42 @@ extern void *memcpy(void *__to, __const_
  25. #define __HAVE_ARCH_MEMMOVE
  26. extern void *memmove(void *__dest, __const__ void *__src, size_t __n);
  27. +#ifndef __ZBOOT__
  28. +#define memset(__s, __c, len) \
  29. +({ \
  30. + size_t __len = (len); \
  31. + void *__ret; \
  32. + if (__builtin_constant_p(len) && __len >= 64) \
  33. + __ret = memset((__s), (__c), __len); \
  34. + else \
  35. + __ret = __builtin_memset((__s), (__c), __len); \
  36. + __ret; \
  37. +})
  38. +
  39. +#define memcpy(dst, src, len) \
  40. +({ \
  41. + size_t __len = (len); \
  42. + void *__ret; \
  43. + if (__builtin_constant_p(len) && __len >= 64) \
  44. + __ret = memcpy((dst), (src), __len); \
  45. + else \
  46. + __ret = __builtin_memcpy((dst), (src), __len); \
  47. + __ret; \
  48. +})
  49. +
  50. +#define memmove(dst, src, len) \
  51. +({ \
  52. + size_t __len = (len); \
  53. + void *__ret; \
  54. + if (__builtin_constant_p(len) && __len >= 64) \
  55. + __ret = memmove((dst), (src), __len); \
  56. + else \
  57. + __ret = __builtin_memmove((dst), (src), __len); \
  58. + __ret; \
  59. +})
  60. +
  61. +#define __HAVE_ARCH_MEMCMP
  62. +#define memcmp(src1, src2, len) __builtin_memcmp((src1), (src2), (len))
  63. +#endif
  64. +
  65. #endif /* _ASM_STRING_H */
  66. --- a/arch/mips/lib/Makefile
  67. +++ b/arch/mips/lib/Makefile
  68. @@ -5,7 +5,7 @@
  69. lib-y += bitops.o csum_partial.o delay.o memcpy.o memset.o \
  70. mips-atomic.o strncpy_user.o \
  71. - strnlen_user.o uncached.o
  72. + strnlen_user.o uncached.o memcmp.o
  73. obj-y += iomap.o iomap_copy.o
  74. obj-$(CONFIG_PCI) += iomap-pci.o
  75. --- /dev/null
  76. +++ b/arch/mips/lib/memcmp.c
  77. @@ -0,0 +1,22 @@
  78. +/*
  79. + * copied from linux/lib/string.c
  80. + *
  81. + * Copyright (C) 1991, 1992 Linus Torvalds
  82. + */
  83. +
  84. +#include <linux/module.h>
  85. +#include <linux/string.h>
  86. +
  87. +#undef memcmp
  88. +int memcmp(const void *cs, const void *ct, size_t count)
  89. +{
  90. + const unsigned char *su1, *su2;
  91. + int res = 0;
  92. +
  93. + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  94. + if ((res = *su1 - *su2) != 0)
  95. + break;
  96. + return res;
  97. +}
  98. +EXPORT_SYMBOL(memcmp);
  99. +