<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Mon, 26 Mar 2001 15:12:23 -0800 (PST)
From   : Thomas Harte <t.harte@...>
Subject: Re: 2 requests

Sent my reply straight to the original poster, as I usually quite stupidly
do, but is this a good solution to 'decoding a BBC [80 column] screen
address' ?

int x, y;

/* gets x and y in either

       3 and's
       8 shifts
       7 adds
       1 conditional branch

or

       3 and's
       10 shifts
       8 adds
       1 conditional branch
*/

void get_addr(int offset)
{
       int yoffs;

       /* take away bottom 3 bits */
       yoffs = offset&7;
       offset >>= 3;

       /* in order to divide by 80, divide by 16 first, since 5*16=80 */
       y = offset >> 4;

       /*

       now divide by 5 using a numeric method based on the observation that

       x/5 = x/4 - x/10, and hence x/10 = x/8 - x/20, etc.

       a few fractional bits are used to maintain some accuracy - either 3
       or 5 depending on the low bit, since this produces the right result

       */
       if(y&1)
       {
               y =     + (y << 1) - (y >> 1)
                       + (y >> 3) - (y >> 5)
                       + (y >> 7);

               y &= ~7;
       }
       else
       {
               y =     + (y << 3) - (y << 1)
                       + (y >> 1) - (y >> 3)
                       + (y >> 5) - (y >> 7);

               y = (y >> 2)& ~7;
       }

       /*

       get x just with

       offset - (y*60) = offset - y*64 - y*16

       */
       x = offset - (y << 3) - (y << 1);

       /* add back low 3 bits */
       y += yoffs;
}

I made it up myself, and I've tested it and it works with 100% accuracy on
at least the range of valid screen addresses . . . just thought it might be
a little slow thats all.

-Thomas





_______________________________________________________
Send a cool gift with your E-Card
http://www.bluemountain.com/giftcenter/
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>