From patchwork Thu Mar 14 12:30:02 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: 10852709 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 3F71C139A for ; Thu, 14 Mar 2019 12:29:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2528E286EC for ; Thu, 14 Mar 2019 12:29:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 194312A006; Thu, 14 Mar 2019 12:29:21 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8015E29EF5 for ; Thu, 14 Mar 2019 12:29:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=W/5GS66YYQKdjerXP8slR4oeAyQdvJBU8Beb0XK1v4A=; b=FoFlc9FI2UlQB1 i0qsYs7lkUejp7U+1OrmreC5jYCG+/UAUvSpHTN5+ZmJB5d979ihXXsedBZNRyeBj52DKRM1nfoAz eEd0TjZw/DES5JyaMcohphG+4K6nvdLl/HASkmGEGy3uYnM4Exdq512ksSMLXhM5NWnzZjga2xLF9 VZEeJ8GwN9glrHIA5BTKnF3HmDthAjopBxgypzSwWzwvzYWJN07QziUpok87sC78WfFWvJGOj7ghE By2sgYdkgzZ6J1TGodPPyD7Sa89Uc1r5W67bhEClnll5BOlNmfW2E7ZdOjGbKiwttoqcq6/xHIonV UVfu8dB2fDE9SYk0UKHA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1h4PU6-0005UH-Gs; Thu, 14 Mar 2019 12:29:18 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1h4PU2-0005TB-0m for linux-arm-kernel@lists.infradead.org; Thu, 14 Mar 2019 12:29:16 +0000 Received: by mail-pg1-x541.google.com with SMTP id e17so3882383pgd.2 for ; Thu, 14 Mar 2019 05:29:13 -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=fq6Xm53C+heMXT2c+ND0di1HUX14yEET8qKlzoFNfpw=; b=Ck7GLcpyG6wLhHl5A0PNRGDMVGSWdvreR9shUs1kcEsyr2w4FWN/ygos1sZsT+cdCi 7dTW4CTApaRASe7cnYM84Szvazoj5wTcuCpYy76YpyGYSPVhNSAMI+EYoQ76cPzQfhiA OIJjRDAy5iTMszFp5hdA0uYdMOq8ncPn7ClCUCxsW7bSyAdcMBE79dpg3fF7m1mFUNFs xh7W9PGxIafqueYac7Nhdoi5FXQucwjz9D/MMRabWS9HNyhBMmw+2wnTlOAJlIUeiWoR pcCq/VDhlJn+SGD96E1MC1Om44ckVDoZJWQh+huf3+2u475PTfSiu3vfIOS7xqbILtmD DaEQ== 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=fq6Xm53C+heMXT2c+ND0di1HUX14yEET8qKlzoFNfpw=; b=OczM1RN/lhUv+8TdTpZ4IlhsjP7Yfym3x/rfJI41H3RT2NOPp7mRG3hIIxERnUxeH9 dzxdCqHo8IoBprG74OlvTvNTGByGphQRzXTFYgHKBeIP0Jdn64oLkzRouWcdluGf0RTm HwEUckm4tX3ePjmO7kX8ZtqT8ZkSCXxiyl3QJUE8djriEtBqelx+8jnpZPzMGg9gwPZQ 9mHzaHwhs17aZk4QpEIvDOWj+H6ob5bBGlfdHLWKWcdeA2b598zBR8qw8plwqRt1j8cZ o6wGiCeqlCpZp5RyZkIiwOuNemeO3lo1WCfLrmnZML9OgeOVs6fQW7yinWX9zAt3WIvP w6SA== X-Gm-Message-State: APjAAAXPGyUl4VcOAV1uMPvpxKFkM3cBJ6jTG/wQ7o898jHkyUp9MRfs JRLg7MJnIQcmTFPv9orNP60= X-Google-Smtp-Source: APXvYqxzAQX9w1BWqiu5T2oM2H1q3X0lR4pNz3Z8C9cG00cB+QxlRrzSofJUB/mxr/DW4W0PAImhEg== X-Received: by 2002:a17:902:112c:: with SMTP id d41mr51072666pla.177.1552566553320; Thu, 14 Mar 2019 05:29:13 -0700 (PDT) Received: from localhost.localdomain ([2001:268:c0a4:3974:c70:4af9:86e2:2]) by smtp.gmail.com with ESMTPSA id g2sm26358279pfm.144.2019.03.14.05.29.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Mar 2019 05:29:12 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org Subject: [PATCH v10 01/10] bitops: Introduce the for_each_set_clump8 macro Date: Thu, 14 Mar 2019 21:30:02 +0900 Message-Id: <0e4d352418252d480dfc7d529604819f8ff88d9c.1552566113.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190314_052914_059019_E2145BC7 X-CRM114-Status: GOOD ( 14.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, Arnd Bergmann , yamada.masahiro@socionext.com, linux@rasmusvillemoes.dk, linux-kernel@vger.kernel.org, William Breathitt Gray , linux-gpio@vger.kernel.org, Andy Shevchenko , bgolaszewski@baylibre.com, akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.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 Cc: Arnd Bergmann Acked-by: Andrew Morton Reviewed-by: Andy Shevchenko Reviewed-by: Linus Walleij Signed-off-by: William Breathitt Gray --- include/asm-generic/bitops/find.h | 14 ++++++ include/linux/bitops.h | 5 ++ lib/find_bit.c | 81 +++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..9a76adff59c6 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 long bitmap_get_value8(const unsigned long *const bitmap, + const unsigned int size, + const unsigned int start); + +void bitmap_set_value8(unsigned long *const bitmap, const unsigned int size, + const unsigned long 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..c2af1f013ea2 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -218,3 +218,84 @@ 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 + * @size: bitmap size in number of bits + * @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 long bitmap_get_value8(const unsigned long *const bitmap, + const unsigned int size, + const unsigned int start) +{ + const size_t index = BIT_WORD(start); + const unsigned int offset = start % BITS_PER_LONG; + const unsigned int low_width = (offset + 8 > BITS_PER_LONG) ? + BITS_PER_LONG - offset : 8; + const unsigned long low = bitmap[index] >> offset; + const unsigned long high = (low_width < 8 && start + 8 <= size) ? + bitmap[index + 1] << low_width : 0; + + return (low | high) & 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 + * @size: bitmap size in number of bits + * @value: the 8-bit value; values wider than 8 bits may clobber bitmap + * @start: bit offset of the 8-bit value + */ +void bitmap_set_value8(unsigned long *const bitmap, const unsigned int size, + const unsigned long value, const unsigned int start) +{ + const size_t index = BIT_WORD(start); + const unsigned int offset = start % BITS_PER_LONG; + const unsigned int low_width = (offset + 8 > BITS_PER_LONG) ? + BITS_PER_LONG - offset : 8; + const unsigned long low_mask = GENMASK(offset + low_width - 1, offset); + const unsigned int high_width = 8 - low_width; + const unsigned long high_mask = GENMASK(high_width - 1, 0); + + /* set lower portion */ + bitmap[index] &= ~low_mask; + bitmap[index] |= value << offset; + + /* set higher portion if space available in bitmap */ + if (high_width && start + 8 <= size) { + bitmap[index + 1] &= ~high_mask; + bitmap[index + 1] |= value >> low_width; + } +} +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, size, offset); + if (!*clump) + continue; + + return offset; + } + + return size; +} +EXPORT_SYMBOL(find_next_clump8);