Skip to contentSkip to conversation

arm: nomadik: avoid assigning u32 to bool

PATCH In review Nicolas Kaiser 4 participants 6 messages Apr 1, 2011 – Apr 3, 2011

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

PATCH 1/1 arm: nomadik: avoid assigning u32 to bool

Avoid assigning u32 to the bool 'enabled'.

---
 arch/arm/plat-nomadik/gpio.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/arm/plat-nomadik/gpio.c +1 -1
index f49748e..8b722f7 100644
@@ -636,7 +636,7 @@ static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
 	if (type & IRQ_TYPE_LEVEL_LOW)
 		return -EINVAL;
 
-	enabled = nmk_chip->enabled & bitmask;
+	enabled = !!(nmk_chip->enabled & bitmask);
 
 	spin_lock_irqsave(&nmk_chip->lock, flags);
 
Signature · 1 line
1.7.3.4
Acked-by on v1 1/1
#1 lore raw

2011/4/1 Nicolas Kaiser <[email protected]>:

Quoted 12 lines

Avoid assigning u32 to the bool 'enabled'.

---
?arch/arm/plat-nomadik/gpio.c | ? ?2 +-
?1 files changed, 1 insertions(+), 1 deletions(-)

arch/arm/plat-nomadik/gpio.c +0 -0
index f49748e..8b722f7 100644

arch/arm/plat-nomadik/gpio.c static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) · line 639

Quoted 5 lines
arch/arm/plat-nomadik/gpio.c +1 -1
@@ -636,7 +636,7 @@ static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
 ? ? ? ?if (type & IRQ_TYPE_LEVEL_LOW)
 ? ? ? ? ? ? ? ?return -EINVAL;
 
- ? ? ? enabled = nmk_chip->enabled & bitmask;
+ ? ? ? enabled = !!(nmk_chip->enabled & bitmask);

I've learned to live this way of casting stuff into boolean,

Linus Walleij

#2 lore raw
Quoted 4 lines

arch/arm/plat-nomadik/gpio.c static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) · line 639

Quoted 2 lines
arch/arm/plat-nomadik/gpio.c +1 -1
-       enabled = nmk_chip->enabled & bitmask;
+       enabled = !!(nmk_chip->enabled & bitmask);

I've learned to live this way of casting stuff into boolean,

I use (and love) !! since I learnt C. What I really dislike is the concept of boolean that doesn't fit in the C way of thinking, in my opinion. But I'm not going to change that.

/alessandro

#3 lore raw
In reply to Nicolas Kaiser
Quoted 12 lines · Nicolas Kaiser

Avoid assigning u32 to the bool 'enabled'.

---
?arch/arm/plat-nomadik/gpio.c | ? ?2 +-
?1 files changed, 1 insertions(+), 1 deletions(-)

arch/arm/plat-nomadik/gpio.c +0 -0
index f49748e..8b722f7 100644

arch/arm/plat-nomadik/gpio.c static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) · line 639

Quoted 5 lines
arch/arm/plat-nomadik/gpio.c +1 -1
@@ -636,7 +636,7 @@ static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
 ? ? ? ?if (type & IRQ_TYPE_LEVEL_LOW)
 ? ? ? ? ? ? ? ?return -EINVAL;
 
- ? ? ? enabled = nmk_chip->enabled & bitmask;
+ ? ? ? enabled = !!(nmk_chip->enabled & bitmask);

Why? There are dozens of places in the kernel where this done, the code generated should be the same, and it's pretty obvious what is being done as it is.

#4 lore raw

* Rabin Vincent <[email protected]>:

Quoted 16 lines · Nicolas Kaiser
Quoted 15 lines

Avoid assigning u32 to the bool 'enabled'.

---
?arch/arm/plat-nomadik/gpio.c | ? ?2 +-
?1 files changed, 1 insertions(+), 1 deletions(-)

arch/arm/plat-nomadik/gpio.c +0 -0
index f49748e..8b722f7 100644
@@ -636,7 +636,7 @@ static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
 ? ? ? ?if (type & IRQ_TYPE_LEVEL_LOW)
 ? ? ? ? ? ? ? ?return -EINVAL;
 
Quoted 6 lines

arch/arm/plat-nomadik/gpio.c static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type) · line 639

Quoted 2 lines
arch/arm/plat-nomadik/gpio.c +1 -1
- ? ? ? enabled = nmk_chip->enabled & bitmask;
+ ? ? ? enabled = !!(nmk_chip->enabled & bitmask);

Why? There are dozens of places in the kernel where this done, the code generated should be the same, and it's pretty obvious what is being done as it is.

Primarily because we were asked to avoid casts to bool even if they are safe.
https://lkml.org/lkml/2011/4/1/255

Besides, many of the places I found that do 'bool a = b & c;' actually do 'bool a = b & 1;'.

Best regards,
Nicolas Kaiser

#5 lore raw
Quoted 6 lines
Quoted 3 lines

Why? There are dozens of places in the kernel where this done, the code generated should be the same, and it's pretty obvious what is being done as it is.

Primarily because we were asked to avoid casts to bool even if they are safe.

[I have studied, meanwhile]

Actually the point of Rabin is, I think, that the patch is not needed. Our "bool" is the C99 "_Bool" type, for which the compiler automatically converts all non-0 assignments to 1. Even if storage is still one byte.

IIUC, the point of _Bool is allowing comparisong with "true", while in general non-0 is considered true if evaluated in a conditional but may be "!= 1" so "!= true" if compared explicitly.

You can compile a one-liner to check. I used a few more:

    _Bool i[10];

    int main(void)
    {
        i[0] = 1;
        i[1] = 10;
        return i[0];
    }

/alessandro