Skip to contentSkip to conversation

eal, power: don't use '-' sign with unsigned literals

PATCH In review Tyler Retzlaff 5 participants 12 messages Mar 9, 2021 – Apr 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal, power: don't use '-' sign with unsigned literals

use ~0ULL instead of -1ULL to avoid contridctory application of '-' sign to integer literal where the desired type is unsigned.

---
 lib/librte_eal/common/eal_common_fbarray.c | 12 ++++++------
 lib/librte_power/rte_power_pmd_mgmt.c      |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +6 -6
index 592ec5859..82d271725 100644
@@ -138,7 +138,7 @@ find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	 */
 	last = MASK_LEN_TO_IDX(arr->len);
 	last_mod = MASK_LEN_TO_MOD(arr->len);
-	last_msk = ~(-1ULL << last_mod);
+	last_msk = ~(~0ULL << last_mod);
 
 	for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
 		uint64_t cur_msk, lookahead_msk;
@@ -398,8 +398,8 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				~0ULL : /* prevent overflow */
+				~(~0ULL << (first_mod + 1));
 
 	/* go backwards, include zero */
 	msk_idx = first;
@@ -513,7 +513,7 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 				 * no runs in the space we've lookbehind-scanned
 				 * as well, so skip that on next iteration.
 				 */
-				ignore_msk = -1ULL << need;
+				ignore_msk = ~0ULL << need;
 				msk_idx = lookbehind_idx;
 				break;
 			}
@@ -560,8 +560,8 @@ find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				~0ULL : /* prevent overflow */
+				~(~0ULL << (first_mod + 1));
 
 	/* go backwards, include zero */
 	idx = first;
lib/librte_power/rte_power_pmd_mgmt.c +1 -1
index 454ef7091..579f10e4b 100644
@@ -111,7 +111,7 @@ clb_umwait(uint16_t port_id, uint16_t qidx, struct rte_mbuf **pkts __rte_unused,
 				ret = rte_eth_get_monitor_addr(port_id, qidx,
 						&pmc);
 				if (ret == 0)
-					rte_power_monitor(&pmc, -1ULL);
+					rte_power_monitor(&pmc, ~0ULL);
 			}
 			q_conf->umwait_in_progress = false;
 
Signature · 1 line
2.30.0.vfs.0.2
#1 lore raw
Quoted 5 lines · Tyler Retzlaff

use ~0ULL instead of -1ULL to avoid contridctory application of '-' sign to integer literal where the desired type is unsigned.

---

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

Signature · 2 lines
Thanks,
Anatoly
#2 lore raw
Quoted 10 lines · Anatoly
Quoted 5 lines · Tyler Retzlaff

use ~0ULL instead of -1ULL to avoid contridctory application of '-' sign to integer literal where the desired type is unsigned.

---

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

it produces warnings under some compilers. in some enterprises we are required to fix certain classes of warnings (not suppress them from the command line) as a function of security policies.

as an alternative would you be more willing to accept something like the following? ``(unsigned long long)-1LL'' if you don't like ``~0ULL'' it would make explicit what the compiler is already doing.

the issue is the application of the sign to what is clearly something not signed; it get's flagged. so the cast is an explicit expression of intent that will not generate the warnings.

appreciate you're help in finding a solution even if it isn't the proposed solution.

thanks!

Quoted 4 lines
Signature · 3 lines
-- 
Thanks,
Anatoly
#3 lore raw
Quoted 20 lines · Tyler Retzlaff
Quoted 10 lines · Anatoly
Quoted 5 lines · Tyler Retzlaff

use ~0ULL instead of -1ULL to avoid contridctory application of '-' sign to integer literal where the desired type is unsigned.

---

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

it produces warnings under some compilers. in some enterprises we are required to fix certain classes of warnings (not suppress them from the command line) as a function of security policies.

as an alternative would you be more willing to accept something like the following? ``(unsigned long long)-1LL'' if you don't like ``~0ULL'' it would make explicit what the compiler is already doing.

Quoted 7 lines

the issue is the application of the sign to what is clearly something not signed; it get's flagged. so the cast is an explicit expression of intent that will not generate the warnings.

appreciate you're help in finding a solution even if it isn't the proposed solution.

What about using ULLONG_MAX and similar defines from limits.h?

#4 lore raw
Quoted 13 lines · Bruce Richardson
Quoted 12 lines · Tyler Retzlaff
Quoted 3 lines

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

it produces warnings under some compilers. in some enterprises we are required to fix certain classes of warnings (not suppress them from the command line) as a function of security policies.

