Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast int to float conversion

I am doing computations in Cuda using floats. Because we do not have enough memory on the GPU, we store the raw data as uint16_t and int16_t on the GPU. Thus, before I use this data I have to convert it to floats. The number of ints is not that large (approximately 12k of uint16_t and the same number of int16_t). Profiling showed that converting the numbers takes a considerable amount of time (approx. 5-10%). The rest of the calculation cannot be optimized more. Thus my 3+1 questions are:

  • What is the fastest way to convert ints to floats.
  • Is there a substantial difference when converting int16_t or uint16_t.
  • Is there a substantial difference when converting larger int types, e.g. int32 or int64.
  • Why are all questions on SO about converting floats to ints. Is this something one usually does not do?
like image 268
tommsch Avatar asked Oct 15 '25 08:10

tommsch


1 Answers

  • What is the fastest way to convert ints to floats.

Simple assignment. There are hardware type conversion instructions which the CUDA compiler will emit automatically without you doing anything. Hardware conversion includes the correct IEEE rounding modes.

  • Is there a substantial difference when converting int16_t or uint16_t.

No.

  • Is there a substantial difference when converting larger int types, e.g. int32 or int64.

No.Yes. The instruction throughput for type conversion instructions is documented. 32 bit and 16 bit integer to float conversion instructions have the same throughput. 64 bit conversion instructions are considerably slower than 16 and 32 bit conversion instructions on most architectures.

  • Why are all questions on SO about converting floats to ints. Is this something one usually does not do?

Because many people don't get the difference between float and int types, and why they loose precision, when they convert their float or double type to an int type.
That's nothing you have to worry about in your situation.

like image 118
πάντα ῥεῖ Avatar answered Oct 16 '25 20:10

πάντα ῥεῖ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!