Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by jk1 ( 1 year ago )
// Define the bit allocation and shift values for packing
int Field1Bits = 14; // Bits allocated for Field 1
int Field2Bits = 11; // Bits allocated for Field 2
int Field1Shift = 0; // Field 1 starts at bit 0
int Field2Shift = Field1Bits; // Field 2 starts after Field 1
// Calculate the maximum integer values representable in each field
int Field1MaxInt = (1 << Field1Bits) - 1; // Max value for Field 1 (e.g., 16383 for 14 bits)
int Field2MaxInt = (1 << Field2Bits) - 1; // Max value for Field 2 (e.g., 2047 for 11 bits)
void storeField1(inout uint packed_data, float field1_value, float scaleFactor)
{
// Clamp and scale the value to fit within the allocated bit range
float clamped_value = clamp(field1_value / scaleFactor, 0.0f, float(Field1MaxInt));
packed_data = bitfieldInsert(packed_data, uint(round(clamped_value)), Field1Shift, Field1Bits);
}
void storeField2(inout uint packed_data, float field2_value)
{
// Clamp the value to [0, 1] and scale to fit within the allocated bit range
field2_value = clamp(field2_value, 0.0f, 1.0f);
uint scaled_value = uint(round(field2_value * Field2MaxInt));
packed_data = bitfieldInsert(packed_data, scaled_value, Field2Shift, Field2Bits);
}
float getField1(uint packed_data, float scaleFactor)
{
const uint field1_value = bitfieldExtract(packed_data, Field1Shift, Field1Bits);
return float(field1_value) * scaleFactor;
}
float getField2(uint packed_data)
{
const uint field2_value = bitfieldExtract(packed_data, Field2Shift, Field2Bits);
return float(field2_value) / Field2MaxInt;
}
Revise this Paste