half_ Class Reference

#include <image.h>

Class Description

Half precision (16 bit) float class.

Supports fast conversion between float and half precision.

Format is compatible with openEXRs half precision type. 1 sign bit, 5 exponent bits (bias = 15), 10 fraction bits.

Definition at line 29 of file image.h.

Public Member Functions

 half_ ()
 
 half_ (float f)
 
 operator float () const
 
half_operator= (float f)
 

Constructor & Destructor Documentation

half_ ( )
inline

Definition at line 40 of file image.h.

40 {};
half_ ( float  f)
inline

Definition at line 41 of file image.h.

42  {
43  unsigned int x = *(unsigned int *)&f;
44  unsigned int frac = (x >> 13) & 0x03ff;
45  int exp = (int)((x >> 23) & 0xff) - 127;
46 
47  m_bits = (x >> 16) & 0x8000; // extract the sign bit
48 
49  if (exp > 16) { // handle overflows/underflows
50  exp = 16;
51  frac = 0x03ff;
52  } else if (exp <= -15) {
53  frac = 0;
54  exp = -15;
55  }
56 
57  m_bits |= ((exp + 15) << 10) | frac;
58  }
unsigned int(APIENTRYP PFNGLXGETAGPOFFSETMESAPROC)(const void *pointer)
Definition: GLee.h:10762
GLenum GLint x
Definition: GLee.h:876
GLclampf f
Definition: GLee.h:9303

Member Function Documentation

operator float ( ) const
inline

Definition at line 63 of file image.h.

64  {
65  int exp = (m_bits >> 10) & 0x1f;
66  unsigned int f = (m_bits & 0x8000) << 16; // extract the sign bit
67  unsigned int frac = (m_bits & 0x03ff) << 13;
68 
69  if (exp == 0x1f) { // convert 16-bit FP inf/NaN to 32-bit inf/NaN value
70  exp = 0xff - ExponentBiasDelta;
71  } else if (exp == 0) {
72  exp = -ExponentBiasDelta; // convert 16-bit FP zero/denorm to 32-bit zero/denorm value
73  }
74 
75  f |= ((exp + ExponentBiasDelta) << 23) | frac;
76  return *(float *)&f;
77  }
#define ExponentBiasDelta
Definition: image.h:62
GLclampf f
Definition: GLee.h:9303
half_& operator= ( float  f)
inline

Definition at line 79 of file image.h.

80  {
81  *this = half_ (f);
82  return *this;
83  }
GLclampf f
Definition: GLee.h:9303

The documentation for this class was generated from the following file: