VcDevel / std-simd

std::experimental::simd for GCC [ISO/IEC TS 19570:2018]
Other
572 stars 37 forks source link

hmin and / or imin #18

Open dcampora opened 4 years ago

dcampora commented 4 years ago

Hi there,

Thanks a lot for your work on std-simd, it's looking very well so far!

I was attempting my first code at using your library. I have previously used Vc, as well as other vectorization libraries. I bumped into the following use-case: I would like to calculate the index of the lowest element in a vector, out of some active elements in a mask. Looking at the following document:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4808.pdf

I found that there is hmin, which can accept a const_where_expression, which sort-of does what I want but not quite, since this returns the value of the lowest element, not the index. In order to further fetch the index, I figured one can create yet another mask comparing with the obtained value, and then find the index of that. Ie. see the following pseudo-code:

Testcase

const auto best_scatter = std::experimental::parallelism_v2::hmin(std::experimental::const_where_expression(mask, scatter));
const auto best_mask = best_scatter == scatter;
const auto best_scatter_index = std::experimental::parallelism_v2::find_first_set(best_mask);

With this, I have two questions:

boblytton commented 1 year ago

As a user, having member functions simd::min_element & simd::max_element instead of hmin and hmax would reduce my cognitive burden. They would give a nod to the std algorithms that return by iterator rather than value.