Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4716919imu; Tue, 18 Dec 2018 22:03:14 -0800 (PST) X-Google-Smtp-Source: AFSGD/U4N0NmOBctR8AlptS79SbXjME24FJB4pY4nZq+DVzq1nWO+4c7eYUNqKPq7j+/oMFwVafN X-Received: by 2002:a62:7e93:: with SMTP id z141mr18965142pfc.239.1545199394117; Tue, 18 Dec 2018 22:03:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545199394; cv=none; d=google.com; s=arc-20160816; b=Ze2xBUVXn831dfHZxwpTz5Q2PTHajTRToxw0io1qsE/0kAkOMA00353lpXiGsvbY3b Vsa3RGiJHB0CahAjJu4kE7y7p5RhRXk/DgCTL5A7F951GvKSuxA2mxVznH9X/8oF6lN7 XjLxNp6BHPZqwDUKUYpvxYxvylzripqUvZfA8rTD4ykDg6bTax7oDri6xbBEasd7L1yM sycXW5+/hkjELbz1a0KA3RtJLpnPxcN07L+GB1mSYnldomxhrvOvm6Ld/L8TNphg4FP1 Jie2Dm/z1fFO6vm5Mx4s4/h8nKvNqkXgyPlDN5ETiMXWe5r3/clOGikGAnxt+XWZJqVZ M6dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u1oZXhPLBwf6ld/Ap5bG26cpxbbDtyrh+8dpvM7Fc0E=; b=elGd/SxHoda7RoE242Y6IoQf1/yVjc+DBKMFAXOPyFJa/uDuqUHLNYrQc+rV3e0/9p k4hPM+o831wbGd85Ft691WLb/zxrUQZp3T92OflAJMK1B9sJ2lPiDF5tTw75pjbRFfTI lKHvqx8eCrIJeacheVnghUqAxmOumE7h1csPKv+JZlQoS9wIvkk0TxpPpQFk4pV3yFkl 1gJwcl/jjqbMEAeVz5fC3oOf+RbgDY7phRfz3/9iaoVNtDZZCEnZqyt7XdqMOVr68IWw y5O93WDsku2nzZbiYvP5C68+NDfuEjlSYYv8NF0lU3MoDN3wJx/Y7LpykKs3x/YMV3Nc CnwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TqubCjyI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5si14115621pgs.497.2018.12.18.22.02.58; Tue, 18 Dec 2018 22:03:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TqubCjyI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727750AbeLSGAM (ORCPT + 99 others); Wed, 19 Dec 2018 01:00:12 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:46937 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726920AbeLSGAM (ORCPT ); Wed, 19 Dec 2018 01:00:12 -0500 Received: by mail-pg1-f194.google.com with SMTP id w7so8945479pgp.13; Tue, 18 Dec 2018 22:00:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u1oZXhPLBwf6ld/Ap5bG26cpxbbDtyrh+8dpvM7Fc0E=; b=TqubCjyIfIJJbFdMSsP5578TCHQFawBB5j28IM913BWu5NKWtsn34/uCMSTVNW+dXK W+CieSYpmmOY2V6v52Oi7HfZKhlNUs6J5tiRvUGOlR++7Eq9PCAKNH+YSk6e4IfFCx5w HmiqWVM/DB7pini343cGVPIo3a2Z0nfnDurTiJjWjX650ibEs6c5MYNzx0XDsf20BvW1 Ch/hmOWNJ8ojH5CV0WP87ghMiqRquCy26aSFS8F0oOGppddcf4bS/6mOfh9MnuSce7eb 8wQBx/MoD4nUEymCKinPHg/z8/uRhxCqohJGo7yfdUQbR2jyB0v/6ahsho2RCNxG49yu 6P0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u1oZXhPLBwf6ld/Ap5bG26cpxbbDtyrh+8dpvM7Fc0E=; b=HNda6cRBeShPP3OCVtuHWLmdCbAL5AbzwN98rfWxopxuvDAOH9D7NydpLlRfbr6S2L 9jPEw11MZf7j/o1arIUi6NNnCrlrMwc4YC3CKUxSo7EFFJD0/Tu+Cd5YWITHZNQJSSc7 fXaNXGaAzA3lrKa4TraMHBv/GJQT/lupimbRIdyKr40U1cqzbM5Ad0flgosm6C9OhK51 z7nXhWYiC7nV6f2wOS61HOMfMP+d4Vfrm1LYygLi5S879/LxjYyk3B7MF6+qdLAAOdh6 lEmM4i+WwRQQmCps9zULSutM8eYu6qXMLJkqWxY0G2fXf9XlAMw/HRxPmS1Hh7/navRF /GpQ== X-Gm-Message-State: AA+aEWaH6RZBaL/bNMUG85MVM+JQhy6uBCJWpEmfdXd76PD8JZa4GSJG 3o/LZR9haND+bx+FQpGL4WmwJPst X-Received: by 2002:a63:ac46:: with SMTP id z6mr1850033pgn.162.1545199211433; Tue, 18 Dec 2018 22:00:11 -0800 (PST) Received: from localhost.localdomain ([2001:268:c0a2:eb51:6018:45d4:55a2:e94b]) by smtp.gmail.com with ESMTPSA id h15sm20600853pgl.43.2018.12.18.22.00.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Dec 2018 22:00:10 -0800 (PST) From: William Breathitt Gray To: linus.walleij@linaro.org Cc: akpm@linux-foundation.org, linux-gpio@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, andriy.shevchenko@linux.intel.com, linux@rasmusvillemoes.dk, William Breathitt Gray , Andy Shevchenko , Arnd Bergmann Subject: [PATCH v6 1/8] bitops: Introduce the for_each_set_clump8 macro Date: Wed, 19 Dec 2018 15:00:39 +0900 Message-Id: <9fb3981cbcfbce3fb8b42bf419d734c0e5a44d0e.1545198260.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This macro iterates for each 8-bit group of bits (clump) with set bits, within a bitmap memory region. For each iteration, "start" is set to the bit offset of the found clump, while the respective clump value is stored to the location pointed by "clump". Additionally, the bitmap_get_value8 and bitmap_set_value8 functions are introduced to respectively get and set an 8-bit value in a bitmap memory region. Suggested-by: Andy Shevchenko Suggested-by: Rasmus Villemoes Cc: Arnd Bergmann Cc: Andrew Morton Signed-off-by: William Breathitt Gray --- include/asm-generic/bitops/find.h | 14 +++++++ include/linux/bitops.h | 5 +++ lib/find_bit.c | 63 +++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..457b93e6f5c9 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -80,4 +80,18 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ +unsigned int bitmap_get_value8(const unsigned long *const bitmap, + const unsigned int start); + +void bitmap_set_value8(unsigned long *const bitmap, + const unsigned long *const value, + const unsigned int start); + +unsigned int find_next_clump8(unsigned long *const clump, + const unsigned long *const addr, + unsigned int offset, const unsigned int size); + +#define find_first_clump8(clump, bits, size) \ + find_next_clump8((clump), (bits), 0, (size)) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 705f7c442691..61c10f20079e 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -40,6 +40,11 @@ extern unsigned long __sw_hweight64(__u64 w); (bit) < (size); \ (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) +#define for_each_set_clump8(start, clump, bits, size) \ + for ((start) = find_first_clump8(&(clump), (bits), (size)); \ + (start) < (size); \ + (start) = find_next_clump8(&(clump), (bits), (start) + 8, (size))) + static inline int get_bitmask_order(unsigned int count) { int order; diff --git a/lib/find_bit.c b/lib/find_bit.c index ee3df93ba69a..2e56d2b907bc 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -218,3 +218,66 @@ EXPORT_SYMBOL(find_next_bit_le); #endif #endif /* __BIG_ENDIAN */ + +/** + * bitmap_get_value8 - get an 8-bit value within a memory region + * @bitmap: address to the bitmap memory region + * @start: bit offset of the 8-bit value + * + * Returns the 8-bit value located at the @start bit offset within the @bitmap + * memory region. + */ +unsigned int bitmap_get_value8(const unsigned long *const bitmap, + const unsigned int start) +{ + const size_t index = BIT_WORD(start); + const unsigned int offset = start % BITS_PER_LONG; + + return (bitmap[index] >> offset) & 0xFF; +} +EXPORT_SYMBOL(bitmap_get_value8); + +/** + * bitmap_set_value8 - set an 8-bit value within a memory region + * @bitmap: address to the bitmap memory region + * @value: the 8-bit value + * @start: bit offset of the 8-bit value + */ +void bitmap_set_value8(unsigned long *const bitmap, + const unsigned long *const value, + const unsigned int start) +{ + const size_t index = BIT_WORD(start); + const unsigned int offset = start % BITS_PER_LONG; + const unsigned long mask = GENMASK(7, offset); + + bitmap[index] &= ~mask; + bitmap[index] |= (*value << offset) & mask; +} +EXPORT_SYMBOL(bitmap_set_value8); + +/** + * find_next_clump8 - find next 8-bit clump with set bits in a memory region + * @clump: location to store copy of found clump + * @addr: address to base the search on + * @offset: bit offset at which to start searching + * @size: bitmap size in number of bits + * + * Returns the bit offset for the next set clump; the found clump value is + * copied to the location pointed by @clump. If no bits are set, returns @size. + */ +unsigned int find_next_clump8(unsigned long *const clump, + const unsigned long *const addr, + unsigned int offset, const unsigned int size) +{ + for (; offset < size; offset += 8) { + *clump = bitmap_get_value8(addr, offset); + if (!*clump) + continue; + + return offset; + } + + return size; +} +EXPORT_SYMBOL(find_next_clump8); -- 2.20.1