From patchwork Sat Jan 12 01:48:04 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: 1023833 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZmveBxgE"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43c2fz03Xmz9sDX for ; Sat, 12 Jan 2019 12:47:39 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726227AbfALBrc (ORCPT ); Fri, 11 Jan 2019 20:47:32 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:34825 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726491AbfALBrb (ORCPT ); Fri, 11 Jan 2019 20:47:31 -0500 Received: by mail-pl1-f193.google.com with SMTP id p8so7543045plo.2; Fri, 11 Jan 2019 17:47:31 -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=illMOt5TaobGwcDI7Guq/1X4h0CJv2ntS15Gr9KbqVs=; b=ZmveBxgE1z/B5oYqjdB2x4uZybFrWzQx04VDoQhQm4vPdU9tfNMrJuE2mOhKVNJtQy aJ2hmaJsAqiF4XtxW5OXmllaQU3BtzUL1bfGOvEQFM98RcIh1OjHAbSM9TMn057b9PIu X7qyAv4GiOjLC2hA76r9NyzuZXZYNmWJeTU4aZUC+cbfnsfP+tYARAkec7kdgqxwg8La BNLVkbAVhPXbDb8VV7o5hmLibhjF1i/kQDxzHRlB5E0n4Uzt5hDoM8YlzuGUJg1VdKzd GRTLHUP3I4IVAd2nh/Qtj1+L0Ri50VgextjcMHj36lUObgtlAmJn3SRw424FA0RJJavG M0vQ== 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=illMOt5TaobGwcDI7Guq/1X4h0CJv2ntS15Gr9KbqVs=; b=o4lUSAIhtoB+NdJ4/pLueG9kpDWoa4rDKG4tN1yX32WXxPOfntSG2YNVccsja4qYxX mTBLpbwZmfon8Hsgeuv7jH3OqAHncP2WTnf2Xyr8IcT1u3SPQV86kGktX01X9ZpVEHLR EFloNAjCKZN3061HuZ1uQxyQzoLQB+ok8n2CgkvIkA0HShXk0wZRYi/Ona7CwZCjBHij KwrX0JPDHB/wN9cStYPvxWJCL+Wd/IPKudSM0yq/7ezCPuOd9hL5TNj4iyiq1+GBxT1X ALFwlWP+dWXeNLI1tr5Xop86XLBarloluoXQIu/meFGwKWLNUp0AUkZK4c/cZiri9Wo5 A7SA== X-Gm-Message-State: AJcUuke9siDIM93KDQZ1qjSr2pDSMR6pzCQ86WRLkqjHaPcAz2N9RekY 4H5IN+/bnqwdk5YyznbgNpcheWX0 X-Google-Smtp-Source: ALg8bN5uq+eJVGOV4ZinkDWFmXW1PB+ryQAxeieq4m4cPotiC8i7gin6VG7AY2orsUTRxZhRc6oWkA== X-Received: by 2002:a17:902:5ac7:: with SMTP id g7mr17110945plm.212.1547257650635; Fri, 11 Jan 2019 17:47:30 -0800 (PST) Received: from localhost.localdomain ([2001:268:c0a3:ffc5:36e2:bccb:ba63:9e84]) by smtp.gmail.com with ESMTPSA id 128sm120175296pfu.129.2019.01.11.17.47.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Jan 2019 17:47:30 -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 v7 1/8] bitops: Introduce the for_each_set_clump8 macro Date: Sat, 12 Jan 2019 10:48:04 +0900 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@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..61c496ae94cf 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 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..97d96e65a3d9 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 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; +} +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);