File: FloatSafe.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (1190 lines) | stat: -rw-r--r-- 47,730 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#pragma once

#include <limits.h>
#include <cmath>

namespace iSTD
{
/*****************************************************************************\
Constants:
    FPU_FLOAT32_*

Description:
    Binary representation of 32-bit floating point specials.
    FPU_FLOAT32_COMPUTE special value can be used in result tables to mark
    cases, where final value should be computed normally.
\*****************************************************************************/
const DWORD FPU_FLOAT32_NAN         = 0x7FFFFFFF;
const DWORD FPU_FLOAT32_NEG_INF     = 0xFF800000;
const DWORD FPU_FLOAT32_POS_INF     = 0x7F800000;
const DWORD FPU_FLOAT32_NEG_ZERO    = 0x80000000;
const DWORD FPU_FLOAT32_POS_ZERO    = 0x00000000;
const DWORD FPU_FLOAT32_COMPUTE     = 0xFFFFFFFF;
const DWORD FPU_FLOAT32_ONE         = (DWORD) 0x3F800000;
const DWORD FPU_FLOAT32_MINUS_ONE   = (DWORD) 0xBF800000;


/*****************************************************************************\
Enumeration:
    FPU_FLOAT_CLASS

Description:
    Classes of floating point numbers.
    (+0, -0, +finite, -finite, +Inf, -Inf, NaN, -denorm, +denorm)
\*****************************************************************************/
enum FPU_FLOAT_CLASS {
    FPU_FLOAT_CLASS_NEG_INF      = 0,
    FPU_FLOAT_CLASS_NEG_FINITE   = 1,
    FPU_FLOAT_CLASS_NEG_DENORM   = 2,
    FPU_FLOAT_CLASS_NEG_ZERO     = 3,
    FPU_FLOAT_CLASS_POS_ZERO     = 4,
    FPU_FLOAT_CLASS_POS_DENORM   = 5,
    FPU_FLOAT_CLASS_POS_FINITE   = 6,
    FPU_FLOAT_CLASS_POS_INF      = 7,
    FPU_FLOAT_CLASS_NAN          = 8,
    NUM_FPU_FLOAT_CLASSES        = 9
};

/*****************************************************************************\
Inline Function:
    Float32GetClass

Description:
    Returns class (+0, -0, +finite, -finite, +Inf, -Inf, NaN) of 32-bit float.
\*****************************************************************************/
inline FPU_FLOAT_CLASS Float32GetClass( const float f )
{
    FLOAT32 f32;
    f32.value.f = f;

    switch( f32.value.u )
    {
    case FPU_FLOAT32_POS_ZERO:  return FPU_FLOAT_CLASS_POS_ZERO;
    case FPU_FLOAT32_NEG_ZERO:  return FPU_FLOAT_CLASS_NEG_ZERO;
    case FPU_FLOAT32_POS_INF:   return FPU_FLOAT_CLASS_POS_INF;
    case FPU_FLOAT32_NEG_INF:   return FPU_FLOAT_CLASS_NEG_INF;
    default:                    break;
    }

    if( f32.exponent == 0xFF )
    {
        return FPU_FLOAT_CLASS_NAN;
    }
    else if( f32.exponent == 0x00 )
    {
        if( f32.sign == 0 )
        {
            return FPU_FLOAT_CLASS_POS_DENORM;
        }
        else
        {
            return FPU_FLOAT_CLASS_NEG_DENORM;
        }
    }

    if( f32.sign )
    {
        return FPU_FLOAT_CLASS_NEG_FINITE;
    }

    return FPU_FLOAT_CLASS_POS_FINITE;
}

/*****************************************************************************\
Inline Function:
    Float32IsInfinity

Description:
    Returns true if class is +Inf or -Inf of 32-bit float.
\*****************************************************************************/
inline bool Float32IsInfinity( const float f )
{
    FPU_FLOAT_CLASS fClass = Float32GetClass( f );

    return ( fClass == FPU_FLOAT_CLASS_POS_INF ) ||
           ( fClass == FPU_FLOAT_CLASS_NEG_INF );
}

/*****************************************************************************\
Inline Function:
    Float32IsDenorm

Description:
    Returns true if class is +Denorm or -Denorm.
\*****************************************************************************/
inline bool Float32IsDenorm( const float f )
{
    FPU_FLOAT_CLASS fClass = Float32GetClass( f );

    return ( fClass == FPU_FLOAT_CLASS_NEG_DENORM ) ||
           ( fClass == FPU_FLOAT_CLASS_POS_DENORM );
}

/*****************************************************************************\

Inline Function:
    Float32IsFinite

Description:
    Returns true if f is finite: not +/-INF, and not NaN.
\*****************************************************************************/
inline bool Float32IsFinite( const float f )
{
    FPU_FLOAT_CLASS fClass = Float32GetClass( f );

    return ( fClass != FPU_FLOAT_CLASS_NAN )     &&
           ( fClass != FPU_FLOAT_CLASS_NEG_INF ) &&
           ( fClass != FPU_FLOAT_CLASS_POS_INF );
}

/*****************************************************************************\
Inline Function:
    IsFPZero

Description:
    Returns true if the argument x seen as a 32-bit IEEE754 floating point
    number is either positive or negative zero  +0.0, -0.0.

Input:
    dword value that will be interpreted as a binary32 representation
    of single-precision floating point value.

Output:
    True if the value represents either positive or negative float zero.

\*****************************************************************************/    
inline bool IsFPZero( const DWORD x )
{
    return ( x == iSTD::FPU_FLOAT32_POS_ZERO ) || 
           ( x == iSTD::FPU_FLOAT32_NEG_ZERO );
}

/*****************************************************************************\
Inline Function:
    Float32SafeAdd

Description:
    Performs addition taking care of floating point specials in software.
\*****************************************************************************/
inline float Float32SafeAdd( const float arg1, const float arg2, const bool denormRetain  )
{
    // Table for handling IEEE 754 specials in addition
    //
    //  a + b       -Inf    -X      -0      +0      +X      +Inf    NaN
    //
    //  -Inf        -Inf    -Inf    -Inf    -Inf    -Inf    NaN     NaN
    //  -X          -Inf    <add>   <add>   <add>   <add>   +Inf    NaN
    //  -0          -Inf    <add>   -0      +0      <add>   +Inf    NaN
    //  +0          -Inf    <add>   +0      +0      <add>   +Inf    NaN
    //  +X          -Inf    <add>   <add>   <add>   <add>   +Inf    NaN
    //  +Inf        NaN     +Inf    +Inf    +Inf    +Inf    +Inf    NaN
    //  NaN         NaN     NaN     NaN     NaN     NaN     NaN     NaN
    //

    static const DWORD RESULT[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf                  -X                    -denorm               -0                    +0                    +denorm               +X                    +Inf                  NaN
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF  , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // -Inf
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE  , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // -X
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_ZERO , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // -denorm
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_ZERO , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // -0
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_ZERO , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +0
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_ZERO , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +denorm
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE  , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +X
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF  , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +Inf
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // NaN
    };

    const FPU_FLOAT_CLASS t1 = Float32GetClass( arg1 );
    const FPU_FLOAT_CLASS t2 = Float32GetClass( arg2 );

    FLOAT32 f32;
    f32.value.u = RESULT[ t1 ][ t2 ];

    bool computeDenorms = ( denormRetain && ( Float32IsDenorm( arg1 ) || Float32IsDenorm( arg2 ) ) );

    if( ( f32.value.u == FPU_FLOAT32_COMPUTE ) || ( computeDenorms ) )
    {
        return arg1 + arg2;
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    Float32SafeSubtract

Description:
    Performs subtraction taking care of floating point specials in software.
\*****************************************************************************/
inline float Float32SafeSubtract( const float arg1, const float arg2, const bool denormRetain )
{
    FLOAT32 f32;
    f32.value.f = arg2;

    // flip sign bit
    f32.sign ^= 1;

    return Float32SafeAdd( arg1, f32.value.f, denormRetain );
}

/*****************************************************************************\
Inline Function:
    Float32SafeMultiply

Description:
    Performs multiplication taking care of floating point specials in software.
\*****************************************************************************/
inline float Float32SafeMultiply( const float arg1, const float arg2, const bool denormRetain )
{
    // Table for handling IEEE 754 specials in multiplication
    //
    //  a * b       -Inf    -X      -0      +0      +X      +Inf    NaN
    //
    //  -Inf        +Inf    +Inf    NaN     NaN     -Inf    -Inf    NaN
    //  -X          +Inf    <mul>   +0      -0      <mul>   -Inf    NaN
    //  -0          NaN     +0      +0      -0      -0      NaN     NaN
    //  +0          NaN     -0      -0      +0      +0      NaN     NaN
    //  +X          -Inf    <mul>   -0      +0      <mul>   +Inf    NaN
    //  +Inf        -Inf    -Inf    NaN     NaN     +Inf    +Inf    NaN
    //  NaN         NaN     NaN     NaN     NaN     NaN     NaN     NaN
    //

    static const DWORD RESULT[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf                  -X                    -denorm               -0                    +0                    +denorm               +X                    +Inf                  NaN
        { FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NAN      },  // -Inf
        { FPU_FLOAT32_POS_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NAN      },  // -X
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // -denorm
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // -0
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // +0
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // +denorm
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +X
        { FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN      },  // +Inf
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // NaN
    };

    FPU_FLOAT_CLASS t1 = Float32GetClass( arg1 );
    FPU_FLOAT_CLASS t2 = Float32GetClass( arg2 );

    FLOAT32 f32;
    f32.value.u = RESULT[ t1 ][ t2 ];

    bool computeDenorms = ( denormRetain && ( Float32IsDenorm( arg1 ) || Float32IsDenorm( arg2 ) ) );

    if( ( f32.value.u == FPU_FLOAT32_COMPUTE ) || ( computeDenorms ) )
    {
        return arg1 * arg2;
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    Float32SafeFMA

Description:
    Performs fused mutliply and add taking care of floating point specials in 
    software.

    This is machine generated code provided by SSG.

\*****************************************************************************/
inline float Float32SafeFMA( const float a, const float b, const float c )
{
    const DWORD _own_large_value_32[] = { 0x71800000, 0xf1800000 };
    const DWORD _own_small_value_32[] = { 0x0d800000, 0x8d800000 };
    const DWORD _ones[]               = { 0x3f800000, 0xbf800000 };

    DWORD ux = 0;
    DWORD uy = 0; 
    DWORD uz = 0;
    DWORD ur = 0;
    DWORD xbits = 0;
    DWORD ybits = 0; 
    DWORD zbits = 0;
    DWORD uhi = 0;
    DWORD ulo = 0;
    DWORD vhi = 0;
    DWORD vlo = 0;
    DWORD remain = 0;
    DWORD temp = 0;
    DWORD L_mask = 0;
    DWORD R_mask = 0;

    INT zsign = 0;
    INT rsign = 0;
    INT xexp = 0; 
    INT yexp = 0; 
    INT zexp = 0; 
    INT rexp = 0;
    INT carry = 0;
    INT borrow = 0;
    INT rm = 0;
    INT shift = 0;
    INT L_shift = 0;
    INT R_shift = 0;

    UINT64 ubits = 0;
    float resultf = 0;
    float tv = 0;
    float x = a;
    float y = b;
    float z = c;

    // Set to round to nearest even.
    rm = 0;        

    ux = FLOAT32( x >= 0.0f ? x : -x ).value.u;
    uy = FLOAT32( y >= 0.0f ? y : -y ).value.u;;
    uz = FLOAT32( z >= 0.0f ? z : -z ).value.u;;

    int cond1 = ( ux == 0 ) | 
        ( ux >= 0x7f800000 ) | 
        ( ux == 0x3f800000 ) |
        ( uy == 0 ) | 
        ( uy >= 0x7f800000 ) | 
        ( uy == 0x3f800000 ) |
        ( uz == 0 ) | 
        ( uz >= 0x7f800000 );

    if( cond1 != 0 )
    {
        if(  Float32IsInfinity( z ) && 
            !Float32IsInfinity( x ) && 
            !Float32IsInfinity( y ) )
        {
            resultf = ( z + x ) + y;
        }
        else
        {
            resultf = x * y + z;
        }

        return resultf;
    }

    xexp = (int)( ux >> 23 );
    yexp = (int)( uy >> 23 );
    zexp = (int)( uz >> 23 );

    xbits = 0x00800000 | ( ux & 0x007fffff );
    ybits = 0x00800000 | ( uy & 0x007fffff );
    zbits = 0x00800000 | ( uz & 0x007fffff );

  
    rsign = ( FLOAT32(x).value.s ^ FLOAT32(y).value.s ) & 0x80000000;
    rexp  = ( xexp + yexp ) - 0x7F;
    ubits = (UINT64)xbits * ybits;

    if( (DWORD) ( ubits >> 32 ) & 0x00008000 )
    {
        uhi = (DWORD)( ubits >> 24 );
        ulo = ( (DWORD)ubits << 8 );
        rexp++;
    }
    else
    {
        uhi = (DWORD)( ubits >> 23 );
        ulo = ( (DWORD)ubits << 9 );
    }

    int cond2 = ( rexp > zexp ) | 
                ( ( rexp == zexp ) & ( uhi >= zbits ) );

    if( cond2 != 0 )
    {
        shift = ( rexp - zexp );
        vhi = zbits;
        vlo = 0;
        zsign = FLOAT32(z).value.s & 0x80000000;
    }
    else
    {
        shift = ( zexp - rexp );
        rexp = zexp;
        vhi = uhi;
        vlo = ulo;
        uhi = zbits;
        ulo = 0;
        zsign = rsign;
        rsign = FLOAT32(z).value.s & 0x80000000;
    }

    remain = 0;
    if( shift != 0 )
    {
        if( shift < 32 )
        {
            L_shift = 32 - shift;
            R_shift = shift - 0;
            L_mask = ~( 0xffffffffu >> R_shift );
            remain = ( vlo << L_shift );
            vlo = ( ( vhi << L_shift ) & L_mask) | ( vlo >> R_shift );
            vhi = ( vhi >> R_shift );
        }
        else if( shift < 64 )
        {
            L_shift = 64 - shift;
            R_shift = shift - 32;
            L_mask = ~( 0xffffffffu >> R_shift );
            remain = ( ( vhi << L_shift ) & L_mask ) | ( vlo != 0 );
            vlo = ( vhi >> R_shift );
            vhi = 0;
        }
        else
        {
            remain = ( vhi | vlo ) != 0;
            vhi = vlo = 0;
        }
    }

    if( rsign == zsign )
    {
        temp = ulo;
        ulo += vlo;
        carry = ( ulo < temp );
        uhi += ( vhi + carry );

        if ( uhi & 0x01000000 )
        {
            remain = ( uhi << 31 ) | ( ( ulo | remain ) != 0 );
            ur = ( uhi >> 1 ) & 0x007fffff;
            rexp += 1;
        }
        else
        {
            remain = ulo | ( remain != 0 );
            ur = (uhi & 0x007fffff);
        }
    }
    else
    {
        remain = ( 0 - remain );
        borrow = ( remain != 0 );
        temp = ulo;
        ulo -= borrow;
        borrow = ( ulo > temp );
        uhi -= borrow;
        temp = ulo;
        ulo -= vlo;
        borrow = ( ulo > temp );
        uhi -= borrow;
        uhi -= vhi;

        if( uhi != 0 )
        {
            temp = ( uhi << 8 );
            shift = 0;
        }
        else if( ulo != 0 )
        {
            temp = ulo;
            shift = 24;
        }
        else if( remain != 0 )
        {
            temp = remain;
            shift = 24 + 32;
        }
        else
        {
            return FLOAT32( (DWORD)0x00000000 ).value.f;
        }

        shift += clz( temp );

        if( shift < 32 )
        {
            L_shift = shift - 0;
            R_shift = 32 - shift;
            R_mask = ( (DWORD) 1 << L_shift ) - 1;
            ur = ( ( uhi << L_shift ) | (( ulo >> R_shift ) & R_mask ) ) & 0x007fffff;
            remain = ( ulo << L_shift ) | ( remain != 0 );
        }
        else if( shift < 64 )
        {
            L_shift = shift - 32;
            R_shift = 64 - shift;
            R_mask = ( (DWORD) 1 << L_shift ) - 1;
            ur = ( ( ulo << L_shift ) | ( ( remain >> R_shift ) & R_mask ) ) & 0x007fffff;
            remain = ( remain << L_shift );
        }
        else
        {
            L_shift = shift - 64;
            ur = ( remain << L_shift ) & 0x007fffff;
            remain = 0;
        }
        rexp -= shift;
    }

    if( (DWORD) rexp - 1 >= 0xFF - 1 )
    {
        if( rexp >= 0xFF )
        {
            rsign = ( (DWORD)rsign >> 31 );
            if( rsign )
            {
                resultf = tv = FLOAT32(_own_large_value_32[(1)]).value.f * FLOAT32(_own_large_value_32[0]).value.f;
            }
            else
            {
                resultf = tv = FLOAT32(_own_large_value_32[(0)]).value.f * FLOAT32(_own_large_value_32[0]).value.f;
            }

            return resultf;
        }
        else
        {
            //enters here only for rexp = 0
            L_shift = 31;
            R_shift = 1;
            L_mask = ~(0xffffffffu >>  R_shift );
            ur |= 0x00800000;
            remain = ( ( ur << L_shift ) & L_mask ) | ( remain != 0 );
            ur = ( ur >> R_shift );

        }
    }
    else
    {
        ur |= ( rexp << 23 );
    }

    if( remain != 0 )
    {
        tv = ( ( (float *)_ones)[0] + ( (float *)_own_small_value_32)[0] );
        
        int cond3, cond4, cond5, cond6;

        switch( rm )
        {
        case ( 0 << 10 ):
            cond3 = ( ( remain & 0x80000000 ) != 0 ) & ( ( ( ur & 1 ) != 0 ) | 
                    ( ( remain & ~0x80000000 ) != 0 ) );
            if( cond3 != 0 )
            {
                ur++;
                if( ur >= 0x7f800000 )
                {
                    rsign = ( (unsigned)rsign >> 31 );
                    if( rsign )
                    {
                        resultf = tv =
                            ( ( (float *) _own_large_value_32)[1] *
                            ( (float *) _own_large_value_32)[0] );
                    }
                    else
                    {
                        resultf = tv =
                            (((float *) _own_large_value_32)[(0)] *
                            ((float *) _own_large_value_32)[0]);
                    }
                    
                    return resultf;
                }
            }

        case ( 3 << 10 ):
            cond4 = ( ur < 0x00800000 ) | 
                    ( (ur == 0x00800000 ) & ( remain == 0x80000000 ) );

            if( cond4 != 0 )
            {
                tv = ( ( ( float *)_own_small_value_32)[0] * 
                     ( ( float *)_own_small_value_32)[0] );
            }
            break;

        case ( 2 << 10 ):
            cond5 = ( rsign & ( ur < 0x00800000 ) ) | 
                    ( (!rsign) & ( (ur < 0x007fffff ) | ( ( ur == 0x007fffff ) & ( remain < 0x80000000 ) ) ) );

            if( cond5 != 0 )
            {
                tv = ( ( (float *)_own_small_value_32)[0] * 
                       ( (float *)_own_small_value_32)[0] );
            }

            if( !rsign )
            {
                ur++;
                if( ur >= 0x7f800000 )
                {
                    //rsign = ((unsigned) rsign >> 31);
                    resultf = tv = ( ( (float *)_own_large_value_32)[0] * 
                                     ( (float *)_own_large_value_32)[0] );
                    return resultf;
                }
            }
            break;

        case ( 1 << 10 ):
            cond6 = ( !rsign & ( ur < 0x00800000 ) ) | 
                    ( rsign & ( (ur < 0x007fffff ) | ( ( ur == 0x007fffff ) & ( remain < 0x80000000 ) ) ) );

            if( cond6 != 0 )
            {
                tv = ( ( (float *)_own_small_value_32)[0] * 
                       ( (float *)_own_small_value_32)[0] );
            }

            if( rsign )
            {
                ur++;
                if (ur >= 0x7f800000 )
                {
                    //rsign = ((unsigned) rsign >> 31);
                    resultf = tv =
                        ( ( (float *)_own_large_value_32)[1] *
                          ( (float *)_own_large_value_32)[0] );

                    return resultf;
                }
            }
            break;
        }
    }

    resultf = FLOAT32( (DWORD) (rsign | ur ) ).value.f;

    return resultf;
}

/*****************************************************************************\
Inline Function:
    Float32SafeRSQRT

Description:
    Performs correctly rounded single precision reciprocal square root 
    operation taking care of floating point specials in software.
\*****************************************************************************/
inline float Float32SafeRSQRT( const float arg, bool denormRetain )
{
    static const DWORD RESULT[NUM_FPU_FLOAT_CLASSES] =
    {
        FPU_FLOAT32_NAN,       // rsqrt( -inf )    = NaN
        FPU_FLOAT32_NAN,       // rsqrt( -X )      = NaN  //but to be really OK,we should try to maintain the NaN payload
        FPU_FLOAT32_NAN,       // rsqrt( -denorm ) = NaN  //but to be really OK,we should try to maintain the NaN payload
        FPU_FLOAT32_NEG_INF,   // rsqrt( -0 )      = -inf
        FPU_FLOAT32_POS_INF,   // rsqrt( +0 )      = +inf
        FPU_FLOAT32_COMPUTE,   // rsqrt( +denorm)  = computed value
        FPU_FLOAT32_COMPUTE,   // rsqrt( +X )      == computed value
        FPU_FLOAT32_POS_ZERO,  // rsqrt( +inf )    == +0.0
        FPU_FLOAT32_NAN        // rsqrt( NaN )     == NaN
    };

    FPU_FLOAT_CLASS t1 = Float32GetClass( arg );

    FLOAT32 f32;
    f32.value.u = RESULT[ t1 ];

    bool computeDenorms = denormRetain &&  Float32IsDenorm( arg );

    if ( !computeDenorms && t1 == FPU_FLOAT_CLASS_NEG_DENORM ) 
    {
        f32.value.u = FPU_FLOAT32_NEG_INF;
    }
    if ( !computeDenorms && t1 == FPU_FLOAT_CLASS_POS_DENORM )
    {
        f32.value.u = FPU_FLOAT32_POS_INF;
    }

    if( ( f32.value.u == FPU_FLOAT32_COMPUTE ) || ( computeDenorms ) )
    {
        double darg = arg;
        double s = sqrt(darg);      //double-precision square root
        double result = 1.0 / s;    //double-precision division
        return static_cast<float>(result);     //back to floats
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    Float32SafeDivide

Description:
    Performs division taking care of floating point specials in software.
\*****************************************************************************/
inline float Float32SafeDivide( const float arg1, const float arg2, const bool denormRetain )
{
    // Table for handling IEEE 754 specials in division
    //
    //  a / b       -Inf    -X      -0      +0      +X      +Inf    NaN
    //
    //  -Inf        NaN     +Inf    +Inf    -Inf    -Inf    NaN     NaN
    //  -X          +0      <div>   +Inf    -Inf    <div>   -0      NaN
    //  -0          +0      +0      NaN     NaN     -0      -0      NaN
    //  +0          -0      -0      NaN     NaN     +0      +0      NaN
    //  +X          -0      <div>   -Inf    +Inf    <div>   +0      NaN
    //  +Inf        NaN     -Inf    -Inf    +Inf    +Inf    NaN     NaN
    //  NaN         NaN     NaN     NaN     NaN     NaN     NaN     NaN
    //

    static const DWORD RESULT[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf                  -X                    -denorm               -0                    +0                    +denorm               +X                    +Inf                  NaN
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // -Inf
        { FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN      },  // -X
        { FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN      },  // -denorm
        { FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN      },  // -0
        { FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN      },  // +0
        { FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN      },  // +denorm
        { FPU_FLOAT32_NEG_ZERO, FPU_FLOAT32_COMPUTE , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_COMPUTE , FPU_FLOAT32_POS_ZERO, FPU_FLOAT32_NAN      },  // +X
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_NEG_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_POS_INF , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // +Inf
        { FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN     , FPU_FLOAT32_NAN      },  // NaN
    };

    FPU_FLOAT_CLASS t1 = Float32GetClass( arg1 );
    FPU_FLOAT_CLASS t2 = Float32GetClass( arg2 );

    FLOAT32 f32;
    f32.value.u = RESULT[ t1 ][ t2 ];

    bool computeDenorms = ( denormRetain && ( Float32IsDenorm( arg1 ) || Float32IsDenorm( arg2 ) ) );

    if( ( f32.value.u == FPU_FLOAT32_COMPUTE ) || ( computeDenorms ) )
    {
        return arg1 / arg2;
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    Signed32SafeDivideQuotient

Description:
    Computes src0 divided by src1
    Table for handling signed divide quotient and remainder:
        IDIV            SRC0    
            SRC1        +INT            -INT             0
            +INT        +INT            -INT             0
            -INT        -INT            +INT             0
              0     Q:0x7FFFFFFF    Q: 0x80000000   Q:0x7FFFFFFF
                    R:0x7FFFFFFF    R: 0x80000000   R:0x7FFFFFFF
\*****************************************************************************/
inline signed long Signed32SafeDivideQuotient( 
    const signed long src0,
    const signed long src1 )
{
    if( !src1 )
    {
        if( src0 < 0 )
        {
            return LONG_MIN;
        }
        return LONG_MAX;
    }

    return src0 / src1;
}

/*****************************************************************************\
Inline Function:
    Signed32SafeDivideRemainder

Description:
    Computes remainder of src0 divided by src1
\*****************************************************************************/
inline signed long Signed32SafeDivideRemainder( 
    const signed long src0,
    const signed long src1 )
{
    if( !src1 )
    {
        if( src0 < 0 )
        {
            return LONG_MIN;
        }
        return LONG_MAX;
    }

    return src0 % src1;
}

/*****************************************************************************\
Inline Function:
    Unsigned32SafeDivideQuotient

Description:
    Computes src0 divided by src1
       Table for handling unsigned divide quotient and remainder 
          UDIV          SRC0    
              SRC1      <>0             0
              <>0       UINT            0
                0   Q:0xFFFFFFFF    Q:0xFFFFFFFF
                    R:0xFFFFFFFF    R:0xFFFFFFFF
\*****************************************************************************/
inline DWORD Unsigned32SafeDivideQuotient( 
    const DWORD src0,
    const DWORD src1 )
{
    if( !src1 )
    {
        return UINT_MAX;
    }

    return src0 / src1;
}

/*****************************************************************************\
Inline Function:
    Unsigned32SafeDivideRemainder

Description:
    Computes remainder of src0 divided by src1
\*****************************************************************************/
inline DWORD Unsigned32SafeDivideRemainder( 
    const DWORD src0,
    const DWORD src1 )
{
    if( !src1 )
    {
        return UINT_MAX;
    }

    return src0 % src1;
}

/*****************************************************************************\
Inline Function:
    F32ToF16_d

Description:
    Float32 to float16 conversion based on "Fast Half Float Conversions" 
    by Jeroen van der Zijp

Input: 
    32-bit DWORD represantation of float value
Output:
    16-bit DWORD represantation of float value

\*****************************************************************************/
inline WORD F32ToF16_d( DWORD arg )
{
    static const WORD btbl[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,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
        0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,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,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,
        0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00,0xfc00
    };
    static const unsigned char stbl[512] = {
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
        0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
        0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x0d,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,0x0f,
        0x0e,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,
        0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x0d,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
        0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x0d
    };
    DWORD sexp = (arg>>23)&0x1ff;
    return (WORD)(btbl[ sexp ]+( (arg&0x007fffff)>>stbl[ sexp ] ));
}

/*****************************************************************************\

Inline Function:
    F32ToF16_f

Description:
    Float32 to float16 conversion based on "Fast Half Float Conversions" 
    by Jeroen van der Zijp

Input: 
    32-bit float value
Output:
    16-bit WORD represantation of float value

\*****************************************************************************/
inline WORD F32ToF16_f( float arg )
{
    return F32ToF16_d( *(DWORD *)&arg );
}

/*****************************************************************************\

Inline Function:
    F16ToF32

Description:
    Float16 to float32 conversion

Input: 
    16-bit WORD representation of float16 value
Output:
    32-bit DWORD represantation of float32 value

\*****************************************************************************/
static inline DWORD F16ToF32( WORD v )
{
    unsigned long index;
    return 
        // is exponent!=0 ?
        v & 0x7C00
            // is exponent==max ?
            ? ( v & 0x7C00 ) == 0x7C00
                // is mantissa!=0 ?
                ? v & 0x03FF
                    // convert NaN
                    ? ( ( v << 13 ) + 0x70000000 ) | 0x7f800000
                    // convert infinities
                    : ( v << 16 ) | 0x7f800000
                // convert normalized values
                : ( ( ( v << 13 ) + 0x70000000 ) & ~0x70000000 ) + 0x38000000
            // is mantissa non-zero ?
            : v & 0x03FF
                // convert denormalized values
                ? index=bsr( v & 0x03FF ), ( ( ( ( v << 16 ) & 0x80000000 ) | ( ( v << 13 ) & 0xF800000 ) ) + 0x33800000 + ( index << 23 ) ) | ( ( ( v & 0x03FF ) << ( 23-index ) ) & ~0x800000 )
                // convert zeros
                : v << 16;
}

/*****************************************************************************\
Inline Function:
    Float32SafeMax

Description:
    MinMax of Floating Point Numbers.

Input:
    arg1
    arg2
    isGen7

Output:
    max( arg1, arg2 )

\*****************************************************************************/
inline float Float32SafeMax( const float arg1, const float arg2, bool isGen7 )
{
    // Values of following arrays corresponds to results of sel.l instructions.

    static const bool RESULT_preGen7[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf   -X      -denorm   -0      +0   +denorm   +X      +Inf    NaN
        { true  , false , false , false , false , false , false , false , true      },  // -Inf
        { true  , false , false , false , false , false , false , false , true      },  // -X
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // -denorm
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // -0
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +0
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +denorm
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +X
        { true  , true  , true  , true  , true  , true  , true  , true  , true      },  // +Inf
        { false , false , false , false , false , false , false , false , false     },  // NaN
    };

    static const bool RESULT_Gen7[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf   -X      -denorm   -0      +0   +denorm   +X      +Inf    NaN
        { true  , false , false , false , false , false , false , false , true      },  // -Inf
        { true  , false , false , false , false , false , false , false , true      },  // -X
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // -denorm
        { true  , true  , true  , true  , false , true  , false , false , true      },  // -0
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +0
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +denorm
        { true  , true  , true  , true  , true  , true  , false , false , true      },  // +X
        { true  , true  , true  , true  , true  , true  , true  , true  , true      },  // +Inf
        { false , false , false , false , false , false , false , false , false     },  // NaN
    };

    const FPU_FLOAT_CLASS t1 = Float32GetClass( arg1 );
    const FPU_FLOAT_CLASS t2 = Float32GetClass( arg2 );

    if( ( t1 == FPU_FLOAT_CLASS_NEG_FINITE || t1 == FPU_FLOAT_CLASS_POS_FINITE ) &&
        ( t2 == FPU_FLOAT_CLASS_NEG_FINITE || t2 == FPU_FLOAT_CLASS_POS_FINITE ) )
    {
        return ( arg1 >= arg2 ) ? arg1 : arg2;
    }

    FLOAT32 f32;

    if( isGen7 )
    {
        f32.value.f = ( RESULT_Gen7[t1][t2] ) ? arg1 : arg2;
    }
    else 
    {
        f32.value.f = ( RESULT_preGen7[t1][t2] ) ? arg1 : arg2;
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    Float32SafeMin

Description:
    MinMax of Floating Point Numbers.

Input:
    arg1
    arg2
    isGen7

Output:
    max( arg1, arg2 )

\*****************************************************************************/
inline float Float32SafeMin( const float arg1, const float arg2, bool isGen7 )
{
    // Values of following arrays corresponds to results of sel.ge instruction.

    static const bool RESULT_preGen7[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf   -X      -denorm   -0      +0   +denorm   +X      +Inf    NaN
        { false , true  , true  , true  , true  , true  , true  , true  , true      },  // -Inf
        { false , false , true  , true  , true  , true  , true  , true  , true      },  // -X
        { false , false , false , false , false , false , true  , true  , true      },  // -denorm
        { false , false , false , false , false , false , true  , true  , true      },  // -0
        { false , false , false , false , false , false , true  , true  , true      },  // +0
        { false , false , false , false , false , false , true  , true  , true      },  // +denorm
        { false , false , false , false , false , false , false , true  , true      },  // +X
        { false , false , false , false , false , false , false , false , true      },  // +Inf
        { false , false , false , false , false , false , false , false , false     },  // NaN
    };

    static const bool RESULT_Gen7[NUM_FPU_FLOAT_CLASSES][NUM_FPU_FLOAT_CLASSES] = {
    //    -Inf   -X      -denorm   -0      +0   +denorm   +X      +Inf    NaN
        { false , true  , true  , true  , true  , true  , true  , true  , true      },  // -Inf
        { false , false , true  , true  , true  , true  , true  , true  , true      },  // -X
        { false , false , false , false , false , false , true  , true  , true      },  // -denorm
        { false , false , false , false , true  , false , true  , true  , true      },  // -0
        { false , false , false , false , false , false , true  , true  , true      },  // +0
        { false , false , false , false , false , false , true  , true  , true      },  // +denorm
        { false , false , false , false , false , false , false , true  , true      },  // +X
        { false , false , false , false , false , false , false , false , true      },  // +Inf
        { false , false , false , false , false , false , false , false , false     },  // NaN
    };

    const FPU_FLOAT_CLASS t1 = Float32GetClass( arg1 );
    const FPU_FLOAT_CLASS t2 = Float32GetClass( arg2 );

    if( ( t1 == FPU_FLOAT_CLASS_NEG_FINITE || t1 == FPU_FLOAT_CLASS_POS_FINITE ) &&
        ( t2 == FPU_FLOAT_CLASS_NEG_FINITE || t2 == FPU_FLOAT_CLASS_POS_FINITE ) )
    {
        return ( arg1 < arg2 ) ? arg1 : arg2;
    }

    FLOAT32 f32;

    if( isGen7 )
    {
        f32.value.f = ( RESULT_Gen7[t1][t2] ) ? arg1 : arg2;
    }
    else 
    {
        f32.value.f = ( RESULT_preGen7[t1][t2] ) ? arg1 : arg2;
    }

    return f32.value.f;
}

/*****************************************************************************\
Inline Function:
    FloatSaturate

Description:

    For a floating-point destination type, the saturation target range is [0.0,
    1.0]. For a floating-point NaN, there is no "closest value"; any NaN
    saturates to 0.0. (...) Any floating-point number greater than 1.0,
    including +INF, saturates to 1.0. Any negative floating-point number,
    including -INF, saturates to 0.0. Any floating-point number in the range 0.0
    to 1.0 is not changed by saturation.

    -0.0 is changed to +0.0.

Input:
    const float f

Output:
    float

\*****************************************************************************/
inline float FloatSaturate( const float f )
{
    switch( Float32GetClass( f ) )
    {
    case FPU_FLOAT_CLASS_NEG_INF:
    case FPU_FLOAT_CLASS_NEG_FINITE:
    case FPU_FLOAT_CLASS_NEG_DENORM:
    case FPU_FLOAT_CLASS_NEG_ZERO:
    case FPU_FLOAT_CLASS_POS_ZERO:
    case FPU_FLOAT_CLASS_NAN:
        return 0.f;
    case FPU_FLOAT_CLASS_POS_DENORM:
        return f;
    case FPU_FLOAT_CLASS_POS_FINITE:
        return ( f <= 1.f ) ? f : 1.f;
    case FPU_FLOAT_CLASS_POS_INF:
        return 1.f;
    default:
        ASSERT( 0 );
        return 0.f;
    }
}

} // namespace iSTD