+/**
+ * An iterator for VectorizedArray.
+ *
+ * @author Peter Munch, 2019
+ */
+template <typename T>
+class VectorizedArrayIterator
+{
+public:
+ /**
+ * Constructor.
+ *
+ * @param data The actual VectorizedArray.
+ * @param lane A pointer to the current lane.
+ */
+ VectorizedArrayIterator(T &data, unsigned int lane)
+ : data(data)
+ , lane(lane)
+ {}
+
+ /**
+ * Compare for inequality.
+ */
+ bool
+ operator!=(const VectorizedArrayIterator<T> &other) const
+ {
+ return this->lane != other.lane;
+ }
+
+ /**
+ * Dereferencing operator (const version): returns the value of the current
+ * lane.
+ */
+ const typename T::value_type &operator*() const
+ {
+ return data[lane];
+ }
+
+
+ /**
+ * Dereferencing operator (non-@p const version): returns the value of the
+ * current lane.
+ */
+ template <typename U = T>
+ typename std::enable_if<!std::is_same<U, const U>::value,
+ typename T::value_type>::type &
+ operator*()
+ {
+ return data[lane];
+ }
+
+ /**
+ * Prefix <tt>++</tt> operator: <tt>++iterator</tt>. This operator advances
+ * the iterator to the next lane and returns a reference to
+ * <tt>*this</tt>.
+ */
+ VectorizedArrayIterator<T> &
+ operator++()
+ {
+ lane++;
+ return *this;
+ }
+
+private:
+ /**
+ * Reference to the actual VectorizedArray.
+ */
+ T &data;
+
+ /**
+ * Pointer to the current lane.
+ */
+ unsigned int lane;
+};
+
+
+
+/**
+ * A base class for the VectorizedArray specialization, containing common
+ * functionalities.
+ *
+ * @tparam Type of the actual vectorized array. We are using CRTP to prevent
+ * the usage of the virtual keyword.
+ *
+ * @author Peter Munch, 2019
+ */
+template <typename T>
+class VectorizedArrayBase
+{
+public:
+ /**
+ * Return the number of elements in the array stored in the variable
+ * n_array_elements of VectorizedArray.
+ */
+ static constexpr unsigned int
+ size()
+ {
+ return T::n_array_elements;
+ }
+
+ /**
+ * @return An iterator pointing to the beginning of the underlying data.
+ */
+ VectorizedArrayIterator<T>
+ begin()
+ {
+ return VectorizedArrayIterator<T>(static_cast<T &>(*this), 0);
+ }
+
+ /**
+ * @return An iterator pointing to the end of the underlying data.
+ */
+ VectorizedArrayIterator<T>
+ end()
+ {
+ return VectorizedArrayIterator<T>(static_cast<T &>(*this),
+ T::n_array_elements);
+ }
+
+ /**
+ * @return An iterator pointing to the beginning of the underlying data (const
+ * version).
+ */
+ VectorizedArrayIterator<const T>
+ begin() const
+ {
+ return VectorizedArrayIterator<const T>(static_cast<const T &>(*this), 0);
+ }
+
+ /**
+ * @return An iterator pointing to the end of the underlying data (const
+ * version).
+ */
+ VectorizedArrayIterator<const T>
+ end() const
+ {
+ return VectorizedArrayIterator<const T>(static_cast<const T &>(*this),
+ T::n_array_elements);
+ }
+};
+
+
+
/**
* This generic class defines a unified interface to a vectorized data type.
* For general template arguments, this class simply corresponds to the
*/
template <typename Number, int width>
class VectorizedArray
+ : public VectorizedArrayBase<VectorizedArray<Number, width>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator (only valid with component 0 in the base class without
* specialization).
*/
template <>
class VectorizedArray<double, 8>
+ : public VectorizedArrayBase<VectorizedArray<double, 8>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator.
*/
*/
template <>
class VectorizedArray<float, 16>
+ : public VectorizedArrayBase<VectorizedArray<float, 16>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator.
*/
*/
template <>
class VectorizedArray<double, 4>
+ : public VectorizedArrayBase<VectorizedArray<double, 4>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator.
*/
*/
template <>
class VectorizedArray<float, 8>
+ : public VectorizedArrayBase<VectorizedArray<float, 8>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator.
*/
*/
template <>
class VectorizedArray<double, 2>
+ : public VectorizedArrayBase<VectorizedArray<double, 2>>
{
public:
/**
this->operator=(scalar);
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* This function can be used to set all data fields to a given scalar.
*/
*/
template <>
class VectorizedArray<float, 4>
+ : public VectorizedArrayBase<VectorizedArray<float, 4>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator.
*/
template <>
class VectorizedArray<double, 2>
+ : public VectorizedArrayBase<VectorizedArray<double, 2>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator. The component must be either 0 or 1.
*/
template <>
class VectorizedArray<float, 4>
+ : public VectorizedArrayBase<VectorizedArray<float, 4>>
{
public:
/**
return *this;
}
- /**
- * Return the number of elements in the array stored in the variable
- * n_array_elements.
- */
- static constexpr unsigned int
- size()
- {
- return n_array_elements;
- }
-
/**
* Access operator. The component must be between 0 and 3.
*/
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2019 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+// test for arithmetic operations on VectorizedArray
+
+#include <deal.II/base/vectorization.h>
+
+#include "../tests.h"
+
+
+template <typename Number>
+void
+test_const(const VectorizedArray<Number> &vector)
+{
+ unsigned int counter = 0;
+ for (const auto v : vector)
+ {
+ AssertThrow(v == vector[counter++], ExcInternalError());
+ }
+}
+
+template <typename Number>
+void
+test_nonconst(VectorizedArray<Number> &vector)
+{
+ unsigned int counter = 0;
+ for (auto v : vector)
+ {
+ AssertThrow(v == vector[counter++], ExcInternalError());
+ }
+}
+
+template <typename Number>
+void
+test()
+{
+ VectorizedArray<Number> vector;
+
+ for (unsigned int v = 0; v < VectorizedArray<Number>::size(); v++)
+ vector[v] = v;
+
+ test_const(vector);
+ test_nonconst(vector);
+}
+
+
+int
+main()
+{
+ initlog();
+
+ deallog.push("float");
+ test<float>();
+ deallog << "OK" << std::endl;
+ deallog.pop();
+
+ deallog.push("double");
+ test<double>();
+ deallog << "OK" << std::endl;
+ deallog.pop();
+}