Jump to content
  • Advertisement
Sign in to follow this  
alessandro ferrucci

replace branch statements with bitwise operations

This topic is 3065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm writing an image binarization algorithm which simply converts each pixel's luminance value (grayscale image) to a black or white. Currently the algorithm for binarizing each pixel is roughly this if( grayscale[x] < thresholdValue) { bitonal[x] = 1; } (this is actually a simplification of the ACTUAL algorithm because the bitonal image is actually a bitpacked image (each array index holds 8 pixels) so I actually bitpack the 1 inside the current array index...but I don't think that changes my question. What I'm attempting to do is to remove the need for the if-statement. What I was thinking was to do something along the lines of this. Subtract the thresholdValue by the grayscale and then perform some bit manipulation trickery to clear out or shift bits such that if the result of (grayscale[x]-threshold) is less than 0, I get a 0. otherwise I would get a 1 . If it's easier to do it the other way around (if grayscale[x]-threshold < 0 + bitwise trickery get a 1, else get a 0) that would also work... as long as I can get rid of the branch statements...any help appreciated..

Share this post


Link to post
Share on other sites
Advertisement
These sorts of micro-optimizations are already being performed for you by the compiler, particularly if you've set it up to target a processor that has CMOV. If you empirically discover a need to improve performance (as determined by using a profiler, in a real-world usage scenario) you then move to the inline assembly.

Share this post


Link to post
Share on other sites
If this truly is a performance problem then you're probably better off looking at SIMD intrinsics rather than trying to work out clever C bit-hacks. On x86 processors you can perform this particular operation, including the bit-packing, on sixteen pixels in parallel using a pair of SSE-2 instructions (PADDUSB and PMOVMSKB.)

Share this post


Link to post
Share on other sites
Quote:
Original post by alessandro ferrucci
Hello,
I'm writing an image binarization algorithm which simply converts each pixel's luminance value (grayscale image) to a black or white. Currently the algorithm for binarizing each pixel is roughly this

if( grayscale[x] < thresholdValue)
{
bitonal[x] = 1;
}

(this is actually a simplification of the ACTUAL algorithm because the bitonal image is actually a bitpacked image (each array index holds 8 pixels) so I actually bitpack the 1 inside the current array index...but I don't think that changes my question.

What I'm attempting to do is to remove the need for the if-statement.

What I was thinking was to do something along the lines of this. Subtract the thresholdValue by the grayscale and then perform some bit manipulation trickery to clear out or shift bits such that if the result of (grayscale[x]-threshold) is less than 0, I get a 0. otherwise I would get a 1 . If it's easier to do it the other way around (if grayscale[x]-threshold < 0 + bitwise trickery get a 1, else get a 0) that would also work... as long as I can get rid of the branch statements...any help appreciated..



Can you post the assembly being generated by your compiler on an optimized build?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!