Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert float to int preserving bit value

Tags:

c++

bit

hlsl

I have a float4 coming into a compute shader, 3 of these floats are really floats but the fourth is 2 uints shifted together, how would i convert the float to uint by preserving the bit sequence instead of the numeric value?

on the c++ side i solved it by creating a uint pointer, filling it with the desired number and passing on the pointer as a float pointer instead. However in hlsl as similar as it is to c/c++ there are no pointers so im stuck here :|

like image 371
Jake Freelander Avatar asked Dec 26 '22 23:12

Jake Freelander


2 Answers

In HLSL you should be able to do the following (assuming the value you are after is in f4.w)

uint ui = asuint( f4.w );
uint ui1 = ui & 0xffff;
uint ui2 = ui >> 16;

Basically it looks like the asuint intrinsic is your friend :)

like image 55
Goz Avatar answered Jan 04 '23 22:01

Goz


You could use a union.

float f;  // you float value is here

union X  
{  
    float f;  
    short int a[2];  
} x;  
x.f = f;

int i1 = x.a[0];  // these are your ints  
int i2 = x.a[1];
like image 31
Anon Mail Avatar answered Jan 04 '23 22:01

Anon Mail