Skip to contentSkip to conversation

find_closest() macro

PATCH Superseded Bartosz Golaszewski 3 participants 8 messages Feb 24, 2015 – Feb 26, 2015

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover find_closest() macro

This series proposes to unduplicate the code used to find the member in an array closest to 'x'.

The first patch adds a macro implementing the algorithm in two flavors - for arrays sorted in ascending and descending order. Other three patches replace duplicated code with calls to one of these macros in some hwmon drivers.

Bartosz Golaszewski (4):
kernel.h: add find_closest() macro
hwmon: (ina2xx) replace ina226_avg_bits() with find_closest()
hwmon: (lm85) replace XXX_TO_REG() functions with find_closest()
hwmon: (w83795) use find_closest_desc() in pwm_freq_to_reg()

 drivers/hwmon/ina2xx.c | 16 ++--------------
 drivers/hwmon/lm85.c   | 43 ++++++++++++-------------------------------
 drivers/hwmon/w83795.c |  7 ++-----
 include/linux/kernel.h | 23 +++++++++++++++++++++++
 4 files changed, 39 insertions(+), 50 deletions(-)
Signature · 1 line
2.1.4
#6 lore raw

PATCH 2/4 hwmon: (ina2xx) replace ina226_avg_bits() with find_closest()

Use find_closest() to locate the closest average in ina226_avg_tab.

---
 drivers/hwmon/ina2xx.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
drivers/hwmon/ina2xx.c +2 -14
index d1542b7..fc7f023 100644
@@ -141,19 +141,6 @@ static const struct ina2xx_config ina2xx_config[] = {
  */
 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
 
-static int ina226_avg_bits(int avg)
-{
-	int i;
-
-	/* Get the closest average from the tab. */
-	for (i = 0; i < ARRAY_SIZE(ina226_avg_tab) - 1; i++) {
-		if (avg <= (ina226_avg_tab[i] + ina226_avg_tab[i + 1]) / 2)
-			break;
-	}
-
-	return i; /* Return 0b0111 for values greater than 1024. */
-}
-
 static int ina226_reg_to_interval(u16 config)
 {
 	int avg = ina226_avg_tab[INA226_READ_AVG(config)];
@@ -171,7 +158,8 @@ static u16 ina226_interval_to_reg(int interval, u16 config)
 
 	avg = DIV_ROUND_CLOSEST(interval * 1000,
 				INA226_TOTAL_CONV_TIME_DEFAULT);
-	avg_bits = ina226_avg_bits(avg);
+	avg_bits = find_closest(avg, ina226_avg_tab,
+				ARRAY_SIZE(ina226_avg_tab));
 
 	return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits);
 }
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/4 kernel.h: add find_closest() macro

In reply to Bartosz Golaszewski

Searching for the member of an array closest to 'x' is duplicated in several places.

Add two macros that implement this algorithm for arrays sorted both in ascending and descending order.

---
 include/linux/kernel.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/linux/kernel.h +23 -0
index d6d630d..f4294da 100644
@@ -116,6 +116,29 @@
 }							\
 )
 
+#define __find_closest(x, a, as, op)(					\
+{									\
+	typeof(as) _i, _as = (as) - 1;					\
+	typeof(x) _x = (x);						\
+	typeof(*a) *_a = (a);						\
+	for (_i = 0; _i < _as; _i++) {					\
+		if (_x op DIV_ROUND_CLOSEST(_a[_i] + _a[_i + 1], 2))	\
+			break;						\
+	}								\
+	(_i);								\
+}									\
+)
+
+/*
+ * Given an array 'a' (sorted in ascending order) of size 'as' return
+ * the index of the element in that array closest to 'x'.
+ */
+#define find_closest(x, a, as) __find_closest(x, a, as, <=)
+/*
+ * Similar to find_closest(), but 'a' is expected to be sorted
+ * in descending order.
+ */
+#define find_closest_desc(x, a, as) __find_closest(x, a, as, >)
 
 #define _RET_IP_		(unsigned long)__builtin_return_address(0)
 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
Signature · 7 lines
2.1.4


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#7 lore raw

PATCH 4/4 hwmon: (w83795) use find_closest_desc() in pwm_freq_to_reg()

In reply to Bartosz Golaszewski

Replace the loop iterating over pwm_freq_cksel0 by a call to find_closest_desc().

---
 drivers/hwmon/w83795.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/hwmon/w83795.c +2 -5
index 2189413..1f0b301 100644
@@ -308,11 +308,8 @@ static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
 	unsigned long best0, best1;
 
 	/* Best fit for cksel = 0 */
-	for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
-		if (val > (pwm_freq_cksel0[reg0] +
-			   pwm_freq_cksel0[reg0 + 1]) / 2)
-			break;
-	}
+	reg0 = find_closest_desc(val, pwm_freq_cksel0,
+				 ARRAY_SIZE(pwm_freq_cksel0));
 	if (val < 375)	/* cksel = 1 can't beat this */
 		return reg0;
 	best0 = pwm_freq_cksel0[reg0];
Signature · 7 lines
2.1.4


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw

Re: [lm-sensors] [PATCH 1/4] kernel.h: add find_closest() macro

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

Searching for the member of an array closest to 'x' is duplicated in several places.

Add two macros that implement this algorithm for arrays sorted both in ascending and descending order.

I don't see the point here. You're not saving any code because your macros create functions at each invocation site. And your macro is more complicated than the code it replaces because it has all the syntactic cruft to make it adaptable to the different datatypes and sort orders.

Certainly it is easy to make an off by one mistake in a loop like this so there might be some small value there, but I'm not sure the complication is worth that savings for the small number of use points. Particularly because you're not saving any code.

