From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 CDA07318EE7 for ; Sat, 10 Jan 2026 10:50:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768042211; cv=none; b=oP80QuKbfhIvPAnUNyUXDJe2WHlf0d/QrFqONSwHRXgGhGSZpDwh7Zc9ttHZl9pnr6fPxyFnjMbmd6VrE1P+ghvYNUbXVmKVUtcqVNjJ7XUOeytB4B6k5U+SVbKdzDDmTn8l66HxjmGB8fKpJIz6CDTROowSnB4t9vMQnG1qfL8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768042211; c=relaxed/simple; bh=BNaZzPXtz2JcZpDAXqjYhp4mJLvBtv+ybReBcSIbUXU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kYRqJS32wM6hNrLAIBr/fqZieOysrNvJ4d5mVfGyXEiA7LBy972bKVyPORW+8Lqjs2LnBVTaurthUbpxVopO15nUlnJcTz4y+BwOlIR4aH3xto24Uq8XkWlsWQnbhHJbABfiff5jwpBY4E85GlwEstIdttCf3yUPCQDt8lLgdxk= 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=WcYW8vtZ; arc=none smtp.client-ip=209.85.128.41 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="WcYW8vtZ" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4779cb0a33fso53332845e9.0 for ; Sat, 10 Jan 2026 02:50:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768042208; x=1768647008; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=nPhDICnebAeuoQQn8tscw0JVV2JlOFcqJf73xaAVQrw=; b=WcYW8vtZmo2Mf7hW2jlpftyn1LpfbdgK/dK5i5V0GZbDZMEesh5pYHckIOYn0aqDwe rhG7hJNzE8BGmtbnCr85+n2rjTOtBZy+CxDk1tTAA+6iyR9+ERERUfPTjkGHDflgvyRq b1ZdzoKBUIEIc18x0nMBqKyRNpU/HFrB/SxCY9evygJc5TM5CkmaPREnHDEb3sPjx9CF Z90HZfyGrGKLtBaAWv4VPedRjgNPstO2sWT7uRvP1tUxpc6e1iG7TWGrFPlkSYa22Tvd UIdXDlsR6VCXa5Aa8vvWOgD4ohBD2F+JYU4vAGCIVhHrzg3a7QUjEIBuh2jRa1Vx/toA navA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768042208; x=1768647008; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nPhDICnebAeuoQQn8tscw0JVV2JlOFcqJf73xaAVQrw=; b=AF2WshPo0UJvmFnAg8eHKbYTm6RoARzho0dUf7g5y1AQLMxff7D2++mmA8rg0j9t33 w6PQyHGKiR5hNN3Z8uoq5EmbRZFnz4tpVs4RB2/Sv4xxom7qk5LZiWuNV/WgU1fNmtMQ mCl95eZSaALm0gxR1gxqvmWVaJXbDAsp1JY+dwaROQ1Zt7GqnWzZm9nKdedcOoPOSx2w 5Rrssj5iGozRFsbSlnvGGcT1xKaGb9w2S9esjcdrSut2sh5ER574ecSRmzRwVfwH2Ir5 G2tU4BITBJlCFRroE+h1rNB+dk/VBItRkPIytnZgdu2uc4F8rfw/tpMwsOv1YglnvFqw MiIw== X-Forwarded-Encrypted: i=1; AJvYcCX1NlDP+01iMST8TXTpLEsMRqqXjyotHhoPHMdyubUW4R1O0QaIBsvPb8Ze51L/Rq9HygCSfoHVLi9rmOw=@vger.kernel.org X-Gm-Message-State: AOJu0YysttjZOvIjNBoKKx3nrDx2NqEYnO3BYloFygqXB2XTCWZbWCYO Cw52O/uiW+HvD3apo6cq4RHXlvLYjQKxPDVJ8NwObn9TKbWhPonaEH6e X-Gm-Gg: AY/fxX4Q8DPWWuENMVCUTYhjGb9IDNduTRL3hX2jWYffyCpSCC4pQyrrwN3H/zXwaVA drMbfLTxudee9x+0lZF5ofaqAIFY4Ai2GSUYjSF0boxRYwJk3iyyqm96m1aQvmK9n1hHBPeJZeA Nm6dcG3IzuzIYf5KNtqB8nXF4raUMG7Xma9/u4aFi16jx6DzjvF4k8pcfsS+tgT8eB5pJWAkSgy LDgWoxq/sVNhZOjtjwcYWKUx4e06C4lvwxfJosMqHXRiVQWYx+Kz0rVe9FEgZF9U83p3JW6yKPm Dqyaor9aJnXksW35UoCTFQMVIcOwGdOU3kcgop+QhrOF8GJjHndeXOIdomMwRTTfl2q8CVJVA6Y AsZB+J9j5/tQ41pK/Xkq+7VmjTIhNSvt31hzCQwcrvmeEzwIQ3WgCoxVhgKFm+zDhqBer2WKuay RTgtvxW9pY5m6rbpHEHdYwZeAQWd7WAjPvgih/5HwI3s1xryebYpnZ X-Google-Smtp-Source: AGHT+IFi6WPXSv1z9iyd5Zr1BhlyirFdHIqTo8Ca0JCJAkGcWhGUZs7WMDIKLf0ts+rCLDkeZgMoCA== X-Received: by 2002:a05:600c:4747:b0:47b:da85:b9ef with SMTP id 5b1f17b1804b1-47d84b18a7dmr178148995e9.16.1768042207916; Sat, 10 Jan 2026 02:50:07 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d8715b5d9sm86187505e9.5.2026.01.10.02.50.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 02:50:07 -0800 (PST) Date: Sat, 10 Jan 2026 10:50:02 +0000 From: David Laight To: Petr Tesarik Cc: Yury Norov , Rasmus Villemoes , Richard Henderson , Matt Turner , Magnus Lindholm , Vineet Gupta , Geert Uytterhoeven , "Maciej W. Rozycki" , Thomas Bogendoerfer , Madhavan Srinivasan , Michael Ellerman , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Chris Zankel , Max Filippov , Patrik Jakobsson , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Robin Murphy , Joerg Roedel , Will Deacon , Jakub Kicinski , Andrew Lunn , "David S. Miller" , Eric Dumazet , Paolo Abeni , Oliver Neukum , Arnd Bergmann , Kuan-Wei Chiu , Andrew Morton , Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Pablo Neira Ayuso , Florian Westphal , linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH 1/2] bits: introduce ffs_val() Message-ID: <20260110105002.1067bf38@pumpkin> In-Reply-To: <9767487fcab7dbe7a7282a48a492171629eb935b.1767975412.git.ptesarik@suse.com> References: <9767487fcab7dbe7a7282a48a492171629eb935b.1767975412.git.ptesarik@suse.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) 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-Transfer-Encoding: 7bit On Fri, 9 Jan 2026 17:37:56 +0100 Petr Tesarik wrote: > Introduce a macro that can efficiently extract the least significant > non-zero bit from a value. > > Interestingly, this bit-twiddling trick is open-coded in some places, but > it also appears to be little known, leading to various inefficient > implementations in other places. Let's make it part of the standard bitops > arsenal. I'm not sure whether ffs_val(x) is actually more readable than an open-coded (x & -x). If you don't know what either means you have to look it up or work it out. The latter just requires a bit of thought, the former searching through the source tree for the correct header and then believing the comment or, again, working out what it does. That said, I'm not objecting to adding it, but the churn of changing existing code is probably not worth the effort. I'd also define it as x & (~x + 1) - which makes it a lot more obvious why it is correct, the compiler will convert it to a signed negate. Also, as I pointed out earlier, many modern cpu have an instruction for ffs(). While x & -x is usualy better than 1u << __ffs(x); the same is not true for y * (x & -x) and y << __ffs(x). In particular on Zen4/5 bsf (used for __ffs) has a latency of 1 but the multiply has a latency of 3. Intel mainstream x86 cpu all have latency 3 for both imul and bsf. There should be #define definitions of is_power_of_2_or_zero() !(x + (x-1)) and is_power_of_2() (!x && is_power_of_2_or_zero(x)) in the same header. But there is only an inline is_power_of_2(unsigned long) in log.h. David