drivers/acpi/asus_acpi.c: Correct use of ! and &
Subsystems (2)
PATCH 2/9 drivers/acpi/asus_acpi.c: Correct use of ! and &
From: Julia Lawall <[email protected]>
In commit e6bafba5b4765a5a252f1b8d31cbf6d2459da337, a bug was fixed that involved converting !x & y to !(x & y). The code below shows the same pattern, and thus should perhaps be fixed in the same way.
This is not tested and clearly changes the semantics, so it is only something to consider.
The semantic patch that makes this change is as follows:
(http://www.emn.fr/x-info/coccinelle/)
// <smpl>
@@ expression E1,E2; @@
(
!E1 & !E2
|
- !E1 & E2
+ !(E1 & E2)
)
// </smpl>
---
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 renamed from drivers/acpi/asus_acpi.c 2008-02-10 22:34:05.000000000 +0100 +1 -1
@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask); if (invert) /* invert target value */ - led_out = !led_out & 0x1; + led_out = !(led_out & 0x1); if (!write_acpi_int(hotk->handle, ledname, led_out, NULL)) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 write_led(const char __user * buffer, un · line 616
Quoted 6 lines · Julia Lawall
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 renamed from drivers/acpi/asus_acpi.c 2008-02-10 22:34:05.000000000 +0100 +1
-1if (invert) /* invert target value */ - led_out = !led_out & 0x1; + led_out = !(led_out & 0x1); if (!write_acpi_int(hotk->handle, ledname, led_out, NULL)) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
IIRC we're just supposed to flip the last bit here, so the original code is correct.
Best regards,
Signature · 2 lines
Karol Kozimor [email protected]
Quoted 11 lines · Karol Kozimor
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 write_led(const char __user * buffer, un · line 616
Quoted 6 lines · Julia Lawall
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 renamed from drivers/acpi/asus_acpi.c 2008-02-10 22:34:05.000000000 +0100 +1
-1if (invert) /* invert target value */ - led_out = !led_out & 0x1; + led_out = !(led_out & 0x1); if (!write_acpi_int(hotk->handle, ledname, led_out, NULL)) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",IIRC we're just supposed to flip the last bit here, so the original code is correct.
I spent some time thinking about this one. The original code is ok if led_out is always either 0x01 or 0x00. But what if it is eg 0xc0? Then the negation amounts to the negation of a nonzero number, so the result is
0. So the result of the bit and is 0. So the last bit is not flipped. But I don't know what is the range of led_out. If it is always 1 or 0, then why bother with the bit and?
julia
Quoted 5 lines · Karol Kozimor
Quoted 4 lines · Julia Lawall
if (invert) /* invert target value */
drivers/acpi/asus_acpi.c 2008-02-26 08:00:42.000000000 +0100 renamed from drivers/acpi/asus_acpi.c 2008-02-10 22:34:05.000000000 +0100 +1
-1- led_out = !led_out & 0x1; + led_out = !(led_out & 0x1);
Quoted 8 lines
Quoted 2 lines
if (!write_acpi_int(hotk->handle, ledname,
led_out, NULL))
printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",IIRC we're just supposed to flip the last bit here, so the original code is correct.
Best regards,
Seems an odd way of doing:
led_out ^= 0x01;
It this due to some optimisation?
Cheers, Mark.
Quoted 10 lines · On Wed, 27 Feb 2008 19:29:15 +0100 Mark Pearson <[email protected]>…
Quoted 9 lines · Karol Kozimor
Quoted 6 lines · Julia Lawall
if (invert) /* invert target value */
(quoted diff) +1
-1- led_out = !led_out & 0x1; + led_out = !(led_out & 0x1); if (!write_acpi_int(hotk->handle, ledname, led_out, NULL)) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
Quoted 8 lines
Quoted 4 lines
IIRC we're just supposed to flip the last bit here, so the original code is correct.
Best regards,Seems an odd way of doing:
led_out ^= 0x01;
It does.
Quoted 1 line
It this due to some optimisation?
Surely not ;)
That code has been there for many years.
I changed the patch to this:
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1 -1
@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;if (!write_acpi_int (hotk->handle, ledname, led_out,NULL )) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
_
drivers/acpi/asus_acpi.c write_led(const char __user * buffer, un · line 615
Quoted 5 lines · Andrew Morton
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;if (!write_acpi_int (hotk->handle, ledname, led_out,NULL ))
But now you're writing 0xffffffff instead of 1. I think the suggestion of led_out ^= 1 was the correct one.
Signature · 4 lines
Intel are signing my paycheques ... these opinions are still mine "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 9 lines · Matthew Wilcox
drivers/acpi/asus_acpi.c write_led(const char __user * buffer, un · line 615
Quoted 5 lines · Andrew Morton
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;if (!write_acpi_int (hotk->handle, ledname, led_out,NULL ))But now you're writing
0xffffffffinstead of 1. I think the suggestion ofled_out^= 1 was the correct one.
! is not ~
! is not ~
! is not ~
....
I'll go to sleep now.
Signature · 4 lines
Intel are signing my paycheques ... these opinions are still mine "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 10 lines · On Thu, 28 Feb 2008 23:10:23 -0700 Matthew Wilcox <[email protected]> wrote:
drivers/acpi/asus_acpi.c write_led(const char __user * buffer, un · line 615
Quoted 5 lines · Andrew Morton
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;if (!write_acpi_int (hotk->handle, ledname, led_out,NULL ))But now you're writing
0xffffffffinstead of 1. I think the suggestion ofled_out^= 1 was the correct one.
!0 is 1, not 0xffffffff.
IOW, ! != ~ ;)
Quoted 34 lines · Andrew Morton
Quoted 10 lines · On Wed, 27 Feb 2008 19:29:15 +0100 Mark Pearson <[email protected]>…
Quoted 9 lines · Karol Kozimor
Quoted 6 lines · Julia Lawall
if (invert) /* invert target value */
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1- led_out = !led_out &0x1 ; + led_out = !(led_out &0x1 );if (!write_acpi_int (hotk->handle, ledname, led_out,NULL )) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",Quoted 8 lines
Quoted 4 lines
IIRC we're just supposed to flip the last bit here, so the original code is correct.
Best regards,Seems an odd way of doing:
led_out ^= 0x01;It does.
Quoted 1 line
It this due to some optimisation?
Surely not ;)
That code has been there for many years.
I changed the patch to this:
drivers/acpi/asus_acpi.c write_led(const char __user * buffer, un · line 613
Quoted 5 lines
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;
I don't think this is the same:
!(0110 & 0x01) = !0 = 1
!0110 = 0
led_out ^= 0x01;
is also not the same:
0110 ^ 0x01 = 0111
Is it desired to keep the value and flip the last bit or just obtain the opposite of the last bit?
julia
Quoted 47 lines · On Fri, 29 Feb 2008 12:01:26 +0100 (CET) Julia Lawall <[email protected]> wrote:
Quoted 34 lines · Andrew Morton
Quoted 10 lines · On Wed, 27 Feb 2008 19:29:15 +0100 Mark Pearson <[email protected]>…
Quoted 9 lines · Karol Kozimor
Quoted 6 lines · Julia Lawall
if (invert) /* invert target value */
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1- led_out = !led_out &0x1 ; + led_out = !(led_out &0x1 );if (!write_acpi_int (hotk->handle, ledname, led_out,NULL )) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",Quoted 8 lines
Quoted 4 lines
IIRC we're just supposed to flip the last bit here, so the original code is correct.
Best regards,Seems an odd way of doing:
led_out ^= 0x01;It does.
Quoted 1 line
It this due to some optimisation?
Surely not ;)
That code has been there for many years.
I changed the patch to this:
drivers/acpi/asus_acpi.c write_led(const char __user * buffer, un · line 613
Quoted 5 lines
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;I don't think this is the same:
!(0110 &
0x01) = !0 = 1
!0110 = 0
Quoted 9 lines
led_out^=0x01;is also not the same:
0110 ^
0x01= 0111Is it desired to keep the value and flip the last bit or just obtain the opposite of the last bit?
led_out can only take the values 0 or 1 here.
Quoted 10 lines · Andrew Morton
Quoted 3 lines
Seems an odd way of doing:
led_out ^= 0x01;It does.
Quoted 1 line
It this due to some optimisation?
Surely not ;)
;) Thought so - one doesn't like to be too presumptuous ;)
Quoted 15 lines
That code has been there for many years.
I changed the patch to this:
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;if (!write_acpi_int (hotk->handle, ledname, led_out,NULL )) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
Quoted 3 lines
_
Is the ! operator architecture/compiler dependent? or can one always say that !NON_ZERO_VALUE = 0 and !0 = 1?
Cheers, Mark.
Quoted 26 lines · On Fri, 29 Feb 2008 19:06:48 +0100 Mark Pearson <[email protected]>…
Quoted 10 lines · Andrew Morton
Quoted 3 lines
Seems an odd way of doing:
led_out ^= 0x01;It does.
Quoted 1 line
It this due to some optimisation?
Surely not ;)
;) Thought so - one doesn't like to be too presumptuous ;)
Quoted 13 lines
That code has been there for many years.
I changed the patch to this:
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +1
-1@@ -610,7 +610,7 @@ write_led(const char __user * buffer, un (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);if (invert)/* invert target value */ - led_out = !led_out &0x1 ; + led_out = !led_out;
Quoted 7 lines
Quoted 5 lines
drivers/acpi/asus_acpi.c renamed from drivers/acpi/asus_acpi.c~drivers-acpi-asus_acpic-correct-use-of-and +0
-0if (!write_acpi_int (hotk->handle, ledname, led_out,NULL )) printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",_
Is the ! operator architecture/compiler dependent?
It shouldn't be.
Quoted 3 lines
or can one always say that
!NON_ZERO_VALUE = 0 and !0 = 1?
I always have ;) I expect it's in the C standard somewhere.