uint32_t *p=(uint32_t*)buff; //32 bit pointer
Sum=*p;
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.
The pointer method should be far faster; but that does depend on the CPU architecture and instructions available.
If it's an 8 bit CPU, it's going to be four separate byte moves anyway. A 32 bit should only need a couple of instructions.
In the 8 bit case, using a union and explicitly moving each byte directly to its destination could be the faster method?
Both CPUs are 8 bit which is why I was surprised that the non pointer method was quicker on the Arduino (atmega 328).
With a union I wouldn't have to do anything, I could just access it as a 32 bit word.
What I mean is, in your example, the 4 x 8-bit values that make up the 32-bit address, are hard coded into the source and loaded into the 4 byte buffer.
If that is the case in the real code, then why not just assign the pointer directly ... (I'm not sure about the endianess, so this may be wrong.)
C-like:
uint32_t i = 0x12*24 + 0x34*16 + 0x56*8 + 0x78;
The compiler should perform the math at compile time and the result shoudl be 4 byte assignments at runtime with the result be usable directly as a pointer.
The data is made up of 14 bit words loaded into a 56 byte buffer. 14 * 32 = 56 * 8. The 56 bytes are a 16 bit counter, a 32 bit checksum and 50 bytes of data. The checksum calculation is where the pointer/maths question comes in.
Thanks Ian, never tried putting an array in a union. Will play with it tomorrow - OMG, sounded like someone else then.