Macro Definitions and Usage

This is a discussion on Macro Definitions and Usage within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have defined a MACRO as follows: Code: #define GET_BITS(bits, num_bits, bit)\ if(num_bits > 0)\ {\ GET_BIT(bit);\ /* ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Question Macro Definitions and Usage

    Hello everyone,

    I have defined a MACRO as follows:

    Code:
    #define GET_BITS(bits, num_bits, bit)\
       if(num_bits > 0)\
       {\
          GET_BIT(bit);\    /* Gets one bit from a string of Bytes*/
          bits = bits | bit;\      
          while(0 < (num_bits-1))\
           {\
               bits = bits << 1;\
               GET_BIT(bit);\
               bits = bits | bit;\
               num_bits--;\
             }\
         }


    usage:

    Code:
    int var=0;
    
    XsGET_BITS(var, 3, 0);

    my C Compiler had no problem compiling just the macro definition. However when I try to use this macro in one of my functions I get a compiler error that states the following.

    "Invalid lvalue in assignment"

    Can anyone point me in the right direction???

    Thanks!
    Last edited by Dave_Sinkula; 08-17-2006 at 09:08 PM. Reason: Couldn't stand it anymore without [code][/code] tags -- learn to use them yourself.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The macro uses num_bits as a variable but you give it a literal value.
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    More non-use of code tags - when will these people ever learn to read past their own question???
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    First off, this looks like it should definitely be a function instead of a macro.

    Secondly, you should use parentheses around bits, num_bits, and bit inside the macro.

    Thirdly, what does the GET_BIT macro look like?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    The reason behind making this a Macro instead of a Function is for speed purposes.
    Constantly making a function call to this routine would seriously affect my performance.

    Code:
    #define GET_BIT(ret_bit) \
           if(bit_counter == 32) \
           { \
              bit_counter = 0; \
              source_ptr++; \
            }
             bit_counter++;\
             ret_bit = bit_holder & 0x00000001;\
             bit_holder >>= 1;
    bit_holder,
    bit_counter,
    source_ptr are ALL globals variables
    Last edited by Dave_Sinkula; 08-17-2006 at 09:09 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by edunia11
    The reason behind making this a Macro instead of a Function is for speed purposes.
    Constantly making a function call to this routine would seriously affect my performance.
    Unless you are on an OLD OLD OLD OLD OLD OLD machine (like 8088) it would be very difficult to see a performace loss on loading a function.

    If you look at the ammount of codepage required to constantly expand this code in-line and compare that to making the function call I believe you'd see it is almost nill. If not faster.
    Last edited by Kennedy; 08-17-2006 at 12:48 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > The reason behind making this a Macro instead of a Function is for speed purposes
    If your macro was a single statement, you might have a point.
    But it's a nested macro with a while loop in it, so I'd say you're blowing all the issues out of proportion.

    Write it first, get it working and tested with real world data.
    Then profile it to find out where the real hot spots are, not the ones you imagine.
    Then make careful changes which you can prove produce the same results AND give a useful performance improvement.

    Until then, you're just wasting time bastardising the code structure just to satisfy your preconceived notions of performance.

    Like someone said, it doesn't matter how much you write your bubble sort in assembler, it will still suck.

    Besides, there are more efficient ways of extracting 'n' consecutive bits from a bit-stream than one at a time. With a bit of though, you should be able to split 'n' into 'x' + 'y' bits, and with careful use of masks and shifts extract all 'x' bits from word1 and all 'y' bits from word2 and combine both results with just a few & >> << and | operations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    >With a bit of though, you should be able to split 'n' into 'x' + 'y' bits, and with careful use of masks and shifts extract all 'x' bits from word1 and all 'y' bits from word2 and combine both results with just a few & >> << and | operations.
    Shame on you. . . you give too much basic-level information .

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    The code HAS been tested using routines and not MACROS, I'm just trying to get a little bit of an edge using macros, since my performance is so awful.

    Thanks for all of your suggestions.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by edunia11
    The code HAS been tested using routines and not MACROS, I'm just trying to get a little bit of an edge using macros, since my performance is so awful.

    Thanks for all of your suggestions.
    Like Salem said, using a better (read: smarter) algorithm would be a lot better than macro-izing the routine.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm just trying to get a little bit of an edge using macros, since my performance is so awful.
    If your performance is that bad, I seriously doubt the difference is in function call overhead.
    My best code is written with the delete key.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > The code HAS been tested using routines and not MACROS
    To paraphrase Raiders of the Lost Ark, "you're digging in the wrong place!"
    Which is generally what happens when you use the wrong measuring stick.

    > I'm just trying to get a little bit of an edge using macros, since my performance is so awful.
    So you're looking for a 10x performance?
    That comes from choosing a better algorithm, not from implementing MACRO hell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sorry, I can't help but mention this and this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21