blob: de1d9ffcc965720dbb4c1fbb44012263ee46e5a3 [file] [log] [blame]
Mathias Agopian595ea772013-08-21 23:10:41 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopian595ea772013-08-21 23:10:41 -070017
Romain Guy5d4bae72016-11-08 09:49:25 -080018#ifndef UI_TVECHELPERS_H_
19#define UI_TVECHELPERS_H_
Mathias Agopian595ea772013-08-21 23:10:41 -070020
Romain Guy5d4bae72016-11-08 09:49:25 -080021#include <math.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070022#include <stdint.h>
23#include <sys/types.h>
24
Romain Guy5d4bae72016-11-08 09:49:25 -080025#include <cmath>
26#include <limits>
27#include <iostream>
28
Mathias Agopian595ea772013-08-21 23:10:41 -070029#define PURE __attribute__((pure))
30
Romain Guycaf2ca42016-11-10 11:45:58 -080031#if __cplusplus >= 201402L
32#define CONSTEXPR constexpr
33#else
34#define CONSTEXPR
35#endif
36
Mathias Agopian595ea772013-08-21 23:10:41 -070037namespace android {
Romain Guy5d4bae72016-11-08 09:49:25 -080038namespace details {
Mathias Agopian595ea772013-08-21 23:10:41 -070039// -------------------------------------------------------------------------------------
40
41/*
42 * No user serviceable parts here.
43 *
44 * Don't use this file directly, instead include ui/vec{2|3|4}.h
45 */
46
47/*
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070048 * TVec{Add|Product}Operators implements basic arithmetic and basic compound assignments
Mathias Agopian595ea772013-08-21 23:10:41 -070049 * operators on a vector of type BASE<T>.
50 *
51 * BASE only needs to implement operator[] and size().
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070052 * By simply inheriting from TVec{Add|Product}Operators<BASE, T> BASE will automatically
Mathias Agopian595ea772013-08-21 23:10:41 -070053 * get all the functionality here.
54 */
55
Romain Guy5d4bae72016-11-08 09:49:25 -080056template <template<typename T> class VECTOR, typename T>
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070057class TVecAddOperators {
Mathias Agopian595ea772013-08-21 23:10:41 -070058public:
59 /* compound assignment from a another vector of the same size but different
60 * element type.
61 */
Romain Guy5d4bae72016-11-08 09:49:25 -080062 template<typename OTHER>
63 VECTOR<T>& operator +=(const VECTOR<OTHER>& v) {
64 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
65 for (size_t i = 0; i < lhs.size(); i++) {
66 lhs[i] += v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -070067 }
Romain Guy5d4bae72016-11-08 09:49:25 -080068 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -070069 }
Romain Guy5d4bae72016-11-08 09:49:25 -080070 template<typename OTHER>
71 VECTOR<T>& operator -=(const VECTOR<OTHER>& v) {
72 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
73 for (size_t i = 0; i < lhs.size(); i++) {
74 lhs[i] -= v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -070075 }
Romain Guy5d4bae72016-11-08 09:49:25 -080076 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -070077 }
Mathias Agopian595ea772013-08-21 23:10:41 -070078
79 /* compound assignment from a another vector of the same type.
80 * These operators can be used for implicit conversion and handle operations
81 * like "vector *= scalar" by letting the compiler implicitly convert a scalar
82 * to a vector (assuming the BASE<T> allows it).
83 */
Romain Guy5d4bae72016-11-08 09:49:25 -080084 VECTOR<T>& operator +=(const VECTOR<T>& v) {
85 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
86 for (size_t i = 0; i < lhs.size(); i++) {
87 lhs[i] += v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -070088 }
Romain Guy5d4bae72016-11-08 09:49:25 -080089 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -070090 }
Romain Guy5d4bae72016-11-08 09:49:25 -080091 VECTOR<T>& operator -=(const VECTOR<T>& v) {
92 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
93 for (size_t i = 0; i < lhs.size(); i++) {
94 lhs[i] -= v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -070095 }
Romain Guy5d4bae72016-11-08 09:49:25 -080096 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -070097 }
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070098
99 /*
100 * NOTE: the functions below ARE NOT member methods. They are friend functions
101 * with they definition inlined with their declaration. This makes these
102 * template functions available to the compiler when (and only when) this class
103 * is instantiated, at which point they're only templated on the 2nd parameter
104 * (the first one, BASE<T> being known).
105 */
106
Romain Guy5d4bae72016-11-08 09:49:25 -0800107 /* The operators below handle operation between vectors of the same size
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700108 * but of a different element type.
109 */
110 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800111 friend inline constexpr VECTOR<T> PURE operator +(VECTOR<T> lv, const VECTOR<RT>& rv) {
112 // don't pass lv by reference because we need a copy anyways
113 return lv += rv;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700114 }
115 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800116 friend inline constexpr VECTOR<T> PURE operator -(VECTOR<T> lv, const VECTOR<RT>& rv) {
117 // don't pass lv by reference because we need a copy anyways
118 return lv -= rv;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700119 }
120
121 /* The operators below (which are not templates once this class is instanced,
122 * i.e.: BASE<T> is known) can be used for implicit conversion on both sides.
Romain Guy5d4bae72016-11-08 09:49:25 -0800123 * These handle operations like "vector + scalar" and "scalar + vector" by
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700124 * letting the compiler implicitly convert a scalar to a vector (assuming
125 * the BASE<T> allows it).
126 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800127 friend inline constexpr VECTOR<T> PURE operator +(VECTOR<T> lv, const VECTOR<T>& rv) {
128 // don't pass lv by reference because we need a copy anyways
129 return lv += rv;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700130 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800131 friend inline constexpr VECTOR<T> PURE operator -(VECTOR<T> lv, const VECTOR<T>& rv) {
132 // don't pass lv by reference because we need a copy anyways
133 return lv -= rv;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700134 }
135};
136
Romain Guy5d4bae72016-11-08 09:49:25 -0800137template<template<typename T> class VECTOR, typename T>
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700138class TVecProductOperators {
139public:
140 /* compound assignment from a another vector of the same size but different
141 * element type.
142 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800143 template<typename OTHER>
144 VECTOR<T>& operator *=(const VECTOR<OTHER>& v) {
145 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
146 for (size_t i = 0; i < lhs.size(); i++) {
147 lhs[i] *= v[i];
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700148 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800149 return lhs;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700150 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800151 template<typename OTHER>
152 VECTOR<T>& operator /=(const VECTOR<OTHER>& v) {
153 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
154 for (size_t i = 0; i < lhs.size(); i++) {
155 lhs[i] /= v[i];
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700156 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800157 return lhs;
Mathias Agopian1d4d8f92013-09-01 21:35:36 -0700158 }
159
160 /* compound assignment from a another vector of the same type.
161 * These operators can be used for implicit conversion and handle operations
162 * like "vector *= scalar" by letting the compiler implicitly convert a scalar
163 * to a vector (assuming the BASE<T> allows it).
164 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800165 VECTOR<T>& operator *=(const VECTOR<T>& v) {
166 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
167 for (size_t i = 0; i < lhs.size(); i++) {
168 lhs[i] *= v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -0700169 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800170 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -0700171 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800172 VECTOR<T>& operator /=(const VECTOR<T>& v) {
173 VECTOR<T>& lhs = static_cast<VECTOR<T>&>(*this);
174 for (size_t i = 0; i < lhs.size(); i++) {
175 lhs[i] /= v[i];
Mathias Agopian595ea772013-08-21 23:10:41 -0700176 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800177 return lhs;
Mathias Agopian595ea772013-08-21 23:10:41 -0700178 }
179
180 /*
181 * NOTE: the functions below ARE NOT member methods. They are friend functions
182 * with they definition inlined with their declaration. This makes these
183 * template functions available to the compiler when (and only when) this class
184 * is instantiated, at which point they're only templated on the 2nd parameter
185 * (the first one, BASE<T> being known).
186 */
187
Romain Guy5d4bae72016-11-08 09:49:25 -0800188 /* The operators below handle operation between vectors of the same size
Mathias Agopian595ea772013-08-21 23:10:41 -0700189 * but of a different element type.
190 */
191 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800192 friend inline constexpr VECTOR<T> PURE operator *(VECTOR<T> lv, const VECTOR<RT>& rv) {
193 // don't pass lv by reference because we need a copy anyways
194 return lv *= rv;
Mathias Agopian595ea772013-08-21 23:10:41 -0700195 }
196 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800197 friend inline constexpr VECTOR<T> PURE operator /(VECTOR<T> lv, const VECTOR<RT>& rv) {
198 // don't pass lv by reference because we need a copy anyways
199 return lv /= rv;
Mathias Agopian595ea772013-08-21 23:10:41 -0700200 }
201
202 /* The operators below (which are not templates once this class is instanced,
203 * i.e.: BASE<T> is known) can be used for implicit conversion on both sides.
204 * These handle operations like "vector * scalar" and "scalar * vector" by
205 * letting the compiler implicitly convert a scalar to a vector (assuming
206 * the BASE<T> allows it).
207 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800208 friend inline constexpr VECTOR<T> PURE operator *(VECTOR<T> lv, const VECTOR<T>& rv) {
209 // don't pass lv by reference because we need a copy anyways
210 return lv *= rv;
Mathias Agopian595ea772013-08-21 23:10:41 -0700211 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800212 friend inline constexpr VECTOR<T> PURE operator /(VECTOR<T> lv, const VECTOR<T>& rv) {
213 // don't pass lv by reference because we need a copy anyways
214 return lv /= rv;
Mathias Agopian595ea772013-08-21 23:10:41 -0700215 }
216};
217
218/*
219 * TVecUnaryOperators implements unary operators on a vector of type BASE<T>.
220 *
221 * BASE only needs to implement operator[] and size().
222 * By simply inheriting from TVecUnaryOperators<BASE, T> BASE will automatically
223 * get all the functionality here.
224 *
225 * These operators are implemented as friend functions of TVecUnaryOperators<BASE, T>
226 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800227template<template<typename T> class VECTOR, typename T>
Mathias Agopian595ea772013-08-21 23:10:41 -0700228class TVecUnaryOperators {
229public:
Romain Guy5d4bae72016-11-08 09:49:25 -0800230 VECTOR<T>& operator ++() {
231 VECTOR<T>& rhs = static_cast<VECTOR<T>&>(*this);
232 for (size_t i = 0; i < rhs.size(); i++) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700233 ++rhs[i];
234 }
235 return rhs;
236 }
Romain Guycaf2ca42016-11-10 11:45:58 -0800237
Romain Guy5d4bae72016-11-08 09:49:25 -0800238 VECTOR<T>& operator --() {
239 VECTOR<T>& rhs = static_cast<VECTOR<T>&>(*this);
240 for (size_t i = 0; i < rhs.size(); i++) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700241 --rhs[i];
242 }
243 return rhs;
244 }
Romain Guycaf2ca42016-11-10 11:45:58 -0800245
246 CONSTEXPR VECTOR<T> operator -() const {
Romain Guy5d4bae72016-11-08 09:49:25 -0800247 VECTOR<T> r(VECTOR<T>::NO_INIT);
248 VECTOR<T> const& rv(static_cast<VECTOR<T> const&>(*this));
249 for (size_t i = 0; i < r.size(); i++) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700250 r[i] = -rv[i];
251 }
252 return r;
253 }
254};
255
Mathias Agopian595ea772013-08-21 23:10:41 -0700256/*
257 * TVecComparisonOperators implements relational/comparison operators
258 * on a vector of type BASE<T>.
259 *
260 * BASE only needs to implement operator[] and size().
261 * By simply inheriting from TVecComparisonOperators<BASE, T> BASE will automatically
262 * get all the functionality here.
263 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800264template<template<typename T> class VECTOR, typename T>
Mathias Agopian595ea772013-08-21 23:10:41 -0700265class TVecComparisonOperators {
266public:
267 /*
268 * NOTE: the functions below ARE NOT member methods. They are friend functions
269 * with they definition inlined with their declaration. This makes these
270 * template functions available to the compiler when (and only when) this class
271 * is instantiated, at which point they're only templated on the 2nd parameter
272 * (the first one, BASE<T> being known).
273 */
274 template<typename RT>
275 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800276 bool PURE operator ==(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
277 for (size_t i = 0; i < lv.size(); i++)
Mathias Agopian595ea772013-08-21 23:10:41 -0700278 if (lv[i] != rv[i])
279 return false;
280 return true;
281 }
282
283 template<typename RT>
284 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800285 bool PURE operator !=(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700286 return !operator ==(lv, rv);
287 }
288
289 template<typename RT>
290 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800291 bool PURE operator >(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
292 for (size_t i = 0; i < lv.size(); i++) {
293 if (lv[i] == rv[i]) {
294 continue;
295 }
296 return lv[i] > rv[i];
297 }
298 return false;
Mathias Agopian595ea772013-08-21 23:10:41 -0700299 }
300
301 template<typename RT>
302 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800303 constexpr bool PURE operator <=(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700304 return !(lv > rv);
305 }
306
307 template<typename RT>
308 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800309 bool PURE operator <(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
310 for (size_t i = 0; i < lv.size(); i++) {
311 if (lv[i] == rv[i]) {
312 continue;
313 }
314 return lv[i] < rv[i];
315 }
316 return false;
Mathias Agopian595ea772013-08-21 23:10:41 -0700317 }
318
319 template<typename RT>
320 friend inline
Romain Guy5d4bae72016-11-08 09:49:25 -0800321 constexpr bool PURE operator >=(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700322 return !(lv < rv);
323 }
324};
325
Mathias Agopian595ea772013-08-21 23:10:41 -0700326/*
327 * TVecFunctions implements functions on a vector of type BASE<T>.
328 *
329 * BASE only needs to implement operator[] and size().
330 * By simply inheriting from TVecFunctions<BASE, T> BASE will automatically
331 * get all the functionality here.
332 */
Romain Guy5d4bae72016-11-08 09:49:25 -0800333template<template<typename T> class VECTOR, typename T>
Mathias Agopian595ea772013-08-21 23:10:41 -0700334class TVecFunctions {
335public:
336 /*
337 * NOTE: the functions below ARE NOT member methods. They are friend functions
338 * with they definition inlined with their declaration. This makes these
339 * template functions available to the compiler when (and only when) this class
340 * is instantiated, at which point they're only templated on the 2nd parameter
341 * (the first one, BASE<T> being known).
342 */
343 template<typename RT>
Romain Guycaf2ca42016-11-10 11:45:58 -0800344 friend inline CONSTEXPR T PURE dot(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700345 T r(0);
Romain Guy5d4bae72016-11-08 09:49:25 -0800346 for (size_t i = 0; i < lv.size(); i++) {
347 //r = std::fma(lv[i], rv[i], r);
348 r += lv[i] * rv[i];
349 }
Mathias Agopian595ea772013-08-21 23:10:41 -0700350 return r;
351 }
352
Romain Guy5d4bae72016-11-08 09:49:25 -0800353 friend inline constexpr T PURE norm(const VECTOR<T>& lv) {
354 return std::sqrt(dot(lv, lv));
355 }
356
357 friend inline constexpr T PURE length(const VECTOR<T>& lv) {
358 return norm(lv);
359 }
360
361 friend inline constexpr T PURE norm2(const VECTOR<T>& lv) {
362 return dot(lv, lv);
363 }
364
365 friend inline constexpr T PURE length2(const VECTOR<T>& lv) {
366 return norm2(lv);
Mathias Agopian595ea772013-08-21 23:10:41 -0700367 }
368
369 template<typename RT>
Romain Guy5d4bae72016-11-08 09:49:25 -0800370 friend inline constexpr T PURE distance(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
Mathias Agopian595ea772013-08-21 23:10:41 -0700371 return length(rv - lv);
372 }
373
Romain Guy5d4bae72016-11-08 09:49:25 -0800374 template<typename RT>
375 friend inline constexpr T PURE distance2(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
376 return length2(rv - lv);
377 }
378
379 friend inline constexpr VECTOR<T> PURE normalize(const VECTOR<T>& lv) {
380 return lv * (T(1) / length(lv));
381 }
382
Romain Guycaf2ca42016-11-10 11:45:58 -0800383 friend inline constexpr VECTOR<T> PURE rcp(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800384 return T(1) / v;
385 }
386
Romain Guycaf2ca42016-11-10 11:45:58 -0800387 friend inline CONSTEXPR VECTOR<T> PURE abs(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800388 for (size_t i=0 ; i<v.size() ; i++) {
389 v[i] = std::abs(v[i]);
390 }
391 return v;
392 }
393
Romain Guycaf2ca42016-11-10 11:45:58 -0800394 friend inline CONSTEXPR VECTOR<T> PURE floor(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800395 for (size_t i=0 ; i<v.size() ; i++) {
396 v[i] = std::floor(v[i]);
397 }
398 return v;
399 }
400
Romain Guycaf2ca42016-11-10 11:45:58 -0800401 friend inline CONSTEXPR VECTOR<T> PURE ceil(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800402 for (size_t i=0 ; i<v.size() ; i++) {
403 v[i] = std::ceil(v[i]);
404 }
405 return v;
406 }
407
Romain Guycaf2ca42016-11-10 11:45:58 -0800408 friend inline CONSTEXPR VECTOR<T> PURE round(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800409 for (size_t i=0 ; i<v.size() ; i++) {
410 v[i] = std::round(v[i]);
411 }
412 return v;
413 }
414
Romain Guycaf2ca42016-11-10 11:45:58 -0800415 friend inline CONSTEXPR VECTOR<T> PURE inversesqrt(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800416 for (size_t i=0 ; i<v.size() ; i++) {
417 v[i] = T(1) / std::sqrt(v[i]);
418 }
419 return v;
420 }
421
Romain Guycaf2ca42016-11-10 11:45:58 -0800422 friend inline CONSTEXPR VECTOR<T> PURE sqrt(VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800423 for (size_t i=0 ; i<v.size() ; i++) {
424 v[i] = std::sqrt(v[i]);
425 }
426 return v;
427 }
428
Romain Guycaf2ca42016-11-10 11:45:58 -0800429 friend inline CONSTEXPR VECTOR<T> PURE pow(VECTOR<T> v, T p) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800430 for (size_t i=0 ; i<v.size() ; i++) {
431 v[i] = std::pow(v[i], p);
432 }
433 return v;
434 }
435
Romain Guycaf2ca42016-11-10 11:45:58 -0800436 friend inline CONSTEXPR VECTOR<T> PURE saturate(const VECTOR<T>& lv) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800437 return clamp(lv, T(0), T(1));
438 }
439
Romain Guycaf2ca42016-11-10 11:45:58 -0800440 friend inline CONSTEXPR VECTOR<T> PURE clamp(VECTOR<T> v, T min, T max) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800441 for (size_t i=0 ; i< v.size() ; i++) {
442 v[i] = std::min(max, std::max(min, v[i]));
443 }
444 return v;
445 }
446
Romain Guycaf2ca42016-11-10 11:45:58 -0800447 friend inline CONSTEXPR VECTOR<T> PURE fma(const VECTOR<T>& lv, const VECTOR<T>& rv, VECTOR<T> a) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800448 for (size_t i=0 ; i<lv.size() ; i++) {
449 //a[i] = std::fma(lv[i], rv[i], a[i]);
450 a[i] += (lv[i] * rv[i]);
451 }
452 return a;
453 }
454
Romain Guycaf2ca42016-11-10 11:45:58 -0800455 friend inline CONSTEXPR VECTOR<T> PURE min(const VECTOR<T>& u, VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800456 for (size_t i=0 ; i<v.size() ; i++) {
457 v[i] = std::min(u[i], v[i]);
458 }
459 return v;
460 }
461
Romain Guycaf2ca42016-11-10 11:45:58 -0800462 friend inline CONSTEXPR VECTOR<T> PURE max(const VECTOR<T>& u, VECTOR<T> v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800463 for (size_t i=0 ; i<v.size() ; i++) {
464 v[i] = std::max(u[i], v[i]);
465 }
466 return v;
467 }
468
Romain Guycaf2ca42016-11-10 11:45:58 -0800469 friend inline CONSTEXPR T PURE max(const VECTOR<T>& v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800470 T r(std::numeric_limits<T>::lowest());
471 for (size_t i=0 ; i<v.size() ; i++) {
472 r = std::max(r, v[i]);
473 }
474 return r;
475 }
476
Romain Guycaf2ca42016-11-10 11:45:58 -0800477 friend inline CONSTEXPR T PURE min(const VECTOR<T>& v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800478 T r(std::numeric_limits<T>::max());
479 for (size_t i=0 ; i<v.size() ; i++) {
480 r = std::min(r, v[i]);
481 }
482 return r;
483 }
Romain Guycaf2ca42016-11-10 11:45:58 -0800484
485 friend inline CONSTEXPR VECTOR<T> PURE apply(VECTOR<T> v, const std::function<T(T)>& f) {
486 for (size_t i=0 ; i<v.size() ; i++) {
487 v[i] = f(v[i]);
488 }
489 return v;
490 }
Romain Guy5d4bae72016-11-08 09:49:25 -0800491};
492
493/*
494 * TVecDebug implements functions on a vector of type BASE<T>.
495 *
496 * BASE only needs to implement operator[] and size().
497 * By simply inheriting from TVecDebug<BASE, T> BASE will automatically
498 * get all the functionality here.
499 */
500template<template<typename T> class VECTOR, typename T>
501class TVecDebug {
502public:
503 /*
504 * NOTE: the functions below ARE NOT member methods. They are friend functions
505 * with they definition inlined with their declaration. This makes these
506 * template functions available to the compiler when (and only when) this class
507 * is instantiated, at which point they're only templated on the 2nd parameter
508 * (the first one, BASE<T> being known).
509 */
510 friend std::ostream& operator<<(std::ostream& stream, const VECTOR<T>& v) {
511 stream << "< ";
512 for (size_t i = 0; i < v.size() - 1; i++) {
513 stream << T(v[i]) << ", ";
514 }
515 stream << T(v[v.size() - 1]) << " >";
516 return stream;
Mathias Agopian595ea772013-08-21 23:10:41 -0700517 }
518};
519
Romain Guycaf2ca42016-11-10 11:45:58 -0800520#undef CONSTEXPR
Mathias Agopian595ea772013-08-21 23:10:41 -0700521#undef PURE
522
523// -------------------------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -0800524} // namespace details
525} // namespace android
Mathias Agopian595ea772013-08-21 23:10:41 -0700526
527
Romain Guy5d4bae72016-11-08 09:49:25 -0800528#endif // UI_TVECHELPERS_H_