LIBINT 2.9.0
vector.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint library.
5 *
6 * Libint library 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 library 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 library. 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#if defined(__cplusplus)
25
26#include <libint2/util/type_traits.h>
27
28#include <algorithm>
29#include <cstddef>
30
31namespace libint2 {
32
37namespace simd {
38
39// add __declspec(align(N*sizeof(T))) ?
40
46template <std::size_t N, typename T>
47struct Vector {
48 T d[N];
49
53 Vector() {}
54
58 Vector(T a) { std::fill_n(&(d[0]), N, a); }
59
63 Vector(T (&a)[N]) { std::copy(&a[0], &a[0] + N, &d[0]); }
64
65 Vector& operator=(T a) {
66 for (std::size_t i = 0; i < N; ++i) d[i] = a;
67 return *this;
68 }
69
70 Vector& operator+=(Vector a) {
71 for (std::size_t i = 0; i < N; ++i) d[i] += a.d[i];
72 return *this;
73 }
74
75 Vector& operator-=(Vector a) {
76 for (std::size_t i = 0; i < N; ++i) d[i] -= a.d[i];
77 return *this;
78 }
79
80 operator double() const { return d[0]; }
81};
82
84template <std::size_t N, typename T>
85Vector<N, T> operator*(T a, Vector<N, T> b) {
86 Vector<N, T> c;
87 for (std::size_t i = 0; i < N; ++i) c.d[i] = a * b.d[i];
88 return c;
89}
90
91template <std::size_t N, typename T>
92Vector<N, T> operator*(Vector<N, T> a, T b) {
93 Vector<N, T> c;
94 for (std::size_t i = 0; i < N; ++i) c.d[i] = b * a.d[i];
95 return c;
96}
97
98template <std::size_t N, typename T>
99Vector<N, T> operator*(int a, Vector<N, T> b) {
100 if (a == 1)
101 return b;
102 else {
103 Vector<N, T> c;
104 for (std::size_t i = 0; i < N; ++i) c.d[i] = a * b.d[i];
105 return c;
106 }
107}
108
109template <std::size_t N, typename T>
110Vector<N, T> operator*(Vector<N, T> a, int b) {
111 if (b == 1)
112 return a;
113 else {
114 Vector<N, T> c;
115 for (std::size_t i = 0; i < N; ++i) c.d[i] = b * a.d[i];
116 return c;
117 }
118}
119
120template <std::size_t N, typename T>
121Vector<N, T> operator*(Vector<N, T> a, Vector<N, T> b) {
122 Vector<N, T> c;
123 for (std::size_t i = 0; i < N; ++i) c.d[i] = a.d[i] * b.d[i];
124 return c;
125}
126
127template <std::size_t N, typename T>
128Vector<N, T> operator+(Vector<N, T> a, Vector<N, T> b) {
129 Vector<N, T> c;
130 for (std::size_t i = 0; i < N; ++i) c.d[i] = a.d[i] + b.d[i];
131 return c;
132}
133
134template <std::size_t N, typename T>
135Vector<N, T> operator-(Vector<N, T> a, Vector<N, T> b) {
136 Vector<N, T> c;
137 for (std::size_t i = 0; i < N; ++i) c.d[i] = a.d[i] - b.d[i];
138 return c;
139}
140
141template <std::size_t N, typename T>
142Vector<N, T> operator/(Vector<N, T> a, Vector<N, T> b) {
143 Vector<N, T> c;
144 for (std::size_t i = 0; i < N; ++i) c.d[i] = a.d[i] / b.d[i];
145 return c;
146}
147
149
150}; // namespace simd
151}; // namespace libint2
152
153namespace libint2 {
154
155template <std::size_t N, typename T>
156struct is_vector<simd::Vector<N, T> > {
157 static const bool value = true;
158};
159
160template <std::size_t N, typename T>
161struct vector_traits<simd::Vector<N, T> > {
162 typedef T value_type;
163 static const std::size_t extent = N;
164};
165
166} // namespace libint2
167
168#include "vector_ppc.h"
169#include "vector_x86.h"
170
171#endif // C++ only
172
173#endif
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
std::shared_ptr< CTimeEntity< typename ProductType< T, U >::result > > operator*(const std::shared_ptr< CTimeEntity< T > > &A, const std::shared_ptr< CTimeEntity< U > > &B)
Creates product A*B.
Definition entity.h:302
Definition type_traits.h:29
Vector<N,T> is used by vectorized Libint library as fixed-length vectors amenable for SIMD-style para...
Definition vector.h:47
Vector()
creates a vector of default-initialized values.
Definition vector.h:53
Vector(T a)
Initializes all elements to the same value.
Definition vector.h:58
Vector(T(&a)[N])
creates a vector of values initialized by an ordinary static-sized array
Definition vector.h:63
Definition type_traits.h:34