These are more interesting for other languages since the JDK does not do a good job of inline/reg-alloc & schedule.
public static final int clampPositive(int a)
{
// return (a >= 0) ? a : 0;
return a & ~(a >> 31);
}
public static final int abs(int a)
{
// return (a >= 0) ? a : -a;
int s = a >> 31;
a ^= s;
a -= s;
return a;
}
public static final int min(int a, int b)
{
// return (a <= b) ? a : b;
a -= b;
a &= (a >> 31);
a += b;
return a;
}
public static final int max(int a, int b)
{
// return (a >= b) ? a : b;
a -= b;
a &= ~(a >> 31);
a += b;
return a;
}
public static final int select(int a, int b, int c, int d)
{
// return (a<b) ?c : d
return (((a-b)>>31) & (c^d))^d;
}
public static final int sgn(int a)
{
// return (a > 0) ? 1 : (a < 0) ? -1 : 0;
return (-a >>> 31) | (a >> 31);
}