Hi,

a lookup table seems to be the best solution, because it´s similar to bitcount. The C-FAQs say that this function is optimal for bitcount:

// fast and small bitcounting function
static int bitcounts[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
unsigned int
bitcount (unsigned int u) // int can be arbitrary long
{
  int n = 0;

  for (; u != 0; u >>= 4)
    n += bitcounts[u & 0x0f];

  return (n);
}

This version is better than this

int
bitcount (unsigned int value)
{
  int count;

  for (count = 0; value; count++)
    value &= (value - 1);
  return (count);
}

or this

#define BITCOUNT(x)     (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
#define  BX_(x)         ((x) - (((x)>>1)&0x77777777)                    \
                             - (((x)>>2)&0x33333333)                    \
                             - (((x)>>3)&0x11111111))

Regards

Rolf F.

Sumukh Pathare schrieb:
Use the following algorithm:
low_nibble = input_number & 0x0F;
low_nibble_rev = (bit_reverse_lookup[low_nibble]) <<
4;
hi_nibble = input_number >>4;
hi_nibble_rev = bit_reverse_lookup[hi_nibble];
answer = hi_nibble_rev | low_nibble_rev;

Note that bit_reverse_lookup array is 16 bytes in
size.
I am not sure if this version is any more efficient
than  doing the bit reverse bit by bit. But does
require smaller lookup table.
If you can spare 16 more bytes than you can make the
above algorithm slightly more efficient:

low_nibble = input_number & 0x0F;
low_nibble_rev = bit_reverse_lookup_lo[low_nibble];
hi_nibble = input_number >>4;
hi_nibble_rev = bit_reverse_lookup_hi[hi_nibble];
answer = hi_nibble_rev | low_nibble_rev;

-Sumukh

--- David Brown <da...@westcontrol.com> wrote:


Hi,

thanks for all the input!
Leon, this really is a cool function. It will go

to my collection ;-).

Though I used your version, Steve. Worked out of

the box!

Why does it say "+r" ?

I think the "+r" means that it is both an input and
an output, allowing it
to be initialised at the C level ("unsigned char tmp
= 0").  Of course, this
initialisation is not actually needed, since all 8
bits of the original tmp
get shifted out, so an instruction could be saved by
using plain "unsigned
char tmp;", and changing to "=r".

David



And, perhaps you could add the "gcc manual hint"

to the 1.§ in chapter 6
(didn't

even had the idea to look there, too).

Greetz and thanks,

Georg


Steve Underwood wrote:

Hi Georg,

I haven't tested this, but to embed the assembly

language you need

something like the following. If you look in the

mspgcc documentation

you will find some information about this. If

you look in the GNU GCC

documentation you will find some more. However,

this area is not as well

documented as it might be, with clear examples,

etc. Figuring out

exactly how to use those dependency fields is

not easy.

Regards,
Steve

unsigned char reverse(unsigned char b)
{
unsigned char tmp = 0;

for( i=0; i<8; i++ )
{
  __asm__ (
          " rrc.b  %[b] \n"
          " rrl.b   %[tmp] \n"
      : [tmp] "+r"(tmp)
      : [b] "r"(b));
}
  return tmp;
}

Georg Ritter wrote:


Good morning!

I'm looking for a proper implementation of a

bitwisereverse function

of a byte (1010 0001 becomes: 1000 0101). I

want to replace an ugly

and clumpsy function I used in testing.

I would like to write smth like:

unsigned char reverse(unsigned char b)
{
 unsigned char tmp=0;
 for( i=0; i<8; i++ )
 {
   // RRC.B of b
   // RRL.B into tmp
 }
   return tmp;
}

There's no real "C" way of doing it, isn't it?

So what could the

inline asmbly line for that look like. I never

used it before, and

it's tricky and documents didn't enlighten my

too much. So my versions

either didn't compile or didn't work.

Or is there a smaller (in terms of code size)

way of doing the

reversion (hacker contest ;-) )?

..





-------------------------------------------------------

The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and

Integration

See the breadth of Eclipse activity. February 3-5

in Anaheim, CA.

http://www.eclipsecon.org/osdn
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net


https://lists.sourceforge.net/lists/listinfo/mspgcc-users







-------------------------------------------------------

The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and
Integration
See the breadth of Eclipse activity. February 3-5 in
Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net


https://lists.sourceforge.net/lists/listinfo/mspgcc-users



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users




Reply via email to