-
Notifications
You must be signed in to change notification settings - Fork 0
/
FF.cpp
361 lines (314 loc) · 10.9 KB
/
FF.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "FF.h"
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <type_traits>
namespace FF {
template <class T>
using FI = FloatInfo<T>;
// Half precision
template <>
const unsigned FI<hex_half>::mantissa_bits = 10;
template <>
const unsigned FI<hex_half>::exponent_bits = 5;
template <>
const unsigned FI<hex_half>::sign_shift = 15;
template <>
const uint16_t FI<hex_half>::mantissa_mask = 0x3FF;
template <>
const uint16_t FI<hex_half>::exponent_mask = 0x7C00;
template <>
const uint16_t FI<hex_half>::sign_mask = 0x8000;
template <>
const int FI<hex_half>::exp_bias = -15;
// Single precision
template <>
const unsigned FI<hex_single>::mantissa_bits = 23;
template <>
const unsigned FI<hex_single>::exponent_bits = 8;
template <>
const unsigned FI<hex_single>::sign_shift = 31;
template <>
const uint32_t FI<hex_single>::mantissa_mask = 0x007fffff;
template <>
const uint32_t FI<hex_single>::exponent_mask = 0x7f800000;
template <>
const uint32_t FI<hex_single>::sign_mask = 0x80000000;
template <>
const int FI<hex_single>::exp_bias = -127;
// Double precision
template <>
const unsigned FI<hex_double>::mantissa_bits = 52;
template <>
const unsigned FI<hex_double>::exponent_bits = 11;
template <>
const unsigned FI<hex_double>::sign_shift = 63;
template <>
const uint64_t FI<hex_double>::mantissa_mask = 0xfffffffffffff;
template <>
const uint64_t FI<hex_double>::exponent_mask = 0x7ff0000000000000;
template <>
const uint64_t FI<hex_double>::sign_mask = 0x8000000000000000;
template <>
const int FI<hex_double>::exp_bias = -1023;
}
namespace {
template <typename U, typename T>
inline U bitcast(const T in) {
static_assert(sizeof(T) == sizeof(U), "Sizes must match");
static_assert(std::is_trivially_copyable<T>::value,
"Source type not trivially copyable");
static_assert(std::is_trivially_copyable<U>::value,
"Destination type not trivially copyable");
U out;
std::memcpy(&out, &in, sizeof(T));
return out;
}
template <typename T>
inline std::ostream& dumpFloat(std::ostream& os, T f) {
const auto default_precision{std::cout.precision()};
constexpr auto max_precision{std::numeric_limits<float>::digits10 + 1};
os << "Float: " << std::defaultfloat << std::setprecision(max_precision) << f;
os << ", " << std::hexfloat << f;
os << ", " << std::scientific << f;
os << std::defaultfloat << std::setprecision(default_precision);
return os;
}
template <>
inline std::ostream& dumpFloat(std::ostream& os, double f) {
const auto default_precision{std::cout.precision()};
constexpr auto max_precision{std::numeric_limits<double>::digits10 + 1};
os << "Float: " << std::defaultfloat << std::setprecision(max_precision) << f;
os << ", " << std::hexfloat << f;
os << ", " << std::scientific << f;
os << std::defaultfloat << std::setprecision(default_precision);
return os;
}
float convertHalfToFloat(FF::hex_half half) {
using FI_H = FF::FloatInfo<FF::hex_half>;
using FI_S = FF::FloatInfo<FF::hex_single>;
if (FI_H(half).isInf()) {
uint32_t bits = (FI_H::sign_mask & half) << 16;
bits |= FI_S::exponent_mask;
return bitcast<float>(bits);
} else if (FI_H(half).isNan()) {
return NAN;
} else if (FI_H(half).isZero()) {
const uint32_t bits = (FI_H::sign_mask & half) << 16;
return bitcast<float>(bits);
}
const uint16_t abs_bits = ~FI_H::sign_mask & half;
uint16_t mantissa_bits = abs_bits & FI_H::mantissa_mask;
uint16_t exponent_bits = abs_bits >> FI_H::mantissa_bits;
constexpr int bias_diff = FI_H::exp_bias - FI_S::exp_bias;
const uint16_t exp_lsb = 1 << FI_H::mantissa_bits;
if (exponent_bits == 0) {
int16_t e = -1;
do {
e++;
mantissa_bits <<= 1;
} while ((mantissa_bits & exp_lsb) == 0);
mantissa_bits &= FI_H::mantissa_mask;
exponent_bits = bias_diff - e;
} else {
exponent_bits += bias_diff;
}
uint32_t single_bits = exponent_bits << FI_S::mantissa_bits;
single_bits |= (FI_H::sign_mask & half) << 16;
constexpr uint32_t mantissa_shift = FI_S::mantissa_bits - FI_H::mantissa_bits;
single_bits |= uint32_t(mantissa_bits) << mantissa_shift;
return bitcast<float>(single_bits);
}
template <typename T>
struct Converter final {
using F = typename FF::NativeFloat<T>::NativeType;
static F HexToFloat(T x) {
return bitcast<F>(x);
}
};
template <>
struct Converter<FF::hex_half> final {
static float HexToFloat(FF::hex_half x) {
return convertHalfToFloat(x);
}
};
} // namespace anonymous
namespace FF {
template <class T>
using FI = FloatInfo<T>;
template <class T>
bool FloatInfo<T>::isDenorm() const {
return (0 == (hex_val.hex & FI<T>::exponent_mask)) &&
(hex_val.hex & FI<T>::mantissa_mask);
}
template <class T>
bool FloatInfo<T>::isInf() const {
return (hex_val.hex & ~FI<T>::sign_mask) == FI<T>::exponent_mask;
}
template <class T>
bool FloatInfo<T>::isNan() const {
return (hex_val.hex & FI<T>::mantissa_mask) &&
((hex_val.hex & FI<T>::exponent_mask) == FI<T>::exponent_mask);
}
template <class T>
bool FloatInfo<T>::isZero() const {
return (hex_val.hex == FI<T>::sign_mask) || (hex_val.hex == 0);
}
template <>
FloatInfo<hex_half>::FloatInfo(float single_precision) {
float single_abs = std::fabs(single_precision);
const uint32_t single_bits = *reinterpret_cast<uint32_t*>(&single_abs);
if (std::isinf(single_precision)) {
hex_val.hex = FI<hex_half>::exponent_mask;
} else if (std::isnan(single_precision)) {
hex_val.hex = FI<hex_half>::exponent_mask | FI<hex_half>::mantissa_mask;
} else {
int exp_unbiased = std::ilogb(single_abs);
if (exp_unbiased < -14) {
constexpr const auto mant_shift =
FI<hex_single>::mantissa_bits - FI<hex_half>::mantissa_bits;
uint32_t mant_bits =
(single_bits & FI<hex_single>::mantissa_mask) >> mant_shift;
// Implicit dropped bit
mant_bits |= uint32_t(1) << FI<hex_half>::mantissa_bits;
const unsigned exp_shift =
std::abs(exp_unbiased - FI<hex_half>::exp_bias) + 1;
if (exp_shift > FI<hex_half>::mantissa_bits) {
// 2^-24
hex_val.hex = 0; // return
} else {
hex_val.hex = mant_bits >> exp_shift;
const uint32_t MSB_dropped_bit = uint32_t(1) << (exp_shift - 1);
const bool dropped_bit_set = 0 != (mant_bits & MSB_dropped_bit);
if (dropped_bit_set) {
const bool RTE = 0 == (mant_bits & (MSB_dropped_bit - 1));
if (!RTE) {
hex_val.hex++; // Round away from zero
} else if (hex_val.hex & 1) {
// lsb we're keeping is set, round up
hex_val.hex++;
}
}
}
} else if (single_abs > 65519.0f) {
// 65504 is max value half can hold, but RTE limit 65519
hex_val.hex = FI<hex_half>::exponent_mask;
} else {
uint32_t exponent_bits = (single_bits & FI<hex_single>::exponent_mask) >>
FI<hex_single>::mantissa_bits;
exponent_bits += FI<hex_single>::exp_bias;
hex_val.hex = (exponent_bits - FI<hex_half>::exp_bias)
<< FI<hex_half>::mantissa_bits;
constexpr const auto shift =
FI<hex_single>::mantissa_bits - FI<hex_half>::mantissa_bits;
const uint32_t mant_bits =
(single_bits & FI<hex_single>::mantissa_mask) >> shift;
hex_val.hex |= mant_bits; // RTZ value
/*
RTE Rounding - Round To Nearest Even
* If first lost mantissa bit from isn't set, round down towards zero.
* If first lost mantissa bit is set:
* If at least one other bit set, Round up
* If no other bits set -> then round to even:
* Even if first non-dropped bit is set, then we round up
*/
constexpr const uint32_t MSB_dropped_bit = uint32_t(1) << (shift - 1);
const bool dropped_bit_set = 0 != (single_bits & MSB_dropped_bit);
if (dropped_bit_set) {
const bool RTE = 0 == (single_bits & (MSB_dropped_bit - 1));
if (!RTE) {
hex_val.hex++; // Round away from zero
} else if (mant_bits & 1) {
// lsb we're keeping is set, round up
hex_val.hex++;
}
}
}
}
if (std::signbit(single_precision)) {
hex_val.hex |= FI<hex_half>::sign_mask;
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const FI<T>& fi) {
static_assert(std::is_same<T, hex_double>::value ||
std::is_same<T, hex_single>::value ||
std::is_same<T, hex_half>::value,
"bad template instantiation");
if (std::is_same<T, hex_double>::value) {
os << "64-bit double precision";
} else if (std::is_same<T, hex_single>::value) {
os << "32-bit single precision";
} else {
os << "16-bit half precision";
}
if (fi.isDenorm()) {
os << " denormal\n";
} else if (fi.isInf()) {
os << " infinity\n";
} else if (fi.isNan()) {
os << " NaN\n";
} else {
os << "\n";
}
os << "Hex: 0x" << std::hex << fi.hex_val.hex << "\n";
os << "Bits: ";
os << std::bitset<1>(fi.getSignBits()) << " ";
os << std::bitset<FI<T>::exponent_bits>(fi.getExponentBits()) << " ";
os << std::bitset<FI<T>::mantissa_bits>(fi.getMantissaBits()) << "\n";
if (std::is_same<T, hex_half>::value) {
using FloatType = typename NativeFloat<T>::NativeType;
const FloatType printer = Converter<T>::HexToFloat(fi.hex_val.hex);
return dumpFloat(os, printer);
} else {
return dumpFloat(os, fi.hex_val.f);
}
}
template <typename T>
typename NativeFloat<T>::NativeType parseString(const std::string&& str) {
// Check for 0x hex digits
const bool isHex =
(str.size() > 2) && (str.compare(0, 2, "0x") == 0) &&
(str.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos);
using FloatType = typename NativeFloat<T>::NativeType;
FloatType native;
if (isHex) {
const T hex_value = static_cast<T>(std::strtoul(str.c_str(), 0, 16));
native = Converter<T>::HexToFloat(hex_value);
} else {
try {
native = static_cast<FloatType>(std::stod(str));
} catch (std::invalid_argument&) {
std::cerr << "Invalid argument: " << str << "\n";
return -1;
}
}
return native;
}
// instantiates all template types
void printAll(const double x) {
FF::FloatInfo<FF::hex_double> double_info(x);
std::cout << double_info << "\n\n";
const float downcast = static_cast<float>(x);
FF::FloatInfo<FF::hex_single> single_info(downcast);
std::cout << single_info << "\n\n";
FF::FloatInfo<FF::hex_half> half_info(downcast);
std::cout << half_info << "\n";
}
void parseHalf(const char* str) {
FF::NativeFloat<uint16_t>::NativeType native = FF::parseString<uint16_t>(str);
FF::printAll(native);
}
void parseSingle(const char* str) {
FF::NativeFloat<uint32_t>::NativeType native = FF::parseString<uint32_t>(str);
FF::printAll(native);
}
void parseDouble(const char* str) {
FF::NativeFloat<uint64_t>::NativeType native = FF::parseString<uint64_t>(str);
FF::printAll(native);
}
} // namespace FF