From nobody Tue Dec 16 11:04:38 2025 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 33D0FC83F1D for ; Mon, 28 Aug 2023 18:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233153AbjH1Soa (ORCPT ); Mon, 28 Aug 2023 14:44:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233127AbjH1SoL (ORCPT ); Mon, 28 Aug 2023 14:44:11 -0400 Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23524B3 for ; Mon, 28 Aug 2023 11:44:08 -0700 (PDT) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-1c4de3b9072so2547124fac.3 for ; Mon, 28 Aug 2023 11:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693248247; x=1693853047; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BO9WTCDSdetqwfC+adFwBkJ/NQLbVLN7w9OB2nLu2Hc=; b=DLek+L8gCTU0hMcedn9UA3/i0XY1QAITS45X6Ot7ZMKcvuRbZBrW7ljoLCAHxQe9CD +ZwSVm1AL50qjt59vQUPGZYruGPhk5OCcTLPUtC9bW7p+JzPRGzWT7xM7TZoY0OXysVx XTVPO16r+Az/KqypLw76ul0WyY025xuBzI14JhiAzFWCrYhs5v8ffIJhtHBLPhwG4wGO SUJY1lRdvfno2LLT3R+qjiOnrdNiNdd5o8oNvp/8Nu5j4unEQqdVGtDABfWr47ICQOAw jAn2ePCpVYCnPpYYpqBdSlv6PXD2acut/gzda4+7O2ziB0CiDhQX4mUJ+az3GuEbtjC8 pEcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693248247; x=1693853047; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BO9WTCDSdetqwfC+adFwBkJ/NQLbVLN7w9OB2nLu2Hc=; b=j5HUrEZ53FjaX7YZ16MscGVWHwG/dfHWmbLYo27O/zX0eYqW1KCM7kRQHBmatuO2Kc Up1O1PVdyy/trpK9z8BS0lgIBauwkEHZ6C8bWkZ5A2b1V++fJIu4gxeQCEtSq4Eh9hgx 5agmxgszZYyJQMBV0RsiwOSdXr0yqL+0K3uAErVIhXyzSu/IJedsnMBZ8Yf6LZnvinx3 /H4qEYIR1KdxYuGLtgfDB07sFidIrwFqEp9I8rST2Bur71xxBib3PArBwB5Avu7RLB2D U9m5+zOhLsXMz5W+YkxhWFZYgpbcJh8VVkGyDDWdd9V8Rt2XNdUHdQrQtYq3+r3PiWSk 3C1Q== X-Gm-Message-State: AOJu0YzUZFDpgfPZ7ciNU5vtsudvZIQx7M70xL9d2Gj40h6rOCVvx1dA uw09yyJIyWiK4Ei8QRoMqgB+FyExQjM= X-Google-Smtp-Source: AGHT+IE/G/wNcyY5+i7oSoidqwFqCa9RNOmaz8F0+1Vn3Ze2ATe1jqHhSeiQH5EYSLzeDuC0fcxjjw== X-Received: by 2002:a05:6870:414d:b0:1bf:32bf:1913 with SMTP id r13-20020a056870414d00b001bf32bf1913mr11968205oad.43.1693248247059; Mon, 28 Aug 2023 11:44:07 -0700 (PDT) Received: from localhost ([2600:6c5e:2a00:5805:e348:56d4:5da8:636d]) by smtp.gmail.com with ESMTPSA id 67-20020a4a1446000000b005660ff9e037sm4321624ood.25.2023.08.28.11.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 11:44:06 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Andy Shevchenko , Rasmus Villemoes Subject: [PATCH 07/12] bitmap: add small_const_nbits() optimization for bitmap_bitremap() Date: Mon, 28 Aug 2023 11:43:47 -0700 Message-Id: <20230828184353.5145-8-yury.norov@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230828184353.5145-1-yury.norov@gmail.com> References: <20230828184353.5145-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When 'bits' is less than BITS_PER_LONG, we can remap a bit inline. Signed-off-by: Yury Norov --- include/linux/bitmap.h | 36 ++++++++++++++++++++++++++++++++++-- lib/bitmap.c | 15 ++------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 486451b80339..da5030a99af5 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -163,6 +163,8 @@ void __bitmap_replace(unsigned long *dst, const unsigned long *mask, unsigned int nbits); void __bitmap_remap(unsigned long *dst, const unsigned long *src, const unsigned long *old, const unsigned long *new, unsigned int nbits); +int __bitmap_bitremap(int oldbit, + const unsigned long *old, const unsigned long *new, int nbits); bool __bitmap_intersects(const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); bool __bitmap_subset(const unsigned long *bitmap1, @@ -214,8 +216,6 @@ int bitmap_parselist(const char *buf, unsigned long *ma= skp, int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, unsigned long *dst, int nbits); =20 -int bitmap_bitremap(int oldbit, - const unsigned long *old, const unsigned long *new, int bits); void bitmap_onto(unsigned long *dst, const unsigned long *orig, const unsigned long *relmap, unsigned int bits); void bitmap_fold(unsigned long *dst, const unsigned long *orig, @@ -733,6 +733,38 @@ static inline void bitmap_remap(unsigned long *dst, co= nst unsigned long *src, __bitmap_remap(dst, src, old, new, nbits); } =20 +/** + * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit + * @oldbit: bit position to be mapped + * @old: defines domain of map + * @new: defines range of map + * @nbits: number of bits in each of these bitmaps + * + * A special case of bitmap_remap(), when a single bit remapping is needed. + * + * Returns: position of remapped bit + */ +static inline int bitmap_bitremap(int oldbit, + const unsigned long *old, const unsigned long *new, int nbits) +{ + if (small_const_nbits(nbits)) { + unsigned int w, n; + + if ((*old & BIT(oldbit)) =3D=3D 0 || + (*old & BITMAP_LAST_WORD_MASK(nbits)) =3D=3D 0 || + (*new & BITMAP_LAST_WORD_MASK(nbits)) =3D=3D 0 || + ((*new ^ *old) & BITMAP_LAST_WORD_MASK(nbits)) =3D=3D 0) { + return oldbit; + } + + w =3D bitmap_weight(new, nbits); + n =3D bitmap_weight(old, oldbit); + return find_nth_bit(new, nbits, n % w); + } + + return __bitmap_bitremap(oldbit, old, new, nbits); +} + #endif /* __ASSEMBLY__ */ =20 #endif /* __LINUX_BITMAP_H */ diff --git a/lib/bitmap.c b/lib/bitmap.c index 2ac48d9bcbc0..9ecdc74cb6b4 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -1014,18 +1014,7 @@ void __bitmap_remap(unsigned long *dst, const unsign= ed long *src, } EXPORT_SYMBOL(__bitmap_remap); =20 -/** - * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit - * @oldbit: bit position to be mapped - * @old: defines domain of map - * @new: defines range of map - * @bits: number of bits in each of these bitmaps - * - * A special case of bitmap_remap(), when a single bit remapping is needed. - * - * Returns: position of remapped bit - */ -int bitmap_bitremap(int oldbit, const unsigned long *old, +int __bitmap_bitremap(int oldbit, const unsigned long *old, const unsigned long *new, int bits) { int w =3D bitmap_weight(new, bits); @@ -1035,7 +1024,7 @@ int bitmap_bitremap(int oldbit, const unsigned long *= old, else return find_nth_bit(new, bits, n % w); } -EXPORT_SYMBOL(bitmap_bitremap); +EXPORT_SYMBOL(__bitmap_bitremap); =20 #ifdef CONFIG_NUMA /** --=20 2.39.2