cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

roddomi
Journeyman III

Round to nearest of an unsigned integer

Hi,

How do I use the ROUND_ instruction to do a round-to-nearest of an unsigned integer to a float? Should I do ROUND first and then UTOF or viceversa?

Thank you.

0 Likes
5 Replies
scollange
Journeyman III

Hi Rod,

If your GPU has double-precision support, you can first convert your int to double (no rounding involved), then convert it to the nearest float using d2f.

Unfortunately, there is no int-to-double conversion, but you can emulate it using a subtraction.

Here is some pseudo-code to do this. If you cannot use double-precision, the solution is more involved...

 

// assumes twop53.yx = double(2**53) mov I.y, twop53.y mov I.x, int_to_convert // force exponent 53 dsub tmp, I, twop53 // normalize (subtract 2^53) d2f dest, tmp // convert to float, round-to-nearest even

0 Likes

use the convert_*_rt[nzpe] functions from OpenCL, these provide the functionality you want.
0 Likes

Micah,

I am working at the CAL level.

0 Likes

Then you will want to generate a simple CL kernel and compile to IL to find the correct sequence of IL instructions required to the do conversion correctly.
0 Likes

I did that and I found the IL doesn't use any of the ROUND_* instructions. Instead it uses a macro with some FFB's and CMOV's. Why doesn't it use the ROUND_* instructions?

Thank you.

0 Likes