blob: e13ad969134ddaa9a8dff9c93da719cfe25cd700 [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
Romain Guy5d4bae72016-11-08 09:49:25 -080017#ifndef UI_VEC4_H_
18#define UI_VEC4_H_
Mathias Agopian595ea772013-08-21 23:10:41 -070019
Romain Guy5d4bae72016-11-08 09:49:25 -080020#include <ui/vec3.h>
21#include <ui/half.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070022#include <stdint.h>
23#include <sys/types.h>
24
Romain Guycaf2ca42016-11-10 11:45:58 -080025#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
27#pragma clang diagnostic ignored "-Wnested-anon-types"
Mathias Agopian595ea772013-08-21 23:10:41 -070028
29namespace android {
30// -------------------------------------------------------------------------------------
31
Romain Guy5d4bae72016-11-08 09:49:25 -080032namespace details {
33
Mathias Agopian595ea772013-08-21 23:10:41 -070034template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080035class TVec4 : public TVecProductOperators<TVec4, T>,
36 public TVecAddOperators<TVec4, T>,
37 public TVecUnaryOperators<TVec4, T>,
38 public TVecComparisonOperators<TVec4, T>,
39 public TVecFunctions<TVec4, T>,
40 public TVecDebug<TVec4, T> {
Mathias Agopian595ea772013-08-21 23:10:41 -070041public:
42 enum no_init { NO_INIT };
43 typedef T value_type;
44 typedef T& reference;
45 typedef T const& const_reference;
46 typedef size_t size_type;
47
48 union {
49 struct { T x, y, z, w; };
50 struct { T s, t, p, q; };
51 struct { T r, g, b, a; };
Romain Guy5d4bae72016-11-08 09:49:25 -080052 TVec2<T> xy;
53 TVec2<T> st;
54 TVec2<T> rg;
55 TVec3<T> xyz;
56 TVec3<T> stp;
57 TVec3<T> rgb;
Mathias Agopian595ea772013-08-21 23:10:41 -070058 };
59
Romain Guy5d4bae72016-11-08 09:49:25 -080060 static constexpr size_t SIZE = 4;
61 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070062
63 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080064 inline constexpr T const& operator[](size_t i) const {
65#if __cplusplus >= 201402L
66 // only possible in C++0x14 with constexpr
67 assert(i < SIZE);
68#endif
69 return (&x)[i];
70 }
71
72 inline T& operator[](size_t i) {
73 assert(i < SIZE);
74 return (&x)[i];
75 }
Mathias Agopian595ea772013-08-21 23:10:41 -070076
77 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080078 // we want the compiler generated versions for these...
79 TVec4(const TVec4&) = default;
80 ~TVec4() = default;
81 TVec4& operator = (const TVec4&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070082
83 // constructors
84
85 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080086 explicit
87 constexpr TVec4(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070088
89 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080090 constexpr TVec4() : x(0), y(0), z(0), w(0) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070091
92 // handles implicit conversion to a tvec4. must not be explicit.
Romain Guy5d4bae72016-11-08 09:49:25 -080093 template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
94 constexpr TVec4(A v) : x(v), y(v), z(v), w(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070095
96 template<typename A, typename B, typename C, typename D>
Romain Guy5d4bae72016-11-08 09:49:25 -080097 constexpr TVec4(A x, B y, C z, D w) : x(x), y(y), z(z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070098
99 template<typename A, typename B, typename C>
Romain Guy5d4bae72016-11-08 09:49:25 -0800100 constexpr TVec4(const TVec2<A>& v, B z, C w) : x(v.x), y(v.y), z(z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700101
102 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -0800103 constexpr TVec4(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700104
105 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -0800106 explicit
107 constexpr TVec4(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700108};
109
Romain Guy5d4bae72016-11-08 09:49:25 -0800110} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700111
112// ----------------------------------------------------------------------------------------
Mathias Agopian595ea772013-08-21 23:10:41 -0700113
Romain Guy5d4bae72016-11-08 09:49:25 -0800114typedef details::TVec4<double> double4;
115typedef details::TVec4<float> float4;
116typedef details::TVec4<float> vec4;
117typedef details::TVec4<half> half4;
118typedef details::TVec4<int32_t> int4;
119typedef details::TVec4<uint32_t> uint4;
120typedef details::TVec4<int16_t> short4;
121typedef details::TVec4<uint16_t> ushort4;
122typedef details::TVec4<int8_t> byte4;
123typedef details::TVec4<uint8_t> ubyte4;
124
125// ----------------------------------------------------------------------------------------
126} // namespace android
127
Romain Guycaf2ca42016-11-10 11:45:58 -0800128#pragma clang diagnostic pop
129
Romain Guy5d4bae72016-11-08 09:49:25 -0800130#endif // UI_VEC4_H_