Signature · 11 lines
Philip Pokorny, RHCE
Chief Technology Officer
PENGUIN COMPUTING, Inc
www.penguincomputing.com

Changing the world through technical innovation

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
maintainer
#3 lore raw

Re: [lm-sensors] [PATCH 1/4] kernel.h: add find_closest() macro

Quoted 15 lines · Phil Pokorny
Quoted 6 lines · Bartosz Golaszewski

Searching for the member of an array closest to 'x' is duplicated in several places.

Add two macros that implement this algorithm for arrays sorted both in ascending and descending order.

I don't see the point here. You're not saving any code because your macros create functions at each invocation site. And your macro is more complicated than the code it replaces because it has all the syntactic cruft to make it adaptable to the different datatypes and sort orders.

Quoted 5 lines

Certainly it is easy to make an off by one mistake in a loop like this so there might be some small value there, but I'm not sure the complication is worth that savings for the small number of use points. Particularly because you're not saving any code.

I think the lm85 conversion actually introduces a bug with such an off-by-one mistake. And if it doesn't, there is still a unexplained and not easy to understand '-1' in one of the calls to find_closest().

So the question is if the new code really improves the situation in that respect.

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#4 lore raw

Re: [lm-sensors] [PATCH 1/4] kernel.h: add find_closest() macro

2015-02-24 21:51 GMT+01:00 Guenter Roeck <[email protected]>:

Quoted 6 lines

I think the lm85 conversion actually introduces a bug with such an off-by-one mistake. And if it doesn't, there is still a unexplained and not easy to understand '-1' in one of the calls to find_closest().

So the question is if the new code really improves the situation in that respect.

Yes, it's a mistake. I couldn't test this one and missed this '-1'. Sorry for that.

As for the size comparisons - at first glance it seems as if it adds bloat:

ina2xx:
add/remove: 0/0 grow/shrink: 1/0 up/down: 24/0 (24)

function                                     old     new   delta
ina226_set_interval                          296     320     +24

lm85:
add/remove: 0/0 grow/shrink: 3/0 up/down: 72/0 (72)

function                                     old     new   delta
set_temp_auto_temp_min                       364     388     +24
set_temp_auto_temp_max                       336     360     +24
set_pwm_freq                                 284     308     +24

w83795:
add/remove: 0/0 grow/shrink: 1/0 up/down: 16/0 (16)

function                                     old     new   delta
store_pwm                                    496     512     +16

But this actually comes from DIV_ROUND_CLOSEST() since replacing it with a simple '/ 2' gives different results:

ina2xx.ko:
add/remove: 0/0 grow/shrink: 1/0 up/down: 6/0 (6)
function old new delta
__UNIQUE_ID_vermagic0 73 79 +6

lm85.ko:
add/remove: 0/0 grow/shrink: 1/0 up/down: 6/0 (6)
function old new delta
__UNIQUE_ID_vermagic0 73 79 +6

w83795.ko:
add/remove: 0/0 grow/shrink: 2/0 up/down: 14/0 (14)

function                                     old     new   delta
store_pwm                                    496     504      +8

__UNIQUE_ID_vermagic0 73 79 +6

The idea however was to remove duplicated operations from source files and prevent off-by-one bugs (how ironic the lm85 patch...), not really to reduce size.

Bart

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

maintainer
#5 lore raw

Re: [lm-sensors] [PATCH 1/4] kernel.h: add find_closest() macro

Quoted 45 lines · Bartosz Golaszewski

2015-02-24 21:51 GMT+01:00 Guenter Roeck <[email protected]>:

Quoted 6 lines

I think the lm85 conversion actually introduces a bug with such an off-by-one mistake. And if it doesn't, there is still a unexplained and not easy to understand '-1' in one of the calls to find_closest().

So the question is if the new code really improves the situation in that respect.

Yes, it's a mistake. I couldn't test this one and missed this '-1'. Sorry for that.

As for the size comparisons - at first glance it seems as if it adds bloat:

ina2xx:
add/remove: 0/0 grow/shrink: 1/0 up/down: 24/0 (24)

function                                     old     new   delta
ina226_set_interval                          296     320     +24

lm85:
add/remove: 0/0 grow/shrink: 3/0 up/down: 72/0 (72)

function                                     old     new   delta
set_temp_auto_temp_min                       364     388     +24
set_temp_auto_temp_max                       336     360     +24
set_pwm_freq                                 284     308     +24

w83795:
add/remove: 0/0 grow/shrink: 1/0 up/down: 16/0 (16)

function                                     old     new   delta
store_pwm                                    496     512     +16

But this actually comes from DIV_ROUND_CLOSEST() since replacing it with a simple '/ 2' gives different results:

ina2xx.ko:
add/remove: 0/0 grow/shrink: 1/0 up/down: 6/0 (6)
function old new delta
__UNIQUE_ID_vermagic0 73 79 +6

lm85.ko:
add/remove: 0/0 grow/shrink: 1/0 up/down: 6/0 (6)
function old new delta
__UNIQUE_ID_vermagic0 73 79 +6

w83795.ko:
add/remove: 0/0 grow/shrink: 2/0 up/down: 14/0 (14)

Quoted 8 lines
function                                     old     new   delta
store_pwm                                    496     504      +8

__UNIQUE_ID_vermagic0 73 79 +6

The idea however was to remove duplicated operations from source files and prevent off-by-one bugs (how ironic the lm85 patch...), not really to reduce size.

I don't know, seems to me it can cause more trouble and potential for getting something wrong than it is worth. I'll definitely want to see feedback (and acceptance) from more senior maintainers than myself.

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors