I have a project where I need to load a 32 bit variable from a byte array. I tried two different methods,
First
And second,
I first tried this on MPLABX and, as I expected, the pointer method used 35 words less of program space.
I then tried the same thing on Arduino and to my surprise the non pointer method used 36 less program words.
I'm guessing this is to do with optimisation - the MPLABX having limited optimisation.
Is there a more efficient way to do this?
Mike.
For anyone interested, the arduino code is,
First
Code:
Sum=buff[0]+((uint32_t)buff[1]<<8)+((uint32_t)buff[2]<<16)+((uint32_t)buff[3]<<24);
Code:
uint32_t *p=(uint32_t*)buff; //32 bit pointer
Sum=*p;
I then tried the same thing on Arduino and to my surprise the non pointer method used 36 less program words.
I'm guessing this is to do with optimisation - the MPLABX having limited optimisation.
Is there a more efficient way to do this?
Mike.
For anyone interested, the arduino code is,
Code:
uint8_t buff[6];
void setup(){
Serial.begin(115200);
uint32_t Sum;
buff[3]=0x12;
buff[2]=0x34;
buff[1]=0x56;
buff[0]=0x78;
//uint32_t *p=(uint32_t*)buff; //32 bit pointer
//Sum=*p;
Sum=buff[0]+((uint32_t)buff[1]<<8)+((uint32_t)buff[2]<<16)+((uint32_t)buff[3]<<24);
Serial.print("Sum = ");Serial.println(Sum,HEX);
while(1){
}
}
void loop(){
}