[Concept,09/16] ext4l: Move memweight to linux/string.h
Commit Message
From: Simon Glass <simon.glass@canonical.com>
Move the memweight() function to include/linux/string.h where it belongs
in the Linux kernel header hierarchy. Use an inline popcount algorithm
to avoid header include dependencies.
Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
fs/ext4l/ext4_uboot.h | 12 +-----------
include/linux/string.h | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+), 11 deletions(-)
@@ -640,17 +640,7 @@ struct dx_hash_info {
/* strtomem_pad is in linux/string.h */
/* strscpy_pad is in linux/string.h */
-/* Memory weight - count set bits */
-static inline unsigned long memweight(const void *ptr, size_t bytes)
-{
- unsigned long ret = 0;
- const unsigned char *p = ptr;
- size_t i;
-
- for (i = 0; i < bytes; i++)
- ret += hweight8(p[i]);
- return ret;
-}
+/* memweight is in linux/string.h */
/* BITS_PER_BYTE is in linux/bitops.h */
@@ -188,6 +188,30 @@ char *strreplace(char *str, char old, char new);
*/
#define strscpy_pad(dest, src) strncpy(dest, src, sizeof(dest))
+/**
+ * memweight - Count total number of bits set in a memory region
+ * @ptr: Pointer to memory region
+ * @bytes: Number of bytes to examine
+ *
+ * Return: Number of set bits in the memory region
+ */
+static inline unsigned long memweight(const void *ptr, size_t bytes)
+{
+ unsigned long ret = 0;
+ const unsigned char *p = ptr;
+ size_t i;
+
+ for (i = 0; i < bytes; i++) {
+ /* Inline popcount for byte */
+ unsigned char v = p[i];
+
+ v = v - ((v >> 1) & 0x55);
+ v = (v & 0x33) + ((v >> 2) & 0x33);
+ ret += (v + (v >> 4)) & 0x0f;
+ }
+ return ret;
+}
+
#ifdef __cplusplus
}
#endif