Nugget
Loading...
Searching...
No Matches
vector.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2023 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <concepts>
30#include <type_traits>
31
32#include "psyqo/fixed-point.hh"
34
35// MSVC and clang-cl target the MSVC ABI, which ignores the standard
36// [[no_unique_address]]; they need the vendor spelling to elide empty members.
37#if __has_cpp_attribute(msvc::no_unique_address)
38#define PSYQO_NO_UNIQUE_ADDR [[msvc::no_unique_address]]
39#else
40#define PSYQO_NO_UNIQUE_ADDR [[no_unique_address]]
41#endif
42
43namespace psyqo {
44
45template <unsigned N, unsigned precisionBits = 12, std::integral T = int32_t>
46 requires((N >= 2) && (N <= 4))
47struct Vector {
48 typedef FixedPoint<precisionBits, T> FixedPointType;
49 FixedPoint<precisionBits, T> x, y;
50 struct EmptyZ {};
51 PSYQO_NO_UNIQUE_ADDR std::conditional_t<(N > 2), FixedPoint<precisionBits, T>, EmptyZ> z;
52 struct EmptyW {};
53 PSYQO_NO_UNIQUE_ADDR std::conditional_t<(N > 3), FixedPoint<precisionBits, T>, EmptyW> w;
54 constexpr FixedPointType& get(unsigned i) {
55 if constexpr (N == 2) {
56 return (i == 0) ? x : y;
57 } else if constexpr (N == 3) {
58 return (i == 0) ? x : (i == 1) ? y : z;
59 } else if constexpr (N == 4) {
60 return (i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;
61 }
62 }
63 constexpr const FixedPointType& get(unsigned i) const {
64 if constexpr (N == 2) {
65 return (i == 0) ? x : y;
66 } else if constexpr (N == 3) {
67 return (i == 0) ? x : (i == 1) ? y : z;
68 } else if constexpr (N == 4) {
69 return (i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;
70 }
71 }
72 constexpr FixedPointType& operator[](unsigned i) { return get(i); }
73 constexpr operator Vertex() const
74 requires((N == 2) && std::is_signed<T>::value)
75 {
76 return {{.x = x.template integer<int16_t>(), .y = y.template integer<int16_t>()}};
77 }
78 constexpr Vector& operator+=(const Vector& rhs) {
79 for (unsigned i = 0; i < N; i++) {
80 get(i) += rhs.get(i);
81 }
82 return *this;
83 }
84 constexpr Vector& operator-=(const Vector& rhs) {
85 for (unsigned i = 0; i < N; i++) {
86 get(i) -= rhs.get(i);
87 }
88 return *this;
89 }
90 constexpr Vector& operator*=(const FixedPointType& rhs) {
91 for (unsigned i = 0; i < N; i++) {
92 get(i) *= rhs;
93 }
94 return *this;
95 }
96 constexpr Vector& operator/=(const FixedPointType& rhs) {
97 for (unsigned i = 0; i < N; i++) {
98 get(i) /= rhs;
99 }
100 return *this;
101 }
102 template <std::integral U>
103 constexpr Vector& operator*=(U rhs) {
104 for (unsigned i = 0; i < N; i++) {
105 get(i) *= rhs;
106 }
107 return *this;
108 }
109 template <std::integral U>
110 constexpr Vector& operator/=(U rhs) {
111 for (unsigned i = 0; i < N; i++) {
112 get(i) /= rhs;
113 }
114 return *this;
115 }
116 constexpr Vector operator-() const {
118 for (unsigned i = 0; i < N; i++) {
119 result.get(i) = -get(i);
120 }
121 return result;
122 }
123 constexpr Vector operator+(const Vector& rhs) const {
125 for (unsigned i = 0; i < N; i++) {
126 result.get(i) = get(i) + rhs.get(i);
127 }
128 return result;
129 }
130 constexpr Vector operator-(const Vector& rhs) const {
132 for (unsigned i = 0; i < N; i++) {
133 result.get(i) = get(i) - rhs.get(i);
134 }
135 return result;
136 }
137 constexpr Vector operator*(const FixedPointType& rhs) const {
139 for (unsigned i = 0; i < N; i++) {
140 result.get(i) = get(i) * rhs;
141 }
142 return result;
143 }
144 constexpr Vector operator/(const FixedPointType& rhs) const {
146 for (unsigned i = 0; i < N; i++) {
147 result.get(i) = get(i) / rhs;
148 }
149 return result;
150 }
151 template <std::integral U>
152 constexpr Vector operator*(U rhs) const {
154 for (unsigned i = 0; i < N; i++) {
155 result.get(i) = get(i) * rhs;
156 }
157 return result;
158 }
159 template <std::integral U>
160 constexpr Vector operator/(U rhs) const {
162 for (unsigned i = 0; i < N; i++) {
163 result.get(i) = get(i) / rhs;
164 }
165 return result;
166 }
167 static constexpr Vector ZERO()
168 requires(N <= 3)
169 {
171 for (unsigned i = 0; i < N; i++) {
172 result.get(i) = FixedPointType(0.0);
173 }
174 return result;
175 }
176 static constexpr Vector ONE()
177 requires(N <= 3)
178 {
180 for (unsigned i = 0; i < N; i++) {
181 result.get(i) = FixedPointType(1.0);
182 }
183 return result;
184 }
185 static constexpr Vector UP()
186 requires(N <= 3)
187 {
189 for (unsigned i = 0; i < N; i++) {
190 result.get(i) = (i == 1) ? FixedPointType(1.0) : FixedPointType(0.0);
191 }
192 return result;
193 }
194 static constexpr Vector DOWN()
195 requires(N <= 3)
196 {
198 for (unsigned i = 0; i < N; i++) {
199 result.get(i) = (i == 1) ? -FixedPointType(1.0) : FixedPointType(0.0);
200 }
201 return result;
202 }
203 static constexpr Vector LEFT()
204 requires(N <= 3)
205 {
207 for (unsigned i = 0; i < N; i++) {
208 result.get(i) = (i == 0) ? -FixedPointType(1.0) : FixedPointType(0.0);
209 }
210 return result;
211 }
212 static constexpr Vector RIGHT()
213 requires(N <= 3)
214 {
216 for (unsigned i = 0; i < N; i++) {
217 result.get(i) = (i == 0) ? FixedPointType(1.0) : FixedPointType(0.0);
218 }
219 return result;
220 }
221 static constexpr Vector FORWARD()
222 requires(N == 3)
223 {
225 for (unsigned i = 0; i < N; i++) {
226 result.get(i) = (i == 2) ? FixedPointType(1.0) : FixedPointType(0.0);
227 }
228 return result;
229 }
230 static constexpr Vector BACKWARD()
231 requires(N == 3)
232 {
234 for (unsigned i = 0; i < N; i++) {
235 result.get(i) = (i == 2) ? -FixedPointType(1.0) : FixedPointType(0.0);
236 }
237 return result;
238 }
239};
240
244
245static_assert(sizeof(Vec2) == 8);
246static_assert(sizeof(Vec3) == 12);
247static_assert(sizeof(Vec4) == 16);
248
249} // namespace psyqo
int i
Definition gte-regio.c:287
void * result
Definition memcpy.c:47
Definition lua.hh:38
Vector< 4 > Vec4
Definition vector.hh:243
Vector< 3 > Vec3
Definition vector.hh:242
Vector< 2 > Vec2
Definition vector.hh:241
Definition vector.hh:52
Definition vector.hh:50
Definition vector.hh:47
static constexpr Vector ZERO()
Definition vector.hh:167
static constexpr Vector LEFT()
Definition vector.hh:203
constexpr Vector operator*(U rhs) const
Definition vector.hh:152
FixedPoint< precisionBits, T > x
Definition vector.hh:49
static constexpr Vector ONE()
Definition vector.hh:176
constexpr Vector & operator/=(U rhs)
Definition vector.hh:110
constexpr Vector operator/(U rhs) const
Definition vector.hh:160
constexpr Vector operator-() const
Definition vector.hh:116
constexpr FixedPointType & operator[](unsigned i)
Definition vector.hh:72
constexpr Vector & operator+=(const Vector &rhs)
Definition vector.hh:78
constexpr Vector operator-(const Vector &rhs) const
Definition vector.hh:130
static constexpr Vector BACKWARD()
Definition vector.hh:230
constexpr Vector & operator*=(U rhs)
Definition vector.hh:103
constexpr Vector & operator-=(const Vector &rhs)
Definition vector.hh:84
static constexpr Vector RIGHT()
Definition vector.hh:212
static constexpr Vector UP()
Definition vector.hh:185
constexpr Vector operator*(const FixedPointType &rhs) const
Definition vector.hh:137
constexpr Vector operator/(const FixedPointType &rhs) const
Definition vector.hh:144
static constexpr Vector DOWN()
Definition vector.hh:194
constexpr Vector operator+(const Vector &rhs) const
Definition vector.hh:123
constexpr const FixedPointType & get(unsigned i) const
Definition vector.hh:63
FixedPoint< precisionBits, T > FixedPointType
Definition vector.hh:48
static constexpr Vector FORWARD()
Definition vector.hh:221
constexpr FixedPointType & get(unsigned i)
Definition vector.hh:54
constexpr Vector & operator*=(const FixedPointType &rhs)
Definition vector.hh:90
constexpr Vector & operator/=(const FixedPointType &rhs)
Definition vector.hh:96
static int value
Definition syscalls.h:535
The Vertex struct.
Definition common.hh:47
int16_t x
Definition common.hh:50
#define PSYQO_NO_UNIQUE_ADDR
Definition vector.hh:40