Thread: Macro type safety

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    Macro type safety

    I have this macro which, obviously assumes that the provided type is the correct and expected type.

    Code:
    #define ENCODE_EVENT(devid, info, addr, rshift) do { \ 
           *(uint16_t *)&evt[0] = devid; \
            *(uint8_t *)&evt[3]  = info;  \
            *(uint64_t *)&evt[4] = rshift ? cpu_to_le64(addr) :\
                                   cpu_to_le64(addr) >> rshift; \
        } while (0)
    I'm currently required to get rid of this macro in the code and I can't help thinking whether I could in a way make it "type safe". Could someone have any ideas ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    > *(uint64_t *)&evt[4] = rshift ? cpu_to_le64(addr) :\
    > cpu_to_le64(addr) >> rshift; \
    What does this do?
    If rshift is 0, you do cpu_to_le64(addr) >> 0; otherwise you do nothing.

    What is evt? Is it always correctly aligned to permit the type punning to be valid?
    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.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Salem View Post
    > *(uint64_t *)&evt[4] = rshift ? cpu_to_le64(addr) :\
    > cpu_to_le64(addr) >> rshift; \
    What does this do?
    If rshift is 0, you do cpu_to_le64(addr) >> 0; otherwise you do nothing.
    Huh, You're right.

    It should be something like
    Code:
            *(uint64_t *)&evt[4] = rshift != 0 ? cpu_to_le64(addr) :\
                                   cpu_to_le64(addr) >> rshift;
    Is there a cleaner way ? Thanks for spotting that!

    Quote Originally Posted by Salem View Post
    What is evt? Is it always correctly aligned to permit the type punning to be valid?
    This macro assumes that there's a local 64-bit integer array of size 5 of named 'evt'.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    Quote Originally Posted by Aslaville
    I'm currently required to get rid of this macro in the code and I can't help thinking whether I could in a way make it "type safe". Could someone have any ideas ?
    Turn it into a function!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    > This macro assumes that there's a local 64-bit integer array of size 5 of named 'evt'.

    > *(uint16_t *)&evt[0] = devid;
    OK, so how does this differ from
    evt[0] = devid;

    Is it important that some of the bits in your evt are not assigned by simple assignment?

    Another thought, does the type punning imply that there are some endian issues which a simple assignment-like operator couldn't achieve?
    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.

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post
    Turn it into a function!
    That's an option!
    Quote Originally Posted by Salem View Post
    > This macro assumes that there's a local 64-bit integer array of size 5 of named 'evt'.

    > *(uint16_t *)&evt[0] = devid;
    OK, so how does this differ from
    evt[0] = devid;

    Is it important that some of the bits in your evt are not assigned by simple assignment?
    No, There's no difference between the two I just wrote that because I was blindly following what's specified ;-)

    Quote Originally Posted by Salem View Post
    Another thought, does the type punning imply that there are some endian issues which a simple assignment-like operator couldn't achieve?
    I might be doing this wrong but here's the idea. 'evt' is just a chunk of 256 bits. I want to assign say bits 16:23 some value that's why I do things like.

    Code:
            *(uint8_t *)&evt[3]  = info;
    Okay, the question should be am I addressing the right bits in this case ?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    I would start by building a simple function to put a set of bits into your bit-buffer at a given position.
    Code:
    #include <stdio.h>
    
    // This doesn't account for values which have start+length spanning word boundaries.
    void setBits ( unsigned long *buffer, unsigned long value, int start, int length ) {
      int index = start / 32;
      int bitpos= start % 32;
      unsigned long mask = ( ( 1 << length ) - 1 ) << bitpos;
      buffer[index] &= ~mask;
      buffer[index] |= (value << bitpos) & mask;
    }
    
    int main()
    {
      unsigned long buffer[5] = { 0 };
      setBits(buffer,0x12,0,5);
      setBits(buffer,0x34,8,8);
      setBits(buffer,0xFFFF,36,16);
      for ( int i = 0 ; i < 5 ; i++ ) {
        printf("%08lx\n",buffer[i]);
      }
      return 0;
    }
    Then write an ENCODE_EVENT function which calls setBits three times.
    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.

  8. #8
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Salem View Post
    I would start by building a simple function to put a set of bits into your bit-buffer at a given position.
    Thanks you. This is much more flexible than I was hoping or could ever come up with :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I enforce type safety?
    By Absurd in forum C Programming
    Replies: 4
    Last Post: 03-26-2016, 11:44 AM
  2. Put together function name from type in macro
    By Groogy in forum C Programming
    Replies: 4
    Last Post: 09-09-2012, 02:33 PM
  3. What type is a macro constant?
    By Vespasian in forum C Programming
    Replies: 15
    Last Post: 09-08-2011, 11:10 AM
  4. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  5. C/C++ Type Safety
    By forumuser in forum C Programming
    Replies: 8
    Last Post: 09-23-2009, 12:27 AM

Tags for this Thread