2022-07-10 14:32:15

by Yu-Jen Chang

[permalink] [raw]
Subject: [PATCH 1/2] lib/string.c: Add a macro for memchr_inv()

We add a macro MEMCHR_MASK_GEN() so that both memchr_inv()
and memchr() can use it to generate a 8 bytes mask.

Signed-off-by: Yu-Jen Chang <[email protected]>
Signed-off-by: Ching-Chun (Jim) Huang <[email protected]>
---
lib/string.c | 34 ++++++++++++++++++++++++----------
1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 485777c9d..80469e6c3 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -879,6 +879,29 @@ char *strnstr(const char *s1, const char *s2, size_t len)
EXPORT_SYMBOL(strnstr);
#endif

+#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
+
+#define MEMCHR_MASK_GEN(mask) (mask *= 0x0101010101010101ULL)
+
+#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
+
+#define MEMCHR_MASK_GEN(mask) \
+ do { \
+ mask *= 0x01010101; \
+ mask |= mask << 32; \
+ } while (0)
+
+#else
+
+#define MEMCHR_MASK_GEN(mask) \
+ do { \
+ mask |= mask << 8; \
+ mask |= mask << 16; \
+ mask |= mask << 32; \
+ } while (0)
+
+#endif
+
#ifndef __HAVE_ARCH_MEMCHR
/**
* memchr - Find a character in an area of memory.
@@ -932,16 +955,7 @@ void *memchr_inv(const void *start, int c, size_t bytes)
return check_bytes8(start, value, bytes);

value64 = value;
-#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
- value64 *= 0x0101010101010101ULL;
-#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
- value64 *= 0x01010101;
- value64 |= value64 << 32;
-#else
- value64 |= value64 << 8;
- value64 |= value64 << 16;
- value64 |= value64 << 32;
-#endif
+ MEMCHR_MASK_GEN(value64);

prefix = (unsigned long)start % 8;
if (prefix) {
--
2.25.1