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_VE_H
27 #define SQUISH_SIMD_VE_H
28 
29 #include <altivec.h>
30 #undef bool
31 
32 namespace squish {
33 
34 #define VEC4_CONST( X ) Vec4( ( vector float )( X ) )
35 
36 class Vec4
37 {
38 public:
39 	typedef Vec4 Arg;
40 
Vec4()41 	Vec4() {}
42 
Vec4(vector float v)43 	explicit Vec4( vector float v ) : m_v( v ) {}
44 
Vec4(Vec4 const & arg)45 	Vec4( Vec4 const& arg ) : m_v( arg.m_v ) {}
46 
47 	Vec4& operator=( Vec4 const& arg )
48 	{
49 		m_v = arg.m_v;
50 		return *this;
51 	}
52 
Vec4(float s)53 	explicit Vec4( float s )
54 	{
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;
61 	}
62 
Vec4(float x,float y,float z,float w)63 	Vec4( float x, float y, float z, float w )
64 	{
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;
71 	}
72 
GetVec3()73 	Vec3 GetVec3() const
74 	{
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] );
78 	}
79 
SplatX()80 	Vec4 SplatX() const { return Vec4( vec_splat( m_v, 0 ) ); }
SplatY()81 	Vec4 SplatY() const { return Vec4( vec_splat( m_v, 1 ) ); }
SplatZ()82 	Vec4 SplatZ() const { return Vec4( vec_splat( m_v, 2 ) ); }
SplatW()83 	Vec4 SplatW() const { return Vec4( vec_splat( m_v, 3 ) ); }
84 
85 	Vec4& operator+=( Arg v )
86 	{
87 		m_v = vec_add( m_v, v.m_v );
88 		return *this;
89 	}
90 
91 	Vec4& operator-=( Arg v )
92 	{
93 		m_v = vec_sub( m_v, v.m_v );
94 		return *this;
95 	}
96 
97 	Vec4& operator*=( Arg v )
98 	{
99 		m_v = vec_madd( m_v, v.m_v, ( vector float )( -0.0f ) );
100 		return *this;
101 	}
102 
103 	friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right  )
104 	{
105 		return Vec4( vec_add( left.m_v, right.m_v ) );
106 	}
107 
108 	friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right  )
109 	{
110 		return Vec4( vec_sub( left.m_v, right.m_v ) );
111 	}
112 
113 	friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right  )
114 	{
115 		return Vec4( vec_madd( left.m_v, right.m_v, ( vector float )( -0.0f ) ) );
116 	}
117 
118 	//! Returns a*b + c
MultiplyAdd(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)119 	friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
120 	{
121 		return Vec4( vec_madd( a.m_v, b.m_v, c.m_v ) );
122 	}
123 
124 	//! Returns -( a*b - c )
NegativeMultiplySubtract(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)125 	friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
126 	{
127 		return Vec4( vec_nmsub( a.m_v, b.m_v, c.m_v ) );
128 	}
129 
Reciprocal(Vec4::Arg v)130 	friend Vec4 Reciprocal( Vec4::Arg v )
131 	{
132 		// get the reciprocal estimate
133 		vector float estimate = vec_re( v.m_v );
134 
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 ) );
138 	}
139 
Min(Vec4::Arg left,Vec4::Arg right)140 	friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
141 	{
142 		return Vec4( vec_min( left.m_v, right.m_v ) );
143 	}
144 
Max(Vec4::Arg left,Vec4::Arg right)145 	friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
146 	{
147 		return Vec4( vec_max( left.m_v, right.m_v ) );
148 	}
149 
Truncate(Vec4::Arg v)150 	friend Vec4 Truncate( Vec4::Arg v )
151 	{
152 		return Vec4( vec_trunc( v.m_v ) );
153 	}
154 
CompareAnyLessThan(Vec4::Arg left,Vec4::Arg right)155 	friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
156 	{
157 		return vec_any_lt( left.m_v, right.m_v ) != 0;
158 	}
159 
160 private:
161 	vector float m_v;
162 };
163 
164 } // namespace squish
165 
166 #endif // ndef SQUISH_SIMD_VE_H
167