13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _TCUFORMATUTIL_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _TCUFORMATUTIL_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Tester Core
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief String format utilities.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <ostream>
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace tcu
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace Format
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Hexadecimal value formatter.
385f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <size_t NumDigits>
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Hex
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Hex (deUint64 value_) : value(value_) {}
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream& toStream (std::ostream& stream) const
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return stream << this->toString();
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::string toString (void) const
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_STATIC_ASSERT(0 < NumDigits && NumDigits <= 16);
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char longFmt[]	= {'0', 'x', '%', '0', '0' + NumDigits/10, '0' + NumDigits%10, 'l', 'l', 'x', 0};
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char shortFmt[]	= {'0', 'x', '%', '0', '0' + NumDigits, 'l', 'l', 'x', 0};
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		char buf[sizeof(deUint64)*2 + 3];
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deSprintf(buf, sizeof(buf), NumDigits > 9 ? longFmt : shortFmt, value);
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return std::string(buf);
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint64 value;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
665f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <size_t NumDigits>
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::ostream& operator<< (std::ostream& stream, tcu::Format::Hex<NumDigits> hex)
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return hex.toStream(stream);
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Bitfield formatter.
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass BitDesc
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint64	bit;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const char*	name;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	BitDesc (deUint64 bit_, const char* name_) : bit(bit_), name(name_) {}
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define TCU_BIT_DESC(BIT) tcu::Format::BitDesc(BIT, #BIT)
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
855f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <size_t BitfieldSize>
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Bitfield
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Bitfield (deUint64 value, const BitDesc* begin, const BitDesc* end)
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_value	(value)
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_begin	(begin)
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_end		(end)
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream& toStream (std::ostream& stream)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint64 bitsLeft = m_value;
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (const BitDesc* curDesc = m_begin; curDesc != m_end; curDesc++)
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (curDesc->bit & bitsLeft)
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (bitsLeft != m_value)
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					stream << "|";
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				stream << curDesc->name;
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				bitsLeft ^= curDesc->bit;
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (bitsLeft != 0)
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (bitsLeft != m_value)
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				stream << "|";
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			stream << Hex<BitfieldSize/4>(bitsLeft);
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return stream;
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint64			m_value;
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const BitDesc*		m_begin;
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const BitDesc*		m_end;
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1265f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <size_t BitfieldSize>
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline std::ostream& operator<< (std::ostream& stream, Bitfield<BitfieldSize> decoder)
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return decoder.toStream(stream);
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Enum formatter.
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// \todo [2012-10-30 pyry] Use template for GetName.
1345f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <typename T, size_t NumBytes = sizeof(T)>
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Enum
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1381f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	typedef const char* (*GetNameFunc) (T value);
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1401f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	Enum (GetNameFunc getName, T value)
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_getName	(getName)
1421f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry		, m_value (value)
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostream& toStream (std::ostream& stream) const
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char* name = m_getName(m_value);
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (name)
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return stream << name;
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
15224ceed3e1acf66512ee25ee75002198b6672879dJarkko Pöyry			return stream << Hex<NumBytes*2>((deUint64)m_value);
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::string toString (void) const
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char* name = m_getName(m_value);
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (name)
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return std::string(name);
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
16124ceed3e1acf66512ee25ee75002198b6672879dJarkko Pöyry			return Hex<NumBytes*2>((deUint64)m_value).toString();
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1651f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	const GetNameFunc	m_getName;
1661f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	const T				m_value;
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1695f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <typename T, size_t NumBytes>
1701f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyryinline std::ostream& operator<< (std::ostream& stream, const Enum<T, NumBytes>& fmt) { return fmt.toStream(stream); }
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Array formatters.
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename Iterator>
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Array
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Iterator	begin;
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Iterator	end;
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Array (const Iterator& begin_, const Iterator& end_) : begin(begin_), end(end_) {}
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass ArrayPointer
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const T*	arr;
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int			size;
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ArrayPointer (const T* arr_, int size_) : arr(arr_), size(size_) {}
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename Iterator>
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::ostream& operator<< (std::ostream& str, const Array<Iterator>& fmt)
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << "{ ";
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (Iterator cur = fmt.begin; cur != fmt.end; ++cur)
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (cur != fmt.begin)
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			str << ", ";
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		str << *cur;
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << " }";
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return str;
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::ostream& operator<< (std::ostream& str, const ArrayPointer<T>& fmt)
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (fmt.arr != DE_NULL)
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return str << Array<const T*>(fmt.arr, fmt.arr+fmt.size);
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return str << "(null)";
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Hex format iterator (useful for combining with ArrayFormatter).
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// \todo [2012-10-30 pyry] Implement more generic format iterator.
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, typename Iterator = const T*>
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass HexIterator
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										HexIterator			(Iterator iter) : m_iter(iter) {}
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HexIterator<T, Iterator>&			operator++			(void)	{ ++m_iter; return *this;		}
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HexIterator<T, Iterator>			operator++			(int)	{ return HexIterator(m_iter++);	}
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool								operator==			(const HexIterator<T, Iterator>& other) const { return m_iter == other.m_iter; }
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool								operator!=			(const HexIterator<T, Iterator>& other) const { return m_iter != other.m_iter; }
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if !defined(__INTELLISENSE__)
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Intellisense in VS2013 crashes when parsing this.
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Hex<sizeof(T)*2>					operator*			(void) const { return Hex<sizeof(T)*2>(*m_iter);	}
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Iterator							m_iter;
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // Format
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Bits>		inline deUint64 makeMask64			(void)				{ return (1ull<<Bits)-1;								}
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <>				inline deUint64 makeMask64<64>		(void)				{ return ~0ull;											}
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>	inline deUint64	toUint64			(T value)			{ return (deUint64)value & makeMask64<sizeof(T)*8>();	}
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/** Format value as hexadecimal number. */
2485f40053fac3cbba8a029ce5685d8519c3668e89eJarkko Pöyrytemplate <size_t NumDigits, typename T>
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Format::Hex<NumDigits> toHex (T value)
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return Format::Hex<NumDigits>(toUint64(value));
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/** Format value as hexadecimal number. */
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Format::Hex<sizeof(T)*2> toHex (T value)
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return Format::Hex<sizeof(T)*2>(toUint64(value));
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/** Decode and format bitfield. */
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, size_t Size>
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Format::Bitfield<sizeof(T)*8> formatBitfield (T value, const Format::BitDesc (&desc)[Size])
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return Format::Bitfield<sizeof(T)*8>((deUint64)value, &desc[0], &desc[Size]);
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/** Format array contents. */
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename Iterator>
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Format::Array<Iterator> formatArray (const Iterator& begin, const Iterator& end)
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return Format::Array<Iterator>(begin, end);
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/** Format array contents. */
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Format::ArrayPointer<T> formatArray (const T* arr, int size)
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return Format::ArrayPointer<T>(arr, size);
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2823605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyry/** Format array contents. */
2833605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyrytemplate <typename T, int Size>
2843605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyryinline Format::ArrayPointer<T> formatArray (const T (&arr)[Size])
2853605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyry{
2863605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyry	return Format::ArrayPointer<T>(arr, Size);
2873605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyry}
2883605487eafb5713b09a01a6ee8cea7ccdbfee04fJarkko Pöyry
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // tcu
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _TCUFORMATUTIL_HPP
292