Skip to contentSkip to conversation

joydev: fix HZ->millisecond transformation

PATCH In review Bjorn Helgaas 4 participants 9 messages Dec 16, 2002 – Dec 21, 2002 Input drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 joydev: fix HZ->millisecond transformation

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

drivers/input/joydev.c +4 -2
diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS		32
 #define JOYDEV_BUFFER_SIZE	64
 
+#define MSECS(t)	(1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)
+
 struct joydev {
 	int exist;
 	int open;
@@ -134,7 +136,7 @@
 			return;
 	}  
 
-	event.time = jiffies * (1000 / HZ);
+	event.time = MSECS(jiffies);
 
 	while (list) {
 
@@ -279,7 +281,7 @@
 
 		struct js_event event;
 
-		event.time = jiffies * (1000/HZ);
+		event.time = MSECS(jiffies);
 
 		if (list->startup < joydev->nkey) {
 			event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
#1 lore raw
Quoted 10 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 4 lines
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

Quoted 28 lines
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +3 -3
+
 struct joydev {
        int exist;
        int open;
@@ -134,7 +136,7 @@
                        return;
        }
 
-       event.time = jiffies * (1000 / HZ);
+       event.time = MSECS(jiffies);
 
        while (list) {
 
@@ -279,7 +281,7 @@
 
                struct js_event event;
 
-               event.time = jiffies * (1000/HZ);
+               event.time = MSECS(jiffies);
 
                if (list->startup < joydev->nkey) {
                        event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
 
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 5 lines
George Anzinger   [email protected]
High-res-timers: 
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml
#2 lore raw
Quoted 14 lines · george anzinger
Quoted 13 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +0 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
Quoted 4 lines

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 1 line
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

No, both parts of the equaition can be nonzero.

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

Signature · 2 lines
Vojtech Pavlik
SuSE Labs
#3 lore raw

[ repost -- I am going to have to get outgoing mail fixed on my lkml-reading
machine ]

Quoted 10 lines · george anzinger
Quoted 7 lines
Quoted 6 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])
[...]
+#define MSECS(t) (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)
[...]

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

The more complex version avoids integer overflow for more values of 't' in the (1000 * t) subexpression.

Joe

#4 lore raw
In reply to Vojtech Pavlik
Quoted 16 lines · Vojtech Pavlik
Quoted 14 lines · george anzinger
Quoted 13 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +0 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
Quoted 6 lines
Quoted 4 lines

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 1 line
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

No, both parts of the equaition can be nonzero.

I don't think so. s%HZ has to be less than HZ. Then dividing that by HZ should result in zero. Where is my thinking flawed?

Quoted 3 lines

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

That overflows... As does the other if HZ is less than 1000....

Quoted 9 lines

--
Vojtech Pavlik
SuSE Labs
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 5 lines
George Anzinger   [email protected]
High-res-timers: 
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml
#5 lore raw
Quoted 22 lines · george anzinger
Quoted 21 lines · Vojtech Pavlik
Quoted 18 lines · george anzinger
Quoted 10 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 4 lines
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

Quoted 5 lines
Quoted 1 line

No, both parts of the equaition can be nonzero.

I don't think so. s%HZ has to be less than HZ. Then dividing that by HZ should result in zero. Where is my thinking flawed?

You first multiply it by 1000.

Quoted 4 lines
Quoted 2 lines

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

That overflows... As does the other if HZ is less than 1000....

You're right, t can be all 32 bits.

Signature · 2 lines
Vojtech Pavlik
SuSE Labs
#6 lore raw
Quoted 24 lines · Vojtech Pavlik
Quoted 22 lines · george anzinger
Quoted 21 lines · Vojtech Pavlik
Quoted 18 lines · george anzinger
Quoted 10 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 4 lines
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

No, both parts of the equaition can be nonzero.

I don't think so. s%HZ has to be less than HZ. Then dividing that by HZ should result in zero. Where is my thinking flawed?

You first multiply it by 1000.

But then it should read: (1000 * (t)) % HZ) / HZ

But this is still zero. There is no way that ((X % HZ) / HZ) is other than zero, me thinks.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

That overflows... As does the other if HZ is less than 1000....

You're right, t can be all 32 bits.

What, exactly, is this code trying to do? It looks like the number is being passed to user space...

Quoted 9 lines

--
Vojtech Pavlik
SuSE Labs
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 5 lines
George Anzinger   [email protected]
High-res-timers: 
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml
#7 lore raw
Quoted 27 lines · george anzinger
Quoted 26 lines · Vojtech Pavlik
Quoted 24 lines · george anzinger
Quoted 22 lines · Vojtech Pavlik
Quoted 14 lines · george anzinger
Quoted 13 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +0 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
Quoted 4 lines

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 1 line
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

No, both parts of the equaition can be nonzero.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I don't think so. s%HZ has to be less than HZ. Then dividing that by HZ should result in zero. Where is my thinking flawed?

You first multiply it by 1000.

But then it should read: (1000 * (t)) % HZ) / HZ

The expression is evaluated from left to right. No need for parentheses here.

Quoted 2 lines

But this is still zero. There is no way that ((X % HZ) / HZ) is other than zero, me thinks.

Example:

HZ = 100
t = 70

1000 * (t % HZ) / HZ = 1000 * 70 / 100 = 700 != 0

Anyway (x % HZ) / HZ must be zero, but that's something different.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

That overflows... As does the other if HZ is less than 1000....

You're right, t can be all 32 bits.

What, exactly, is this code trying to do? It looks like the number is being passed to user space...

It's trying to convert jiffies to microseconds from computer start (or any other arbitrary point in time), thus making the value independent of HZ.

Quoted 6 lines
Signature · 5 lines
George Anzinger   [email protected]
High-res-timers: 
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml
Signature · 2 lines
Vojtech Pavlik
SuSE Labs
#8 lore raw
Quoted 43 lines · Vojtech Pavlik
Quoted 27 lines · george anzinger
Quoted 26 lines · Vojtech Pavlik
Quoted 24 lines · george anzinger
Quoted 22 lines · Vojtech Pavlik
Quoted 14 lines · george anzinger
Quoted 13 lines · Bjorn Helgaas

* fix a problem with HZ->millisecond transformation on non-x86 archs (from 2.5 change by [email protected])

Applies to 2.4.20.

diff -Nru a/drivers/input/joydev.c b/drivers/input/joydev.c

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +0 -0
@@ -50,6 +50,8 @@
 #define JOYDEV_MINORS          32
 #define JOYDEV_BUFFER_SIZE     64
 
Quoted 4 lines

drivers/input/joydev.c Mon Dec 16 12:16:32 2002 line 53

Quoted 1 line
drivers/input/joydev.c Mon Dec 16 12:16:32 2002 +1 -0
+#define MSECS(t)       (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)

Uh...
^^^^^^^^^^^^^^^^
by definition this is zero, is it not?

No, both parts of the equaition can be nonzero.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I don't think so. s%HZ has to be less than HZ. Then dividing that by HZ should result in zero. Where is my thinking flawed?

You first multiply it by 1000.

But then it should read: (1000 * (t)) % HZ) / HZ

The expression is evaluated from left to right. No need for parentheses here.

Quoted 2 lines

But this is still zero. There is no way that ((X % HZ) / HZ) is other than zero, me thinks.

Quoted 8 lines

Example:

HZ = 100
t = 70

1000 * (t % HZ) / HZ = 1000 * 70 / 100 = 700 != 0

Anyway (x % HZ) / HZ must be zero, but that's something different.

OK, I miss read the "("s. :(

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Though it might be easier to say (1000 * t) / HZ, now that I think about it.

That overflows... As does the other if HZ is less than 1000....

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

You're right, t can be all 32 bits.

What, exactly, is this code trying to do? It looks like the number is being passed to user space...

It's trying to convert jiffies to microseconds from computer start (or any other arbitrary point in time), thus making the value independent of HZ.

This is possible, but in general it can not be kept in one 32-bit word. I don't know what it is used for or if the format can be changed, but the struct timeval comes to mind.

Signature · 5 lines
George Anzinger   [email protected]
High-res-timers: 
http://sourceforge.net/projects/high-res-timers/
Preemption patch:
http://www.kernel.org/pub/linux/kernel/people/rml