Beatmup
Beatmup::GL::Float16 Class Reference

16 bit floating point representation. More...

#include <float16.h>

Public Member Functions

 Float16 (float input)
 Encodes a given floating point value in 16 bit. More...
 
 Float16 (uint8_t frac, uint8_t exp)
 Constructs a Float16 number from the fractional part and the exponent values given explicitly. More...
 
 operator float () const
 Converts the encoded value to a floating-point value. More...
 
uint8_t getExp () const
 Returns encoded exponent. More...
 
uint8_t getFrac () const
 Returns encoded fractional part. More...
 

Static Public Member Functions

static std::string encodeGlsl (const std::string &name="encode")
 Returns a GLSL code defining the encoding function from a high precision floating point value into a low precision vec2. More...
 
static std::string decodeGlsl (const std::string &name="decode")
 Returns a GLSL code defining the decoding function from a low precision vec2 to a high precision floating point value. More...
 

Private Attributes

uint8_t frac
 fractional part More...
 
uint8_t exp
 exponent part More...
 

Detailed Description

16 bit floating point representation.

The exponent and the signed fractional part are encoded to 8 bits each. x = frac * 2 ^ exp

Definition at line 31 of file float16.h.

Constructor & Destructor Documentation

◆ Float16() [1/2]

Beatmup::GL::Float16::Float16 ( float  input)
inline

Encodes a given floating point value in 16 bit.

Parameters
inputThe input floating point value

Definition at line 41 of file float16.h.

41  {
42  const int exponent = (int)(std::log2(std::abs(input)) + 1.0f);
43  const float frac = std::ldexp(input, -exponent) * 128.0f + 128.5f;
44  this->frac = frac < 0.0f ? 0 : frac > 255.0f ? 255 : (uint8_t)frac;
45  this->exp = (uint8_t)(exponent + 128);
46  }
uint8_t exp
exponent part
Definition: float16.h:34
uint8_t frac
fractional part
Definition: float16.h:33

◆ Float16() [2/2]

Beatmup::GL::Float16::Float16 ( uint8_t  frac,
uint8_t  exp 
)
inline

Constructs a Float16 number from the fractional part and the exponent values given explicitly.

Parameters
fracThe fractional part
exponentThe exponent

Definition at line 53 of file float16.h.

53 : frac(frac), exp(exp) {}

Member Function Documentation

◆ operator float()

Beatmup::GL::Float16::operator float ( ) const
inline

Converts the encoded value to a floating-point value.

Definition at line 58 of file float16.h.

58  {
59  return std::ldexp(frac / 128.0f - 1.0f, (int)exp - 128);
60  }

◆ getExp()

uint8_t Beatmup::GL::Float16::getExp ( ) const
inline

Returns encoded exponent.

Definition at line 65 of file float16.h.

65  {
66  return exp;
67  }

◆ getFrac()

uint8_t Beatmup::GL::Float16::getFrac ( ) const
inline

Returns encoded fractional part.

Definition at line 72 of file float16.h.

72  {
73  return frac;
74  }

◆ encodeGlsl()

static std::string Beatmup::GL::Float16::encodeGlsl ( const std::string &  name = "encode")
inlinestatic

Returns a GLSL code defining the encoding function from a high precision floating point value into a low precision vec2.

Parameters
nameThe function name

Definition at line 80 of file float16.h.

80  {
81  return "lowp vec2 " + name + R"((highp float value) {
82  highp float e = floor(log2(abs(value)) + 1.0);
83  return vec2(value * exp2(-e - 1.0) + (0.5 + 0.5/255.0), (e + 128.0) / 255.0);
84  })";
85  }
return(jlong) new Beatmup jlong jstring name

◆ decodeGlsl()

static std::string Beatmup::GL::Float16::decodeGlsl ( const std::string &  name = "decode")
inlinestatic

Returns a GLSL code defining the decoding function from a low precision vec2 to a high precision floating point value.

Parameters
nameThe function name

Definition at line 91 of file float16.h.

91  {
92  return "highp float " + name + R"((lowp vec2 pack) {
93  highp float e = floor(pack.y * 255.0 - 127.5);
94  return (floor(pack.x * 255.0) / 128.0 - 1.0) * exp2(e);
95  })";
96  }

Member Data Documentation

◆ frac

uint8_t Beatmup::GL::Float16::frac
private

fractional part

Definition at line 33 of file float16.h.

◆ exp

uint8_t Beatmup::GL::Float16::exp
private

exponent part

Definition at line 34 of file float16.h.


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