-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhalf.h
More file actions
313 lines (252 loc) · 7.91 KB
/
Copy pathhalf.h
File metadata and controls
313 lines (252 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#pragma once
#include <cstdint>
#include <cmath>
#include <algorithm>
namespace impl {
template<class T> inline T clamp(T v, T vmin, T vmax) { return std::min<T>(std::max<T>(v, vmin), vmax); }
template<class T> inline T clamp01(T v) { return clamp(v, T(0), T(1)); }
template<class T> inline T clamp11(T v) { return clamp(v, T(-1), T(1)); }
template<class T> inline T sign(T v) { return v < T(0) ? T(-1) : T(1); }
// thanks: https://gist.github.com/rygorous/2156668
typedef unsigned int uint;
union FP32
{
uint u;
float f;
struct
{
uint Mantissa : 23;
uint Exponent : 8;
uint Sign : 1;
};
};
union FP16
{
unsigned short u;
struct
{
uint Mantissa : 10;
uint Exponent : 5;
uint Sign : 1;
};
};
// Original ISPC reference version; this always rounds ties up.
inline FP16 float_to_half_full(FP32 f)
{
FP16 o = { 0 };
// Based on ISPC reference code (with minor modifications)
if (f.Exponent == 0) // Signed zero/denormal (which will underflow)
o.Exponent = 0;
else if (f.Exponent == 255) // Inf or NaN (all exponent bits set)
{
o.Exponent = 31;
o.Mantissa = f.Mantissa ? 0x200 : 0; // NaN->qNaN and Inf->Inf
}
else // Normalized number
{
// Exponent unbias the single, then bias the halfp
int newexp = f.Exponent - 127 + 15;
if (newexp >= 31) // Overflow, return signed infinity
o.Exponent = 31;
else if (newexp <= 0) // Underflow
{
if ((14 - newexp) <= 24) // Mantissa might be non-zero
{
uint mant = f.Mantissa | 0x800000; // Hidden 1 bit
o.Mantissa = mant >> (14 - newexp);
if ((mant >> (13 - newexp)) & 1) // Check for rounding
o.u++; // Round, might overflow into exp bit, but this is OK
}
}
else
{
o.Exponent = newexp;
o.Mantissa = f.Mantissa >> 13;
if (f.Mantissa & 0x1000) // Check for rounding
o.u++; // Round, might overflow to inf, this is OK
}
}
o.Sign = f.Sign;
return o;
}
inline FP32 half_to_float(FP16 h)
{
static const FP32 magic = { 113 << 23 };
static const uint shifted_exp = 0x7c00 << 13; // exponent mask after shift
FP32 o;
o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits
uint exp = shifted_exp & o.u; // just the exponent
o.u += (127 - 15) << 23; // exponent adjust
// handle exponent special cases
if (exp == shifted_exp) // Inf/NaN?
o.u += (128 - 16) << 23; // extra exp adjust
else if (exp == 0) // Zero/Denormal?
{
o.u += 1 << 23; // extra exp adjust
o.f -= magic.f; // renormalize
}
o.u |= (h.u & 0x8000) << 16; // sign bit
return o;
}
}
struct half
{
uint16_t value = 0;
half() {}
half(float v)
{
value = (const uint16_t&)float_to_half_full((impl::FP32&)v);
}
half& operator=(float v)
{
*this = half(v);
return *this;
}
float to_float() const
{
return (const float&)half_to_float((impl::FP16&)value);
}
operator float() const { return to_float(); }
static half zero() { return half(0.0f); }
static half one() { return half(1.0f); }
};
// -1.0f - 1.0f <-> -127 - 127
struct snorm8
{
static constexpr float C = float(0x7f);
static constexpr float R = 1.0f / float(0x7f);
int8_t value = 0;
snorm8() {}
snorm8(float v) : value(int8_t(impl::clamp11(v)* C)) {}
snorm8& operator=(float v)
{
*this = snorm8(v);
return *this;
}
float to_float() const { return (float)value * R; }
operator float() const { return to_float(); }
static snorm8 zero() { return snorm8(0.0f); }
static snorm8 one() { return snorm8(1.0f); }
};
// 0.0f - 1.0f <-> 0 - 255
struct unorm8
{
static constexpr float C = float(0xff);
static constexpr float R = 1.0f / float(0xff);
uint8_t value = 0;
unorm8() {}
unorm8(float v) : value(uint8_t(impl::clamp01(v)* C)) {}
unorm8& operator=(float v)
{
*this = unorm8(v);
return *this;
}
float to_float() const { return (float)value * R; }
operator float() const { return to_float(); }
static unorm8 zero() { return unorm8(0.0f); }
static unorm8 one() { return unorm8(1.0f); }
};
// -1.0f - 1.0f <-> 0 - 255
// for audio sample
struct unorm8n
{
static constexpr float C = float(0xff);
static constexpr float R = 1.0f / float(0xff);
uint8_t value = 0;
unorm8n() {}
unorm8n(float v) : value(uint8_t((impl::clamp11(v) * 0.5f + 0.5f)* C)) {}
unorm8n& operator=(float v)
{
*this = unorm8n(v);
return *this;
}
float to_float() const { return (float)value * R * 2.0f - 1.0f; }
operator float() const { return to_float(); }
static unorm8n zero() { return unorm8n(0.0f); }
static unorm8n one() { return unorm8n(1.0f); }
};
// -1.0f - 1.0f <-> -32767 - 32767
struct snorm16
{
static constexpr float C = float(0x7fff);
static constexpr float R = 1.0f / float(0x7fff);
int16_t value = 0;
snorm16() {}
snorm16(float v) : value(int16_t(impl::clamp11(v)* C)) {}
snorm16& operator=(float v)
{
*this = snorm16(v);
return *this;
}
float to_float() const { return (float)value * R; }
operator float() const { return to_float(); }
static snorm16 zero() { return snorm16(0.0f); }
static snorm16 one() { return snorm16(1.0f); }
};
// 0.0f - 1.0f <-> 0 - 65535
struct unorm16
{
static constexpr float C = float(0xffff);
static constexpr float R = 1.0f / float(0xffff);
uint16_t value = 0;
unorm16() {}
unorm16(float v) : value(uint16_t(impl::clamp01(v)* C)) {}
unorm16& operator=(float v)
{
*this = unorm16(v);
return *this;
}
float to_float() const { return (float)value * R; }
operator float() const { return to_float(); }
static unorm16 zero() { return unorm16(0.0f); }
static unorm16 one() { return unorm16(1.0f); }
};
// -1.0f - 1.0f <-> -2147483647 - 2147483647
// for audio sample
struct snorm24
{
static constexpr double C = double(0x7fffffff);
static constexpr double R = 1.0 / double(0x7fffffff);
uint8_t value[3]{};
snorm24() {}
snorm24(float v)
{
// store upper 24 bits
int i32 = int((double)impl::clamp11(v) * C);
value[0] = uint8_t((i32 & 0x0000ff00) >> 8);
value[1] = uint8_t((i32 & 0x00ff0000) >> 16);
value[2] = uint8_t((i32 & 0xff000000) >> 24);
}
snorm24& operator=(float v)
{
*this = snorm24(v);
return *this;
}
float to_float() const
{
int i32 = (value[0] << 8) | (value[1] << 16) | (value[2] << 24);
return float((double)i32 * R);
}
operator float() const { return to_float(); }
static snorm24 zero() { return snorm24(0.0f); }
static snorm24 one() { return snorm24(1.0f); }
};
// -1.0f - 1.0f <-> -2147483647 - 2147483647
// for audio sample
struct snorm32
{
static constexpr double C = double(0x7fffffff);
static constexpr double R = 1.0 / double(0x7fffffff);
int value = 0;
snorm32() {}
snorm32(float v) : value(int((double)impl::clamp11(v)* C)) {}
snorm32& operator=(float v)
{
*this = snorm32(v);
return *this;
}
float to_float() const { return float((double)value * R); }
operator float() const { return to_float(); }
static snorm32 zero() { return snorm32(0.0f); }
static snorm32 one() { return snorm32(1.0f); }
};