Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatrix4x1.h
Go to the documentation of this file.
1 #include <align_16.h>
2 
3 #ifndef USE_SSE2
4 
5 // Matrix class without SIMD instructions
6 class DMatrix4x1{
7 public:
9  for (unsigned int i=0;i<4;i++) mA[i]=0.;
10  }
11  DMatrix4x1(double a1, double a2, double a3, double a4){
12  mA[0]=a1;
13  mA[1]=a2;
14  mA[2]=a3;
15  mA[3]=a4;
16  }
18 
19  // Access by row number
20  double &operator() (int row){
21  return mA[row];
22  }
23  double operator() (int row) const{
24  return mA[row];
25  }
26  // Copy constructor
27  DMatrix4x1(const DMatrix4x1 &m2){
28  for (unsigned int i=0;i<4;i++){
29  mA[i]=m2(i);
30  }
31  }
32  // Assignment operator
34  for (unsigned int i=0;i<4;i++){
35  mA[i]=m2(i);
36  }
37  return *this;
38  }
39  // Matrix addition
40  DMatrix4x1 operator+(const DMatrix4x1 &m2) const{
41  return DMatrix4x1(mA[0]+m2(0),mA[1]+m2(1),mA[2]+m2(2),mA[3]+m2(3));
42 
43  }
45  for (unsigned int i=0;i<4;i++){
46  mA[i]+=m2(i);
47  }
48  return *this;
49  }
50 
51  // Matrix subtraction
52  DMatrix4x1 operator-(const DMatrix4x1 &m2) const{
53  return DMatrix4x1(mA[0]-m2(0),mA[1]-m2(1),mA[2]-m2(2),mA[3]-m2(3));
54 
55  }
56 
57  bool IsFinite(){
58  return (isfinite(mA[0]) && isfinite(mA[1]) && isfinite(mA[2]) && isfinite(mA[3]));
59  }
60 
61  void Print(){
62  cout << "DMatrix4x1:" <<endl;
63  cout << " | 0 |" <<endl;
64  cout << "----------------------" <<endl;
65  for (unsigned int i=0;i<4;i++){
66  cout <<" "<<i<<" |" << setw(11)<<setprecision(4) << mA[i] << endl;
67  }
68  }
69 
70 private:
71  double mA[4];
72 };
73 
74 // Scale 4x1 matrix by a floating point number
75 inline DMatrix4x1 operator*(const double c,const DMatrix4x1 &M){
76  return DMatrix4x1(c*M(0),c*M(1),c*M(2),c*M(3));
77 }
78 
79 #else
80 
81 // Matrix class with SIMD instructions
82 
83 class DMatrix4x1{
84  public:
85  DMatrix4x1()
86  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 1, mA) )
87  {
88  for (unsigned int j=0;j<2;j++){
89  mA->v[j]=_mm_setzero_pd();
90  }
91  }
92  DMatrix4x1(__m128d v1, __m128d v2)
93  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 1, mA) )
94  {
95  mA->v[0]=v1;
96  mA->v[1]=v2;
97  }
98  DMatrix4x1(double a1, double a2, double a3, double a4)
99  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 1, mA) )
100  {
101  mA->d[0]=a1;
102  mA->d[1]=a2;
103  mA->d[2]=a3;
104  mA->d[3]=a4;
105  }
106  ~DMatrix4x1(){};
107 
108  __m128d GetV(int pair) const{
109  return mA->v[pair];
110  }
111 
112  // Copy constructor
113  DMatrix4x1(const DMatrix4x1 &m2)
114  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 1, mA) )
115  {
116  mA->v[0]=m2.GetV(0);
117  mA->v[1]=m2.GetV(1);
118  }
119  // Access by row number
120  double &operator() (int row){
121  return mA->d[row];
122  }
123  double operator() (int row) const{
124  return mA->d[row];
125  }
126  // Assignment operator
127  DMatrix4x1 &operator=(const DMatrix4x1 &m2){
128  mA->v[0]=m2.GetV(0);
129  mA->v[1]=m2.GetV(1);
130  return *this;
131  }
132 
133  // Matrix addition
134  DMatrix4x1 operator+(const DMatrix4x1 &m2) const{
135  return DMatrix4x1(_mm_add_pd(GetV(0),m2.GetV(0)),
136  _mm_add_pd(GetV(1),m2.GetV(1)));
137  }
138  DMatrix4x1 &operator+=(const DMatrix4x1 &m2){
139  mA->v[0]=_mm_add_pd(GetV(0),m2.GetV(0));
140  mA->v[1]=_mm_add_pd(GetV(1),m2.GetV(1));
141  return *this;
142  }
143 
144  // Matrix subtraction
145  DMatrix4x1 operator-(const DMatrix4x1 &m2) const{
146  return DMatrix4x1(_mm_sub_pd(GetV(0),m2.GetV(0)),
147  _mm_sub_pd(GetV(1),m2.GetV(1)));
148  }
149 
150  void Print(){
151  cout << "DMatrix4x1:" <<endl;
152  cout << " | 0 |" <<endl;
153  cout << "----------------------" <<endl;
154  for (unsigned int i=0;i<4;i++){
155  cout <<" "<<i<<" |" << setw(11)<<setprecision(4) << mA->d[i] << endl;
156  }
157  }
158 
159  private:
160  union dvec{
161  __m128d v[2];
162  double d[4];
163  };
164  ALIGNED_16_BLOCK(union dvec, 1, mA)
165 
166 };
167 
168 // Scale 4x1 matrix by a floating point number
169 inline DMatrix4x1 operator*(const double c,const DMatrix4x1 &M){
170  ALIGNED_16_BLOCK_WITH_PTR(__m128d, 1, p)
171  __m128d &scale=p[0];
172  scale=_mm_set1_pd(c);
173  return DMatrix4x1(_mm_mul_pd(scale,M.GetV(0)),
174  _mm_mul_pd(scale,M.GetV(1)));
175 }
176 
177 #endif
DMatrix2x1 operator*(const double c, const DMatrix2x1 &M)
Definition: DMatrix2x1.h:58
#define ALIGNED_16_BLOCK_PTR(TYPE, NUM, PTR)
Definition: align_16.h:24
double mA[4]
Definition: DMatrix4x1.h:71
#define c
DMatrix4x1 & operator=(const DMatrix4x1 &m2)
Definition: DMatrix4x1.h:33
#define ALIGNED_16_BLOCK_WITH_PTR(TYPE, NUM, PTR)
Definition: align_16.h:27
double & operator()(int row)
Definition: DMatrix4x1.h:20
DMatrix4x1 operator+(const DMatrix4x1 &m2) const
Definition: DMatrix4x1.h:40
bool IsFinite()
Definition: DMatrix4x1.h:57
DMatrix4x1(const DMatrix4x1 &m2)
Definition: DMatrix4x1.h:27
DMatrix4x1(double a1, double a2, double a3, double a4)
Definition: DMatrix4x1.h:11
DMatrix4x1 operator-(const DMatrix4x1 &m2) const
Definition: DMatrix4x1.h:52
void Print()
Definition: DMatrix4x1.h:61
DMatrix4x1()
Definition: DMatrix4x1.h:8
DMatrix4x1 & operator+=(const DMatrix4x1 &m2)
Definition: DMatrix4x1.h:44
#define ALIGNED_16_BLOCK(TYPE, NUM, PTR)
Definition: align_16.h:19