Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp732267imm; Mon, 1 Oct 2018 18:14:23 -0700 (PDT) X-Google-Smtp-Source: ACcGV622R25Q7yb5T61ruRL4IPVFUm57VbHBtGQMsvd311xNxVyx1zAk1rTmPYmOtb00AOtuLzO5 X-Received: by 2002:aa7:86cc:: with SMTP id h12-v6mr7816594pfo.58.1538442863231; Mon, 01 Oct 2018 18:14:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538442863; cv=none; d=google.com; s=arc-20160816; b=ewk+PSCN++EAPucZBqJKA3ikPqiE13FKnoTxRO+dkHzLs6IwROTqdMtd1q8SfU1gnt gJ99TfiDOG7jBthHiYUUmMkLoTiAltJCHa9h4GWK2oZmG4HzrIOFn0+o1vIx1X3v+uFN 9vHeJWxk5xxToFExybycPnAZE/pTerCMupXIzVb1hb+prcilhPxNGJ6OVJIqO1Pw7C3W tXDi+wPcVqj69wA8avaeuh39zhRFk6evccdTW30K/mPlaA1smksXWhieXRaFUFIsLvrc FAm/95Tlli7gjWqo6LrMMd4oxfdv6tjspNt+UULnoMmR5cc8glpaZ6YPvWlQmjbmurou K3Wg== 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=jfa2koyh7eVMIEAS5/kPyF27HM45LnvHcNpvnhOSl4Q=; b=gPH+DMveGs5ohHzRLJr+4pvcZSHUj1sT9Ou2i4c8Z4MmEPK2PyA0r/ejA4qK/vGZKQ 0oeR/HIvmZLKbUkdoVfGMlTxrGj7/3NULeg3G9oW2vg25wc3sUSRoFq6lzrd3tdOLT4E xMeGNiaYURR/7enTDLZBjaHd5h5P7uKRq90XKszKWvbAm1GQe5S1y22GWovcPDeEve75 PxFCBM+IdFnKnWp8nAiOVxI7rYTYVCxlP1bI9qUBiP8r9SB6iEeibyx+2h3leVaNWCXi jDKUHByrO6gTW0vAxeo/R7NtY2HOThMKf0VhEw02rdW9cjat2zLUow/CAn0VFayklrOX BoSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TSKOvFCZ; 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 i37-v6si13422041pgi.15.2018.10.01.18.14.08; Mon, 01 Oct 2018 18:14:23 -0700 (PDT) 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=TSKOvFCZ; 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 S1727345AbeJBHyQ (ORCPT + 99 others); Tue, 2 Oct 2018 03:54:16 -0400 Received: from mail-it1-f193.google.com ([209.85.166.193]:53804 "EHLO mail-it1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726459AbeJBHyQ (ORCPT ); Tue, 2 Oct 2018 03:54:16 -0400 Received: by mail-it1-f193.google.com with SMTP id q70-v6so1047329itb.3; Mon, 01 Oct 2018 18:13:42 -0700 (PDT) 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=jfa2koyh7eVMIEAS5/kPyF27HM45LnvHcNpvnhOSl4Q=; b=TSKOvFCZVPqbXYdOpuJlMXh6MmfgsVMjvwGmaZWaEp75dkL9b9MK8+lwZPZ5YFkD3I /WeWQHv8zDON8XVSnMgsk+pxqk33ovassq79VNxpBqDopi+U/cL4aJQMxTZRgZiuat9d pwt53/Fi6dpWiV4iWt6mJ+qQ6I+WDt4umS3qZlU9cDXWFh1sile+5XsLMCmHSQD2ZzAx Xc557OuP4ArCUs3v8mDQYeVTokJc9xIaofbiLSHNd+ysDR3GIeKM7xluxBY6rF4v1nGS yJrTITsn4f9ZjrImPQ446rFyg0rHX4xqfODFiTV6tvHai4i+We1inCLSc7asquCaY1Cb CfSw== 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=jfa2koyh7eVMIEAS5/kPyF27HM45LnvHcNpvnhOSl4Q=; b=SMOGL3vNWu/0S03+wzUsYuyDD4uQ2LmIWP2T0CfeqQTBd7KGcd7bQ1H6Ox7A9qKtfx uT/QvaEI5Z2ypR/PEGM3c/TYz+g5lkcyy4ElNMG10tYB33JNUMeqPsx+ERhLWMSt3Yfh JfyhvCVKSvFn1cGtrukcyXU82GqIjQVdqKFw6bhE+33zcTSg7U9GlF2IBhGW43Kw7BvA kIkCFrp6GqwFJH3VLy+DCZmcNng6aN4dFq+z3rorKVhMkCv5p+xN+GMOlu1kOEaztCFR aOEwf7uPtR0UV71UKx8ipJweXaRDvdN9/01WbwzKBE1vC6G76QarZeyw0UTk592AjDn5 PS2Q== X-Gm-Message-State: ABuFfogYGd6M5pqTFwj6htCWDYZxGsU3cB0qGZ6bRlQ0sZvG4W7G3tCM esEEijWPTBT7PxXLk9i0FNABSIHE X-Received: by 2002:a65:5b07:: with SMTP id y7-v6mr12589723pgq.125.1538442821989; Mon, 01 Oct 2018 18:13:41 -0700 (PDT) Received: from localhost.localdomain (122-223-50-245.fukuoka.fdn.vectant.ne.jp. [122.223.50.245]) by smtp.gmail.com with ESMTPSA id v38-v6sm18783549pgn.57.2018.10.01.18.13.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Oct 2018 18:13:41 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org, akpm@linux-foundation.org Cc: 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 , Arnd Bergmann Subject: [RESEND PATCH v4 1/8] bitops: Introduce the for_each_set_clump macro Date: Tue, 2 Oct 2018 10:13:44 +0900 Message-Id: X-Mailer: git-send-email 2.19.0 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 group of bits (clump) with set bits, within a bitmap memory region. For each iteration, "clump" is set to the found clump index, "index" is set to the word index of the bitmap containing the found clump, and "offset" is set to the bit offset of the found clump within the respective bitmap word. Suggested-by: Andy Shevchenko Cc: Arnd Bergmann Cc: Andrew Morton Cc: Rasmus Villemoes Signed-off-by: William Breathitt Gray --- include/asm-generic/bitops/find.h | 9 +++++++ include/linux/bitops.h | 7 ++++++ lib/find_bit.c | 40 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..3d3b2fc34908 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -2,6 +2,8 @@ #ifndef _ASM_GENERIC_BITOPS_FIND_H_ #define _ASM_GENERIC_BITOPS_FIND_H_ +#include + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region @@ -80,4 +82,11 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ +size_t find_next_clump(size_t *const index, unsigned int *const offset, + const unsigned long *const bits, const size_t size, + const size_t clump_index, const unsigned int clump_size); + +#define find_first_clump(index, offset, bits, size, clump_size) \ + find_next_clump((index), (offset), (bits), (size), 0, (clump_size)) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 7ddb1349394d..089381017f74 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -40,6 +40,13 @@ extern unsigned long __sw_hweight64(__u64 w); (bit) < (size); \ (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) +#define for_each_set_clump(clump, index, offset, bits, size, clump_size) \ + for ((clump) = find_first_clump(&(index), &(offset), (bits), (size), \ + (clump_size)); \ + (clump) < (size); \ + (clump) = find_next_clump(&(index), &(offset), (bits), (size), \ + (clump) + 1, (clump_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..1d547fe9304f 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -218,3 +218,43 @@ EXPORT_SYMBOL(find_next_bit_le); #endif #endif /* __BIG_ENDIAN */ + +/** + * find_next_clump - find next clump with set bits in a memory region + * @index: location to store bitmap word index of found clump + * @offset: bits offset of the found clump within the respective bitmap word + * @bits: address to base the search on + * @size: bitmap size in number of clumps + * @clump_index: clump index at which to start searching + * @clump_size: clump size in bits + * + * Returns the clump index for the next clump with set bits; the respective + * bitmap word index is stored at the location pointed by @index, and the bits + * offset of the found clump within the respective bitmap word is stored at the + * location pointed by @offset. If no bits are set, returns @size. + */ +size_t find_next_clump(size_t *const index, unsigned int *const offset, + const unsigned long *const bits, const size_t size, + const size_t clump_index, const unsigned int clump_size) +{ + size_t i; + unsigned int bits_offset; + unsigned long word_mask; + const unsigned long clump_mask = GENMASK(clump_size - 1, 0); + + for (i = clump_index; i < size; i++) { + bits_offset = i * clump_size; + + *index = BIT_WORD(bits_offset); + *offset = bits_offset % BITS_PER_LONG; + + word_mask = bits[*index] & (clump_mask << *offset); + if (!word_mask) + continue; + + return i; + } + + return size; +} +EXPORT_SYMBOL(find_next_clump); -- 2.19.0