Copy float sign bit?


I’m reading a variable from global mem, and multiplying it with itself to raise it one power. I’d like to keep the signedness of the 2nd power number the same as the original number - Can I copy the sign bit of the input number somehow and apply it to the new number? This is what I’m currently doing:

numPowSum += tmpNum * tmpNum  * ((tmpNum < 0) ? -1 : 1);

I’d like to see if there’s any benefit to getting rid of the branching - A hunch says that it might be. Right now I’m doing a check + multiplication, a blind copy should be simpler. Might take more registers though. Any thoughts?


CUDA supports the C99 function copysignf().

Awesome answer - it couldn’t be shorter, yet it is a smorgasbord of info.

I can’t take credit, that exact line arrived in my email and I just reposted it for your benefit.

Just curious, did this function work for you? It doesn’t seem to actually do anything for me with CUDA 2.2 running on Debian on a Mac Pro, with an Apple firmware 8800 GT. I tried all permutations of swapping the arguments and copysignf() vs. copysign().



Never mind; I was calling it wrong; you need to use the return value, i.e. y= copysignf(y,x) copies the sign of x to y. The C99 docs explain these sorts of functions. Sorry for the noise.