29 #ifndef VC_COMMON_ITERATORS_H_
30 #define VC_COMMON_ITERATORS_H_
37 namespace Vc_VERSIONED_NAMESPACE
43 template<
typename _V,
typename Flags>
class MemoryVectorIterator;
48 :
public std::iterator<std::bidirectional_iterator_tag, typename V::EntryType>
53 constexpr Iterator(V &_v,
size_t _i) : v(_v), i(_i) {}
54 constexpr Iterator(
const Iterator &) =
default;
55 constexpr Iterator(Iterator &&) =
default;
57 Vc_ALWAYS_INLINE decltype(v[i]) operator->() {
return v[i]; }
58 Vc_ALWAYS_INLINE decltype(v[i]) operator->()
const {
return v[i]; }
60 Vc_ALWAYS_INLINE decltype(v[i]) operator*() {
return v[i]; }
61 Vc_ALWAYS_INLINE decltype(v[i]) operator*()
const {
return v[i]; }
63 Vc_ALWAYS_INLINE Iterator &operator++() { ++i;
return *
this; }
64 Vc_ALWAYS_INLINE Iterator operator++(
int) { Iterator tmp = *
this; ++i;
return tmp; }
66 Vc_ALWAYS_INLINE Iterator &operator--() { --i;
return *
this; }
67 Vc_ALWAYS_INLINE Iterator operator--(
int) { Iterator tmp = *
this; --i;
return tmp; }
70 Vc_ALWAYS_INLINE
bool operator==(
const Iterator<V> &rhs)
const {
return i == rhs.i; }
71 Vc_ALWAYS_INLINE
bool operator!=(
const Iterator<V> &rhs)
const {
return i != rhs.i; }
72 Vc_ALWAYS_INLINE
bool operator< (const Iterator<V> &rhs)
const {
return i < rhs.i; }
73 Vc_ALWAYS_INLINE
bool operator<=(const Iterator<V> &rhs)
const {
return i <= rhs.i; }
74 Vc_ALWAYS_INLINE
bool operator> (
const Iterator<V> &rhs)
const {
return i > rhs.i; }
75 Vc_ALWAYS_INLINE
bool operator>=(
const Iterator<V> &rhs)
const {
return i >= rhs.i; }
78 template<
typename V>
using ConstIterator = Iterator<const V>;
86 Vc_ALWAYS_INLINE BitmaskIterator(
int m) : mask(m), bit(_mm_tzcnt_32(mask)) {}
87 Vc_ALWAYS_INLINE BitmaskIterator(
const BitmaskIterator &) =
default;
88 Vc_ALWAYS_INLINE BitmaskIterator(BitmaskIterator &&) =
default;
90 Vc_ALWAYS_INLINE
size_t operator->()
const {
return bit; }
91 Vc_ALWAYS_INLINE
size_t operator*()
const {
return bit; }
93 Vc_ALWAYS_INLINE BitmaskIterator &operator++() {
94 bit = _mm_tzcnti_32(bit, mask);
97 Vc_ALWAYS_INLINE BitmaskIterator operator++(
int) {
98 BitmaskIterator tmp = *
this;
99 bit = _mm_tzcnti_32(bit, mask);
103 Vc_ALWAYS_INLINE
bool operator==(
const BitmaskIterator &rhs)
const {
return bit == rhs.bit; }
104 Vc_ALWAYS_INLINE
bool operator!=(
const BitmaskIterator &rhs)
const {
return bit != rhs.bit; }
107 class BitmaskIterator
115 bit = __builtin_ctzl(mask);
116 #elif defined(_WIN64)
117 _BitScanForward64(&bit, mask);
118 #elif defined(_WIN32)
119 _BitScanForward(&bit, mask);
121 #error "Not implemented yet. Please contact vc-devel@compeng.uni-frankfurt.de"
141 BitmaskIterator(
size_t m) : mask(m) { nextBit(); }
142 BitmaskIterator(
const BitmaskIterator &) =
default;
143 BitmaskIterator(BitmaskIterator &&) =
default;
145 Vc_ALWAYS_INLINE
size_t operator->()
const {
return bit; }
146 Vc_ALWAYS_INLINE
size_t operator*()
const {
return bit; }
148 Vc_ALWAYS_INLINE BitmaskIterator &operator++() { resetLsb(); nextBit();
return *
this; }
149 Vc_ALWAYS_INLINE BitmaskIterator operator++(
int) { BitmaskIterator tmp = *
this; resetLsb(); nextBit();
return tmp; }
151 Vc_ALWAYS_INLINE
bool operator==(
const BitmaskIterator &rhs)
const {
return mask == rhs.mask; }
152 Vc_ALWAYS_INLINE
bool operator!=(
const BitmaskIterator &rhs)
const {
return mask != rhs.mask; }
156 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_vector<V>::value, Iterator<V>>::type begin(V &v)
161 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_vector<V>::value, Iterator<V>>::type end(V &v)
163 return { v, V::Size };
166 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_mask<V>::value || Traits::is_simd_vector<V>::value, ConstIterator<V>>::type begin(
const V &v)
171 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_mask<V>::value || Traits::is_simd_vector<V>::value, ConstIterator<V>>::type end(
const V &v)
173 return { v, V::Size };
176 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_mask<V>::value || Traits::is_simd_vector<V>::value, ConstIterator<V>>::type cbegin(
const V &v)
181 template<
typename V> constexpr
typename std::enable_if<Traits::is_simd_mask<V>::value || Traits::is_simd_vector<V>::value, ConstIterator<V>>::type cend(
const V &v)
183 return { v, V::Size };
186 template<
typename M> Vc_ALWAYS_INLINE BitmaskIterator begin(
const WhereImpl::WhereMask<M> &w)
188 return w.mask.toInt();
191 template<
typename M> Vc_ALWAYS_INLINE BitmaskIterator end(
const WhereImpl::WhereMask<M> &)
196 template<
typename V,
typename Flags,
typename T> Vc_ALWAYS_INLINE MemoryVectorIterator<V, Flags>
197 makeIterator(T *mem, Flags)
199 return new(mem) MemoryVector<V, Flags>;
202 template<
typename V,
typename Flags,
typename T> Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags>
203 makeIterator(
const T *mem, Flags)
205 return new(
const_cast<T *
>(mem)) MemoryVector<const V, Flags>;
208 template<
typename V,
typename Flags,
typename FlagsX> Vc_ALWAYS_INLINE MemoryVectorIterator<V, Flags>
209 makeIterator(MemoryVector<V, FlagsX> &mv, Flags)
211 return new(&mv) MemoryVector<V, Flags>;
214 template<
typename V,
typename Flags,
typename FlagsX> Vc_ALWAYS_INLINE MemoryVectorIterator<const V, Flags>
215 makeIterator(MemoryVector<const V, FlagsX> &mv, Flags)
217 return new(&mv) MemoryVector<const V, Flags>;
224 using Common::makeIterator;
227 #endif // VC_COMMON_ITERATORS_H_
Helper class for the Memory::vector(size_t) class of functions.