From nobody Sat Feb 7 16:02:01 2026 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 647715578A for ; Thu, 2 May 2024 09:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714641894; cv=none; b=Z4uWjLEwQmpdJU8B4J5eCTILK6QpYxDzgAaYFyBEwStBpmXMUGDyVr6lGgQ91nCvJhW4tccyL6yDzhIiP+J+oG7bYsg/LLIaDuO1d0xTQmii4OgopsisJ+C9iIIfAomESmvMLAzJkvEbK0ui6NFRvbbqY8+dWj22K9m1W9XRHlI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714641894; c=relaxed/simple; bh=Q0j+FEUbSyqV92vLdWL4Yq6IfpsYB1S99FEiQf2pX5E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G5Ti71krSQbD24O3sdQRahggCl3WCyabSojV/HtENV2jX2jGM3AGzzR7dN6ZEDc1SHZFOlkHM563IkE79GBaCXKLbmFhrYkSHzcEseAJoP9SdV2zYqkO2M42kujdaO6lMW4htbmFyOIZGdJE+ZVHZQXRpAarO/G8j2lK2faWwjw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S1prhNpc; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S1prhNpc" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2b27e960016so682644a91.1 for ; Thu, 02 May 2024 02:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714641893; x=1715246693; darn=vger.kernel.org; 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=E663sjFFt5eNpPHpt2DGNzWmy8ubvWUrprjgv845FQ8=; b=S1prhNpcI+ue74bckJMp2BKCU5pzNxLyHsJ+atjUwUhqlu28tctyOnehJ9lTjt9OJc nxM0JCVltwL//Pbfv3bRFD5wo8ZTkB/Wq5gYyZqsBGpmMIXdFmLVTFN5hkpxVvKtmO6p 3eoyh4OjKJfTWL2ZvhrOjQ/VOds4t+oQNp+SnmV47fO2aSM5JD95pMXH6hOiNo94NTHd aoj37VHf9rhcoQ4lFjmKvGDC/02QB8xSZlDwCFa49cVWsAYAScst0XoM8LLr40ej20pS PjjVUFlBLvUY7vwQ5Z0Ci3PkYArVhzAHHuSafQk1Dtp7CqtT7mTAjVf2crbzgyjT13tk wGTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714641893; x=1715246693; 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=E663sjFFt5eNpPHpt2DGNzWmy8ubvWUrprjgv845FQ8=; b=HHE7AezrdGyPMzpOK6QUeaFhKUJrIyiLLW2lC4m3w/N2kXcHqJHGbm4ViRvnlko+fo LskOT0+rx3b0x6zdk3HrG8k/+VpfDWpn36lk+bU2hRyrifP2HysME2NHtnVrBPpXxHfT sbHj661ZqxdPuev/Ls0A3HNK1rXmeBLDk7ZGT1iOoFombdEnR00tIy3hg5hybKJBvkbo qbQ3TRbVA+zjsRvsXFsyrprzN+RIm1squ+suSbZt7a+59oXthqCy8ImLtpniUpwNZkev mtppcy0cFyCV/wKu8S1HqlfxH3OuR/DWHNPrLjPUQXrVKFzGwlx0xhgwVpT/7JYTkp95 VkjQ== X-Forwarded-Encrypted: i=1; AJvYcCXNtuFdxIJx0sd7gX6Z+kp0ff2svReOHp52j3hV5Pi4nDxlnqpCiG/+cLsEfmXNzAWoVf38WvhoJxbyRS9kJWfnndqgwb8X1JsP8wbd X-Gm-Message-State: AOJu0YxhwBas/WEqSfpMkueMMI7TJl6IObOOtvfPVoXfyvPUVd21FbTY XUXjQjzbIWOYyW0Q5qEZGqdwrByu9dwLGvqKn0KkTmf4A9M5j/NI X-Google-Smtp-Source: AGHT+IGaEf5cJEO9SrJsD1B2VyQprhEHez2s8f5FrIZUTssd9+4+d3mLIfOSH8E9syhiac4i5BEk/Q== X-Received: by 2002:a17:90b:f90:b0:2af:d64:4887 with SMTP id ft16-20020a17090b0f9000b002af0d644887mr4938013pjb.4.1714641892595; Thu, 02 May 2024 02:24:52 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id bk15-20020a17090b080f00b002a51dcecc49sm746836pjb.38.2024.05.02.02.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 02:24:52 -0700 (PDT) From: Kuan-Wei Chiu To: akpm@linux-foundation.org, yury.norov@gmail.com Cc: linux@rasmusvillemoes.dk, n26122115@gs.ncku.edu.tw, jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v5 1/2] lib/test_bitops: Add benchmark test for fns() Date: Thu, 2 May 2024 17:24:42 +0800 Message-Id: <20240502092443.6845-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240502092443.6845-1-visitorckw@gmail.com> References: <20240502092443.6845-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a benchmark test for the fns(). It measures the total time taken by fns() to process 10,000 test data generated using get_random_bytes() for each n in the range [0, BITS_PER_LONG). example: test_bitops: fns: 7637268 ns Signed-off-by: Kuan-Wei Chiu Suggested-by: Yury Norov --- Changes in v5: - Reduce testing iterations from 1000000 to 10000 to decrease testing time. - Move 'buf' inside the function. - Mark 'buf' as __initdata. - Assign the results of fns() to a volatile variable to prevent compiler optimization. - Remove the iteration count from the benchmark result. lib/test_bitops.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/test_bitops.c b/lib/test_bitops.c index 3b7bcbee84db..5c627b525a48 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,26 @@ static unsigned long order_comb_long[][2] =3D { }; #endif =20 +static int __init test_fns(void) +{ + static unsigned long buf[10000] __initdata; + static volatile __used unsigned long tmp __initdata; + unsigned int i, n; + ktime_t time; + + get_random_bytes(buf, sizeof(buf)); + time =3D ktime_get(); + + for (n =3D 0; n < BITS_PER_LONG; n++) + for (i =3D 0; i < 10000; i++) + tmp =3D fns(buf[i], n); + + time =3D ktime_get() - time; + pr_err("fns: %18llu ns\n", time); + + return 0; +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +114,8 @@ static int __init test_bitops_startup(void) if (bit_set !=3D BITOPS_LAST) pr_err("ERROR: FOUND SET BIT %d\n", bit_set); =20 + test_fns(); + pr_info("Completed bitops test\n"); =20 return 0; --=20 2.34.1 From nobody Sat Feb 7 16:02:01 2026 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6AEB5915A for ; Thu, 2 May 2024 09:24:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714641898; cv=none; b=hQ4wcXrs7EL5B+TxrId65/ib0sorGZ2v5GcvQ1GWdOThSwmvvs79kyoc9FGODpTW687fW7K8AtYLbdoskx8fyel8iPfCKM2tKx3RCfRVBU3V4OxaG2XNpykKqnLnSEY6a+PlBh2OUnjDvQ1on5gCo5cNqpKAaj4v1x4BsWphqXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714641898; c=relaxed/simple; bh=IWMavLt3kFulGzQxZIoWZWxUM2RWdm2WOTlumZ3O1yY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PrzUbaKpnUEv7h6DW4i6mF0LNx2rfwFrXFZNmu7a22bhGPdgVcJ5z9Wug9/0Zaa7KrjQfn/pQXSRTmUu5JbEiL3wh1mr2E9b3KGPOSgNl/FTt6L1DYOGBBsx87OjoTTTiOIwPGYOLaYCNJte7RSjWFzimY37F8oWERuKAlAsA9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XX8keK1D; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XX8keK1D" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2ab48c1c007so1967083a91.2 for ; Thu, 02 May 2024 02:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714641896; x=1715246696; darn=vger.kernel.org; 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=8Vtuu8T9epphwTtTuu3lp/GQ6ElIGl8lBIMHX4TrsJc=; b=XX8keK1DzMtknkUI07aQY63yogE6jBIurP9xTWVm9DS7/IbyjzBAVLwY6n5gGj8lgR l02gjwMT380wUxOJPTLoVwAEmwMZGgANXDk/JvYU0svz19Lb/0ybzjtCURCWXcb451rc 6AskaOvu9Mpq/kAcJlgIwwpPRMEuVlAFxTQd7Au4HVRyocJZorld0Ky4YPN5WKCMelFo xl9lsHg9zZNwEJp5lyDbGH0BrtggJU8UZIPzd86VjhSw3R40zl6txD+L0UK2JzaM/S5I W/TnLDVkXuspONzCkkCc7IoDS3VnfB+Xr3wVfBPfbuwgcXp1QN5eiPUy6EVENiTNItkw MmpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714641896; x=1715246696; 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=8Vtuu8T9epphwTtTuu3lp/GQ6ElIGl8lBIMHX4TrsJc=; b=R8QIbOMDtj/g+X1IHB0t6g/3vTOzrohcd1Cw/j6HPsEgQfVOzreHdTVkZhqjPfLTfI ZtCVMvcscDgyHHtZnPPxX+4fSNSx8x02p0b6OL8UpIRHFVwDAcHoMJzT4MyIEBM+aRXc XKjeh1n6LgenqFEfOglAsGKSiP5aTOdblu0x58YAQRIPqnSAUW7RBCL9B9Wax/AW9/uv aS6eG/b6kQlSmTiat+TrjUOkk7n+D3T1KbBvlU3A0QAVMle8Xq3JqNGkdO5x2g/sccGC mb4V6ExsBvOtUwUuSuNabZmaorTY4CpiZWyCONn4SVOMOa8jqVnGmF7YWljP9njp11iA ruZQ== X-Forwarded-Encrypted: i=1; AJvYcCXkZcYQDotkDl6lEoyE0jqmlr6yDeJjMh4V6HtQ4gHsDt1HmM6ydHhxopxpHMYx0bdt58a9nlNnmJ3MUrJM1XmryQbzwOjVsJ0ljdJD X-Gm-Message-State: AOJu0YzeSbR+PNgfZmmr80lDsxkY9TgZpXk7I2DI3qoz5fqdHyttQWJv cWETxwxBoJuJKyds5hUNARfZf15SZnpX6wH6+p6L+4IKhcQpfYvbsfIWQJTz X-Google-Smtp-Source: AGHT+IHKNU0etU7+knP43TQ0jsaxdSCdUU2aisVlh1h0CtI+thkwDQ6wADoLeaIEi+DFGWe4rwH/fA== X-Received: by 2002:a05:6a20:f388:b0:1ae:42c8:4f69 with SMTP id qr8-20020a056a20f38800b001ae42c84f69mr5799654pzb.0.1714641895964; Thu, 02 May 2024 02:24:55 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id bk15-20020a17090b080f00b002a51dcecc49sm746836pjb.38.2024.05.02.02.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 02:24:55 -0700 (PDT) From: Kuan-Wei Chiu To: akpm@linux-foundation.org, yury.norov@gmail.com Cc: linux@rasmusvillemoes.dk, n26122115@gs.ncku.edu.tw, jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH v5 2/2] bitops: Optimize fns() for improved performance Date: Thu, 2 May 2024 17:24:43 +0800 Message-Id: <20240502092443.6845-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240502092443.6845-1-visitorckw@gmail.com> References: <20240502092443.6845-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The current fns() repeatedly uses __ffs() to find the index of the least significant bit and then clears the corresponding bit using __clear_bit(). The method for clearing the least significant bit can be optimized by using word &=3D word - 1 instead. Typically, the execution time of one __ffs() plus one __clear_bit() is longer than that of a bitwise AND operation and a subtraction. To improve performance, the loop for clearing the least significant bit has been replaced with word &=3D word - 1, followed by a single __ffs() operation to obtain the answer. This change reduces the number of __ffs() iterations from n to just one, enhancing overall performance. This modification significantly accelerates the fns() function in the test_bitops benchmark, improving its speed by approximately 7.6 times. Additionally, it enhances the performance of find_nth_bit() in the find_bit benchmark by approximately 26%. Before: test_bitops: fns: 58033164 ns find_nth_bit: 4254313 ns, 16525 iterations After: test_bitops: fns: 7637268 ns find_nth_bit: 3362863 ns, 16501 iterations Signed-off-by: Kuan-Wei Chiu --- Changes in v5: - Update benchmark results in the commit message. include/linux/bitops.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 2ba557e067fe..57ecef354f47 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -254,16 +254,10 @@ static inline unsigned long __ffs64(u64 word) */ static inline unsigned long fns(unsigned long word, unsigned int n) { - unsigned int bit; + while (word && n--) + word &=3D word - 1; =20 - while (word) { - bit =3D __ffs(word); - if (n-- =3D=3D 0) - return bit; - __clear_bit(bit, &word); - } - - return BITS_PER_LONG; + return word ? __ffs(word) : BITS_PER_LONG; } =20 /** --=20 2.34.1