Skip to content

Optimize bit reverse table #1

@X-Ryl669

Description

@X-Ryl669

Although the BitReverseTable is only used in a single function, it's possible to optimize it away either by doing the bit test in the function itself, or to use this code:

uint8_t reverseByte(const uint8_t a)
{
    uint8 v = a;
    v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
    v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
    v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
    return (uint8)v;
}

This compiles to a lot less than 256 bytes, it's using 15 cycles with no multiply or division, so is probably faster than accessing the SPI flash for the table.

There's also a version with 2 multiplication:

uint8_t reverveByte2(const uint8_t b)
{
    return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
}

It's using only 7 operations. Those operations are described in Bit Twiddling Hacks

By the way, even better the code could probably be replaced by:

		for (int r = Height >> 3; --r >= 0;) {
			uint8_t Byte = *Data++; 
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth; Byte <<= 1;
			*optr = (*optr & bit) | ((Byte & 0x80) >> shift); optr += DWidth;
		}	

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions