LIBINT  2.1.0-stable
vector.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License, version 2,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program. If not, see http://www.gnu.org/licenses/.
16  *
17  */
18 
19 #ifndef _libint2_src_lib_libint_vector_h_
20 #define _libint2_src_lib_libint_vector_h_
21 
22 #include <unistd.h>
23 
24 #if defined(__cplusplus)
25 
26 #include <algorithm>
27 
28 #include <libint2/type_traits.h>
29 
30 namespace libint2 {
31 
35  namespace simd {
36 
37  // add __declspec(align(N*sizeof(T))) ?
38 
43  template <size_t N, typename T>
44  struct Vector {
45 
46  T d[N];
47 
51  Vector() {}
52 
56  Vector(T a) {
57  std::fill_n(&(d[0]), N, a);
58  }
59 
63  Vector(T (&a)[N]) {
64  std::copy(&a[0], &a[0]+N, &d[0]);
65  }
66 
67  Vector& operator=(T a) {
68  for(size_t i=0; i<N; ++i)
69  d[i] = a;
70  return *this;
71  }
72 
73  Vector& operator+=(Vector a) {
74  for(size_t i=0; i<N; ++i)
75  d[i] += a.d[i];
76  return *this;
77  }
78 
79  Vector& operator-=(Vector a) {
80  for(size_t i=0; i<N; ++i)
81  d[i] -= a.d[i];
82  return *this;
83  }
84 
85  operator double() const {
86  return d[0];
87  }
88 
89  };
90 
92  template <size_t N, typename T>
94  Vector<N,T> c;
95  for(size_t i=0; i<N; ++i)
96  c.d[i] = a * b.d[i];
97  return c;
98  }
99 
100  template <size_t N, typename T>
102  Vector<N,T> c;
103  for(size_t i=0; i<N; ++i)
104  c.d[i] = b * a.d[i];
105  return c;
106  }
107 
108  template <size_t N, typename T>
109  Vector<N,T> operator*(int a, Vector<N,T> b) {
110  if (a == 1)
111  return b;
112  else {
113  Vector<N, T> c;
114  for (size_t i = 0; i < N; ++i)
115  c.d[i] = a * b.d[i];
116  return c;
117  }
118  }
119 
120  template <size_t N, typename T>
121  Vector<N,T> operator*(Vector<N,T> a, int b) {
122  if (b == 1)
123  return a;
124  else {
125  Vector<N, T> c;
126  for (size_t i = 0; i < N; ++i)
127  c.d[i] = b * a.d[i];
128  return c;
129  }
130  }
131 
132  template <size_t N, typename T>
134  Vector<N,T> c;
135  for(size_t i=0; i<N; ++i)
136  c.d[i] = a.d[i] * b.d[i];
137  return c;
138  }
139 
140  template <size_t N, typename T>
141  Vector<N,T> operator+(Vector<N,T> a, Vector<N,T> b) {
142  Vector<N,T> c;
143  for(size_t i=0; i<N; ++i)
144  c.d[i] = a.d[i] + b.d[i];
145  return c;
146  }
147 
148  template <size_t N, typename T>
149  Vector<N,T> operator-(Vector<N,T> a, Vector<N,T> b) {
150  Vector<N,T> c;
151  for(size_t i=0; i<N; ++i)
152  c.d[i] = a.d[i] - b.d[i];
153  return c;
154  }
155 
156  template <size_t N, typename T>
157  Vector<N,T> operator/(Vector<N,T> a, Vector<N,T> b) {
158  Vector<N,T> c;
159  for(size_t i=0; i<N; ++i)
160  c.d[i] = a.d[i] / b.d[i];
161  return c;
162  }
163 
164 
166 
167 };}; // namespace libint2::simd
168 
169 namespace libint2 {
170 
171  template <size_t N, typename T>
172  struct is_vector<simd::Vector<N,T> > {
173  static const bool value = true;
174  };
175 
176  template <size_t N, typename T>
177  struct vector_traits<simd::Vector<N,T> > {
178  typedef T value_type;
179  static const size_t extent = N;
180  };
181 
182 } // namespace libint2
183 
184 #include "vector_x86.h"
185 #include "vector_ppc.h"
186 
187 #endif // C++ only
188 
189 #endif
Vector(T a)
Initializes all elements to the same value.
Definition: vector.h:56
Definition: type_traits.h:30
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
SafePtr< CTimeEntity< typename ProductType< T, U >::result > > operator*(const SafePtr< CTimeEntity< T > > &A, const SafePtr< CTimeEntity< U > > &B)
Creates product A*B.
Definition: entity.h:277
Vector(T(&a)[N])
creates a vector of values initialized by an ordinary static-sized array
Definition: vector.h:63
Vector<N,T> is used by vectorized Libint library as fixed-length vectors amenable for SIMD-style para...
Definition: vector.h:44
Definition: type_traits.h:25
Vector()
creates a vector of default-initialized values.
Definition: vector.h:51