Skip to content

Commit

Permalink
Array_of_vectors optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleš Hrabalík committed Jan 22, 2016
1 parent 68f3262 commit 29bb5cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
45 changes: 18 additions & 27 deletions include/simdify/impl/array_of_vectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace simd {
static_assert(std::is_trivial<e_t>::value, "array_of_vectors_impl: e_t not trivial");

using value_type = named_array<e_t, Ids...>;
using value_type_vector = named_array<simd_t, Ids...>;
using value_type_vector = named_array<simd::storage<simd_t>, Ids...>;
using reference = named_array<simd::reference<simd::storage<e_t>>, Ids...>;
using const_reference = named_array<simd::const_reference<simd::storage<e_t>>, Ids...>;
using reference_vector = value_type_vector&;
Expand Down Expand Up @@ -112,29 +112,6 @@ namespace simd {

SIMDIFY_CONTAINERS_COMMON_POP_BACK("structure_of_arrays");

template <typename Val>
struct vector_iterator : std::iterator<std::random_access_iterator_tag, Val> {
vector_iterator(const self_t& self, std::size_t idx) :
m_ptr(self.data_as_value_vector_type_ptr() + (idx / W)) {}

vector_iterator& operator=(const vector_iterator& rhs) = default;

vector_iterator& operator++() { ++m_ptr; return *this; }
vector_iterator& operator--() { --m_ptr; return *this; }
vector_iterator& operator+=(std::ptrdiff_t add) { m_ptr += add; return *this; }
std::ptrdiff_t operator-(const vector_iterator& rhs) { return m_ptr - rhs.m_ptr; }
bool operator<(const vector_iterator& rhs) const { return m_ptr < rhs.m_ptr; }
bool operator<=(const vector_iterator& rhs) const { return m_ptr <= rhs.m_ptr; }
bool operator==(const vector_iterator& rhs) const { return m_ptr == rhs.m_ptr; }
Val& operator*() { return *m_ptr; }
Val* operator->() { return m_ptr; }

SIMDIFY_CONTAINERS_COMMON_ITERATOR_FACILITIES(vector_iterator);

private:
Val* m_ptr;
};

template <typename Val, typename Ref>
struct scalar_iterator : std::iterator<std::random_access_iterator_tag, Val, std::ptrdiff_t, scalar_iterator<Val, Ref>, Ref> {
scalar_iterator(const self_t& self, std::size_t idx) {
Expand Down Expand Up @@ -194,11 +171,25 @@ namespace simd {

using iterator = scalar_iterator<value_type, reference>;
using const_iterator = scalar_iterator<const value_type, const_reference>;
using iterator_vector = vector_iterator<value_type_vector>;
using const_iterator_vector = vector_iterator<const value_type_vector>;
using iterator_vector = value_type_vector*;
using const_iterator_vector = const value_type_vector*;

SIMDIFY_INL iterator_vector begin_overspan() { return data_as_value_vector_type_ptr(); }
SIMDIFY_INL iterator_vector end_overspan() { return data_as_value_vector_type_ptr() + (size_overspan() / W); }
SIMDIFY_INL iterator_vector begin_body() { return data_as_value_vector_type_ptr(); }
SIMDIFY_INL iterator_vector end_body() { return data_as_value_vector_type_ptr() + (size_body() / W); }

SIMDIFY_INL const_iterator_vector begin_overspan() const { return cbegin_overspan(); }
SIMDIFY_INL const_iterator_vector end_overspan() const { return cend_overspan(); }
SIMDIFY_INL const_iterator_vector begin_body() const { return cbegin_body(); }
SIMDIFY_INL const_iterator_vector end_body() const { return cend_body(); }

SIMDIFY_INL const_iterator_vector cbegin_overspan() const { return data_as_value_vector_type_ptr(); }
SIMDIFY_INL const_iterator_vector cend_overspan() const { return data_as_value_vector_type_ptr() + (size_overspan() / W); }
SIMDIFY_INL const_iterator_vector cbegin_body() const { return data_as_value_vector_type_ptr(); }
SIMDIFY_INL const_iterator_vector cend_body() const { return data_as_value_vector_type_ptr() + (size_body() / W); }

SIMDIFY_CONTAINERS_COMMON_ITERATION_SCALAR;
SIMDIFY_CONTAINERS_COMMON_ITERATION_VECTOR;

private:
value_type_vector* data_as_value_vector_type_ptr() const {
Expand Down
11 changes: 10 additions & 1 deletion include/simdify/impl/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ namespace simd {
template <typename E_t>
struct storage<E_t, typename std::enable_if<std::is_arithmetic<E_t>::value>::type> {
using stored_t = E_t;
using e_t = E_t;
using data_t = E_t;

SIMDIFY_INL constexpr storage() = default;
SIMDIFY_INL constexpr storage(const storage&) = default;
Expand All @@ -122,12 +124,14 @@ namespace simd {

SIMDIFY_INL E_t* data() { return &m_data; }
SIMDIFY_INL const E_t* data() const { return &m_data; }
SIMDIFY_INL e_t& operator[](std::size_t i) { return m_data; }
SIMDIFY_INL const e_t& operator[](std::size_t i) const { return m_data; }

// implicit conversion to E_t
SIMDIFY_INL operator E_t() const { return m_data; }

// data
E_t m_data;
data_t m_data;
};

//
Expand Down Expand Up @@ -190,6 +194,7 @@ namespace simd {
template <typename Storage>
struct reference {
using referred_t = typename Storage::stored_t;
using e_t = typename Storage::e_t;

SIMDIFY_INL constexpr reference() = default;
SIMDIFY_INL constexpr reference(const reference&) = default;
Expand Down Expand Up @@ -221,6 +226,8 @@ namespace simd {
SIMDIFY_INL Storage* reset(void* ptr) { m_data = static_cast<Storage*>(ptr); return m_data; }
SIMDIFY_INL Storage*& ptr() { return m_data; }
SIMDIFY_INL Storage* ptr() const { return m_data; }
SIMDIFY_INL e_t& operator[](std::size_t i) { return (*m_data)[i]; }
SIMDIFY_INL const e_t& operator[](std::size_t i) const { return (*m_data)[i]; }

SIMDIFY_INL void swap(reference& rhs) {
using std::swap;
Expand All @@ -245,6 +252,7 @@ namespace simd {
template <typename Storage>
struct const_reference {
using referred_t = typename Storage::stored_t;
using e_t = typename Storage::e_t;

SIMDIFY_INL constexpr const_reference() = default;
SIMDIFY_INL constexpr const_reference(const const_reference&) = default;
Expand All @@ -267,6 +275,7 @@ namespace simd {
SIMDIFY_INL const Storage* reset(const void* ptr) { m_data = static_cast<const Storage*>(ptr); return m_data; }
SIMDIFY_INL const Storage*& ptr() { return m_data; }
SIMDIFY_INL const Storage* ptr() const { return m_data; }
SIMDIFY_INL const e_t& operator[](std::size_t i) const { return (*m_data)[i]; }

// implicit conversion to const referred_t
SIMDIFY_INL operator const referred_t() const { return referred_t(*m_data); }
Expand Down

0 comments on commit 29bb5cb

Please sign in to comment.