Repository navigation
Expand file tree
/
Copy pathhalfp.cpp
More file actions
261 lines (223 loc) · 5.81 KB
/
Copy pathhalfp.cpp
File metadata and controls
261 lines (223 loc) · 5.81 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
#include <intrin.h>
#include <cassert>
#include <algorithm>
#include "halfp.h"
halfp::halfp(float x)
{
assign(x);
}
halfp::halfp(halfp const & src)
{
m_data.raw = src.m_data.raw;
}
halfp::halfp(data data)
{
m_data = data;
}
halfp::halfp(unsigned short num)
{
#if 0
halfp tmp0;
tmp0.m_data.raw = (15 + 10) << 10;
tmp0.m_data.raw |= num & ((1 << 10) - 1);
tmp0 -= halfp(static_cast<float>(1 << 10));
#if 0
// this needs to be fixed
halfp tmp1;
tmp1.m_data.raw = (15 + 15) << 10;
tmp1.m_data.raw |= (num >> 10) << 5;
tmp0 += tmp1;
#endif
#else
halfp tmp0(static_cast<float>(num));
#endif
m_data = tmp0.m_data;
}
halfp::operator float() const
{
singlep x;
x.raw = (m_data.raw & ~(1 << 15)) << (23 - 10);
x.raw += (127 - 15) << 23;
x.raw |= (m_data.raw & (1 << 15)) << 16;
return x.x;
}
halfp& halfp::operator=(halfp const & src)
{
m_data.raw = src.m_data.raw;
return *this;
}
halfp & halfp::operator=(float x)
{
assign(x);
return *this;
}
halfp & halfp::operator*=(halfp const& x)
{
assign(static_cast<float>(*this) * static_cast<float>(x));
return *this;
}
halfp & halfp::operator-=(halfp const & x)
{
assign(static_cast<float>(*this) - static_cast<float>(x));
return *this;
}
halfp halfp::abs() const
{
halfp dst(*this);
dst.m_data.raw &= (1 << 15) - 1;
return dst;
}
halfp & halfp::operator+=(halfp const & x)
{
assign(static_cast<float>(*this) + static_cast<float>(x));
return *this;
}
halfp halfp::operator-() const
{
halfp dst(*this);
dst.m_data.raw ^= 1 << 15;
return dst;
}
unsigned short halfp::round(int fixed_point) const
{
#if 0
// this works, but is it worth it?
unsigned short res = ((m_data.signed_raw >> 10) & 31) - 15;
unsigned short big = 1 << (res + fixed_point);
// this depends on ALU implementation, if there are no signed shifts then this will work correctly
// if the power is non zero then one will always evaluate to zero, otherwise the extra summation will be subtracted
unsigned short small0 = (m_data.raw & ((1 << 10) - 1)) >> (10 - res - fixed_point);
unsigned short small1 = (m_data.raw & ((1 << 10) - 1)) << (res + fixed_point - 10);
unsigned short carry = ((m_data.raw & ((1 << 10) - 1)) >> (10 - res - fixed_point - 1)) & 1;
res = big + small0 + small1 + carry - (small0 & small1);
return res;
#elif 0
// incorrect for numbers larger than 2^10, but fast otherwise
halfp tmp(static_cast<float>(1 << (10 - fixed_point)) + static_cast<float>(this->abs()));
short res = tmp.m_data.raw & ((1 << 10) - 1);
short sign = m_data.signed_raw >> 15;
return (res ^ sign) - sign;
#elif 0
short res = ((m_data.signed_raw >> 10) & 31) - 15;
short sign = m_data.signed_raw >> 15;
res = (1 << (res + fixed_point)) + ((m_data.raw & ((1 << 10) - 1)) >> (10 - res - fixed_point));
return (res ^ sign) - sign;
#elif 0
halfp tmp0(this->abs()), tmp1;
short exp_diff = ((tmp0.m_data.raw >> 10) & 31) - 15 - 10;
exp_diff = std::max(exp_diff, 0i16);
tmp1.m_data.raw = exp_diff & (1 << 15);
tmp1.m_data.signed_raw >>= exp_diff; // replicate bits for mantissa
unsigned short big = tmp1.m_data.raw;
tmp1.m_data.raw >>= 5; // move into place
tmp1.m_data.raw += 15 << 10; // add bias
tmp0 -= tmp1; // this one can be safely rounded
tmp0 += static_cast<float>(1 << (10 - fixed_point));
unsigned short res = tmp0.m_data.raw & ((1 << 10) - 1);
res += big;
short sign = m_data.signed_raw >> 15;
return (res ^ sign) - sign;
#else
return static_cast<unsigned short>(::round(static_cast<float>(*this)));
#endif
}
halfp halfp::rcp(int order) const
{
halfp dst;
#if 1
dst.m_data.raw = ((15 * 2 - 1) << 10) | 0x3A8;
#else
dst.m_data.mantissa = 936;
dst.m_data.exponent = (15 * 2 - 1) << 10;
#endif
dst.m_data.raw -= m_data.raw & ~(1 << 15);
dst.m_data.raw |= m_data.raw & (1 << 15);
halfp tmp0(dst), tmp1;
switch (order) {
case 0:
case 1:
break;
case 2:
tmp0 *= *this;
tmp0 -= 2.f;
dst *= -tmp0;
break;
case 3:
// unstable for half precision
tmp0 *= *this;
tmp1 = tmp0;
tmp0 -= 3.f;
tmp0 *= tmp1;
tmp0 += 3.f;
dst *= tmp0;
break;
default:
assert(false);
};
return dst;
}
halfp halfp::rsqrt(int order) const
{
halfp dst;
dst.m_data.raw = (22 << 10) | 0x1BE;
dst.m_data.raw -= (m_data.raw & ~(1 << 15)) >> 1;
dst.m_data.raw |= m_data.raw & (1 << 15);
halfp tmp0(dst), tmp1;
switch (order) {
case 0:
case 1:
break;
case 2:
tmp0 *= dst;
tmp0 *= *this;
tmp0 -= 3.f;
dst *= -tmp0;
dst.m_data.raw -= 1 << 10;
break;
case 3:
// doesn't converge exactly due to lack of precision
// but seems stable
tmp0 *= dst;
tmp0 *= *this;
tmp1 = tmp0;
tmp0 *= 0.375f;
tmp0 -= 1.25f;
tmp0 *= tmp1;
tmp0 += 1.875f;
dst *= tmp0;
break;
default:
assert(false);
};
return dst;
}
halfp halfp::exp2() const
{
halfp dst(*this);
dst += 15.f;
dst *= static_cast<float>(1 << 10);
dst.m_data.signed_raw = dst.round();
return dst;
}
halfp halfp::log2() const
{
halfp dst(m_data.raw);
dst *= static_cast<float>(1.f / (1 << 10));
dst -= 15.f;
return dst;
}
void halfp::assign(float x)
{
singlep y;
y.x = x;
#if 1
y.raw -= (127 - 15) << 23;
m_data.raw = (y.raw >> (23 - 10 - 1)) & 1;
m_data.raw += y.raw >> (23 - 10);
m_data.raw |= _bittest(&y.intrin_raw, 31) << 15;
#else
m_data.sign = y.sign;
m_data.mantissa = (y.mantissa >> (23 - 10)) + ((y.mantissa >> (23 - 10 - 1)) & 1);
m_data.exponent = y.exponent - 127 + 15;
#endif
}