This C code is fairly effective too. It works on nibble-level instead of byte-level, to save ROM:
#pragma CONST_SEG GLOBAL_ROM
static const uint8 MIRROR_TABLE[16] =
{
0x0, 0x8, 0x4, 0xC, /* 0-3 */
0x2, 0xA, 0x6, 0xE, /* 4-7 */
0x1, 0x9, 0x5, 0xD, /* 8-11 */
0x3, 0xB, 0x7, 0xF /* 12-15 */
};
#pragma CONST_SEG DEFAULT
uint8 my_byte; /* byte to mirror */
uint8 tmp;
tmp = MIRROR_TABLE[my_byte >> 4];
tmp <<= 4;
my_byte = MIRROR_TABLE[my_byte & 0x0F] | tmp;On a Freescale micro, the left shift will be optimized into a MUL instruction, saving a few ticks. The right shift will become 4 LSRx instructions (no optimization possible).
If you expand the lookup table from 16 bytes to 256 bytes you don't need to do the shifting and thus save 10 CPU ticks or so, at the cost of ROM.
Message Edited by Lundin on 2009-12-01 11:46 AM