bool variable.(a non-standard question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shaanxxx

    bool variable.(a non-standard question)

    I have code which says

    #define MYBOOL int

    This code is very old. people who have written is not avaible. I was
    thinking what could be reason.
    1) bool datatype was not available that time (10 years back: correct me
    if i am wrong)
    2) int was word aligned (16bit and 32bit). Writter of above code doesnt
    want to give choice to compiler.
    if we say bool , compiler can represent this on his way (again,correct
    me if i am wrong)
    This much i can think of it.

    even in many books, i have seen author using int as a flag. example
    Richard Stevens, "advance programming in unix environment" . and this
    book uses 'restrict' keyword.

    Now, i have to change it for 64bit arch. In my system, performance is
    more important thn anything else.
    I thought of changing it to long on 64arch.
    #define MYBOOL long

    Any comment on this will be appreciated.

  • goose

    #2
    Re: bool variable.(a non-standard question)

    shaanxxx wrote:
    <snipped.
    Now, i have to change it for 64bit arch. In my system, performance is
    more important thn anything else.
    I thought of changing it to long on 64arch.
    #define MYBOOL long
    >
    Any comment on this will be appreciated.
    >
    Don't change it yet; run it first and change it
    only if there actually is a performance hit.

    --
    goose
    Have I offended you? Send flames to root@localhost
    real email: lelanthran at gmail dot com
    website : www.lelanthran.com

    Comment

    • Spiros Bousbouras

      #3
      Re: bool variable.(a non-standard question)

      shaanxxx wrote:
      I have code which says
      >
      #define MYBOOL int
      >
      This code is very old. people who have written is not avaible. I was
      thinking what could be reason.
      1) bool datatype was not available that time (10 years back: correct me
      if i am wrong)
      2) int was word aligned (16bit and 32bit). Writter of above code doesnt
      want to give choice to compiler.
      if we say bool , compiler can represent this on his way (again,correct
      me if i am wrong)
      This much i can think of it.
      >
      even in many books, i have seen author using int as a flag. example
      Richard Stevens, "advance programming in unix environment" . and this
      book uses 'restrict' keyword.
      >
      Now, i have to change it for 64bit arch. In my system, performance is
      more important thn anything else.
      I thought of changing it to long on 64arch.
      #define MYBOOL long
      So you want to declare a variable which will only
      take the values 0 and 1 and you want its type to
      be such that access will be as fast as possible , is
      that right ?

      Assuming I got it right then I would have to say that
      it's out of topic. Ultimately the answer depends on
      the processor you are using and perhaps also the
      operating system. Your compiler presumably knows
      about these things so read your compiler's documentation
      and see if it says anything on how you should declare
      variables in order to get the fastest possible access.

      Comment

      • Richard Heathfield

        #4
        Re: bool variable.(a non-standard question)

        Spiros Bousbouras said:

        <snip>
        >
        So you want to declare a variable which will only
        take the values 0 and 1 and you want its type to
        be such that access will be as fast as possible , is
        that right ?
        It seems likely. Of course, apart from a bitfield (of type unsigned int and
        width 1), there is no C type that can /only/ take a value from the range
        [0, 1], and a bitfield would be unlikely to win any awards for speed.

        If speed is at a premium, he should choose int:

        typedef int bool;

        But if space is at a premium, he can decide how many bools he wants:

        #define NUM_FLAGS 123 /* or whatever */

        Then he can define an array of unsigned char:

        #include <limits.h>

        unsigned char flag[(NUM_FLAGS + CHAR_BIT - 1) / CHAR_BIT] = {0};

        and then use bit-twiddling macros (e.g. see snippets.org) to get at
        individual bits.

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • Ben Pfaff

          #5
          Re: bool variable.(a non-standard question)

          "shaanxxx" <shaanxxx@yahoo .comwrites:
          I have code which says
          >
          #define MYBOOL int
          [...]
          Now, i have to change it for 64bit arch. In my system, performance is
          more important thn anything else.
          I thought of changing it to long on 64arch.
          #define MYBOOL long
          If you are using a modern implementation that has <stdint.h>, I
          would suggest using int_fast8_t, which designates the fastest
          signed integer type with at least 8 bits.
          --
          "I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

          Comment

          • shaanxxx

            #6
            Re: bool variable.(a non-standard question)




            Richard Heathfield wrote:
            Spiros Bousbouras said:
            >
            <snip>

            So you want to declare a variable which will only
            take the values 0 and 1 and you want its type to
            be such that access will be as fast as possible , is
            that right ?
            >
            It seems likely. Of course, apart from a bitfield (of type unsigned int and
            width 1), there is no C type that can /only/ take a value from the range
            [0, 1], and a bitfield would be unlikely to win any awards for speed.
            >
            If speed is at a premium, he should choose int:
            >
            typedef int bool;
            This is also true for 64bit arch ?

            Comment

            • CBFalconer

              #7
              Re: bool variable.(a non-standard question)

              shaanxxx wrote:
              >
              I have code which says
              >
              #define MYBOOL int
              >
              This code is very old. people who have written is not avaible. I
              was thinking what could be reason.
              1) bool datatype was not available that time (10 years back:
              correct me if i am wrong)
              2) int was word aligned (16bit and 32bit). Writter of above code
              doesnt want to give choice to compiler.
              if we say bool , compiler can represent this on his way (again,
              correct me if i am wrong)
              C still has no bool type, unless you #include <stdbool.hin C99
              systems. After which true and false will be macros that expand to
              integers 1 and 0 respectively, and bool will expand to _Bool (which
              is system defined, usually as an int).

              However, since the dawn of time, C has defined logical expressions
              (e.g. (x < y)) as returning either 0 or 1. This is why !!x can be
              used to force the value to those, and why we can do arithmetical
              tricks with booleans.

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

              Comment

              • Richard Heathfield

                #8
                Re: bool variable.(a non-standard question)

                shaanxxx said:
                Richard Heathfield wrote:
                <snip>
                >>
                >If speed is at a premium, he should choose int:
                >>
                >typedef int bool;
                >
                This is also true for 64bit arch ?
                It has always been the intent of the powers-that-C that implementors should
                choose a size of int that reflects the most natural object size for the
                machine, and the most natural size is pretty darned likely also to be the
                fastest size.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at above domain (but drop the www, obviously)

                Comment

                • Spiros Bousbouras

                  #9
                  Re: bool variable.(a non-standard question)

                  Richard Heathfield wrote:
                  Spiros Bousbouras said:
                  >
                  <snip>

                  So you want to declare a variable which will only
                  take the values 0 and 1 and you want its type to
                  be such that access will be as fast as possible , is
                  that right ?
                  >
                  It seems likely. Of course, apart from a bitfield (of type unsigned int and
                  width 1), there is no C type that can /only/ take a value from the range
                  [0, 1], and a bitfield would be unlikely to win any awards for speed.
                  >
                  If speed is at a premium, he should choose int:
                  >
                  typedef int bool;
                  >From his opening post I gather that he thinks that
                  a 64 bit variable may be aligned in such a way as to
                  allow faster access than int. (I'm assuming that int
                  is 32 bits on his platform.)

                  Comment

                  • Richard Heathfield

                    #10
                    Re: bool variable.(a non-standard question)

                    Spiros Bousbouras said:

                    <snip>
                    >
                    (I'm assuming that int is 32 bits on his platform.)
                    Why?

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at above domain (but drop the www, obviously)

                    Comment

                    • Spiros Bousbouras

                      #11
                      Re: bool variable.(a non-standard question)

                      Richard Heathfield wrote:
                      shaanxxx said:
                      >
                      Richard Heathfield wrote:
                      <snip>
                      >
                      If speed is at a premium, he should choose int:
                      >
                      typedef int bool;
                      This is also true for 64bit arch ?
                      >
                      It has always been the intent of the powers-that-C that implementors should
                      choose a size of int that reflects the most natural object size for the
                      machine, and the most natural size is pretty darned likely also to be the
                      fastest size.
                      Does the standard actually say that somewhere ?

                      Comment

                      • Spiros Bousbouras

                        #12
                        Re: bool variable.(a non-standard question)

                        Richard Heathfield wrote:
                        Spiros Bousbouras said:
                        >
                        <snip>

                        (I'm assuming that int is 32 bits on his platform.)
                        >
                        Why?
                        Because he believes that long int might make a
                        difference in speed so int can't have size 64. Plus
                        32 bits for int seems a reasonable choice for a 64
                        bits machine.

                        Comment

                        • Richard Heathfield

                          #13
                          Re: bool variable.(a non-standard question)

                          Spiros Bousbouras said:
                          Richard Heathfield wrote:
                          >
                          >shaanxxx said:
                          >>
                          Richard Heathfield wrote:
                          ><snip>
                          >>
                          >If speed is at a premium, he should choose int:
                          >>
                          >typedef int bool;
                          >
                          This is also true for 64bit arch ?
                          >>
                          >It has always been the intent of the powers-that-C that implementors
                          >should choose a size of int that reflects the most natural object size
                          >for the machine, and the most natural size is pretty darned likely also
                          >to be the fastest size.
                          >
                          Does the standard actually say that somewhere ?
                          3.1.2.5 has this to say: "A ``plain'' int object has the natural size
                          suggested by the architecture of the execution environment (large enough to
                          contain any value in the range INT_MIN to INT_MAX as defined in the header
                          <limits.h)."

                          And it's normative text, so yes, this is mandatory for a conforming
                          implementation.

                          --
                          Richard Heathfield
                          "Usenet is a strange place" - dmr 29/7/1999

                          email: rjh at above domain (but drop the www, obviously)

                          Comment

                          • Keith Thompson

                            #14
                            Re: bool variable.(a non-standard question)

                            Ben Pfaff <blp@cs.stanfor d.eduwrites:
                            "shaanxxx" <shaanxxx@yahoo .comwrites:
                            >I have code which says
                            >>
                            >#define MYBOOL int
                            >
                            [...]
                            >
                            >Now, i have to change it for 64bit arch. In my system, performance is
                            >more important thn anything else.
                            >I thought of changing it to long on 64arch.
                            >#define MYBOOL long
                            >
                            If you are using a modern implementation that has <stdint.h>, I
                            would suggest using int_fast8_t, which designates the fastest
                            signed integer type with at least 8 bits.
                            <stdint.his new in C99. So is <bool.h>, which would be a better
                            choice.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: bool variable.(a non-standard question)

                              Keith Thompson <kst-u@mib.orgwrites :
                              Ben Pfaff <blp@cs.stanfor d.eduwrites:
                              >"shaanxxx" <shaanxxx@yahoo .comwrites:
                              >>I have code which says
                              >>>
                              >>#define MYBOOL int
                              >>
                              >[...]
                              >>
                              >>Now, i have to change it for 64bit arch. In my system, performance is
                              >>more important thn anything else.
                              >>I thought of changing it to long on 64arch.
                              >>#define MYBOOL long
                              >>
                              >If you are using a modern implementation that has <stdint.h>, I
                              >would suggest using int_fast8_t, which designates the fastest
                              >signed integer type with at least 8 bits.
                              >
                              <stdint.his new in C99.
                              It is new in C99, but many implementations that do not support
                              all of C99 do include a conforming <stdint.h(e.g . GCC).
                              So is <bool.h>, which would be a better choice.
                              There is no standard <bool.h>. You must mean <stdbool.h>. And I
                              think that bool or _Bool is less likely to optimize strictly for
                              speed than int_fast8_t.
                              --
                              "IMO, Perl is an excellent language to break your teeth on"
                              --Micah Cowan

                              Comment

                              Working...