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