1 /* -----------------------------------------------------------------------------
2 
3 	Copyright (c) 2006 Simon Brown                          si@sjbrown.co.uk
4 
5 	Permission is hereby granted, free of charge, to any person obtaining
6 	a copy of this software and associated documentation files (the
7 	"Software"), to	deal in the Software without restriction, including
8 	without limitation the rights to use, copy, modify, merge, publish,
9 	distribute, sublicense, and/or sell copies of the Software, and to
10 	permit persons to whom the Software is furnished to do so, subject to
11 	the following conditions:
12 
13 	The above copyright notice and this permission notice shall be included
14 	in all copies or substantial portions of the Software.
15 
16 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24    -------------------------------------------------------------------------- */
25 
26 #ifndef SQUISH_SIMD_SSE_H
27 #define SQUISH_SIMD_SSE_H
28 
29 #include <xmmintrin.h>
30 #if ( SQUISH_USE_SSE > 1 )
31 #include <emmintrin.h>
32 #endif
33 #include <cassert>
34 
35 #define SQUISH_SSE_SPLAT( a )										\
36 	( ( a ) | ( ( a ) << 2 ) | ( ( a ) << 4 ) | ( ( a ) << 6 ) )
37 
38 namespace squish {
39 
40 #define VEC4_CONST( X ) Vec4( _mm_set1_ps( X ) )
41 
42 class Vec4
43 {
44 public:
45 	typedef Vec4 const& Arg;
46 
Vec4()47 	Vec4() {}
48 
Vec4(__m128 v)49 	explicit Vec4( __m128 v ) : m_v( v ) {}
50 
Vec4(Vec4 const & arg)51 	Vec4( Vec4 const& arg ) : m_v( arg.m_v ) {}
52 
53 	Vec4& operator=( Vec4 const& arg )
54 	{
55 		m_v = arg.m_v;
56 		return *this;
57 	}
58 
Vec4(const float * v)59 	Vec4( const float * v )
60 	{
61 		m_v = _mm_load_ps( v );
62 	}
63 
Vec4(float x,float y,float z,float w)64 	Vec4( float x, float y, float z, float w )
65 	{
66 		m_v = _mm_setr_ps( x, y, z, w );
67 	}
68 
GetVec3()69 	Vec3 GetVec3() const
70 	{
71 		SQUISH_ALIGN_16 float c[4];
72 		_mm_store_ps( c, m_v );
73 		return Vec3( c[0], c[1], c[2] );
74 	}
75 
SplatX()76 	Vec4 SplatX() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 0 ) ) ); }
SplatY()77 	Vec4 SplatY() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 1 ) ) ); }
SplatZ()78 	Vec4 SplatZ() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 2 ) ) ); }
SplatW()79 	Vec4 SplatW() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 3 ) ) ); }
80 
81 	Vec4& operator+=( Arg v )
82 	{
83 		m_v = _mm_add_ps( m_v, v.m_v );
84 		return *this;
85 	}
86 
87 	Vec4& operator-=( Arg v )
88 	{
89 		m_v = _mm_sub_ps( m_v, v.m_v );
90 		return *this;
91 	}
92 
93 	Vec4& operator*=( Arg v )
94 	{
95 		m_v = _mm_mul_ps( m_v, v.m_v );
96 		return *this;
97 	}
98 
99 	friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right  )
100 	{
101 		return Vec4( _mm_add_ps( left.m_v, right.m_v ) );
102 	}
103 
104 	friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right  )
105 	{
106 		return Vec4( _mm_sub_ps( left.m_v, right.m_v ) );
107 	}
108 
109 	friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right  )
110 	{
111 		return Vec4( _mm_mul_ps( left.m_v, right.m_v ) );
112 	}
113 
114 	//! Returns a*b + c
MultiplyAdd(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)115 	friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
116 	{
117 		return Vec4( _mm_add_ps( _mm_mul_ps( a.m_v, b.m_v ), c.m_v ) );
118 	}
119 
120 	//! Returns -( a*b - c )
NegativeMultiplySubtract(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)121 	friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
122 	{
123 		return Vec4( _mm_sub_ps( c.m_v, _mm_mul_ps( a.m_v, b.m_v ) ) );
124 	}
125 
Reciprocal(Vec4::Arg v)126 	friend Vec4 Reciprocal( Vec4::Arg v )
127 	{
128 		// get the reciprocal estimate
129 		__m128 estimate = _mm_rcp_ps( v.m_v );
130 
131 		// one round of Newton-Rhaphson refinement
132 		__m128 diff = _mm_sub_ps( _mm_set1_ps( 1.0f ), _mm_mul_ps( estimate, v.m_v ) );
133 		return Vec4( _mm_add_ps( _mm_mul_ps( diff, estimate ), estimate ) );
134 	}
135 
Min(Vec4::Arg left,Vec4::Arg right)136 	friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
137 	{
138 		return Vec4( _mm_min_ps( left.m_v, right.m_v ) );
139 	}
140 
Max(Vec4::Arg left,Vec4::Arg right)141 	friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
142 	{
143 		return Vec4( _mm_max_ps( left.m_v, right.m_v ) );
144 	}
145 
Truncate(Vec4::Arg v)146 	friend Vec4 Truncate( Vec4::Arg v )
147 	{
148 #if ( SQUISH_USE_SSE == 1 )
149 		// convert to ints
150 		__m128 input = v.m_v;
151 		__m64 lo = _mm_cvttps_pi32( input );
152 		__m64 hi = _mm_cvttps_pi32( _mm_movehl_ps( input, input ) );
153 
154 		// convert to floats
155 		__m128 part = _mm_movelh_ps( input, _mm_cvtpi32_ps( input, hi ) );
156 		__m128 truncated = _mm_cvtpi32_ps( part, lo );
157 
158 		// clear out the MMX multimedia state to allow FP calls later
159 		_mm_empty();
160 		return Vec4( truncated );
161 #else
162 		// use SSE2 instructions
163 		return Vec4( _mm_cvtepi32_ps( _mm_cvttps_epi32( v.m_v ) ) );
164 #endif
165 	}
166 
CompareEqual(Vec4::Arg left,Vec4::Arg right)167 	friend Vec4 CompareEqual( Vec4::Arg left, Vec4::Arg right )
168 	{
169 		return Vec4( _mm_cmpeq_ps( left.m_v, right.m_v ) );
170 	}
171 
Select(Vec4::Arg off,Vec4::Arg on,Vec4::Arg bits)172 	friend Vec4 Select( Vec4::Arg off, Vec4::Arg on, Vec4::Arg bits )
173 	{
174         __m128 a = _mm_andnot_ps( bits.m_v, off.m_v );
175         __m128 b = _mm_and_ps( bits.m_v, on.m_v );
176 
177         return Vec4( _mm_or_ps( a, b ) );
178 	}
179 
CompareAnyLessThan(Vec4::Arg left,Vec4::Arg right)180 	friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
181 	{
182 		__m128 bits = _mm_cmplt_ps( left.m_v, right.m_v );
183 		int value = _mm_movemask_ps( bits );
184 		return value != 0;
185 	}
186 
187 private:
188 	__m128 m_v;
189 };
190 
191 } // namespace squish
192 
193 #endif // ndef SQUISH_SIMD_SSE_H
194