Namespaces
Variants

std::experimental:: reduce, std::experimental:: hmin, std::experimental:: hmax

From cppreference.net
定义于头文件 <experimental/simd>
template < class T, class Abi, class BinaryOperation = std:: plus <> >
T reduce ( const simd < T, Abi > & v, BinaryOperation binary_op = { } ) ;
(1) (并行 TS v2)
template < class M, class V, class BinaryOperation >

typename V :: value_type
reduce ( const const_where_expression < M, V > & x,

typename V :: value_type identity_element, BinaryOperation binary_op = { } ) ;
(2) (并行 TS v2)
template < class M, class V >

typename V :: value_type

reduce ( const const_where_expression < M, V > & x, std:: plus <> binary_op ) noexcept ;
(3) (并行 TS v2)
template < class M, class V >

typename V :: value_type

reduce ( const const_where_expression < M, V > & x, std:: multiplies <> binary_op ) noexcept ;
(4) (并行 TS v2)
template < class M, class V >

typename V :: value_type

reduce ( const const_where_expression < M, V > & x, std:: bit_and <> binary_op ) noexcept ;
(5) (并行 TS v2)
template < class M, class V >

typename V :: value_type

reduce ( const const_where_expression < M, V > & x, std:: bit_or <> binary_op ) noexcept ;
(6) (并行 TS v2)
template < class M, class V >

typename V :: value_type

reduce ( const const_where_expression < M, V > & x, std:: bit_xor <> binary_op ) noexcept ;
(7) (并行 TS v2)
template < class T, class Abi >
T hmin ( const simd < T, Abi > & v ) noexcept ;
(8) (并行 TS v2)
template < class M, class V >

typename V :: value_type

hmin ( const const_where_expression < M, V > & x ) noexcept ;
(9) (并行 TS v2)
template < class T, class Abi >
T hmax ( const simd < T, Abi > & v ) noexcept ;
(10) (并行 TS v2)
template < class M, class V >

typename V :: value_type

hmax ( const const_where_expression < M, V > & x ) noexcept ;
(11) (并行 TS v2)
1) v 中的所有值通过 binary_op 进行归约操作。
2) x 中关联掩码元素为 true 的值,通过 binary_op 进行归约操作。
3) 返回 x 中所有掩码元素为 true 的对应值的总和。
4) 返回 x 中所有掩码元素为 true 的对应值的乘积。
5) 返回 x 中所有关联掩码元素为 true 的值的按位与聚合结果。
6) 返回对 x 中所有掩码元素为 true 的值进行按位或聚合的结果。
7) 返回使用按位异或对 x 中所有掩码元素为 true 的数值进行聚合的结果。
8) 使用 std:: min v 中的所有值进行归约。
9) x 中所有关联掩码元素为 true 的值,通过 std:: min 进行归约操作。
10) v 中的所有值通过 std:: max 进行归约。
11) x 中所有关联掩码元素为 true 的值通过 std:: max 进行归约操作。

如果 binary_op 不满足结合律或交换律,该行为将具有不确定性。

目录

参数

v - 要应用归约操作的 simd 向量
x - 要应用归约操作的 where 表达式返回值
identity_element - 作为 binary_op 单位元的值;对于类型为 V :: value_type 的所有有限值 a ,必须满足 binary_op ( identity_element, a ) == a
binary_op - 二元 FunctionObject ,将以未指定顺序应用于类型为 V :: value_type simd < V :: value_type , A > 的参数(具有未指定的 ABI 标签 A )。 binary_op ( v, v ) 必须可转换为 V

返回值

该类型操作的结果:

1,8,10) T
2-7,9,11) V :: value_type

示例

#include <array>
#include <cassert>
#include <cstddef>
#include <experimental/simd>
#include <functional>
#include <iostream>
#include <numeric>
namespace stdx = std::experimental;
int main()
{
    using V = stdx::native_simd<double>;
    alignas(stdx::memory_alignment_v<V>) std::array<V::value_type, 1024> data;
    std::iota(data.begin(), data.end(), 0);
    V::value_type acc{};
    for (std::size_t i = 0; i < data.size(); i += V::size())
        acc += stdx::reduce(V(&data[i], stdx::vector_aligned), std::plus{});
    std::cout << "sum of data = " << acc << '\n';
    using W = stdx::fixed_size_simd<int, 4>;
    alignas(stdx::memory_alignment_v<W>) std::array<int, 4> arr{2, 5, 4, 1};
    auto w = W(&arr[0], stdx::vector_aligned);
    assert(stdx::hmin(w) == 1 and stdx::hmax(w) == 5);
}

输出:

sum of data = 523776

参见

(C++17)
类似于 std::accumulate ,但支持乱序执行
(函数模板)