Use this: y= 1/(1+e^(-10000*(x-0.995)))
This will give y = 0 for x <= 0.99
and y=1 for x>= 1
I don't know what SIMD is and there could very well be a better way to do this. But I figured, if you don't want to use a condition you can use a sigmoid function that returns a 0 or a 1 according to your criteria. This function would be your some_operation(x)
.Note that this function will only work for numbers with 2 decimals. That is, an input of 0.99 will return 0 but an input of 0.999 will return 1. Make sure you round your number down to the nearest 2-decimal number before doing the calculation.
I will go through step by step of my thought process below if anyone is interested:
If you want to use a function, and not a logical condition it would have to be continuous which by definition would mean that som of the values would not fit the criteria. But if those values were within a very narrow range, and your steps between numbers were bigger than that narrow range it would work.
So you could use a sigmoid function. (input it into wolfram alpha so see each change)
y = 1/(1+e^(-x))
And shift it one step to the right so it's centered around 1 instead of zero.
y = 1/(1+e^(-(x-1)))
Then you can increase the slope of the function by increasing the weight.
y= 1/(1+e^(-10000*(x-1)))
Now the slope is really, really steep.
But we still get y = 0.5 if we input x=1. So we need to shift the sigmoid a bit to the left.
y= 1/(1+e^(-10000*(x-0.995)))
Now we will get y= 0 if x <= 0.99 and y=1 if x >= 1.
If you want to use a finer resolution you would have to adjust the weight (10000 in this case) and the center point (0.995 in this case).
I just checked the calculation in wolfram alpha and iterated what works. You can use a weight as low as 4000 if you are using only 2 decimals.
I'm sure there is a better way to do this, but this is how I would solve it.
max
/min
instructions, so if you are sure thatx
cannot be between 0 and 1 something likex = x>1?1:x;
should translate to efficient code usingmaxpd
or something like that. – Fringe>
, go the other way around using==
.y = (x == 0 ? 0 : 1)
– Sauveur>
token appears or where there's no explicitif
or?
, the point is if it can be translated to branchless SIMD instructions. – Fringey = (x > 1)? 0:1;
and the question's title are somehow not in line ... – Millicentmillie>= 1
, but the code in the question says> 1
. Which is it? – Radnorshire(x >= 1)? 0:1;
evaluates to0
for alli >= 1
. The title say the opposite. – Millicentmilliey = (int) (bool) x
avoid relational operators? Or can you not do that at all in C? – Newspapermin(x, 1)
orx > 0
) which you've dismissed with no underlying reason, instead accepting an answer which answers the letter but not the spirit of your request. – Stroyx
signed or unsigned? – Hook