[U-Boot] [PATCH 2/2] musb: sunxi: Implement dfu_usb_get_reset()

Siarhei Siamashka siarhei.siamashka at gmail.com
Sun Oct 25 21:48:42 CET 2015


On Sun, 25 Oct 2015 19:24:04 +0000
Ian Campbell <ijc+uboot at hellion.org.uk> wrote:

> On Sun, 2015-10-25 at 16:46 +0200, Siarhei Siamashka wrote:
> > replace "#include <stdbool.h>" with "typedef int bool;" and try it
> > again.
> 
> So don't do that then.

Yes, I'm not going to do this. It was just an artificial example,
demonstrating how things work. I'm only a little bit worried about
a potential rogue typedef unexpectedly coming from one of the
include files one day.

> In u-boot the "bool" type comes from stdbool.h and it is therefore
> completely reasonable to assume that bool in u-boot has the properties
> which this implies.

Right, except that there are still a few bool related typedefs
in the code:
    https://github.com/u-boot/u-boot/blob/v2015.10/lib/lzma/Types.h#L83
    https://github.com/u-boot/u-boot/blob/v2015.10/lib/bzip2/bzlib_private.h#L85

Yes, they come from third-party libraries and don't really redefine
bool or _Bool. But the C language did not always have a native bool
data type. And historically it had been a common practice to typedef
bool to some integer type. One should not be very much surprised upon
encountering such code in the wild even now.

Regarding the code efficiency. The following example:

    #include <stdbool.h>

    bool foo(int x)
    {
        return x & 4;
    }

    bool bar(int x)
    {
        return !!(x & 4);
    }

Compiles into the following code on ARM:

    00000000 <foo>:
       0:	e7e00150 	ubfx	r0, r0, #2, #1
       4:	e12fff1e 	bx	lr

    00000008 <bar>:
       8:	e7e00150 	ubfx	r0, r0, #2, #1
       c:	e12fff1e 	bx	lr

Basically, there is exactly no difference. And removing "!!" only
has a cosmetic value for the code that uses the bool definition
from <stdbool.h>.

As I already said, I'm perfectly fine with either of these variants :-)
The purists are welcome to start a crusade eliminating all of the uses
of the "!!" construct in the U-Boot code. Probably starting with the
other dfu_usb_get_reset() implementations for the sake of consistency:
    https://github.com/u-boot/u-boot/blob/v2015.10/drivers/usb/gadget/s3c_udc_otg.c#L151
    https://github.com/u-boot/u-boot/blob/v2015.10/drivers/usb/gadget/ci_udc.c#L1062

-- 
Best regards,
Siarhei Siamashka


More information about the U-Boot mailing list