All of lore.kernel.org
 help / color / mirror / Atom feed
* Devise macros to encapsulate (x & -x)
@ 2023-11-17 10:17 Nicola Vetrini
  2023-11-17 11:04 ` Andrew Cooper
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Nicola Vetrini @ 2023-11-17 10:17 UTC (permalink / raw)
  To: Xen Devel
  Cc: Consulting, Stefano Stabellini, Jbeulich, Andrew Cooper3,
	George Dunlap, Julien Grall, Wei Liu, Roger Pau, Bertrand Marquis,
	Michal Orzel

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
- is the proposed form actually the best, or maybe it could be an inline 
function?

Then testing can happen and a subsequent version of those uncommitted 
patches introducing uses of either construct will be submitted, to 
modify all the call sites only once in the commit history.

Let me know what you think of this.
Regards,

[1] 
https://lore.kernel.org/xen-devel/8a1313b3ab5ba6dd556cf37409e3b703@bugseng.com/T/#mdeb510325e1acacb6477a88de8577e9e87351ba5
[2] https://gitlab.com/xen-project/people/bugseng/xen/-/jobs/5423693947

-- 
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-11-22 21:22 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-17 10:17 Devise macros to encapsulate (x & -x) Nicola Vetrini
2023-11-17 11:04 ` Andrew Cooper
2023-11-17 11:15   ` Nicola Vetrini
2023-11-17 11:37     ` Jan Beulich
2023-11-18  2:46   ` Stefano Stabellini
2023-11-20 11:16     ` Julien Grall
2023-11-22  1:43       ` Stefano Stabellini
2023-11-22  8:07         ` Jan Beulich
2023-11-22  9:27           ` Nicola Vetrini
2023-11-22 21:22             ` Stefano Stabellini
2023-11-17 11:39 ` Jan Beulich
2023-11-17 16:12   ` Nicola Vetrini
2023-11-17 16:15 ` Nicola Vetrini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.