Received: by 10.192.165.148 with SMTP id m20csp4304414imm; Tue, 8 May 2018 06:27:45 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq6oMEseAAEHXqeF19eKz2Wj6z48U6FXvXmHsxVtDKtUajA1/zEj0F4eKJqgBLiRiOXYiak X-Received: by 2002:a17:902:3001:: with SMTP id u1-v6mr41553495plb.164.1525786065839; Tue, 08 May 2018 06:27:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525786065; cv=none; d=google.com; s=arc-20160816; b=HccrxfNhvabKqgzoojjD/Wq5CgKR2ay7Iz9IeEd6lJ52E1XIJhhRzd518NgVM98RBq w7jmfS32AvIqgLFNaRiEJNnMgbCSV4BOfdjhvoPalPU/Kw56Am/pyupcVAZcOwQdSOJM 9d89Aket4DWNkHGsjfRWUyzwChUpou10Ai7Jc5OWwVlsGAhL1jMMBiaDN+VNL3cLJaXd ar+GA2WeYAXtg/A3RmHz+GKjFZWBGROTMS/WbxY6ui4p3L/jtb/wvc0vMWezrxAD4DOg ZxcUe+n9zehYWAHcogpheIKf1pBP8GDqKdRdNzY4wvFzIdcQNLD8Ta5SIrjpXyWpBqq6 K6sw== 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=ZqDn/ROY86HSNffSXEK9eC3xCuqviTyWTk8aIx9dsKk=; b=hCwkBaeIT4+V8P+zRNML/nnizhMR0sgI5JovNgMMnP5IORRrTWqK0RGLdY82j2Aoko myfurED1fgie4PWDPqpYUchgfoWp41/aGRfdHMyuhnuoN05G8REnguNLyB7qlg1oFbas Jb/ozybMHbJWi1Iy3oabysutflme+fsw+Ouph9DCGw2z/kowWUPRMj4zUfsDlAEPi5jo 5eLMWLGHa9Y29MALrNlT/rCXWXAs39QW/uUkIsX/gcp4mBwld/wuBt0yNeL8nFt/Pglj m3ozFQpIMof8VfF4jkuEL8szWRErzNXmdi1y3cfTAicRaMXx/W5hoA+Ey1wVLHBCbjdD A+MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=agWhHLJj; 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 az8-v6si23772122plb.347.2018.05.08.06.27.31; Tue, 08 May 2018 06:27:45 -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=agWhHLJj; 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 S1755183AbeEHN1A (ORCPT + 99 others); Tue, 8 May 2018 09:27:00 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:42784 "EHLO mail-yw0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755159AbeEHN06 (ORCPT ); Tue, 8 May 2018 09:26:58 -0400 Received: by mail-yw0-f193.google.com with SMTP id q7-v6so1309492ywd.9; Tue, 08 May 2018 06:26:58 -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=ZqDn/ROY86HSNffSXEK9eC3xCuqviTyWTk8aIx9dsKk=; b=agWhHLJjfu4RfX0o2Jt95lHTPN/fYUTbI7KKLZIWpqZdJdDBprNzSrn8zQPRsx56xN EPEh5fj2nlvpnNJeC0lehdJWQje8OeqeSDWgs+Xr/Gn2qA5YszbrBL8JCBY/zMLftL3J PDktAqcQBkhlJpZVNaAEYpbpZsTSvgiHdg/zT0E2mD8F63PTaN+ps+kYAw34fXzSw5L3 uQ6ECUb46zn3bl2VicPPS59hYwm0wCi9joTO9VAbT773OSTg+8D5lvwkyTDLGX/r7Ghj bqZ0jAym6PIf0KVkhwedEkOaM/S70xDx4tEQGi+w2KJG+PO/Jdic3Dh1SKo+V4DYUQM7 GXwQ== 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=ZqDn/ROY86HSNffSXEK9eC3xCuqviTyWTk8aIx9dsKk=; b=NDxNEn3anDk8fiVXBrhnCqwEOIt5drdnsV3EAjxg9f6fcVgrfAsBD/vG0CAMlERkaP fzPEWdFBAyM8uCHsywSYojfl98dw3oJSk89pHYWB4llbB7CJWp7IGfUN3vEy7Sx5moxG yDuA9lIJy1EJHHhsfGoVhtCio5YeJeWlhwr7DqHsZ8VIG7k9Fi5hWOAg9AZYLLjpMFqa bzzoe00l3B2rSBB2BjXtb8b5SRLW/uBIvz8oQpjz5HrQrSIIBHScYOzg4FObjCrf6rAm MtySoAUmol7nCIHxCL0R8EmK0XYQIaXsmD3V+dYGkmql/1MiMRX3pCYj2jhmZ6eOxasB Qe0A== X-Gm-Message-State: ALQs6tCCK6A+6FbQmFq3IAMDRmbZ/Tkg7Kp57a7ijyySB9tduiZuIXGI mFffvLpy/d2eRj5TvUUUkwA= X-Received: by 2002:a0d:d50e:: with SMTP id x14-v6mr22097468ywd.3.1525786017616; Tue, 08 May 2018 06:26:57 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id r3-v6sm10863849ywa.101.2018.05.08.06.26.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 06:26:57 -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 v2 1/7] bitops: Introduce the for_each_set_port_word macro Date: Tue, 8 May 2018 09:26:51 -0400 Message-Id: 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 | 26 ++++++++++++++++++++++++++ include/linux/bitops.h | 9 +++++++++ lib/find_bit.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..c59514547e63 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -2,6 +2,8 @@ #ifndef _ASM_GENERIC_BITOPS_FIND_H_ #define _ASM_GENERIC_BITOPS_FIND_H_ +#include + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region @@ -80,4 +82,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