Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3177027pxb; Mon, 24 Jan 2022 04:16:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJwW4/XsVxHpMu9Tzh4wKMEClfjbFx26jT2Lp/64hkdIEhGtdQJIuloZmmLylzO+AtCnzGHg X-Received: by 2002:a63:7d0c:: with SMTP id y12mr11765922pgc.517.1643026610144; Mon, 24 Jan 2022 04:16:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643026610; cv=none; d=google.com; s=arc-20160816; b=CjYAgZ1QwygawJY7MLPOz5tn8YBORdvjoVFhmtMB6jrWXBBqZddftjMXhdQV0XgFTS TjkPocSrW4JZboZMNjGhGrfCtGYCQLqSOmA5LTdafdO0Ne2qZhvRVS08JUjx5Yq3Vroe es1+wDrXzndGZsU7iyYmzh3NI3xe/JPf78H3Ih+RUgzplnewVqSoia/fi8z044FqzSf2 RnuY2iiW1AgnnOE6Z40Iwzyu2gxcfW5OFLMdms4LwJn3fPJ6hgdLr4AxYJDIPUaiK8fP BR/MZSlyjK/MginpPhnLiL2T354FZoAZj6RAgp0It0wj/a36XqQYvczuIHkK2xDTcnq5 4AXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=uDMwxBm35O7PZhXWiy5lbRAhZckr6GgPEF1IMYcAF1M=; b=FKOneXIKf2Qm9t/PSMxju30azLjAK84CUJbzh4KVLDfAmXkK/h/QUmlg9CyA01lSvS SE87fs3vHLG5tXgdQD/c1ltPiO0o6ZS1YadBVE6KcBTuG520Nsn1WXee0CdKgi+hGi2X cIB00Zrwd0DWc6Ava5UJOsKw8jT+7+xdxuAwX5Udd5p2pXx5zZsPQQy/tKTVsRKMk+Qb OTJwCe7Siz7EM+qMKuGpc5yhcruz3PjWB3C30xj8olcaJGJ7yiZ3LWbqDqb/rM8p3Yp7 4suAJvk9WIs4I2R7ss4j/39KAVcP0XISfMeZh2/uT0zOJnC3UgZthWHHpujdVyIREm5Y WB5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AixtNyS6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id v23si11344620pgl.345.2022.01.24.04.16.37; Mon, 24 Jan 2022 04:16:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AixtNyS6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S240168AbiAWSnk (ORCPT + 99 others); Sun, 23 Jan 2022 13:43:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240144AbiAWSmW (ORCPT ); Sun, 23 Jan 2022 13:42:22 -0500 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43E7EC06177D for ; Sun, 23 Jan 2022 10:42:22 -0800 (PST) Received: by mail-pg1-x52f.google.com with SMTP id f8so13146029pgf.8 for ; Sun, 23 Jan 2022 10:42:22 -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=uDMwxBm35O7PZhXWiy5lbRAhZckr6GgPEF1IMYcAF1M=; b=AixtNyS6WzA9hFKoRzjQ/Nln9Z2IsXf6Y7NSR+XlTdqyMMHMuAB2etyp7oOq8QIS23 I5NqfG9LyPILw33K6lCPHGVtIDK4NaLmU8fQWqww30sYGKnx4PDt2aONbX5UMEU/mjhK E5zU6vYPgyNaxyMP8vXWHhTZDlaFuVqn8hGiPFJVtl9dQ0ESYF0VTL6Vxsn/otIIcGMK E7/QKttkC07gwR/Qm+a1n0sYX+y6sr6KITS56A52HAQbBPTaaKjVm1pCgLoux+lxSUVG 63OBKz1TY9QXGAeTU5Oc1lyh/nXKFNj9zmcsV3DXXO8Acedl7zi7fw1xhVcB0FNayCMQ C5KQ== 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=uDMwxBm35O7PZhXWiy5lbRAhZckr6GgPEF1IMYcAF1M=; b=AgxYpJbsrRJaEszSfi7aubXIVvoBCYFrzDTgOidKgQKF5NiOog/kJkIYMxCshDy/Gp nONoYjnT6ONOmeND/1+2FZ/TWNP2fmqKfsL56vLNjUZSs6gSoQgOpPLX2NPoLzOa+p3y ZfQU0oanT9B4ja1fZ2sX62e/XsRpWcmv3VYEDyKXcsXRZlt8KRtn4oMQTjAS6OF0J1+k xUxJocX6BBpVG3kfIWIgG97gT3UB2Lmif/vLgf/4q4vrOyfxV6Dp5ZRLMXA8hL3LlWIR YojhQU9tqZ1EEb0+BdhI8ZdL18ztW7mXz9I9ZNi+xSRasuW5jRPFvWLUxsZbk3RZcpye GwPA== X-Gm-Message-State: AOAM533/YIWOpehAlDRKoys2lQlY5L+g0mhuUWMAj+icD95VuV13x7jv 7frnozPf9Dw5vRaBfojEuHI= X-Received: by 2002:aa7:804c:0:b0:4bc:6ca6:4bac with SMTP id y12-20020aa7804c000000b004bc6ca64bacmr11151360pfm.52.1642963341772; Sun, 23 Jan 2022 10:42:21 -0800 (PST) Received: from localhost (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id q9sm13269180pfj.113.2022.01.23.10.42.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jan 2022 10:42:21 -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 49/54] lib/nodemask: add nodemask_weight_{eq,gt,ge,lt,le} Date: Sun, 23 Jan 2022 10:39:20 -0800 Message-Id: <20220123183925.1052919-50-yury.norov@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220123183925.1052919-1-yury.norov@gmail.com> References: <20220123183925.1052919-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.30.2