-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathfloat64_t.hlsl
More file actions
668 lines (568 loc) · 26.3 KB
/
float64_t.hlsl
File metadata and controls
668 lines (568 loc) · 26.3 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#ifndef _NBL_BUILTIN_HLSL_EMULATED_FLOAT64_T_HLSL_INCLUDED_
#define _NBL_BUILTIN_HLSL_EMULATED_FLOAT64_T_HLSL_INCLUDED_
#include <nbl/builtin/hlsl/emulated/float64_t_impl.hlsl>
namespace nbl
{
namespace hlsl
{
/*enum E_ROUNDING_MODE
{
FLOAT_ROUND_NEAREST_EVEN,
FLOAT_ROUND_TO_ZERO,
FLOAT_ROUND_DOWN,
FLOAT_ROUND_UP
};*/
// currently only FLOAT_ROUND_TO_ZERO is supported, cannot implement partial specialization in this case due to dxc bug https://github.com/microsoft/DirectXShaderCompiler/issues/5563
// TODO: partial specializations with new template parameter `E_ROUNDING_MODE RoundingMode`
template<bool FastMath = true, bool FlushDenormToZero = true>
struct emulated_float64_t
{
using storage_t = uint64_t;
using this_t = emulated_float64_t<FastMath, FlushDenormToZero>;
storage_t data;
template<bool FastMathOther, bool FlushDenormToZeroOther>
inline static this_t create(emulated_float64_t<FastMathOther, FlushDenormToZeroOther> other)
{
if (FlushDenormToZero)
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(other.data));
else
return bit_cast<this_t>(other.data);
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(int32_t val)
{
if (FlushDenormToZero)
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(int64_t(val))));
else
return bit_cast<this_t>(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(int64_t(val)));
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(int64_t val)
{
if (FlushDenormToZero)
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(val)));
else
return bit_cast<this_t>(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(val));
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(uint32_t val)
{
if (FlushDenormToZero)
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(uint64_t(val))));
else
return bit_cast<this_t>(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(uint64_t(val)));
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(uint64_t val)
{
if (FlushDenormToZero)
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(val)));
else
return bit_cast<this_t>(emulated_float64_t_impl::reinterpretAsFloat64BitPattern(val));
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(float32_t val)
{
this_t output;
output.data = emulated_float64_t_impl::castFloat32ToStorageType<FlushDenormToZero>(val);
return output;
}
NBL_CONSTEXPR_STATIC_INLINE this_t create(float64_t val)
{
this_t retval;
#ifdef __HLSL_VERSION
uint32_t lo, hi;
asuint(val, lo, hi);
retval.data = emulated_float64_t_impl::flushDenormToZero((uint64_t(hi) << 32) | uint64_t(lo));
return retval;
#else
retval.data = emulated_float64_t_impl::flushDenormToZero(bit_cast<uint64_t>(val));
return retval;
#endif
}
// arithmetic operators
this_t operator+(const this_t rhs) NBL_CONST_MEMBER_FUNC
{
if (FlushDenormToZero)
{
if(!FastMath)
{
const bool isRhsInf = cpp_compat_intrinsics_impl::isinf_uint_impl(rhs.data);
if (cpp_compat_intrinsics_impl::isinf_uint_impl(data))
{
if (isRhsInf && ((data ^ rhs.data) & ieee754::traits<float64_t>::signMask))
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | ieee754::traits<float64_t>::signMask);
return bit_cast<this_t>(data);
}
else if (isRhsInf)
return bit_cast<this_t>(rhs.data);
}
const int lhsBiasedExp = ieee754::extractBiasedExponent(data);
const int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data);
uint64_t lhsSign = ieee754::extractSignPreserveBitPattern(data);
uint64_t rhsSign = ieee754::extractSignPreserveBitPattern(rhs.data);
if(!FastMath)
{
if (cpp_compat_intrinsics_impl::isinf_uint_impl(data))
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | ieee754::extractSignPreserveBitPattern(max(data, rhs.data)));
}
const bool isRhsZero = emulated_float64_t_impl::isZero(rhs.data);
if (emulated_float64_t_impl::isZero(data))
{
if(isRhsZero)
return bit_cast<this_t>((uint64_t(data == rhs.data) << 63) & lhsSign);
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(rhs.data));
}
else if (isRhsZero)
{
return bit_cast<this_t>(emulated_float64_t_impl::flushDenormToZero(data));
}
uint64_t lhsNormMantissa = ieee754::extractNormalizeMantissa(data);
uint64_t rhsNormMantissa = ieee754::extractNormalizeMantissa(rhs.data);
const int expDiff = lhsBiasedExp - rhsBiasedExp;
int exp = max(lhsBiasedExp, rhsBiasedExp) - ieee754::traits<float64_t>::exponentBias;
const uint32_t shiftAmount = abs(expDiff);
if (expDiff < 0)
{
// so lhsNormMantissa always holds mantissa of number with greater exponent
swap<uint64_t>(lhsNormMantissa, rhsNormMantissa);
swap<uint64_t>(lhsSign, rhsSign);
}
uint64_t resultMantissa;
if (lhsSign != rhsSign)
{
if ((data | ieee754::traits<float64_t>::signMask) == (rhs.data | ieee754::traits<float64_t>::signMask))
return _static_cast<this_t>(0ull);
uint64_t rhsNormMantissaHigh = shiftAmount >= 64u ? 0ull : rhsNormMantissa >> shiftAmount;
uint64_t rhsNormMantissaLow = 0ull;
if (shiftAmount < 128)
{
if (shiftAmount >= 64)
{
rhsNormMantissaLow = rhsNormMantissa >> (shiftAmount - 64);
}
else
{
const uint32_t lowMantissaShiftAmount = 64 - shiftAmount;
if(lowMantissaShiftAmount < 64)
rhsNormMantissaLow = rhsNormMantissa << lowMantissaShiftAmount;
}
}
const int64_t mantissaDiff = int64_t(lhsNormMantissa) - int64_t(rhsNormMantissaHigh);
// can only happen when shiftAmount == 0, so it is safe to swap only high bits of rhs mantissa
if (mantissaDiff < 0)
{
swap<uint64_t>(lhsNormMantissa, rhsNormMantissaHigh);
swap<uint64_t>(lhsSign, rhsSign);
}
resultMantissa = emulated_float64_t_impl::subMantissas128NormalizeResult(lhsNormMantissa, rhsNormMantissaHigh, rhsNormMantissaLow, exp);
if (resultMantissa == 0ull)
return _static_cast<this_t>(0ull);
}
else
{
rhsNormMantissa = shiftAmount > 63 ? 0ull : rhsNormMantissa >> shiftAmount;
resultMantissa = lhsNormMantissa + rhsNormMantissa;
if (resultMantissa & 1ull << 53)
{
++exp;
resultMantissa >>= 1;
}
}
uint64_t resultBiasedExp = uint64_t(exp) + ieee754::traits<float64_t>::exponentBias;
resultMantissa &= ieee754::traits<float64_t>::mantissaMask;
uint64_t output = emulated_float64_t_impl::assembleFloat64(lhsSign, resultBiasedExp << ieee754::traits<float64_t>::mantissaBitCnt, resultMantissa);
output = emulated_float64_t_impl::flushDenormToZero(output);
return bit_cast<this_t>(output);
}
// not implemented
if (!FlushDenormToZero)
return bit_cast<this_t>(0xdeadbeefbadcaffeull);
}
this_t operator+(float rhs)
{
return bit_cast<this_t>(data) + create(rhs);
}
this_t operator-(this_t rhs) NBL_CONST_MEMBER_FUNC
{
this_t lhs = bit_cast<this_t>(data);
this_t rhsFlipped = rhs.flipSign();
return lhs + rhsFlipped;
}
this_t operator-(float rhs) NBL_CONST_MEMBER_FUNC
{
return bit_cast<this_t>(data) - create(rhs);
}
this_t operator*(this_t rhs) NBL_CONST_MEMBER_FUNC
{
if(FlushDenormToZero)
{
uint64_t sign = (data ^ rhs.data) & ieee754::traits<float64_t>::signMask;
if (!FastMath)
{
if (cpp_compat_intrinsics_impl::isnan_uint_impl(data) || cpp_compat_intrinsics_impl::isnan_uint_impl(rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | sign);
if (cpp_compat_intrinsics_impl::isinf_uint_impl(data) || cpp_compat_intrinsics_impl::isinf_uint_impl(rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
if (emulated_float64_t_impl::isZero(data) || emulated_float64_t_impl::isZero(rhs.data))
return bit_cast<this_t>(sign);
}
if (emulated_float64_t_impl::isZero(data) || emulated_float64_t_impl::isZero(rhs.data))
return bit_cast<this_t>(sign);
this_t retval = this_t::create(0ull);
int lhsBiasedExp = ieee754::extractBiasedExponent(data);
int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data);
int exp = int(lhsBiasedExp + rhsBiasedExp) - ieee754::traits<float64_t>::exponentBias;
uint64_t lhsMantissa = ieee754::extractMantissa(data);
uint64_t rhsMantissa = ieee754::extractMantissa(rhs.data);
const uint64_t hi_l = (lhsMantissa >> 21) | (1ull << 31);
const uint64_t lo_l = lhsMantissa & ((1ull << 21) - 1);
const uint64_t hi_r = (rhsMantissa >> 21) | (1ull << 31);
const uint64_t lo_r = rhsMantissa & ((1ull << 21) - 1);
//const uint64_t RoundToNearest = (1ull << 31) - 1;
uint64_t newPseudoMantissa = ((hi_l * hi_r) >> 10) + ((hi_l * lo_r + lo_l * hi_r/* + RoundToNearest*/) >> 31);
if (newPseudoMantissa & (0x1ull << 53))
{
newPseudoMantissa >>= 1;
++exp;
}
newPseudoMantissa &= (ieee754::traits<float64_t>::mantissaMask);
uint64_t output = emulated_float64_t_impl::assembleFloat64(sign, uint64_t(exp) << ieee754::traits<float64_t>::mantissaBitCnt, newPseudoMantissa);
output = emulated_float64_t_impl::flushDenormToZero(output);
return bit_cast<this_t>(output);
}
else
{
//static_assert(false, "not implemented yet");
return bit_cast<this_t>(0xdeadbeefbadcaffeull);
}
}
this_t operator*(float rhs)
{
return bit_cast<this_t>(data) * create(rhs);
}
this_t operator/(const this_t rhs) NBL_CONST_MEMBER_FUNC
{
if (FlushDenormToZero)
{
const uint64_t sign = (data ^ rhs.data) & ieee754::traits<float64_t>::signMask;
int lhsBiasedExp = ieee754::extractBiasedExponent(data);
int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data);
if(!FastMath)
{
if (cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(data) || cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN);
if (emulated_float64_t_impl::areBothZero(data, rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | sign);
if (emulated_float64_t_impl::isZero(rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
if (emulated_float64_t_impl::areBothInfinity(data, rhs.data))
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | ieee754::traits<float64_t>::signMask);
if (cpp_compat_intrinsics_impl::isinf_uint_impl(data))
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
if (cpp_compat_intrinsics_impl::isinf_uint_impl(rhs.data))
return bit_cast<this_t>(sign);
}
if (emulated_float64_t_impl::isZero(data))
return bit_cast<this_t>(sign);
const uint64_t lhsRealMantissa = (ieee754::extractMantissa(data) | (1ull << ieee754::traits<float64_t>::mantissaBitCnt));
const uint64_t rhsRealMantissa = ieee754::extractMantissa(rhs.data) | (1ull << ieee754::traits<float64_t>::mantissaBitCnt);
int exp = lhsBiasedExp - rhsBiasedExp + int(ieee754::traits<float64_t>::exponentBias);
uint64_t2 lhsMantissaShifted = emulated_float64_t_impl::shiftMantissaLeftBy53(lhsRealMantissa);
uint64_t mantissa = emulated_float64_t_impl::divmod128by64(lhsMantissaShifted.x, lhsMantissaShifted.y, rhsRealMantissa);
const int msb = findMSB(mantissa);
if(msb != -1)
{
const int shiftAmount = 52 - msb;
assert(shiftAmount >= 0);
mantissa <<= shiftAmount;
exp -= shiftAmount;
}
mantissa &= ieee754::traits<float64_t>::mantissaMask;
uint64_t output = emulated_float64_t_impl::assembleFloat64(sign, uint64_t(exp) << ieee754::traits<float64_t>::mantissaBitCnt, mantissa);
output = emulated_float64_t_impl::flushDenormToZero(output);
return bit_cast<this_t>(output);
}
else
{
//static_assert(false, "not implemented yet");
return bit_cast<this_t>(0xdeadbeefbadcaffeull);
}
}
this_t operator/(const float rhs) NBL_CONST_MEMBER_FUNC
{
return bit_cast<this_t>(data) / create(rhs);
}
// relational operators
bool operator==(this_t rhs) NBL_CONST_MEMBER_FUNC
{
if (!FastMath)
{
if (cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(data) || cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(rhs.data))
return false;
if (emulated_float64_t_impl::areBothZero(data, rhs.data))
return true;
}
return data == rhs.data;
}
bool operator!=(this_t rhs) NBL_CONST_MEMBER_FUNC
{
if (!FastMath && (cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(data) || cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(rhs.data)))
return false;
return !(bit_cast<this_t>(data) == rhs);
}
bool operator<(this_t rhs) NBL_CONST_MEMBER_FUNC
{
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, hlsl::less<uint64_t> >(data, rhs.data);
}
bool operator>(this_t rhs) NBL_CONST_MEMBER_FUNC
{
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, hlsl::greater<uint64_t> >(data, rhs.data);
}
bool operator<=(this_t rhs) NBL_CONST_MEMBER_FUNC
{
if (!FastMath && (cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(data) || cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(rhs.data)))
return false;
return !(bit_cast<this_t>(data) > bit_cast<this_t>(rhs.data));
}
bool operator>=(this_t rhs)
{
if (!FastMath && (cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(data) || cpp_compat_intrinsics_impl::isnan_uint_impl<uint64_t>(rhs.data)))
return false;
return !(bit_cast<this_t>(data) < bit_cast<this_t>(rhs.data));
}
this_t flipSign()
{
return bit_cast<this_t>(data ^ ieee754::traits<float64_t>::signMask);
}
/**
* @brief Computes sqare root estimation.
*
* Can be less precise when FastMath is disabled.
* sqrt(inf) = inf
* sqrt(-0) = -0
* sqrt(NaN) = NaN
*/
static this_t sqrt(this_t number)
{
bool isZero = !(number.data & 0x7FFFFFFFFFFFFFFFull);
if (isZero)
return number;
static const uint64_t MaxFloat64AsUint64 = 0x7FEFFFFFFFFFFFFFull;
if (number.data > MaxFloat64AsUint64)
{
bool isInf = cpp_compat_intrinsics_impl::isinf_uint_impl(number.data);
if (isInf)
return number;
// when (number.data > MaxFloat64AsUint64) and is not infinity, we can be sure that number is either NaN or negative
return bit_cast<this_t>(ieee754::traits<this_t>::quietNaN);
}
const float f32InverseSquareRoot = nbl::hlsl::rsqrt(_static_cast<float>(number));
// find sqrt approximation using the Newton-Raphson method
this_t inverseSquareRoot = _static_cast<this_t>(f32InverseSquareRoot);
const int Iterations = 5;
static const this_t Half = this_t::create(0.5f);
static const this_t ThreeHalfs = this_t::create(1.5f);
const this_t x2 = number * Half;
[[unroll]]
for (int i = 0; i < Iterations; ++i)
{
inverseSquareRoot = inverseSquareRoot * (ThreeHalfs - (x2 * inverseSquareRoot * inverseSquareRoot));
}
if (FastMath)
{
return this_t::create(1.0f) / inverseSquareRoot;
}
else
{
// 2 Newton-Raphson iterations to increase precision
this_t squareRoot = this_t::create(1.0f) / inverseSquareRoot;
squareRoot = Half * (squareRoot + number / squareRoot);
squareRoot = Half * (squareRoot + number / squareRoot);
return squareRoot;
}
}
NBL_CONSTEXPR_STATIC bool isFastMathSupported = FastMath;
};
#define IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE(...) \
template<>\
struct traits_base<__VA_ARGS__ >\
{\
NBL_CONSTEXPR_STATIC_INLINE int16_t exponentBitCnt = 11;\
NBL_CONSTEXPR_STATIC_INLINE int16_t mantissaBitCnt = 52;\
};\
template<>\
inline uint32_t extractBiasedExponent(__VA_ARGS__ x)\
{\
return extractBiasedExponent<uint64_t>(x.data);\
}\
\
template<>\
inline int extractExponent(__VA_ARGS__ x)\
{\
return extractExponent(x.data);\
}\
\
template<>\
NBL_CONSTEXPR_FUNC __VA_ARGS__ replaceBiasedExponent(__VA_ARGS__ x, typename unsigned_integer_of_size<sizeof(__VA_ARGS__)>::type biasedExp)\
{\
return __VA_ARGS__(replaceBiasedExponent(x.data, biasedExp));\
}\
\
template <>\
NBL_CONSTEXPR_FUNC __VA_ARGS__ fastMulExp2(__VA_ARGS__ x, int n)\
{\
return __VA_ARGS__(replaceBiasedExponent(x.data, extractBiasedExponent(x) + uint32_t(n)));\
}\
\
template <>\
NBL_CONSTEXPR_FUNC unsigned_integer_of_size<sizeof(__VA_ARGS__)>::type extractMantissa(__VA_ARGS__ x)\
{\
return extractMantissa(x.data);\
}\
\
template <>\
NBL_CONSTEXPR_FUNC uint64_t extractNormalizeMantissa(__VA_ARGS__ x)\
{\
return extractNormalizeMantissa(x.data);\
}\
\
#define DEFINE_BIT_CAST_SPEC(...)\
template<>\
NBL_CONSTEXPR_FUNC __VA_ARGS__ bit_cast<__VA_ARGS__, uint64_t>(NBL_CONST_REF_ARG(uint64_t) val)\
{\
__VA_ARGS__ output;\
output.data = val;\
\
return output;\
}\
\
template<>\
NBL_CONSTEXPR_FUNC __VA_ARGS__ bit_cast<__VA_ARGS__, float64_t>(NBL_CONST_REF_ARG(float64_t) val)\
{\
__VA_ARGS__ output;\
output.data = bit_cast<uint64_t>(val);\
\
return output;\
}\
\
template<>\
NBL_CONSTEXPR_FUNC uint64_t bit_cast<uint64_t, __VA_ARGS__ >(NBL_CONST_REF_ARG( __VA_ARGS__ ) val)\
{\
return val.data;\
}\
\
template<>\
NBL_CONSTEXPR_FUNC float64_t bit_cast<float64_t, __VA_ARGS__ >(NBL_CONST_REF_ARG( __VA_ARGS__ ) val)\
{\
return bit_cast<float64_t>(val.data);\
}\
\
namespace impl
{
template<typename To, bool FastMath, bool FlushDenormToZero>
struct static_cast_helper<To,emulated_float64_t<FastMath,FlushDenormToZero>,void>
{
static_assert(is_scalar<To>::value);
using From = emulated_float64_t<FastMath,FlushDenormToZero>;
static inline To cast(From v)
{
using ToAsFloat = typename float_of_size<sizeof(To)>::type;
using ToAsUint = typename unsigned_integer_of_size<sizeof(To)>::type;
if (emulated_float64_t_impl::isZero(v.data))
return 0;
if (is_same_v<To, float64_t>)
return To(bit_cast<float64_t>(v.data));
if (is_floating_point<To>::value)
{
const int exponent = ieee754::extractExponent(v.data);
if (!From::isFastMathSupported)
{
if (exponent > ieee754::traits<ToAsFloat>::exponentMax)
return bit_cast<To>(ieee754::traits<ToAsFloat>::inf);
if (exponent < ieee754::traits<ToAsFloat>::exponentMin)
return bit_cast<To>(-ieee754::traits<ToAsFloat>::inf);
if (cpp_compat_intrinsics_impl::isinf_uint_impl(v.data))
return bit_cast<To>(ieee754::traits<ToAsFloat>::quietNaN);
}
const uint32_t toBitSize = sizeof(To) * 8;
const ToAsUint sign = ToAsUint(ieee754::extractSign(v.data) << (toBitSize - 1));
const ToAsUint biasedExponent = ToAsUint(exponent + ieee754::traits<ToAsFloat>::exponentBias) << ieee754::traits<ToAsFloat>::mantissaBitCnt;
const ToAsUint mantissa = ToAsUint(v.data >> (ieee754::traits<float64_t>::mantissaBitCnt - ieee754::traits<ToAsFloat>::mantissaBitCnt)) & ieee754::traits<ToAsFloat>::mantissaMask;
return bit_cast<ToAsFloat>(sign | biasedExponent | mantissa);
}
// NOTE: casting from negative float to unsigned int is an UB, function will return abs value in this case
if (is_integral<To>::value)
{
const int exponent = ieee754::extractExponent(v.data);
if (exponent < 0)
return 0;
uint64_t unsignedOutput = ieee754::extractMantissa(v.data) | 1ull << ieee754::traits<float64_t>::mantissaBitCnt;
const int shiftAmount = exponent - int(ieee754::traits<float64_t>::mantissaBitCnt);
if (shiftAmount < 0)
unsignedOutput >>= -shiftAmount;
else
unsignedOutput <<= shiftAmount;
if (is_signed<To>::value)
{
int64_t signedOutput64 = unsignedOutput & ((1ull << 63) - 1);
To signedOutput = To(signedOutput64);
if (ieee754::extractSignPreserveBitPattern(v.data) != 0)
signedOutput = -signedOutput;
return signedOutput;
}
return To(unsignedOutput);
}
// assert(false);
return To(0xdeadbeefbadcaffeull);
}
};
template<typename From, bool FastMath, bool FlushDenormToZero>
struct static_cast_helper<emulated_float64_t<FastMath, FlushDenormToZero>, From, void>
{
using To = emulated_float64_t<FastMath, FlushDenormToZero>;
static inline To cast(From v)
{
return To::create(v);
}
};
template<bool FastMath, bool FlushDenormToZero>
struct static_cast_helper<emulated_float64_t<FastMath, FlushDenormToZero>, emulated_float64_t<FastMath, FlushDenormToZero>, void>
{
static inline emulated_float64_t<FastMath, FlushDenormToZero> cast(emulated_float64_t<FastMath, FlushDenormToZero> v)
{
return v;
}
};
}
DEFINE_BIT_CAST_SPEC(emulated_float64_t<true, true>);
DEFINE_BIT_CAST_SPEC(emulated_float64_t<false, false>);
DEFINE_BIT_CAST_SPEC(emulated_float64_t<true, false>);
DEFINE_BIT_CAST_SPEC(emulated_float64_t<false, true>);
//template<bool FastMath, bool FlushDenormToZero>
//struct is_floating_point<emulated_float64_t<FastMath, FlushDenormToZero> > : bool_constant<true> {};
namespace ieee754
{
namespace impl
{
template<> NBL_CONSTEXPR_FUNC uint64_t bitCastToUintType(emulated_float64_t<true, true> x) { return x.data; }
template<> NBL_CONSTEXPR_FUNC uint64_t bitCastToUintType(emulated_float64_t<false, false> x) { return x.data; }
template<> NBL_CONSTEXPR_FUNC uint64_t bitCastToUintType(emulated_float64_t<true, false> x) { return x.data; }
template<> NBL_CONSTEXPR_FUNC uint64_t bitCastToUintType(emulated_float64_t<false, true> x) { return x.data; }
}
IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE(emulated_float64_t<true, true>);
IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE(emulated_float64_t<false, false>);
IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE(emulated_float64_t<true, false>);
IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE(emulated_float64_t<false, true>);
}
namespace concepts
{
namespace impl
{
template<bool FastMath, bool FlushDenormToZero>
struct is_emulating_floating_point_scalar<emulated_float64_t<FastMath, FlushDenormToZero> >
{
NBL_CONSTEXPR_STATIC_INLINE bool value = true;
};
}
}
}
}
#undef IMPLEMENT_IEEE754_FUNC_SPEC_FOR_EMULATED_F64_TYPE
#undef DEFINE_BIT_CAST_SPEC
#endif