Trick: Reversing bits using hw

8bitforce

Member
If you need to reverse bits, FLEXIO module already has several registers that do it automatically. You write it to one register and can read it from a few others that have different bit flippings... This was very handy for my transputer project where I have to reverse 32bits constantly. FYI.

Code:
// Use FLEXIO3 aliased buffers to flip bits for free :)
                                // (IMXRT_FLEXIO3_ADDRESS = 4202_0000h)
  FLEXIO3_SHIFTBUF0 = x;        // d[31:0] 200h + (a * 4h)
  x = FLEXIO3_SHIFTBUFBIS0;     // returns d[0:31] 280h + (a * 4h)
 
This might be handy if you wanted to reverse a value without involving the CPU (e.g. using DMA) but otherwise the rbit opcode is going to be much faster.
 
Thank you. I learned something new. I'm still stuck in 1980's.

Is there a better way to implement it than below? There is an __RBIT(uint32_t value) defined in cmsis_gcc.h.

Code:
uint32_t value = 0x12345678;
uint32_t result;

asm volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );

Serial.println(value, HEX);
Serial.println(result, HEX);
 
Unfortunately no.
You can remove "volatile" to give the compiler a bit more leeway for scheduling, but the best you can do to try to keep the code portable is wrapping it in a macro that throws a compile error for any machine that isn't ARM. People have long requested a GCC builtin for bit reversal but as of yet it's only appeared in clang.
 
Back
Top