2010-03-24 18:12:45 +01:00
|
|
|
#ifndef U_HALF_H
|
|
|
|
#define U_HALF_H
|
|
|
|
|
|
|
|
#include "pipe/p_compiler.h"
|
2010-04-01 15:11:05 +01:00
|
|
|
#include "u_math.h"
|
2010-03-24 18:12:45 +01:00
|
|
|
|
2010-04-01 15:15:28 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 18:12:45 +01:00
|
|
|
extern uint32_t util_half_to_float_mantissa_table[2048];
|
|
|
|
extern uint32_t util_half_to_float_exponent_table[64];
|
|
|
|
extern uint32_t util_half_to_float_offset_table[64];
|
|
|
|
extern uint16_t util_float_to_half_base_table[512];
|
|
|
|
extern uint8_t util_float_to_half_shift_table[512];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that if the half float is a signaling NaN, the x87 FPU will turn
|
|
|
|
* it into a quiet NaN immediately upon loading into a float.
|
|
|
|
*
|
|
|
|
* Additionally, denormals may be flushed to zero.
|
|
|
|
*
|
|
|
|
* To avoid this, use the floatui functions instead of the float ones
|
|
|
|
* when just doing conversion rather than computation on the resulting
|
|
|
|
* floats.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static INLINE uint32_t
|
|
|
|
util_half_to_floatui(half h)
|
|
|
|
{
|
2010-04-02 00:39:08 +02:00
|
|
|
unsigned exp = h >> 10;
|
|
|
|
return util_half_to_float_mantissa_table[util_half_to_float_offset_table[exp] + (h & 0x3ff)] + util_half_to_float_exponent_table[exp];
|
2010-03-24 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE float
|
|
|
|
util_half_to_float(half h)
|
|
|
|
{
|
2010-04-02 00:39:08 +02:00
|
|
|
union fi r;
|
|
|
|
r.ui = util_half_to_floatui(h);
|
|
|
|
return r.f;
|
2010-03-24 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE half
|
|
|
|
util_floatui_to_half(uint32_t v)
|
|
|
|
{
|
2010-04-02 00:39:08 +02:00
|
|
|
unsigned signexp = v >> 23;
|
|
|
|
return util_float_to_half_base_table[signexp] + ((v & 0x007fffff) >> util_float_to_half_shift_table[signexp]);
|
2010-03-24 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE half
|
|
|
|
util_float_to_half(float f)
|
|
|
|
{
|
2010-04-02 00:39:08 +02:00
|
|
|
union fi i;
|
|
|
|
i.f = f;
|
|
|
|
return util_floatui_to_half(i.ui);
|
2010-03-24 18:12:45 +01:00
|
|
|
}
|
|
|
|
|
2010-04-01 15:15:28 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 18:12:45 +01:00
|
|
|
#endif /* U_HALF_H */
|
2010-04-02 00:39:08 +02:00
|
|
|
|