Nektar++
scalar.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: scalar.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Scalar type used when a vector type is needed, but no SIMD
32 // extension is available.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_LIBUTILITES_SIMDLIB_SCALAR_H
37 #define NEKTAR_LIB_LIBUTILITES_SIMDLIB_SCALAR_H
38 
39 #include "allocator.hpp"
40 #include "traits.hpp"
41 #include <cmath>
42 #include <cstdint>
43 #include <type_traits>
44 #include <vector>
45 
46 namespace tinysimd
47 {
48 
49 namespace abi
50 {
51 
52 template <typename scalarType> struct scalar
53 {
54  using type = void;
55 };
56 
57 } // namespace abi
58 
59 // forward declaration of concrete types
60 // makes default type available for all arithmetic types
61 template <typename T,
62  typename =
63  typename std::enable_if<std::is_arithmetic<T>::value>::type>
64 struct scalarT;
65 struct scalarMask;
66 
67 namespace abi
68 {
69 
70 // mapping between abstract types and concrete types
71 template <> struct scalar<double>
72 {
74 };
75 template <> struct scalar<float>
76 {
78 };
79 template <> struct scalar<std::int64_t>
80 {
82 };
83 template <> struct scalar<std::uint64_t>
84 {
86 };
87 template <> struct scalar<std::int32_t>
88 {
90 };
91 template <> struct scalar<std::uint32_t>
92 {
94 };
95 template <> struct scalar<bool>
96 {
97  using type = scalarMask;
98 };
99 
100 #ifdef __APPLE__ // for apple size_t is recognised as uint64_t
101 template <> struct scalar<size_t>
102 {
103  using type = scalarT<size_t>;
104 };
105 #endif
106 
107 } // namespace abi
108 
109 // concrete types
110 template <typename T, typename> struct scalarT
111 {
112  static constexpr unsigned int width = 1;
113  static constexpr unsigned int alignment = sizeof(T);
114 
115  using scalarType = T;
118 
119  // storage
121 
122  // ctors
123  inline scalarT() = default;
124  inline scalarT(const scalarT &rhs) = default;
125  inline scalarT(const vectorType &rhs) : _data(rhs)
126  {
127  }
128 
129  // store
130  inline void store(scalarType *p) const
131  {
132  *p = _data;
133  }
134 
135  template <class flag> inline void store(scalarType *p, flag) const
136  {
137  *p = _data;
138  }
139 
140  // load
141  inline void load(const scalarType *p)
142  {
143  _data = *p;
144  }
145 
146  template <class flag> inline void load(const scalarType *p, flag)
147  {
148  _data = *p;
149  }
150 
151  inline void broadcast(const scalarType rhs)
152  {
153  _data = rhs;
154  }
155 
156  template <typename U, typename = typename std::enable_if<
157  std::is_integral<U>::value>::type>
158  inline void gather(const scalarType *p, const scalarT<U> &indices)
159  {
160  _data = *(p + indices._data);
161  }
162 
163  template <typename U, typename = typename std::enable_if<
164  std::is_integral<U>::value>::type>
165  inline void scatter(scalarType *p, const scalarT<U> &indices) const
166  {
167  p += indices._data;
168  *p = _data;
169  }
170 
171  // fma
172  // this = this + a * b
173  inline void fma(const scalarT<T> &a, const scalarT<T> &b)
174  {
175  _data += a._data * b._data;
176  }
177 
178  // subscript
179  inline scalarType operator[](size_t) const
180  {
181  return _data;
182  }
183 
184  inline scalarType &operator[](size_t)
185  {
186  return _data;
187  }
188 
189  // unary ops
190  inline void operator+=(scalarT<T> rhs)
191  {
192  _data += rhs._data;
193  }
194 
195  inline void operator-=(scalarT<T> rhs)
196  {
197  _data -= rhs._data;
198  }
199 
200  inline void operator*=(scalarT<T> rhs)
201  {
202  _data *= rhs._data;
203  }
204 
205  inline void operator/=(scalarT<T> rhs)
206  {
207  _data /= rhs._data;
208  }
209 };
210 
211 template <typename T>
213 {
214  return lhs._data + rhs._data;
215 }
216 template <
217  typename T, typename U,
218  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
219 inline scalarT<T> operator+(U lhs, scalarT<T> rhs)
220 {
221  return lhs + rhs._data;
222 }
223 template <
224  typename T, typename U,
225  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
226 inline scalarT<T> operator+(scalarT<T> lhs, U rhs)
227 {
228  return lhs._data + rhs;
229 }
230 
231 template <typename T>
233 {
234  return lhs._data - rhs._data;
235 }
236 template <
237  typename T, typename U,
238  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
239 inline scalarT<T> operator-(U lhs, scalarT<T> rhs)
240 {
241  return lhs - rhs._data;
242 }
243 template <
244  typename T, typename U,
245  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
246 inline scalarT<T> operator-(scalarT<T> lhs, U rhs)
247 {
248  return lhs._data - rhs;
249 }
250 
251 template <typename T>
253 {
254  return lhs._data * rhs._data;
255 }
256 template <
257  typename T, typename U,
258  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
259 inline scalarT<T> operator*(U lhs, scalarT<T> rhs)
260 {
261  return lhs * rhs._data;
262 }
263 template <
264  typename T, typename U,
265  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
266 inline scalarT<T> operator*(scalarT<T> lhs, U rhs)
267 {
268  return lhs._data * rhs;
269 }
270 
271 template <typename T>
273 {
274  return lhs._data / rhs._data;
275 }
276 template <
277  typename T, typename U,
278  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
279 inline scalarT<T> operator/(U lhs, scalarT<T> rhs)
280 {
281  return lhs / rhs._data;
282 }
283 template <
284  typename T, typename U,
285  typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
286 inline scalarT<T> operator/(scalarT<T> lhs, U rhs)
287 {
288  return lhs._data / rhs;
289 }
290 
291 template <typename T> inline scalarT<T> sqrt(scalarT<T> in)
292 {
293  return std::sqrt(in._data);
294 }
295 template <typename T> inline scalarT<T> abs(scalarT<T> in)
296 {
297  return std::abs(in._data);
298 }
299 
300 template <typename T> inline scalarT<T> log(scalarT<T> in)
301 {
302  return std::log(in._data);
303 }
304 
305 template <typename T>
306 inline void load_interleave(const T *in, size_t dataLen,
307  std::vector<scalarT<T>, allocator<scalarT<T>>> &out)
308 {
309  for (size_t i = 0; i < dataLen; ++i)
310  {
311  out[i] = in[i];
312  }
313 }
314 
315 template <typename T>
316 inline void deinterleave_store(
317  const std::vector<scalarT<T>, allocator<scalarT<T>>> &in, size_t dataLen,
318  T *out)
319 {
320  for (size_t i = 0; i < dataLen; ++i)
321  {
322  out[i] = in[i]._data;
323  }
324 }
325 
326 ////////////////////////////////////////////////////////////////////////////////
327 
328 // mask type
329 // mask is a int type that uses boolean promotion
330 //
331 // VERY LIMITED SUPPORT...just enough to make cubic eos work...
332 //
333 struct scalarMask : public scalarT<std::uint64_t>
334 {
335  // bring in ctors
336  using scalarT::scalarT;
337 
338  static constexpr scalarType true_v = true;
339  static constexpr scalarType false_v = false;
340 
341  // needs to be able to work with std::uint32_t
342  // for single precision overload
343  // usually using 32 or 64 bits would result in a different number of lanes
344  // this is not the case for a scalar
345 
346  // store
347  inline void store(std::uint32_t *p) const
348  {
349  *p = static_cast<std::uint32_t>(_data);
350  }
351 
352  // load
353  inline void load(const std::uint32_t *p)
354  {
355  _data = static_cast<std::uint32_t>(*p);
356  }
357 
358  // make base implementations visible
361 };
362 
364 {
365  return lhs._data > rhs._data;
366 }
367 
369 {
370  return lhs._data > rhs._data;
371 }
372 
373 inline bool operator&&(scalarMask lhs, bool rhs)
374 {
375  return lhs._data && rhs;
376 }
377 
378 } // namespace tinysimd
379 #endif
scalarT< T > log(scalarT< T > in)
Definition: scalar.hpp:300
scalarT< T > operator+(scalarT< T > lhs, scalarT< T > rhs)
Definition: scalar.hpp:212
void deinterleave_store(const std::vector< scalarT< T >, allocator< scalarT< T >>> &in, size_t dataLen, T *out)
Definition: scalar.hpp:316
scalarT< T > operator-(scalarT< T > lhs, scalarT< T > rhs)
Definition: scalar.hpp:232
scalarT< T > abs(scalarT< T > in)
Definition: scalar.hpp:295
boost::alignment::aligned_allocator< T, T::alignment > allocator
Definition: allocator.hpp:49
scalarMask operator>(scalarT< double > lhs, scalarT< double > rhs)
Definition: scalar.hpp:363
void load_interleave(const T *in, size_t dataLen, std::vector< scalarT< T >, allocator< scalarT< T >>> &out)
Definition: scalar.hpp:306
bool operator&&(scalarMask lhs, bool rhs)
Definition: scalar.hpp:373
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:291
scalarT< T > operator/(scalarT< T > lhs, scalarT< T > rhs)
Definition: scalar.hpp:272
scalarT< T > operator*(scalarT< T > lhs, scalarT< T > rhs)
Definition: scalar.hpp:252
static constexpr scalarType false_v
Definition: scalar.hpp:339
void load(const std::uint32_t *p)
Definition: scalar.hpp:353
void store(std::uint32_t *p) const
Definition: scalar.hpp:347
static constexpr scalarType true_v
Definition: scalar.hpp:338
void store(scalarType *p) const
Definition: scalar.hpp:130
scalarT(const scalarT &rhs)=default
void gather(const scalarType *p, const scalarT< U > &indices)
Definition: scalar.hpp:158
vectorType _data
Definition: scalar.hpp:120
static constexpr unsigned int alignment
Definition: scalar.hpp:113
void broadcast(const scalarType rhs)
Definition: scalar.hpp:151
scalarType & operator[](size_t)
Definition: scalar.hpp:184
scalarType operator[](size_t) const
Definition: scalar.hpp:179
void operator/=(scalarT< T > rhs)
Definition: scalar.hpp:205
scalarType[width] scalarArray
Definition: scalar.hpp:117
void load(const scalarType *p, flag)
Definition: scalar.hpp:146
void operator+=(scalarT< T > rhs)
Definition: scalar.hpp:190
scalarT(const vectorType &rhs)
Definition: scalar.hpp:125
static constexpr unsigned int width
Definition: scalar.hpp:112
scalarType vectorType
Definition: scalar.hpp:116
void fma(const scalarT< T > &a, const scalarT< T > &b)
Definition: scalar.hpp:173
void operator-=(scalarT< T > rhs)
Definition: scalar.hpp:195
void load(const scalarType *p)
Definition: scalar.hpp:141
void operator*=(scalarT< T > rhs)
Definition: scalar.hpp:200
void store(scalarType *p, flag) const
Definition: scalar.hpp:135
void scatter(scalarType *p, const scalarT< U > &indices) const
Definition: scalar.hpp:165