PandaRoot
PSEUDO_F32vec4.h
Go to the documentation of this file.
1 #ifndef L1Algo_PSEUDO_F32vec4_H
2 #define L1Algo_PSEUDO_F32vec4_H
3 
4 #include <iostream>
5 #include <cmath>
6 #include "vec_arithmetic.h"
7 
8 /**********************************
9  *
10  * Vector of four floats
11  *
12  **********************************/
13 
14 float min(float x, float y);
15 float max(float x, float y);
16 float asgnb(float x, float y);
17 float rsqrt(float x);
18 float rcp(float x);
19 float sgn(float x);
20 
21 class F32vec4 {
22 
23  public:
24  float v[4];
25 
26  float &operator[](int i) { return ((float *)&v)[i]; }
27  float operator[](int i) const { return ((float *)&v)[i]; }
28 
29  F32vec4() {}
30  F32vec4(const F32vec4 &a)
31  {
32  v[0] = a.v[0];
33  v[1] = a.v[1];
34  v[2] = a.v[2];
35  v[3] = a.v[3];
36  }
37  F32vec4(const float &a)
38  {
39  v[0] = a;
40  v[1] = a;
41  v[2] = a;
42  v[3] = a;
43  }
44 
45  F32vec4(const float &f0, const float &f1, const float &f2, const float &f3)
46  {
47  v[0] = f0;
48  v[1] = f1;
49  v[2] = f2;
50  v[3] = f3;
51  }
52 
53 #define _f2(A, B, F) \
54  F32vec4 z; \
55  z.v[0] = F(A.v[0], B.v[0]); \
56  z.v[1] = F(A.v[1], B.v[1]); \
57  z.v[2] = F(A.v[2], B.v[2]); \
58  z.v[3] = F(A.v[3], B.v[3]); \
59  return z;
60 #define _f1(A, F) \
61  F32vec4 z; \
62  z.v[0] = F(A.v[0]); \
63  z.v[1] = F(A.v[1]); \
64  z.v[2] = F(A.v[2]); \
65  z.v[3] = F(A.v[3]); \
66  return z;
67 #define _op(A, B, O) \
68  F32vec4 z; \
69  z.v[0] = A.v[0] O B.v[0]; \
70  z.v[1] = A.v[1] O B.v[1]; \
71  z.v[2] = A.v[2] O B.v[2]; \
72  z.v[3] = A.v[3] O B.v[3]; \
73  return z;
74 
75  /* Arithmetic Operators */
76  friend F32vec4 operator+(const F32vec4 &a, const F32vec4 &b) { _op(a, b, +) }
77  friend F32vec4 operator-(const F32vec4 &a, const F32vec4 &b) { _op(a, b, -) }
78  friend F32vec4 operator*(const F32vec4 &a, const F32vec4 &b) { _op(a, b, *) }
79  friend F32vec4 operator/(const F32vec4 &a, const F32vec4 &b) { _op(a, b, /) }
80 
81  /* Comparison */
82  friend F32vec4 operator<(const F32vec4 &a, const F32vec4 &b) { _op(a, b, <) }
83  friend F32vec4 operator<=(const F32vec4 &a, const F32vec4 &b) { _op(a, b, <=) }
84  friend F32vec4 operator>(const F32vec4 &a, const F32vec4 &b) { _op(a, b, >) }
85  friend F32vec4 operator>=(const F32vec4 &a, const F32vec4 &b) { _op(a, b, >=) }
86 
87  /* Logic */
88  friend F32vec4 operator&(const F32vec4 &a, const F32vec4 &b) { _op(a, b, &&) }
89  friend F32vec4 operator|(const F32vec4 &a, const F32vec4 &b) { _op(a, b, ||) }
90  friend F32vec4 operator||(const F32vec4 &a, const F32vec4 &b) { _op(a, b, ||) }
91 
92  friend F32vec4 operator!(const F32vec4 &a)
93  {
94  F32vec4 z;
95  z[0] = !a[0];
96  z[1] = !a[1];
97  z[2] = !a[2];
98  z[3] = !a[3];
99 
100  return z;
101  }
102 
103  friend F32vec4 if3(const F32vec4 &a, const F32vec4 &b, const F32vec4 &c)
104  {
105  F32vec4 z;
106  z[0] = (a[0]) ? b[0] : c[0];
107  z[1] = (a[1]) ? b[1] : c[1];
108  z[2] = (a[2]) ? b[2] : c[2];
109  z[3] = (a[3]) ? b[3] : c[3];
110 
111  return z;
112  }
114 #define NotEmpty(a) bool((a)[0]) | bool((a)[1]) | bool((a)[2]) | bool((a)[3])
115 #define Empty(a) !(bool((a)[0]) | bool((a)[1]) | bool((a)[2]) | bool((a)[3]))
116  // bool NotEmpty(const F32vec4 &a) { return a[0]||a[1]||a[2]||a[3]; }
117  // bool Empty(const F32vec4 &a) { return !(a[0]||a[1]||a[2]||a[3]); } // optimize
118  friend F32vec4 bool2int(const F32vec4 &a)
119  { // mask returned
120  return if3(a, 1, 0);
121  }
123  /* Functions */
124  friend float min(float x, float y) { return x < y ? x : y; }
125  friend float max(float x, float y) { return x < y ? y : x; }
126  friend float asgnb(float x, float y) { return y >= 0 ? fabs(x) : -fabs(x); }
127  friend float rsqrt(float x) { return 1. / sqrt(x); }
128  friend float rcp(float x) { return 1. / x; }
129  friend float sgn(float x) { return x >= 0 ? 1 : -1; }
131  friend F32vec4 min(const F32vec4 &a, const F32vec4 &b) { _f2(a, b, min) }
132  friend F32vec4 max(const F32vec4 &a, const F32vec4 &b) { _f2(a, b, max) }
133  friend F32vec4 asgnb(const F32vec4 &a, const F32vec4 &b) { _f2(a, b, asgnb) }
134  friend F32vec4 sqrt(const F32vec4 &a) { _f1(a, sqrt) }
135  friend F32vec4 rsqrt(const F32vec4 &a) { _f1(a, rsqrt) }
136  friend F32vec4 rcp(const F32vec4 &a) { _f1(a, rcp) }
137  friend F32vec4 fabs(const F32vec4 &a) { _f1(a, fabs) }
138  friend F32vec4 sgn(const F32vec4 &a) { _f1(a, sgn) }
139  friend F32vec4 exp(const F32vec4 &a) { _f1(a, exp) }
140  friend F32vec4 log(const F32vec4 &a) { _f1(a, log) }
141  friend F32vec4 sin(const F32vec4 &a) { _f1(a, sin) }
142  friend F32vec4 cos(const F32vec4 &a) { _f1(a, cos) }
143 #undef _f1
144 #undef _f2
145 #undef _op
146 
147  /* Define all operators for consistensy */
148 
150 
151  friend ostream &operator<<(ostream &strm, const F32vec4 &a)
152  {
153  strm << a[0] << " " << a[1] << " " << a[2] << " " << a[3];
154  return strm;
155  }
156 
157  friend istream &operator>>(istream &strm, F32vec4 &a)
158  {
159  float tmp;
160  strm >> tmp;
161  a = tmp;
162  return strm;
163  }
164 
165 } __attribute__((aligned(16)));
166 ;
167 
168 typedef F32vec4 fvec;
169 typedef float fscal;
170 const int fvecLen = 4;
171 //#define fvec_true _f32vec4_true
172 //#define fvec_false _f32vec4_false
173 #define _fvecalignment
174 
175 namespace nsL1 {
176 template <typename T>
177 struct vector {
178  typedef std::vector<T> TStd;
179  typedef std::vector<T> TSimd;
180 };
181 
183 }; // namespace nsL1
184 
185 template <typename T>
186 struct nsL1vector : public nsL1::vector<T> // just for use std::vector simultaniosly
187 {
188 };
189 
190 #endif
float asgnb(float x, float y)
const int fvecLen
friend F32vec4 operator!(const F32vec4 &a)
friend F32vec4 log(const F32vec4 &a)
Definition: P4_F32vec4.h:154
friend float rcp(float x)
float min(float x, float y)
#define _op(A, B, O)
F32vec4(const float &a)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:62
friend F32vec4 rsqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:68
F32vec4(const float &f0, const float &f1, const float &f2, const float &f3)
nsL1::vector< fvec >::TSimd vector_fvec
#define _f2(A, B, F)
unsigned int i
Definition: P4_F32vec4.h:21
vec_arithmetic(F32vec4, float)
friend F32vec4 operator>(const F32vec4 &a, const F32vec4 &b)
float rcp(float x)
friend F32vec4 operator+(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:55
friend F32vec4 exp(const F32vec4 &a)
Definition: P4_F32vec4.h:153
friend F32vec4 operator||(const F32vec4 &a, const F32vec4 &b)
friend F32vec4 rcp(const F32vec4 &a)
Definition: P4_F32vec4.h:76
std::vector< T > TSimd
friend F32vec4 operator/(const F32vec4 &a, const F32vec4 &b)
F32vec4 fvec
friend F32vec4 asgnb(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:87
friend F32vec4 operator|(const F32vec4 &a, const F32vec4 &b)
friend F32vec4 if3(const F32vec4 &a, const F32vec4 &b, const F32vec4 &c)
friend F32vec4 operator<(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:113
friend F32vec4 bool2int(const F32vec4 &a)
float fscal
friend F32vec4 cos(const F32vec4 &a)
friend F32vec4 operator<=(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:117
float & operator[](int i)
friend F32vec4 operator &(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:91
#define _f1(A, F)
nsL1vector __attribute__
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:83
float sgn(float x)
friend F32vec4 operator-(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:56
float rsqrt(float x)
float max(float x, float y)
friend F32vec4 sgn(const F32vec4 &a)
Definition: P4_F32vec4.h:86
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:61
friend F32vec4 operator>=(const F32vec4 &a, const F32vec4 &b)
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:65
friend ostream & operator<<(ostream &strm, const F32vec4 &a)
friend F32vec4 operator*(const F32vec4 &a, const F32vec4 &b)
friend F32vec4 sin(const F32vec4 &a)
std::vector< T > TStd
friend istream & operator>>(istream &strm, F32vec4 &a)
__m128 v
Definition: P4_F32vec4.h:40
F32vec4(const F32vec4 &a)