[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Devise macros to encapsulate (x & -x)
- To: Jan Beulich <jbeulich@xxxxxxxx>
- From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
- Date: Fri, 17 Nov 2023 17:12:56 +0100
- Cc: Consulting <consulting@xxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Andrew Cooper3 <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Wei Liu <wl@xxxxxxx>, Roger Pau <roger.pau@xxxxxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Xen Devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
- Delivery-date: Fri, 17 Nov 2023 16:13:09 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On 2023-11-17 12:39, Jan Beulich wrote:
On 17.11.2023 11:17, Nicola Vetrini wrote:
Hi all,
As discussed in this thread [1], which is about complying with MISRA C
Rule 10.1,
a macro was introduced to encapsulate a well-known construct:
/*
* Given an unsigned integer argument, expands to a mask where just
the
least
* significant nonzero bit of the argument is set, or 0 if no bits
are
set.
*/
#define ISOLATE_LSB(x) ((x) & -(x))
This macro has a gained some calls in the subsequent patches in that
thread, but concerns were raised around the fact that it would be
better
to devise a macro that evaluates its argument only once. A proposed
solution is this (thanks to Jan Beulich):
#define ISOLATE_LSB(x) ({ \
typeof(x) x_ = (x); \
x_ & -x_; \
})
However, it can't be used in all call sites that the previous macro
would have once that series is committed, as can be seen in [2].
Therefore, while the implementation looks ok,
a case has been made to have separate macros, of which the latter form
is preferred.
The following points require some thought:
- where the single evaluation macro should be placed?
One proposed location is xen/include/xen/bitops.h
Or next to the existing one in macros.h. I can see pros and cons for
either.
- is the proposed form actually the best, or maybe it could be an
inline
function?
How would you make such a function type generic?
Ah, yes indeed this wouldn't work.
--
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
|