half_float.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2020 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 namespace clan
32 {
35 
36  class HalfFloat
37  {
38  public:
39  HalfFloat() : value(0)
40  {
41  }
42 
43  HalfFloat(const HalfFloat &other) = default;
44 
45  HalfFloat(float v) : value(float_to_half(v))
46  {
47  }
48 
49  HalfFloat &operator =(const HalfFloat &other) = default;
50 
51  HalfFloat &operator =(const float v)
52  {
53  return from_float(v);
54  }
55 
56  operator float() const
57  {
58  return to_float();
59  }
60 
61  float to_float() const
62  {
63  return half_to_float(value);
64  }
65 
67  {
68  value = float_to_half(v);
69  return *this;
70  }
71 
73  static float half_to_float_simple(unsigned short hf)
74  {
75  unsigned int float_value = ((hf & 0x8000) << 16) | (((hf & 0x7c00) + 0x1C000) << 13) | ((hf & 0x03FF) << 13);
76  void *ptr = static_cast<void*>(&float_value);
77  return *static_cast<float*>(ptr);
78  }
79 
81  static unsigned short float_to_half_simple(float float_value)
82  {
83  void *ptr = static_cast<void*>(&float_value);
84  unsigned int f = *static_cast<unsigned int*>(ptr);
85  return ((f >> 16) & 0x8000) | ((((f & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) | ((f >> 13) & 0x03ff);
86  }
87 
88  static float half_to_float(unsigned short hf)
89  {
90  unsigned int float_value = mantissa_table[offset_table[hf >> 10] + (hf & 0x3ff)] + exponent_table[hf >> 10];
91  void *ptr = static_cast<void*>(&float_value);
92  return *static_cast<float*>(ptr);
93  }
94 
95  static unsigned short float_to_half(float float_value)
96  {
97  void *ptr = static_cast<void*>(&float_value);
98  unsigned int f = *static_cast<unsigned int*>(ptr);
99  return base_table[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shift_table[(f >> 23) & 0x1ff]);
100  }
101 
102  private:
103  unsigned short value;
104 
105  static unsigned int mantissa_table[2048];
106  static unsigned int exponent_table[64];
107  static unsigned short offset_table[64];
108 
109  static unsigned short base_table[512];
110  static unsigned char shift_table[512];
111  };
112 
114 }
Definition: clanapp.h:35
HalfFloat(float v)
Definition: half_float.h:45
HalfFloat & from_float(float v)
Definition: half_float.h:66
float to_float() const
Definition: half_float.h:61
HalfFloat()
Definition: half_float.h:39
HalfFloat & operator=(const HalfFloat &other)=default
static unsigned short float_to_half_simple(float float_value)
Only works for &#39;normal&#39; half-float values.
Definition: half_float.h:81
static float half_to_float(unsigned short hf)
Definition: half_float.h:88
static unsigned short float_to_half(float float_value)
Definition: half_float.h:95
static float half_to_float_simple(unsigned short hf)
Only works for &#39;normal&#39; half-float values.
Definition: half_float.h:73
Definition: half_float.h:36