Skip to content

Make simd and simd_mask ranges #40

@someonewithpc

Description

@someonewithpc
version / revision Operating System Compiler & Version Compiler Flags CPU
? All All All All

Motivation

The library already provides indexed access. Providing a range interface/iterator access would allow the type to be used with all STL functions. Event though it would degrade to doing it element-wise, it would still vastly increase the ergonomics when some specific utility is not directly available for the simd type. It could also pave the way for specializing some STL functions, like std::min_element, instead of providing hmin, addressing #18

Let me know if you think this is a direction worth taking and I can write a PR

Testcase

#include <experimental/simd>
#include <iostream>

namespace stdx = std::experimental;

int main() {
  stdx::native_simd<int> a{[](int i) { return 2 << (i - 1); }};

  for (auto v : a) {
    std::cout << v << '\n';
  }
}

Actual Results

$ g++ simd-range.cpp && ./a.out 
simd-range.cpp: In function ‘int main()’:
simd-range.cpp:13:17: error: ‘begin’ was not declared in this scope; did you mean ‘std::begin’?
   13 |   for (auto v : a) {
      |                 ^
      |                 std::begin
In file included from /usr/include/c++/13.2.1/string:53,
                 from /usr/include/c++/13.2.1/bitset:52,
                 from /usr/include/c++/13.2.1/experimental/bits/simd.h:33,
                 from /usr/include/c++/13.2.1/experimental/simd:74,
                 from simd-range.cpp:1:
/usr/include/c++/13.2.1/bits/range_access.h:114:37: note: ‘std::begin’ declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
simd-range.cpp:13:17: error: ‘end’ was not declared in this scope; did you mean ‘std::end’?
   13 |   for (auto v : a) {
      |                 ^
      |                 std::end
/usr/include/c++/13.2.1/bits/range_access.h:116:37: note: ‘std::end’ declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      | 

Expected Results

0
2
4
8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions