Table 2:
Example source code in C++ for the emulation of bit-reversed indirect addressing, an unusual memory access mode implemented on the TMS320C32 DSP, and used by the BIS Engine software for efficient calculation of Fast Fourier Transforms.
Example Emulation of DSP Bit-Reversed Addressing |
Commentary |
---|---|
uint32_t tms3203x_device::mod19(uint32_t op, uint8_t ar) { // Emulates the bit-reversed addressing mode *ARx++(IR0)B. int reg = TMR_AR0 + (ar & 7); uint32_t result = IREG(reg); IREG(reg) = bitrev(bitrev(IREG(reg))+bitrev(IREG(TMR_IR0))); return result; } |
The routine mod19 is called to emulate the bit-reversed addressing mode on the TMS320C32 DSP. This mode is a variant of the Indirect With Postindex Add and Modify mode, written *ARx++(IR0) where ARx is one of the Auxiliary Registers AR0-AR7, and IR0 is one of the Index Registers. A value is retrieved from the memory location in ARx, and then ARx is incremented by IR0. This is useful for accessing tables. In bit-reversed mode, *ARx++(IR0)B, the increment by IR0 is instead performed with bits carried rightwards. This creates an unusual memory access pattern that is ideal for quick calculation of FFTs. |
uint32_t tms3203x_device::bitrev(uint32_t x) { x = ((x & 0xffff0000u) >> 16) ∣ ((x & 0x0000ffffu) << 16); x = ((x & 0xff00ff00u) >> 8 ) ∣ ((x & 0x00ff00ffu) << 8 ); x = ((x & 0xf0f0f0f0u) >> 4 ) ∣ ((x & 0x0f0f0f0fu) << 4 ); x = ((x & 0xccccccccu) >> 2 ) ∣ ((x & 0x33333333u) << 2 ); x = ((x & 0xaaaaaaaau) >> 1 ) ∣ ((x & 0x55555555u) << 1 ); return x; } |
The routine bitrev is an efficient way to reverse the order of the bits in a 32-bit number, here in the variable x. In the first step, the top 16 bits are slid 16 places to the right, and are recombined with the bottom 16 bits slid 16 places to the left. Next, the top 8 bits change places with the next adjacent 8 bits. This process is repeated with groups of 4 bits, 2 bits, and finally each bit swaps place with its immediate neighbor. The result of this line-dance is that the whole bit order is reversed in only 5 steps. Interestingly, these 5 steps can be performed in any order. |