aboutsummaryrefslogtreecommitdiff
path: root/src/indirect/vectors-default.hh
blob: 9a1e1d3e028c4ad49171f5511cbe7fa1fa41ba0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using namespace std;



// A class template that provides a vectorised type for the underlying
// scalar type T. This implementation does "nothing", i.e. just
// provides a "vector" class with a vector size of 1, forwarding all
// operations to the scalar type.
//
// This implementation uses small integers in several places, e.g. in
// the [] operator. This is efficient only if these integers are
// compile time constants, so that the compiler can remove the
// corresponding if and switch statements.
template<typename T>
struct vec_t {
  
  // Names for the underlying scalar type, and for the vector type
  // used to implement this class. For example, with SSE2, it would be
  // scalar_t=double, and impl_t=__m128d.
  typedef T scalar_t;
  typedef T impl_t;
  
  // The payload -- the actual vector content
  impl_t v;
  
  // Vector size (number of elements)
  static inline size_t size()
  {
    return sizeof(impl_t)/sizeof(scalar_t);
  }
  
  // Constructors
  inline vec_t ()
  {
  }
  inline vec_t (scalar_t const& a)
    : v(a)
  {
  }
  
  // Convert to the implementation vector type
  inline operator impl_t ()
  {
    return v;
  }
  
  // Access individual vector elements
  inline scalar_t operator[] (size_t const d) const
  {
    return v;
  }
  
  // Load vectors from memory. For convenience when using this class,
  // these accept references to the scalar type instead of pointers to
  // the vector type. These routines are static members of the class,
  // so that they can be used as VEC::load(p); if they were
  // stand-alone functions, one would have to write load<SCALAR>(p)
  // instead.
  //
  // Aligned load
  static inline vec_t load (scalar_t const& p)
  {
    return p;
  }
  // Unaligned load
  static inline vec_t loadu (scalar_t const& p)
  {
    return p;
  }
  // Load a vector from memory that may or may not be aligned, as
  // decided by the offset and the vector size. These functions are
  // useful e.g. for loading neightbouring grid points while
  // evaluating finite differencing stencils.
  static inline vec_t loadu_maybe (int const off, scalar_t const& p)
  {
    return p;
  }
  static inline vec_t loadu_maybe3 (int const off0, int const off1,
                                    int const off2,
                                    scalar_t const& p)
  {
    return p;
  }
};

// Store vectors to memory. These routines are stand-alone functions,
// so that they can be used as vec_store(p,x); if they were class
// members, one would have to write x.store(p) instead, or possibly
// VEC::store(p,x).
//
// Aligned store
template<typename T>
inline void vec_store (typename vec_t<T>::scalar_t& p, vec_t<T> const& x)
{
  p=x.v;
}
// Unaligned store
template<typename T>
inline void vec_storeu (typename vec_t<T>::scalar_t& p, vec_t<T> const& x)
{
  p=x.v;
}
// Non-temporal store, i.e. a store that bypasses the cache
template<typename T>
inline void vec_store_nta (typename vec_t<T>::scalar_t& p, vec_t<T> const& x)
{
  p=x.v;
}
// Store the cnt lower elements of a vector, bypassing the cache if
// possible
template<typename T>
inline void vec_store_nta_partial_lo (typename vec_t<T>::scalar_t& p,
                                      vec_t<T> const& x,
                                      size_t const cnt)
{
  assert(0);
}
// Store the cnt higher elements of a vector, bypassing the cache if
// possible. This stores the vector elements into memory locations as
// if element 0 were stored at p.
template<typename T>
inline void vec_store_nta_partial_hi (typename vec_t<T>::scalar_t& p,
                                      vec_t<T> const& x,
                                      size_t const cnt)
{
  assert(0);
}

template<typename T>
inline vec_t<T> operator+ (vec_t<T> const& x)
{
  return +x.v;
}
template<typename T>
inline vec_t<T> operator- (vec_t<T> const& x)
{
  return -x.v;
}

template<typename T>
inline vec_t<T> operator+ (vec_t<T> const& x, vec_t<T> const& y)
{
  return x.v + y.v;
}
template<typename T>
inline vec_t<T> operator- (vec_t<T> const& x, vec_t<T> const& y)
{
  return x.v - y.v;
}
template<typename T>
inline vec_t<T> operator* (vec_t<T> const& x, vec_t<T> const& y)
{
  return x.v * y.v;
}
template<typename T>
inline vec_t<T> operator/ (vec_t<T> const& x, vec_t<T> const& y)
{
  return x.v / y.v;
}

template<typename T>
inline vec_t<T>& operator+= (vec_t<T>& x, vec_t<T> const& y)
{
  x.v += y.v;
  return x;
}
template<typename T>
inline vec_t<T>& operator-= (vec_t<T>& x, vec_t<T> const& y)
{
  x.v -= y.v;
  return x;
}
template<typename T>
inline vec_t<T>& operator*= (vec_t<T>& x, vec_t<T> const& y)
{
  x.v *= y.v;
  return x;
}
template<typename T>
inline vec_t<T>& operator/= (vec_t<T>& x, vec_t<T> const& y)
{
  x.v /= y.v;
  return x;
}

template<typename T>
inline vec_t<T> exp (vec_t<T> const& x)
{
  return exp(x.v);
}
template<typename T>
inline vec_t<T> fabs (vec_t<T> const& x)
{
  return fabs(x.v);
}
template<typename T>
inline vec_t<T> log (vec_t<T> const& x)
{
  return log(x.v);
}
template<typename T>
inline vec_t<T> sqrt (vec_t<T> const& x)
{
  return sqrt(x.v);
}

template<typename T>
inline vec_t<T> fmax (vec_t<T> const& x, vec_t<T> const& y)
{
  return fmax(x.v, y.v);
}
template<typename T>
inline vec_t<T> fmin (vec_t<T> const& x, vec_t<T> const& y)
{
  return fmin(x.v, y.v);
}
template<typename T>
inline vec_t<T> pow (vec_t<T> const& x, typename vec_t<T>::scalar_t const& a)
{
  return pow(x.v, a);
}
template<typename T>
inline vec_t<T> pow (vec_t<T> const& x, int const& i)
{
  return pow(x.v, i);
}