[v4,1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

Message ID 20240117182541.211125-2-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series use static assert to cathc build errors |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Jan. 17, 2024, 6:19 p.m. UTC
  These macros work like RTE_MIN and RTE_MAX but take an explicit
type. Necessary when being used in static assertions since
RTE_MIN and RTE_MAX use temporary variables which confuses
compilers constant expression checks. These macros could also
be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/include/rte_common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
  

Comments

Konstantin Ananyev Jan. 18, 2024, 9:35 a.m. UTC | #1
> These macros work like RTE_MIN and RTE_MAX but take an explicit
> type. Necessary when being used in static assertions since
> RTE_MIN and RTE_MAX use temporary variables which confuses
> compilers constant expression checks. These macros could also
> be useful in other scenarios when bounded range is useful.
> 
> Naming is chosen to be similar to Linux kernel conventions.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/eal/include/rte_common.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
> index c1ba32d00e47..33680e818bfb 100644
> --- a/lib/eal/include/rte_common.h
> +++ b/lib/eal/include/rte_common.h
> @@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
>  		_a < _b ? _a : _b; \
>  	})
> 
> +/**
> + * Macro to return the minimum of two numbers
> + * does not use temporarys so not safe if a or b is expression
> + * but is guaranteed to be constant for use in static_assert()
> + */
> +#define RTE_MIN_T(a, b, t) \
> +	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
> +
>  /**
>   * Macro to return the maximum of two numbers
>   */
> @@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
>  		_a > _b ? _a : _b; \
>  	})
> 
> +/**
> + * Macro to return the maxiimum of two numbers
> + * does not use temporarys so not safe if a or b is expression
> + * but is guaranteed to be constant for use in static_assert()
> + */
> +#define RTE_MAX_T(a, b, t) \
> +	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
> +
>  /*********** Other general functions / macros ********/
> 
>  #ifndef offsetof
> --

Acked-by: Konstantin Ananyev <konstantin.ananyev@huawei.com> 

> 2.43.0
  
Andrew Rybchenko Jan. 18, 2024, 9:44 a.m. UTC | #2
On 1/17/24 21:19, Stephen Hemminger wrote:
> These macros work like RTE_MIN and RTE_MAX but take an explicit
> type. Necessary when being used in static assertions since
> RTE_MIN and RTE_MAX use temporary variables which confuses
> compilers constant expression checks. These macros could also
> be useful in other scenarios when bounded range is useful.
> 
> Naming is chosen to be similar to Linux kernel conventions.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Acked-by: Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
  
Tyler Retzlaff Jan. 19, 2024, 8:58 p.m. UTC | #3
On Wed, Jan 17, 2024 at 10:19:55AM -0800, Stephen Hemminger wrote:
> These macros work like RTE_MIN and RTE_MAX but take an explicit
> type. Necessary when being used in static assertions since
> RTE_MIN and RTE_MAX use temporary variables which confuses
> compilers constant expression checks. These macros could also
> be useful in other scenarios when bounded range is useful.
> 
> Naming is chosen to be similar to Linux kernel conventions.

parameter ordering seems weird, also Linux kernel copied? just curious
more than anything. if not i would put 't' as the first parameter.

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>

> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/eal/include/rte_common.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
> index c1ba32d00e47..33680e818bfb 100644
> --- a/lib/eal/include/rte_common.h
> +++ b/lib/eal/include/rte_common.h
> @@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
>  		_a < _b ? _a : _b; \
>  	})
>  
> +/**
> + * Macro to return the minimum of two numbers
> + * does not use temporarys so not safe if a or b is expression
> + * but is guaranteed to be constant for use in static_assert()
> + */
> +#define RTE_MIN_T(a, b, t) \
> +	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
> +
>  /**
>   * Macro to return the maximum of two numbers
>   */
> @@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
>  		_a > _b ? _a : _b; \
>  	})
>  
> +/**
> + * Macro to return the maxiimum of two numbers
> + * does not use temporarys so not safe if a or b is expression
> + * but is guaranteed to be constant for use in static_assert()
> + */
> +#define RTE_MAX_T(a, b, t) \
> +	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
> +
>  /*********** Other general functions / macros ********/
>  
>  #ifndef offsetof
> -- 
> 2.43.0
  
Stephen Hemminger Jan. 19, 2024, 10:39 p.m. UTC | #4
On Fri, 19 Jan 2024 12:58:47 -0800
Tyler Retzlaff <roretzla@linux.microsoft.com> wrote:

> On Wed, Jan 17, 2024 at 10:19:55AM -0800, Stephen Hemminger wrote:
> > These macros work like RTE_MIN and RTE_MAX but take an explicit
> > type. Necessary when being used in static assertions since
> > RTE_MIN and RTE_MAX use temporary variables which confuses
> > compilers constant expression checks. These macros could also
> > be useful in other scenarios when bounded range is useful.
> > 
> > Naming is chosen to be similar to Linux kernel conventions.  
> 
> parameter ordering seems weird, also Linux kernel copied? just curious
> more than anything. if not i would put 't' as the first parameter.
> 
> Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>

Hmm, kernel version takes type first will use that.
  

Patch

diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index c1ba32d00e47..33680e818bfb 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -585,6 +585,14 @@  __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a < _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the minimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MIN_T(a, b, t) \
+	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
+
 /**
  * Macro to return the maximum of two numbers
  */
@@ -595,6 +603,14 @@  __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a > _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the maxiimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MAX_T(a, b, t) \
+	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
+
 /*********** Other general functions / macros ********/
 
 #ifndef offsetof