fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / Squish / simd_sse.h
blob8fc2eb7a41fdaf2dbecd3645f8b83df3995ccda1
1 /* -----------------------------------------------------------------------------
3 Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
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:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
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.
24 -------------------------------------------------------------------------- */
26 #ifndef SQUISH_SIMD_SSE_H
27 #define SQUISH_SIMD_SSE_H
29 #include <xmmintrin.h>
30 #if ( SQUISH_USE_SSE > 1 )
31 #include <emmintrin.h>
32 #endif
34 #define SQUISH_SSE_SPLAT( a ) \
35 ( ( a ) | ( ( a ) << 2 ) | ( ( a ) << 4 ) | ( ( a ) << 6 ) )
37 #define SQUISH_SSE_SHUF( x, y, z, w ) \
38 ( ( x ) | ( ( y ) << 2 ) | ( ( z ) << 4 ) | ( ( w ) << 6 ) )
40 namespace osgsquish {
42 #define VEC4_CONST( X ) Vec4( X )
44 class Vec4
46 public:
47 typedef Vec4 const& Arg;
49 Vec4() {}
51 explicit Vec4( __m128 v ) : m_v( v ) {}
53 Vec4( Vec4 const& arg ) : m_v( arg.m_v ) {}
55 Vec4& operator=( Vec4 const& arg )
57 m_v = arg.m_v;
58 return *this;
61 explicit Vec4( float s ) : m_v( _mm_set1_ps( s ) ) {}
63 Vec4( float x, float y, float z, float w ) : m_v( _mm_setr_ps( x, y, z, w ) ) {}
65 Vec3 GetVec3() const
67 #ifdef __GNUC__
68 __attribute__ ((__aligned__ (16))) float c[4];
69 #else
70 __declspec(align(16)) float c[4];
71 #endif
72 _mm_store_ps( c, m_v );
73 return Vec3( c[0], c[1], c[2] );
76 Vec4 SplatX() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 0 ) ) ); }
77 Vec4 SplatY() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 1 ) ) ); }
78 Vec4 SplatZ() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 2 ) ) ); }
79 Vec4 SplatW() const { return Vec4( _mm_shuffle_ps( m_v, m_v, SQUISH_SSE_SPLAT( 3 ) ) ); }
81 Vec4& operator+=( Arg v )
83 m_v = _mm_add_ps( m_v, v.m_v );
84 return *this;
87 Vec4& operator-=( Arg v )
89 m_v = _mm_sub_ps( m_v, v.m_v );
90 return *this;
93 Vec4& operator*=( Arg v )
95 m_v = _mm_mul_ps( m_v, v.m_v );
96 return *this;
99 friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right )
101 return Vec4( _mm_add_ps( left.m_v, right.m_v ) );
104 friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right )
106 return Vec4( _mm_sub_ps( left.m_v, right.m_v ) );
109 friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right )
111 return Vec4( _mm_mul_ps( left.m_v, right.m_v ) );
114 //! Returns a*b + c
115 friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
117 return Vec4( _mm_add_ps( _mm_mul_ps( a.m_v, b.m_v ), c.m_v ) );
120 //! Returns -( a*b - c )
121 friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
123 return Vec4( _mm_sub_ps( c.m_v, _mm_mul_ps( a.m_v, b.m_v ) ) );
126 friend Vec4 Reciprocal( Vec4::Arg v )
128 // get the reciprocal estimate
129 __m128 estimate = _mm_rcp_ps( v.m_v );
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 ) );
136 friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
138 return Vec4( _mm_min_ps( left.m_v, right.m_v ) );
141 friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
143 return Vec4( _mm_max_ps( left.m_v, right.m_v ) );
146 friend Vec4 Truncate( Vec4::Arg v )
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 ) );
154 // convert to floats
155 __m128 part = _mm_movelh_ps( input, _mm_cvtpi32_ps( input, hi ) );
156 __m128 truncated = _mm_cvtpi32_ps( part, lo );
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
167 friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
169 __m128 bits = _mm_cmplt_ps( left.m_v, right.m_v );
170 int value = _mm_movemask_ps( bits );
171 return value != 0;
174 private:
175 __m128 m_v;
178 } // namespace squish
180 #endif // ndef SQUISH_SIMD_SSE_H