What do multiple typecasts do?

Thread Starter

PaulArt

Joined Mar 14, 2013
3
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))
 

kubeek

Joined Sep 20, 2005
5,796
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.
 

kubeek

Joined Sep 20, 2005
5,796
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).
 

Thread Starter

PaulArt

Joined Mar 14, 2013
3
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
 

takao21203

Joined Apr 28, 2012
3,702
((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.
 

charliewilde

Joined Mar 13, 2013
13
((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.

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