sign(a)

Started by
3 comments, last by fastcall22 9 years, 7 months ago
I just discovered that sign() is slow,and that it returns an int
So is there shorter faster code to do something like this?
float3 a;
if (a.x>0.0f){ a.x = 1.0f; } else{a.x = -1.0f;}
if (a.y>0.0f){ a.y = 1.0f; } else{a.y = -1.0f;}
if (a.z>0.0f){ a.z = 1.0f; } else{a.z = -1.0f;}

Advertisement

You can try doing: "a = (a > 0) * 2 - 1;" Depending on architecture, that may (or may not) be faster. (We use several different options, depending on platform, for example).

So what are the other options?

Ah, sorry - in our actual scenario, we're operating on the Face semantic (or equivalent), which works differently on different platforms. There are some cases where it's already a bool, so we do less math. On all platforms where it's a float, we either use sign or the above math.

x/abs(x)

x>0?1:-1;

EDIT:
Sprry, didn't see HLSL tag

This topic is closed to new replies.

Advertisement