1 /**
2 *** Copyright (C) 1985-1999 Intel Corporation.  All rights reserved.
3 ***
4 *** The information and source code contained herein is the exclusive
5 *** property of Intel Corporation and may not be disclosed, examined
6 *** or reproduced in whole or in part without explicit written authorization
7 *** from the company.
8 ***
9 **/
10
11 /*
12  *  Definition of a C++ class interface to Streaming SIMD Extension intrinsics.
13  *
14  *
15  *    File name : fvec.h  Fvec class definitions
16  *
17  *    Concept: A C++ abstraction of Streaming SIMD Extensions designed to improve
18  *
19  *  programmer productivity.  Speed and accuracy are sacrificed for utility.
20  *
21  *    Facilitates an easy transition to compiler intrinsics
22  *
23  *    or assembly language.
24  *
25  *    F32vec4:    4 packed single precision
26  *                32-bit floating point numbers
27 */
28
29 #ifndef _FVEC_H_INCLUDED
30 #define _FVEC_H_INCLUDED
31 #ifndef RC_INVOKED
32
33 #if !defined __cplusplus
34     #error ERROR: This file is only supported in C++ compilations!
35 #endif /* !__cplusplus */
36
37 #if defined(_M_CEE_PURE)
38     #error ERROR: This file is not supported in the pure mode!
39 #else
40
41 #include <xmmintrin.h> /* Streaming SIMD Extensions Intrinsics include file */
42 #include <assert.h>
43 #include <ivec.h>
44 #include <crtdefs.h>
45
46 /* Define _ENABLE_VEC_DEBUG to enable std::ostream inserters for debug output */
47 #if defined(_ENABLE_VEC_DEBUG)
48     #include <iostream>
49 #endif
50
51 #ifdef  _MSC_VER
52 #pragma pack(push,_CRT_PACKING)
53 #endif  /* _MSC_VER */
54
55 #pragma pack(push,16) /* Must ensure class & union 16-B aligned */
56
57 #define EXPLICIT explicit
58
59 class F32vec4
60 {
61 protected:
62           __m128 vec;
63 public:
64
65     /* Constructors: __m128, 4 floats, 1 float */
66     F32vec4() {}
67
68     /* initialize 4 SP FP with __m128 data type */
69     F32vec4(__m128 m)                    { vec = m;}
70
71     /* initialize 4 SP FPs with 4 floats */
72     F32vec4(float f3, float f2, float f1, float f0)        { vec= _mm_set_ps(f3,f2,f1,f0); }
73
74     /* Explicitly initialize each of 4 SP FPs with same float */
75     EXPLICIT F32vec4(float f)    { vec = _mm_set_ps1(f); }
76
77     /* Explicitly initialize each of 4 SP FPs with same double */
78     EXPLICIT F32vec4(double d)    { vec = _mm_set_ps1((float) d); }
79
80     /* Assignment operations */
81
82     F32vec4& operator =(float f) { vec = _mm_set_ps1(f); return *this; }
83
84     F32vec4& operator =(double d) { vec = _mm_set_ps1((float) d); return *this; }
85
86     /* Conversion functions */
87     operator  __m128() const    { return vec; }        /* Convert to __m128 */
88
89      /* Logical Operators */
90     friend F32vec4 operator &(const F32vec4 &a, const F32vec4 &b) { return _mm_and_ps(a,b); }
91     friend F32vec4 operator |(const F32vec4 &a, const F32vec4 &b) { return _mm_or_ps(a,b); }
92     friend F32vec4 operator ^(const F32vec4 &a, const F32vec4 &b) { return _mm_xor_ps(a,b); }
93
94     /* Arithmetic Operators */
95     friend F32vec4 operator +(const F32vec4 &a, const F32vec4 &b) { return _mm_add_ps(a,b); }
96     friend F32vec4 operator -(const F32vec4 &a, const F32vec4 &b) { return _mm_sub_ps(a,b); }
97     friend F32vec4 operator *(const F32vec4 &a, const F32vec4 &b) { return _mm_mul_ps(a,b); }
98     friend F32vec4 operator /(const F32vec4 &a, const F32vec4 &b) { return _mm_div_ps(a,b); }
99
100     F32vec4& operator =(const F32vec4 &a) { vec = a.vec; return *this; }
101     F32vec4& operator =(const __m128 &avec) { vec = avec; return *this; }
102     F32vec4& operator +=(F32vec4 &a) { return *this = _mm_add_ps(vec,a); }
103     F32vec4& operator -=(F32vec4 &a) { return *this = _mm_sub_ps(vec,a); }
104     F32vec4& operator *=(F32vec4 &a) { return *this = _mm_mul_ps(vec,a); }
105     F32vec4& operator /=(F32vec4 &a) { return *this = _mm_div_ps(vec,a); }
106     F32vec4& operator &=(F32vec4 &a) { return *this = _mm_and_ps(vec,a); }
107     F32vec4& operator |=(F32vec4 &a) { return *this = _mm_or_ps(vec,a); }
108     F32vec4& operator ^=(F32vec4 &a) { return *this = _mm_xor_ps(vec,a); }
109
110     /* Horizontal Add */
111     friend float add_horizontal(F32vec4 &a)
112     {
113         F32vec4 ftemp = _mm_add_ss(a,_mm_add_ss(_mm_shuffle_ps(a, a, 1),_mm_add_ss(_mm_shuffle_ps(a, a, 2),_mm_shuffle_ps(a, a, 3))));
114         return ftemp[0];
115     }
116
117     /* Square Root */
118     friend F32vec4 sqrt(const F32vec4 &a)        { return _mm_sqrt_ps(a); }
119     /* Reciprocal */
120     friend F32vec4 rcp(const F32vec4 &a)        { return _mm_rcp_ps(a); }
121     /* Reciprocal Square Root */
122     friend F32vec4 rsqrt(const F32vec4 &a)        { return _mm_rsqrt_ps(a); }
123
124     /* NewtonRaphson Reciprocal
125          [2 * rcpps(x) - (x * rcpps(x) * rcpps(x))] */
126     friend F32vec4 rcp_nr(const F32vec4 &a)
127     {
128         F32vec4 Ra0 = _mm_rcp_ps(a);
129         return _mm_sub_ps(_mm_add_ps(Ra0, Ra0), _mm_mul_ps(_mm_mul_ps(Ra0, a), Ra0));
130     }
131
132     /*    NewtonRaphson Reciprocal Square Root
133           0.5 * rsqrtps * (3 - x * rsqrtps(x) * rsqrtps(x)) */
134 #pragma warning(push)
135 #pragma warning(disable : 4640)
136     friend F32vec4 rsqrt_nr(const F32vec4 &a)
137     {
138         static const F32vec4 fvecf0pt5(0.5f);
139         static const F32vec4 fvecf3pt0(3.0f);
140         F32vec4 Ra0 = _mm_rsqrt_ps(a);
141         return (fvecf0pt5 * Ra0) * (fvecf3pt0 - (a * Ra0) * Ra0);
142     }
143 #pragma warning(pop)
144
145     /* Compares: Mask is returned  */
146     /* Macros expand to all compare intrinsics.  Example:
147     friend F32vec4 cmpeq(const F32vec4 &a, const F32vec4 &b)
148     { return _mm_cmpeq_ps(a,b);} */
149     #define Fvec32s4_COMP(op) \
150     friend F32vec4 cmp##op (const F32vec4 &a, const F32vec4 &b) { return _mm_cmp##op##_ps(a,b); }
151         Fvec32s4_COMP(eq)                    /* expanded to cmpeq(a,b) */
152         Fvec32s4_COMP(lt)                    /* expanded to cmplt(a,b) */
153         Fvec32s4_COMP(le)                    /* expanded to cmple(a,b) */
154         Fvec32s4_COMP(gt)                    /* expanded to cmpgt(a,b) */
155         Fvec32s4_COMP(ge)                    /* expanded to cmpge(a,b) */
156         Fvec32s4_COMP(neq)                    /* expanded to cmpneq(a,b) */
157         Fvec32s4_COMP(nlt)                    /* expanded to cmpnlt(a,b) */
158         Fvec32s4_COMP(nle)                    /* expanded to cmpnle(a,b) */
159         Fvec32s4_COMP(ngt)                    /* expanded to cmpngt(a,b) */
160         Fvec32s4_COMP(nge)                    /* expanded to cmpnge(a,b) */
161     #undef Fvec32s4_COMP
162
163     /* Min and Max */
164     friend F32vec4 simd_min(const F32vec4 &a, const F32vec4 &b) { return _mm_min_ps(a,b); }
165     friend F32vec4 simd_max(const F32vec4 &a, const F32vec4 &b) { return _mm_max_ps(a,b); }
166
167     /* Debug Features */
168 #if defined(_ENABLE_VEC_DEBUG)
169     /* Output */
170     friend std::ostream & operator<<(std::ostream & os, const F32vec4 &a)
171     {
172     /* To use: cout << "Elements of F32vec4 fvec are: " << fvec; */
173       float *fp = (float*)&a;
174           os << "[3]:" << *(fp+3)
175             << " [2]:" << *(fp+2)
176             << " [1]:" << *(fp+1)
177             << " [0]:" << *fp;
178         return os;
179     }
180 #endif
181     /* Element Access Only, no modifications to elements*/
182     const float& operator[](int i) const
183     {
184         /* Assert enabled only during debug /DDEBUG */
185         assert((0 <= i) && (i <= 3));            /* User should only access elements 0-3 */
186         float *fp = (float*)&vec;
187         return *(fp+i);
188     }
189     /* Element Access and Modification*/
190     float& operator[](int i)
191     {
192         /* Assert enabled only during debug /DDEBUG */
193         assert((0 <= i) && (i <= 3));            /* User should only access elements 0-3 */
194         float *fp = (float*)&vec;
195         return *(fp+i);
196     }
197 };
198
199                         /* Miscellaneous */
200
201 /* Interleave low order data elements of a and b into destination */
202 inline F32vec4 unpack_low(const F32vec4 &a, const F32vec4 &b)
203 { return _mm_unpacklo_ps(a, b); }
204
205 /* Interleave high order data elements of a and b into target */
206 inline F32vec4 unpack_high(const F32vec4 &a, const F32vec4 &b)
207 { return _mm_unpackhi_ps(a, b); }
208
209 /* Move Mask to Integer returns 4 bit mask formed of most significant bits of a */
210 inline int move_mask(const F32vec4 &a)
211 { return _mm_movemask_ps(a);}
212
213                         /* Data Motion Functions */
214
215 /* Load Unaligned loadu_ps: Unaligned */
216 inline void loadu(F32vec4 &a, float *p)
217 { a = _mm_loadu_ps(p); }
218
219 /* Store Temporal storeu_ps: Unaligned */
220 inline void storeu(float *p, const F32vec4 &a)
221 { _mm_storeu_ps(p, a); }
222
223                         /* Cacheability Support */
224
225 /* Non-Temporal Store */
226 inline void store_nta(float *p, F32vec4 &a)
227 { _mm_stream_ps(p,a);}
228
229                         /* Conditional Selects:*/
230 /*(a OP b)? c : d; where OP is any compare operator
231 Macros expand to conditional selects which use all compare intrinsics.
232 Example:
233 friend F32vec4 select_eq(const F32vec4 &a, const F32vec4 &b, const F32vec4 &c, const F32vec4 &d)
234 {
235     F32vec4 mask = _mm_cmpeq_ps(a,b);
236     return( (mask & c) | F32vec4((_mm_andnot_ps(mask,d))));
237 }
238 */
239
240 #define Fvec32s4_SELECT(op) \
241 inline F32vec4 select_##op (const F32vec4 &a, const F32vec4 &b, const F32vec4 &c, const F32vec4 &d)          \
242 {                                                                \
243     F32vec4 mask = _mm_cmp##op##_ps(a,b);                        \
244     return( (mask & c) | F32vec4((_mm_andnot_ps(mask,d))));    \
245 }
246 Fvec32s4_SELECT(eq)            /* generates select_eq(a,b) */
247 Fvec32s4_SELECT(lt)            /* generates select_lt(a,b) */
248 Fvec32s4_SELECT(le)            /* generates select_le(a,b) */
249 Fvec32s4_SELECT(gt)            /* generates select_gt(a,b) */
250 Fvec32s4_SELECT(ge)            /* generates select_ge(a,b) */
251 Fvec32s4_SELECT(neq)        /* generates select_neq(a,b) */
252 Fvec32s4_SELECT(nlt)        /* generates select_nlt(a,b) */
253 Fvec32s4_SELECT(nle)        /* generates select_nle(a,b) */
254 Fvec32s4_SELECT(ngt)        /* generates select_ngt(a,b) */
255 Fvec32s4_SELECT(nge)        /* generates select_nge(a,b) */
256 #undef Fvec32s4_SELECT
257
258
259 /* Streaming SIMD Extensions Integer Intrinsics */
260
261 /* Max and Min */
262 inline Is16vec4 simd_max(const Is16vec4 &a, const Is16vec4 &b)        { return _m_pmaxsw(a,b);}
263 inline Is16vec4 simd_min(const Is16vec4 &a, const Is16vec4 &b)        { return _m_pminsw(a,b);}
264 inline Iu8vec8 simd_max(const Iu8vec8 &a, const Iu8vec8 &b)            { return _m_pmaxub(a,b);}
265 inline Iu8vec8 simd_min(const Iu8vec8 &a, const Iu8vec8 &b)            { return _m_pminub(a,b);}
266
267 /* Average */
268 inline Iu16vec4 simd_avg(const Iu16vec4 &a, const Iu16vec4 &b)        { return _m_pavgw(a,b); }
269 inline Iu8vec8 simd_avg(const Iu8vec8 &a, const Iu8vec8 &b)            { return _m_pavgb(a,b); }
270
271 /* Move ByteMask To Int: returns mask formed from most sig bits    of each vec of a */
272 inline int move_mask(const I8vec8 &a)                                { return _m_pmovmskb(a);}
273
274 /* Packed Multiply High Unsigned */
275 inline Iu16vec4 mul_high(const Iu16vec4 &a, const Iu16vec4 &b)        { return _m_pmulhuw(a,b); }
276
277 /* Byte Mask Write: Write bytes if most significant bit in each corresponding byte is set */
278 inline void mask_move(const I8vec8 &a, const I8vec8 &b, char *addr)    { _m_maskmovq(a, b, addr); }
279
280 /* Data Motion: Store Non Temporal */
281 inline void store_nta(__m64 *p, M64 &a) { _mm_stream_pi(p,a); }
282
283 /* Conversions between ivec <-> fvec */
284
285 /* Convert first element of F32vec4 to int with truncation */
286 inline int F32vec4ToInt(const F32vec4 &a)
Lines 287 ... 296 are skipped.
297     __m64 result;
298     result = _mm_cvtt_ps2pi(a);
299     return Is32vec2(result);
300
301 }
302
303 /* Convert the 32-bit int i to an SP FP value; the upper three SP FP values are passed through from a. */
304 inline F32vec4 IntToF32vec4(const F32vec4 &a, int i)
305 {
306
307     __m128 result;
308     result = _mm_cvt_si2ss(a,i);
309     return F32vec4(result);
310
311 }
312
313 /* Convert the two 32-bit integer values in b to two SP FP values; the upper two SP FP values are passed from a. */
314 inline F32vec4 Is32vec2ToF32vec4(const F32vec4 &a, const Is32vec2 &b)
315 {
316
317     __m128 result;
318     result = _mm_cvt_pi2ps(a,b);
319     return F32vec4(result);
320 }
321
322 class F32vec1
323 {
324 protected:
325           __m128 vec;
326 public:
327
328     /* Constructors: 1 float */
329     F32vec1() {}
330
331     F32vec1(int i)        { vec = _mm_cvt_si2ss(vec,i);};
332
333     /* Initialize each of 4 SP FPs with same float */
334     EXPLICIT F32vec1(float f)    { vec = _mm_set_ss(f); }
335
336     /* Initialize each of 4 SP FPs with same float */
337     EXPLICIT F32vec1(double d)    { vec = _mm_set_ss((float) d); }
338
339     /* initialize with __m128 data type */
340     F32vec1(__m128 m)    { vec = m; }
341
342     /* Conversion functions */
343     operator  __m128() const    { return vec; }        /* Convert to float */
344
345      /* Logical Operators */
346     friend F32vec1 operator &(const F32vec1 &a, const F32vec1 &b) { return _mm_and_ps(a,b); }
347     friend F32vec1 operator |(const F32vec1 &a, const F32vec1 &b) { return _mm_or_ps(a,b); }
348     friend F32vec1 operator ^(const F32vec1 &a, const F32vec1 &b) { return _mm_xor_ps(a,b); }
349
350     /* Arithmetic Operators */
351     friend F32vec1 operator +(const F32vec1 &a, const F32vec1 &b) { return _mm_add_ss(a,b); }
352     friend F32vec1 operator -(const F32vec1 &a, const F32vec1 &b) { return _mm_sub_ss(a,b); }
353     friend F32vec1 operator *(const F32vec1 &a, const F32vec1 &b) { return _mm_mul_ss(a,b); }
354     friend F32vec1 operator /(const F32vec1 &a, const F32vec1 &b) { return _mm_div_ss(a,b); }
355
356     F32vec1& operator +=(F32vec1 &a) { return *this = _mm_add_ss(vec,a); }
357     F32vec1& operator -=(F32vec1 &a) { return *this = _mm_sub_ss(vec,a); }
358     F32vec1& operator *=(F32vec1 &a) { return *this = _mm_mul_ss(vec,a); }
359     F32vec1& operator /=(F32vec1 &a) { return *this = _mm_div_ss(vec,a); }
360     F32vec1& operator &=(F32vec1 &a) { return *this = _mm_and_ps(vec,a); }
361     F32vec1& operator |=(F32vec1 &a) { return *this = _mm_or_ps(vec,a); }
362     F32vec1& operator ^=(F32vec1 &a) { return *this = _mm_xor_ps(vec,a); }
363
364
365     /* Square Root */
366     friend F32vec1 sqrt(const F32vec1 &a)        { return _mm_sqrt_ss(a); }
367     /* Reciprocal */
368     friend F32vec1 rcp(const F32vec1 &a)        { return _mm_rcp_ss(a); }
369     /* Reciprocal Square Root */
370     friend F32vec1 rsqrt(const F32vec1 &a)        { return _mm_rsqrt_ss(a); }
371
372     /* NewtonRaphson Reciprocal
373          [2 * rcpss(x) - (x * rcpss(x) * rcpss(x))] */
374     friend F32vec1 rcp_nr(const F32vec1 &a)
375     {
376         F32vec1 Ra0 = _mm_rcp_ss(a);
377         return _mm_sub_ss(_mm_add_ss(Ra0, Ra0), _mm_mul_ss(_mm_mul_ss(Ra0, a), Ra0));
378     }
379
380     /*    NewtonRaphson Reciprocal Square Root
381           0.5 * rsqrtss * (3 - x * rsqrtss(x) * rsqrtss(x)) */
382 #pragma warning(push)
383 #pragma warning(disable : 4640)
384     friend F32vec1 rsqrt_nr(const F32vec1 &a)
385     {
386         static const F32vec1 fvecf0pt5(0.5f);
387         static const F32vec1 fvecf3pt0(3.0f);
388         F32vec1 Ra0 = _mm_rsqrt_ss(a);
389         return (fvecf0pt5 * Ra0) * (fvecf3pt0 - (a * Ra0) * Ra0);
390     }
391 #pragma warning(pop)
392
393     /* Compares: Mask is returned  */
394     /* Macros expand to all compare intrinsics.  Example:
395     friend F32vec1 cmpeq(const F32vec1 &a, const F32vec1 &b)
396     { return _mm_cmpeq_ss(a,b);} */
397     #define Fvec32s1_COMP(op) \
398     friend F32vec1 cmp##op (const F32vec1 &a, const F32vec1 &b) { return _mm_cmp##op##_ss(a,b); }
399         Fvec32s1_COMP(eq)                    /* expanded to cmpeq(a,b) */
400         Fvec32s1_COMP(lt)                    /* expanded to cmplt(a,b) */
401         Fvec32s1_COMP(le)                    /* expanded to cmple(a,b) */
402         Fvec32s1_COMP(gt)                    /* expanded to cmpgt(a,b) */
403         Fvec32s1_COMP(ge)                    /* expanded to cmpge(a,b) */
404         Fvec32s1_COMP(neq)                    /* expanded to cmpneq(a,b) */
405         Fvec32s1_COMP(nlt)                    /* expanded to cmpnlt(a,b) */
406         Fvec32s1_COMP(nle)                    /* expanded to cmpnle(a,b) */
407         Fvec32s1_COMP(ngt)                    /* expanded to cmpngt(a,b) */
408         Fvec32s1_COMP(nge)                    /* expanded to cmpnge(a,b) */
409     #undef Fvec32s1_COMP
410
411     /* Min and Max */
412     friend F32vec1 simd_min(const F32vec1 &a, const F32vec1 &b) { return _mm_min_ss(a,b); }
413     friend F32vec1 simd_max(const F32vec1 &a, const F32vec1 &b) { return _mm_max_ss(a,b); }
414
415     /* Debug Features */
416 #if defined(_ENABLE_VEC_DEBUG)
417     /* Output */
418     friend std::ostream & operator<<(std::ostream & os, const F32vec1 &a)
419     {
420     /* To use: cout << "Elements of F32vec1 fvec are: " << fvec; */
421       float *fp = (float*)&a;
422           os << "float:" << *fp;
423         return os;
424     }
425 #endif
426
427 };
428
429                         /* Conditional Selects:*/
430 /*(a OP b)? c : d; where OP is any compare operator
431 Macros expand to conditional selects which use all compare intrinsics.
432 Example:
433 friend F32vec1 select_eq(const F32vec1 &a, const F32vec1 &b, const F32vec1 &c, const F32vec1 &d)
434 {
435     F32vec1 mask = _mm_cmpeq_ss(a,b);
436     return( (mask & c) | F32vec1((_mm_andnot_ps(mask,d))));
437 }
438 */
439
440 #define Fvec32s1_SELECT(op) \
441 inline F32vec1 select_##op (const F32vec1 &a, const F32vec1 &b, const F32vec1 &c, const F32vec1 &d)          \
442 {                                                         \
443     F32vec1 mask = _mm_cmp##op##_ss(a,b);                                                       \
444     return( (mask & c) | F32vec1((_mm_andnot_ps(mask,d))));                                                                           \
445 }
446 Fvec32s1_SELECT(eq)            /* generates select_eq(a,b) */
447 Fvec32s1_SELECT(lt)            /* generates select_lt(a,b) */
448 Fvec32s1_SELECT(le)            /* generates select_le(a,b) */
449 Fvec32s1_SELECT(gt)            /* generates select_gt(a,b) */
450 Fvec32s1_SELECT(ge)            /* generates select_ge(a,b) */
451 Fvec32s1_SELECT(neq)        /* generates select_neq(a,b) */
452 Fvec32s1_SELECT(nlt)        /* generates select_nlt(a,b) */
453 Fvec32s1_SELECT(nle)        /* generates select_nle(a,b) */
454 Fvec32s1_SELECT(ngt)        /* generates select_ngt(a,b) */
455 Fvec32s1_SELECT(nge)        /* generates select_nge(a,b) */
456 #undef Fvec32s1_SELECT
457
458 /* Conversions between ivec <-> fvec */
459
460 /* Convert F32vec1 to int */
461 inline int F32vec1ToInt(const F32vec1 &a)
462 {
463     return _mm_cvtt_ss2si(a);
464 }
465
466
467
468 #pragma pack(pop) /* 16-B aligned */
469
470 #ifdef  _MSC_VER
471 #pragma pack(pop)
472 #endif  /* _MSC_VER */
473
474 #endif /* defined(_M_CEE_PURE) */
475
476 #endif /* RC_INVOKED */
477 #endif /* _FVEC_H_INCLUDED */
478
479
480