Extreme Efficiency for Odd/Even

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dcorbit@connx.com

    #31
    Re: Extreme Efficiency for Odd/Even


    Michal Nazarewicz wrote:
    "Clark S. Cox III" <clarkcox3@gmai l.comwrites:
    >
    Michal Nazarewicz wrote:
    Oh pleas... Even *if* macros are evil it doesn't mean *never* use
    them. They are good for small subroutines such as IS_ODD/IS_EVEN.
    Did I say to never use them? No. I did not. Please do not put words into
    my mouth.
    >
    Not directly but saying IS_ODD/IS_EVEN should be made into inline
    function sounds like "do note ever use macros 'cuz they're evil."
    Using macros as functions is a very bad idea. There is no type
    checking performed at all. Sometimes you will get an error message as
    a side effect (e.g. in the IS_ODD() macro a double or pointer would
    object to the & operator ... but you might also have a macro that
    increments which would work willy-nilly on pointers, doubles, ints,
    etc.). Macros can be redefined to something else.

    They have all sorts of evil side effects. Using macros as functions is
    the product of a lazy mind -- {ironically} hard at work doing as little
    thinking as possible.

    See (for instance):


    That having been said, I do it myself -- once in a while.

    Macros are useful -- for instance -- for conditional compiling or
    creating idempotent headers. But function macros are bug engines of
    the worst kind.
    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

    Comment

    • Michal Nazarewicz

      #32
      Re: Extreme Efficiency for Odd/Even

      "Clark S. Cox III" <clarkcox3@gmai l.comwrites:
      "Now that C has inline functions many of the arguments that
      C++-ers have used against function macros for years now apply to C. I
      would write those as inline functions and only switch to macros after
      profiling shows that they are indeed slower."
      Do you really think a sequence which involves two instructions (namely
      '%'/'&' and '=='/'!=') is worth that effort? The fact that macros are
      evil doesn't mean one should do ridiculous things.

      --
      Best regards, _ _
      .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
      ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
      ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

      Comment

      • Michal Nazarewicz

        #33
        Re: Extreme Efficiency for Odd/Even

        dcorbit@connx.c om writes:
        Using macros as functions is a very bad idea. There is no type
        checking performed at all. Sometimes you will get an error message as
        a side effect (e.g. in the IS_ODD() macro a double or pointer would
        object to the & operator ... but you might also have a macro that
        increments which would work willy-nilly on pointers, doubles, ints,
        etc.). Macros can be redefined to something else.
        I'm not saying macros ain't evil but C gives you a lot of
        opportunities to shoot yourself in your leg and still macros make live
        easier in some situations and if someone really wants IS_ODD/IS_EVEN
        then defining those as macros is probably one of such situations.

        --
        Best regards, _ _
        .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
        ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
        ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

        Comment

        • Chris Dollin

          #34
          Re: Extreme Efficiency for Odd/Even

          Michal Nazarewicz wrote:
          "Clark S. Cox III" <clarkcox3@gmai l.comwrites:
          >
          > "Now that C has inline functions many of the arguments that
          >C++-ers have used against function macros for years now apply to C. I
          >would write those as inline functions and only switch to macros after
          >profiling shows that they are indeed slower."
          >
          Do you really think a sequence which involves two instructions (namely
          '%'/'&' and '=='/'!=') is worth that effort?
          It's not significantly more effort to write an inline function than
          it is to write the corresponding function macro.

          (It's even less effort to write a non-inline function for the same
          thing.)

          --
          Chris "falling further in" Dollin
          The "good old days" used to be much better.

          Comment

          • dcorbit@connx.com

            #35
            Re: Extreme Efficiency for Odd/Even


            Chris Dollin wrote:
            Michal Nazarewicz wrote:
            >
            "Clark S. Cox III" <clarkcox3@gmai l.comwrites:
            "Now that C has inline functions many of the arguments that
            C++-ers have used against function macros for years now apply to C. I
            would write those as inline functions and only switch to macros after
            profiling shows that they are indeed slower."
            Do you really think a sequence which involves two instructions (namely
            '%'/'&' and '=='/'!=') is worth that effort?
            >
            It's not significantly more effort to write an inline function than
            it is to write the corresponding function macro.
            And that function will have type checking and also be immune to evil
            macro side effects.
            Using macros for functions is both lazy and stupid. Of course, I have
            admitted that I do it from time to time {what does that say about
            me...}
            (It's even less effort to write a non-inline function for the same
            thing.)
            Which most compilers will inline if you ask them to (and in a way that
            is smarter than you, for the most part).
            --
            Chris "falling further in" Dollin
            The "good old days" used to be much better.
            /* Some people might even be surprised by the output of this: */
            #define square(x) ((x)*(x))
            #define even(x) ((((x)>>1)<<1) == (x))

            #include <stdio.h>
            int main(void)
            {
            long long ll = 217;
            long l = 19;
            int i = 2;
            char c = 8;

            if (even(square(++ c)))
            puts("square of 8+1 is even");
            else
            puts("square of 8+1 is odd");

            if (even(square(++ i)))
            puts("square of 2+1 is even");
            else
            puts("square of 2+1 is odd");

            if (even(square(++ l)))
            puts("square of 19+1 is even");
            else
            puts("square of 19+1 is odd");

            if (even(square(++ ll)))
            puts("square of 217+1 is even");
            else
            puts("square of 217+1 is odd");

            ll = 217;
            l = 19;
            i = 2;
            c = 8;

            printf("218 squared = %.0f\n", (double) square(++ll));
            printf("20 squared = %.0f\n", (double) square(++l));
            printf("3 squared = %.0f\n", (double) square(++i));
            printf("9 squared = %.0f\n", (double) square(++c));

            ll = 217;
            l = 19;
            i = 2;
            c = 8;

            if (even(++ll))
            puts("218 is even");
            else
            puts("218 is odd");

            if (even(++ll))
            puts("20 is even");
            else
            puts("20 is odd");

            if (even(++ll))
            puts("3 is even");
            else
            puts("3 is odd");

            if (even(++ll))
            puts("9 is even");
            else
            puts("9 is odd");

            return 0;
            }
            /*
            dcorbit@DCORBIT 64 /c/tmp
            $ gcc -Wall -ansi -pedantic t.c
            t.c: In function 'main':
            t.c:7: warning: ISO C90 does not support 'long long'
            t.c:37: warning: operation on 'll' may be undefined
            t.c:38: warning: operation on 'l' may be undefined
            t.c:39: warning: operation on 'i' may be undefined
            t.c:40: warning: operation on 'c' may be undefined

            dcorbit@DCORBIT 64 /c/tmp
            $ ./a
            square of 8+1 is odd
            square of 2+1 is odd
            square of 19+1 is odd
            square of 217+1 is odd
            218 squared = 47961
            20 squared = 441
            3 squared = 16
            9 squared = 90
            218 is odd
            20 is odd
            3 is odd
            9 is odd

            dcorbit@DCORBIT 64 /c/tmp
            $
            */

            Of course, nasal demons are another possible output.

            Comment

            Working...