Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3674539imu; Tue, 18 Dec 2018 02:13:06 -0800 (PST) X-Google-Smtp-Source: AFSGD/VK0wXF9sCMJpVM2o9PEdlWnlkmCKDvR4GOsmhlW+nwfq3vhz3T5AMBcnVUwwkDHFCQ1ubw X-Received: by 2002:a17:902:8346:: with SMTP id z6mr15804363pln.340.1545127986303; Tue, 18 Dec 2018 02:13:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545127986; cv=none; d=google.com; s=arc-20160816; b=MeAOdh0FN6oQNvOrVcGJ3LhKaGpD4TvdRxFWWF/YbDhZzgnSrXSTUGo6bmisip7xTZ wK/0CTh2hhWVdhfO2H8TYoTwlQltMOEl4J7FTJr86NHIKBOlCqUPpqZrM5EOdTH/rsix i3yimsYoOEUAWAD1RLikqagJPXnNccVUrJnV+xQosIuQznX/dDrlMks7oJ5JjgUY8kHe Puj6gmoCRJ0jg92ivS1Z58SCupe4Br6xY4NryqXGkqjNyn7YfpnP8kEPk+S1Xfr2K7Sw DymDj+npO7ETXuyPjUf5F1jyaOW3POw6Qpaznd2w5IsgF6VckwebBbNu0m6Z1o4HuVle hiaw== 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=CxXXUv+EgkVrU6KdE+cFR4qqon7JP0P+l/dMlMJYIp0=; b=rehLWSl22A0xlEq8YYQA2YSgeuPG31vm03N86J/CMKJWXA3moJP8cuMSq41W8HSH10 CMnaHWiDCwav8Fmtn+YUFN4PbPJBVlHVGbgiPx2phJ7/ynbKX3Wn9X6t0LbackJ8poQ/ 6yIIBh3h3c10qPA60SilWAF9MzjWcPNMgMJ7tY2D3Vq/wBEBNFklw1Cs0pqwVl57wTjq AaHiDfVgmo/JmoIn94gGQJkt++sVH2+lwyB68/Yr6jmlpD/LJjRlCLoXq9w6j6GRoZJC jMG1ZyAZUXJMBDx7xN1Q4bubAikqRaa1HopS6P4NgtnUuu6r1xwmO756SNGOK0DUNQM8 UHXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oWyavLmb; 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 x186si1487877pfx.269.2018.12.18.02.12.50; Tue, 18 Dec 2018 02:13:06 -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=oWyavLmb; 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 S1726545AbeLRKLm (ORCPT + 99 others); Tue, 18 Dec 2018 05:11:42 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39704 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726364AbeLRKLm (ORCPT ); Tue, 18 Dec 2018 05:11:42 -0500 Received: by mail-pf1-f196.google.com with SMTP id r136so855562pfc.6; Tue, 18 Dec 2018 02:11:41 -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=CxXXUv+EgkVrU6KdE+cFR4qqon7JP0P+l/dMlMJYIp0=; b=oWyavLmbkSMk/CnwrLC4jXM8RdYEz51OStmyxaKPcgo8p9DiaJL9ehfOJUAWKfCLcr OTd/nvoSICO7oAqxv1AqnZmSCCN0rKG1cq/o6saBP4CqmmlmgJhRQsSzEALLOcwEaSpa 9lD4UMpxXU0u6nmzIwn90CsQjK2n7FlJI4c2F4wBZqc9WSjAa62yhTOn1UFqVuPkJQws sdwepQ4EBDrEYkbJp1EZjpho1zGb5apWJW3D+FaJTRw0XYK6S9eTJE4FxU93ihVteNWY MIUaauJXiGEHkfoRpwR1DlSjKIywvij9PXdqAZGIjypzR7+b3rW43Ncdg4d3ymQ0Q4rb bOFg== 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=CxXXUv+EgkVrU6KdE+cFR4qqon7JP0P+l/dMlMJYIp0=; b=DFCiBrLB6lvr2OYf2P8hx4MSfo8kYAQq1Gnimyld93YqK97cMbmwXapu9OGkZB/7V4 tVmVym+WIb9K/EFLSL2dH+tmpq3alTrtZZsePm4ajTqb60yeaW5fbicyYwFMD5BPjpWU ymMQWUkh3C0GckBanfTeui/sF1xS4FAzcudbgdlf+pdeJhLUAL8vEOIaqTp4C/Iz6VUr l6ReSJAYfdKFO4gblkl7Cz0C3zZe7ZiABYE4X2Toor7tUdzWMjqaRtnfzsYK9ObXFSHN h9E0CeeH78m5ikz6veY7dCBbfECA/U62gDr/Ll7zwFLkpkbwH0E1wzIjwbt3xupDCmds eWjQ== X-Gm-Message-State: AA+aEWZVjjgWGABjw+SfFLbLEHbxLBz3V0fRZRL++9Uh76tdEkStAaZx G4yvuv+3wKyeAAVESS6zEU4= X-Received: by 2002:a63:f515:: with SMTP id w21mr15176931pgh.220.1545127900916; Tue, 18 Dec 2018 02:11:40 -0800 (PST) Received: from localhost.localdomain ([2001:268:c0a5:b5e9:3c79:e26e:bf45:b953]) by smtp.gmail.com with ESMTPSA id m19sm38420743pgn.35.2018.12.18.02.11.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Dec 2018 02:11:40 -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 v5 1/8] bitops: Introduce the for_each_set_clump8 macro Date: Tue, 18 Dec 2018 19:12:09 +0900 Message-Id: <40f8a86600f6c741b8c577dc821042d5a250f95f.1545126272.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.20.1 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 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..457b93e6f5c9 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 *const 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..850af60d36b9 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), (offset) + 1, (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..2e56d2b907bc 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 *const 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) & mask; +} +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); -- 2.20.1