I’m trying to clamp a value between -127 and 127 on a Cortex-M based microcontroller.
I have two competing functions, one uses conditionals the other uses a branchless hack I found here.
// Using conditional statements
int clamp(int val) { return ((val > 127) ? 127 : (val < -127) ? -127 : val); }
// Using branchless hacks
int clamp(int val) {
val -= -127;
val &= (~val) >> 31;
val += -127;
val -= 127;
val &= val >> 31;
val += 127;
return val;
}
Now I know in some cases one of these methods might be faster than the other, and vise-versa but in general is it worth it to use the branchless technique seeing as it doesn’t really matter to me which I use, they both will work just fine in my case?
A little background on the microcontroller, it’s a ARM based microcontroller running at 90 MIPS with a 3 stage pipeline, fetch, decode and execute and it seems to have some sort of branch predictor but I couldn’t dig up details.
ARM code (GCC 4.6.3 with
-O3):Thumb code:
Both are branchless thanks to ARM’s conditional execution design. I will bet you they are essentially comparable in performance.