What do multiple typecasts do?

Discussion in 'Embedded Systems and Microcontrollers' started by PaulArt, Mar 14, 2013.

  1. PaulArt

    Thread Starter New Member

    Mar 14, 2013
    3
    0
    I am trying to decipher someone else's code (story of my life). I am from the microprocessor world(PowerPC/StrongARM/ARM) and also doing fixed point math convolutions for the first time so bear with me.
    This is for an ATXMega128 compiled on IAR compiler
    I do not understand the multiple casts in the macros below:

    integerToFixedPoint(x)
    ((unsigned long int
    )((unsigned long int)(x) << 6U))

    fixedpointToInteger(x)
    (unsigned short int)(unsigned long int)(x) >> 6U))
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Not sure about IAR, but I think GCC used to have some problems with bit shifts in the way that when you did for example
    unsigned char a;
    a<<6;
    it promoted the char to short, did the shift on the two bytes and then trimmed it back to one byte. So this maybe a way of telling the compiler how exactly the shift should be done.

    Also, did you notice the bottom one has different parenthesis than the top one? That seems rather odd, but probably just a typo without impact on evaluation.
     
  3. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Also, what made you choose the IAR environment? I am using the new atmel studio and it seems to work very well (apart from a few quirks with code folds).
     
  4. PaulArt

    Thread Starter New Member

    Mar 14, 2013
    3
    0
    Thanks much for your reply Kubeek it definitely gives me a clue now.

    I am using AVR Studio also and finding it good but the IAR is legacy, it was chosen sometime back
     
  5. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    ((unsigned long int)((unsigned long int)(x) << 6U))

    (\
    (unsigned long int)\
    ((unsigned long int)(x) << 6U)\
    )

    I was always thinking (from experience),
    each casted expression must be in parantheses too.

    so rather (((unsigned long int)(x))<<6U)

    The simulator/single step debugger would be of help here to modify such complicate casts until the result is same as desired.
     
  6. PaulArt

    Thread Starter New Member

    Mar 14, 2013
    3
    0
    Sorry guys - yes that was a typo.
     
  7. charliewilde

    New Member

    Mar 13, 2013
    13
    1

    Do you mind if you care to explain this thing? I really getting a hard time in understanding it.
     
Loading...