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