as an alternative would you be more willing to accept something like the following? ``(unsigned long long)-1LL'' if you don't like ``~0ULL'' it would make explicit what the compiler is already doing.

Quoted 8 lines
Quoted 7 lines

the issue is the application of the sign to what is clearly something not signed; it get's flagged. so the cast is an explicit expression of intent that will not generate the warnings.

appreciate you're help in finding a solution even if it isn't the proposed solution.

What about using ULLONG_MAX and similar defines from limits.h?

i think this would be okay even in circumstances where the code is building masks so long as in practice it results in "all bits being set". i'm not aware of a XXX_MAX where max isn't all bits set.. is there?

#5 lore raw
Quoted 21 lines · Tyler Retzlaff
Quoted 20 lines · Bruce Richardson
Quoted 19 lines · Tyler Retzlaff
Quoted 3 lines

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

it produces warnings under some compilers. in some enterprises we are required to fix certain classes of warnings (not suppress them from the command line) as a function of security policies.

as an alternative would you be more willing to accept something like the following? ``(unsigned long long)-1LL'' if you don't like ``~0ULL'' it would make explicit what the compiler is already doing.

the issue is the application of the sign to what is clearly something not signed; it get's flagged. so the cast is an explicit expression of intent that will not generate the warnings.

appreciate you're help in finding a solution even if it isn't the proposed solution.

Quoted 6 lines
Quoted 1 line

What about using ULLONG_MAX and similar defines from limits.h?

i think this would be okay even in circumstances where the code is building masks so long as in practice it results in "all bits being set". i'm not aware of a XXX_MAX where max isn't all bits set.. is there?

just a qualification to my previous.

specifically for the UXXX_MAX (unsigned) preprocessor definitions, we aren't talking about signed here (or at least i wasn't).

#6 lore raw
Quoted 17 lines

From: dev [mailto:[email protected]] On Behalf Of Tyler Retzlaff
Sent: Friday, March 12, 2021 7:40 PM

Quoted 8 lines · Tyler Retzlaff
Quoted 7 lines · Bruce Richardson
Quoted 6 lines · Tyler Retzlaff
Quoted 3 lines

Not sure i agree. It's a very common pattern and is widely used and understood. I mean, if anything, seeing ~0 would have me stop and think as i've literally never seen such code before.

it produces warnings under some compilers. in some enterprises we are required to fix certain classes of warnings (not suppress them from

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

command line) as a function of security policies.

as an alternative would you be more willing to accept something like

the

Quoted 14 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

following? ``(unsigned long long)-1LL'' if you don't like ``~0ULL''

it

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

would make explicit what the compiler is already doing.

the issue is the application of the sign to what is clearly something

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

signed; it get's flagged. so the cast is an explicit expression of

intent

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

that will not generate the warnings.

appreciate you're help in finding a solution even if it isn't the proposed solution.

What about using ULLONG_MAX and similar defines from limits.h?

The type of the variable being manipulated is not unsigned long long, but uint64_t, which theoretically is not the same. Long long can be more than 64 bit, although all current implementations use 64 bit for long long.

So -1ULL was wrong from the start, and ~0ULL is similarly incorrect. The correct value would be UINT64_MAX from stdint.h.

Quoted 10 lines
Quoted 5 lines

i think this would be okay even in circumstances where the code is building masks so long as in practice it results in "all bits being set". i'm not aware of a XXX_MAX where max isn't all bits set.. is there?

just a qualification to my previous.

specifically for the UXXX_MAX (unsigned) preprocessor definitions, we aren't talking about signed here (or at least i wasn't).

#7 lore raw

PATCH v2 1/1 eal, power: use UINT64_MAX instead of -1ULL

In reply to Tyler Retzlaff

use UINT64_MAX instead of -1ULL when manipulating uint64_t masks and initializing sentinel values.

some compilers generate a warning when applying a '-' to an unsigned literal so avoid this by initializing with unsigned preprocessor definitions where appropriate.

---
 lib/librte_eal/common/eal_common_fbarray.c | 12 ++++++------
 lib/librte_power/rte_power_pmd_mgmt.c      |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +6 -6
index 592ec5859..3a28a5324 100644
@@ -138,7 +138,7 @@ find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	 */
 	last = MASK_LEN_TO_IDX(arr->len);
 	last_mod = MASK_LEN_TO_MOD(arr->len);
-	last_msk = ~(-1ULL << last_mod);
+	last_msk = ~(UINT64_MAX << last_mod);
 
 	for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
 		uint64_t cur_msk, lookahead_msk;
