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 
operator <(const Float<StorageType,ExponentBits,MantissaBits,ExponentBias,Flags> & other) const129 	inline bool				operator<		(const Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>& other) const { return this->asDouble() < other.asDouble(); }
130 
131 	static Float			zero			(int sign);
132 	static Float			inf				(int sign);
133 	static Float			nan				(void);
134 
135 	static Float			largestNormal	(int sign);
136 	static Float			smallestNormal	(int sign);
137 
138 private:
139 	StorageType				m_value;
140 } DE_WARN_UNUSED_TYPE;
141 
142 // Common floating-point types.
143 typedef Float<deUint16,  5, 10,   15, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM>	Float16;	//!< IEEE 754-2008 16-bit floating-point value
144 typedef Float<deUint32,  8, 23,  127, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM>	Float32;	//!< IEEE 754 32-bit floating-point value
145 typedef Float<deUint64, 11, 52, 1023, FLOAT_HAS_SIGN|FLOAT_SUPPORT_DENORM>	Float64;	//!< IEEE 754 64-bit floating-point value
146 
147 typedef Float<deUint16,  5, 10,   15, FLOAT_HAS_SIGN>	Float16Denormless;	//!< IEEE 754-2008 16-bit floating-point value without denormalized support
148 
149 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(void)150 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (void)
151 	: m_value(0)
152 {
153 }
154 
155 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(StorageType value)156 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (StorageType value)
157 	: m_value(value)
158 {
159 }
160 
161 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(float value,RoundingDirection rd)162 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (float value, RoundingDirection rd)
163 	: m_value(0)
164 {
165 	deUint32 u32;
166 	memcpy(&u32, &value, sizeof(deUint32));
167 	*this = convert(Float32(u32), rd);
168 }
169 
170 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
Float(double value,RoundingDirection rd)171 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::Float (double value, RoundingDirection rd)
172 	: m_value(0)
173 {
174 	deUint64 u64;
175 	memcpy(&u64, &value, sizeof(deUint64));
176 	*this = convert(Float64(u64), rd);
177 }
178 
179 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
asFloat(void) const180 inline float Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::asFloat (void) const
181 {
182 	float		v;
183 	deUint32	u32		= Float32::convert(*this).bits();
184 	memcpy(&v, &u32, sizeof(deUint32));
185 	return v;
186 }
187 
188 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
asDouble(void) const189 inline double Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::asDouble (void) const
190 {
191 	double		v;
192 	deUint64	u64		= Float64::convert(*this).bits();
193 	memcpy(&v, &u64, sizeof(deUint64));
194 	return v;
195 }
196 
197 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
zero(int sign)198 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::zero (int sign)
199 {
200 	DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
201 	return Float(StorageType((sign > 0 ? 0ull : 1ull) << (ExponentBits+MantissaBits)));
202 }
203 
204 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
inf(int sign)205 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::inf (int sign)
206 {
207 	DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
208 	return Float(StorageType(((sign > 0 ? 0ull : 1ull) << (ExponentBits+MantissaBits)) | (((1ull<<ExponentBits)-1) << MantissaBits)));
209 }
210 
211 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
nan(void)212 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::nan (void)
213 {
214 	return Float(StorageType((1ull<<(ExponentBits+MantissaBits))-1));
215 }
216 
217 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
largestNormal(int sign)218 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::largestNormal (int sign)
219 {
220 	DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
221 	return Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct(sign, ExponentBias, (static_cast<StorageType>(1) << (MantissaBits + 1)) - 1);
222 }
223 
224 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
smallestNormal(int sign)225 inline Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags> Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::smallestNormal (int sign)
226 {
227 	DE_ASSERT(sign == 1 || ((Flags & FLOAT_HAS_SIGN) && sign == -1));
228 	return Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct(sign, 1 - ExponentBias, (static_cast<StorageType>(1) << MantissaBits));
229 }
230 
231 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
232 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
construct(int sign,int exponent,StorageType mantissa)233 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::construct
234 	(int sign, int exponent, StorageType mantissa)
235 {
236 	// Repurpose this otherwise invalid input as a shorthand notation for zero (no need for caller to care about internal representation)
237 	const bool			isShorthandZero	= exponent == 0 && mantissa == 0;
238 
239 	// Handles the typical notation for zero (min exponent, mantissa 0). Note that the exponent usually used exponent (-ExponentBias) for zero/subnormals is not used.
240 	// Instead zero/subnormals have the (normally implicit) leading mantissa bit set to zero.
241 	const bool			isDenormOrZero	= (exponent == 1 - ExponentBias) && (mantissa >> MantissaBits == 0);
242 	const StorageType	s				= StorageType((StorageType(sign < 0 ? 1 : 0)) << (StorageType(ExponentBits+MantissaBits)));
243 	const StorageType	exp				= (isShorthandZero  || isDenormOrZero) ? StorageType(0) : StorageType(exponent + ExponentBias);
244 
245 	DE_ASSERT(sign == +1 || sign == -1);
246 	DE_ASSERT(isShorthandZero || isDenormOrZero || mantissa >> MantissaBits == 1);
247 	DE_ASSERT(exp >> ExponentBits == 0);
248 
249 	return Float(StorageType(s | (exp << MantissaBits) | (mantissa & ((StorageType(1)<<MantissaBits)-1))));
250 }
251 
252 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
253 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
constructBits(int sign,int exponent,StorageType mantissaBits)254 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::constructBits
255 	(int sign, int exponent, StorageType mantissaBits)
256 {
257 	const StorageType signBit		= static_cast<StorageType>(sign < 0 ? 1 : 0);
258 	const StorageType exponentBits	= static_cast<StorageType>(exponent + ExponentBias);
259 
260 	DE_ASSERT(sign == +1 || sign == -1 );
261 	DE_ASSERT(exponentBits >> ExponentBits == 0);
262 	DE_ASSERT(mantissaBits >> MantissaBits == 0);
263 
264 	return Float(StorageType((signBit << (ExponentBits+MantissaBits)) | (exponentBits << MantissaBits) | (mantissaBits)));
265 }
266 
267 template <typename StorageType, int ExponentBits, int MantissaBits, int ExponentBias, deUint32 Flags>
268 template <typename OtherStorageType, int OtherExponentBits, int OtherMantissaBits, int OtherExponentBias, deUint32 OtherFlags>
269 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>
convert(const Float<OtherStorageType,OtherExponentBits,OtherMantissaBits,OtherExponentBias,OtherFlags> & other,RoundingDirection rd)270 Float<StorageType, ExponentBits, MantissaBits, ExponentBias, Flags>::convert
271 	(const Float<OtherStorageType, OtherExponentBits, OtherMantissaBits, OtherExponentBias, OtherFlags>& other, RoundingDirection rd)
272 {
273 	if (!(Flags & FLOAT_HAS_SIGN) && other.sign() < 0)
274 	{
275 		// Negative number, truncate to zero.
276 		return zero(+1);
277 	}
278 
279 	if (other.isInf())
280 	{
281 		return inf(other.sign());
282 	}
283 
284 	if (other.isNaN())
285 	{
286 		return nan();
287 	}
288 
289 	if (other.isZero())
290 	{
291 		return zero(other.sign());
292 	}
293 
294 	const int			eMin	= 1 - ExponentBias;
295 	const int			eMax	= ((1<<ExponentBits)-2) - ExponentBias;
296 
297 	const StorageType	s		= StorageType((StorageType(other.signBit())) << (StorageType(ExponentBits+MantissaBits))); // \note Not sign, but sign bit.
298 	int					e		= other.exponent();
299 	deUint64			m		= other.mantissa();
300 
301 	// Normalize denormalized values prior to conversion.
302 	while (!(m & (1ull<<OtherMantissaBits)))
303 	{
304 		m <<= 1;
305 		e  -= 1;
306 	}
307 
308 	if (e < eMin)
309 	{
310 		// Underflow.
311 		if ((Flags & FLOAT_SUPPORT_DENORM) && (eMin-e-1 <= MantissaBits))
312 		{
313 			// Shift and round.
314 			int			bitDiff			= (OtherMantissaBits-MantissaBits) + (eMin-e);
315 			deUint64	lastBitsMask	= (1ull << bitDiff) - 1ull;
316 			deUint64	lastBits		= (static_cast<deUint64>(m) & lastBitsMask);
317 			deUint64	half			= (1ull << (bitDiff - 1)) - 1;
318 			deUint64	bias			= (m >> bitDiff) & 1;
319 
320 			switch (rd)
321 			{
322 			case ROUND_TO_EVEN:
323 				return Float(StorageType(s | (m + half + bias) >> bitDiff));
324 
325 			case ROUND_DOWNWARD:
326 				m = (m >> bitDiff);
327 				if (lastBits != 0ull && other.sign() < 0)
328 				{
329 					m += 1;
330 				}
331 				return Float(StorageType(s | m));
332 
333 			case ROUND_UPWARD:
334 				m = (m >> bitDiff);
335 				if (lastBits != 0ull && other.sign() > 0)
336 				{
337 					m += 1;
338 				}
339 				return Float(StorageType(s | m));
340 
341 			default:
342 				DE_ASSERT(false);
343 				break;
344 			}
345 		}
346 
347 		return zero(other.sign());
348 	}
349 
350 	// Remove leading 1.
351 	m = m & ~(1ull<<OtherMantissaBits);
352 
353 	if (MantissaBits < OtherMantissaBits)
354 	{
355 		// Round mantissa.
356 		int			bitDiff			= OtherMantissaBits-MantissaBits;
357 		deUint64	lastBitsMask	= (1ull << bitDiff) - 1ull;
358 		deUint64	lastBits		= (static_cast<deUint64>(m) & lastBitsMask);
359 		deUint64	half			= (1ull << (bitDiff - 1)) - 1;
360 		deUint64	bias			= (m >> bitDiff) & 1;
361 
362 		switch (rd)
363 		{
364 		case ROUND_TO_EVEN:
365 			m = (m + half + bias) >> bitDiff;
366 			break;
367 
368 		case ROUND_DOWNWARD:
369 			m = (m >> bitDiff);
370 			if (lastBits != 0ull && other.sign() < 0)
371 			{
372 				m += 1;
373 			}
374 			break;
375 
376 		case ROUND_UPWARD:
377 			m = (m >> bitDiff);
378 			if (lastBits != 0ull && other.sign() > 0)
379 			{
380 				m += 1;
381 			}
382 			break;
383 
384 		default:
385 			DE_ASSERT(false);
386 			break;
387 		}
388 
389 		if (m & (1ull<<MantissaBits))
390 		{
391 			// Overflow in mantissa.
392 			m  = 0;
393 			e += 1;
394 		}
395 	}
396 	else
397 	{
398 		int bitDiff = MantissaBits-OtherMantissaBits;
399 		m = m << bitDiff;
400 	}
401 
402 	if (e > eMax)
403 	{
404 		// Overflow.
405 		return (((other.sign() < 0 && rd == ROUND_UPWARD) || (other.sign() > 0 && rd == ROUND_DOWNWARD)) ? largestNormal(other.sign()) : inf(other.sign()));
406 	}
407 
408 	DE_ASSERT(de::inRange(e, eMin, eMax));
409 	DE_ASSERT(((e + ExponentBias) & ~((1ull<<ExponentBits)-1)) == 0);
410 	DE_ASSERT((m & ~((1ull<<MantissaBits)-1)) == 0);
411 
412 	return Float(StorageType(s | (StorageType(e + ExponentBias) << MantissaBits) | m));
413 }
414 
415 } // tcu
416 
417 #endif // _TCUFLOAT_HPP
418