Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp1605279imb; Sat, 2 Mar 2019 23:49:47 -0800 (PST) X-Google-Smtp-Source: AHgI3IZI7fBejrPPuZ3Fn80u5wpK4wNbD9Rlpvob03zkYf3U6W2DJYAq6nimNeEdTxjIL39uDS56 X-Received: by 2002:a62:4793:: with SMTP id p19mr14463377pfi.76.1551599387543; Sat, 02 Mar 2019 23:49:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551599387; cv=none; d=google.com; s=arc-20160816; b=GLLMjTsH/7sSxi+LqDgBdS4KMNUMV3OffEtFiTe+8DfGi072v3EnBOaRZaeX5oysJz pOCSlyHAxn840JpaMJhQ8Kjf10xm20qM/bqiRMDjZc1alNI5/AyXab5T9j9636knyD3i SDqs/LhLtqIqxJu8apKgw1HR+Q7oySM3XvFPDMOzODeTqt+aADK0xYpAVjW9GcYBUB4H 7tHrGTgULl7xGEfMdRt+ltDw3UHYbCxsetAupLABQutGu06fgPrB/N4xdVBibGRWwydA qg3ycJ4B1ypbTgTtZFmGrxN9avFqwhowDUREpq0PXaNi7uZZwlA+73BA5i62ram+DKPk y8vA== 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=5ShWnQ4n5foUkzD6q0EUYaaxbLy/TY+i7Ft3bXpmfRg=; b=mRlV1sJIDOLFw8zyh2y6s7qAsFtDK7S8rFSTypkFmrn6czH/v4nw0Cpveo/5FJXfxU XR0KHsGSwwT0fZ35xOeWHfMdwiEjtgawPwG2poUIwAswwStpwnXj1OKQHU0VwdFE4jW8 62MOM/zRzpkR3HDsyJdDRdP2XIqptOl5puxJouyJix7QjNajFmhbkgwCDr59389BPmDN E6TPNO9fSUtBRWWhbwGcLOBBIvfWmmgONO6LjPbSAEs+IAhXoZqOrdpdwuxfBcERP/Hm jKiHUrIHP9BPmTlLQJtV5eM0NCeKyheiptN4+QNJSsfY4uM8WI98kBWwhAjDCEqSZiz+ 3nnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="K3s/GXb+"; 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 r35si2358950pgl.379.2019.03.02.23.49.32; Sat, 02 Mar 2019 23:49:47 -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="K3s/GXb+"; 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 S1726068AbfCCHru (ORCPT + 99 others); Sun, 3 Mar 2019 02:47:50 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:45969 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725877AbfCCHru (ORCPT ); Sun, 3 Mar 2019 02:47:50 -0500 Received: by mail-pf1-f193.google.com with SMTP id v21so954891pfm.12; Sat, 02 Mar 2019 23:47:49 -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=5ShWnQ4n5foUkzD6q0EUYaaxbLy/TY+i7Ft3bXpmfRg=; b=K3s/GXb+qCLU0tBYbjBhACOHRqxVXy2BWt5nyY+EvGvmDBAC7MK9VBL/i9RznuxDjb 1RWbpzO1U2Yn7QVl+uUxTwp2Lh3HJZCuy1YsIQ+SnBUBW+OSxZ47/stUiOUO1X24WApq kUpZmhyburujHHRvLdYqnB6KXa6+PJH1SF93j9FUT8Zts5rQf2RcQ46nZscCTFjljzEu M3CoCh/u7zsq9wqWYQF4W0llTsv/YS10EYOrCfCDlpCE91P51dqtXii2ghQHWOckIPe1 FyuyzDMT0DXMpv8gtxsVodAw8oPkNg/2X1sQxVj1ODG1pV0zkIyKX392ccLS28bpeedJ Xp8A== 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=5ShWnQ4n5foUkzD6q0EUYaaxbLy/TY+i7Ft3bXpmfRg=; b=L8kfIErlfxGqJIECI5Fh7wmJy/VAaJnmvnyKHixp9ooA4ZJP0RvN7pallubkoac3x3 BszgTr/Oj+CvqlLyW27dMH9BEF0AIHhtuW00w+ItqH9JmsoEFHuu6WUcmL0mggjNufYJ 75OeJMOegSfRTRWXBJquhc29gFHXDJVR+cvCtdC6qzocm380uiO7eQprnRiBZokJw2hc +YiLhoL/D/bxZz+A3DKG0K6HSrtR2mYLuZD0QPgN6xuIx6SwzBho9PQhyMRE4B0V6l05 prj0fnjKzWcDsfiyS5B/dxtTZouOuKxHmvPBgFqLUiR9XPUXB3/YZj2CF0WWvjn7o83Z 0n0A== X-Gm-Message-State: APjAAAUgVpArhY1tNXwvWqBU8T70FcBuWCvjNjTgmD7c9rO+9Rgrg2u+ D3fkHRIgyzR12QiTjdp6tvc= X-Received: by 2002:a63:8042:: with SMTP id j63mr12561873pgd.36.1551599269023; Sat, 02 Mar 2019 23:47:49 -0800 (PST) Received: from localhost.localdomain ([2001:268:c0a5:3b5c:c70:4af9:86e2:2]) by smtp.gmail.com with ESMTPSA id s1sm3443392pgv.30.2019.03.02.23.47.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 02 Mar 2019 23:47:48 -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, yamada.masahiro@socionext.com, bgolaszewski@baylibre.com, linux-arm-kernel@lists.infradead.org, William Breathitt Gray , Andy Shevchenko , Arnd Bergmann Subject: [PATCH v9 1/9] bitops: Introduce the for_each_set_clump8 macro Date: Sun, 3 Mar 2019 16:48:36 +0900 Message-Id: <12b3a364ac7fc32194fa6f55911dd800aca92a56.1551598603.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.21.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 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 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); -- 2.21.0