intelliproject logo

Location: Desktop development - C/C++    License: The Intelliproject Open License (IPOL)

Optimize through simplifying the formula

Posted by Wong Shao Voon

Optimize through Simplifying the Formula

Skill: Beginner

Posted: 31/03/2009

Views: 616

Rating: 0.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

When we have a formula, we could see if we can optimize away the some of the operations and the integer to float conversions by simplifying the formula. I will demostrate this by using the alphablend formula in MSDN. This may come useful if you are writing your own custom-optimized alphablend function.

Note in the formula: S is one of source primary colors(RGB), D is one of destination primary colors(RGB) and A is the alpha channel. S, D and A are byte integers(that is, value is from 0 to 255).

First, we will attempt to remove the expensive integer to float conversion through rearrangement. Below is the original BLENDFUNCTION formula in MSDN.

OriginalEquation.GIF

As shown below, we execute the subtraction to invert the alpha first before the division by 255.0.

SubtractionRearranged.GIF

Next, we will do the multiplications before the divisions. If the multplications are done first, the results of the divisions will not be 0.0 to 1.0 but 0 to 255, thus we can eliminate the float values and the integer to float conversions. You may ask, since S, D and A are byte integers, will we overflow the byte integer if we execute the multiplications first? The answer is no. Because in C/C++, byte and short integers are promoted to full integers before any computation begins.

FloatsEliminated.GIF

Since both divisions are the same(both are divided by 255), we can group them together as below. In that case, we have eliminate one division.

DivisionEliminated.GIF

As a further optimzation, since 255 is close to 256, we could replaced the division by 255 with shift to the right by 8 as shown below. Of course, this optimization would sacrifice some accuracy, for alphablend operations, this is okay.

ShiftRightByEight.GIF

The source code

I have made a benchmark application to benchmark the alphablending of each formula for 1000 times. As shown in the screenshot below, the "Unoptimized" option is the original MSDN formula, the "Optimized" option is the new improved formula and the "Very Optimized" option is the new improved formula with right shifting to replace the division.

Screenshot.JPG

The results for a release build on my machine, is as follows

Unoptimized: 10030 msec
Optimized: 3790 msec
Very Optimized: 2821 msec

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for IntelliProject; I have a few ideas to write about but never get around writing because of hectic schedule.

Location: Singapore
Ocupation: Software Developer

Posted by Silviu Caragea at 01/04/2009 14:10
You can find some tips and tricks for optimization here : http://aggregate.org/MAGIC/

Sign up to post message on the article message board!