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_FLOAT_H
27 #define SQUISH_SIMD_FLOAT_H
28 
29 #include <algorithm>
30 
31 namespace squish {
32 
33 #define VEC4_CONST( X ) Vec4( X )
34 
35 class Vec4
36 {
37 public:
38     typedef Vec4 const& Arg;
39 
Vec4()40     Vec4() {}
41 
Vec4(float s)42     explicit Vec4( float s )
43       : m_x( s ),
44         m_y( s ),
45         m_z( s ),
46         m_w( s )
47     {
48     }
49 
Vec4(float x,float y,float z,float w)50     Vec4( float x, float y, float z, float w )
51       : m_x( x ),
52         m_y( y ),
53         m_z( z ),
54         m_w( w )
55     {
56     }
57 
GetVec3()58     Vec3 GetVec3() const
59     {
60         return Vec3( m_x, m_y, m_z );
61     }
62 
SplatX()63     Vec4 SplatX() const { return Vec4( m_x ); }
SplatY()64     Vec4 SplatY() const { return Vec4( m_y ); }
SplatZ()65     Vec4 SplatZ() const { return Vec4( m_z ); }
SplatW()66     Vec4 SplatW() const { return Vec4( m_w ); }
67 
68     Vec4& operator+=( Arg v )
69     {
70         m_x += v.m_x;
71         m_y += v.m_y;
72         m_z += v.m_z;
73         m_w += v.m_w;
74         return *this;
75     }
76 
77     Vec4& operator-=( Arg v )
78     {
79         m_x -= v.m_x;
80         m_y -= v.m_y;
81         m_z -= v.m_z;
82         m_w -= v.m_w;
83         return *this;
84     }
85 
86     Vec4& operator*=( Arg v )
87     {
88         m_x *= v.m_x;
89         m_y *= v.m_y;
90         m_z *= v.m_z;
91         m_w *= v.m_w;
92         return *this;
93     }
94 
95     friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right  )
96     {
97         Vec4 copy( left );
98         return copy += right;
99     }
100 
101     friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right  )
102     {
103         Vec4 copy( left );
104         return copy -= right;
105     }
106 
107     friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right  )
108     {
109         Vec4 copy( left );
110         return copy *= right;
111     }
112 
113     //! Returns a*b + c
MultiplyAdd(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)114     friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
115     {
116         return a*b + c;
117     }
118 
119     //! Returns -( a*b - c )
NegativeMultiplySubtract(Vec4::Arg a,Vec4::Arg b,Vec4::Arg c)120     friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
121     {
122         return c - a*b;
123     }
124 
Reciprocal(Vec4::Arg v)125     friend Vec4 Reciprocal( Vec4::Arg v )
126     {
127         return Vec4(
128             1.0f/v.m_x,
129             1.0f/v.m_y,
130             1.0f/v.m_z,
131             1.0f/v.m_w
132         );
133     }
134 
Min(Vec4::Arg left,Vec4::Arg right)135     friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
136     {
137         return Vec4(
138             std::min( left.m_x, right.m_x ),
139             std::min( left.m_y, right.m_y ),
140             std::min( left.m_z, right.m_z ),
141             std::min( left.m_w, right.m_w )
142         );
143     }
144 
Max(Vec4::Arg left,Vec4::Arg right)145     friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
146     {
147         return Vec4(
148             std::max( left.m_x, right.m_x ),
149             std::max( left.m_y, right.m_y ),
150             std::max( left.m_z, right.m_z ),
151             std::max( left.m_w, right.m_w )
152         );
153     }
154 
Truncate(Vec4::Arg v)155     friend Vec4 Truncate( Vec4::Arg v )
156     {
157         return Vec4(
158             v.m_x > 0.0f ? std::floor( v.m_x ) : std::ceil( v.m_x ),
159             v.m_y > 0.0f ? std::floor( v.m_y ) : std::ceil( v.m_y ),
160             v.m_z > 0.0f ? std::floor( v.m_z ) : std::ceil( v.m_z ),
161             v.m_w > 0.0f ? std::floor( v.m_w ) : std::ceil( v.m_w )
162         );
163     }
164 
CompareAnyLessThan(Vec4::Arg left,Vec4::Arg right)165     friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
166     {
167         return left.m_x < right.m_x
168             || left.m_y < right.m_y
169             || left.m_z < right.m_z
170             || left.m_w < right.m_w;
171     }
172 
173 private:
174     float m_x;
175     float m_y;
176     float m_z;
177     float m_w;
178 };
179 
180 } // namespace squish
181 
182 #endif // ndef SQUISH_SIMD_FLOAT_H
183 
184