1///////////////////////////////////////////////////////////////////////////////////
2/// OpenGL Mathematics (glm.g-truc.net)
3///
4/// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
5/// Permission is hereby granted, free of charge, to any person obtaining a copy
6/// of this software and associated documentation files (the "Software"), to deal
7/// in the Software without restriction, including without limitation the rights
8/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9/// copies of the Software, and to permit persons to whom the Software is
10/// furnished to do so, subject to the following conditions:
11///
12/// The above copyright notice and this permission notice shall be included in
13/// all copies or substantial portions of the Software.
14///
15/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21/// THE SOFTWARE.
22///
23/// @ref gtx_simd_vec4
24/// @file glm/gtx/simd_vec4.hpp
25/// @date 2009-05-07 / 2011-06-07
26/// @author Christophe Riccio
27///
28/// @see core (dependence)
29///
30/// @defgroup gtx_simd_vec4 GLM_GTX_simd_vec4
31/// @ingroup gtx
32///
33/// @brief SIMD implementation of vec4 type.
34///
35/// <glm/gtx/simd_vec4.hpp> need to be included to use these functionalities.
36///////////////////////////////////////////////////////////////////////////////////
37
38#ifndef GLM_GTX_simd_vec4
39#define GLM_GTX_simd_vec4
40
41// Dependency:
42#include "../glm.hpp"
43
44#if(GLM_ARCH != GLM_ARCH_PURE)
45
46#if(GLM_ARCH & GLM_ARCH_SSE2)
47#	include "../detail/intrinsic_common.hpp"
48#	include "../detail/intrinsic_geometric.hpp"
49#	include "../detail/intrinsic_integer.hpp"
50#else
51#	error "GLM: GLM_GTX_simd_vec4 requires compiler support of SSE2 through intrinsics"
52#endif
53
54#if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
55#	pragma message("GLM: GLM_GTX_simd_vec4 extension included")
56#endif
57
58
59// Warning silencer for nameless struct/union.
60#if (GLM_COMPILER & GLM_COMPILER_VC)
61#	pragma warning(push)
62#	pragma warning(disable:4201)   // warning C4201: nonstandard extension used : nameless struct/union
63#endif
64
65namespace glm
66{
67	enum comp
68	{
69		X = 0,
70		R = 0,
71		S = 0,
72		Y = 1,
73		G = 1,
74		T = 1,
75		Z = 2,
76		B = 2,
77		P = 2,
78		W = 3,
79		A = 3,
80		Q = 3
81	};
82
83}//namespace glm
84
85namespace glm{
86namespace detail
87{
88	/// 4-dimensional vector implemented using SIMD SEE intrinsics.
89	/// \ingroup gtx_simd_vec4
90	GLM_ALIGNED_STRUCT(16) fvec4SIMD
91	{
92		enum ctor{null};
93		typedef __m128 value_type;
94		typedef std::size_t size_type;
95		static size_type value_size();
96
97		typedef fvec4SIMD type;
98		typedef tvec4<bool, highp> bool_type;
99
100#ifdef GLM_SIMD_ENABLE_XYZW_UNION
101		union
102		{
103			__m128 Data;
104			struct {float x, y, z, w;};
105		};
106#else
107		__m128 Data;
108#endif
109
110		//////////////////////////////////////
111		// Implicit basic constructors
112
113		fvec4SIMD();
114		fvec4SIMD(__m128 const & Data);
115		fvec4SIMD(fvec4SIMD const & v);
116
117		//////////////////////////////////////
118		// Explicit basic constructors
119
120		explicit fvec4SIMD(
121			ctor);
122		explicit fvec4SIMD(
123			float const & s);
124		explicit fvec4SIMD(
125			float const & x,
126			float const & y,
127			float const & z,
128			float const & w);
129		explicit fvec4SIMD(
130			vec4 const & v);
131
132		////////////////////////////////////////
133		//// Conversion vector constructors
134
135		fvec4SIMD(vec2 const & v, float const & s1, float const & s2);
136		fvec4SIMD(float const & s1, vec2 const & v, float const & s2);
137		fvec4SIMD(float const & s1, float const & s2, vec2 const & v);
138		fvec4SIMD(vec3 const & v, float const & s);
139		fvec4SIMD(float const & s, vec3 const & v);
140		fvec4SIMD(vec2 const & v1, vec2 const & v2);
141		//fvec4SIMD(ivec4SIMD const & v);
142
143		//////////////////////////////////////
144		// Unary arithmetic operators
145
146		fvec4SIMD& operator= (fvec4SIMD const & v);
147		fvec4SIMD& operator+=(fvec4SIMD const & v);
148		fvec4SIMD& operator-=(fvec4SIMD const & v);
149		fvec4SIMD& operator*=(fvec4SIMD const & v);
150		fvec4SIMD& operator/=(fvec4SIMD const & v);
151
152		fvec4SIMD& operator+=(float const & s);
153		fvec4SIMD& operator-=(float const & s);
154		fvec4SIMD& operator*=(float const & s);
155		fvec4SIMD& operator/=(float const & s);
156
157		fvec4SIMD& operator++();
158		fvec4SIMD& operator--();
159
160		//////////////////////////////////////
161		// Swizzle operators
162
163		template <comp X, comp Y, comp Z, comp W>
164		fvec4SIMD& swizzle();
165		template <comp X, comp Y, comp Z, comp W>
166		fvec4SIMD swizzle() const;
167		template <comp X, comp Y, comp Z>
168		fvec4SIMD swizzle() const;
169		template <comp X, comp Y>
170		fvec4SIMD swizzle() const;
171		template <comp X>
172		fvec4SIMD swizzle() const;
173	};
174}//namespace detail
175
176	typedef glm::detail::fvec4SIMD simdVec4;
177
178	/// @addtogroup gtx_simd_vec4
179	/// @{
180
181	//! Convert a simdVec4 to a vec4.
182	/// @see gtx_simd_vec4
183	vec4 vec4_cast(
184		detail::fvec4SIMD const & x);
185
186	//! Returns x if x >= 0; otherwise, it returns -x.
187	/// @see gtx_simd_vec4
188	detail::fvec4SIMD abs(detail::fvec4SIMD const & x);
189
190	//! Returns 1.0 if x > 0, 0.0 if x = 0, or -1.0 if x < 0.
191	/// @see gtx_simd_vec4
192	detail::fvec4SIMD sign(detail::fvec4SIMD const & x);
193
194	//! Returns a value equal to the nearest integer that is less then or equal to x.
195	/// @see gtx_simd_vec4
196	detail::fvec4SIMD floor(detail::fvec4SIMD const & x);
197
198	//! Returns a value equal to the nearest integer to x
199	//! whose absolute value is not larger than the absolute value of x.
200	/// @see gtx_simd_vec4
201	detail::fvec4SIMD trunc(detail::fvec4SIMD const & x);
202
203	//! Returns a value equal to the nearest integer to x.
204	//! The fraction 0.5 will round in a direction chosen by the
205	//! implementation, presumably the direction that is fastest.
206	//! This includes the possibility that round(x) returns the
207	//! same value as roundEven(x) for all values of x.
208	///
209	/// @see gtx_simd_vec4
210	detail::fvec4SIMD round(detail::fvec4SIMD const & x);
211
212	//! Returns a value equal to the nearest integer to x.
213	//! A fractional part of 0.5 will round toward the nearest even
214	//! integer. (Both 3.5 and 4.5 for x will return 4.0.)
215	///
216	/// @see gtx_simd_vec4
217	//detail::fvec4SIMD roundEven(detail::fvec4SIMD const & x);
218
219	//! Returns a value equal to the nearest integer
220	//! that is greater than or equal to x.
221	/// @see gtx_simd_vec4
222	detail::fvec4SIMD ceil(detail::fvec4SIMD const & x);
223
224	//! Return x - floor(x).
225	///
226	/// @see gtx_simd_vec4
227	detail::fvec4SIMD fract(detail::fvec4SIMD const & x);
228
229	//! Modulus. Returns x - y * floor(x / y)
230	//! for each component in x using the floating point value y.
231	///
232	/// @see gtx_simd_vec4
233	detail::fvec4SIMD mod(
234		detail::fvec4SIMD const & x,
235		detail::fvec4SIMD const & y);
236
237	//! Modulus. Returns x - y * floor(x / y)
238	//! for each component in x using the floating point value y.
239	///
240	/// @see gtx_simd_vec4
241	detail::fvec4SIMD mod(
242		detail::fvec4SIMD const & x,
243		float const & y);
244
245	//! Returns the fractional part of x and sets i to the integer
246	//! part (as a whole number floating point value). Both the
247	//! return value and the output parameter will have the same
248	//! sign as x.
249	//! (From GLM_GTX_simd_vec4 extension, common function)
250	//detail::fvec4SIMD modf(
251	//	detail::fvec4SIMD const & x,
252	//	detail::fvec4SIMD & i);
253
254	//! Returns y if y < x; otherwise, it returns x.
255	///
256	/// @see gtx_simd_vec4
257	detail::fvec4SIMD min(
258		detail::fvec4SIMD const & x,
259		detail::fvec4SIMD const & y);
260
261	detail::fvec4SIMD min(
262		detail::fvec4SIMD const & x,
263		float const & y);
264
265	//! Returns y if x < y; otherwise, it returns x.
266	///
267	/// @see gtx_simd_vec4
268	detail::fvec4SIMD max(
269		detail::fvec4SIMD const & x,
270		detail::fvec4SIMD const & y);
271
272	detail::fvec4SIMD max(
273		detail::fvec4SIMD const & x,
274		float const & y);
275
276	//! Returns min(max(x, minVal), maxVal) for each component in x
277	//! using the floating-point values minVal and maxVal.
278	///
279	/// @see gtx_simd_vec4
280	detail::fvec4SIMD clamp(
281		detail::fvec4SIMD const & x,
282		detail::fvec4SIMD const & minVal,
283		detail::fvec4SIMD const & maxVal);
284
285	detail::fvec4SIMD clamp(
286		detail::fvec4SIMD const & x,
287		float const & minVal,
288		float const & maxVal);
289
290	//! \return If genTypeU is a floating scalar or vector:
291	//! Returns x * (1.0 - a) + y * a, i.e., the linear blend of
292	//! x and y using the floating-point value a.
293	//! The value for a is not restricted to the range [0, 1].
294	//!
295	//! \return If genTypeU is a boolean scalar or vector:
296	//! Selects which vector each returned component comes
297	//! from. For a component of a that is false, the
298	//! corresponding component of x is returned. For a
299	//! component of a that is true, the corresponding
300	//! component of y is returned. Components of x and y that
301	//! are not selected are allowed to be invalid floating point
302	//! values and will have no effect on the results. Thus, this
303	//! provides different functionality than
304	//! genType mix(genType x, genType y, genType(a))
305	//! where a is a Boolean vector.
306	//!
307	//! From GLSL 1.30.08 specification, section 8.3
308	//!
309	//! \param[in]  x Floating point scalar or vector.
310	//! \param[in]  y Floating point scalar or vector.
311	//! \param[in]  a Floating point or boolean scalar or vector.
312	//!
313	/// \todo Test when 'a' is a boolean.
314	///
315	/// @see gtx_simd_vec4
316	detail::fvec4SIMD mix(
317		detail::fvec4SIMD const & x,
318		detail::fvec4SIMD const & y,
319		detail::fvec4SIMD const & a);
320
321	//! Returns 0.0 if x < edge, otherwise it returns 1.0.
322	///
323	/// @see gtx_simd_vec4
324	detail::fvec4SIMD step(
325		detail::fvec4SIMD const & edge,
326		detail::fvec4SIMD const & x);
327
328	detail::fvec4SIMD step(
329		float const & edge,
330		detail::fvec4SIMD const & x);
331
332	//! Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and
333	//! performs smooth Hermite interpolation between 0 and 1
334	//! when edge0 < x < edge1. This is useful in cases where
335	//! you would want a threshold function with a smooth
336	//! transition. This is equivalent to:
337	//! genType t;
338	//! t = clamp ((x - edge0) / (edge1 - edge0), 0, 1);
339	//! return t * t * (3 - 2 * t);
340	//! Results are undefined if edge0 >= edge1.
341	///
342	/// @see gtx_simd_vec4
343	detail::fvec4SIMD smoothstep(
344		detail::fvec4SIMD const & edge0,
345		detail::fvec4SIMD const & edge1,
346		detail::fvec4SIMD const & x);
347
348	detail::fvec4SIMD smoothstep(
349		float const & edge0,
350		float const & edge1,
351		detail::fvec4SIMD const & x);
352
353	//! Returns true if x holds a NaN (not a number)
354	//! representation in the underlying implementation's set of
355	//! floating point representations. Returns false otherwise,
356	//! including for implementations with no NaN
357	//! representations.
358	///
359	/// @see gtx_simd_vec4
360	//bvec4 isnan(detail::fvec4SIMD const & x);
361
362	//! Returns true if x holds a positive infinity or negative
363	//! infinity representation in the underlying implementation's
364	//! set of floating point representations. Returns false
365	//! otherwise, including for implementations with no infinity
366	//! representations.
367	///
368	/// @see gtx_simd_vec4
369	//bvec4 isinf(detail::fvec4SIMD const & x);
370
371	//! Returns a signed or unsigned integer value representing
372	//! the encoding of a floating-point value. The floatingpoint
373	//! value's bit-level representation is preserved.
374	///
375	/// @see gtx_simd_vec4
376	//detail::ivec4SIMD floatBitsToInt(detail::fvec4SIMD const & value);
377
378	//! Returns a floating-point value corresponding to a signed
379	//! or unsigned integer encoding of a floating-point value.
380	//! If an inf or NaN is passed in, it will not signal, and the
381	//! resulting floating point value is unspecified. Otherwise,
382	//! the bit-level representation is preserved.
383	///
384	/// @see gtx_simd_vec4
385	//detail::fvec4SIMD intBitsToFloat(detail::ivec4SIMD const & value);
386
387	//! Computes and returns a * b + c.
388	///
389	/// @see gtx_simd_vec4
390	detail::fvec4SIMD fma(
391		detail::fvec4SIMD const & a,
392		detail::fvec4SIMD const & b,
393		detail::fvec4SIMD const & c);
394
395	//! Splits x into a floating-point significand in the range
396	//! [0.5, 1.0) and an integral exponent of two, such that:
397	//! x = significand * exp(2, exponent)
398	//! The significand is returned by the function and the
399	//! exponent is returned in the parameter exp. For a
400	//! floating-point value of zero, the significant and exponent
401	//! are both zero. For a floating-point value that is an
402	//! infinity or is not a number, the results are undefined.
403	///
404	/// @see gtx_simd_vec4
405	//detail::fvec4SIMD frexp(detail::fvec4SIMD const & x, detail::ivec4SIMD & exp);
406
407	//! Builds a floating-point number from x and the
408	//! corresponding integral exponent of two in exp, returning:
409	//! significand * exp(2, exponent)
410	//! If this product is too large to be represented in the
411	//! floating-point type, the result is undefined.
412	///
413	/// @see gtx_simd_vec4
414	//detail::fvec4SIMD ldexp(detail::fvec4SIMD const & x, detail::ivec4SIMD const & exp);
415
416	//! Returns the length of x, i.e., sqrt(x * x).
417	///
418	/// @see gtx_simd_vec4
419	float length(
420		detail::fvec4SIMD const & x);
421
422	//! Returns the length of x, i.e., sqrt(x * x).
423	//! Less accurate but much faster than simdLength.
424	///
425	/// @see gtx_simd_vec4
426	float fastLength(
427		detail::fvec4SIMD const & x);
428
429	//! Returns the length of x, i.e., sqrt(x * x).
430	//! Slightly more accurate but much slower than simdLength.
431	///
432	/// @see gtx_simd_vec4
433	float niceLength(
434		detail::fvec4SIMD const & x);
435
436	//! Returns the length of x, i.e., sqrt(x * x).
437	///
438	/// @see gtx_simd_vec4
439	detail::fvec4SIMD length4(
440		detail::fvec4SIMD const & x);
441
442	//! Returns the length of x, i.e., sqrt(x * x).
443	//! Less accurate but much faster than simdLength4.
444	///
445	/// @see gtx_simd_vec4
446	detail::fvec4SIMD fastLength4(
447		detail::fvec4SIMD const & x);
448
449	//! Returns the length of x, i.e., sqrt(x * x).
450	//! Slightly more accurate but much slower than simdLength4.
451	///
452	/// @see gtx_simd_vec4
453	detail::fvec4SIMD niceLength4(
454		detail::fvec4SIMD const & x);
455
456	//! Returns the distance betwwen p0 and p1, i.e., length(p0 - p1).
457	///
458	/// @see gtx_simd_vec4
459	float distance(
460		detail::fvec4SIMD const & p0,
461		detail::fvec4SIMD const & p1);
462
463	//! Returns the distance betwwen p0 and p1, i.e., length(p0 - p1).
464	///
465	/// @see gtx_simd_vec4
466	detail::fvec4SIMD distance4(
467		detail::fvec4SIMD const & p0,
468		detail::fvec4SIMD const & p1);
469
470	//! Returns the dot product of x and y, i.e., result = x * y.
471	///
472	/// @see gtx_simd_vec4
473	float simdDot(
474		detail::fvec4SIMD const & x,
475		detail::fvec4SIMD const & y);
476
477	//! Returns the dot product of x and y, i.e., result = x * y.
478	///
479	/// @see gtx_simd_vec4
480	detail::fvec4SIMD dot4(
481		detail::fvec4SIMD const & x,
482		detail::fvec4SIMD const & y);
483
484	//! Returns the cross product of x and y.
485	///
486	/// @see gtx_simd_vec4
487	detail::fvec4SIMD cross(
488		detail::fvec4SIMD const & x,
489		detail::fvec4SIMD const & y);
490
491	//! Returns a vector in the same direction as x but with length of 1.
492	///
493	/// @see gtx_simd_vec4
494	detail::fvec4SIMD normalize(
495		detail::fvec4SIMD const & x);
496
497	//! Returns a vector in the same direction as x but with length of 1.
498	//! Less accurate but much faster than simdNormalize.
499	///
500	/// @see gtx_simd_vec4
501	detail::fvec4SIMD fastNormalize(
502		detail::fvec4SIMD const & x);
503
504	//! If dot(Nref, I) < 0.0, return N, otherwise, return -N.
505	///
506	/// @see gtx_simd_vec4
507	detail::fvec4SIMD simdFaceforward(
508		detail::fvec4SIMD const & N,
509		detail::fvec4SIMD const & I,
510		detail::fvec4SIMD const & Nref);
511
512	//! For the incident vector I and surface orientation N,
513	//! returns the reflection direction : result = I - 2.0 * dot(N, I) * N.
514	///
515	/// @see gtx_simd_vec4
516	detail::fvec4SIMD reflect(
517		detail::fvec4SIMD const & I,
518		detail::fvec4SIMD const & N);
519
520	//! For the incident vector I and surface normal N,
521	//! and the ratio of indices of refraction eta,
522	//! return the refraction vector.
523	///
524	/// @see gtx_simd_vec4
525	detail::fvec4SIMD refract(
526		detail::fvec4SIMD const & I,
527		detail::fvec4SIMD const & N,
528		float const & eta);
529
530	//! Returns the positive square root of x.
531	///
532	/// @see gtx_simd_vec4
533	detail::fvec4SIMD sqrt(
534		detail::fvec4SIMD const & x);
535
536	//! Returns the positive square root of x with the nicest quality but very slow.
537	//! Slightly more accurate but much slower than simdSqrt.
538	///
539	/// @see gtx_simd_vec4
540	detail::fvec4SIMD niceSqrt(
541		detail::fvec4SIMD const & x);
542
543	//! Returns the positive square root of x
544	//! Less accurate but much faster than sqrt.
545	///
546	/// @see gtx_simd_vec4
547	detail::fvec4SIMD fastSqrt(
548		detail::fvec4SIMD const & x);
549
550	//! Returns the reciprocal of the positive square root of x.
551	///
552	/// @see gtx_simd_vec4
553	detail::fvec4SIMD inversesqrt(
554		detail::fvec4SIMD const & x);
555
556	//! Returns the reciprocal of the positive square root of x.
557	//! Faster than inversesqrt but less accurate.
558	///
559	/// @see gtx_simd_vec4
560	detail::fvec4SIMD fastInversesqrt(
561		detail::fvec4SIMD const & x);
562
563	/// @}
564}//namespace glm
565
566#include "simd_vec4.inl"
567
568#if (GLM_COMPILER & GLM_COMPILER_VC)
569#	pragma warning(pop)
570#endif
571
572#endif//(GLM_ARCH != GLM_ARCH_PURE)
573
574#endif//GLM_GTX_simd_vec4
575