[v2] eal/windows: resolve conversion and truncation warnings

Message ID 1709836482-22576-1-git-send-email-roretzla@linux.microsoft.com (mailing list archive)
State Rejected, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] eal/windows: resolve conversion and truncation warnings |

Checks

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

Commit Message

Tyler Retzlaff March 7, 2024, 6:34 p.m. UTC
  * Initialize const int NS_PER_SEC with an integer literal instead of
  double thereby avoiding implicit conversion from double to int.

* Cast the result of the expression assigned to timespec.tv_nsec to long.

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---

v2:
  * update commit message to correct misspelled timspec -> timespec,
    remove remarks about casting to long they were unnecessary.

 lib/eal/windows/include/rte_os_shim.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Bruce Richardson March 7, 2024, 8:53 p.m. UTC | #1
On Thu, Mar 07, 2024 at 10:34:42AM -0800, Tyler Retzlaff wrote:
> * Initialize const int NS_PER_SEC with an integer literal instead of
>   double thereby avoiding implicit conversion from double to int.
> 
> * Cast the result of the expression assigned to timespec.tv_nsec to long.
> 
> Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
> Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> ---
> 
> v2:
>   * update commit message to correct misspelled timspec -> timespec,
>     remove remarks about casting to long they were unnecessary.
> 
>  lib/eal/windows/include/rte_os_shim.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
> index eda8113..19b12e9 100644
> --- a/lib/eal/windows/include/rte_os_shim.h
> +++ b/lib/eal/windows/include/rte_os_shim.h
> @@ -87,7 +87,7 @@
>  static inline int
>  rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
>  {
> -	const int NS_PER_SEC = 1E9;
> +	const int NS_PER_SEC = 1000000000;

Just for readability, and the immediate visibility of errors, could this be
rewritten as (1000 * 1000 * 1000). That avoids us having to count the zeros
to know that the number is correct.

BTW: is "int" still the best type to use for this value? Would it be better
as a #define?

/Bruce

>  	LARGE_INTEGER pf, pc;
>  	LONGLONG nsec;
>  
> @@ -102,7 +102,7 @@
>  
>  		nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart;
>  		tp->tv_sec = nsec / NS_PER_SEC;
> -		tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC;
> +		tp->tv_nsec = (long)(nsec - tp->tv_sec * NS_PER_SEC);
>  		return 0;
>  	default:
>  		return -1;
> -- 
> 1.8.3.1
>
  
Tyler Retzlaff March 7, 2024, 8:57 p.m. UTC | #2
On Thu, Mar 07, 2024 at 08:53:49PM +0000, Bruce Richardson wrote:
> On Thu, Mar 07, 2024 at 10:34:42AM -0800, Tyler Retzlaff wrote:
> > * Initialize const int NS_PER_SEC with an integer literal instead of
> >   double thereby avoiding implicit conversion from double to int.
> > 
> > * Cast the result of the expression assigned to timespec.tv_nsec to long.
> > 
> > Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
> > Acked-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> > ---
> > 
> > v2:
> >   * update commit message to correct misspelled timspec -> timespec,
> >     remove remarks about casting to long they were unnecessary.
> > 
> >  lib/eal/windows/include/rte_os_shim.h | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
> > index eda8113..19b12e9 100644
> > --- a/lib/eal/windows/include/rte_os_shim.h
> > +++ b/lib/eal/windows/include/rte_os_shim.h
> > @@ -87,7 +87,7 @@
> >  static inline int
> >  rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
> >  {
> > -	const int NS_PER_SEC = 1E9;
> > +	const int NS_PER_SEC = 1000000000;
> 
> Just for readability, and the immediate visibility of errors, could this be
> rewritten as (1000 * 1000 * 1000). That avoids us having to count the zeros
> to know that the number is correct.
> 
> BTW: is "int" still the best type to use for this value? Would it be better
> as a #define?

i think to save spot fixing i'm going to withdraw the series for now. i
need to come back later and deal with warnings from MSVC more
comprehensively anyway.

thanks folks!

> 
> /Bruce
> 
> >  	LARGE_INTEGER pf, pc;
> >  	LONGLONG nsec;
> >  
> > @@ -102,7 +102,7 @@
> >  
> >  		nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart;
> >  		tp->tv_sec = nsec / NS_PER_SEC;
> > -		tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC;
> > +		tp->tv_nsec = (long)(nsec - tp->tv_sec * NS_PER_SEC);
> >  		return 0;
> >  	default:
> >  		return -1;
> > -- 
> > 1.8.3.1
> >
  

Patch

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index eda8113..19b12e9 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -87,7 +87,7 @@ 
 static inline int
 rte_clock_gettime(clockid_t clock_id, struct timespec *tp)
 {
-	const int NS_PER_SEC = 1E9;
+	const int NS_PER_SEC = 1000000000;
 	LARGE_INTEGER pf, pc;
 	LONGLONG nsec;
 
@@ -102,7 +102,7 @@ 
 
 		nsec = pc.QuadPart * NS_PER_SEC / pf.QuadPart;
 		tp->tv_sec = nsec / NS_PER_SEC;
-		tp->tv_nsec = nsec - tp->tv_sec * NS_PER_SEC;
+		tp->tv_nsec = (long)(nsec - tp->tv_sec * NS_PER_SEC);
 		return 0;
 	default:
 		return -1;