Skip to contentSkip to conversation

Staging: comedi: Avoid bitwise OR operator with zero valued variable

PATCH In review Somya Anand 2 participants 3 messages Mar 18, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: comedi: Avoid bitwise OR operator with zero valued variable

Zero ORed with any expression is equal to that expression itself. So, it is redundant to do OR with a variable whose value is 0.

This patch identifies the issue and simplify the code by using expression x itself instead of 0 | x where ever it is possible. While doing so, checks has been made that the variable is not further assigned some non zero value or there is no for or while loop. This change is done via following coccinelle script

@@
expression x,e,e1;
statement S;
@@

if (x == 0) {
<... when != x = e1

       when != while(...) S
       when != for(...;...;...) S

(
- x |= e;
+ x = e;
|
- x | e
+ e
)
...>
}

---
 drivers/staging/comedi/drivers/amplc_pci230.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/comedi/drivers/amplc_pci230.c +2 -2
index 2dd794d..330bc46 100644
@@ -1108,7 +1108,7 @@ static bool pci230_handle_ao_fifo(struct comedi_device *dev,
 		/* Check for FIFO underrun. */
 		if (dacstat & PCI230P2_DAC_FIFO_UNDERRUN_LATCHED) {
 			dev_err(dev->class_dev, "AO FIFO underrun\n");
-			events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+			events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
 		}
 		/*
 		 * Check for buffer underrun if FIFO less than half full
@@ -1118,7 +1118,7 @@ static bool pci230_handle_ao_fifo(struct comedi_device *dev,
 		if (num_scans == 0 &&
 		    (dacstat & PCI230P2_DAC_FIFO_HALF) == 0) {
 			dev_err(dev->class_dev, "AO buffer underrun\n");
-			events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+			events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
 		}
 	}
 	if (events == 0) {
Signature · 1 line
1.9.1
#1 lore raw
Quoted 46 lines · Somya Anand

Zero ORed with any expression is equal to that expression itself. So, it is redundant to do OR with a variable whose value is 0.

This patch identifies the issue and simplify the code by using expression x itself instead of 0 | x where ever it is possible. While doing so, checks has been made that the variable is not further assigned some non zero value or there is no for or while loop. This change is done via following coccinelle script

@@
expression x,e,e1;
statement S;
@@

if (x == 0) {
<... when != x = e1

       when != while(...) S
       when != for(...;...;...) S

(

(quoted diff) +1 -1
-  x |= e;
+  x = e;

|

(quoted diff) +1 -1
-  x | e
+  e

)
...>
}

---
 drivers/staging/comedi/drivers/amplc_pci230.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/comedi/drivers/amplc_pci230.c +1 -1
index 2dd794d..330bc46 100644
@@ -1108,7 +1108,7 @@ static bool pci230_handle_ao_fifo(struct comedi_device *dev,
 		/* Check for FIFO underrun. */
 		if (dacstat & PCI230P2_DAC_FIFO_UNDERRUN_LATCHED) {
 			dev_err(dev->class_dev, "AO FIFO underrun\n");
-			events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+			events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
 		}
 		/*
 		 * Check for buffer underrun if FIFO less than half full

drivers/staging/comedi/drivers/amplc_pci230.c static bool pci230_handle_ao_fifo(struct comedi_device *dev, · line 1121

Quoted 5 lines
drivers/staging/comedi/drivers/amplc_pci230.c +1 -1
@@ -1118,7 +1118,7 @@ static bool pci230_handle_ao_fifo(struct comedi_device *dev,
 		if (num_scans == 0 &&
 		    (dacstat & PCI230P2_DAC_FIFO_HALF) == 0) {
 			dev_err(dev->class_dev, "AO buffer underrun\n");
-			events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+			events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;

I think that this code was discussed already, and it was decided not to make the change. On the one hand, the issue is that your semantic patch only considers an assignment using =, whereas here before the second case there is an update of events using |= (ie the first case). On the other hand, the two assignments actually |= the same value, so they are actually the same as an = assignment. But maybe in reading the code it could look strange to abandon the previous value.

julia

Quoted 13 lines
drivers/staging/comedi/drivers/amplc_pci230.c +0 -1
 		}
 	}
 	if (events == 0) {
--

1.9.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/1426696229-25453-1-git-send-email-somyaanand214%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
Quoted 63 lines · Julia Lawall
Quoted 4 lines · Somya Anand

Zero ORed with any expression is equal to that expression itself. So, it is redundant to do OR with a variable whose value is 0.

This patch identifies the issue and simplify the code by using

expression x

Quoted 1 line

itself instead of 0 | x where ever it is possible. While doing so,

checks has

Quoted 1 line

been made that the variable is not further assigned some non zero value

or

Quoted 1 line

there is no for or while loop. This change is done via following

coccinelle script

Quoted 20 lines

@@
expression x,e,e1;
statement S;
@@

if (x == 0) {
<... when != x = e1

       when != while(...) S
       when != for(...;...;...) S

(

(quoted diff) +1 -1
-  x |= e;
+  x = e;

|

(quoted diff) +1 -1
-  x | e
+  e

)
...>
}

Review

Quoted 6 lines
---
 drivers/staging/comedi/drivers/amplc_pci230.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/comedi/drivers/amplc_pci230.c

drivers/staging/comedi/drivers/amplc_pci230.c static bool pci230_handle_ao_fifo(struct

Quoted 4 lines

index 2dd794d..330bc46 100644

drivers/staging/comedi/drivers/amplc_pci230.c +0 -0
@@ -1108,7 +1108,7 @@ static bool pci230_handle_ao_fifo(struct

comedi_device *dev,

drivers/staging/comedi/drivers/amplc_pci230.c static bool pci230_handle_ao_fifo(struct

Quoted 9 lines
drivers/staging/comedi/drivers/amplc_pci230.c +1 -1
              /* Check for FIFO underrun. */
              if (dacstat & PCI230P2_DAC_FIFO_UNDERRUN_LATCHED) {
                      dev_err(dev->class_dev, "AO FIFO underrun\n");
-                     events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+                     events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
              }
              /*
               * Check for buffer underrun if FIFO less than half full
@@ -1118,7 +1118,7 @@ static bool pci230_handle_ao_fifo(struct

comedi_device *dev,

drivers/staging/comedi/drivers/amplc_pci230.c static bool pci230_handle_ao_fifo(struct comedi_device *dev, · line 1121

Quoted 5 lines
drivers/staging/comedi/drivers/amplc_pci230.c +1 -1
              if (num_scans == 0 &&
                  (dacstat & PCI230P2_DAC_FIFO_HALF) == 0) {
                      dev_err(dev->class_dev, "AO buffer underrun\n");
-                     events |= COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
+                     events = COMEDI_CB_OVERFLOW | COMEDI_CB_ERROR;
Quoted 7 lines

I think that this code was discussed already, and it was decided not to make the change. On the one hand, the issue is that your semantic patch only considers an assignment using =, whereas here before the second case there is an update of events using |= (ie the first case). On the other hand, the two assignments actually |= the same value, so they are actually the same as an = assignment. But maybe in reading the code it could look strange to abandon the previous value.

I am sorry, I might have missed that discussion of code. Okay I got it
that its better to drop the change.

--
Somya

Quoted 1 line

julia

Quoted 20 lines
Quoted 10 lines
drivers/staging/comedi/drivers/amplc_pci230.c +0 -1
              }
      }
      if (events == 0) {
--

1.9.1

--
You received this message because you are subscribed to the Google

Groups "outreachy-kernel" group.

Quoted 1 line

To unsubscribe from this group and stop receiving emails from it, send

an email to [email protected].

Quoted 2 lines

To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/

msgid/outreachy-kernel/1426696229-25453-1-git-send- email-somyaanand214%40gmail.com.

Quoted 2 lines

For more options, visit https://groups.google.com/d/optout.