Inverted Counter


Inverted counting is best described by example:

 0 0 0
 1 0 0
 0 1 0
 1 1 0
 0 0 1
 1 0 1
 0 1 1
 1 1 1

Note that this counter caries to the right. This makes the slowest changing digit be the lest significant digit. Contrast this to a normal counter:

 0 0 0 
 0 0 1
 0 1 0
 0 1 1
 1 0 0 
 1 0 1
 1 1 0
 1 1 1


Implementation

I have looked for a quick way to implement an inverted increment. I am currently doing a normal increment and then moving the bits one at a time using sixteen AVR bit load/store instructions.

    .macro movb
	bst byseq,@1
	bld bytmp,@0
    .endm
	movb 7,0
	movb 6,1
	movb 5,2
	movb 4,3
	movb 3,4
	movb 2,5
	movb 1,6
	movb 0,7

I've asked hackers I know for a better idea. They suggested table lookup and then lost interest. I don't want to allocate 512 bytes for the table or write all the instructions need to do an indexed fetch from it.

Kragen Javier Sitaker observes (crediting Henry Massalin) that three perfect shuffles will solve my problem, if only my computer had a bit-shuffle instruction.

  abcd EFGH
  EaFb GcHd
  GEca HFdb
  HGFE dcba

http://lists.canonical.o ... mber/001024.html

Mike Stone observes that, rather than bit-flipping my counter one bit at a time, I could perform my compare operation one bit at a time.

 

Last edited November 17, 2010
Return to WelcomeVisitors