Skip to content

Commit f6230f8

Browse files
committed
optimize vector component access
1 parent 33b4a62 commit f6230f8

File tree

3 files changed

+6
-60
lines changed

3 files changed

+6
-60
lines changed

glm/detail/type_vec2.inl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,14 @@ namespace glm
106106
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i)
107107
{
108108
GLM_ASSERT_LENGTH(i, this->length());
109-
switch(i)
110-
{
111-
default:
112-
case 0:
113-
return x;
114-
case 1:
115-
return y;
116-
}
109+
return (&x)[i];
117110
}
118111

119112
template<typename T, qualifier Q>
120113
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) const
121114
{
122115
GLM_ASSERT_LENGTH(i, this->length());
123-
switch(i)
124-
{
125-
default:
126-
case 0:
127-
return x;
128-
case 1:
129-
return y;
130-
}
116+
return (&x)[i];
131117
}
132118

133119
// -- Unary arithmetic operators --

glm/detail/type_vec3.inl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,32 +171,14 @@ namespace glm
171171
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i)
172172
{
173173
GLM_ASSERT_LENGTH(i, this->length());
174-
switch(i)
175-
{
176-
default:
177-
case 0:
178-
return x;
179-
case 1:
180-
return y;
181-
case 2:
182-
return z;
183-
}
174+
return (&x)[i];
184175
}
185176

186177
template<typename T, qualifier Q>
187178
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) const
188179
{
189180
GLM_ASSERT_LENGTH(i, this->length());
190-
switch(i)
191-
{
192-
default:
193-
case 0:
194-
return x;
195-
case 1:
196-
return y;
197-
case 2:
198-
return z;
199-
}
181+
return (&x)[i];
200182
}
201183

202184
// -- Unary arithmetic operators --

glm/detail/type_vec4.inl

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -362,36 +362,14 @@ namespace detail
362362
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i)
363363
{
364364
GLM_ASSERT_LENGTH(i, this->length());
365-
switch (i)
366-
{
367-
default:
368-
case 0:
369-
return x;
370-
case 1:
371-
return y;
372-
case 2:
373-
return z;
374-
case 3:
375-
return w;
376-
}
365+
return (&x)[i];
377366
}
378367

379368
template<typename T, qualifier Q>
380369
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) const
381370
{
382371
GLM_ASSERT_LENGTH(i, this->length());
383-
switch (i)
384-
{
385-
default:
386-
case 0:
387-
return x;
388-
case 1:
389-
return y;
390-
case 2:
391-
return z;
392-
case 3:
393-
return w;
394-
}
372+
return (&x)[i];
395373
}
396374

397375
// -- Unary arithmetic operators --

0 commit comments

Comments
 (0)