Sign in to follow this  
lomateron

sign(a)

Recommended Posts

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;}

 

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this