@@ -398,8 +398,8 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
 	msk_idx = first;
@@ -513,7 +513,7 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 				 * no runs in the space we've lookbehind-scanned
 				 * as well, so skip that on next iteration.
 				 */
-				ignore_msk = -1ULL << need;
+				ignore_msk = UINT64_MAX << need;
 				msk_idx = lookbehind_idx;
 				break;
 			}
@@ -560,8 +560,8 @@ find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
 	idx = first;
lib/librte_power/rte_power_pmd_mgmt.c +1 -1
index 454ef7091..db03cbf42 100644
@@ -111,7 +111,7 @@ clb_umwait(uint16_t port_id, uint16_t qidx, struct rte_mbuf **pkts __rte_unused,
 				ret = rte_eth_get_monitor_addr(port_id, qidx,
 						&pmc);
 				if (ret == 0)
-					rte_power_monitor(&pmc, -1ULL);
+					rte_power_monitor(&pmc, UINT64_MAX);
 			}
 			q_conf->umwait_in_progress = false;
 
Signature · 1 line
2.30.0.vfs.0.2
#8 lore raw

PATCH v3 1/1 eal, power: use UINT64_MAX and UINT32_MAX

In reply to Tyler Retzlaff

Use UINT64_MAX and UINT32_MAX instead of -1 or ~0 literal variations of different explicit widths when creating masks and sentinel values.

some compilers generate a warning when applying a '-' to an unsigned literal so avoid this by initializing with unsigned preprocessor definitions where appropriate.

---
 lib/librte_eal/common/eal_common_fbarray.c | 12 ++++++------
 lib/librte_eal/common/eal_common_log.c     |  2 +-
 lib/librte_power/rte_power_pmd_mgmt.c      |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +6 -6
index 592ec5859..3a28a5324 100644
@@ -138,7 +138,7 @@ find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	 */
 	last = MASK_LEN_TO_IDX(arr->len);
 	last_mod = MASK_LEN_TO_MOD(arr->len);
-	last_msk = ~(-1ULL << last_mod);
+	last_msk = ~(UINT64_MAX << last_mod);
 
 	for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
 		uint64_t cur_msk, lookahead_msk;
@@ -398,8 +398,8 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
 	msk_idx = first;
@@ -513,7 +513,7 @@ find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
 				 * no runs in the space we've lookbehind-scanned
 				 * as well, so skip that on next iteration.
 				 */
-				ignore_msk = -1ULL << need;
+				ignore_msk = UINT64_MAX << need;
 				msk_idx = lookbehind_idx;
 				break;
 			}
@@ -560,8 +560,8 @@ find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
 	idx = first;
lib/librte_eal/common/eal_common_log.c +1 -1
index c5554badb..1b2745329 100644
@@ -30,7 +30,7 @@ static struct rte_logs {
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
-	.type = ~0,
+	.type = UINT32_MAX,
 	.level = RTE_LOG_DEBUG,
 };
 
lib/librte_power/rte_power_pmd_mgmt.c +1 -1
index 454ef7091..db03cbf42 100644
@@ -111,7 +111,7 @@ clb_umwait(uint16_t port_id, uint16_t qidx, struct rte_mbuf **pkts __rte_unused,
 				ret = rte_eth_get_monitor_addr(port_id, qidx,
 						&pmc);
 				if (ret == 0)
-					rte_power_monitor(&pmc, -1ULL);
+					rte_power_monitor(&pmc, UINT64_MAX);
 			}
 			q_conf->umwait_in_progress = false;
 
Signature · 1 line
2.30.0.vfs.0.2
Acked-by on v3 1/1
#9 lore raw

Re: [dpdk-dev] [PATCH v3] eal, power: use UINT64_MAX and UINT32_MAX

Quoted 97 lines

From: dev [mailto:[email protected]] On Behalf Of Tyler Retzlaff
Sent: Tuesday, March 16, 2021 1:14 AM

Use UINT64_MAX and UINT32_MAX instead of -1 or ~0 literal variations of different explicit widths when creating masks and sentinel values.

some compilers generate a warning when applying a '-' to an unsigned literal so avoid this by initializing with unsigned preprocessor definitions where appropriate.

---
 lib/librte_eal/common/eal_common_fbarray.c | 12 ++++++------
 lib/librte_eal/common/eal_common_log.c     |  2 +-
 lib/librte_power/rte_power_pmd_mgmt.c      |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)
