-
Notifications
You must be signed in to change notification settings - Fork 11
/
half.hpp
4576 lines (4285 loc) · 205 KB
/
half.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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// half - IEEE 754-based half-precision floating-point library.
//
// Copyright (c) 2012-2019 Christian Rau <rauy@users.sourceforge.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Version 2.1.0
/// \file
/// Main header file for half-precision functionality.
#ifndef HALF_HALF_HPP
#define HALF_HALF_HPP
#define HALF_GCC_VERSION (__GNUC__*100+__GNUC_MINOR__)
#if defined(__INTEL_COMPILER)
#define HALF_ICC_VERSION __INTEL_COMPILER
#elif defined(__ICC)
#define HALF_ICC_VERSION __ICC
#elif defined(__ICL)
#define HALF_ICC_VERSION __ICL
#else
#define HALF_ICC_VERSION 0
#endif
// check C++11 language features
#if defined(__clang__) // clang
#if __has_feature(cxx_static_assert) && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
#define HALF_ENABLE_CPP11_STATIC_ASSERT 1
#endif
#if __has_feature(cxx_constexpr) && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
#define HALF_ENABLE_CPP11_CONSTEXPR 1
#endif
#if __has_feature(cxx_noexcept) && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
#define HALF_ENABLE_CPP11_NOEXCEPT 1
#endif
#if __has_feature(cxx_user_literals) && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
#define HALF_ENABLE_CPP11_USER_LITERALS 1
#endif
#if __has_feature(cxx_thread_local) && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
#define HALF_ENABLE_CPP11_THREAD_LOCAL 1
#endif
#if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L) && !defined(HALF_ENABLE_CPP11_LONG_LONG)
#define HALF_ENABLE_CPP11_LONG_LONG 1
#endif
#elif HALF_ICC_VERSION && defined(__INTEL_CXX11_MODE__) // Intel C++
#if HALF_ICC_VERSION >= 1500 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
#define HALF_ENABLE_CPP11_THREAD_LOCAL 1
#endif
#if HALF_ICC_VERSION >= 1500 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
#define HALF_ENABLE_CPP11_USER_LITERALS 1
#endif
#if HALF_ICC_VERSION >= 1400 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
#define HALF_ENABLE_CPP11_CONSTEXPR 1
#endif
#if HALF_ICC_VERSION >= 1400 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
#define HALF_ENABLE_CPP11_NOEXCEPT 1
#endif
#if HALF_ICC_VERSION >= 1110 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
#define HALF_ENABLE_CPP11_STATIC_ASSERT 1
#endif
#if HALF_ICC_VERSION >= 1110 && !defined(HALF_ENABLE_CPP11_LONG_LONG)
#define HALF_ENABLE_CPP11_LONG_LONG 1
#endif
#elif defined(__GNUC__) // gcc
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
#if HALF_GCC_VERSION >= 408 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
#define HALF_ENABLE_CPP11_THREAD_LOCAL 1
#endif
#if HALF_GCC_VERSION >= 407 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
#define HALF_ENABLE_CPP11_USER_LITERALS 1
#endif
#if HALF_GCC_VERSION >= 406 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
#define HALF_ENABLE_CPP11_CONSTEXPR 1
#endif
#if HALF_GCC_VERSION >= 406 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
#define HALF_ENABLE_CPP11_NOEXCEPT 1
#endif
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
#define HALF_ENABLE_CPP11_STATIC_ASSERT 1
#endif
#if !defined(HALF_ENABLE_CPP11_LONG_LONG)
#define HALF_ENABLE_CPP11_LONG_LONG 1
#endif
#endif
#define HALF_TWOS_COMPLEMENT_INT 1
#elif defined(_MSC_VER) // Visual C++
#if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_THREAD_LOCAL)
#define HALF_ENABLE_CPP11_THREAD_LOCAL 1
#endif
#if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_USER_LITERALS)
#define HALF_ENABLE_CPP11_USER_LITERALS 1
#endif
#if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_CONSTEXPR)
#define HALF_ENABLE_CPP11_CONSTEXPR 1
#endif
#if _MSC_VER >= 1900 && !defined(HALF_ENABLE_CPP11_NOEXCEPT)
#define HALF_ENABLE_CPP11_NOEXCEPT 1
#endif
#if _MSC_VER >= 1600 && !defined(HALF_ENABLE_CPP11_STATIC_ASSERT)
#define HALF_ENABLE_CPP11_STATIC_ASSERT 1
#endif
#if _MSC_VER >= 1310 && !defined(HALF_ENABLE_CPP11_LONG_LONG)
#define HALF_ENABLE_CPP11_LONG_LONG 1
#endif
#define HALF_TWOS_COMPLEMENT_INT 1
#define HALF_POP_WARNINGS 1
#pragma warning(push)
#pragma warning(disable : 4099 4127 4146) //struct vs class, constant in if, negative unsigned
#endif
// check C++11 library features
#include <utility>
#if defined(_LIBCPP_VERSION) // libc++
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103
#ifndef HALF_ENABLE_CPP11_TYPE_TRAITS
#define HALF_ENABLE_CPP11_TYPE_TRAITS 1
#endif
#ifndef HALF_ENABLE_CPP11_CSTDINT
#define HALF_ENABLE_CPP11_CSTDINT 1
#endif
#ifndef HALF_ENABLE_CPP11_CMATH
#define HALF_ENABLE_CPP11_CMATH 1
#endif
#ifndef HALF_ENABLE_CPP11_HASH
#define HALF_ENABLE_CPP11_HASH 1
#endif
#ifndef HALF_ENABLE_CPP11_CFENV
#define HALF_ENABLE_CPP11_CFENV 1
#endif
#endif
#elif defined(__GLIBCXX__) // libstdc++
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103
#ifdef __clang__
#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
#define HALF_ENABLE_CPP11_TYPE_TRAITS 1
#endif
#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CSTDINT)
#define HALF_ENABLE_CPP11_CSTDINT 1
#endif
#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CMATH)
#define HALF_ENABLE_CPP11_CMATH 1
#endif
#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_HASH)
#define HALF_ENABLE_CPP11_HASH 1
#endif
#if __GLIBCXX__ >= 20080606 && !defined(HALF_ENABLE_CPP11_CFENV)
#define HALF_ENABLE_CPP11_CFENV 1
#endif
#else
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
#define HALF_ENABLE_CPP11_TYPE_TRAITS 1
#endif
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CSTDINT)
#define HALF_ENABLE_CPP11_CSTDINT 1
#endif
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CMATH)
#define HALF_ENABLE_CPP11_CMATH 1
#endif
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_HASH)
#define HALF_ENABLE_CPP11_HASH 1
#endif
#if HALF_GCC_VERSION >= 403 && !defined(HALF_ENABLE_CPP11_CFENV)
#define HALF_ENABLE_CPP11_CFENV 1
#endif
#endif
#endif
#elif defined(_CPPLIB_VER) // Dinkumware/Visual C++
#if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_TYPE_TRAITS)
#define HALF_ENABLE_CPP11_TYPE_TRAITS 1
#endif
#if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_CSTDINT)
#define HALF_ENABLE_CPP11_CSTDINT 1
#endif
#if _CPPLIB_VER >= 520 && !defined(HALF_ENABLE_CPP11_HASH)
#define HALF_ENABLE_CPP11_HASH 1
#endif
#if _CPPLIB_VER >= 610 && !defined(HALF_ENABLE_CPP11_CMATH)
#define HALF_ENABLE_CPP11_CMATH 1
#endif
#if _CPPLIB_VER >= 610 && !defined(HALF_ENABLE_CPP11_CFENV)
#define HALF_ENABLE_CPP11_CFENV 1
#endif
#endif
#undef HALF_GCC_VERSION
#undef HALF_ICC_VERSION
// any error throwing C++ exceptions?
#if defined(HALF_ERRHANDLING_THROW_INVALID) || defined(HALF_ERRHANDLING_THROW_DIVBYZERO) || defined(HALF_ERRHANDLING_THROW_OVERFLOW) || defined(HALF_ERRHANDLING_THROW_UNDERFLOW) || defined(HALF_ERRHANDLING_THROW_INEXACT)
#define HALF_ERRHANDLING_THROWS 1
#endif
// any error handling enabled?
#define HALF_ERRHANDLING (HALF_ERRHANDLING_FLAGS||HALF_ERRHANDLING_ERRNO||HALF_ERRHANDLING_FENV||HALF_ERRHANDLING_THROWS)
#if HALF_ERRHANDLING
#define HALF_UNUSED_NOERR(name) name
#else
#define HALF_UNUSED_NOERR(name)
#endif
// support constexpr
#if HALF_ENABLE_CPP11_CONSTEXPR
#define HALF_CONSTEXPR constexpr
#define HALF_CONSTEXPR_CONST constexpr
#if HALF_ERRHANDLING
#define HALF_CONSTEXPR_NOERR
#else
#define HALF_CONSTEXPR_NOERR constexpr
#endif
#else
#define HALF_CONSTEXPR
#define HALF_CONSTEXPR_CONST const
#define HALF_CONSTEXPR_NOERR
#endif
// support noexcept
#if HALF_ENABLE_CPP11_NOEXCEPT
#define HALF_NOEXCEPT noexcept
#define HALF_NOTHROW noexcept
#else
#define HALF_NOEXCEPT
#define HALF_NOTHROW throw()
#endif
// support thread storage
#if HALF_ENABLE_CPP11_THREAD_LOCAL
#define HALF_THREAD_LOCAL thread_local
#else
#define HALF_THREAD_LOCAL static
#endif
#include <utility>
#include <algorithm>
#include <istream>
#include <ostream>
#include <limits>
#include <stdexcept>
#include <climits>
#include <cmath>
#include <cstring>
#include <cstdlib>
#if HALF_ENABLE_CPP11_TYPE_TRAITS
#include <type_traits>
#endif
#if HALF_ENABLE_CPP11_CSTDINT
#include <cstdint>
#endif
#if HALF_ERRHANDLING_ERRNO
#include <cerrno>
#endif
#if HALF_ENABLE_CPP11_CFENV
#include <cfenv>
#endif
#if HALF_ENABLE_CPP11_HASH
#include <functional>
#endif
#if HALF_ENABLE_F16C_INTRINSICS
#include <immintrin.h>
#endif
#ifndef HALF_ENABLE_F16C_INTRINSICS
/// Enable F16C intruction set intrinsics.
/// Defining this to 1 enables the use of [F16C compiler intrinsics](https://en.wikipedia.org/wiki/F16C) for converting between
/// half-precision and single-precision values which may result in improved performance. This will not perform additional checks
/// for support of the F16C instruction set, so an appropriate target platform is required when enabling this feature.
///
/// Unless predefined it will be enabled automatically when the `__F16C__` symbol is defined, which some compilers do on supporting platforms.
#define HALF_ENABLE_F16C_INTRINSICS __F16C__
#endif
#ifdef HALF_DOXYGEN_ONLY
/// Type for internal floating-point computations.
/// This can be predefined to a built-in floating-point type (`float`, `double` or `long double`) to override the internal
/// half-precision implementation to use this type for computing arithmetic operations and mathematical function (if available).
/// This can result in improved performance for arithmetic operators and mathematical functions but might cause results to
/// deviate from the specified half-precision rounding mode and inhibits proper detection of half-precision exceptions.
#define HALF_ARITHMETIC_TYPE (undefined)
/// Enable internal exception flags.
/// Defining this to 1 causes operations on half-precision values to raise internal floating-point exception flags according to
/// the IEEE 754 standard. These can then be cleared and checked with clearexcept(), testexcept().
#define HALF_ERRHANDLING_FLAGS 0
/// Enable exception propagation to `errno`.
/// Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to
/// [errno](https://en.cppreference.com/w/cpp/error/errno) from `<cerrno>`. Specifically this will propagate domain errors as
/// [EDOM](https://en.cppreference.com/w/cpp/error/errno_macros) and pole, overflow and underflow errors as
/// [ERANGE](https://en.cppreference.com/w/cpp/error/errno_macros). Inexact errors won't be propagated.
#define HALF_ERRHANDLING_ERRNO 0
/// Enable exception propagation to built-in floating-point platform.
/// Defining this to 1 causes operations on half-precision values to propagate floating-point exceptions to the built-in
/// single- and double-precision implementation's exception flags using the
/// [C++11 floating-point environment control](https://en.cppreference.com/w/cpp/numeric/fenv) from `<cfenv>`. However, this
/// does not work in reverse and single- or double-precision exceptions will not raise the corresponding half-precision
/// exception flags, nor will explicitly clearing flags clear the corresponding built-in flags.
#define HALF_ERRHANDLING_FENV 0
/// Throw C++ exception on domain errors.
/// Defining this to a string literal causes operations on half-precision values to throw a
/// [std::domain_error](https://en.cppreference.com/w/cpp/error/domain_error) with the specified message on domain errors.
#define HALF_ERRHANDLING_THROW_INVALID (undefined)
/// Throw C++ exception on pole errors.
/// Defining this to a string literal causes operations on half-precision values to throw a
/// [std::domain_error](https://en.cppreference.com/w/cpp/error/domain_error) with the specified message on pole errors.
#define HALF_ERRHANDLING_THROW_DIVBYZERO (undefined)
/// Throw C++ exception on overflow errors.
/// Defining this to a string literal causes operations on half-precision values to throw a
/// [std::overflow_error](https://en.cppreference.com/w/cpp/error/overflow_error) with the specified message on overflows.
#define HALF_ERRHANDLING_THROW_OVERFLOW (undefined)
/// Throw C++ exception on underflow errors.
/// Defining this to a string literal causes operations on half-precision values to throw a
/// [std::underflow_error](https://en.cppreference.com/w/cpp/error/underflow_error) with the specified message on underflows.
#define HALF_ERRHANDLING_THROW_UNDERFLOW (undefined)
/// Throw C++ exception on rounding errors.
/// Defining this to 1 causes operations on half-precision values to throw a
/// [std::range_error](https://en.cppreference.com/w/cpp/error/range_error) with the specified message on general rounding errors.
#define HALF_ERRHANDLING_THROW_INEXACT (undefined)
#endif
#ifndef HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
/// Raise INEXACT exception on overflow.
/// Defining this to 1 (default) causes overflow errors to automatically raise inexact exceptions in addition.
/// These will be raised after any possible handling of the underflow exception.
#define HALF_ERRHANDLING_OVERFLOW_TO_INEXACT 1
#endif
#ifndef HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
/// Raise INEXACT exception on underflow.
/// Defining this to 1 (default) causes underflow errors to automatically raise inexact exceptions in addition.
/// These will be raised after any possible handling of the underflow exception.
///
/// **Note:** This will actually cause underflow (and the accompanying inexact) exceptions to be raised *only* when the result
/// is inexact, while if disabled bare underflow errors will be raised for *any* (possibly exact) subnormal result.
#define HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT 1
#endif
/// Default rounding mode.
/// This specifies the rounding mode used for all conversions between [half](\ref half_float::half)s and more precise types
/// (unless using half_cast() and specifying the rounding mode directly) as well as in arithmetic operations and mathematical
/// functions. It can be redefined (before including half.hpp) to one of the standard rounding modes using their respective
/// constants or the equivalent values of
/// [std::float_round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/float_round_style):
///
/// `std::float_round_style` | value | rounding
/// ---------------------------------|-------|-------------------------
/// `std::round_indeterminate` | -1 | fastest
/// `std::round_toward_zero` | 0 | toward zero
/// `std::round_to_nearest` | 1 | to nearest (default)
/// `std::round_toward_infinity` | 2 | toward positive infinity
/// `std::round_toward_neg_infinity` | 3 | toward negative infinity
///
/// By default this is set to `1` (`std::round_to_nearest`), which rounds results to the nearest representable value. It can even
/// be set to [std::numeric_limits<float>::round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/round_style) to synchronize
/// the rounding mode with that of the built-in single-precision implementation (which is likely `std::round_to_nearest`, though).
#ifndef HALF_ROUND_STYLE
#define HALF_ROUND_STYLE 1 // = std::round_to_nearest
#endif
/// Value signaling overflow.
/// In correspondence with `HUGE_VAL[F|L]` from `<cmath>` this symbol expands to a positive value signaling the overflow of an
/// operation, in particular it just evaluates to positive infinity.
///
/// **See also:** Documentation for [HUGE_VAL](https://en.cppreference.com/w/cpp/numeric/math/HUGE_VAL)
#define HUGE_VALH std::numeric_limits<half_float::half>::infinity()
/// Fast half-precision fma function.
/// This symbol is defined if the fma() function generally executes as fast as, or faster than, a separate
/// half-precision multiplication followed by an addition, which is always the case.
///
/// **See also:** Documentation for [FP_FAST_FMA](https://en.cppreference.com/w/cpp/numeric/math/fma)
#define FP_FAST_FMAH 1
/// Half rounding mode.
/// In correspondence with `FLT_ROUNDS` from `<cfloat>` this symbol expands to the rounding mode used for
/// half-precision operations. It is an alias for [HALF_ROUND_STYLE](\ref HALF_ROUND_STYLE).
///
/// **See also:** Documentation for [FLT_ROUNDS](https://en.cppreference.com/w/cpp/types/climits/FLT_ROUNDS)
#define HLF_ROUNDS HALF_ROUND_STYLE
#ifndef FP_ILOGB0
#define FP_ILOGB0 INT_MIN
#endif
#ifndef FP_ILOGBNAN
#define FP_ILOGBNAN INT_MAX
#endif
#ifndef FP_SUBNORMAL
#define FP_SUBNORMAL 0
#endif
#ifndef FP_ZERO
#define FP_ZERO 1
#endif
#ifndef FP_NAN
#define FP_NAN 2
#endif
#ifndef FP_INFINITE
#define FP_INFINITE 3
#endif
#ifndef FP_NORMAL
#define FP_NORMAL 4
#endif
#if !HALF_ENABLE_CPP11_CFENV && !defined(FE_ALL_EXCEPT)
#define FE_INVALID 0x10
#define FE_DIVBYZERO 0x08
#define FE_OVERFLOW 0x04
#define FE_UNDERFLOW 0x02
#define FE_INEXACT 0x01
#define FE_ALL_EXCEPT (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW|FE_INEXACT)
#endif
/// Main namespace for half-precision functionality.
/// This namespace contains all the functionality provided by the library.
namespace half_float
{
class half;
#if HALF_ENABLE_CPP11_USER_LITERALS
/// Library-defined half-precision literals.
/// Import this namespace to enable half-precision floating-point literals:
/// ~~~~{.cpp}
/// using namespace half_float::literal;
/// half_float::half = 4.2_h;
/// ~~~~
namespace literal
{
half operator "" _h(long double);
}
#endif
/// \internal
/// \brief Implementation details.
namespace detail
{
#if HALF_ENABLE_CPP11_TYPE_TRAITS
/// Conditional type.
template<bool B,typename T,typename F> struct conditional : std::conditional<B,T,F> {};
/// Helper for tag dispatching.
template<bool B> struct bool_type : std::integral_constant<bool,B> {};
using std::true_type;
using std::false_type;
/// Type traits for floating-point types.
template<typename T> struct is_float : std::is_floating_point<T> {};
#else
/// Conditional type.
template<bool,typename T,typename> struct conditional { typedef T type; };
template<typename T,typename F> struct conditional<false,T,F> { typedef F type; };
/// Helper for tag dispatching.
template<bool> struct bool_type {};
typedef bool_type<true> true_type;
typedef bool_type<false> false_type;
/// Type traits for floating-point types.
template<typename> struct is_float : false_type {};
template<typename T> struct is_float<const T> : is_float<T> {};
template<typename T> struct is_float<volatile T> : is_float<T> {};
template<typename T> struct is_float<const volatile T> : is_float<T> {};
template<> struct is_float<float> : true_type {};
template<> struct is_float<double> : true_type {};
template<> struct is_float<long double> : true_type {};
#endif
/// Type traits for floating-point bits.
template<typename T> struct bits { typedef unsigned char type; };
template<typename T> struct bits<const T> : bits<T> {};
template<typename T> struct bits<volatile T> : bits<T> {};
template<typename T> struct bits<const volatile T> : bits<T> {};
#if HALF_ENABLE_CPP11_CSTDINT
/// Unsigned integer of (at least) 16 bits width.
typedef std::uint_least16_t uint16;
/// Fastest unsigned integer of (at least) 32 bits width.
typedef std::uint_fast32_t uint32;
/// Fastest signed integer of (at least) 32 bits width.
typedef std::int_fast32_t int32;
/// Unsigned integer of (at least) 32 bits width.
template<> struct bits<float> { typedef std::uint_least32_t type; };
/// Unsigned integer of (at least) 64 bits width.
template<> struct bits<double> { typedef std::uint_least64_t type; };
#else
/// Unsigned integer of (at least) 16 bits width.
typedef unsigned short uint16;
/// Fastest unsigned integer of (at least) 32 bits width.
typedef unsigned long uint32;
/// Fastest unsigned integer of (at least) 32 bits width.
typedef long int32;
/// Unsigned integer of (at least) 32 bits width.
template<> struct bits<float> : conditional<std::numeric_limits<unsigned int>::digits>=32,unsigned int,unsigned long> {};
#if HALF_ENABLE_CPP11_LONG_LONG
/// Unsigned integer of (at least) 64 bits width.
template<> struct bits<double> : conditional<std::numeric_limits<unsigned long>::digits>=64,unsigned long,unsigned long long> {};
#else
/// Unsigned integer of (at least) 64 bits width.
template<> struct bits<double> { typedef unsigned long type; };
#endif
#endif
#ifdef HALF_ARITHMETIC_TYPE
/// Type to use for arithmetic computations and mathematic functions internally.
typedef HALF_ARITHMETIC_TYPE internal_t;
#endif
/// Tag type for binary construction.
struct binary_t {};
/// Tag for binary construction.
HALF_CONSTEXPR_CONST binary_t binary = binary_t();
/// \name Implementation defined classification and arithmetic
/// \{
/// Check for infinity.
/// \tparam T argument type (builtin floating-point type)
/// \param arg value to query
/// \retval true if infinity
/// \retval false else
template<typename T> bool builtin_isinf(T arg)
{
#if HALF_ENABLE_CPP11_CMATH
return std::isinf(arg);
#elif defined(_MSC_VER)
return !::_finite(static_cast<double>(arg)) && !::_isnan(static_cast<double>(arg));
#else
return arg == std::numeric_limits<T>::infinity() || arg == -std::numeric_limits<T>::infinity();
#endif
}
/// Check for NaN.
/// \tparam T argument type (builtin floating-point type)
/// \param arg value to query
/// \retval true if not a number
/// \retval false else
template<typename T> bool builtin_isnan(T arg)
{
#if HALF_ENABLE_CPP11_CMATH
return std::isnan(arg);
#elif defined(_MSC_VER)
return ::_isnan(static_cast<double>(arg)) != 0;
#else
return arg != arg;
#endif
}
/// Check sign.
/// \tparam T argument type (builtin floating-point type)
/// \param arg value to query
/// \retval true if signbit set
/// \retval false else
template<typename T> bool builtin_signbit(T arg)
{
#if HALF_ENABLE_CPP11_CMATH
return std::signbit(arg);
#else
return arg < T() || (arg == T() && T(1)/arg < T());
#endif
}
/// Platform-independent sign mask.
/// \param arg integer value in two's complement
/// \retval -1 if \a arg negative
/// \retval 0 if \a arg positive
inline uint32 sign_mask(uint32 arg)
{
static const int N = std::numeric_limits<uint32>::digits - 1;
#if HALF_TWOS_COMPLEMENT_INT
return static_cast<int32>(arg) >> N;
#else
return -((arg>>N)&1);
#endif
}
/// Platform-independent arithmetic right shift.
/// \param arg integer value in two's complement
/// \param i shift amount (at most 31)
/// \return \a arg right shifted for \a i bits with possible sign extension
inline uint32 arithmetic_shift(uint32 arg, int i)
{
#if HALF_TWOS_COMPLEMENT_INT
return static_cast<int32>(arg) >> i;
#else
return static_cast<int32>(arg)/(static_cast<int32>(1)<<i) - ((arg>>(std::numeric_limits<uint32>::digits-1))&1);
#endif
}
/// \}
/// \name Error handling
/// \{
/// Internal exception flags.
/// \return reference to global exception flags
inline int& errflags() { HALF_THREAD_LOCAL int flags = 0; return flags; }
/// Raise floating-point exception.
/// \param flags exceptions to raise
/// \param cond condition to raise exceptions for
inline void raise(int HALF_UNUSED_NOERR(flags), bool HALF_UNUSED_NOERR(cond) = true)
{
#if HALF_ERRHANDLING
if(!cond)
return;
#if HALF_ERRHANDLING_FLAGS
errflags() |= flags;
#endif
#if HALF_ERRHANDLING_ERRNO
if(flags & FE_INVALID)
errno = EDOM;
else if(flags & (FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW))
errno = ERANGE;
#endif
#if HALF_ERRHANDLING_FENV && HALF_ENABLE_CPP11_CFENV
std::feraiseexcept(flags);
#endif
#ifdef HALF_ERRHANDLING_THROW_INVALID
if(flags & FE_INVALID)
throw std::domain_error(HALF_ERRHANDLING_THROW_INVALID);
#endif
#ifdef HALF_ERRHANDLING_THROW_DIVBYZERO
if(flags & FE_DIVBYZERO)
throw std::domain_error(HALF_ERRHANDLING_THROW_DIVBYZERO);
#endif
#ifdef HALF_ERRHANDLING_THROW_OVERFLOW
if(flags & FE_OVERFLOW)
throw std::overflow_error(HALF_ERRHANDLING_THROW_OVERFLOW);
#endif
#ifdef HALF_ERRHANDLING_THROW_UNDERFLOW
if(flags & FE_UNDERFLOW)
throw std::underflow_error(HALF_ERRHANDLING_THROW_UNDERFLOW);
#endif
#ifdef HALF_ERRHANDLING_THROW_INEXACT
if(flags & FE_INEXACT)
throw std::range_error(HALF_ERRHANDLING_THROW_INEXACT);
#endif
#if HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
if((flags & FE_UNDERFLOW) && !(flags & FE_INEXACT))
raise(FE_INEXACT);
#endif
#if HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
if((flags & FE_OVERFLOW) && !(flags & FE_INEXACT))
raise(FE_INEXACT);
#endif
#endif
}
/// Check and signal for any NaN.
/// \param x first half-precision value to check
/// \param y second half-precision value to check
/// \retval true if either \a x or \a y is NaN
/// \retval false else
/// \exception FE_INVALID if \a x or \a y is NaN
inline HALF_CONSTEXPR_NOERR bool compsignal(unsigned int x, unsigned int y)
{
#if HALF_ERRHANDLING
raise(FE_INVALID, (x&0x7FFF)>0x7C00 || (y&0x7FFF)>0x7C00);
#endif
return (x&0x7FFF) > 0x7C00 || (y&0x7FFF) > 0x7C00;
}
/// Signal and silence signaling NaN.
/// \param nan half-precision NaN value
/// \return quiet NaN
/// \exception FE_INVALID if \a nan is signaling NaN
inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int nan)
{
#if HALF_ERRHANDLING
raise(FE_INVALID, !(nan&0x200));
#endif
return nan | 0x200;
}
/// Signal and silence signaling NaNs.
/// \param x first half-precision value to check
/// \param y second half-precision value to check
/// \return quiet NaN
/// \exception FE_INVALID if \a x or \a y is signaling NaN
inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int x, unsigned int y)
{
#if HALF_ERRHANDLING
raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)));
#endif
return ((x&0x7FFF)>0x7C00) ? (x|0x200) : (y|0x200);
}
/// Signal and silence signaling NaNs.
/// \param x first half-precision value to check
/// \param y second half-precision value to check
/// \param z third half-precision value to check
/// \return quiet NaN
/// \exception FE_INVALID if \a x, \a y or \a z is signaling NaN
inline HALF_CONSTEXPR_NOERR unsigned int signal(unsigned int x, unsigned int y, unsigned int z)
{
#if HALF_ERRHANDLING
raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)) || ((z&0x7FFF)>0x7C00 && !(z&0x200)));
#endif
return ((x&0x7FFF)>0x7C00) ? (x|0x200) : ((y&0x7FFF)>0x7C00) ? (y|0x200) : (z|0x200);
}
/// Select value or signaling NaN.
/// \param x preferred half-precision value
/// \param y ignored half-precision value except for signaling NaN
/// \return \a y if signaling NaN, \a x otherwise
/// \exception FE_INVALID if \a y is signaling NaN
inline HALF_CONSTEXPR_NOERR unsigned int select(unsigned int x, unsigned int HALF_UNUSED_NOERR(y))
{
#if HALF_ERRHANDLING
return (((y&0x7FFF)>0x7C00) && !(y&0x200)) ? signal(y) : x;
#else
return x;
#endif
}
/// Raise domain error and return NaN.
/// return quiet NaN
/// \exception FE_INVALID
inline HALF_CONSTEXPR_NOERR unsigned int invalid()
{
#if HALF_ERRHANDLING
raise(FE_INVALID);
#endif
return 0x7FFF;
}
/// Raise pole error and return infinity.
/// \param sign half-precision value with sign bit only
/// \return half-precision infinity with sign of \a sign
/// \exception FE_DIVBYZERO
inline HALF_CONSTEXPR_NOERR unsigned int pole(unsigned int sign = 0)
{
#if HALF_ERRHANDLING
raise(FE_DIVBYZERO);
#endif
return sign | 0x7C00;
}
/// Check value for underflow.
/// \param arg non-zero half-precision value to check
/// \return \a arg
/// \exception FE_UNDERFLOW if arg is subnormal
inline HALF_CONSTEXPR_NOERR unsigned int check_underflow(unsigned int arg)
{
#if HALF_ERRHANDLING && !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
raise(FE_UNDERFLOW, !(arg&0x7C00));
#endif
return arg;
}
/// \}
/// \name Conversion and rounding
/// \{
/// Half-precision overflow.
/// \tparam R rounding mode to use
/// \param sign half-precision value with sign bit only
/// \return rounded overflowing half-precision value
/// \exception FE_OVERFLOW
template<std::float_round_style R> HALF_CONSTEXPR_NOERR unsigned int overflow(unsigned int sign = 0)
{
#if HALF_ERRHANDLING
raise(FE_OVERFLOW);
#endif
return (R==std::round_toward_infinity) ? (sign+0x7C00-(sign>>15)) :
(R==std::round_toward_neg_infinity) ? (sign+0x7BFF+(sign>>15)) :
(R==std::round_toward_zero) ? (sign|0x7BFF) :
(sign|0x7C00);
}
/// Half-precision underflow.
/// \tparam R rounding mode to use
/// \param sign half-precision value with sign bit only
/// \return rounded underflowing half-precision value
/// \exception FE_UNDERFLOW
template<std::float_round_style R> HALF_CONSTEXPR_NOERR unsigned int underflow(unsigned int sign = 0)
{
#if HALF_ERRHANDLING
raise(FE_UNDERFLOW);
#endif
return (R==std::round_toward_infinity) ? (sign+1-(sign>>15)) :
(R==std::round_toward_neg_infinity) ? (sign+(sign>>15)) :
sign;
}
/// Round half-precision number.
/// \tparam R rounding mode to use
/// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
/// \param value finite half-precision number to round
/// \param g guard bit (most significant discarded bit)
/// \param s sticky bit (or of all but the most significant discarded bits)
/// \return rounded half-precision value
/// \exception FE_OVERFLOW on overflows
/// \exception FE_UNDERFLOW on underflows
/// \exception FE_INEXACT if value had to be rounded or \a I is `true`
template<std::float_round_style R,bool I> HALF_CONSTEXPR_NOERR unsigned int rounded(unsigned int value, int g, int s)
{
#if HALF_ERRHANDLING
value += (R==std::round_to_nearest) ? (g&(s|value)) :
(R==std::round_toward_infinity) ? (~(value>>15)&(g|s)) :
(R==std::round_toward_neg_infinity) ? ((value>>15)&(g|s)) : 0;
if((value&0x7C00) == 0x7C00)
raise(FE_OVERFLOW);
else if(value & 0x7C00)
raise(FE_INEXACT, I || (g|s)!=0);
else
raise(FE_UNDERFLOW, !(HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT) || I || (g|s)!=0);
return value;
#else
return (R==std::round_to_nearest) ? (value+(g&(s|value))) :
(R==std::round_toward_infinity) ? (value+(~(value>>15)&(g|s))) :
(R==std::round_toward_neg_infinity) ? (value+((value>>15)&(g|s))) :
value;
#endif
}
/// Round half-precision number to nearest integer value.
/// \tparam R rounding mode to use
/// \tparam E `true` for round to even, `false` for round away from zero
/// \tparam I `true` to raise INEXACT exception (if inexact), `false` to never raise it
/// \param value half-precision value to round
/// \return half-precision bits for nearest integral value
/// \exception FE_INVALID for signaling NaN
/// \exception FE_INEXACT if value had to be rounded and \a I is `true`
template<std::float_round_style R,bool E,bool I> unsigned int integral(unsigned int value)
{
unsigned int abs = value & 0x7FFF;
if(abs < 0x3C00)
{
raise(FE_INEXACT, I);
return ((R==std::round_to_nearest) ? (0x3C00&-static_cast<unsigned>(abs>=(0x3800+E))) :
(R==std::round_toward_infinity) ? (0x3C00&-(~(value>>15)&(abs!=0))) :
(R==std::round_toward_neg_infinity) ? (0x3C00&-static_cast<unsigned>(value>0x8000)) :
0) | (value&0x8000);
}
if(abs >= 0x6400)
return (abs>0x7C00) ? signal(value) : value;
unsigned int exp = 25 - (abs>>10), mask = (1<<exp) - 1;
raise(FE_INEXACT, I && (value&mask));
return (( (R==std::round_to_nearest) ? ((1<<(exp-1))-(~(value>>exp)&E)) :
(R==std::round_toward_infinity) ? (mask&((value>>15)-1)) :
(R==std::round_toward_neg_infinity) ? (mask&-(value>>15)) :
0) + value) & ~mask;
}
/// Convert fixed point to half-precision floating-point.
/// \tparam R rounding mode to use
/// \tparam F number of fractional bits (at least 11)
/// \tparam S `true` for signed, `false` for unsigned
/// \tparam N `true` for additional normalization step, `false` if already normalized to 1.F
/// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
/// \param m mantissa in Q1.F fixed point format
/// \param exp exponent
/// \param sign half-precision value with sign bit only
/// \param s sticky bit (or of all but the most significant already discarded bits)
/// \return value converted to half-precision
/// \exception FE_OVERFLOW on overflows
/// \exception FE_UNDERFLOW on underflows
/// \exception FE_INEXACT if value had to be rounded or \a I is `true`
template<std::float_round_style R,unsigned int F,bool S,bool N,bool I> unsigned int fixed2half(uint32 m, int exp = 14, unsigned int sign = 0, int s = 0)
{
if(S)
{
uint32 msign = sign_mask(m);
m = (m^msign) - msign;
sign = msign & 0x8000;
}
if(N)
for(; m<(static_cast<uint32>(1)<<F) && exp; m<<=1,--exp) ;
else if(exp < 0)
return rounded<R,I>(sign+(m>>(F-10-exp)), (m>>(F-11-exp))&1, s|((m&((static_cast<uint32>(1)<<(F-11-exp))-1))!=0));
return rounded<R,I>(sign+(exp<<10)+(m>>(F-10)), (m>>(F-11))&1, s|((m&((static_cast<uint32>(1)<<(F-11))-1))!=0));
}
/// Convert IEEE single-precision to half-precision.
/// Credit for this goes to [Jeroen van der Zijp](ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf).
/// \tparam R rounding mode to use
/// \param value single-precision value to convert
/// \return rounded half-precision value
/// \exception FE_OVERFLOW on overflows
/// \exception FE_UNDERFLOW on underflows
/// \exception FE_INEXACT if value had to be rounded
template<std::float_round_style R> unsigned int float2half_impl(float value, true_type)
{
#if HALF_ENABLE_F16C_INTRINSICS
return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(value),
(R==std::round_to_nearest) ? _MM_FROUND_TO_NEAREST_INT :
(R==std::round_toward_zero) ? _MM_FROUND_TO_ZERO :
(R==std::round_toward_infinity) ? _MM_FROUND_TO_POS_INF :
(R==std::round_toward_neg_infinity) ? _MM_FROUND_TO_NEG_INF :
_MM_FROUND_CUR_DIRECTION));
#else
bits<float>::type fbits;
std::memcpy(&fbits, &value, sizeof(float));
#if 1
unsigned int sign = (fbits>>16) & 0x8000;
fbits &= 0x7FFFFFFF;
if(fbits >= 0x7F800000)
return sign | 0x7C00 | ((fbits>0x7F800000) ? (0x200|((fbits>>13)&0x3FF)) : 0);
if(fbits >= 0x47800000)
return overflow<R>(sign);
if(fbits >= 0x38800000)
return rounded<R,false>(sign|(((fbits>>23)-112)<<10)|((fbits>>13)&0x3FF), (fbits>>12)&1, (fbits&0xFFF)!=0);
if(fbits >= 0x33000000)
{
int i = 125 - (fbits>>23);
fbits = (fbits&0x7FFFFF) | 0x800000;
return rounded<R,false>(sign|(fbits>>(i+1)), (fbits>>i)&1, (fbits&((static_cast<uint32>(1)<<i)-1))!=0);
}
if(fbits != 0)
return underflow<R>(sign);
return sign;
#else
static const uint16 base_table[512] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
0x0200, 0x0400, 0x0800, 0x0C00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x2400, 0x2800, 0x2C00, 0x3000, 0x3400, 0x3800, 0x3C00,
0x4000, 0x4400, 0x4800, 0x4C00, 0x5000, 0x5400, 0x5800, 0x5C00, 0x6000, 0x6400, 0x6800, 0x6C00, 0x7000, 0x7400, 0x7800, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7C00,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8001, 0x8002, 0x8004, 0x8008, 0x8010, 0x8020, 0x8040, 0x8080, 0x8100,
0x8200, 0x8400, 0x8800, 0x8C00, 0x9000, 0x9400, 0x9800, 0x9C00, 0xA000, 0xA400, 0xA800, 0xAC00, 0xB000, 0xB400, 0xB800, 0xBC00,
0xC000, 0xC400, 0xC800, 0xCC00, 0xD000, 0xD400, 0xD800, 0xDC00, 0xE000, 0xE400, 0xE800, 0xEC00, 0xF000, 0xF400, 0xF800, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFC00 };
static const unsigned char shift_table[256] = {
24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 13 };
int sexp = fbits >> 23, exp = sexp & 0xFF, i = shift_table[exp];
fbits &= 0x7FFFFF;
uint32 m = (fbits|((exp!=0)<<23)) & -static_cast<uint32>(exp!=0xFF);
return rounded<R,false>(base_table[sexp]+(fbits>>i), (m>>(i-1))&1, (((static_cast<uint32>(1)<<(i-1))-1)&m)!=0);
#endif
#endif
}
/// Convert IEEE double-precision to half-precision.
/// \tparam R rounding mode to use
/// \param value double-precision value to convert
/// \return rounded half-precision value
/// \exception FE_OVERFLOW on overflows
/// \exception FE_UNDERFLOW on underflows
/// \exception FE_INEXACT if value had to be rounded
template<std::float_round_style R> unsigned int float2half_impl(double value, true_type)
{
#if HALF_ENABLE_F16C_INTRINSICS
if(R == std::round_indeterminate)
return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_cvtpd_ps(_mm_set_sd(value)), _MM_FROUND_CUR_DIRECTION));
#endif