-
Notifications
You must be signed in to change notification settings - Fork 11
/
ieee754.hpp
446 lines (370 loc) · 11.1 KB
/
ieee754.hpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifndef __IEEE754_H__
#define __IEEE754_H__
#include <cmath>
#include <limits>
#include <algorithm>
#include <type_traits>
#include "std_overrides.hpp"
#include "smallest_unsigned.hpp"
template<unsigned M, unsigned E, int B = (1 << (E - 1)) - 1 >
class IEEE754 {
// XXX: This actually needs to be private, however I do not know how to make
// The functions at std:: overrides able to access it without anything else does
// (friending everything is very tedious and you cannot friend a namespace)
public:
enum {
MANTISSA_MASK = (1UL << M) - 1,
EXPONENT_MASK = (1UL << E) - 1,
MIN_EXPONENT = -B + 2,
MAX_EXPONENT = B + 1,
BITS = 1 + E + M,
};
typedef typename smallest_unsigned<BITS >::type primitive;
typedef typename std::make_signed<primitive >::type signed_primitive;
primitive mantissa : M;
primitive exponent : E;
primitive sign : 1;
private:
/**
* Build a float from components
*/
inline static IEEE754 from_components(primitive sign, primitive exponent, primitive mantissa) {
IEEE754 result;
result.sign = sign;
result.exponent = exponent;
result.mantissa = mantissa;
return result;
}
/**
* Renormalizes a signed fixed point into a float
*/
template<typename T >
inline static IEEE754 renormalize(T unnormalized, int radix_point) {
IEEE754 result;
result.from_signed(unnormalized, radix_point);
return result;
}
/**
* Renormalizes an unsigned fixed point into a float
*/
template<typename T >
inline static IEEE754 renormalize(T unnormalized, int radix_point, int sign) {
IEEE754 result;
result.sign = sign;
result.from_unsigned(unnormalized, radix_point);
return result;
}
/**
* Returns nan with optional sign and mantissa
*/
inline static IEEE754 nan(primitive sign = 0, primitive mantissa = 1) {
return from_components(sign, EXPONENT_MASK, mantissa);
}
/**
* Returns Infinity
*/
inline static IEEE754 inf(primitive sign = 0) {
return from_components(sign, EXPONENT_MASK, 0);
}
/**
* Shift that allows negative values
*/
template <
typename T,
typename RT = typename std::conditional<(sizeof(T) > sizeof(primitive)), T, primitive >::type
>
static RT shift(T value, int shift_amount) {
if(shift_amount < 0)
return value >> -shift_amount;
if(shift_amount > 0)
return value << shift_amount;
return value;
}
/**
* Computes the real value of the mantissa.
* This adds the implicit 1.xxxx to the mantissa when needed
*/
primitive real_mantissa() const {
return exponent ? mantissa | (1 << M) : (mantissa << 1);
}
/**
* Fills up exponent and mantissa from an unsigned value.
* Sign is left unchanged.
*/
template <typename T >
void from_unsigned(T unsigned_value, int radix_point = 0) {
if(unsigned_value > shift<T >((1 << (M + 1)) - 1, E - radix_point)) {
exponent = EXPONENT_MASK;
mantissa = 0;
} else {
int log2 = std::log2(unsigned_value);
if(radix_point + log2 + 1 < MIN_EXPONENT) {
exponent = 0;
mantissa = shift(unsigned_value, M - (MIN_EXPONENT - radix_point - 1));
} else {
exponent = unsigned_value ? log2 + radix_point + B : 0;
mantissa = shift(unsigned_value, M - log2);
}
}
}
/**
* Fills up sign, exponent and mantissa from an unsigned value.
*/
template <typename T >
void from_signed(T signed_value, int radix_point = 0) {
typedef typename std::make_signed<T >::type signed_T;
typedef typename std::make_unsigned<T >::type unsigned_T;
signed_T forced_signed = signed_value;
sign = (forced_signed < 0);
from_unsigned<unsigned_T >(std::abs(forced_signed), radix_point);
}
/**
* Retrieve the value of this float as an unsigned value
*/
template <typename T >
T to_unsigned(int radix_point = 0) const {
return shift<T >(real_mantissa(), exponent - radix_point - B - M);
}
/**
* Retrieve the value of this float as a signed value
*/
template <typename T >
T to_signed(int radix_point = 0) const {
return (to_unsigned<T >(radix_point) ^ -sign) + sign;
}
public:
// -------------------------- Constructors -------------------------- //
/**
* Default constructor, undefined value.
*/
IEEE754() = default;
/**
* Default copy constructor
*/
IEEE754(const IEEE754 &) = default;
/**
* Conversion from another IEEE floating point object
*/
template <unsigned OM, unsigned OE, int OB >
IEEE754(const IEEE754<OM, OE, OB > &other_ieee754) {
sign = other_ieee754.sign;
exponent = other_ieee754.exponent ? other_ieee754.exponent - OB + B : 0;
mantissa = shift(other_ieee754.mantissa, M - OM);
}
/**
* Conversion from a floating point value
*/
template <
typename T,
typename = typename std::enable_if<std::is_floating_point<T >::value, T >::type
>
IEEE754(T floating_point) {
sign = std::signbit(floating_point);
if(std::isnormal(floating_point)) {
int exp = 0;
primitive man = std::ldexp(std::frexp(floating_point, &exp), M + 1);
if(exp > MAX_EXPONENT) {
exponent = EXPONENT_MASK;
mantissa = 0;
} else if(exp < MIN_EXPONENT) {
exponent = 0;
mantissa = shift(man, exp - MIN_EXPONENT + 1);
} else {
exponent = exp + B - 1;
mantissa = man;
}
} else {
exponent = floating_point == T() ? 0 : EXPONENT_MASK;
mantissa = std::isnan(floating_point);
}
}
/**
* Conversion from a signed integral type
*/
template <
typename T,
typename = typename std::enable_if<!std::is_floating_point<T >::value, T >::type,
typename = typename std::enable_if< std::is_signed<T >::value, T >::type
>
IEEE754(T signed_integral) {
from_signed(signed_integral);
}
/**
* Conversion from an unsigned integral type
*/
template <
typename T,
typename = typename std::enable_if<!std::is_floating_point<T >::value, T >::type,
typename = typename std::enable_if<!std::is_signed<T >::value, T >::type,
typename = typename std::enable_if< std::is_unsigned<T >::value, T >::type
>
IEEE754(T unsigned_integral) {
sign = 0;
from_unsigned(unsigned_integral);
}
// ------------------------- Cast operators ------------------------- //
/**
* Convert to another floating point value
*/
template <
typename T,
typename = typename std::enable_if<std::is_floating_point<T >::value, T >::type
>
operator T() const {
T result;
if(exponent != EXPONENT_MASK) {
result = std::ldexp(real_mantissa() / T(1 << M), exponent - B);
} else {
if(mantissa)
result = std::numeric_limits<T >::quiet_NaN();
else
result = std::numeric_limits<T >::infinity();
}
return std::copysign(result, -sign);
}
/**
* Convert to a signed integer
*/
template <
typename T,
typename = typename std::enable_if<!std::is_floating_point<T >::value, T >::type,
typename = typename std::enable_if< std::is_signed<T >::value, T >::type
>
operator T() const {
return to_signed<T >();
}
/**
* Convert to an unsigned integer
*/
template <
typename T,
typename = typename std::enable_if<!std::is_floating_point<T >::value, T >::type,
typename = typename std::enable_if<!std::is_signed<T >::value, T >::type,
typename = typename std::enable_if< std::is_unsigned<T >::value, T >::type
>
operator T() const {
return to_unsigned<T >();
}
// --------------------------- Arithmetic --------------------------- //
// Unary
friend IEEE754 operator + (const IEEE754 &value) {
return value;
}
friend IEEE754 operator - (const IEEE754 &value) {
return from_components(
value.sign ^ 1,
value.exponent, value.mantissa);
}
// Binary
friend IEEE754 operator + (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return nan();
if(std::isinf(lhs) || std::isinf(rhs)) {
if(std::isinf(lhs) && !std::isinf(rhs))
return lhs;
if(std::isinf(rhs) && !std::isinf(lhs))
return rhs;
if(rhs == lhs)
return rhs;
return nan();
}
int exp = std::min(lhs.exponent - B, rhs.exponent - B) - M;
return renormalize(
lhs.to_signed<signed_primitive >(exp) +
rhs.to_signed<signed_primitive >(exp), exp);
}
friend IEEE754 operator - (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return nan();
if(std::isinf(lhs) || std::isinf(rhs)) {
if(std::isinf(lhs) && !std::isinf(rhs))
return lhs;
if(std::isinf(rhs) && !std::isinf(lhs))
return -rhs;
if(rhs != lhs)
return lhs;
return nan();
}
int exp = std::min(lhs.exponent - B, rhs.exponent - B) - M;
return renormalize(
lhs.to_signed<signed_primitive >(exp) -
rhs.to_signed<signed_primitive >(exp), exp);
}
friend IEEE754 operator * (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return nan();
int exponent = (lhs.exponent - B) + (rhs.exponent - B);
return renormalize(
(lhs.real_mantissa() *
rhs.real_mantissa()) >> M, exponent - M, lhs.sign ^ rhs.sign);
}
friend IEEE754 operator / (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return nan();
if(rhs == 0)
return inf(lhs.sign ^ rhs.sign);
int exponent = (lhs.exponent - B) - (rhs.exponent - B);
return renormalize(
(lhs.real_mantissa() << M) /
rhs.real_mantissa(), exponent - M, lhs.sign ^ rhs.sign);
}
// Placement
IEEE754& operator += (const IEEE754 &value) {
return *this = *this + value;
}
IEEE754& operator -= (const IEEE754 &value) {
return *this = *this - value;
}
IEEE754& operator *= (const IEEE754 &value) {
return *this = *this * value;
}
IEEE754& operator /= (const IEEE754 &value) {
return *this = *this / value;
}
// --------------------------- Comparison --------------------------- //
// Equality
friend bool operator == (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return false;
if(lhs.exponent == 0 && lhs.mantissa == 0
&& rhs.exponent == 0 && rhs.mantissa == 0)
return true;
return (primitive&)lhs == (primitive&)rhs;
}
friend bool operator != (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return true;
if(lhs.exponent == 0 && lhs.mantissa == 0
&& rhs.exponent == 0 && rhs.mantissa == 0)
return false;
return (primitive&)lhs != (primitive&)rhs;
}
// Relative comparison
friend bool operator < (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return false;
if(lhs.exponent - B < rhs.exponent - B)
return !rhs.sign;
if(lhs.mantissa < rhs.mantissa)
return !rhs.sign;
return lhs.sign && !rhs.sign;
}
friend bool operator > (const IEEE754 &lhs, const IEEE754 &rhs) {
if(std::isunordered(lhs, rhs))
return false;
if(lhs.exponent - B > rhs.exponent - B)
return !lhs.sign;
if(lhs.mantissa > rhs.mantissa)
return !lhs.sign;
return !lhs.sign && rhs.sign;
}
friend bool operator <= (const IEEE754 &lhs, const IEEE754 &rhs) {
return (lhs < rhs)
|| (rhs == lhs);
}
friend bool operator >= (const IEEE754 &lhs, const IEEE754 &rhs) {
return (lhs > rhs)
|| (rhs == lhs);
}
};
#endif