Clamping a value to a range in C++, quickly

Started by
13 comments, last by ZQJ 16 years, 5 months ago
I wonder if splitting it into 2 loops would help: one doing the min clamping and one doing the max clamping. Twice as much loop overhead, some redundant checks made, but half the conditionals in each iteration.

I also wonder if it's implementing the sequential indexing as address increments, or if there's some quirk of C++ that requires it to not do that in case i changes somehow. I expect it handles it just fine, but I always find it interesting to read the assembly.
Advertisement
Quote:Original post by Kylotan
I wonder if splitting it into 2 loops would help: one doing the min clamping and one doing the max clamping. Twice as much loop overhead, some redundant checks made, but half the conditionals in each iteration.


That probably depends on the data set - if it's small enough to fit comfortably in the L1 cache it may be faster as two loops. If it doesn't fit the L1 but fits the L2 then I would expect two loops to be a bit slower. If it doesn't fit in L2 then two loops would be a lot slower. It comes back to needing to know more about the context to offer any better optimization advice I think.

Game Programming Blog: www.mattnewport.com/blog

More great ideas; I'll give these a shot later today.
if you are using VS you may also want to look at restrict and noalias annotations for your code. Loop unrolling is also a possibility.
Well, I'm not sure exactly how difficult this would be in your situation, but what you really want is to get your compiler to generate MINPS and MAXPS instructions. That will allow you to do four elements of the array at a time. For GCC look at X86 builtins in the manual for how to get this (and take a look in pmmintrin.h).

Edit: didn't read that part about portability carefully enough. If you don't want to sacrifice any portability, I think your options are a bit thin beyond what you have. You can always have a backup version that gets compiled if any of the optimized versions aren't good enough.

This topic is closed to new replies.

Advertisement