From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DB3FC433F5 for ; Fri, 11 Feb 2022 00:29:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346333AbiBKA3I (ORCPT ); Thu, 10 Feb 2022 19:29:08 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:57402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345570AbiBKA3F (ORCPT ); Thu, 10 Feb 2022 19:29:05 -0500 Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA6B6E1E for ; Thu, 10 Feb 2022 16:29:05 -0800 (PST) Received: by mail-io1-xd34.google.com with SMTP id p63so9500344iod.11 for ; Thu, 10 Feb 2022 16:29:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ZDTlgBeWU01PBOzjEa5E75U6QWQseHc5+WCVq5kwy3w=; b=HM8kw4tWsOdALdKvtiwg2yy9eOvpQP+q0yOYC3qOjgrpR8fI4vo/sNinxN+PB+x/Ol jByaLF6SaOUOAP39+R0hdd76DXy4YmvFOlSGW6WvBQuLHJhLwD9LtChUJ0kIsq1VgDpt 9zckiYssOWPzRn1v/clJUdveiACOLwgOPwPmzgTEXdvK0beN6ZNDn9AYQI6fFMTgnM38 wm9a4sIxFBKuY0LyCYAB26zyZCuFv/y2XI02KUyo7gtVdClkz+WUvOihIGw2V5OsAtQJ Wifm0YtgNEUu29R3DbtPrD2hbVucPDQkBrf9ZGTr8wkNeEu0FKXVsUdOusf28EPD3pz9 b5SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZDTlgBeWU01PBOzjEa5E75U6QWQseHc5+WCVq5kwy3w=; b=DhCan/V1CsBzAh0hXkPQyhS6EFI46o7pi20dlmA0ueO8vsrSCpoxjS97jyB0EvdyhS jaSmtk88cbptYEKlvuWl7xaSMaRqwOSl803EflpuDAjbyXB7L+t+vaeKbPxWzJkT7FxJ Qnz7iVRPBjwrjRi1+q55we4vlkgogpsgN5xaDnOQtWEUR3qM9jUUO2HYgAjiHIK21CtB gs9eCyWYjbMkyVm2CvLRcKXqiIPDzXWxMDrHDP/lUkbTopX5DnnL+mcVgeUxRh9iHsjd Z68wNa6qRqkr8/YsECBjbogrhf0S8zKUsYEFtQwC1Kye9eyEWpZrNbQcrlHNPhUL7JZB yLvw== X-Gm-Message-State: AOAM533fHsWoDkCnJdAKhbFjNMt5NBmMlsOgAUnlrZzIGLrZbrFr0KUt mJWXrZGfwvfMqBQ2wJX6j1o= X-Google-Smtp-Source: ABdhPJwy0yG0d3qlBtG2SbAr160MBRvEPBQfXpQbtOrFB7KYpu61+Ou5PiEWocR3zTMOS1zHZBe0nA== X-Received: by 2002:a02:852e:: with SMTP id g43mr5671744jai.40.1644539345325; Thu, 10 Feb 2022 16:29:05 -0800 (PST) Received: from localhost ([12.28.44.171]) by smtp.gmail.com with ESMTPSA id c12sm10343849ilo.70.2022.02.10.16.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Feb 2022 16:29:04 -0800 (PST) From: Yury Norov To: Yury Norov , Andy Shevchenko , Rasmus Villemoes , Andrew Morton , =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= , Greg Kroah-Hartman , Peter Zijlstra , David Laight , Joe Perches , Dennis Zhou , Emil Renner Berthing , Nicholas Piggin , Matti Vaittinen , Alexey Klimov , linux-kernel@vger.kernel.org Subject: [PATCH 44/49] nodemask: add nodemask_weight_{eq,gt,ge,lt,le} Date: Thu, 10 Feb 2022 14:49:28 -0800 Message-Id: <20220210224933.379149-45-yury.norov@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220210224933.379149-1-yury.norov@gmail.com> References: <20220210224933.379149-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In many cases kernel code uses nodemask_weight() to compare the result against some number or expression: if (nodes_weight(...) > 1) do_something(); It may be significantly improved for large nodemasks: if first few words count set bits to a number greater than given, we can stop counting and immediately return. The same idea would work in other direction: if we know that the number of set bits that we counted so far is small enough, so that it would be smaller than required number even if all bits of the rest of the nodemask are set, we can stop counting earlier. This patch adds nodes_weight{eq, gt, ge, lt, le} helpers based on corresponding bitmap functions. The following patches apply new functions where appropriate. Signed-off-by: Yury Norov --- include/linux/nodemask.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h index 567c3ddba2c4..197598e075e9 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -38,6 +38,11 @@ * int nodes_empty(mask) Is mask empty (no bits sets)? * int nodes_full(mask) Is mask full (all bits sets)? * int nodes_weight(mask) Hamming weight - number of set bits + * bool nodes_weight_eq(src, nbits, num) Hamming Weight is equal to num + * bool nodes_weight_gt(src, nbits, num) Hamming Weight is greater than num + * bool nodes_weight_ge(src, nbits, num) Hamming Weight is greater than or equal to num + * bool nodes_weight_lt(src, nbits, num) Hamming Weight is less than num + * bool nodes_weight_le(src, nbits, num) Hamming Weight is less than or equal to num * * void nodes_shift_right(dst, src, n) Shift right * void nodes_shift_left(dst, src, n) Shift left @@ -240,6 +245,36 @@ static inline int __nodes_weight(const nodemask_t *srcp, unsigned int nbits) return bitmap_weight(srcp->bits, nbits); } +#define nodes_weight_eq(nodemask, num) __nodes_weight_eq(&(nodemask), MAX_NUMNODES, (num)) +static inline int __nodes_weight_eq(const nodemask_t *srcp, unsigned int nbits, int num) +{ + return bitmap_weight_eq(srcp->bits, nbits, num); +} + +#define nodes_weight_gt(nodemask, num) __nodes_weight_gt(&(nodemask), MAX_NUMNODES, (num)) +static inline int __nodes_weight_gt(const nodemask_t *srcp, unsigned int nbits, int num) +{ + return bitmap_weight_gt(srcp->bits, nbits, num); +} + +#define nodes_weight_ge(nodemask, num) __nodes_weight_ge(&(nodemask), MAX_NUMNODES, (num)) +static inline int __nodes_weight_ge(const nodemask_t *srcp, unsigned int nbits, int num) +{ + return bitmap_weight_ge(srcp->bits, nbits, num); +} + +#define nodes_weight_lt(nodemask, num) __nodes_weight_lt(&(nodemask), MAX_NUMNODES, (num)) +static inline int __nodes_weight_lt(const nodemask_t *srcp, unsigned int nbits, int num) +{ + return bitmap_weight_lt(srcp->bits, nbits, num); +} + +#define nodes_weight_le(nodemask, num) __nodes_weight_le(&(nodemask), MAX_NUMNODES, (num)) +static inline int __nodes_weight_le(const nodemask_t *srcp, unsigned int nbits, int num) +{ + return bitmap_weight_le(srcp->bits, nbits, num); +} + #define nodes_shift_right(dst, src, n) \ __nodes_shift_right(&(dst), &(src), (n), MAX_NUMNODES) static inline void __nodes_shift_right(nodemask_t *dstp, -- 2.32.0