Beatmup
float16.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <string>
21 #include <cstdint>
22 #include <cmath>
23 
24 namespace Beatmup {
25  namespace GL {
26  /**
27  16 bit floating point representation.
28  The exponent and the signed fractional part are encoded to 8 bits each.
29  x = frac * 2 ^ exp
30  */
31  class Float16 {
32  private:
33  uint8_t frac; //!< fractional part
34  uint8_t exp; //!< exponent part
35 
36  public:
37  /**
38  Encodes a given floating point value in 16 bit.
39  \param input The input floating point value
40  */
41  inline Float16(float input) {
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  }
47 
48  /**
49  Constructs a Float16 number from the fractional part and the exponent values given explicitly.
50  \param frac The fractional part
51  \param exponent The exponent
52  */
53  inline Float16(uint8_t frac, uint8_t exp): frac(frac), exp(exp) {}
54 
55  /**
56  Converts the encoded value to a floating-point value.
57  */
58  inline operator float() const {
59  return std::ldexp(frac / 128.0f - 1.0f, (int)exp - 128);
60  }
61 
62  /**
63  Returns encoded exponent.
64  */
65  inline uint8_t getExp() const {
66  return exp;
67  }
68 
69  /**
70  Returns encoded fractional part.
71  */
72  inline uint8_t getFrac() const {
73  return frac;
74  }
75 
76  /**
77  Returns a GLSL code defining the encoding function from a high precision floating point value into a low precision vec2.
78  \param name The function name
79  */
80  static inline std::string encodeGlsl(const std::string& name = "encode") {
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  }
86 
87  /**
88  Returns a GLSL code defining the decoding function from a low precision vec2 to a high precision floating point value.
89  \param name The function name
90  */
91  static inline std::string decodeGlsl(const std::string& name = "decode") {
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  }
97  };
98  }
99 }
16 bit floating point representation.
Definition: float16.h:31
Float16(float input)
Encodes a given floating point value in 16 bit.
Definition: float16.h:41
Float16(uint8_t frac, uint8_t exp)
Constructs a Float16 number from the fractional part and the exponent values given explicitly.
Definition: float16.h:53
uint8_t exp
exponent part
Definition: float16.h:34
uint8_t getExp() const
Returns encoded exponent.
Definition: float16.h:65
uint8_t getFrac() const
Returns encoded fractional part.
Definition: float16.h:72
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 ...
Definition: float16.h:80
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...
Definition: float16.h:91
uint8_t frac
fractional part
Definition: float16.h:33
return(jlong) new Beatmup jlong jstring name