[comp.sys.amiga.programmer] best way to reverse a bitplane?

markv@kuhub.cc.ukans.edu (04/29/91)

In article <1991Apr29.101302.1@atmo2.atmo.arizona.edu>, leuthold@atmo2.atmo.arizona.edu writes:
> What is the best way to reverse a bitplane?  A brute force method would be 
> to reverse the bytes and then reverse the bits in each byte.  The only way I
> can think of to reverse the bits in each byte is to mask each bit off then
> shift it to its new place and add it to the new byte.  Is there a better way?

Well, for a 68000, you have to do it bit by bit.  However, your basic
loop is going to have an AND, an OR, a LSR/LSL.  With the 68000, you
would get about 33% better speed with a BTST followed by a BSET.  Note
this assumes you optimize things for operand size, etc.  In general
you will gain a lot of speed by unrolling your loop for each bit,
since you wont have to caclulate the Mask with for each bit.

The 68020 and later have some bit range instructions that may help,
but I dont have a reference handy.

Also, I assume you mean reverse right/left, since a top bottom reverse
can be done by just swapping bytes.  Note the blitter doesn't help
since all its DMA must progress in the same "direction".

> Mike Leuthold
> leuthold@atmo1.atmo.arizona.edu
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Gooderum			Only...		\    Good Cheer !!!
Academic Computing Services	       ///	  \___________________________
University of Kansas		     ///  /|         __    _
Bix:	  mgooderum	      \\\  ///  /__| |\/| | | _   /_\  makes it
Bitnet:   MARKV@UKANVAX		\/\/  /    | |  | | |__| /   \ possible...
Internet: markv@kuhub.cc.ukans.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

leuthold@atmo2.atmo.arizona.edu (04/30/91)

What is the best way to reverse a bitplane?  A brute force method would be 
to reverse the bytes and then reverse the bits in each byte.  The only way I
can think of to reverse the bits in each byte is to mask each bit off then
shift it to its new place and add it to the new byte.  Is there a better way?

Mike Leuthold
leuthold@atmo1.atmo.arizona.edu

ccplumb@rose.waterloo.edu (Colin Plumb) (04/30/91)

leuthold@atmo2.atmo.arizona.edu wrote:
>What is the best way to reverse a bitplane?  A brute force method would be 
>to reverse the bytes and then reverse the bits in each byte.  The only way I
>can think of to reverse the bits in each byte is to mask each bit off then
>shift it to its new place and add it to the new byte.  Is there a better way?

Yes, table look-up.  A 256 byte table wouldn't take up much more room than
your bit-reversing code, and would be a lot faster.

Oh, and the best way to reverse a bitplane is to swap bytes in from the 
ends.  In C,

UBYTE *p1, *p2;
UBYTE c1, c2;
extern UBYTE table[256];

p1 = <starting address of row>;
p2 = p1 + <bytes per row>;	/* Note: assumed even, and >0 */
do {
	c1 = *p1;
	c2 = *--p2;
	*p1++ = table[c2];
	*p2 = table[c1];
} while (p1 != p2);

Translating this into assembler is easy.  Just keep the high parts
of the registers holding c1 and c2 clear, and use the indexed addressing
mode.
-- 
	-Colin

mykes@amiga0.SF-Bay.ORG (Mike Schwartz) (05/01/91)

In article <1991Apr29.155129.30171@kuhub.cc.ukans.edu> markv@kuhub.cc.ukans.edu writes:
>In article <1991Apr29.101302.1@atmo2.atmo.arizona.edu>, leuthold@atmo2.atmo.arizona.edu writes:
>> What is the best way to reverse a bitplane?  A brute force method would be 
>> to reverse the bytes and then reverse the bits in each byte.  The only way I
>> can think of to reverse the bits in each byte is to mask each bit off then
>> shift it to its new place and add it to the new byte.  Is there a better way?
>
>Well, for a 68000, you have to do it bit by bit.  However, your basic
>loop is going to have an AND, an OR, a LSR/LSL.  With the 68000, you
>would get about 33% better speed with a BTST followed by a BSET.  Note
>this assumes you optimize things for operand size, etc.  In general
>you will gain a lot of speed by unrolling your loop for each bit,
>since you wont have to caclulate the Mask with for each bit.
>
>The 68020 and later have some bit range instructions that may help,
>but I dont have a reference handy.
>
>Also, I assume you mean reverse right/left, since a top bottom reverse
>can be done by just swapping bytes.  Note the blitter doesn't help
>since all its DMA must progress in the same "direction".
>

You can use a 256-byte lookup table to get your reversed bits and do it
a lot faster than any of the methods mentioned above.


--
****************************************************
* I want games that look like Shadow of the Beast  *
* but play like Leisure Suit Larry.                *
****************************************************

chrisg@cbmvax.commodore.com (Chris Green) (05/01/91)

In article <1991Apr29.101302.1@atmo2.atmo.arizona.edu> leuthold@atmo2.atmo.arizona.edu writes:
>What is the best way to reverse a bitplane?  A brute force method would be 
>to reverse the bytes and then reverse the bits in each byte.  The only way I
>can think of to reverse the bits in each byte is to mask each bit off then
>shift it to its new place and add it to the new byte.  Is there a better way?


	You want a 256 byte table for the bit reversal. A 64K table if you REALLY
need it to be fast.
-- 
*-------------------------------------------*---------------------------*
|Chris Green - Graphics Software Engineer   - chrisg@commodore.COM      f
|                  Commodore-Amiga          - uunet!cbmvax!chrisg       n
|My opinions are my own, and do not         - killyouridolssonicdeath   o
|necessarily represent those of my employer.- itstheendoftheworld       r
*-------------------------------------------*---------------------------d

dvljrt@cs.umu.se (Joakim Rosqvist) (05/02/91)

In article <21107@cbmvax.commodore.com> chrisg@cbmvax.commodore.com (Chris Green) writes:
>In article <1991Apr29.101302.1@atmo2.atmo.arizona.edu> leuthold@atmo2.atmo.arizona.edu writes:
>>What is the best way to reverse a bitplane?  A brute force method would be 
>>to reverse the bytes and then reverse the bits in each byte.  The only way I
>>can think of to reverse the bits in each byte is to mask each bit off then
>>shift it to its new place and add it to the new byte.  Is there a better way?
>
>
>	You want a 256 byte table for the bit reversal. A 64K table if you REALLY
>need it to be fast.
>|Chris Green - Graphics Software Engineer   - chrisg@commodore.COM      f

Make that a 128K table - you would be using 64K words!
/$DR.HEX$