lib/librte_eal/common/eal_common_fbarray.c +1 -1
index 592ec5859..3a28a5324 100644
@@ -138,7 +138,7 @@ find_next_n(const struct rte_fbarray *arr, unsigned
 int start, unsigned int n,
 	 */
 	last = MASK_LEN_TO_IDX(arr->len);
 	last_mod = MASK_LEN_TO_MOD(arr->len);
-	last_msk = ~(-1ULL << last_mod);
+	last_msk = ~(UINT64_MAX << last_mod);
 
 	for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
lib/librte_eal/common/eal_common_fbarray.c +2 -2
 		uint64_t cur_msk, lookahead_msk;
@@ -398,8 +398,8 @@ find_prev_n(const struct rte_fbarray *arr, unsigned
 int start, unsigned int n,
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
lib/librte_eal/common/eal_common_fbarray.c +0 -0
 	msk_idx = first;
@@ -513,7 +513,7 @@ find_prev_n(const struct rte_fbarray *arr, unsigned
 int start, unsigned int n,
 				 * no runs in the space we've lookbehind-

scanned

lib/librte_eal/common/eal_common_fbarray.c +3 -3
 				 * as well, so skip that on next iteration.
 				 */
-				ignore_msk = -1ULL << need;
+				ignore_msk = UINT64_MAX << need;
 				msk_idx = lookbehind_idx;
 				break;
 			}
@@ -560,8 +560,8 @@ find_prev(const struct rte_fbarray *arr, unsigned
 int start, bool used)
 	first_mod = MASK_LEN_TO_MOD(start);
 	/* we're going backwards, so mask must start from the top */
 	ignore_msk = first_mod == MASK_ALIGN - 1 ?
-				-1ULL : /* prevent overflow */
-				~(-1ULL << (first_mod + 1));
+				UINT64_MAX : /* prevent overflow */
+				~(UINT64_MAX << (first_mod + 1));
 
 	/* go backwards, include zero */
lib/librte_eal/common/eal_common_fbarray.c +0 -0
 	idx = first;
lib/librte_eal/common/eal_common_log.c +1 -1
index c5554badb..1b2745329 100644
@@ -30,7 +30,7 @@ static struct rte_logs {
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
 } rte_logs = {
-	.type = ~0,
+	.type = UINT32_MAX,
 	.level = RTE_LOG_DEBUG,
 };
 
lib/librte_power/rte_power_pmd_mgmt.c +1 -2
index 454ef7091..db03cbf42 100644
@@ -111,7 +111,7 @@ clb_umwait(uint16_t port_id, uint16_t qidx, struct
 rte_mbuf **pkts __rte_unused,
 				ret = rte_eth_get_monitor_addr(port_id, qidx,
 						&pmc);
 				if (ret == 0)
-					rte_power_monitor(&pmc, -1ULL);
+					rte_power_monitor(&pmc, UINT64_MAX);
 			}
 			q_conf->umwait_in_progress = false;
 
--
Quoted 2 lines

2.30.0.vfs.0.2

Acked-by on v3 1/1
#10 lore raw

Re: [dpdk-dev] [PATCH v3] eal, power: use UINT64_MAX and UINT32_MAX

In reply to Tyler Retzlaff
Quoted 9 lines · Tyler Retzlaff

Use UINT64_MAX and UINT32_MAX instead of -1 or ~0 literal variations of different explicit widths when creating masks and sentinel values.

some compilers generate a warning when applying a '-' to an unsigned literal so avoid this by initializing with unsigned preprocessor definitions where appropriate.

---

Signature · 2 lines
Thanks,
Anatoly
#11 lore raw

Re: [dpdk-dev] [PATCH v3] eal, power: use UINT64_MAX and UINT32_MAX

01/04/2021 13:22, Burakov, Anatoly:

Quoted 11 lines · Tyler Retzlaff
Quoted 8 lines

Use UINT64_MAX and UINT32_MAX instead of -1 or ~0 literal variations of different explicit widths when creating masks and sentinel values.

some compilers generate a warning when applying a '-' to an unsigned literal so avoid this by initializing with unsigned preprocessor definitions where appropriate.

Split in 2 patches (EAL and power) and applied, thanks.

There are other occurences to remove:
git grep -l -- '-1U'
app/test-eventdev/test_perf_common.c
app/test-eventdev/test_pipeline_common.c
drivers/net/bnxt/tf_ulp/ulp_flow_db.c
drivers/net/bnxt/tf_ulp/ulp_rte_parser.h
drivers/net/mlx5/mlx5_rxtx_vec_neon.h

I think we should add a checkpatch test
to avoid inserting more occurences in future.
Tyler, please could you work on it?