Skip to contentSkip to conversation

eal: silence -Wconstant-logical-operand in RTE_IS_POWER_OF_2

PATCH Superseded Stephen Hemminger 4 participants 6 messages May 18 – May 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: silence -Wconstant-logical-operand in RTE_IS_POWER_OF_2

Newer GCC warns when a non-boolean constant is an operand of &&, which trips whenever RTE_IS_POWER_OF_2 is used in a static_assert with a power-of-two literal. Make the zero check explicit.

Fixes 7c872b96983a ("hash: validate hash bucket entries while compiling")
---
 lib/eal/include/rte_bitops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_bitops.h +1 -1
index aa6ac73abb..d2719ecd5e 100644
@@ -1299,7 +1299,7 @@ rte_fls_u64(uint64_t x)
 /**
  * Macro to return 1 if n is a power of 2, 0 otherwise
  */
-#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
+#define RTE_IS_POWER_OF_2(n) ((n) != 0 && !(((n) - 1) & (n)))
 
 /**
  * Returns true if n is a power of 2
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Stephen Hemminger

Newer GCC warns when a non-boolean constant is an operand of &&, which trips whenever RTE_IS_POWER_OF_2 is used in a static_assert with a power-of-two literal. Make the zero check explicit.

Fixes 7c872b96983a ("hash: validate hash bucket entries while compiling")

One further suggestion below, since you are changing this.

Quoted 9 lines
---
 lib/eal/include/rte_bitops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_bitops.h +0 -0
index aa6ac73abb..d2719ecd5e 100644

lib/eal/include/rte_bitops.h rte_fls_u64(uint64_t x) · line 1303

Quoted 6 lines
lib/eal/include/rte_bitops.h +1 -1
@@ -1299,7 +1299,7 @@ rte_fls_u64(uint64_t x)
 /**
  * Macro to return 1 if n is a power of 2, 0 otherwise
  */
-#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
+#define RTE_IS_POWER_OF_2(n) ((n) != 0 && !(((n) - 1) & (n)))
 

I'd also suggest changing the second part of the && to match, changing the "!" for explicit "== 0":

#define RTE_IS_POWER_OF_2(n) ((n) != 0 && (((n) - 1) & (n)) == 0)

Quoted 5 lines
lib/eal/include/rte_bitops.h +0 -0
 /**
  * Returns true if n is a power of 2
Signature · 1 line
2.53.0
#2 lore raw
Quoted 4 lines · On Mon, 18 May 2026 17:56:08 +0100 Bruce Richardson…

I'd also suggest changing the second part of the && to match, changing the "!" for explicit "== 0":

#define RTE_IS_POWER_OF_2(n) ((n) != 0 && (((n) - 1) & (n)) == 0)

Was heading for the smallest possible part

Acked-by on v1 1/1
#3 lore raw
In reply to Stephen Hemminger
Quoted 17 lines · Stephen Hemminger

Newer GCC warns when a non-boolean constant is an operand of &&, which trips whenever RTE_IS_POWER_OF_2 is used in a static_assert with a power-of-two literal. Make the zero check explicit.

Fixes 7c872b96983a ("hash: validate hash bucket entries while compiling")
---
  lib/eal/include/rte_bitops.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_bitops.h +0 -0
index aa6ac73abb..d2719ecd5e 100644

lib/eal/include/rte_bitops.h rte_fls_u64(uint64_t x) · line 1305

Quoted 8 lines
lib/eal/include/rte_bitops.h +1 -1
@@ -1299,7 +1299,7 @@ rte_fls_u64(uint64_t x)
  /**
   * Macro to return 1 if n is a power of 2, 0 otherwise
   */
-#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
+#define RTE_IS_POWER_OF_2(n) ((n) != 0 && !(((n) - 1) & (n)))
 
  /**
   * Returns true if n is a power of 2
#4 lore raw

PATCH v2 1/1 eal: silence -Wconstant-logical-operand in RTE_IS_POWER_OF_2

In reply to Stephen Hemminger

Newer GCC warns when a non-boolean constant is an operand of &&, which trips whenever RTE_IS_POWER_OF_2 is used in a static_assert with a power-of-two literal. Make the zero check explicit.

Fixes 7c872b96983a ("hash: validate hash bucket entries while compiling")

---
v2 - use suggestion to make both sides comparisons

Note: AI incorrectly complains about multiple evaluations in this macro
but that has always been the case, and has to be macro for use with static assert.

 lib/eal/include/rte_bitops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_bitops.h +1 -1
index aa6ac73abb..b170447714 100644
@@ -1299,7 +1299,7 @@ rte_fls_u64(uint64_t x)
 /**
  * Macro to return 1 if n is a power of 2, 0 otherwise
  */
-#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
+#define RTE_IS_POWER_OF_2(n) ((n) != 0 && (((n) - 1) & (n)) == 0)
 
 /**
  * Returns true if n is a power of 2
Signature · 1 line
2.53.0
#5 lore raw

19/05/2026 15:49, Stephen Hemminger:

Quoted 4 lines

Newer GCC warns when a non-boolean constant is an operand of &&, which trips whenever RTE_IS_POWER_OF_2 is used in a static_assert with a power-of-two literal. Make the zero check explicit.

Quoted 8 lines
Fixes 7c872b96983a ("hash: validate hash bucket entries while compiling")

---
v2 - use suggestion to make both sides comparisons

Applied, thanks.