I'm programming in Standard C on several microcontrollers now for about 30 years.
Now I have a question:
I have somewhere a variable with type long. The value of this variable does never extend +-0x7fff.
Choosing "long" has other benefits. Because it's a 32 bit machine, arithmetic with 32 bit is faster than 16 or 8 bit (no sign extension needed, this speeds up my fast control algorithm up to 10%...).
On the other hand I have a legacy function, that requires this variable as short. At the moment I'm copying this variable somewhere from "long" into a "short" working variable. To speed my code further up, I want to get rid of this useless copy. As modern CPUs are quite good with pointer operations, I like to access the long variable with a local pointer
short * p;
During initialization of the module
p = (short *) pointer_to_long_variable
so that I can access this variable simply with
do_some_calculations with *p
I think this idea should work on my low-endian machine, but is this save even on machines with big-endianess?
Sorry for my bad English, I'm not a native speaker :)