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);
58 inline operator float()
const {
59 return std::ldexp(
frac / 128.0f - 1.0f, (
int)
exp - 128);
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);
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);
16 bit floating point representation.
Float16(float input)
Encodes a given floating point value in 16 bit.
Float16(uint8_t frac, uint8_t exp)
Constructs a Float16 number from the fractional part and the exponent values given explicitly.
uint8_t getExp() const
Returns encoded exponent.
uint8_t getFrac() const
Returns encoded fractional part.
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 ...
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 floa...
uint8_t frac
fractional part
return(jlong) new Beatmup jlong jstring name