Graphics Reference
In-Depth Information
have to support 16-bit floating-point arithmetic operations with these
values. Most implementations will convert denormalized numbers and
NaN values to zero.
Converting a Float to a Half-Float
The following routines describe how to convert a single-precision
floating-point number to a half-float value, and vice versa. The
conversion routines are useful when vertex attributes are generated using
single-precision floating-point calculations but then converted to half-
floats before they are used as vertex attributes:
// −15 stored using a single-precision bias of 127
const unsigned int HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP =
0x38000000;
// max exponent value in single precision that will be converted
// to Inf or NaN when stored as a half-float
const unsigned int HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP =
0x47800000;
// 255 is the max exponent biased value
const unsigned int FLOAT_MAX_BIASED_EXP = (0x1F << 23);
const unsigned int HALF_FLOAT_MAX_BIASED_EXP = (0x1F << 10);
typedef unsigned short hfloat;
hfloat
convertFloatToHFloat(float *f)
{
unsigned int x = *(unsigned int *)f;
unsigned int sign = (unsigned short)(x >> 31);
unsigned int mantissa;
unsigned int exp;
hfloat hf;
// get mantissa
mantissa = x & ((1 << 23) − 1);
// get exponent bits
exp = X & FLOAT_MAX_BIASED_EXP;
if (exp >= HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP)
{
// check if the original single-precision float number
// is a NaN
 
 
Search WWH ::




Custom Search