[simd.reductions]

# 29 Numerics library [[numerics]](./#numerics)

## 29.10 Data-parallel types [[simd]](simd#reductions)

### 29.10.8 basic_vec non-member operations [[simd.nonmembers]](simd.nonmembers#simd.reductions)

#### 29.10.8.5 basic_vec reductions [simd.reductions]

[ð](#lib:reduce,simd)

`template<class T, class Abi, class BinaryOperation = plus<>>
  constexpr T reduce(const basic_vec<T, Abi>& x, BinaryOperation binary_op = {});
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18313)

*Constraints*: BinaryOperation models[*reduction-binary-operation*](simd.expos.defn#concept:reduction-binary-operation "29.10.2.1 Exposition-only helpers [simd.expos.defn]")<T>[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18318)

*Preconditions*: binary_op does not modify x[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18322)

*Returns*: *GENERALIZED_SUM*(binary_op, vec<T, 1>(x[0]), …,
vec<T, 1>(x[x.size() - 1]))[​0] ([[numerics.defns]](numerics.defns "26.10.2 Definitions"))[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18327)

*Throws*: Any exception thrown from binary_op[.](#4.sentence-1)

[ð](#lib:reduce,simd_)

`template<class T, class Abi, class BinaryOperation = plus<>>
  constexpr T reduce(
    const basic_vec<T, Abi>& x, const typename basic_vec<T, Abi>::mask_type& mask,
    BinaryOperation binary_op = {}, type_identity_t<T> identity_element = see below);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18341)

*Constraints*: 

- [(5.1)](#5.1)

  BinaryOperation models [*reduction-binary-operation*](simd.expos.defn#concept:reduction-binary-operation "29.10.2.1 Exposition-only helpers [simd.expos.defn]")<T>[.](#5.1.sentence-1)

- [(5.2)](#5.2)

  An argument for identity_element is provided for the invocation,
 unless BinaryOperation is one of plus<>, multiplies<>, bit_and<>, bit_or<>, or bit_xor<>[.](#5.2.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18354)

*Preconditions*: 

- [(6.1)](#6.1)

  binary_op does not modify x[.](#6.1.sentence-1)

- [(6.2)](#6.2)

  For all finite values y representable by T, the results of y == binary_op(vec<T, 1>(identity_element), vec<T, 1>(y))[0] and y == binary_op(vec<T, 1>(y), vec<T, 1>(identity_element))[0] are true[.](#6.2.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18366)

*Returns*: If none_of(mask) is true, returns identity_element[.](#7.sentence-1)

Otherwise, returns *GENERALIZED_SUM*(binary_op, vec<T,1>(x[k0]), …, vec<T, 1>(x[kn]))[0] where k0,â¦,kn are
the selected indices of mask[.](#7.sentence-2)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18373)

*Throws*: Any exception thrown from binary_op[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18377)

*Remarks*: The default argument for identity_element is equal to

- [(9.1)](#9.1)

T() if BinaryOperation is plus<>,

- [(9.2)](#9.2)

T(1) if BinaryOperation is multiplies<>,

- [(9.3)](#9.3)

T(~T()) if BinaryOperation is bit_and<>,

- [(9.4)](#9.4)

T() if BinaryOperation is bit_or<>, or

- [(9.5)](#9.5)

T() if BinaryOperation is bit_xor<>[.](#9.sentence-1)

[ð](#lib:reduce_min,simd)

`template<class T, class Abi> constexpr T reduce_min(const basic_vec<T, Abi>& x) noexcept;
`

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18395)

*Constraints*: T models [totally_ordered](concept.totallyordered#concept:totally_ordered "18.5.5 Concept totally_­ordered [concept.totallyordered]")[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18399)

*Returns*: The value of an element x[j] for which x[i] < x[j] isfalse for all i in the range of [0, basic_vec<T, Abi>​::​size())[.](#11.sentence-1)

[ð](#lib:reduce_min,simd_)

`template<class T, class Abi>
  constexpr T reduce_min(
    const basic_vec<T, Abi>&, const typename basic_vec<T, Abi>::mask_type&) noexcept;
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18413)

*Constraints*: T models [totally_ordered](concept.totallyordered#concept:totally_ordered "18.5.5 Concept totally_­ordered [concept.totallyordered]")[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18417)

*Returns*: If none_of(mask) is true, returnsnumeric_limits<T>​::​max()[.](#13.sentence-1)

Otherwise, returns the value of a selected element x[j] for whichx[i] < x[j] is false for all selected indices i ofmask[.](#13.sentence-2)

[ð](#lib:reduce_max,simd)

`template<class T, class Abi> constexpr T reduce_max(const basic_vec<T, Abi>& x) noexcept;
`

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18432)

*Constraints*: T models [totally_ordered](concept.totallyordered#concept:totally_ordered "18.5.5 Concept totally_­ordered [concept.totallyordered]")[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18436)

*Returns*: The value of an element x[j] for which x[j] < x[i] isfalse for all i in the range of [0, basic_vec<T, Abi>​::​size())[.](#15.sentence-1)

[ð](#lib:reduce_max,simd_)

`template<class T, class Abi>
  constexpr T reduce_max(
    const basic_vec<T, Abi>&, const typename basic_vec<T, Abi>::mask_type&) noexcept;
`

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18450)

*Constraints*: T models [totally_ordered](concept.totallyordered#concept:totally_ordered "18.5.5 Concept totally_­ordered [concept.totallyordered]")[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L18454)

*Returns*: If none_of(mask) is true, returnsnumeric_limits<V​::​value_type>​::​lowest()[.](#17.sentence-1)

Otherwise, returns the value of a selected element x[j] for whichx[j] < x[i] is false for all selected indices i ofmask[.](#17.sentence-2)
