-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloating.cpp
More file actions
626 lines (521 loc) · 14.4 KB
/
floating.cpp
File metadata and controls
626 lines (521 loc) · 14.4 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include "floating.hpp"
template<typename T>
constexpr bool EQ (const T a, const T b){return a==b;}
constexpr bool EQ (const float a, const float b){
return std::bit_cast<uint32_t>(a)==std::bit_cast<uint32_t>(b)
|| ((std::abs(a - b) <= std::numeric_limits<float>::epsilon() * std::max(std::abs(a), std::abs(b))))
|| ((std::abs(b - a) <= std::numeric_limits<float>::epsilon() * std::max(std::abs(a), std::abs(b))));
}
constexpr bool EQ (const Float32 a, const Float32 b){
return EQ((float)a, (float)b);
}
#define RETURN_IF_A_B_BOTH(a, b, only_a, only_b, both) \
if((a)){ \
if((b)) \
return both; \
return (only_a); \
}else if((b)){ \
return (only_b); \
}
constexpr bool is_nan(const Float32 a){
// max exponent && mantissa != 0
return ((a.bits >> Float32::EXPONENT_OFFSET) & bit_mask<uint32_t>(Float32::EXPONENT_SIZE)) == bit_mask<uint32_t>(Float32::EXPONENT_SIZE)
&& (a.bits & bit_mask<uint32_t>(Float32::MANTISSA_SIZE)) != 0;
}
constexpr bool is_signaling(const Float32 a){
return a == Float32::SNaN;
}
constexpr bool is_inf(Float32 a){
// max exponent && mantissa = 0
return ((a.bits >> Float32::EXPONENT_OFFSET) & bit_mask<uint32_t>(Float32::EXPONENT_SIZE)) == bit_mask<uint32_t>(Float32::EXPONENT_SIZE)
&& (a.bits & (bit_mask<uint32_t>(Float32::MANTISSA_SIZE))) == 0;
}
constexpr bool is_zero(Float32 a){
return (a.bits & bit_mask<uint32_t>(Float32::MANTISSA_SIZE + Float32::EXPONENT_SIZE)) == 0;
}
constexpr bool is_sign_minus(const Float32 a){
return a.bits&(1<<Float32::SIGN_OFFSET);
}
constexpr bool is_normal(const Float32 a){
if(is_nan(a) || is_inf(a))
return false;
return !is_subnormal(a);
}
constexpr bool is_subnormal(const Float32 a){
return ((a.bits >> Float32::EXPONENT_OFFSET) & bit_mask<uint32_t>(Float32::EXPONENT_SIZE)) == 0;
}
constexpr bool is_finite(const Float32 a){
return !is_inf(a);
}
constexpr bool total_order_mag(const Float32 a, const Float32 b){
return total_order(abs(a), abs(b));
}
constexpr Float32 int_to_float(int32_t n){
if(n==0)
return Float32::ZERO;
uint32_t sign = 0;
if(n<0){
sign = 1<<Float32::SIGN_OFFSET;
n = -n;
}
uint32_t mantissa = n;
uint32_t exponent = Float32::EXPONENT_BIAS + Float32::MANTISSA_SIZE;
while(mantissa > bit_mask<uint32_t>(sizeof(uint32_t)*8)){
mantissa >>= 1;
exponent += 1;
}
while(mantissa < bit_mask<uint32_t>(Float32::MANTISSA_SIZE)){
mantissa <<= 1;
exponent -= 1;
}
exponent <<= Float32::EXPONENT_OFFSET;
mantissa &= bit_mask<uint32_t>(Float32::MANTISSA_SIZE);
return Float32{sign | exponent | mantissa};
}
constexpr Float32 add(const Float32 fa, const Float32 fb){
if(is_nan(fa))
return fa;
if(is_nan(fb))
return fb;
if(is_sign_minus(fa))
return sub(fb, neg(fa));
if(is_sign_minus(fb))
return sub(fa, neg(fb));
// they are both positive:
if(is_inf(fa) || is_inf(fb))
return Float32::MAX;
auto a = unpack(fa);
auto b = unpack(fb);
const uint8_t sign = 0;
//adjust smaller number and simply add their mantisa
//then adjust result
uint16_t exponent{};
uint32_t to_shift{};
uint32_t mantissa{};
if(a.exponent < b.exponent)
{
exponent = b.exponent;
to_shift = b.exponent - a.exponent;
mantissa = (a.mantissa >> to_shift) + b.mantissa;
if(to_shift >= Float32::TOTAL_SIZE)
mantissa = b.mantissa;
}
else
{
exponent = a.exponent;
to_shift = a.exponent - b.exponent;
mantissa = (b.mantissa >> to_shift) + a.mantissa;
if(to_shift >= 32)
mantissa = a.mantissa;
}
while(mantissa > bit_mask<uint32_t>(Float32::MANTISSA_SIZE+1)) //24 bits
{
exponent++;
mantissa >>= 1;
}
const Float32 t = pack({sign, exponent, mantissa});
if(is_nan(t))
return sign ? Float32::MIN : Float32::MAX;
return t;
}
constexpr Float32 sub(const Float32 fa, const Float32 fb){
if(is_nan(fa))
return fa;
if(is_nan(fb))
return fb;
if(is_sign_minus(fb))
return add(fa, neg(fb));
if(is_sign_minus(fa))
return neg(add(neg(fa), fb));
if(is_zero(fa)){
if(is_zero(fb))
return Float32::ZERO;
return neg(fb);
}
if(is_zero(fb))
return fa;
if(is_inf(fa)){
if(is_inf(fb))
return neg(Float32::QNaN);
return Float32::INFTY;
}
if(is_inf(fb))
return Float32::NEG_INFTY;
auto a = unpack(fa);
auto b = unpack(fb);
const bool sign = qls(fa, fb);
uint16_t exponent{};
uint32_t to_shift{};
uint64_t a_mantisa = a.mantissa;
uint64_t b_mantisa = b.mantissa;
uint64_t mantissa{};
const size_t PREC_SHIFT = 40;
a_mantisa <<= PREC_SHIFT;
b_mantisa <<= PREC_SHIFT;
if(!sign)
{
exponent = a.exponent;
to_shift = a.exponent - b.exponent;
mantissa = a_mantisa - (b_mantisa >> to_shift);
if(to_shift >= sizeof(mantissa)*8)
mantissa = a_mantisa - 1;
}
else
{
exponent = b.exponent;
to_shift = b.exponent - a.exponent;
mantissa = b_mantisa - (a_mantisa >> to_shift);
if(to_shift >= sizeof(mantissa)*8)
mantissa = b_mantisa - 1;
}
if(mantissa == 0)
return 0;
while(mantissa > (bit_mask<uint64_t>(Float32::MANTISSA_SIZE+1) << PREC_SHIFT)) //24 bits
{
exponent++;
mantissa >>= 1;
}
while(mantissa < (Float32::MANTISSA_LEADING_1 << PREC_SHIFT)) //24 bits
{
exponent--;
mantissa <<= 1;
}
if(mantissa == 0)
return 0;
mantissa >>= PREC_SHIFT;
const Float32 t = pack({sign, exponent, static_cast<uint32_t>(mantissa)});
if(is_nan(t))
return sign ? Float32::MIN : Float32::MAX;
return t;
}
constexpr Float32 div(const Float32 fa, const Float32 fb){
if(is_nan(fa))
return fa;
if(is_nan(fb))
return fb;
const bool sign = is_sign_minus(fa) ^ is_sign_minus(fb);
RETURN_IF_A_B_BOTH(is_inf(fa), is_inf(fb),
sign?Float32::NEG_INFTY:Float32::INFTY,
sign?Float32::NEG_ZERO:Float32::ZERO,
neg(Float32::QNaN))
RETURN_IF_A_B_BOTH(is_zero(fa), is_zero(fb),
sign?Float32::NEG_ZERO:Float32::ZERO,
sign?Float32::NEG_INFTY:Float32::INFTY,
neg(Float32::QNaN))
IEEEFields a = unpack(fa);
IEEEFields b = unpack(fb);
a.sign_bit ^= b.sign_bit;
/*
shifted 40 to increase precision of result
later res is shifted 40 to the right
*/
const size_t PREC_SHIFT = 40;
uint64_t num = static_cast<uint64_t>(a.mantissa) << PREC_SHIFT;
uint64_t div = b.mantissa;
uint64_t res = num / div;
res <<= Float32::MANTISSA_SIZE;
/*
subtracting exponents so BIAS has to be added
*/
a.exponent = a.exponent - b.exponent + Float32::BIAS;
if(res == 0)
return a.sign_bit ? Float32::MIN : Float32::MAX;
while((res & (1ul<<(8*sizeof(res)-1))) == 0)
{
res <<= 1;
a.exponent--;
}
a.mantissa = res >> PREC_SHIFT;
const Float32 result = pack(a);
if(is_nan(result))
return a.sign_bit?Float32::MIN:Float32::MAX;
return result;
}
constexpr Float32 sqrt(const Float32 fa){
if(is_zero(fa) || is_nan(fa))
return fa;
if(is_sign_minus(fa))
return neg(Float32::QNaN);
if(is_inf(fa)) // negative 0 is nan
return fa;
auto a = unpack(fa);
/*
sqrt(mant) can be computed as integer
in case of odd number we have to multiply mantisa by 2 first, then take square root
because 2^(odd / 2) will lead to 2^(even + 1/2) = 2^even * sqrt(2)
sqrt(2 * mantisa) = sqrt(mantisa) * sqrt(2)
*/
a.exponent -= Float32::BIAS;
const bool even = a.exponent%2==0;
a.exponent = static_cast<int16_t>(a.exponent) >> 1;
a.exponent += Float32::BIAS;
uint64_t mantissa = a.mantissa;
mantissa = std::sqrt(mantissa << (40 - even));
mantissa >>= Float32::EXPONENT_SIZE;
a.mantissa = mantissa;
return pack(a);
}
constexpr Float32 fused_mul_add(const Float32 x, const Float32 y, const Float32 z){
return add(mul(x,y), z);
}
constexpr cls float_class(const Float32 a){
using enum cls;
if(is_nan(a))
return qNan;
if(is_signaling(a))
return sNan;
if(is_inf(a))
return is_sign_minus(a)?nInf:pInf;
if(is_zero(a))
return is_sign_minus(a)?nZr:pZr;
if(is_normal(a))
return is_sign_minus(a)?nNrm:pNrm;
return is_sign_minus(a)?nSbn:pSbn;
}
/*
1.0011b * 101.001b
1.1875 * 5.125 = 6.05
1.001101 + 2^2*1.01001 =
{e:0 m:0011} * {e:2 m:01001} = {e:2 m:01100}
*/
constexpr Float32 mul(Float32 fa,Float32 fb){
if(is_nan(fa))
return fa;
if(is_nan(fb))
return fb;
if((is_inf(fa) && is_zero(fb))
|| (is_zero(fa) && is_inf(fb)))
return neg(Float32::QNaN);
if(is_zero(fa))
return fa;
if(is_zero(fb))
return fb;
// sign remains only if sign is diffrent
const bool sign_bit = (fa.bits ^ fb.bits) >> Float32::SIGN_OFFSET;
if(is_inf(fa) || is_inf(fb))
return Float32{(static_cast<uint32_t>(sign_bit) << Float32::SIGN_OFFSET) | Float32::INFTY.bits};
IEEEFields a = unpack(fa);
IEEEFields b = unpack(fb);
/*
exponent are added
mantissas are mulilied and lower bits are discarded via shift
*/
uint16_t exponent = a.exponent + b.exponent - Float32::BIAS;
uint64_t mantissa = static_cast<uint64_t>(a.mantissa)
* static_cast<uint64_t>(b.mantissa);
mantissa >>= Float32::MANTISSA_SIZE;
// normalization
if(mantissa != 0){
// check if mantissa is greater than its maximum
while(mantissa > bit_mask<uint64_t>(Float32::MANTISSA_SIZE+1)){
exponent += 1;
mantissa >>= 1;
}
// check if mantissa is less than its minimum
while(mantissa < Float32::MANTISSA_LEADING_1){
exponent -= 1;
mantissa <<= 1;
}
}
mantissa += 1;
const Float32 result = pack(IEEEFields{sign_bit,exponent,static_cast<uint32_t>(mantissa)});
// prevent nan
if(is_nan(result))
return sign_bit ? Float32::MIN : Float32::MAX;
return result;
}
constexpr Float32 scaleB(const Float32 x, const int32_t N){
if(N==0 || is_inf(x) || is_nan(x) || is_zero(x))
return x;
if(N >= static_cast<int32_t>(Float32::BIAS))
return Float32::MAX;
if(N <= -static_cast<int32_t>(Float32::BIAS))
return Float32::MIN;
auto a = unpack(x);
a.exponent += N;
auto f = pack(a);
return is_inf(f) ? Float32::MAX : f;
}
constexpr Float32 neg(const Float32 x){
return Float32{x.bits ^ (1<<(Float32::SIGN_OFFSET))};
}
constexpr Float32 abs(const Float32 x){
if(is_nan(x))
return x;
return Float32{x.bits & bit_mask<uint32_t>(Float32::TOTAL_SIZE-1)};
}
constexpr Float32 copy_sign(const Float32 x, const Float32 y){
if(is_nan(x))
return x;
return Float32{(x.bits & bit_mask<uint32_t>(Float32::SIGN_OFFSET)) | (y.bits & (1<<(Float32::TOTAL_SIZE-1))) };
}
constexpr bool qeq(const Float32 a , const Float32 b){
if(is_nan(a) || is_nan(b))
return false;
if(is_zero(a) && is_zero(b))
return true;
return a.bits == b.bits;
}
constexpr bool qne(const Float32 a, const Float32 b){
if(is_nan(a) || is_nan(b))
return false;
if(is_zero(a) && is_zero(b))
return true;
return a.bits == b.bits;
}
constexpr bool qgr(const Float32 a, const Float32 b){
if(is_nan(a) || is_nan(b)
||(is_zero(a) && is_zero(b)))
return false;
const bool sign_a = a.bits >> Float32::SIGN_OFFSET;
const bool sign_b = b.bits >> Float32::SIGN_OFFSET;
// both positive
if(!sign_a && !sign_b)
return a.bits > b.bits;
// both negative
if(sign_a && sign_b)
return b.bits > a.bits;
// a>=0 && b<=0
return !sign_a && sign_b;
}
constexpr bool qge(const Float32 a, const Float32 b){
if(is_nan(a) || is_nan(b))
return false;
if(is_zero(a) && is_zero(b))
return true;
const bool sign_a = is_sign_minus(a);
const bool sign_b = is_sign_minus(b);
// both positive
if(!sign_a && !sign_b)
return a.bits >= b.bits;
// both negative
if(sign_a && sign_b)
return b.bits >= a.bits;
// a>=0 && b<=0
return !sign_a && sign_b;
}
constexpr bool qls(const Float32 a, const Float32 b){
if(is_nan(a) || is_nan(b)
||(is_zero(a) && is_zero(b)))
return false;
const bool sign_a = a.bits >> Float32::SIGN_OFFSET;
const bool sign_b = b.bits >> Float32::SIGN_OFFSET;
// both positive
if(!sign_a && !sign_b)
return a.bits < b.bits;
// both negative
if(sign_a && sign_b)
return b.bits < a.bits;
// a<=0 && b>=0
return sign_a && !sign_b;
}
constexpr bool qle(const Float32 a, const Float32 b){
if(is_nan(a) || is_nan(b))
return false;
if(is_zero(a) && is_zero(b))
return true;
const bool sign_a = a.bits >> Float32::SIGN_OFFSET;
const bool sign_b = b.bits >> Float32::SIGN_OFFSET;
// both positive
if(!sign_a && !sign_b)
return a.bits <= b.bits;
// both negative
if(sign_a && sign_b)
return b.bits <= a.bits;
// a<=0 && b>=0
return sign_a && !sign_b;
}
/*
5.10
*/
constexpr bool total_order(const Float32 a, const Float32 b){
//d)
if(is_nan(a))
{
//3)
if(is_nan(b))
{
//i)
if(is_sign_minus(a))
return true;
//ii)
//???
//iii)
//?????
return false;
}
//1)
else
{
//1)
if(is_sign_minus(a))
return true;
return false;
}
}
if(is_nan(b))
{
//2)
if(is_sign_minus(b))
return false;
return true;
}
//a)
if(qls(a, b))
return true;
//b)
if(qgr(a, b))
return false;
//c)
if(a == b)
return true;
//1) and 2)
return is_sign_minus(a);
}
constexpr Float32 next_up(const Float32 a){
if(is_nan(a))
return a;
if(is_zero(a))
return 1;
if(is_sign_minus(a))
return {a.bits-1};
else
return is_inf(a) ? a : Float32{a.bits+1};
}
constexpr Float32 next_down(const Float32 a){
return neg(next_up(neg(a)));
}
constexpr Float32 min_val(const Float32 a, const Float32 b){
if(is_nan(a))
return a;
if(is_nan(b))
return b;
return qls(a,b)? a : b;
}
constexpr Float32 max_val(const Float32 a, const Float32 b){
if(is_nan(a))
return a;
if(is_nan(b))
return b;
return qgr(a,b)? a : b;
}
constexpr Float32
Float32::ZERO = Float32{0},
Float32::NEG_ZERO = Float32{1u<<Float32::SIGN_OFFSET},
Float32::INFTY{bit_mask<int32_t>(EXPONENT_SIZE) << (Float32::EXPONENT_OFFSET)},
Float32::NEG_INFTY{neg(Float32::INFTY)},
Float32::QNaN{(bit_mask<uint32_t>(EXPONENT_SIZE) << (Float32::EXPONENT_OFFSET)) | (1<<(MANTISSA_SIZE-1))},
Float32::SNaN{(bit_mask<uint32_t>(EXPONENT_SIZE) << (Float32::EXPONENT_OFFSET)) | (1<<(MANTISSA_SIZE-2))},
Float32::MAX{Float32::INFTY.bits - 1},
Float32::MIN{neg(Float32::MAX)};
constexpr bool operator==(const float a, const Float32 b){
return std::bit_cast<uint32_t>(a)== b.bits;
}
constexpr bool operator!=(const float a, const Float32 b){
return !(a == b);
}