Skip to contentSkip to conversation

mceusb: Optimize DIV_ROUND_CLOSEST call

PATCH In review Jean Delvare 3 participants 6 messages Sep 1, 2012 – Sep 19, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mceusb: Optimize DIV_ROUND_CLOSEST call

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;
Signature · 1 line
Jean Delvare
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jean Delvare

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
Quoted 20 lines
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;
Signature · 2 lines
-- 
Jean Delvare
#2 lore raw
In reply to Jean Delvare

Em 01-09-2012 15:53, Jean Delvare escreveu:

Quoted 16 lines

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/media/rc/mceusb.c static void mceusb_dev_printdata(struct · line 633

Quoted 8 lines
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;

Hmm... this generates the following warning with "W=1":

drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

Perhaps it makes sense to use an optimized version for unsigned, or to change the macro to take the data types into account.

Regards,
Mauro

#3 lore raw
Quoted 28 lines · Mauro Carvalho Chehab

Em 01-09-2012 15:53, Jean Delvare escreveu:

Quoted 16 lines

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/media/rc/mceusb.c static void mceusb_dev_printdata(struct · line 633

Quoted 8 lines
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;

Hmm... this generates the following warning with "W=1":

Quoted 6 lines

drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

Perhaps it makes sense to use an optimized version for unsigned, or to change the macro to take the data types into account.

DIV_ROUND_CLOSEST tests "((typeof(x))-1) >= 0" on purpose, so the compiler can optimize the signed part of the macro away if the variable type is unsigned. The test was borrowed from C99 code. Would be great if someone has an idea how to tell the compiler not to create a warning for this test.

Guenter

#4 lore raw
In reply to Mauro Carvalho Chehab

Hi Mauro,

Quoted 26 lines · Mauro Carvalho Chehab

Em 01-09-2012 15:53, Jean Delvare escreveu:

Quoted 14 lines

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/media/rc/mceusb.c static void mceusb_dev_printdata(struct · line 633

Quoted 8 lines
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;

Hmm... this generates the following warning with "W=1":

Quoted 2 lines

drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

I doubt this is the only warning of that kind. There must be a reason why -Wextra isn't enabled by default.

Quoted 2 lines

Perhaps it makes sense to use an optimized version for unsigned, or to change the macro to take the data types into account.

This was discussed before, but Andrew said he preferred a single macro. And I agree with him, having two macros would induce a risk of the wrong one being called.

If you can come up with a variant of DIV_ROUND_CLOSEST which performs the same and doesn't trigger the warning above, we'll be happy to see it, but neither Guenter nor myself could come up with one.

Signature · 1 line
Jean Delvare
#5 lore raw
Quoted 38 lines · Jean Delvare

Hi Mauro,

Quoted 26 lines · Mauro Carvalho Chehab

Em 01-09-2012 15:53, Jean Delvare escreveu:

Quoted 14 lines

DIV_ROUND_CLOSEST is faster if the compiler knows it will only be dealing with unsigned dividends.

Cc Andrew Morton <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
---
 drivers/media/rc/mceusb.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/media/rc/mceusb.c static void mceusb_dev_printdata(struct · line 633

Quoted 8 lines
drivers/media/rc/mceusb.c +1 -1
@@ -627,7 +627,7 @@ static void mceusb_dev_printdata(struct
 			break;
 		case MCE_RSP_EQIRCFS:
 			period = DIV_ROUND_CLOSEST(
-					(1 << data1 * 2) * (data2 + 1), 10);
+					(1U << data1 * 2) * (data2 + 1), 10);
 			if (!period)
 				break;
 			carrier = (1000 * 1000) / period;

Hmm... this generates the following warning with "W=1":

Quoted 2 lines

drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] drivers/media/rc/mceusb.c:629:4: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]

I doubt this is the only warning of that kind. There must be a reason why -Wextra isn't enabled by default.

Quoted 2 lines

Perhaps it makes sense to use an optimized version for unsigned, or to change the macro to take the data types into account.

Quoted 8 lines

This was discussed before, but Andrew said he preferred a single macro. And I agree with him, having two macros would induce a risk of the wrong one being called.

If you can come up with a variant of DIV_ROUND_CLOSEST which performs the same and doesn't trigger the warning above, we'll be happy to see it, but neither Guenter nor myself could come up with one.

I did some more research, and I think I found a fix. I'll send out a patch in a minute for people to try.

Guenter