From patchwork Fri Mar 29 03:04:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 10876235 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6412B1669 for ; Fri, 29 Mar 2019 03:03:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E8FA2909E for ; Fri, 29 Mar 2019 03:03:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 42F3A290A2; Fri, 29 Mar 2019 03:03:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 792A52909E for ; Fri, 29 Mar 2019 03:03:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728648AbfC2DDN (ORCPT ); Thu, 28 Mar 2019 23:03:13 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:43574 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727654AbfC2DDN (ORCPT ); Thu, 28 Mar 2019 23:03:13 -0400 Received: by mail-pg1-f193.google.com with SMTP id z9so468739pgu.10; Thu, 28 Mar 2019 20:03:12 -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=K34goq+zf1vvl+0PBwq115Mp4iHOPY/zupPfI+vUQAE=; b=V5Efa6TNudb05BAu+jsft37SCTSyK3Nz05lSgABhwTr9dPRAszIcUwdt+nYaBN1YUq nnkABvG5YB0g+mbL+H4YOeXLTLMWVfra7pf9w1hi3tgYPFOqdXEq4YgMudGJ6RXlcPIn 5c+GFggYuTno7DCS2U+hdFm9EvEUaH7glYeCT87Mm6zWAm+wbuFqKOQp8QmPPHFR3Etw 83TK9UyeSc17lP/TJB+beQzngcxHo6eJ/qlRplHyCLTz7Z2R419iblqcXAX+iG+2EMiy KqpBpTbVsPCTTxQ0ISWh3jNC/Q2NdMlQjPmN3vqJPH4nLlS4MCIiDiRrUSZCGIkMxsuG EE/w== 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=K34goq+zf1vvl+0PBwq115Mp4iHOPY/zupPfI+vUQAE=; b=OPwCsBF6ZRRiL8yWh+kJOu4XxOa8Mng/Y+oZ+xOCTdO3Rom20ZjKK3aV8JlYt6cDeX 8PaxaDNKi3R0iqESOfweoO68bocXl1dm7AeCuZQdD6wOO9I2cuELgL3Y713QbKXAeheq YCl/b6iCqlciwMORRpbbivtTtw36SAzPagkX1snlUrNkv5Tt08RebBaJ59XA6ZtQz1p+ 5Dmi7bmUVicckvTdmb8CYGJtZiM3eJ5NiJ/jgmtxePOwP9s4gvfDQ/b4pO+ogNjTMNZy d4khGkIGyR2DZG8K8r+z3b5gcFduS1GVzypGXRESWWcQandyoGb+9yPeiAgwLDULgP0g ESlA== X-Gm-Message-State: APjAAAW68uWQSanCSxmZTbZOVXbgjQwxVA4kHQqW/Hjut16kx/ndc4Gy b/GRPB5O50y7PbxmH4LhaGE= X-Google-Smtp-Source: APXvYqyMfQEASrbwMcf+1MYodXZ4CyjTM5wNZZ2aZtGMR/8wEoFz1iHj6gTxqTHYRgJfKOFim8ijag== X-Received: by 2002:a65:4542:: with SMTP id x2mr42882493pgr.65.1553828592563; Thu, 28 Mar 2019 20:03:12 -0700 (PDT) Received: from localhost.localdomain ([2001:268:c0a5:ba2:c70:4af9:86e2:2]) by smtp.gmail.com with ESMTPSA id x9sm730532pfn.60.2019.03.28.20.03.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Mar 2019 20:03:11 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org, bgolaszewski@baylibre.com 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, yamada.masahiro@socionext.com, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org, geert@linux-m68k.org, preid@electromag.com.au, lukas@wunner.de, William Breathitt Gray , Andy Shevchenko , Arnd Bergmann Subject: [PATCH v14 01/11] bitops: Introduce the for_each_set_clump8 macro Date: Fri, 29 Mar 2019 12:04:08 +0900 Message-Id: <1cc8b30bb3954ca2a0961ffb0a2eed8a005ed670.1553828158.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Suggested-by: Lukas Wunner Cc: Arnd Bergmann Cc: Andrew Morton Cc: Linus Walleij Acked-by: Andy Shevchenko Signed-off-by: William Breathitt Gray Reviewed-by: Linus Walleij --- include/asm-generic/bitops/find.h | 61 +++++++++++++++++++++++++++++++ include/linux/bitops.h | 5 +++ 2 files changed, 66 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..45aa6d718cbd 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -80,4 +80,65 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ +/** + * bitmap_get_value8 - get an 8-bit value within a memory region + * @addr: address to the bitmap memory region + * @start: bit offset of the 8-bit value; must be a multiple of 8 + * + * Returns the 8-bit value located at the @start bit offset within the @addr + * memory region. + */ +static inline unsigned long bitmap_get_value8(const unsigned long *addr, + unsigned long start) +{ + const size_t index = BIT_WORD(start); + const unsigned long offset = start % BITS_PER_LONG; + + return (addr[index] >> offset) & 0xFF; +} + +/** + * bitmap_set_value8 - set an 8-bit value within a memory region + * @addr: address to the bitmap memory region + * @value: the 8-bit value; values wider than 8 bits may clobber bitmap + * @start: bit offset of the 8-bit value; must be a multiple of 8 + */ +static inline void bitmap_set_value8(unsigned long *addr, unsigned long value, + unsigned long start) +{ + const size_t index = BIT_WORD(start); + const unsigned long offset = start % BITS_PER_LONG; + + addr[index] &= ~(0xFF << offset); + addr[index] |= value << offset; +} + +/** + * 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 + * @size: bitmap size in number of bits + * @offset: bit offset at which to start searching + * + * 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. + */ +static inline unsigned long find_next_clump8(unsigned long *clump, + const unsigned long *addr, + unsigned long size, + unsigned long offset) +{ + offset = find_next_bit(addr, size, offset); + if (offset == size) + return size; + + offset = round_down(offset, 8); + *clump = bitmap_get_value8(addr, offset); + + return offset; +} + +#define find_first_clump8(clump, bits, size) \ + find_next_clump8((clump), (bits), (size), 0) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 602af23b98c7..1d9b5efb9bd4 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), (size), (start) + 8)) + static inline int get_bitmask_order(unsigned int count) { int order;