[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] swap bits
From: |
Alexandr A. Redchuck |
Subject: |
Re: [avr-gcc-list] swap bits |
Date: |
Tue, 21 Jan 2003 23:03:54 +0200 (EET) |
Hi, Torsten Hahn!
21-Jan-03 17:30 you wrote:
TH> iam looking for a way, to efficiently swap all bits in an register. Can
TH> somebody help me with a little asm-macro or a pice of efficient c-code ?
TH> It schould take an 8bit value as input an give back an value, where bi7
TH> of the input is now the bit 0, bit6 is bit1 etc.
TH> I have no idea how to to this very well.
; fastest but very large - table decoder
mov r30,r16
clr r31
subi r30,low(-bitswaptbl)
sbci r31,high(-bitswaptbl)
lpm
mov r16,r0
; 6 + 128 words, 8 cycles w/o r0,r30,r31 save/restore
; 12 + 128 words, 20 cycles with r0,r30,r31 save/restore
; can be even faster if table placed in 256-byte aligned section
mov r30,r16
ldi r31,high(bitswaptbl)
lpm
mov r16,r0
;--------------------
; smallest but slowest - cycle with rol/ror, well analised by Klaus Rudolph
; -------------------
; intermediate by size and speed
mov r17,r16
clr r16
.set bitno,0
.rept 8
sbrc r17,bitno
sbr r16,1<<bitno
.set bitno,bitno+1
.endr
; 2+2*8 = 18 words, 18 cycles w/o r17 save/restore
; 20 words, 22 cycles with r17 save/restore
wbr,
--
/* Alexandr Redchuck, Kyiv, Ukraine */
/* real '\x40' real '\x2E' kiev '\x2E' ua */
avr-gcc-list at http://avr1.org