3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
7 * Copyright 2014 The Android Open Source Project
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * \brief Generic vector template.
24 *//*--------------------------------------------------------------------*/
26 #include "tcuDefs.hpp"
27 #include "tcuVectorType.hpp"
35 // Accessor proxy class for Vectors.
36 template <typename T, int VecSize, int Size>
40 explicit VecAccess (Vector<T, VecSize>& v, int x, int y);
41 explicit VecAccess (Vector<T, VecSize>& v, int x, int y, int z);
42 explicit VecAccess (Vector<T, VecSize>& v, int x, int y, int z, int w);
44 VecAccess& operator= (const Vector<T, Size>& v);
46 operator Vector<T, Size> (void) const;
49 Vector<T, VecSize>& m_vector;
53 template <typename T, int VecSize, int Size>
54 VecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y)
57 DE_STATIC_ASSERT(Size == 2);
62 template <typename T, int VecSize, int Size>
63 VecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y, int z)
66 DE_STATIC_ASSERT(Size == 3);
72 template <typename T, int VecSize, int Size>
73 VecAccess<T, VecSize, Size>::VecAccess (Vector<T, VecSize>& v, int x, int y, int z, int w)
76 DE_STATIC_ASSERT(Size == 4);
83 template <typename T, int VecSize, int Size>
84 VecAccess<T, VecSize, Size>& VecAccess<T, VecSize, Size>::operator= (const Vector<T, Size>& v)
86 for (int i = 0; i < Size; i++)
87 m_vector.m_data[m_index[i]] = v.m_data[i];
92 template <typename T, int Size>
105 explicit Vector (void);
106 explicit Vector (T s_); // replicate
108 Vector (T x_, T y_, T z_);
109 Vector (T x_, T y_, T z_, T w_);
110 Vector (const Vector<T, Size>& v);
111 Vector (const T (&v)[Size]);
113 const T* getPtr (void) const { return &m_data[0]; }
114 T* getPtr (void) { return &m_data[0]; }
117 T x (void) const { return m_data[0]; }
118 T y (void) const { DE_STATIC_ASSERT(Size >= 2); return m_data[1]; }
119 T z (void) const { DE_STATIC_ASSERT(Size >= 3); return m_data[2]; }
120 T w (void) const { DE_STATIC_ASSERT(Size >= 4); return m_data[3]; }
122 // Read-write access.
123 T& x (void) { return m_data[0]; }
124 T& y (void) { DE_STATIC_ASSERT(Size >= 2); return m_data[1]; }
125 T& z (void) { DE_STATIC_ASSERT(Size >= 3); return m_data[2]; }
126 T& w (void) { DE_STATIC_ASSERT(Size >= 4); return m_data[3]; }
128 // Writable accessors.
129 VecAccess<T, Size, 2> xy (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 1); }
130 VecAccess<T, Size, 2> xz (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 2); }
131 VecAccess<T, Size, 2> xw (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 0, 3); }
132 VecAccess<T, Size, 2> yz (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 1, 2); }
133 VecAccess<T, Size, 2> yw (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 1, 3); }
134 VecAccess<T, Size, 2> zw (void) { DE_ASSERT(Size >= 2); return VecAccess<T, Size, 2>(*this, 2, 3); }
135 VecAccess<T, Size, 3> xyz (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 1, 2); }
136 VecAccess<T, Size, 3> xyw (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 1, 3); }
137 VecAccess<T, Size, 3> xzw (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 0, 2, 3); }
138 VecAccess<T, Size, 3> zyx (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 2, 1, 0); }
139 VecAccess<T, Size, 3> yzw (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 1, 2, 3); }
140 VecAccess<T, Size, 3> wzy (void) { DE_ASSERT(Size >= 3); return VecAccess<T, Size, 3>(*this, 3, 2, 1); }
141 VecAccess<T, Size, 4> xyzw (void) { DE_ASSERT(Size >= 4); return VecAccess<T, Size, 4>(*this, 0, 1, 2, 3); }
144 Vector<T, 1> swizzle (int a) const { DE_ASSERT(a >= 0 && a < Size); return Vector<T, 1>(m_data[a]); }
145 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]); }
146 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]); }
147 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]); }
149 Vector<float, Size> asFloat (void) const { return cast<float>(); }
150 Vector<int, Size> asInt (void) const { return cast<int>(); }
151 Vector<deUint32, Size> asUint (void) const { return cast<deUint32>(); }
152 Vector<bool, Size> asBool (void) const { return cast<bool>(); }
155 Vector<T, Size>& operator+= (const Vector<T, Size>& v);
156 Vector<T, Size>& operator-= (const Vector<T, Size>& v);
157 Vector<T, Size>& operator= (const Vector<T, Size>& v);
159 const T& operator[] (int ndx) const { DE_ASSERT(de::inBounds(ndx, 0, Size)); return m_data[ndx]; }
160 T& operator[] (int ndx) { DE_ASSERT(de::inBounds(ndx, 0, Size)); return m_data[ndx]; }
162 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; }
163 bool operator!= (const Vector<T, Size>& v) const { return !(*this == v); }
165 // Miscellaneous conversions.
166 template<typename NewT>
167 Vector<NewT, Size> cast (void) const;
169 template <int NewSize>
170 Vector<T, NewSize> toWidth (void) const;
171 } DE_WARN_UNUSED_TYPE;
173 template <typename T, int Size>
174 inline Vector<T, Size>::Vector (void)
176 for (int i = 0; i < Size; i++)
180 template <typename T, int Size>
181 inline Vector<T, Size>::Vector (T s)
183 for (int i = 0; i < Size; i++)
187 template <typename T, int Size>
188 inline Vector<T, Size>::Vector (T x_, T y_)
190 DE_STATIC_ASSERT(Size == 2);
195 template <typename T, int Size>
196 inline Vector<T, Size>::Vector (T x_, T y_, T z_)
198 DE_STATIC_ASSERT(Size == 3);
204 template <typename T, int Size>
205 inline Vector<T, Size>::Vector (T x_, T y_, T z_, T w_)
207 DE_STATIC_ASSERT(Size == 4);
214 template <typename T, int Size>
215 inline Vector<T, Size>::Vector (const Vector<T, Size>& v)
217 for (int i = 0; i < Size; i++)
218 m_data[i] = v.m_data[i];
221 template <typename T, int Size>
222 inline Vector<T, Size>& Vector<T, Size>::operator=(const Vector<T, Size>& v)
224 for (int i = 0; i < Size; i++)
225 m_data[i] = v.m_data[i];
229 template <typename T, int Size>
230 inline Vector<T, Size>::Vector (const T (&v)[Size])
232 for (int i = 0; i < Size; i++)
236 // VecAccess to Vector cast.
237 template <typename T, int VecSize, int Size>
238 VecAccess<T, VecSize, Size>::operator Vector<T, Size> (void) const
241 for (int i = 0; i < Size; i++)
242 vec.m_data[i] = m_vector.m_data[m_index[i]];
247 template <typename T, int Size>
248 template <typename NewT>
249 inline Vector<NewT, Size> Vector<T, Size>::cast (void) const
251 Vector<NewT, Size> res;
252 for (int i = 0; i < Size; i++)
253 res.m_data[i] = NewT(m_data[i]);
258 template <typename T, int Size>
259 template <int NewSize>
260 inline Vector<T, NewSize> Vector<T, Size>::toWidth (void) const
262 Vector<T, NewSize> res;
264 for (i = 0; i < deMin32(Size, NewSize); i++)
265 res.m_data[i] = m_data[i];
266 for (; i < NewSize; i++)
267 res.m_data[i] = T(0);
273 template <typename T, int Size>
274 inline Vector<T, Size> operator- (const Vector<T, Size>& a)
277 for (int i = 0; i < Size; i++)
278 res.m_data[i] = -a.m_data[i];
282 template <typename T, int Size>
283 inline Vector<T, Size> operator+ (const Vector<T, Size>& a, const Vector<T, Size>& b)
286 for (int i = 0; i < Size; i++)
287 res.m_data[i] = a.m_data[i] + b.m_data[i];
291 template <typename T, int Size>
292 inline Vector<T, Size> operator- (const Vector<T, Size>& a, const Vector<T, Size>& b)
295 for (int i = 0; i < Size; i++)
296 res.m_data[i] = a.m_data[i] - b.m_data[i];
300 template <typename T, int Size>
301 inline Vector<T, Size> operator* (const Vector<T, Size>& a, const Vector<T, Size>& b)
304 for (int i = 0; i < Size; i++)
305 res.m_data[i] = a.m_data[i] * b.m_data[i];
309 template <typename T, int Size>
310 inline Vector<T, Size> operator/ (const Vector<T, Size>& a, const Vector<T, Size>& b)
313 for (int i = 0; i < Size; i++)
314 res.m_data[i] = a.m_data[i] / b.m_data[i];
318 template <typename T, int Size>
319 inline Vector<T, Size> operator<< (const Vector<T, Size>& a, const Vector<T, Size>& b)
322 for (int i = 0; i < Size; i++)
323 res.m_data[i] = a.m_data[i] << b.m_data[i];
327 template <typename T, int Size>
328 inline Vector<T, Size> operator>> (const Vector<T, Size>& a, const Vector<T, Size>& b)
331 for (int i = 0; i < Size; i++)
332 res.m_data[i] = a.m_data[i] >> b.m_data[i];
336 template <typename T, int Size>
337 inline Vector<T, Size> operator* (T s, const Vector<T, Size>& a)
340 for (int i = 0; i < Size; i++)
341 res.m_data[i] = s * a.m_data[i];
345 template <typename T, int Size>
346 inline Vector<T, Size> operator+ (T s, const Vector<T, Size>& a)
349 for (int i = 0; i < Size; i++)
350 res.m_data[i] = s + a.m_data[i];
354 template <typename T, int Size>
355 inline Vector<T, Size> operator- (T s, const Vector<T, Size>& a)
358 for (int i = 0; i < Size; i++)
359 res.m_data[i] = s - a.m_data[i];
363 template <typename T, int Size>
364 inline Vector<T, Size> operator- (const Vector<T, Size>& a, T s)
367 for (int i = 0; i < Size; i++)
368 res.m_data[i] = a.m_data[i] - s;
372 template <typename T, int Size>
373 inline Vector<T, Size> operator/ (T s, const Vector<T, Size>& a)
376 for (int i = 0; i < Size; i++)
377 res.m_data[i] = s / a.m_data[i];
381 template <typename T, int Size>
382 inline Vector<T, Size> operator* (const Vector<T, Size>& a, T s) { return s * a; }
384 template <typename T, int Size>
385 inline Vector<T, Size> operator+ (const Vector<T, Size>& a, T s) { return s + a; }
387 template <typename T, int Size>
388 inline Vector<T, Size> operator/ (const Vector<T, Size>& a, T s)
391 for (int i = 0; i < Size; i++)
392 res.m_data[i] = a.m_data[i] / s;
396 template <typename T, int Size>
397 inline Vector<T, Size>& Vector<T, Size>::operator+= (const Vector<T, Size>& v)
399 for (int i = 0; i < Size; i++)
400 m_data[i] += v.m_data[i];
404 template <typename T, int Size>
405 inline Vector<T, Size>& Vector<T, Size>::operator-= (const Vector<T, Size>& v)
407 for (int i = 0; i < Size; i++)
408 m_data[i] -= v.m_data[i];
413 template <typename T, int Size>
414 std::ostream& operator<< (std::ostream& stream, const tcu::Vector<T, Size>& vec)
417 for (int i = 0; i < Size; i++)
421 stream << vec.m_data[i];
429 #endif // _TCUVECTOR_HPP