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