Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1523486lqm; Thu, 2 May 2024 19:19:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQVH5hABtbjCGP9ASidN00CF4h4oD3igHWsgYhCP/d+wg1H6Fp9VibgU9oWUb1nfyWt53b7fON9k+2WLYslI50PYA0azO6u3VplrOjzg== X-Google-Smtp-Source: AGHT+IEIU2W0k/E/zi0VAYnkByUP9dJEKx8QxTyuwbbH+7TeL3xdi/lQ3SUjlwYLpH/GM2VQlrHT X-Received: by 2002:adf:f889:0:b0:34d:1b4c:d014 with SMTP id u9-20020adff889000000b0034d1b4cd014mr1271915wrp.57.1714702762096; Thu, 02 May 2024 19:19:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714702762; cv=pass; d=google.com; s=arc-20160816; b=Z0cHBbwjDOIMg1svNmXFdksaGijcGZNO3wiLYuEupVLqwTdpC7n3MPzGedZoVAoj// iMvtIiddcn0xJU2WM4HyHpD/ZbKfhC2cuu9yf7AQ7RqhmjukhWhTxaVD81t52Fgjs1ne dCf2St4iV75qF01Nd/G+JMhHZhrqeetXyq+NyzoeJrTVbtC4L+nA1vQqpTc4ZToO5qr1 r8Qogq3J8IU/cpWlAwUYTW8FkK1bvkkqqjDtHML4SKxyUlikeFCu6KIx8MQDUX6cia9y Rn2nchY8MXLplGbO2WsfBs4kj2WavC63ZH6WHI+RJiBaoLWgU/s6A8QHbZ0F4+SmzsMQ sH5g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=2jPgqeYvIrTI/ZMFkiNcL7c1VtZkdfbZkpA1BybG2D0=; fh=2obbxFBWB9AhgQrtQ0L16GPcDbmAb0YY+BcEGQHttvc=; b=JqXT9fwl02lz7kGynNknFV8M27vD9E/2CxLtBG/evPHkAvBx2oDlZgiC+4CIYd1X13 VGMGIq8knPO7iN966KwgCKaFcIcHqS+M129/6R5uJ6/ydbfyN6tTU+B1Fq01JodmFbA0 Vh2oBvYeOo/Bcf33ZPfVeShvksgx5FtaQCZqEtCj7xIlN7Y6Bdr4crVbOZ6V7dpL1Poi yHfhhgwY+s4vpOxVYR3bhN4SlGz2q/P0xSlR+2F3/hDHu+0p4eLAnGaqW+ylNSHmwHLN mTXwQQuApcgxAKaYHYmJFUHY6ZASH0KnOZ90nDa4225KUKS1wMkAsSz4kF/+8PeV3/kA OTgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PmmHGhv7; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-167178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id qb10-20020a1709077e8a00b00a5993da7b04si61187ejc.310.2024.05.02.19.19.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 19:19:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-167178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PmmHGhv7; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-167178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A75A41F22D4E for ; Fri, 3 May 2024 02:19:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5FCB7168B8; Fri, 3 May 2024 02:19:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PmmHGhv7" Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 1AA3712E78 for ; Fri, 3 May 2024 02:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714702756; cv=none; b=m6q13ETMigHuzpLPSRYuhsvU2KFNCT3kPIrdvtiW7bl7Cpr8iN9jhYk6IT59Nfjn/vr7h5ICCVmF3n7hiS5UTcFI5QkQDHpGRme1bFhA89DZ5uJpxy7ydFLb3IKMS7Egko6moU0j7glZVGPqH7DuI2D2zo9/qSBfEPWleMaN2fs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714702756; c=relaxed/simple; bh=JJ+RCaEAAhlDChYBmeURVjNFhKSKrBAif0kUS5G0LrI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BdToyBvdGtz3fbXbUKKLU2wiA286y2nnDY+bccjlbjWB7PXBFMJ77yfd9trinGk5rAFz2JaVPd3rIcTt8VUQZAv2NE/T8X7w1fNok6lrDqFJ6oLV+YTVkm5nSbApRNQJMdKI5dWUW2p5AKWY8SAa7g+xuUPwHvHd8Bq7VFKli1s= 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=PmmHGhv7; arc=none smtp.client-ip=209.85.216.52 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 Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2b33953a163so609640a91.2 for ; Thu, 02 May 2024 19:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714702754; x=1715307554; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=2jPgqeYvIrTI/ZMFkiNcL7c1VtZkdfbZkpA1BybG2D0=; b=PmmHGhv74PCj+6p30QRZyy0JWpYOvFVY5v9mVrT7P0QN7CqO3e0Lq6zQ10by4vMrvA 7scZaFEsPvybGQZ4KAva7nT3nfHuu7BVUybdfaKvj14+VBXyV/V2RQ/TgcwOIA+K/VUW 9fzsMX/H9+G97ph9oPPVq+ViQck28QP9Ss8jypArxo93k09FpJXJXW5czXrc9qj8JHRZ 3uQvOkPDNCRx//krm84QYdAA4mOAfR7r1WkNYvOp4TVMEoPYe8TyuGPZP2MTO1rn9E6r cCeWtgyXSHeXbO3iR8Rg7fBE4v/cnl0qE3C5N8qRs4/6vH5ubBLnjxOObAweQ5tCVjTw 3HkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714702754; x=1715307554; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2jPgqeYvIrTI/ZMFkiNcL7c1VtZkdfbZkpA1BybG2D0=; b=itjqoJ0xzzKRUSgZcZBMaaqgshCW93NIFbW+4lYVuIgPTawTyNRNqmekKA6khMkl2s g4o9/guBHZPGviEdncOfjBAi2VZjvX36wd/XQIm+VNzLJkcMcvZkGhPugM8sXHBFlUO2 oWn72CQPSHIaW/hzRXQUB2uebcixP+AXlSGGY+yWg6dj7vanHXQTpdLqdeOIq1bHz0+9 l78CYFN8DjipPBTbPNKJn6bErX64dMkbC0cAAN4k/pTv+qkNpUIESGbm3UcH/PT00aHN Lry4OYLkFp3tPNfhL1y9hwe8tRrqZlxg7Wh4yrMowMfUYwyfxwAvtlj6+j2gAASL3mRr Th/A== X-Gm-Message-State: AOJu0YzZ1HAF28XjZEJojf/eBalceBTlnbdYWH+k5aYCYApL6etxd0b/ XYkTAs+Sk9mjljI9HhkAr5AnpnOAijiyFpaiMTgXvBNS0lE5rRheYodvThzM X-Received: by 2002:a05:6a20:9695:b0:1af:667e:fe3c with SMTP id hp21-20020a056a20969500b001af667efe3cmr1403558pzc.1.1714702754192; Thu, 02 May 2024 19:19:14 -0700 (PDT) Received: from visitorckw-System-Product-Name ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id 2-20020a170902ee4200b001ecc9a92e1csm2047580plo.267.2024.05.02.19.19.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 19:19:13 -0700 (PDT) Date: Fri, 3 May 2024 10:19:10 +0800 From: Kuan-Wei Chiu To: Yury Norov Cc: linux-kernel@vger.kernel.org, Rasmus Villemoes , Andrew Morton , Chin-Chun Chen , Ching-Chun Huang Subject: Re: [PATCH 3/4] bitops: squeeze even more out of fns() Message-ID: References: <20240502233204.2255158-1-yury.norov@gmail.com> <20240502233204.2255158-4-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240502233204.2255158-4-yury.norov@gmail.com> +Cc Chin-Chun Chen & Ching-Chun (Jim) Huang On Thu, May 02, 2024 at 04:32:03PM -0700, Yury Norov wrote: > The function clears N-1 first set bits to find the N'th one with: > > while (word && n--) > word &= word - 1; > > In the worst case, it would take 63 iterations. > > Instead of linear walk through the set bits, we can do a binary search > by using hweight(). This would work even better on platforms supporting > hardware-assisted hweight() - pretty much every modern arch. > Chin-Chun once proposed a method similar to binary search combined with hamming weight and discussed it privately with me and Jim. However, Chin-Chun found that binary search would actually impair performance when n is small. Since we are unsure about the typical range of n in our actual workload, we have not yet proposed any relevant patches. If considering only the overall benchmark results, this patch looks good to me. Regards, Kuan-Wei > On my Ryzen 9 5900X, the test_fns() benchmark runs binary fns() twice > faster, comparing to linear one. > > The fns8() returns 64 to make sure that in case of no bit found, the > return value will be greater than the bit capacity of arguments of all > fnsXX() functions up to fns64(). > > Signed-off-by: Yury Norov > --- > include/linux/bitops.h | 42 +++++++++++++++++++++++++++++++++++++----- > 1 file changed, 37 insertions(+), 5 deletions(-) > > diff --git a/include/linux/bitops.h b/include/linux/bitops.h > index 57ecef354f47..1c4773db56e0 100644 > --- a/include/linux/bitops.h > +++ b/include/linux/bitops.h > @@ -247,17 +247,49 @@ static inline unsigned long __ffs64(u64 word) > return __ffs((unsigned long)word); > } > > +static inline unsigned long fns8(u8 word, unsigned int n) > +{ > + while (word && n--) > + word &= word - 1; > + > + return word ? __ffs(word) : 64; > +} > + > +static inline unsigned long fns16(u16 word, unsigned int n) > +{ > + unsigned int w = hweight8((u8)word); > + > + return n < w ? fns8((u8)word, n) : 8 + fns8((u8)(word >> 8), n - w); > +} > + > +static inline unsigned long fns32(u32 word, unsigned int n) > +{ > + unsigned int w = hweight16((u16)word); > + > + return n < w ? fns16((u16)word, n) : 16 + fns16((u16)(word >> 16), n - w); > +} > + > +static inline unsigned long fns64(u64 word, unsigned int n) > +{ > + unsigned int w = hweight32((u32)word); > + > + return n < w ? fns32((u32)word, n) : 32 + fns32((u32)(word >> 32), n - w); > +} > + > /** > * fns - find N'th set bit in a word > * @word: The word to search > - * @n: Bit to find > + * @n: Bit to find, counting from 0 > + * > + * Returns N'th set bit. If no such bit found, returns >= BITS_PER_LONG > */ > static inline unsigned long fns(unsigned long word, unsigned int n) > { > - while (word && n--) > - word &= word - 1; > - > - return word ? __ffs(word) : BITS_PER_LONG; > +#if BITS_PER_LONG == 64 > + return fns64(word, n); > +#else > + return fns32(word, n); > +#endif > } > > /** > -- > 2.40.1 >