Received: by 10.192.165.148 with SMTP id m20csp3220129imm; Mon, 7 May 2018 08:36:06 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqnWAkWtEWnkNKe+L9gxkpqhHkv1joSnh+8YSX2YFCHTvvs7Bjp3S3ABo6m5K4Kf9I0u9v3 X-Received: by 2002:a6b:3049:: with SMTP id w70-v6mr39844611iow.12.1525707365236; Mon, 07 May 2018 08:36:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525707363; cv=none; d=google.com; s=arc-20160816; b=BHlZlnED2WqPUOsmvJPr2P1Chi6gsVZxMuKMK8xeWsAFUP13PR+Og7ebw3BAxByNGQ bl+VJ0JOgnoowkbEfZUIS/ynSICShuO3+zPFY8qX1CDd3FdntFBRFRAhE7A+YwgSHuov mlSA1PaGyCgNe6OAydk7yWpHaJZ0va5cI7Y46Mp6QsDsttUeYOmKyJd7P/mUxoyepGxO YDMt2son/hYjY93zQP2WQESNi3HuVd7hlr8Khg/3bf/MJ6W93k43i0ljf2E0QhOevTly MBlNaS+VCexVZl5QHOgukHNUU6pgcgJ9Bou62gk9gl7joclIRl3eGMhd+d61gqfyT7rz wiPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=SYqjrGM4rdL/qFsrD90ydFOcJqXW8BhXuyIbZcJ9vN4=; b=VTlwx21VLdYu6KoWMLXQxwTbaRI8vqxWbvgSJW+98XlW8l/WtRVWQrWklk9oZ9U8DO sTa4qCmyqFROIpxotEIDxXxSiwnDvq2yKpiQBrvskKwrVjF4XRGM3giDh9wYd0fpOOVM LwlS5S7nAKcaNNofclV5DTDDqvfK3IqrjICRpbVzP+p4eyDyOqL5T7RFP5DdBNCWXQ8+ kVTitCWA1D+nSok7KjNoMLYAphPvGfryy4uJ2EXhtARUArGTfoAc23VDqWSOyRjhSHU9 LcxMJiMZzY3AbHPklW2CsDRXDh/mJHwlkG9SIQmN8Mt70A8YT7538AJrv5D3dyNU2hOl 6Cmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dl5XHRTS; 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 l184-v6si9570762ioa.6.2018.05.07.08.35.50; Mon, 07 May 2018 08:36:03 -0700 (PDT) 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=dl5XHRTS; 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 S1752204AbeEGPf3 (ORCPT + 99 others); Mon, 7 May 2018 11:35:29 -0400 Received: from mail-yw0-f195.google.com ([209.85.161.195]:41638 "EHLO mail-yw0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751913AbeEGPf0 (ORCPT ); Mon, 7 May 2018 11:35:26 -0400 Received: by mail-yw0-f195.google.com with SMTP id q125-v6so4632079ywc.8; Mon, 07 May 2018 08:35:26 -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; bh=SYqjrGM4rdL/qFsrD90ydFOcJqXW8BhXuyIbZcJ9vN4=; b=dl5XHRTSk763x0Er3MpJ0bsfT4QAUtHTZL0Fo3WwquJ6bP6E3gFT84fzD15I0WlxcI MFCOdZCz96ZnXaGmvI1xRTJh1WBYt6j3zvdNAENDiurUlxn82hrtvWUOVNg0jGTVYGY+ ElJkXA32SenzSzfx4ZGSmXVmP7nPneOvyYcfdq5xS35UVzAmv4v8C3TuD0DBbThtU+o2 rE23loNOOjdJc51sTkw6BBfuDW/gSL9yvVkDYRSmy9Qq92aFFgqxstm5Q2HQ38I7MGp0 scTocAuDnBIRVUkRYWqG1fgrQbPRD/6kQZ1F4RQJFfQmbQAe0bo0lgeLunvWI1BIR8g9 s28A== 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; bh=SYqjrGM4rdL/qFsrD90ydFOcJqXW8BhXuyIbZcJ9vN4=; b=OqFMuvr5JasQj7cYsd/o9oOdZ9/5qL9MH8myqRdUoOMqtniTK5GP061kgw4R1Hz7p3 Yu7xnDxzaiUgEpfImv4Ye+zJU/otZ5IDXGAeHNGAhDVtu5OXQ7b6bf0L4HMwj6R7obd9 OaEZCC6re3YWlc8JPeKsT80ylcgj9vALnJng3XW83KGWzCW2OADg6A/Y6bkKzxb9VeEB HqN3CKsvEWU1pqEEUZn7eAO6+J2X7u80yLfdelvgMODMoj3Xd4IzAqviaii5a/IH/++a 5DeNKqos1eSd+52j4zDqcZJGlx36cIdYar42RJSJigmIkrP3Rbhaplu/EFDzAPTsrs3H /y4A== X-Gm-Message-State: ALQs6tCX5VjzHxZKZBIqvKiTdsXtswljdGDmMD+MnqbWezs2oXtdw4QJ hHzqNY1rNn/CXJoA+8EnGdGSoA== X-Received: by 2002:a0d:d995:: with SMTP id b143-v6mr21012122ywe.220.1525707325888; Mon, 07 May 2018 08:35:25 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id g2-v6sm9912190ywa.63.2018.05.07.08.35.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 08:35:25 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org Cc: linux-gpio@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray , Arnd Bergmann Subject: [PATCH 1/7] bitops: Introduce the for_each_set_port_word macro Date: Mon, 7 May 2018 11:35:18 -0400 Message-Id: <7b704b1feb2f2cdb6c3a3061fa5f5d804954d15d.1525704095.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This macro iterates for each group of bits (port word) with set bits, within a bitmap memory region. For each iteration, "port_word" is set to the found port word index, "word_index" is set to the word index of the bitmap containing the found port word, and "word_offset" is set to the bit offset of the found port word within the respective bitmap word. Cc: Arnd Bergmann Signed-off-by: William Breathitt Gray --- include/asm-generic/bitops/find.h | 24 ++++++++++++++++++++++++ include/linux/bitops.h | 9 +++++++++ lib/find_bit.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..b24f3f92642b 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -80,4 +80,28 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ +/** + * find_next_port_word - find next port word with set bits in a memory region + * @word_index: location to store bitmap word index of found port word + * @word_offset: bits offset of the found port word in respective bitmap word + * @bits: address to base the search on + * @size: bitmap size in number of ports + * @offset: port word index to start searching at + * @port_size: port word size in bits + * + * Returns the port index for the next port word with set bits; the respective + * bitmap word index is stored at the location pointed by @word_index, and the + * bits offset of the found port word within the respective bitmap word is + * stored at the location pointed by @word_offset. If no bits are set, returns + * @size. + */ +size_t find_next_port_word(size_t *const word_index, + unsigned int *const word_offset, + const unsigned long *const bits, const size_t size, + const size_t offset, const unsigned int port_size); + +#define find_first_port_word(word_index, word_offset, bits, size, port_size) \ + find_next_port_word((word_index), (word_offset), (bits), (size), 0, \ + (port_size)) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 4cac4e1a72ff..c3b7caf4ad2d 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -59,6 +59,15 @@ extern unsigned long __sw_hweight64(__u64 w); (bit) < (size); \ (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) +#define for_each_set_port_word(port_word, word_index, word_offset, bits, size, \ + port_size) \ + for ((port_word) = find_first_port_word(&(word_index), &(word_offset), \ + (bits), (size), (port_size)); \ + (port_word) < (size); \ + (port_word) = find_next_port_word(&(word_index), &(word_offset), \ + (bits), (size), (port_word) + 1, \ + (port_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..628c94d63d6b 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -20,6 +20,7 @@ #include #include #include +#include #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ !defined(find_next_and_bit) @@ -218,3 +219,30 @@ EXPORT_SYMBOL(find_next_bit_le); #endif #endif /* __BIG_ENDIAN */ + +size_t find_next_port_word(size_t *const word_index, + unsigned int *const word_offset, + const unsigned long *const bits, const size_t size, + const size_t offset, const unsigned int port_size) +{ + size_t i; + unsigned int bits_offset; + unsigned long word_mask; + const unsigned long port_mask = GENMASK(port_size - 1, 0); + + for (i = offset; i < size; i++) { + bits_offset = i * port_size; + + *word_index = BIT_WORD(bits_offset); + *word_offset = bits_offset % BITS_PER_LONG; + + word_mask = bits[*word_index] & (port_mask << *word_offset); + if (!word_mask) + continue; + + return i; + } + + return size; +} +EXPORT_SYMBOL(find_next_port_word); -- 2.17.0