Created
June 7, 2012 07:19
-
-
Save ashwin/2887145 to your computer and use it in GitHub Desktop.
Reinterpret float as int in CUDA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Using union | |
__device__ int floatAsInt( float fval ) | |
{ | |
union FloatInt { | |
float f; | |
int i; | |
}; | |
FloatInt fi; | |
fi.f = fval; | |
return fi.i; | |
} | |
// 2. Using pointer | |
ival = *( ( int* ) &fval ); | |
// 3. Using CUDA intrinsic function | |
ival = __float_as_int( fval ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment