13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _TCUVECTOR_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _TCUVECTOR_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 Generic vector template.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuVectorType.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deInt32.h"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <ostream>
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace tcu
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Accessor proxy class for Vectors.
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass VecAccess
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	explicit				VecAccess	(Vector<T, VecSize>& v, int x, int y);
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	explicit				VecAccess	(Vector<T, VecSize>& v, int x, int y, int z);
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	explicit				VecAccess	(Vector<T, VecSize>& v, int x, int y, int z, int w);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess&				operator=	(const Vector<T, Size>& v);
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	operator Vector<T, Size> (void) const;
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, VecSize>&		m_vector;
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int						m_index[Size];
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
543c827367444ee418f129b2c238299f49d3264554Jarkko PoyryVecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y)
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_vector(v)
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 2);
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[0] = x;
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[1] = y;
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
633c827367444ee418f129b2c238299f49d3264554Jarkko PoyryVecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y, int z)
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_vector(v)
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 3);
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[0] = x;
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[1] = y;
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[2] = z;
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
733c827367444ee418f129b2c238299f49d3264554Jarkko PoyryVecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y, int z, int w)
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_vector(v)
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 4);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[0] = x;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[1] = y;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[2] = z;
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_index[3] = w;
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
843c827367444ee418f129b2c238299f49d3264554Jarkko PoyryVecAccess<T, VecSize, Size>& VecAccess<T, VecSize, Size>::operator= (const Vector<T, Size>& v)
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_vector.m_data[m_index[i]] = v.m_data[i];
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Vector class.
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Vector
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	typedef	T				Element;
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	enum
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SIZE = Size,
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T	m_data[Size];
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Constructors.
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	explicit				Vector		(void);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	explicit				Vector		(T s_); // replicate
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							Vector		(T x_, T y_);
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							Vector		(T x_, T y_, T z_);
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							Vector		(T x_, T y_, T z_, T w_);
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							Vector		(const Vector<T, Size>& v);
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry							Vector		(const T (&v)[Size]);
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const T*				getPtr		(void) const { return &m_data[0]; }
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T*						getPtr		(void) { return &m_data[0]; }
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-only access.
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T						x			(void) const { return m_data[0]; }
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T						y			(void) const { DE_STATIC_ASSERT(Size >= 2); return m_data[1]; }
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T						z			(void) const { DE_STATIC_ASSERT(Size >= 3); return m_data[2]; }
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T						w			(void) const { DE_STATIC_ASSERT(Size >= 4); return m_data[3]; }
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Read-write access.
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&						x			(void) { return m_data[0]; }
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&						y			(void) { DE_STATIC_ASSERT(Size >= 2); return m_data[1]; }
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&						z			(void) { DE_STATIC_ASSERT(Size >= 3); return m_data[2]; }
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&						w			(void) { DE_STATIC_ASSERT(Size >= 4); return m_data[3]; }
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Writable accessors.
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	xy			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 1); }
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	xz			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 2); }
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	xw			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 3); }
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	yz			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 1, 2); }
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	yw			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 1, 3); }
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 2>	zw			(void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 2, 3); }
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	xyz			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 1, 2); }
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	xyw			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 1, 3); }
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	xzw			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 2, 3); }
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	zyx			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 2, 1, 0); }
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	yzw			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 1, 2, 3); }
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 3>	wzy			(void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 3, 2, 1); }
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	VecAccess<T, Size, 4>	xyzw		(void) { DE_ASSERT(Size >= 4); return VecAccess<T, Size, 4>(*this, 0, 1, 2, 3); }
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Swizzles.
14465d04b9f591c99e654edfb1125f4b4b00f355c3aJarkko Pöyry	Vector<T, 1>			swizzle		(int a) const { DE_ASSERT(a >= 0 && a < Size); return Vector<T, 1>(m_data[a]); }
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, 2>			swizzle		(int a, int b) const { DE_ASSERT(a >= 0 && a < Size); DE_ASSERT(b >= 0 && b < Size); return Vector<T, 2>(m_data[a], m_data[b]); }
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, 3>			swizzle		(int a, int b, int c) const { DE_ASSERT(a >= 0 && a < Size); DE_ASSERT(b >= 0 && b < Size); DE_ASSERT(c >= 0 && c < Size); return Vector<T, 3>(m_data[a], m_data[b], m_data[c]); }
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, 4>			swizzle		(int a, int b, int c, int d) const { DE_ASSERT(a >= 0 && a < Size); DE_ASSERT(b >= 0 && b < Size); DE_ASSERT(c >= 0 && c < Size); DE_ASSERT(d >= 0 && d < Size); return Vector<T, 4>(m_data[a], m_data[b], m_data[c], m_data[d]); }
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<float, Size>		asFloat		(void) const { return cast<float>();	}
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<int, Size>		asInt		(void) const { return cast<int>();		}
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<deUint32, Size>	asUint		(void) const { return cast<deUint32>();	}
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<bool, Size>		asBool		(void) const { return cast<bool>();		}
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Operators.
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size>&		operator+=	(const Vector<T, Size>& v);
1561f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	Vector<T, Size>&		operator-=	(const Vector<T, Size>& v);
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const T&				operator[]	(int ndx) const		{ DE_ASSERT(de::inBounds(ndx, 0, Size)); return m_data[ndx]; }
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	T&						operator[]	(int ndx)			{ DE_ASSERT(de::inBounds(ndx, 0, Size)); return m_data[ndx]; }
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool					operator==	(const Vector<T, Size>& v) const { for (int i = 0; i < Size; i++) if (m_data[i] != v.m_data[i]) return false; return true; }
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool					operator!=	(const Vector<T, Size>& v) const { return !(*this == v); }
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Miscellaneous conversions.
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template<typename NewT>
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<NewT, Size>		cast		(void) const;
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template <int NewSize>
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, NewSize>		toWidth		(void) const;
17093df37596ea66700965094b3aa2830cf4f2ca5aaJarkko Pöyry} DE_WARN_UNUSED_TYPE;
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (void)
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i] = T();
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (T s)
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i] = s;
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (T x_, T y_)
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 2);
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[0] = x_;
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[1] = y_;
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (T x_, T y_, T z_)
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 3);
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[0] = x_;
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[1] = y_;
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[2] = z_;
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (T x_, T y_, T z_, T w_)
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(Size == 4);
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[0] = x_;
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[1] = y_;
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[2] = z_;
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_data[3] = w_;
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (const Vector<T, Size>& v)
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i] = v.m_data[i];
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>::Vector (const T (&v)[Size])
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i] = v[i];
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// VecAccess to Vector cast.
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int VecSize, int Size>
2293c827367444ee418f129b2c238299f49d3264554Jarkko PoyryVecAccess<T, VecSize, Size>::operator Vector<T, Size> (void) const
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> vec;
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vec.m_data[i] = m_vector.m_data[m_index[i]];
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return vec;
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Type cast.
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename NewT>
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<NewT, Size> Vector<T, Size>::cast (void) const
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<NewT, Size> res;
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = NewT(m_data[i]);
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Size cast.
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int NewSize>
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, NewSize> Vector<T, Size>::toWidth (void) const
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, NewSize> res;
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int i;
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (i = 0; i < deMin32(Size, NewSize); i++)
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = m_data[i];
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (; i < NewSize; i++)
2588852c82a1ffa4760985c17cc6875d5d521daf343Jarkko Poyry		res.m_data[i] = T(0);
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Operators.
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator- (const Vector<T, Size>& a)
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = -a.m_data[i];
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator+ (const Vector<T, Size>& a, const Vector<T, Size>& b)
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] + b.m_data[i];
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator- (const Vector<T, Size>& a, const Vector<T, Size>& b)
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] - b.m_data[i];
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator* (const Vector<T, Size>& a, const Vector<T, Size>& b)
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] * b.m_data[i];
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator/ (const Vector<T, Size>& a, const Vector<T, Size>& b)
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] / b.m_data[i];
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator<< (const Vector<T, Size>& a, const Vector<T, Size>& b)
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] << b.m_data[i];
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator>> (const Vector<T, Size>& a, const Vector<T, Size>& b)
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] >> b.m_data[i];
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator* (T s, const Vector<T, Size>& a)
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = s * a.m_data[i];
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator+ (T s, const Vector<T, Size>& a)
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = s + a.m_data[i];
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator- (T s, const Vector<T, Size>& a)
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = s - a.m_data[i];
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator- (const Vector<T, Size>& a, T s)
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] - s;
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator/ (T s, const Vector<T, Size>& a)
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = s / a.m_data[i];
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator* (const Vector<T, Size>& a, T s)	{ return s * a; }
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator+ (const Vector<T, Size>& a, T s)	{ return s + a; }
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size> operator/ (const Vector<T, Size>& a, T s)
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Vector<T, Size> res;
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		res.m_data[i] = a.m_data[i] / s;
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return res;
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline Vector<T, Size>& Vector<T, Size>::operator+= (const Vector<T, Size>& v)
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i] += v.m_data[i];
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return *this;
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3951f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyrytemplate <typename T, int Size>
3961f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyryinline Vector<T, Size>& Vector<T, Size>::operator-= (const Vector<T, Size>& v)
3971f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry{
3981f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	for (int i = 0; i < Size; i++)
3991f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry		m_data[i] -= v.m_data[i];
4001f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry	return *this;
4011f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry}
4021f99d6991ce9a27d32ec0543d95646fe4e7bf001Jarkko Pöyry
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Stream operator.
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T, int Size>
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystd::ostream& operator<< (std::ostream& stream, const tcu::Vector<T, Size>& vec)
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream << "(";
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < Size; i++)
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (i != 0)
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			stream << ", ";
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << vec.m_data[i];
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream << ")";
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return stream;
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // tcu
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _TCUVECTOR_HPP
421