From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 B38EC2EDD62 for ; Fri, 9 Jan 2026 23:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768002876; cv=none; b=Zk9hdsAaDNt6/mhQg47EPOGoPEIm4EmH8fT4Rc5qpoCU0bI3z8GFt0sKmfv4IsczlJglOz/A8EaL7Rqmt7ojVApAg7CUYxa3twIfZiiz1dkTuycqElqu/KsWLXsiBUR2+z3BWnt9i1GRaAGRvsSgTMh18Qt8Pi0QE9dIA81J0rk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768002876; c=relaxed/simple; bh=4u/zPksuukO8P/nWDu+70J0WHH7XatE80GDOhPYv7kU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kgAffyG4erC8fXUEuHJg9jG+XZPyfSi0cv1UXn9pOLJqXsiMbobyY/xem/3gM/EAS82uuQkpeKW3+Nb278qbDI8nK0mI4Bb9uG3+j9cZnivVoHyQ8zaY9CaAYLXTpR9910wt7+W5BDy2lfAbFzqW4ldMLSZoPDG+XKS8wDYqq8M= 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=IuB/gQP8; arc=none smtp.client-ip=209.85.221.43 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="IuB/gQP8" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-43277900fb4so1629445f8f.1 for ; Fri, 09 Jan 2026 15:54:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768002873; x=1768607673; 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=H/toLqO+XQDPVDU/b2lJwKHnutfYUiJRdS8lM2ioQd0=; b=IuB/gQP8DA4rYz0vu1Yz1BM8mZMEtLcbpN8gwWTHIZ7woUHXAMlRt0vYQYNwQgogZW LBUjpeY+UUtrbDIXzFMLCOnFqsxI5G026K4cTueI2MnyNFCFfAbY15AFPHgTGvzKejY+ 6BfOMwyx57fpPnS2mYCy8J5IoyvCQMVtNpGWQOxgMXJr7QHZ/G++DNEJvVur3nSzgDvd POwmSfwtAk7Py0yQ6DRZxPyBUoxgG7ZeANPn10CbWL+diU6sc1lOdVN2N0EgHlIAdm1z hjIXkbzDrybwHYQ3yR6kmMaZ5FXgppP/AjZbWpqlGr/DauR6lWNT1HPgOee3YJ+aUtzI 89lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768002873; x=1768607673; 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=H/toLqO+XQDPVDU/b2lJwKHnutfYUiJRdS8lM2ioQd0=; b=fZd1qmkagBv7HMOIAhMyiC8XQlBY0maOXyI4VDQ0Wg8/6OlmXdozpQ1qELB0OZlqX1 d1e2aj9eL1AR/md+KiyPadug8cJzsbqZLDAHUbM7qi9/DCX6lxNvAgYkWaH8wjzw18TY hFau02OKjqEO1Af+lTCo5V0HVZOKMdHPaVhQn3gzuSxynKor7uPdFtNb/q9G3MTNS8uP HMfzEyqSoV20IIjwRgCSSMagX+j9hSvtan9mn0GQrM38s7w9yR7yJAGa2mtdwSGSZ9Tj 3+K7qQHjkqAhrGVtGfYaTIjgfbICG61wdK+51sN+dkfAq0RiAwjSFt0LAMLHbdeF+6pL 6V3g== X-Forwarded-Encrypted: i=1; AJvYcCXiZv2Y7fiVOf5n0NR41KKMloLP1QpaIh+tu6LYN5eqrcYvgkP7KTSwPC8Vrlb0C+aYzMKPjb+mc7lXgH4=@vger.kernel.org X-Gm-Message-State: AOJu0YxkgQHjnbvsM3/0dIFvd871+UgZEx7A24AdN/Bsk+X1ihHXQcrF fctdO1fvhxSkk3ne6SkuYROJrU9Ml65R50rKVeohFIbBa00duSil3trZ X-Gm-Gg: AY/fxX6BAIjQOmSAC/xBIkTswjmpzuhfTBFlstHzbVVwIdns7D/Hy8SPjqpfnwL7gBP dLy9i7VYy3y5aLuDFozr6eeOhU2I0/nEtDn+7enwswf57ESJAsvSnh5kDkHWrZY2VxE1P8n4acX ajnKml5kcThRaKEZ2b6yitjFeHD7UkPcQriYNh/NXTljoqmkrNP/CDIptIozuEGPTX0OrjcTWQh 8hhCDQ4O6hgKr9IN84u4+8y62zb8Wi9NjnlR7NlrbkVOmVP/hNnMRtNLhnynPdoE9OF9Ue159fy aWp/jrKVQPh/Wtu4KNI8bE3ZRHhCqf9+5QmwZt43/4vby3ro/zc2yz0P++a10OdcMDnTP3Xq6zd FkycRpxd1OQZcyGidDp3mErBRPak++fq0s4blj+FGxaVQOFgP9XmjNol0FP5zM56ac0d/5/UTCJ dqtALqCRed/FskoyCMGvV1l5WEVSOTa1E4PXEH4mGLldIeWbxZrW1L X-Google-Smtp-Source: AGHT+IHi1ki3MnWdTz1t6VpvzUD7WC4qHmaR8FA03kmslsdU3wui9rT6JShnenV1csxNWrh5kvkF5g== X-Received: by 2002:a05:6000:184a:b0:431:c06:bc82 with SMTP id ffacd0b85a97d-432bcfa1405mr20043051f8f.12.1768002872799; Fri, 09 Jan 2026 15:54:32 -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 ffacd0b85a97d-432bd0dacc5sm24720089f8f.5.2026.01.09.15.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jan 2026 15:54:32 -0800 (PST) Date: Fri, 9 Jan 2026 23:54:30 +0000 From: David Laight To: "Arnd Bergmann" Cc: "Petr Tesarik" , "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" , "Dave Airlie" , "Simona Vetter" , "Robin Murphy" , "Joerg Roedel" , "Will Deacon" , "Jakub Kicinski" , "Andrew Lunn" , "David S . Miller" , "Eric Dumazet" , "Paolo Abeni" , "Oliver Neukum" , "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: <20260109235430.69ccfff2@pumpkin> In-Reply-To: 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, 09 Jan 2026 18:01:02 +0100 "Arnd Bergmann" wrote: > On Fri, Jan 9, 2026, at 17:37, Petr Tesarik wrote: > > > + * Returns: > > + * least significant non-zero bit, 0 if all bits are zero > > + */ > > +#define ffs_val(x) \ > > +({ \ > > + const typeof(x) val__ = (x); \ > > + val__ & -val__; \ > > +}) > > This looks good to me, but I'd suggest using 'const auto val__' > instead of typeof(), to reduce expanding complex arguments twice. It is more usual to just use a single _ prefix and the same name. I wouldn't bother with 'const' either, maybe: #define ffs_val(val) ({ \ auto _val = val; \ _val & -_val; \ }) However it isn't necessarily better than using __ffs(). FIELD_PREP(mask, val) (for non-constant mask) can be (mask & -mask) * val or val << __ffs(mask). So the 'ffs' version is fewer instructions (assuming non-zero mask). The timings for bsf/bsr are similar to those for imul on intel cpu, mul wins on zen3 and bsf/bsr on zen4. On balance the __ffs() version is actually likely to be faster. Other architectures may fair better or worse. Clearly you don't want to use __ffs() unless it is a single instruction. Of course, for FIELD_GET(reg, mask) you'd need reg/(mask & -mask) and the cost of the integer division is far more than __ffs(). (But that will give you a compile-time error if mask is a constant zero and the compiler will convert the divide to a shift.) And for 64bit calculations on 32bit 'all bets are off'. Even the shift left might be problematic. David > > Arnd >