blob: 1e279fe3f21c9a7f1e8dc0e6928a0d9584d1d5fb [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 Agopian1d77b712017-02-17 15:46:13 -080017#pragma once
Mathias Agopian595ea772013-08-21 23:10:41 -070018
Mathias Agopian1d77b712017-02-17 15:46:13 -080019#include <math/vec3.h>
20#include <math/half.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070021#include <stdint.h>
22#include <sys/types.h>
23
Romain Guycaf2ca42016-11-10 11:45:58 -080024#pragma clang diagnostic push
25#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
26#pragma clang diagnostic ignored "-Wnested-anon-types"
Mathias Agopian595ea772013-08-21 23:10:41 -070027
28namespace android {
29// -------------------------------------------------------------------------------------
30
Romain Guy5d4bae72016-11-08 09:49:25 -080031namespace details {
32
Mathias Agopian595ea772013-08-21 23:10:41 -070033template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080034class TVec4 : public TVecProductOperators<TVec4, T>,
35 public TVecAddOperators<TVec4, T>,
36 public TVecUnaryOperators<TVec4, T>,
37 public TVecComparisonOperators<TVec4, T>,
38 public TVecFunctions<TVec4, T>,
39 public TVecDebug<TVec4, T> {
Mathias Agopian595ea772013-08-21 23:10:41 -070040public:
41 enum no_init { NO_INIT };
42 typedef T value_type;
43 typedef T& reference;
44 typedef T const& const_reference;
45 typedef size_t size_type;
46
47 union {
48 struct { T x, y, z, w; };
49 struct { T s, t, p, q; };
50 struct { T r, g, b, a; };
Romain Guy5d4bae72016-11-08 09:49:25 -080051 TVec2<T> xy;
52 TVec2<T> st;
53 TVec2<T> rg;
54 TVec3<T> xyz;
55 TVec3<T> stp;
56 TVec3<T> rgb;
Mathias Agopian595ea772013-08-21 23:10:41 -070057 };
58
Romain Guy5d4bae72016-11-08 09:49:25 -080059 static constexpr size_t SIZE = 4;
60 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070061
62 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080063 inline constexpr T const& operator[](size_t i) const {
64#if __cplusplus >= 201402L
65 // only possible in C++0x14 with constexpr
66 assert(i < SIZE);
67#endif
68 return (&x)[i];
69 }
70
71 inline T& operator[](size_t i) {
72 assert(i < SIZE);
73 return (&x)[i];
74 }
Mathias Agopian595ea772013-08-21 23:10:41 -070075
76 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080077 // we want the compiler generated versions for these...
78 TVec4(const TVec4&) = default;
79 ~TVec4() = default;
80 TVec4& operator = (const TVec4&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070081
82 // constructors
83
84 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080085 explicit
86 constexpr TVec4(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070087
88 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080089 constexpr TVec4() : x(0), y(0), z(0), w(0) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070090
91 // handles implicit conversion to a tvec4. must not be explicit.
Romain Guy5d4bae72016-11-08 09:49:25 -080092 template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
93 constexpr TVec4(A v) : x(v), y(v), z(v), w(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070094
95 template<typename A, typename B, typename C, typename D>
Romain Guy5d4bae72016-11-08 09:49:25 -080096 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 -070097
98 template<typename A, typename B, typename C>
Romain Guy5d4bae72016-11-08 09:49:25 -080099 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 -0700100
101 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -0800102 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 -0700103
104 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -0800105 explicit
106 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 -0700107};
108
Romain Guy5d4bae72016-11-08 09:49:25 -0800109} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700110
111// ----------------------------------------------------------------------------------------
Mathias Agopian595ea772013-08-21 23:10:41 -0700112
Romain Guy5d4bae72016-11-08 09:49:25 -0800113typedef details::TVec4<double> double4;
114typedef details::TVec4<float> float4;
115typedef details::TVec4<float> vec4;
116typedef details::TVec4<half> half4;
117typedef details::TVec4<int32_t> int4;
118typedef details::TVec4<uint32_t> uint4;
119typedef details::TVec4<int16_t> short4;
120typedef details::TVec4<uint16_t> ushort4;
121typedef details::TVec4<int8_t> byte4;
122typedef details::TVec4<uint8_t> ubyte4;
Romain Guy11ecb632017-01-27 20:04:01 -0800123typedef details::TVec4<bool> bool4;
Romain Guy5d4bae72016-11-08 09:49:25 -0800124
125// ----------------------------------------------------------------------------------------
126} // namespace android
127
Romain Guycaf2ca42016-11-10 11:45:58 -0800128#pragma clang diagnostic pop