[v1,3/4] ipa: libipa: pwl: Add construction from initializer_list
diff mbox series

Message ID 20251014142427.3107490-4-stefan.klug@ideasonboard.com
State New
Headers show
Series
  • libipa: agc: Fix constraints yTarget handling and add PWL support
Related show

Commit Message

Stefan Klug Oct. 14, 2025, 2:24 p.m. UTC
In the tuning files it is possible to construct the PWL from a linear
list of numbers. Add a initializer_list based constructor to replicate
similar style in C++ code. An initializer_list is preferred to a Span
based constructor because the Span makes the static construction
cumbersome ( {{{ 1.0, 2.0 }}} vs { 1.0, 2.0 } ) and initialization is
the only use case at the moment.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---
 src/ipa/libipa/pwl.cpp | 19 +++++++++++++++++++
 src/ipa/libipa/pwl.h   |  1 +
 2 files changed, 20 insertions(+)

Comments

Barnabás Pőcze Oct. 14, 2025, 3:03 p.m. UTC | #1
Hi

2025. 10. 14. 16:24 keltezéssel, Stefan Klug írta:
> In the tuning files it is possible to construct the PWL from a linear
> list of numbers. Add a initializer_list based constructor to replicate
> similar style in C++ code. An initializer_list is preferred to a Span
> based constructor because the Span makes the static construction
> cumbersome ( {{{ 1.0, 2.0 }}} vs { 1.0, 2.0 } ) and initialization is
> the only use case at the moment.
> 
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> ---
>   src/ipa/libipa/pwl.cpp | 19 +++++++++++++++++++
>   src/ipa/libipa/pwl.h   |  1 +
>   2 files changed, 20 insertions(+)
> 
> diff --git a/src/ipa/libipa/pwl.cpp b/src/ipa/libipa/pwl.cpp
> index 1858ab37b101..501ac79ea8b5 100644
> --- a/src/ipa/libipa/pwl.cpp
> +++ b/src/ipa/libipa/pwl.cpp
> @@ -122,6 +122,25 @@ Pwl::Pwl(std::vector<Point> &&points)
>   {
>   }
>   
> +/**
> + * \brief Construct a piecewise linear function from a initializer list
> + * \param[in] data initializer list of doubles
> + *
> + * This constructor takes a even number of doubles and treats them as
> + * x0,y0,x1,y1,...,xn,yn coordinates for the PWL.
> + */
> +Pwl::Pwl(std::initializer_list<double> data)
> +{
> +	ASSERT((data.size() % 2) == 0);

   std::initializer_list<Point>

?

I am not a fan of the runtime %2==0 assertion when the same requirement could be
expressed in the type system easily. Admittedly that will need more braces, but
I still think it's better.

In any case, I would really prefer if the runtime assert could be avoided.
And I think you could already be able to do this initialization without a new
constructor. `Pwl{{ {{0.0, 0.5}}, ... }}` because of `Pwl::Pwl(std::vector<Point>&&)`
It's indeed a bit inconvenient because `Vector` has no n-argument constructors.


Regards,
Barnabás Pőcze

> +
> +	auto iter = data.begin();
> +	while (iter != data.end()) {
> +		double x = *iter++;
> +		double y = *iter++;
> +		append(x, y);
> +	}
> +}
> +
>   /**
>    * \brief Append a point to the end of the piecewise linear function
>    * \param[in] x x-coordinate of the point to add to the piecewise linear function
> diff --git a/src/ipa/libipa/pwl.h b/src/ipa/libipa/pwl.h
> index add20b5867af..b82b17317a5e 100644
> --- a/src/ipa/libipa/pwl.h
> +++ b/src/ipa/libipa/pwl.h
> @@ -45,6 +45,7 @@ public:
>   	Pwl();
>   	Pwl(const std::vector<Point> &points);
>   	Pwl(std::vector<Point> &&points);
> +	Pwl(std::initializer_list<double> data);
>   
>   	void append(double x, double y, double eps = 1e-6);
>

Patch
diff mbox series

diff --git a/src/ipa/libipa/pwl.cpp b/src/ipa/libipa/pwl.cpp
index 1858ab37b101..501ac79ea8b5 100644
--- a/src/ipa/libipa/pwl.cpp
+++ b/src/ipa/libipa/pwl.cpp
@@ -122,6 +122,25 @@  Pwl::Pwl(std::vector<Point> &&points)
 {
 }
 
+/**
+ * \brief Construct a piecewise linear function from a initializer list
+ * \param[in] data initializer list of doubles
+ *
+ * This constructor takes a even number of doubles and treats them as
+ * x0,y0,x1,y1,...,xn,yn coordinates for the PWL.
+ */
+Pwl::Pwl(std::initializer_list<double> data)
+{
+	ASSERT((data.size() % 2) == 0);
+
+	auto iter = data.begin();
+	while (iter != data.end()) {
+		double x = *iter++;
+		double y = *iter++;
+		append(x, y);
+	}
+}
+
 /**
  * \brief Append a point to the end of the piecewise linear function
  * \param[in] x x-coordinate of the point to add to the piecewise linear function
diff --git a/src/ipa/libipa/pwl.h b/src/ipa/libipa/pwl.h
index add20b5867af..b82b17317a5e 100644
--- a/src/ipa/libipa/pwl.h
+++ b/src/ipa/libipa/pwl.h
@@ -45,6 +45,7 @@  public:
 	Pwl();
 	Pwl(const std::vector<Point> &points);
 	Pwl(std::vector<Point> &&points);
+	Pwl(std::initializer_list<double> data);
 
 	void append(double x, double y, double eps = 1e-6);