]> xenbits.xensource.com Git - xen.git/commitdiff
xen/include: add macro ISOLATE_LSB
authorNicola Vetrini <nicola.vetrini@bugseng.com>
Thu, 16 Nov 2023 08:18:23 +0000 (09:18 +0100)
committerJan Beulich <jbeulich@suse.com>
Thu, 16 Nov 2023 08:18:23 +0000 (09:18 +0100)
The purpose of this macro is to encapsulate the well-known expression
'x & -x' that in 2's complement architectures on unsigned integers will
give a mask where only the least significant nonzero bit of 'x' is set,
or 0 if none are set.

A deviation for ECLAIR is also introduced.

Signed-off-by: Nicola Vetrini <nicola.vetrini@bugseng.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
automation/eclair_analysis/ECLAIR/deviations.ecl
docs/misra/deviations.rst
xen/include/xen/macros.h

index d8170106b449e5b7a476f572bd61376e13604a3d..82ae567da4f3014e5d1638025fec12727010998c 100644 (file)
@@ -274,6 +274,13 @@ still non-negative."
 -config=MC3R1.R10.1,etypes+={safe, "stmt(operator(logical)||node(conditional_operator||binary_conditional_operator))", "dst_type(ebool||boolean)"}
 -doc_end
 
+-doc_begin="The macro ISOLATE_LSB encapsulates a well-known pattern to obtain
+a mask where only the lowest bit set in the argument is set, if any, for unsigned
+integers arguments on two's complement architectures
+(all the architectures supported by Xen satisfy this requirement)."
+-config=MC3R1.R10.1,reports+={safe, "any_area(any_loc(any_exp(macro(^ISOLATE_LSB$))))"}
+-doc_end
+
 ### Set 3 ###
 
 #
index 8511a189253b9e8e39e513ca5fb5aa265e2f3e05..5b03c093ad9fc201608f6711cf0b41e3cd0a3510 100644 (file)
@@ -192,6 +192,14 @@ Deviations related to MISRA C:2012 Rules:
        See automation/eclair_analysis/deviations.ecl for the full explanation.
      - Tagged as `safe` for ECLAIR.
 
+   * - R10.1
+     - The macro ISOLATE_LSB encapsulates the well-known pattern (x & -x)
+       applied to unsigned integer values on 2's complement architectures
+       (i.e., all architectures supported by Xen), used to obtain a mask where
+       just the least significant nonzero bit of x is set.
+       If no bits are set, 0 is returned.
+     - Tagged as `safe` for ECLAIR.
+
    * - R13.5
      - All developers and reviewers can be safely assumed to be well aware of
        the short-circuit evaluation strategy for logical operators.
index d0caae7db298c8565dfe82b438460b72e80b0965..f943319ab2478edad6859411bbf9e664f118d507 100644 (file)
@@ -8,8 +8,14 @@
 #define DIV_ROUND(n, d) (((n) + (d) / 2) / (d))
 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
 
-#define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
-#define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
+/*
+ * 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))
+
+#define MASK_EXTR(v, m) (((v) & (m)) / ISOLATE_LSB(m))
+#define MASK_INSR(v, m) (((v) * ISOLATE_LSB(m)) & (m))
 
 #define count_args_(dot, a1, a2, a3, a4, a5, a6, a7, a8, x, ...) x
 #define count_args(args...) \