Code size for bit reversing algorithms

I want to add support for "LSB first" to bitbang SPI in circuitpython. Probably the best way to do this is to optionally reverse the bits in each byte according to a flag setting.

Code space is always at a premium, so I investigated several code fragments for bit reversal to find out which was smallest on arm and xtensa. These code fragments are gathered from the internet. The loop is the smallest alternative on both architectures, but the 16-element look up table is not much bigger (on arm, the difference is bigger on xtensa) and is probably faster.

arm-none-eabi-gcc-7.2.1 -Os -mthumb

   text    data     bss     dec     hex filename
     40       0       0      40      28 bitrev_loop.o
     44       0       0      44      2c bitrev_lut16.o
     44       0       0      44      2c bitrev_shifts1.o
     44       0       0      44      2c bitrev_twiddle1.o

xtensa-lx106-elf-gcc-4.8.5 -Os

   text    data     bss     dec     hex filename
     40       0       0      40      28 bitrev_loop.o
     53       0       0      53      35 bitrev_lut16.o
     57       0       0      57      39 bitrev_shifts1.o
     52       0       0      52      34 bitrev_twiddle1.o


Files currently attached to this page:

bitrev_loop.c138 bytes
bitrev_lut16.c281 bytes
bitrev_shifts1.c193 bytes
bitrev_twiddle1.c178 bytes



Entry first conceived on 23 April 2018, 2:50 UTC, last modified on 23 April 2018, 3:00 UTC
Website Copyright © 2004-2018 Jeff Epler