1 /*============================================================================
2 This C source file is part of the SoftFloat IEC/IEEE Floating-point Arithmetic
3 Package, Release 2b.
4 
5 Written by John R. Hauser.  This work was made possible in part by the
6 International Computer Science Institute, located at Suite 600, 1947 Center
7 Street, Berkeley, California 94704.  Funding was partially provided by the
8 National Science Foundation under grant MIP-9311980.  The original version
9 of this code was written as part of a project to build a fixed-point vector
10 processor in collaboration with the University of California at Berkeley,
11 overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
12 is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
13 arithmetic/SoftFloat.html'.
14 
15 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort has
16 been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
17 RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
18 AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
19 COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
20 EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
21 INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
22 OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
23 
24 Derivative works are acceptable, even for commercial purposes, so long as
25 (1) the source code for the derivative work includes prominent notice that
26 the work is derivative, and (2) the source code includes prominent notice with
27 these four paragraphs for those parts of this code that are retained.
28 =============================================================================*/
29 
30 /*============================================================================
31  * Adapted for Bochs (x86 achitecture simulator) by
32  *            Stanislav Shwartsman [sshwarts at sourceforge net]
33  * ==========================================================================*/
34 
35 #include "softfloat.h"
36 
37 #ifdef FLOAT16
38 
39 #include "softfloat-round-pack.h"
40 #include "softfloat-specialize.h"
41 #include "softfloat-macros.h"
42 
43 /*----------------------------------------------------------------------------
44 | Determine half-precision floating-point number class
45 *----------------------------------------------------------------------------*/
46 
float16_class(float16 a)47 float_class_t float16_class(float16 a)
48 {
49    Bit16s aExp = extractFloat16Exp(a);
50    Bit16u aSig = extractFloat16Frac(a);
51    int  aSign = extractFloat16Sign(a);
52 
53    if(aExp == 0x1F) {
54        if (aSig == 0)
55            return (aSign) ? float_negative_inf : float_positive_inf;
56 
57        return (aSig & 0x200) ? float_QNaN : float_SNaN;
58    }
59 
60    if(aExp == 0) {
61        if (aSig == 0) return float_zero;
62        return float_denormal;
63    }
64 
65    return float_normalized;
66 }
67 
68 /*----------------------------------------------------------------------------
69 | Returns the result of converting the half-precision floating-point value
70 | `a' to the single-precision floating-point format.  The conversion is
71 | performed according to the IEC/IEEE Standard for Binary Floating-Point
72 | Arithmetic.
73 *----------------------------------------------------------------------------*/
74 
float16_to_float32(float16 a,float_status_t & status)75 float32 float16_to_float32(float16 a, float_status_t &status)
76 {
77     Bit16u aSig = extractFloat16Frac(a);
78     Bit16s aExp = extractFloat16Exp(a);
79     int aSign = extractFloat16Sign(a);
80 
81     if (aExp == 0x1F) {
82         if (aSig) return commonNaNToFloat32(float16ToCommonNaN(a, status));
83         return packFloat32(aSign, 0xFF, 0);
84     }
85     if (aExp == 0) {
86         // ignore denormals_are_zeros flag
87         if (aSig == 0) return packFloat32(aSign, 0, 0);
88         float_raise(status, float_flag_denormal);
89         normalizeFloat16Subnormal(aSig, &aExp, &aSig);
90         --aExp;
91     }
92 
93     return packFloat32(aSign, aExp + 0x70, ((Bit32u) aSig)<<13);
94 }
95 
96 /*----------------------------------------------------------------------------
97 | Returns the result of converting the single-precision floating-point value
98 | `a' to the half-precision floating-point format.  The conversion is
99 | performed according to the IEC/IEEE Standard for Binary Floating-Point
100 | Arithmetic.
101 *----------------------------------------------------------------------------*/
102 
float32_to_float16(float32 a,float_status_t & status)103 float16 float32_to_float16(float32 a, float_status_t &status)
104 {
105     Bit32u aSig = extractFloat32Frac(a);
106     Bit16s aExp = extractFloat32Exp(a);
107     int aSign = extractFloat32Sign(a);
108 
109     if (aExp == 0xFF) {
110         if (aSig) return commonNaNToFloat16(float32ToCommonNaN(a, status));
111         return packFloat16(aSign, 0x1F, 0);
112     }
113     if (aExp == 0) {
114         if (get_denormals_are_zeros(status)) aSig = 0;
115         if (aSig == 0) return packFloat16(aSign, 0, 0);
116         float_raise(status, float_flag_denormal);
117     }
118 
119     aSig = shift32RightJamming(aSig, 9);
120     Bit16u zSig = (Bit16u) aSig;
121     if (aExp || zSig) {
122         zSig |= 0x4000;
123         aExp -= 0x71;
124     }
125 
126     return roundAndPackFloat16(aSign, aExp, zSig, status);
127 }
128 
129 #endif
130