1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SRC_FLOAT16_HELPER_H_
16 #define SRC_FLOAT16_HELPER_H_
17 
18 #include <cstdint>
19 
20 namespace amber {
21 namespace float16 {
22 
23 // Convert float |value| whose size is |bits| bits to 32 bits float
24 // based on IEEE-754.
25 //
26 // See https://www.khronos.org/opengl/wiki/Small_Float_Formats
27 // and https://en.wikipedia.org/wiki/IEEE_754.
28 //
29 //    Sign Exponent Mantissa Exponent-Bias
30 // 16    1        5       10            15
31 // 11    0        5        6            15
32 // 10    0        5        5            15
33 // 32    1        8       23           127
34 // 64    1       11       52          1023
35 //
36 // 11 and 10 bits floats are always positive.
37 float HexFloatToFloat(const uint8_t* value, uint8_t bits);
38 
39 // Convert 32 bits float |value| to 16 bits float based on IEEE-754.
40 uint16_t FloatToHexFloat16(const float value);
41 
42 }  // namespace float16
43 }  // namespace amber
44 
45 #endif  // SRC_FLOAT16_HELPER_H_
46