[2/2] lib/hash: avoid implicit conversion to 64 bit number

Message ID 1732758837-6350-2-git-send-email-andremue@linux.microsoft.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series [1/2] lib/cryptodev: avoid implicit conversion to 64 bit number |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/iol-compile-amd64-testing success Testing PASS

Commit Message

Andre Muezerie Nov. 28, 2024, 1:53 a.m. UTC
MSVC issues the warnings below:

1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
    result of 32-bit shift implicitly converted to 64 bits
    (was 64-bit shift intended?)

The code would be better off by using 64 bit numbers to begin with.
That eliminates the need for a conversion to 64 bits later.

2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
    result of 32-bit shift implicitly converted to 64 bits
    (was 64-bit shift intended?)

1ULL should be used as the result of the bit shift gets multiplied
by sizeof(uint32_t).

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/hash/rte_thash.c               | 2 +-
 lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
  

Comments

Bruce Richardson Jan. 22, 2025, 4:12 p.m. UTC | #1
On Wed, Nov 27, 2024 at 05:53:57PM -0800, Andre Muezerie wrote:
> MSVC issues the warnings below:
> 
> 1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
>     result of 32-bit shift implicitly converted to 64 bits
>     (was 64-bit shift intended?)
> 
> The code would be better off by using 64 bit numbers to begin with.
> That eliminates the need for a conversion to 64 bits later.
> 
> 2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
>     result of 32-bit shift implicitly converted to 64 bits
>     (was 64-bit shift intended?)
> 
> 1ULL should be used as the result of the bit shift gets multiplied
> by sizeof(uint32_t).
> 
> Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> ---

Acked-by: Bruce Richardson <bruce.richardson@intel.com>

>  lib/hash/rte_thash.c               | 2 +-
>  lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
> index fa78787143..f076311b57 100644
> --- a/lib/hash/rte_thash.c
> +++ b/lib/hash/rte_thash.c
> @@ -565,7 +565,7 @@ rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
>  		offset;
>  
>  	ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
> -		sizeof(uint32_t) * (1 << ctx->reta_sz_log),
> +		sizeof(uint32_t) * (1ULL << ctx->reta_sz_log),
>  		RTE_CACHE_LINE_SIZE);

Is there a reason not to use RTE_BIT64 here too?
  
Medvedkin, Vladimir Jan. 22, 2025, 4:15 p.m. UTC | #2
Acked-by: Vladimir Medvedkin <vladimir.medvedkin@intel.com>

On 28/11/2024 01:53, Andre Muezerie wrote:
> MSVC issues the warnings below:
>
> 1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
>      result of 32-bit shift implicitly converted to 64 bits
>      (was 64-bit shift intended?)
>
> The code would be better off by using 64 bit numbers to begin with.
> That eliminates the need for a conversion to 64 bits later.
>
> 2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
>      result of 32-bit shift implicitly converted to 64 bits
>      (was 64-bit shift intended?)
>
> 1ULL should be used as the result of the bit shift gets multiplied
> by sizeof(uint32_t).
>
> Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> ---
>   lib/hash/rte_thash.c               | 2 +-
>   lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
>   2 files changed, 4 insertions(+), 4 deletions(-)
>
<snip>
  
Andre Muezerie Jan. 22, 2025, 9:36 p.m. UTC | #3
On Wed, Jan 22, 2025 at 04:12:49PM +0000, Bruce Richardson wrote:
> On Wed, Nov 27, 2024 at 05:53:57PM -0800, Andre Muezerie wrote:
> > MSVC issues the warnings below:
> > 
> > 1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
> >     result of 32-bit shift implicitly converted to 64 bits
> >     (was 64-bit shift intended?)
> > 
> > The code would be better off by using 64 bit numbers to begin with.
> > That eliminates the need for a conversion to 64 bits later.
> > 
> > 2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
> >     result of 32-bit shift implicitly converted to 64 bits
> >     (was 64-bit shift intended?)
> > 
> > 1ULL should be used as the result of the bit shift gets multiplied
> > by sizeof(uint32_t).
> > 
> > Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> > ---
> 
> Acked-by: Bruce Richardson <bruce.richardson@intel.com>
> 
> >  lib/hash/rte_thash.c               | 2 +-
> >  lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
> >  2 files changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
> > index fa78787143..f076311b57 100644
> > --- a/lib/hash/rte_thash.c
> > +++ b/lib/hash/rte_thash.c
> > @@ -565,7 +565,7 @@ rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
> >  		offset;
> >  
> >  	ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
> > -		sizeof(uint32_t) * (1 << ctx->reta_sz_log),
> > +		sizeof(uint32_t) * (1ULL << ctx->reta_sz_log),
> >  		RTE_CACHE_LINE_SIZE);
> 
> Is there a reason not to use RTE_BIT64 here too?

Here we are calculating the size to be passed to the second argument of rte_zmalloc, which is of type size_t. size_t is implementation dependent, typically 4 bytes on 32-bit systems and 8 bytes on 64-bit systems, so using 1ULL seems more appropriate.
  
Morten Brørup Jan. 23, 2025, 7:55 a.m. UTC | #4
> From: Andre Muezerie [mailto:andremue@linux.microsoft.com]
> Sent: Wednesday, 22 January 2025 22.37
> 
> On Wed, Jan 22, 2025 at 04:12:49PM +0000, Bruce Richardson wrote:
> > On Wed, Nov 27, 2024 at 05:53:57PM -0800, Andre Muezerie wrote:
> > > MSVC issues the warnings below:
> > >
> > > 1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
> > >     result of 32-bit shift implicitly converted to 64 bits
> > >     (was 64-bit shift intended?)
> > >
> > > The code would be better off by using 64 bit numbers to begin with.
> > > That eliminates the need for a conversion to 64 bits later.
> > >
> > > 2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
> > >     result of 32-bit shift implicitly converted to 64 bits
> > >     (was 64-bit shift intended?)
> > >
> > > 1ULL should be used as the result of the bit shift gets multiplied
> > > by sizeof(uint32_t).
> > >
> > > Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> > > ---
> >
> > Acked-by: Bruce Richardson <bruce.richardson@intel.com>
> >
> > >  lib/hash/rte_thash.c               | 2 +-
> > >  lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
> > >  2 files changed, 4 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
> > > index fa78787143..f076311b57 100644
> > > --- a/lib/hash/rte_thash.c
> > > +++ b/lib/hash/rte_thash.c
> > > @@ -565,7 +565,7 @@ rte_thash_add_helper(struct rte_thash_ctx *ctx,
> const char *name, uint32_t len,
> > >  		offset;
> > >
> > >  	ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper)
> +
> > > -		sizeof(uint32_t) * (1 << ctx->reta_sz_log),
> > > +		sizeof(uint32_t) * (1ULL << ctx->reta_sz_log),
> > >  		RTE_CACHE_LINE_SIZE);
> >
> > Is there a reason not to use RTE_BIT64 here too?
> 
> Here we are calculating the size to be passed to the second argument of
> rte_zmalloc, which is of type size_t. size_t is implementation
> dependent, typically 4 bytes on 32-bit systems and 8 bytes on 64-bit
> systems, so using 1ULL seems more appropriate.

1ULL makes it 8 byte on 32-bit systems too. Did you mean 1UL?

How about reducing the formula to directly shift the sizeof() instead, i.e.:
sizeof(uint32_t) << ctx->reta_sz_log,
  
Andre Muezerie Jan. 23, 2025, 5:42 p.m. UTC | #5
On Thu, Jan 23, 2025 at 08:55:29AM +0100, Morten Brørup wrote:
> > From: Andre Muezerie [mailto:andremue@linux.microsoft.com]
> > Sent: Wednesday, 22 January 2025 22.37
> > 
> > On Wed, Jan 22, 2025 at 04:12:49PM +0000, Bruce Richardson wrote:
> > > On Wed, Nov 27, 2024 at 05:53:57PM -0800, Andre Muezerie wrote:
> > > > MSVC issues the warnings below:
> > > >
> > > > 1) ../lib/hash/rte_thash_gf2_poly_math.c(128): warning C4334: '<<':
> > > >     result of 32-bit shift implicitly converted to 64 bits
> > > >     (was 64-bit shift intended?)
> > > >
> > > > The code would be better off by using 64 bit numbers to begin with.
> > > > That eliminates the need for a conversion to 64 bits later.
> > > >
> > > > 2) ../lib/hash/rte_thash.c(568): warning C4334: '<<':
> > > >     result of 32-bit shift implicitly converted to 64 bits
> > > >     (was 64-bit shift intended?)
> > > >
> > > > 1ULL should be used as the result of the bit shift gets multiplied
> > > > by sizeof(uint32_t).
> > > >
> > > > Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> > > > ---
> > >
> > > Acked-by: Bruce Richardson <bruce.richardson@intel.com>
> > >
> > > >  lib/hash/rte_thash.c               | 2 +-
> > > >  lib/hash/rte_thash_gf2_poly_math.c | 6 +++---
> > > >  2 files changed, 4 insertions(+), 4 deletions(-)
> > > >
> > > > diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
> > > > index fa78787143..f076311b57 100644
> > > > --- a/lib/hash/rte_thash.c
> > > > +++ b/lib/hash/rte_thash.c
> > > > @@ -565,7 +565,7 @@ rte_thash_add_helper(struct rte_thash_ctx *ctx,
> > const char *name, uint32_t len,
> > > >  		offset;
> > > >
> > > >  	ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper)
> > +
> > > > -		sizeof(uint32_t) * (1 << ctx->reta_sz_log),
> > > > +		sizeof(uint32_t) * (1ULL << ctx->reta_sz_log),
> > > >  		RTE_CACHE_LINE_SIZE);
> > >
> > > Is there a reason not to use RTE_BIT64 here too?
> > 
> > Here we are calculating the size to be passed to the second argument of
> > rte_zmalloc, which is of type size_t. size_t is implementation
> > dependent, typically 4 bytes on 32-bit systems and 8 bytes on 64-bit
> > systems, so using 1ULL seems more appropriate.
> 
> 1ULL makes it 8 byte on 32-bit systems too. Did you mean 1UL?
> 
> How about reducing the formula to directly shift the sizeof() instead, i.e.:
> sizeof(uint32_t) << ctx->reta_sz_log,

Shifting the sizeof() directly is better indeed. Let me know how we should
proceed. Do you want me to send out a new series incorporating this suggestion?
  
Morten Brørup Jan. 25, 2025, 12:56 p.m. UTC | #6
> > How about reducing the formula to directly shift the sizeof()
> instead, i.e.:
> > sizeof(uint32_t) << ctx->reta_sz_log,
> 
> Shifting the sizeof() directly is better indeed. Let me know how we
> should
> proceed. Do you want me to send out a new series incorporating this
> suggestion?

Yes, please.
  

Patch

diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
index fa78787143..f076311b57 100644
--- a/lib/hash/rte_thash.c
+++ b/lib/hash/rte_thash.c
@@ -565,7 +565,7 @@  rte_thash_add_helper(struct rte_thash_ctx *ctx, const char *name, uint32_t len,
 		offset;
 
 	ent = rte_zmalloc(NULL, sizeof(struct rte_thash_subtuple_helper) +
-		sizeof(uint32_t) * (1 << ctx->reta_sz_log),
+		sizeof(uint32_t) * (1ULL << ctx->reta_sz_log),
 		RTE_CACHE_LINE_SIZE);
 	if (ent == NULL)
 		return -ENOMEM;
diff --git a/lib/hash/rte_thash_gf2_poly_math.c b/lib/hash/rte_thash_gf2_poly_math.c
index 825da4382f..858884b4d4 100644
--- a/lib/hash/rte_thash_gf2_poly_math.c
+++ b/lib/hash/rte_thash_gf2_poly_math.c
@@ -119,16 +119,16 @@  static uint32_t
 gf2_mul(uint32_t a, uint32_t b, uint32_t r, int degree)
 {
 	uint64_t product = 0;
-	uint64_t r_poly = r|(1ULL << degree);
+	uint64_t r_poly = r | RTE_BIT64(degree);
 
 	for (; b; b &= (b - 1))
 		product ^= (uint64_t)a << (rte_bsf32(b));
 
 	for (int i = degree * 2 - 1; i >= degree; i--)
-		if (product & (1 << i))
+		if (product & RTE_BIT64(i))
 			product ^= r_poly << (i - degree);
 
-	return product;
+	return (uint32_t)product;
 }
 
 static uint32_t