Calculate abs(int) without branching
For this you need someone to teach it to you: if you made it yourself, then you are a very good Comp-Sci, and you should send your CV to Google ASAP.
Without branching O_o?
Yes, without using any “if ( a < 0 )...". To do that, you need to refresh how Two's Complement works, then come back.
What we really need to focus on is that, given a signed int A, the negative of that number is: B = ~A + 1. BUT, we are trying to calculate the Absolute Value, not the negative. So, something like:
RESULT =
(INPUT, if positive, or NEGATE_INPUT, if negative)
+
(0, if positive, or 1, if negative)
Does it makes sense to you? To me it didn't for the first 10 minutes.
What do we need?
We need, given the integer in input, to calculate 2 values:
- A way to "optionally negate" the input
- A variable carrying
0if the input is positive,1otherwise
Look what we have here: the XOR
Properties of XOR are:
A ^ 0 = AA ^ 1 = ~AA ^ A = 0A ^ B = B ^ A(commutative)
The first two properties are key here: if we could only generate a variable from the input that contains 0 if positive and 1 if negative, we would have half hack done.
Ladies and Gentleman, all shift please
Now, let's see some shifting in action. If A is a Positive number, then:
A = INPUT >> 31 => 0x00000000 => 0 B = -A => -0x00000000 => 0
While if A is a Negative number, then:
A = INPUT >> 31 => 0xFFFFFFFF => -1 B = -A => 0x00000001 => 1
Putting all together
So, this means that we can calculate the absolute value using the new variables we have produced, A and B. Here is how:
#include <stdio.h>
int abs(const int value) {
int A = value >> 31; // 0x00000000 if Positive, 0xFFFFFFFF if Negative
int B = -A; // 0x00000000 if Positive, 0x00000001 if Negative
return (value ^ A) + B; // value ^ A = value if Positive, value ^ A = ~value if Negative
}
int main(int argc, char** argv)
{
int input;
// Check the Input
if ( argc == 2 ) input = atoi(argv[1]); else return (1);
printf("abs(%d) = %d\n\n", input, abs(input));
}
Who showed me this hack? eh eh eh!

about 1 month ago
A JMP is still a branch instruction, tho.
Well, sorry for such a bad joke, the compiler is going to optimize that function out, and this was not even the point anyways
I just wanted to say: go on with these posts, some memory refreshing is always good.
about 1 month ago
You go really deep technical for my taste with this, but I can easily recall that JMP is an UNCONDITIONAL JUMP.
This means that no CONDITION is verified, and the Pipeline can keep prefetching.
I forgot to say that this was the MAIN reason for avoiding branching
about 1 month ago
Yes, you’re perfectly right, I was just playing p[r]icky with the definition of “branch instructions”, the smiley be my witness.
Just for completeness an UNCONDITIONAL JUMP, is also called UNCONDITIONAL BRANCH sometimes. For some, branch and condition are not the same thing. Abuse of language? Maybe, but it was functional to my joke, if nothing else (oh, it’s raining puns…).
All the best, Antonio.
about 1 month ago
I should have said “what was the reason for avoiding branching and which kind of branching in particular”.
You are right: Branch and Condition are not the same thing. But I’m NOT assembly-oriented enough to remember to do the difference up-front.