Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Suggestion for Speed Improvement to Blender Functions

This thread is locked; no one can reply to it. rss feed Print
Suggestion for Speed Improvement to Blender Functions
Kris Asick
Member #1,424
July 2001

I was browsing the Allegro 4.2.0 source to see how complex the blender functions are so I could determine how to effectively integrate them into the game engine I'm working on, and I noticed that there are numerous divisions by constant powers of 2 on unsigned long integers littered throughout "colblend.c". For example:

/* _blender_dodge24:
 *  24 bit dodge blender function.
 */
unsigned long _blender_dodge24(unsigned long x, unsigned long y, unsigned long n)
{
   return BLEND(24, getr24(x) + (getr24(y) * n / 256),
        getg24(x) + (getg24(y) * n / 256),
        getb24(x) + (getb24(y) * n / 256));
}

Wouldn't it be faster and more efficient, especially considering the nature of these procedures, to change all the divisions by 8, 32, and 256 to right bit-shifts of 3, 5 and 8? I'd do the change myself if I understood how changes to the Allegro source were actually proposed and made.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

I'd do the change myself if I understood how changes to the Allegro source were actually proposed and made.

I imagine this hasn't been done because it's trivial for the compiler to optimize constant, power-of-2 division itself.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

gnolam
Member #2,030
March 2002
avatar

Profile it and see what happens.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Kris Asick
Member #1,424
July 2001

Well, I decided to perform a test on my system. I compiled Allegro 4.2.0 the way it always was, made the change from division to bit-shifting, then compiled another copy of the DLL. I quickly wrote up a pixel plotting program and tested it with both DLLs...

...There was no discernable difference in speed.

Then again, when I compile Allegro, the ALLEG42.DLL file ends up almost twice as big as the one that comes with the pre-compiled binaries, so I have to wonder if something's going on with that which would be affecting my tests. shrugs

In either case, I guess it isn't the biggest of deals considering my results.

...though my pixel plotter managed to hit 4,000,000 additive-blended pixels a second, with 5 modulo, 5 rand(), one addition and one IF operation additionally being performed for each of those 4,000,000 pixels... that's a lot of processing going on... :o

When I took the blender functionality out, it only got up to 6,500,000 pixels a second. I guess the blender functions must work faster than I thought...

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Evert
Member #794
November 2000
avatar

Quote:

Wouldn't it be faster and more efficient, especially considering the nature of these procedures, to change all the divisions by 8, 32, and 256 to right bit-shifts of 3, 5 and 8?

Not really; it's one of those trivial optimizations that the compiler can do automatically.

Richard Phipps
Member #1,632
November 2001
avatar

Quote:

I was browsing the Allegro 4.2.0 source to see how complex the blender functions are so I could determine how to effectively integrate them into the game engine I'm working on

If you didn't want to use AllegroGL / OpenLayer, I'd look at the source for FBlend and that use parts of that (without the functions calls if possible).

Go to: