diff mbox series

[v4,03/10] fixp-arith: add a linear interpolation function

Message ID 20200910140000.324091-4-dmitry.baryshkov@linaro.org
State Superseded
Headers show
Series [v4,01/10] iio: adc: qcom-spmi-adc5: fix driver name | expand

Commit Message

Dmitry Baryshkov Sept. 10, 2020, 1:59 p.m. UTC
From: Craig Tatlor <ctatlor97@gmail.com>


Adds a function to interpolate against two points,
this is carried arount as a helper function by tons of drivers.

Signed-off-by: Craig Tatlor <ctatlor97@gmail.com>

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>

---
 include/linux/fixp-arith.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

-- 
2.28.0

Comments

Jonathan Cameron Sept. 13, 2020, 10:06 a.m. UTC | #1
On Thu, 10 Sep 2020 16:59:53 +0300
Dmitry Baryshkov <dmitry.baryshkov@linaro.org> wrote:

> From: Craig Tatlor <ctatlor97@gmail.com>

> 

> Adds a function to interpolate against two points,

> this is carried arount as a helper function by tons of drivers.

> 

> Signed-off-by: Craig Tatlor <ctatlor97@gmail.com>

> Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>


Seems sensible to have this.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>


> ---

>  include/linux/fixp-arith.h | 19 +++++++++++++++++++

>  1 file changed, 19 insertions(+)

> 

> diff --git a/include/linux/fixp-arith.h b/include/linux/fixp-arith.h

> index 8396013785ef..281cb4f83dbe 100644

> --- a/include/linux/fixp-arith.h

> +++ b/include/linux/fixp-arith.h

> @@ -141,4 +141,23 @@ static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)

>  #define fixp_cos32_rad(rad, twopi)	\

>  	fixp_sin32_rad(rad + twopi / 4, twopi)

>  

> +/**

> + * fixp_linear_interpolate() - interpolates a value from two known points

> + *

> + * @x0: x value of point 0

> + * @y0: y value of point 0

> + * @x1: x value of point 1

> + * @y1: y value of point 1

> + * @x: the linear interpolant

> + */

> +static inline int fixp_linear_interpolate(int x0, int y0, int x1, int y1, int x)

> +{

> +	if (y0 == y1 || x == x0)

> +		return y0;

> +	if (x1 == x0 || x == x1)

> +		return y1;

> +

> +	return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));

> +}

> +

>  #endif
diff mbox series

Patch

diff --git a/include/linux/fixp-arith.h b/include/linux/fixp-arith.h
index 8396013785ef..281cb4f83dbe 100644
--- a/include/linux/fixp-arith.h
+++ b/include/linux/fixp-arith.h
@@ -141,4 +141,23 @@  static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
 #define fixp_cos32_rad(rad, twopi)	\
 	fixp_sin32_rad(rad + twopi / 4, twopi)
 
+/**
+ * fixp_linear_interpolate() - interpolates a value from two known points
+ *
+ * @x0: x value of point 0
+ * @y0: y value of point 0
+ * @x1: x value of point 1
+ * @y1: y value of point 1
+ * @x: the linear interpolant
+ */
+static inline int fixp_linear_interpolate(int x0, int y0, int x1, int y1, int x)
+{
+	if (y0 == y1 || x == x0)
+		return y0;
+	if (x1 == x0 || x == x1)
+		return y1;
+
+	return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
+}
+
 #endif