From patchwork Sun Mar 21 21:54:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12153365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2459C43461 for ; Sun, 21 Mar 2021 21:56:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7185F6194C for ; Sun, 21 Mar 2021 21:56:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbhCUVzm (ORCPT ); Sun, 21 Mar 2021 17:55:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231446AbhCUVzM (ORCPT ); Sun, 21 Mar 2021 17:55:12 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A159FC061762; Sun, 21 Mar 2021 14:55:11 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id m7so10990969qtq.11; Sun, 21 Mar 2021 14:55:11 -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 :mime-version:content-transfer-encoding; bh=GJLTuGj5ztiML5kH6e3/bZFvphNBmQOwXj5Cz1PPJdA=; b=jt+ZptmhfuaHxmNiy42zhGeMP8U2TnHOhOxkpZAbngQb+A8Qpnx8xKeX+uLxRFpZUd PWzuvccOjB/VpBVXREm5XYiUhi0F9o9eSFpR/n/uXjrx9LTVXIGXmGik8bYIJ8dt9IxG xdLy7WO3heHvBrArSFdpr97+Xds2jNVIu6E2MMc0kcVid2BlIOe5OHO1EAd+4ZaTIDYX BarshRZJeuLc7+kHVUD/t5QnATvlnEfvw9k2NrNZLxDZMFvTNFfOCvyE5G84EX4Lhsfx Tg8jHF9m6AcTLdCpFJnPsmhBJ4JnDI0Y6UmYZShAX2Utfj5DgpbvisJrd5gPyJM7o2Xk sZhw== 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=GJLTuGj5ztiML5kH6e3/bZFvphNBmQOwXj5Cz1PPJdA=; b=QvWmV7bOantquABr7S+Al7EZmrR00sMzPUHMgk3TJDJNmgdW49eaEnk99Au4+iXVq7 R2iqMlRhObItOFa5BZNQkPjwFOFAoZMYtROVblbLsSW5TVveMipFxbd3dCBlH+RNKAVM oJqO8A9Q/lNWfDOXWZTssqwn3Cjha7wfIRFGN05C4YeEN+HXz3EiqZcytC+vdePf2Tcu kIrEqpWQdXIm+ehSZJnlBbndsHgFW3oyFfuMYUAk//4HAXi4fp7bKT8G71KDc3Mb7RaK 5xSLOrXUZE4fKCSM4YOJa4v/55F7mBqK6uBFGFPLfSGxzHByUsImhV94P/ms+KrjzYsa T8jA== X-Gm-Message-State: AOAM533dJTm1JhDfotxoGadrRQxo+RVmJRL3j25Q29xIsfjUijljQjSE c0PCb8sKy1RItgqDmp8z98tGmOBmHYo= X-Google-Smtp-Source: ABdhPJwhPykAlwdAbNtFUd+UY0ngstzC55emskuuEUOCrdzWVX86Yy50D9a9eRnKiNlON8P4hEa8XQ== X-Received: by 2002:ac8:7f07:: with SMTP id f7mr7324036qtk.134.1616363710576; Sun, 21 Mar 2021 14:55:10 -0700 (PDT) Received: from localhost ([76.73.146.210]) by smtp.gmail.com with ESMTPSA id 6sm7958201qth.82.2021.03.21.14.55.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Mar 2021 14:55:10 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , linux-m68k@lists.linux-m68k.org, linux-arch@vger.kernel.org, linux-sh@vger.kernel.org, Alexey Klimov , Andrew Morton , Andy Shevchenko , Arnd Bergmann , David Sterba , Dennis Zhou , Geert Uytterhoeven , Jianpeng Ma , Joe Perches , John Paul Adrian Glaubitz , Josh Poimboeuf , Rasmus Villemoes , Rich Felker , Stefano Brivio , Wei Yang , Wolfram Sang , Yoshinori Sato Subject: [PATCH 11/12] tools: sync lib/find_bit implementation Date: Sun, 21 Mar 2021 14:54:56 -0700 Message-Id: <20210321215457.588554-12-yury.norov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210321215457.588554-1-yury.norov@gmail.com> References: <20210321215457.588554-1-yury.norov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org Add fast paths to find_*_bit() functions as per kernel implementation. Signed-off-by: Yury Norov --- tools/include/asm-generic/bitops/find.h | 58 +++++++++++++++++++++++-- tools/lib/find_bit.c | 4 +- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/tools/include/asm-generic/bitops/find.h b/tools/include/asm-generic/bitops/find.h index 9fe62d10b084..6481fd11012a 100644 --- a/tools/include/asm-generic/bitops/find.h +++ b/tools/include/asm-generic/bitops/find.h @@ -5,6 +5,9 @@ extern unsigned long _find_next_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long nbits, unsigned long start, unsigned long invert, unsigned long le); +extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); +extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size); +extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size); #ifndef find_next_bit /** @@ -20,6 +23,16 @@ static inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset) { + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >= size)) + return size; + + val = *addr & GENMASK(size - 1, offset); + return val ? __ffs(val) : size; + } + return _find_next_bit(addr, NULL, size, offset, 0UL, 0); } #endif @@ -40,6 +53,16 @@ unsigned long find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size, unsigned long offset) { + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >= size)) + return size; + + val = *addr1 & *addr2 & GENMASK(size - 1, offset); + return val ? __ffs(val) : size; + } + return _find_next_bit(addr1, addr2, size, offset, 0UL, 0); } #endif @@ -58,6 +81,16 @@ static inline unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, unsigned long offset) { + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >= size)) + return size; + + val = *addr | ~GENMASK(size - 1, offset); + return val == ~0UL ? size : ffz(val); + } + return _find_next_bit(addr, NULL, size, offset, ~0UL, 0); } #endif @@ -72,8 +105,17 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, * Returns the bit number of the first set bit. * If no bits are set, returns @size. */ -extern unsigned long find_first_bit(const unsigned long *addr, - unsigned long size); +static inline +unsigned long find_first_bit(const unsigned long *addr, unsigned long size) +{ + if (small_const_nbits(size)) { + unsigned long val = *addr & GENMASK(size - 1, 0); + + return val ? __ffs(val) : size; + } + + return _find_first_bit(addr, size); +} #endif /* find_first_bit */ @@ -87,7 +129,17 @@ extern unsigned long find_first_bit(const unsigned long *addr, * Returns the bit number of the first cleared bit. * If no bits are zero, returns @size. */ -unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size); +static inline +unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) +{ + if (small_const_nbits(size)) { + unsigned long val = *addr | ~GENMASK(size - 1, 0); + + return val == ~0UL ? size : ffz(val); + } + + return _find_first_zero_bit(addr, size); +} #endif #endif /*_TOOLS_LINUX_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/tools/lib/find_bit.c b/tools/lib/find_bit.c index 589fd2f26f94..109aa7ffcf97 100644 --- a/tools/lib/find_bit.c +++ b/tools/lib/find_bit.c @@ -83,7 +83,7 @@ unsigned long _find_next_bit(const unsigned long *addr1, /* * Find the first set bit in a memory region. */ -unsigned long find_first_bit(const unsigned long *addr, unsigned long size) +unsigned long _find_first_bit(const unsigned long *addr, unsigned long size) { unsigned long idx; @@ -100,7 +100,7 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size) /* * Find the first cleared bit in a memory region. */ -unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) +unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size) { unsigned long idx;