half_float.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 uicore
32 {
33  class HalfFloat
34  {
35  public:
36  HalfFloat() : value(0)
37  {
38  }
39 
40  HalfFloat(const HalfFloat &other) : value(other.value)
41  {
42  }
43 
44  HalfFloat(float v) : value(float_to_half(v))
45  {
46  }
47 
49  {
50  value = other.value;
51  return *this;
52  }
53 
54  HalfFloat &operator =(const float v)
55  {
56  return from_float(v);
57  }
58 
59  operator float() const
60  {
61  return to_float();
62  }
63 
64  float to_float() const
65  {
66  return half_to_float(value);
67  }
68 
70  {
71  value = float_to_half(v);
72  return *this;
73  }
74 
76  static float half_to_float_simple(unsigned short hf)
77  {
78  unsigned int float_value = ((hf & 0x8000) << 16) | (((hf & 0x7c00) + 0x1C000) << 13) | ((hf & 0x03FF) << 13);
79  void *ptr = static_cast<void*>(&float_value);
80  return *static_cast<float*>(ptr);
81  }
82 
84  static unsigned short float_to_half_simple(float float_value)
85  {
86  void *ptr = static_cast<void*>(&float_value);
87  unsigned int f = *static_cast<unsigned int*>(ptr);
88  return ((f >> 16) & 0x8000) | ((((f & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) | ((f >> 13) & 0x03ff);
89  }
90 
91  static float half_to_float(unsigned short hf)
92  {
93  unsigned int float_value = mantissa_table[offset_table[hf >> 10] + (hf & 0x3ff)] + exponent_table[hf >> 10];
94  void *ptr = static_cast<void*>(&float_value);
95  return *static_cast<float*>(ptr);
96  }
97 
98  static unsigned short float_to_half(float float_value)
99  {
100  void *ptr = static_cast<void*>(&float_value);
101  unsigned int f = *static_cast<unsigned int*>(ptr);
102  return base_table[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shift_table[(f >> 23) & 0x1ff]);
103  }
104 
105  private:
106  unsigned short value;
107 
108  static unsigned int mantissa_table[2048];
109  static unsigned int exponent_table[64];
110  static unsigned short offset_table[64];
111 
112  static unsigned short base_table[512];
113  static unsigned char shift_table[512];
114  };
115 }
Definition: half_float.h:33
static float half_to_float_simple(unsigned short hf)
Only works for 'normal' half-float values.
Definition: half_float.h:76
HalfFloat & operator=(const HalfFloat &other)
Definition: half_float.h:48
HalfFloat(float v)
Definition: half_float.h:44
HalfFloat()
Definition: half_float.h:36
static unsigned short float_to_half(float float_value)
Definition: half_float.h:98
HalfFloat & from_float(float v)
Definition: half_float.h:69
float to_float() const
Definition: half_float.h:64
static unsigned short float_to_half_simple(float float_value)
Only works for 'normal' half-float values.
Definition: half_float.h:84
HalfFloat(const HalfFloat &other)
Definition: half_float.h:40
static float half_to_float(unsigned short hf)
Definition: half_float.h:91
Definition: Application/application.h:35