pll: who uses _Bool

Discussion in 'C Programming' started by Szabolcs Nagy, Oct 27, 2007.

  1. who has used _Bool? (or included stdbool.h for bool, true, false?)

    (i'm considering using it in a library for home projects)
    Szabolcs Nagy, Oct 27, 2007
    #1
    1. Advertising

  2. Re: who uses _Bool

    "Szabolcs Nagy" <> wrote in message
    > who has used _Bool? (or included stdbool.h for bool, true, false?)
    >
    > (i'm considering using it in a library for home projects)
    >

    Here's what Dennis Ritchie has to say about it

    "Of the new things, restricted pointers probably are a help; variadic macros
    and bool are just adornment. I've heard the argument for complex numbers for
    a long time, and maybe it was inevitable, but it does somewhat increase the
    cross-product of the type rules and inflate the library. One issue the
    question didn't mention is the introduction of the "long long" type and its
    implications, which is one of the more contentious issues in discussion
    groups about the language -- and it also makes the type-promotion rules much
    more complicated. But of course, 64-bit machines and storage are here, and
    it had to be faced."
    http://www.itworld.com/Comp/3380/lw-12-ritchie/

    use bool not _Bool, though. Bool breaks libraries.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Oct 27, 2007
    #2
    1. Advertising

  3. Szabolcs  Nagy

    Tor Rustad Guest

    Szabolcs Nagy wrote:
    > who has used _Bool? (or included stdbool.h for bool, true, false?)
    >
    > (i'm considering using it in a library for home projects)


    Since <stdbool.h> isn't available on all the relevant compilers, I stil
    roll my own boolean definitions. I should perhaps switch to lower-case
    now, using something ala:

    #ifdef __STDC_IEC_559__
    #include <stdbool.h>
    #else
    typedef enum {false, true} bool;
    #endif

    --
    Tor < | tr i-za-h a-z>

    My C page: http://www.pg2.moo.no/C/index.html
    -include Win32 build of splint 3.1.2
    Tor Rustad, Oct 28, 2007
    #3
  4. Szabolcs  Nagy

    Ark Khasin Guest

    Szabolcs Nagy wrote:
    > who has used _Bool? (or included stdbool.h for bool, true, false?)
    >
    > (i'm considering using it in a library for home projects)
    >

    IMHO, bool (or _Bool) goes hand in hand with static inline (and an
    optimizing compiler). Otherwise, you will immediately pay performance
    and code size penalties for syntactic sugar, and if you don't have to
    care, C might not be the right language for your project.
    I mean, types aside, semantic Booleans are not free:
    int cmpneq(int a, int b) {return a!=b;}
    is less efficient than
    int cmpneq(int a, int b) {return a-b;}

    --
    Ark
    Ark Khasin, Oct 28, 2007
    #4
  5. Tor Rustad <> writes:
    > Szabolcs Nagy wrote:
    >> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >> (i'm considering using it in a library for home projects)

    >
    > Since <stdbool.h> isn't available on all the relevant compilers, I
    > stil roll my own boolean definitions. I should perhaps switch to
    > lower-case now, using something ala:
    >
    > #ifdef __STDC_IEC_559__
    > #include <stdbool.h>
    > #else
    > typedef enum {false, true} bool;
    > #endif


    __STDC_IEC_559__ tells you whether the implementation conforms to the
    IEC 60559 floating-point standard.

    What you probably want is

    #if __STDC_VERSION__ >= 199901L
    #include <stdbool.h>
    #else
    typedef enum {false, true} bool;
    #endif

    But note that an implementation might provide <stdbool.h> without
    fully conforming to C99 or having __STDC_VERSION__ >= 199901L. An
    alternative is to use mechanisms outside the language to test during
    configuration whether <stdbool.h> exists. (But occasionally using the
    enum when <stdbool.h> is available isn't a big problem.)

    You also have to be a bit careful with the code that uses ``bool'';
    the enum type doesn't fully capture the semantics of C99's _Bool.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 28, 2007
    #5
  6. Ark Khasin <> writes:
    > Szabolcs Nagy wrote:
    >> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >> (i'm considering using it in a library for home projects)
    >>

    > IMHO, bool (or _Bool) goes hand in hand with static inline (and an
    > optimizing compiler). Otherwise, you will immediately pay performance
    > and code size penalties for syntactic sugar, and if you don't have to
    > care, C might not be the right language for your project.
    > I mean, types aside, semantic Booleans are not free:
    > int cmpneq(int a, int b) {return a!=b;}
    > is less efficient than
    > int cmpneq(int a, int b) {return a-b;}


    The first has the considerable advantage of being correct. The
    ``a-b'' version exhibits undefined behavior on overflow. Consider
    ``cmpneq(INT_MIN, INT_MAX)''.

    And how do you know that ``a!=b'' is less efficient than ``a-b''?
    Have you measured it? Depending on the architecture, it's possible
    that each could compile to a single instruction.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 28, 2007
    #6
  7. Szabolcs  Nagy

    Ben Pfaff Guest

    Tor Rustad <> writes:
    > Since <stdbool.h> isn't available on all the relevant compilers, I
    > stil roll my own boolean definitions. I should perhaps switch to
    > lower-case now, using something ala:
    >
    > #ifdef __STDC_IEC_559__
    > #include <stdbool.h>
    > #else
    > typedef enum {false, true} bool;
    > #endif


    For what it's worth, the standard says that true, false, and bool
    are macros, not enumerations or typedef names. (Not that this
    will often make a difference in practice.)
    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein
    Ben Pfaff, Oct 28, 2007
    #7
  8. Szabolcs  Nagy

    CBFalconer Guest

    Tor Rustad wrote:
    > Szabolcs Nagy wrote:
    >
    >> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >> (i'm considering using it in a library for home projects)

    >
    > Since <stdbool.h> isn't available on all the relevant compilers, I
    > stil roll my own boolean definitions. I should perhaps switch to
    > lower-case now, using something ala:
    >
    > #ifdef __STDC_IEC_559__
    > #include <stdbool.h>
    > #else
    > typedef enum {false, true} bool;
    > #endif


    I suggest that whenever you do so, you duplicate exactly the
    statements (or a subset of them) that you find in stdbool.h. Read
    the c standard to discover what they are. Of course _Bool will not
    exist. That way you won't run into trouble when the system is
    compiled under C99 up.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 28, 2007
    #8
  9. Szabolcs  Nagy

    Ark Khasin Guest

    Keith Thompson wrote:
    > Ark Khasin <> writes:
    >> Szabolcs Nagy wrote:
    >>> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >>> (i'm considering using it in a library for home projects)
    >>>

    >> IMHO, bool (or _Bool) goes hand in hand with static inline (and an
    >> optimizing compiler). Otherwise, you will immediately pay performance
    >> and code size penalties for syntactic sugar, and if you don't have to
    >> care, C might not be the right language for your project.
    >> I mean, types aside, semantic Booleans are not free:
    >> int cmpneq(int a, int b) {return a!=b;}
    >> is less efficient than
    >> int cmpneq(int a, int b) {return a-b;}

    >
    > The first has the considerable advantage of being correct. The
    > ``a-b'' version exhibits undefined behavior on overflow. Consider
    > ``cmpneq(INT_MIN, INT_MAX)''.

    Sorry. I ought to be more careful. Here is a repaired version:
    int cmpneq(int a, int b) {return a^b;}
    As a practical matter though, I've always seen a-b+b yielding a and thus
    a-b didn't evaluate to 0. I know it's a meek defense in this ng. :)
    >
    > And how do you know that ``a!=b'' is less efficient than ``a-b''?
    > Have you measured it? Depending on the architecture, it's possible
    > that each could compile to a single instruction.
    >

    I may have limited exposure, but I've never seen an instruction set that
    would invent a 1 or 0 out of non-equal or equal a and b in a single
    instruction. Every instruction set I've seen can invent a non-0 or a 0
    out of non-equal or equal a and b in a single instruction [common
    disclaimers on sizeof(int) vs. register size apply].
    It's likely that a Boolean version can _never_ be more efficient than
    the integer one, and in most cases is less efficient.

    --
    Ark
    Ark Khasin, Oct 28, 2007
    #9
  10. Szabolcs  Nagy

    Chris Torek Guest

    In article <kbWUi.601$mv.257@trndny08>
    Ark Khasin <> wrote:
    >I may have limited exposure, but I've never seen an instruction set that
    >would invent a 1 or 0 out of non-equal or equal a and b in a single
    >instruction.


    MIPS architecture:

    setne t0,a,b /* assuming a and b are in registers */

    (In fact, if you want to *test* whether a != b, you have to use a
    setne instruction to set a register to 1 or 0, followed by a branch
    on register value instruction. I am not sure whether t0 is a
    conventional target register for this; the "t", "v", and "a"
    registers are the most likely candidates though.)

    >Every instruction set I've seen can invent a non-0 or a 0
    >out of non-equal or equal a and b in a single instruction [common
    >disclaimers on sizeof(int) vs. register size apply].


    Yes, most can do this with subtract or exclusive-or (or both),
    including MIPS. However, care must be used with subtract, lest
    one get an integer-overflow exception (on MIPS this means using
    the "unsigned" instructions, on x86 it means using xor or making
    sure you never set the trap bit, on SPARC it means not adding a
    "trap on condition codes" instruction, etc.).

    >It's likely that a Boolean version can _never_ be more efficient than
    >the integer one, and in most cases is less efficient.


    This is why compilers optimize. :)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Oct 28, 2007
    #10
  11. Szabolcs  Nagy

    Ian Collins Guest

    Ark Khasin wrote:
    > It's likely that a Boolean version can _never_ be more efficient than
    > the integer one, and in most cases is less efficient.
    >

    In the cases where any difference in efficiency would be noticed, the
    function is probably trivial enough to be inlined away.

    --
    Ian Collins.
    Ian Collins, Oct 28, 2007
    #11
  12. "Tor Rustad" <> wrote in message
    > Szabolcs Nagy wrote:
    >> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >>
    >> (i'm considering using it in a library for home projects)

    >
    > Since <stdbool.h> isn't available on all the relevant compilers, I stil
    > roll my own boolean definitions. I should perhaps switch to lower-case
    > now, using something ala:
    >
    > #ifdef __STDC_IEC_559__
    > #include <stdbool.h>
    > #else
    > typedef enum {false, true} bool;
    > #endif
    >

    This is the same mistake as

    #define start {

    but a little bit more subtle.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Oct 28, 2007
    #12
  13. Szabolcs  Nagy

    cr88192 Guest

    Re: who uses _Bool

    "Szabolcs Nagy" <> wrote in message
    news:...
    > who has used _Bool? (or included stdbool.h for bool, true, false?)
    >
    > (i'm considering using it in a library for home projects)
    >


    personally, I don't see any real advantage of _Bool, apart from the vague
    possibility of it serving as a programmer hint...
    and, it is ugly...


    so, C99:
    some features are useful (complex and long long, for example);
    some features just add complexity (dynamic arrays, ...);
    some features are, IMO, frivolous...

    and, worse in the case of bool: it is not present...
    the version of mingw I am using lacks stdbool.h...
    and, what is more, so does cygwin...

    so, if one can convince themselves that it is useful, they then have to find
    a compiler that has it...

    or something...


    now, that is not to stop some people from being like:
    #ifndef bool
    #define bool int
    #endif
    #ifndef true
    #define true 1
    #endif
    #ifndef false
    #define false 0
    #endif

    where I have seen similar in at least a few projects...
    cr88192, Oct 28, 2007
    #13
  14. Ark Khasin <> writes:

    > Keith Thompson wrote:
    >> Ark Khasin <> writes:
    >>> Szabolcs Nagy wrote:
    >>>> who has used _Bool? (or included stdbool.h for bool, true, false?)
    >>>> (i'm considering using it in a library for home projects)
    >>>>
    >>> IMHO, bool (or _Bool) goes hand in hand with static inline (and an
    >>> optimizing compiler). Otherwise, you will immediately pay performance
    >>> and code size penalties for syntactic sugar, and if you don't have to
    >>> care, C might not be the right language for your project.
    >>> I mean, types aside, semantic Booleans are not free:
    >>> int cmpneq(int a, int b) {return a!=b;}
    >>> is less efficient than
    >>> int cmpneq(int a, int b) {return a-b;}

    >>
    >> The first has the considerable advantage of being correct. The
    >> ``a-b'' version exhibits undefined behavior on overflow. Consider
    >> ``cmpneq(INT_MIN, INT_MAX)''.

    > Sorry. I ought to be more careful. Here is a repaired version:
    > int cmpneq(int a, int b) {return a^b;}


    There are, I think, some corner cases that make this non-portable.
    Even on two's compliment implementations, ^ can result in a trap
    representation. My non-authoritative PDF has some change bars here
    (6.2.6.2) so this may be a recent clarification.

    --
    Ben.
    Ben Bacarisse, Oct 28, 2007
    #14
  15. Szabolcs  Nagy

    Army1987 Guest

    On Sat, 27 Oct 2007 22:19:35 -0700, Ben Pfaff wrote:

    > Tor Rustad <> writes:
    >> Since <stdbool.h> isn't available on all the relevant compilers, I
    >> stil roll my own boolean definitions. I should perhaps switch to
    >> lower-case now, using something ala:
    >>
    >> #ifdef __STDC_IEC_559__
    >> #include <stdbool.h>
    >> #else
    >> typedef enum {false, true} bool;
    >> #endif

    >
    > For what it's worth, the standard says that true, false, and bool
    > are macros, not enumerations or typedef names. (Not that this
    > will often make a difference in practice.)

    Well, #if true will do the wrong thing... But I can't think of
    any other difference. But using macros, the most reasonable choice
    for bool in C89 is int, whereas with an enum an implementation is
    allowed to choose a smaller type if it thinks it is equally (or
    more) efficient, in particular I'd expect sizeof (_Bool) to equal
    sizeof (enum {false, true}) on most of implementations having
    _Bool.

    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 28, 2007
    #15
  16. Szabolcs  Nagy

    Eric Sosman Guest

    Ben Pfaff wrote:
    > Tor Rustad <> writes:
    >> Since <stdbool.h> isn't available on all the relevant compilers, I
    >> stil roll my own boolean definitions. I should perhaps switch to
    >> lower-case now, using something ala:
    >>
    >> #ifdef __STDC_IEC_559__
    >> #include <stdbool.h>
    >> #else
    >> typedef enum {false, true} bool;
    >> #endif

    >
    > For what it's worth, the standard says that true, false, and bool
    > are macros, not enumerations or typedef names. (Not that this
    > will often make a difference in practice.)


    Notice that the definition above is not quite equivalent
    to C99's bool. Here's a way to see the difference:

    bool b;
    b = 42;
    if (b == 1)
    puts ("C99-style bool");
    else
    puts ("Trickery is rampant");

    That is, converting *any* non-zero value to C99 bool produces
    the value `(bool)1'.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 28, 2007
    #16
  17. "Eric Sosman" <> a écrit dans le message de
    news: ...
    > Ben Pfaff wrote:
    >> Tor Rustad <> writes:
    >>> Since <stdbool.h> isn't available on all the relevant compilers, I
    >>> stil roll my own boolean definitions. I should perhaps switch to
    >>> lower-case now, using something ala:
    >>>
    >>> #ifdef __STDC_IEC_559__
    >>> #include <stdbool.h>
    >>> #else
    >>> typedef enum {false, true} bool;
    >>> #endif

    >>
    >> For what it's worth, the standard says that true, false, and bool
    >> are macros, not enumerations or typedef names. (Not that this
    >> will often make a difference in practice.)

    >
    > Notice that the definition above is not quite equivalent
    > to C99's bool. Here's a way to see the difference:
    >
    > bool b;
    > b = 42;
    > if (b == 1)
    > puts ("C99-style bool");
    > else
    > puts ("Trickery is rampant");
    >
    > That is, converting *any* non-zero value to C99 bool produces
    > the value `(bool)1'.


    Does it convert NaNs to true ?

    --
    Chqrlie.
    Charlie Gordon, Oct 28, 2007
    #17
  18. Szabolcs  Nagy

    Eric Sosman Guest

    Charlie Gordon wrote:
    > "Eric Sosman" <> a écrit dans le message de
    > news: ...
    >> Ben Pfaff wrote:
    >>> Tor Rustad <> writes:
    >>>> Since <stdbool.h> isn't available on all the relevant compilers, I
    >>>> stil roll my own boolean definitions. I should perhaps switch to
    >>>> lower-case now, using something ala:
    >>>>
    >>>> #ifdef __STDC_IEC_559__
    >>>> #include <stdbool.h>
    >>>> #else
    >>>> typedef enum {false, true} bool;
    >>>> #endif
    >>> For what it's worth, the standard says that true, false, and bool
    >>> are macros, not enumerations or typedef names. (Not that this
    >>> will often make a difference in practice.)

    >> Notice that the definition above is not quite equivalent
    >> to C99's bool. Here's a way to see the difference:
    >>
    >> bool b;
    >> b = 42;
    >> if (b == 1)
    >> puts ("C99-style bool");
    >> else
    >> puts ("Trickery is rampant");
    >>
    >> That is, converting *any* non-zero value to C99 bool produces
    >> the value `(bool)1'.

    >
    > Does it convert NaNs to true ?


    Yes. 6.3.1.2:

    "When any scalar value is converted to _Bool,
    the result is 0 if the value compares equal
    to 0; otherwise, the result is 1."

    Since NaN does not compare equal to 0, the conversion yields 1.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 28, 2007
    #18
  19. Ark Khasin <> writes:
    > Keith Thompson wrote:
    >> Ark Khasin <> writes:

    [...]
    >>> I mean, types aside, semantic Booleans are not free:
    >>> int cmpneq(int a, int b) {return a!=b;}
    >>> is less efficient than
    >>> int cmpneq(int a, int b) {return a-b;}

    >> The first has the considerable advantage of being correct. The
    >> ``a-b'' version exhibits undefined behavior on overflow. Consider
    >> ``cmpneq(INT_MIN, INT_MAX)''.

    > Sorry. I ought to be more careful. Here is a repaired version:
    > int cmpneq(int a, int b) {return a^b;}

    [...]

    Bitwise operations on signed types always make me nervous.

    In this case, for an implementation that uses ones'-complement or
    sign-and-magnitude, a^b will fail if a is +0 and b is -0.

    The language has a built-in != operator. Just use it. Don't
    micro-optimize unless it's absolutely necessary.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 28, 2007
    #19
  20. Re: who uses _Bool

    "cr88192" <> writes:
    [...]
    > and, worse in the case of bool: it is not present...
    > the version of mingw I am using lacks stdbool.h...
    > and, what is more, so does cygwin...

    [...]

    Cygwin does provide <stdbool.h>. Perhaps you just need to update your
    system.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 28, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spiderman_astig
    Replies:
    3
    Views:
    9,575
    Ray Andraka
    Feb 6, 2004
  2. Peter

    pll frequency multiplier

    Peter, Feb 6, 2004, in forum: VHDL
    Replies:
    1
    Views:
    4,213
    Ray Andraka
    Feb 6, 2004
  3. ALuPin

    PLL phase after compensation

    ALuPin, Jul 19, 2004, in forum: VHDL
    Replies:
    2
    Views:
    1,764
    ALuPin
    Jul 20, 2004
  4. Philipp Klaus Krause

    _Bool bitfields and cast / assignment

    Philipp Klaus Krause, Jun 14, 2010, in forum: C Programming
    Replies:
    5
    Views:
    598
    Tim Rentsch
    Jun 20, 2010
  5. sandeep

    Half hearted _Bool

    sandeep, Jun 25, 2010, in forum: C Programming
    Replies:
    47
    Views:
    2,126
    Richard Bos
    Jul 7, 2010
Loading...

Share This Page