-
Notifications
You must be signed in to change notification settings - Fork 8
/
half.cpp
269 lines (207 loc) · 6.37 KB
/
half.cpp
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
#include "half.h"
#define CONVERT_PATTERN( x ) ( reinterpret_cast<UINT32 *>( &x ) )
FLOAT16::FLOAT16() : m_uiFormat(0) {}
FLOAT16::FLOAT16( CONST FLOAT16 & rhs ) : m_uiFormat( rhs.m_uiFormat ) {}
FLOAT16::FLOAT16( CONST FLOAT32 & rhs )
{
(*this) = rhs;
}
FLOAT16::~FLOAT16() {}
FLOAT16::operator FLOAT32()
{
return ToFloat32( *this );
}
FLOAT16 & FLOAT16::operator = ( CONST FLOAT16 & rhs )
{
m_uiFormat = rhs.m_uiFormat;
return (*this);
}
FLOAT16 & FLOAT16::operator = ( CONST FLOAT32 & rhs )
{
(*this) = ToFloat16( rhs );
return (*this);
}
BOOL FLOAT16::operator == ( CONST FLOAT16 & rhs ) CONST
{
return m_uiFormat == rhs.m_uiFormat;
}
BOOL FLOAT16::operator != ( CONST FLOAT16 & rhs ) CONST
{
return !( (*this) == rhs );
}
FLOAT32 FLOAT16::ToFloat32( FLOAT16 rhs )
{
FLOAT32 fOutput = 0; // floating point result
UINT32 * uiOutput = CONVERT_PATTERN( fOutput ); // bit manipulated output
if ( 0 == rhs.m_uiFormat ) return 0.0f; // +zero
else if ( 0x8000 == rhs.m_uiFormat ) return -0.0f; // -zero
UINT32 uiHalfSignBit = GET_HALF_SIGN_BIT( rhs.m_uiFormat );
UINT32 uiHalfMantBits = GET_HALF_MANT_BITS( rhs.m_uiFormat ) << 13;
INT32 iHalfExpBits = GET_HALF_EXP_BITS( rhs.m_uiFormat );
//
// Next we check for additional special cases:
//
if ( 0 == iHalfExpBits )
{
//
// Denormalized values
//
SET_SINGLE_SIGN_BIT( uiHalfSignBit, (*uiOutput) );
SET_SINGLE_EXP_BITS( 0, (*uiOutput) );
SET_SINGLE_MANT_BITS( uiHalfMantBits, (*uiOutput) );
}
else if ( 0x1F == iHalfExpBits )
{
if ( 0 == uiHalfMantBits )
{
//
// +- Infinity
//
(*uiOutput) = ( uiHalfSignBit ? SINGLE_NEG_INFINITY : SINGLE_POS_INFINITY );
}
else
{
//
// (S/Q)NaN
//
SET_SINGLE_SIGN_BIT( uiHalfSignBit, (*uiOutput) );
SET_SINGLE_EXP_BITS( 0xFF, (*uiOutput) );
SET_SINGLE_MANT_BITS( uiHalfMantBits, (*uiOutput) );
}
}
else
{
//
// Normalized values
//
SET_SINGLE_SIGN_BIT( uiHalfSignBit, (*uiOutput) );
SET_SINGLE_EXP_BITS( ( iHalfExpBits - 15 ) + 127, (*uiOutput) );
SET_SINGLE_MANT_BITS( uiHalfMantBits, (*uiOutput) );
}
//
// ATP: uiOutput equals the bit pattern of our floating point result.
//
return fOutput;
}
FLOAT16 FLOAT16::ToFloat16( FLOAT32 rhs )
{
//
// (!) Truncation will occur for values outside the representable range for float16.
//
FLOAT16 fOutput;
UINT32 uiInput = *CONVERT_PATTERN( rhs );
if ( 0.0f == rhs )
{
fOutput.m_uiFormat = 0;
return fOutput;
}
else if ( -0.0f == rhs )
{
fOutput.m_uiFormat = 0x8000;
return fOutput;
}
UINT32 uiSignBit = GET_SINGLE_SIGN_BIT( uiInput );
UINT32 uiMantBits = GET_SINGLE_MANT_BITS( uiInput ) >> 13;
INT32 iExpBits = GET_SINGLE_EXP_BITS( uiInput );
//
// Next we check for additional special cases:
//
if ( 0 == iExpBits )
{
//
// Denormalized values
//
SET_HALF_SIGN_BIT( uiSignBit, fOutput.m_uiFormat );
SET_HALF_EXP_BITS( 0, fOutput.m_uiFormat );
SET_HALF_MANT_BITS( uiMantBits, fOutput.m_uiFormat );
}
else if ( 0xFF == iExpBits )
{
if ( 0 == uiMantBits )
{
//
// +- Infinity
//
fOutput.m_uiFormat = ( uiSignBit ? HALF_NEG_INFINITY : HALF_POS_INFINITY );
}
else
{
//
// (S/Q)NaN
//
SET_HALF_SIGN_BIT( uiSignBit, fOutput.m_uiFormat );
SET_HALF_EXP_BITS( 0x1F, fOutput.m_uiFormat );
SET_HALF_MANT_BITS( uiMantBits, fOutput.m_uiFormat );
}
}
else
{
//
// Normalized values
//
INT32 iExponent = iExpBits - 127 + 15;
if ( iExponent < 0 ) { iExponent = 0; }
else if ( iExponent > 31 ) iExponent = 31;
SET_HALF_SIGN_BIT( uiSignBit, fOutput.m_uiFormat );
SET_HALF_EXP_BITS( iExponent, fOutput.m_uiFormat );
SET_HALF_MANT_BITS( uiMantBits, fOutput.m_uiFormat );
}
//
// ATP: uiOutput equals the bit pattern of our floating point result.
//
return fOutput;
}
FLOAT32 FLOAT16::ToFloat32Fast( FLOAT16 rhs )
{
FLOAT32 fOutput = 0; // floating point result
UINT32 * uiOutput = CONVERT_PATTERN( fOutput ); // bit manipulated output
if ( 0 == rhs.m_uiFormat ) return 0.0f; // +zero
else if ( 0x8000 == rhs.m_uiFormat ) return -0.0f; // -zero
UINT32 uiHalfSignBit = GET_HALF_SIGN_BIT( rhs.m_uiFormat );
UINT32 uiHalfMantBits = GET_HALF_MANT_BITS( rhs.m_uiFormat ) << 13;
INT32 iHalfExpBits = GET_HALF_EXP_BITS( rhs.m_uiFormat );
//
// Normalized values
//
SET_SINGLE_SIGN_BIT( uiHalfSignBit, (*uiOutput) );
SET_SINGLE_EXP_BITS( ( iHalfExpBits - 15 ) + 127, (*uiOutput) );
SET_SINGLE_MANT_BITS( uiHalfMantBits, (*uiOutput) );
//
// ATP: uiOutput equals the bit pattern of our floating point result.
//
return fOutput;
}
FLOAT16 FLOAT16::ToFloat16Fast( FLOAT32 rhs )
{
//
// (!) Truncation will occur for values outside the representable range for float16.
//
FLOAT16 fOutput;
UINT32 uiInput = *CONVERT_PATTERN( rhs );
if ( 0.0f == rhs )
{
fOutput.m_uiFormat = 0;
return fOutput;
}
else if ( -0.0f == rhs )
{
fOutput.m_uiFormat = 0x8000;
return fOutput;
}
UINT32 uiSignBit = GET_SINGLE_SIGN_BIT( uiInput );
UINT32 uiMantBits = GET_SINGLE_MANT_BITS( uiInput ) >> 13;
INT32 iExpBits = GET_SINGLE_EXP_BITS( uiInput );
//
// Normalized values
//
INT32 iExponent = iExpBits - 127 + 15;
if ( iExponent < 0 ) { iExponent = 0; }
else if ( iExponent > 31 ) iExponent = 31;
SET_HALF_SIGN_BIT( uiSignBit, fOutput.m_uiFormat );
SET_HALF_EXP_BITS( iExponent, fOutput.m_uiFormat );
SET_HALF_MANT_BITS( uiMantBits, fOutput.m_uiFormat );
//
// ATP: uiOutput equals the bit pattern of our floating point result.
//
return fOutput;
}