fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / Squish / simd_ve.h
blobdf27fdd9b8904e715925856d894015ee2b63cae8
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_VE_H
27 #define SQUISH_SIMD_VE_H
29 #include <altivec.h>
30 #undef bool
32 namespace osgsquish {
34 #define VEC4_CONST( X ) Vec4( ( vector float )( X ) )
36 class Vec4
38 public:
39 typedef Vec4 Arg;
41 Vec4() {}
43 explicit Vec4( vector float v ) : m_v( v ) {}
45 Vec4( Vec4 const& arg ) : m_v( arg.m_v ) {}
47 Vec4& operator=( Vec4 const& arg )
49 m_v = arg.m_v;
50 return *this;
53 explicit Vec4( float s )
55 union { vector float v; float c[4]; } u;
56 u.c[0] = s;
57 u.c[1] = s;
58 u.c[2] = s;
59 u.c[3] = s;
60 m_v = u.v;
63 Vec4( float x, float y, float z, float w )
65 union { vector float v; float c[4]; } u;
66 u.c[0] = x;
67 u.c[1] = y;
68 u.c[2] = z;
69 u.c[3] = w;
70 m_v = u.v;
73 Vec3 GetVec3() const
75 union { vector float v; float c[4]; } u;
76 u.v = m_v;
77 return Vec3( u.c[0], u.c[1], u.c[2] );
80 Vec4 SplatX() const { return Vec4( vec_splat( m_v, 0 ) ); }
81 Vec4 SplatY() const { return Vec4( vec_splat( m_v, 1 ) ); }
82 Vec4 SplatZ() const { return Vec4( vec_splat( m_v, 2 ) ); }
83 Vec4 SplatW() const { return Vec4( vec_splat( m_v, 3 ) ); }
85 Vec4& operator+=( Arg v )
87 m_v = vec_add( m_v, v.m_v );
88 return *this;
91 Vec4& operator-=( Arg v )
93 m_v = vec_sub( m_v, v.m_v );
94 return *this;
97 Vec4& operator*=( Arg v )
99 m_v = vec_madd( m_v, v.m_v, ( vector float )( -0.0f ) );
100 return *this;
103 friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right )
105 return Vec4( vec_add( left.m_v, right.m_v ) );
108 friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right )
110 return Vec4( vec_sub( left.m_v, right.m_v ) );
113 friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right )
115 return Vec4( vec_madd( left.m_v, right.m_v, ( vector float )( -0.0f ) ) );
118 //! Returns a*b + c
119 friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
121 return Vec4( vec_madd( a.m_v, b.m_v, c.m_v ) );
124 //! Returns -( a*b - c )
125 friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
127 return Vec4( vec_nmsub( a.m_v, b.m_v, c.m_v ) );
130 friend Vec4 Reciprocal( Vec4::Arg v )
132 // get the reciprocal estimate
133 vector float estimate = vec_re( v.m_v );
135 // one round of Newton-Rhaphson refinement
136 vector float diff = vec_nmsub( estimate, v.m_v, ( vector float )( 1.0f ) );
137 return Vec4( vec_madd( diff, estimate, estimate ) );
140 friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
142 return Vec4( vec_min( left.m_v, right.m_v ) );
145 friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
147 return Vec4( vec_max( left.m_v, right.m_v ) );
150 friend Vec4 Truncate( Vec4::Arg v )
152 return Vec4( vec_trunc( v.m_v ) );
155 friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
157 return vec_any_lt( left.m_v, right.m_v ) != 0;
160 private:
161 vector float m_v;
164 } // namespace squish
166 #endif // ndef SQUISH_SIMD_VE_H