1 #ifndef _TCUFLOAT_HPP
2 #define _TCUFLOAT_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Reconfigurable floating-point value template.
24 *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27
28 // For memcpy().
29 #include <string.h>
30
31 namespace tcu
32 {
33
34 enum FloatFlags
35 {
36 FLOAT_HAS_SIGN = (1<<0),
37 FLOAT_SUPPORT_DENORM = (1<<1)
38 };
39
40 enum RoundingDirection
41 {
42 ROUND_TO_EVEN = 0,
43 ROUND_DOWNWARD, // Towards -Inf.
44 ROUND_UPWARD, // Towards +Inf.
45 };
46
47 /*--------------------------------------------------------------------*//*!
48 * \brief Floating-point format template
49 *
50 * This template implements arbitrary floating-point handling. Template
51 * can be used for conversion between different formats and checking
52 * various properties of floating-point values.
53 *//*--------------------------------------------------------------------*/
54 template <typename StorageType_, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
55 class Float
56 {
57 public:
58 typedef StorageType_ StorageType;
59
60 enum
61 {
62 EXPONENT_BITS = ExponentBits,
63 MANTISSA_BITS = MantissaBits,
64 EXPONENT_BIAS = ExponentBias,
65 FLAGS = Flags,
66 };
67
68 Float (void);
69 explicit Float (StorageType value);
70 explicit Float (float v, RoundingDirection rd = ROUND_TO_EVEN);
71 explicit Float (double v, RoundingDirection rd = ROUND_TO_EVEN);
72
73 template <typename OtherStorageType, int OtherExponentBits, int OtherMantissaBits, int OtherExponentBias, deUint32 OtherFlags>
74 static Float convert (const Float<OtherStorageType, OtherExponentBits, OtherMantissaBits, OtherExponentBias, OtherFlags>& src, RoundingDirection rd = ROUND_TO_EVEN);
75
convert(const Float<StorageType,ExponentBits,MantissaBits,ExponentBias,Flags> & src,RoundingDirection=ROUND_TO_EVEN)76 static inline Float convert (const Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>& src, RoundingDirection = ROUND_TO_EVEN) { return src; }
77
78 /*--------------------------------------------------------------------*//*!
79 * \brief Construct floating point value
80 * \param sign Sign. Must be +1/-1
81 * \param exponent Exponent in range [1-ExponentBias, ExponentBias+1]
82 * \param mantissa Mantissa bits with implicit leading bit explicitly set
83 * \return The specified float
84 *
85 * This function constructs a floating point value from its inputs.
86 * The normally implicit leading bit of the mantissa must be explicitly set.
87 * The exponent normally used for zero/subnormals is an invalid input. Such
88 * values are specified with the leading mantissa bit of zero and the lowest
89 * normal exponent (1-ExponentBias). Additionally having both exponent and
90 * mantissa set to zero is a shorthand notation for the correctly signed
91 * floating point zero. Inf and NaN must be specified directly with an
92 * exponent of ExponentBias+1 and the appropriate mantissa (with leading
93 * bit set)
94 *//*--------------------------------------------------------------------*/
95 static inline Float construct (int sign, int exponent, StorageType mantissa);
96
97 /*--------------------------------------------------------------------*//*!
98 * \brief Construct floating point value. Explicit version
99 * \param sign Sign. Must be +1/-1
100 * \param exponent Exponent in range [-ExponentBias, ExponentBias+1]
101 * \param mantissa Mantissa bits
102 * \return The specified float
103 *
104 * This function constructs a floating point value from its inputs with
105 * minimal intervention.
106 * The sign is turned into a sign bit and the exponent bias is added.
107 * See IEEE-754 for additional information on the inputs and
108 * the encoding of special values.
109 *//*--------------------------------------------------------------------*/
110 static Float constructBits (int sign, int exponent, StorageType mantissaBits);
111
bits(void) const112 StorageType bits (void) const { return m_value; }
113 float asFloat (void) const;
114 double asDouble (void) const;
115
signBit(void) const116 inline int signBit (void) const { return (int)(m_value >> (ExponentBits+MantissaBits)) & 1; }
exponentBits(void) const117 inline StorageType exponentBits (void) const { return (m_value >> MantissaBits) & ((StorageType(1)<<ExponentBits)-1); }
mantissaBits(void) const118 inline StorageType mantissaBits (void) const { return m_value & ((StorageType(1)<<MantissaBits)-1); }
119
sign(void) const120 inline int sign (void) const { return signBit() ? -1 : 1; }
exponent(void) const121 inline int exponent (void) const { return isDenorm() ? 1 - ExponentBias : (int)exponentBits() - ExponentBias; }
mantissa(void) const122 inline StorageType mantissa (void) const { return isZero() || isDenorm() ? mantissaBits() : (mantissaBits() | (StorageType(1)<<MantissaBits)); }
123
isInf(void) const124 inline bool isInf (void) const { return exponentBits() == ((1<<ExponentBits)-1) && mantissaBits() == 0; }
isNaN(void) const125 inline bool isNaN (void) const { return exponentBits() == ((1<<ExponentBits)-1) && mantissaBits() != 0; }
isZero(void) const126 inline bool isZero (void) const { return exponentBits() == 0 && mantissaBits() == 0; }
isDenorm(void) const127 inline bool isDenorm (void) const { return exponentBits() == 0 && mantissaBits() != 0; }
128
129 static Float zero (int sign);
130 static Float inf (int sign);
131 static Float nan (void);
132
133 static Float largestNormal (int sign);
134 static Float smallestNormal (int sign);
135
136 private:
137 StorageType m_value;
138 } DE_WARN_UNUSED_TYPE;
139
140 // Common floating-point types.
141 typedef Float<deUint16, 5, 10, 15, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM> Float16; //!< IEEE 754-2008 16-bit floating-point value
142 typedef Float<deUint32, 8, 23, 127, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM> Float32; //!< IEEE 754 32-bit floating-point value
143 typedef Float<deUint64, 11, 52, 1023, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM> Float64; //!< IEEE 754 64-bit floating-point value
144
145 typedef Float<deUint16, 5, 10, 15, FLOAT_HAS_SIGN> Float16Denormless; //!< IEEE 754-2008 16-bit floating-point value without denormalized support
146
147 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(void)148 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (void)
149 : m_value(0)
150 {
151 }
152
153 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(StorageType value)154 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (StorageType value)
155 : m_value(value)
156 {
157 }
158
159 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(float value,RoundingDirection rd)160 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (float value, RoundingDirection rd)
161 : m_value(0)
162 {
163 deUint32 u32;
164 memcpy(&u32, &value, sizeof(deUint32));
165 *this = convert(Float32(u32), rd);
166 }
167
168 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(double value,RoundingDirection rd)169 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (double value, RoundingDirection rd)
170 : m_value(0)
171 {
172 deUint64 u64;
173 memcpy(&u64, &value, sizeof(deUint64));
174 *this = convert(Float64(u64), rd);
175 }
176
177 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
asFloat(void) const178 inline float Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::asFloat (void) const
179 {
180 float v;
181 deUint32 u32 = Float32::convert(*this).bits();
182 memcpy(&v, &u32, sizeof(deUint32));
183 return v;
184 }
185
186 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
asDouble(void) const187 inline double Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::asDouble (void) const
188 {
189 double v;
190 deUint64 u64 = Float64::convert(*this).bits();
191 memcpy(&v, &u64, sizeof(deUint64));
192 return v;
193 }
194
195 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
zero(int sign)196 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::zero (int sign)
197 {
198 DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
199 return Float(StorageType((sign > 0 ? 0ull : 1ull) << (ExponentBits+MantissaBits)));
200 }
201
202 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
inf(int sign)203 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::inf (int sign)
204 {
205 DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
206 return Float(StorageType(((sign > 0 ? 0ull : 1ull) << (ExponentBits+MantissaBits)) | (((1ull<<ExponentBits)-1) << MantissaBits)));
207 }
208
209 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
nan(void)210 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::nan (void)
211 {
212 return Float(StorageType((1ull<<(ExponentBits+MantissaBits))-1));
213 }
214
215 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
largestNormal(int sign)216 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::largestNormal (int sign)
217 {
218 DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
219 return Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct(sign, ExponentBias, (static_cast<StorageType>(1) << (MantissaBits + 1)) - 1);
220 }
221
222 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
smallestNormal(int sign)223 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::smallestNormal (int sign)
224 {
225 DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
226 return Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct(sign, 1 - ExponentBias, (static_cast<StorageType>(1) << MantissaBits));
227 }
228
229 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
230 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
construct(int sign,int exponent,StorageType mantissa)231 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct
232 (int sign, int exponent, StorageType mantissa)
233 {
234 // Repurpose this otherwise invalid input as a shorthand notation for zero (no need for caller to care about internal representation)
235 const bool isShorthandZero = exponent == 0 && mantissa == 0;
236
237 // Handles the typical notation for zero (min exponent, mantissa 0). Note that the exponent usually used exponent (-ExponentBias) for zero/subnormals is not used.
238 // Instead zero/subnormals have the (normally implicit) leading mantissa bit set to zero.
239 const bool isDenormOrZero = (exponent == 1 - ExponentBias) && (mantissa >> MantissaBits == 0);
240 const StorageType s = StorageType((StorageType(sign < 0 ? 1 : 0)) << (StorageType(ExponentBits+MantissaBits)));
241 const StorageType exp = (isShorthandZero || isDenormOrZero) ? StorageType(0) : StorageType(exponent + ExponentBias);
242
243 DE_ASSERT(sign == +1 || sign == -1);
244 DE_ASSERT(isShorthandZero || isDenormOrZero || mantissa >> MantissaBits == 1);
245 DE_ASSERT(exp >> ExponentBits == 0);
246
247 return Float(StorageType(s | (exp << MantissaBits) | (mantissa & ((StorageType(1)<<MantissaBits)-1))));
248 }
249
250 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
251 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
constructBits(int sign,int exponent,StorageType mantissaBits)252 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::constructBits
253 (int sign, int exponent, StorageType mantissaBits)
254 {
255 const StorageType signBit = static_cast<StorageType>(sign < 0 ? 1 : 0);
256 const StorageType exponentBits = static_cast<StorageType>(exponent + ExponentBias);
257
258 DE_ASSERT(sign == +1 || sign == -1 );
259 DE_ASSERT(exponentBits >> ExponentBits == 0);
260 DE_ASSERT(mantissaBits >> MantissaBits == 0);
261
262 return Float(StorageType((signBit << (ExponentBits+MantissaBits)) | (exponentBits << MantissaBits) | (mantissaBits)));
263 }
264
265 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
266 template <typename OtherStorageType, int OtherExponentBits, int OtherMantissaBits, int OtherExponentBias, deUint32 OtherFlags>
267 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
convert(const Float<OtherStorageType,OtherExponentBits,OtherMantissaBits,OtherExponentBias,OtherFlags> & other,RoundingDirection rd)268 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::convert
269 (const Float<OtherStorageType, OtherExponentBits, OtherMantissaBits, OtherExponentBias, OtherFlags>& other, RoundingDirection rd)
270 {
271 if (!(Flags & FLOAT_HAS_SIGN) && other.sign() < 0)
272 {
273 // Negative number, truncate to zero.
274 return zero(+1);
275 }
276
277 if (other.isInf())
278 {
279 return inf(other.sign());
280 }
281
282 if (other.isNaN())
283 {
284 return nan();
285 }
286
287 if (other.isZero())
288 {
289 return zero(other.sign());
290 }
291
292 const int eMin = 1 - ExponentBias;
293 const int eMax = ((1<<ExponentBits)-2) - ExponentBias;
294
295 const StorageType s = StorageType((StorageType(other.signBit())) << (StorageType(ExponentBits+MantissaBits))); // \note Not sign, but sign bit.
296 int e = other.exponent();
297 deUint64 m = other.mantissa();
298
299 // Normalize denormalized values prior to conversion.
300 while (!(m & (1ull<<OtherMantissaBits)))
301 {
302 m <<= 1;
303 e -= 1;
304 }
305
306 if (e < eMin)
307 {
308 // Underflow.
309 if ((Flags & FLOAT_SUPPORT_DENORM) && (eMin-e-1 <= MantissaBits))
310 {
311 // Shift and round.
312 int bitDiff = (OtherMantissaBits-MantissaBits) + (eMin-e);
313 deUint64 lastBitsMask = (1ull << bitDiff) - 1ull;
314 deUint64 lastBits = (static_cast<deUint64>(m) & lastBitsMask);
315 deUint64 half = (1ull << (bitDiff - 1)) - 1;
316 deUint64 bias = (m >> bitDiff) & 1;
317
318 switch (rd)
319 {
320 case ROUND_TO_EVEN:
321 return Float(StorageType(s | (m + half + bias) >> bitDiff));
322
323 case ROUND_DOWNWARD:
324 m = (m >> bitDiff);
325 if (lastBits != 0ull && other.sign() < 0)
326 {
327 m += 1;
328 }
329 return Float(StorageType(s | m));
330
331 case ROUND_UPWARD:
332 m = (m >> bitDiff);
333 if (lastBits != 0ull && other.sign() > 0)
334 {
335 m += 1;
336 }
337 return Float(StorageType(s | m));
338
339 default:
340 DE_ASSERT(false);
341 break;
342 }
343 }
344
345 return zero(other.sign());
346 }
347
348 // Remove leading 1.
349 m = m & ~(1ull<<OtherMantissaBits);
350
351 if (MantissaBits < OtherMantissaBits)
352 {
353 // Round mantissa.
354 int bitDiff = OtherMantissaBits-MantissaBits;
355 deUint64 lastBitsMask = (1ull << bitDiff) - 1ull;
356 deUint64 lastBits = (static_cast<deUint64>(m) & lastBitsMask);
357 deUint64 half = (1ull << (bitDiff - 1)) - 1;
358 deUint64 bias = (m >> bitDiff) & 1;
359
360 switch (rd)
361 {
362 case ROUND_TO_EVEN:
363 m = (m + half + bias) >> bitDiff;
364 break;
365
366 case ROUND_DOWNWARD:
367 m = (m >> bitDiff);
368 if (lastBits != 0ull && other.sign() < 0)
369 {
370 m += 1;
371 }
372 break;
373
374 case ROUND_UPWARD:
375 m = (m >> bitDiff);
376 if (lastBits != 0ull && other.sign() > 0)
377 {
378 m += 1;
379 }
380 break;
381
382 default:
383 DE_ASSERT(false);
384 break;
385 }
386
387 if (m & (1ull<<MantissaBits))
388 {
389 // Overflow in mantissa.
390 m = 0;
391 e += 1;
392 }
393 }
394 else
395 {
396 int bitDiff = MantissaBits-OtherMantissaBits;
397 m = m << bitDiff;
398 }
399
400 if (e > eMax)
401 {
402 // Overflow.
403 return (((other.sign() < 0 && rd == ROUND_UPWARD) || (other.sign() > 0 && rd == ROUND_DOWNWARD)) ? largestNormal(other.sign()) : inf(other.sign()));
404 }
405
406 DE_ASSERT(de::inRange(e, eMin, eMax));
407 DE_ASSERT(((e + ExponentBias) & ~((1ull<<ExponentBits)-1)) == 0);
408 DE_ASSERT((m & ~((1ull<<MantissaBits)-1)) == 0);
409
410 return Float(StorageType(s | (StorageType(e + ExponentBias) << MantissaBits) | m));
411 }
412
413 } // tcu
414
415 #endif // _TCUFLOAT_HPP
416