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