[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Devise macros to encapsulate (x & -x)
- To: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
- From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
- Date: Fri, 17 Nov 2023 12:15:38 +0100
- Cc: Xen Devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Consulting <consulting@xxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Jbeulich <jbeulich@xxxxxxxx>, 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>
- Delivery-date: Fri, 17 Nov 2023 11:15:48 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On 2023-11-17 12:04, Andrew Cooper wrote:
On 17/11/2023 10:17 am, 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_; \
})
Of course this was going to explode.
This isn't even the first time an unwise attempt to do
single-evaluation
has needed to be reverted because it doesn't work with Integer Constant
Expressions.
Switch it back to the first form. It's obviously a macro to begin
with,
and not likely to be used in cases that have side effects.
~Andrew
Actually no usages of either forms are yet committed, just the
definition of the first form, so nothing needs to be reverted. I should
have clarified that, sorry. If the other patches in the series go in
unmodified (modulo renaming, but that's trivial), they would use the
first form.
--
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
|