Skip to main content

Reduction

Trait Reduction 

Source
pub trait Reduction<T> {
    type Total: Copy + Send + Sync;

    const COMMUTATIVE: bool = false;
    const SPLIT: usize = 8;

    // Required methods
    fn unit() -> Self::Total;
    fn step(acc: Self::Total, x: T) -> Self::Total;
    fn merge(a: Self::Total, b: Self::Total) -> (Self::Total, bool);

    // Provided methods
    fn identity() -> Self::Total { ... }
    fn checked(acc: Self::Total, x: T) -> (Self::Total, bool) { ... }
    fn one(x: T) -> (Self::Total, bool) { ... }
    fn chain(a: Self::Total, b: Self::Total) -> (Self::Total, bool) { ... }
    fn slice(x: &[T]) -> (Self::Total, bool)
       where Self: Sized,
             T: Copy { ... }
    fn scan(
        x: &[T],
        start: Option<Self::Total>,
        out: &mut Vec<Self::Total>,
    ) -> bool
       where Self: Sized,
             T: Copy { ... }
    fn rows(data: &[T], columns: usize, out: &mut [Self::Total]) -> bool
       where Self: Sized,
             T: Copy { ... }
    fn short_lanes(
        _data: &[T],
        _len: usize,
        _start: Option<Self::Total>,
        _out: &mut Vec<Self::Total>,
    ) -> Option<bool>
       where Self: Sized { ... }
}
Expand description

A reduction by Sum, Product, ScaledProduct, Max, Min, And, Or, Xor, Xnor, SumAs, Squares or Alternated, implemented inside rustymath once for each item type it takes. slice folds a whole slice of consecutive items, in any order, merge combines the folds of two neighbouring runs, and scan gives the running folds. along, fold_into and scan_along fold along an axis whose items aren’t consecutive. Sum and Product take floats, integers and complex numbers. Max and Min take floats, integers and Booleans, And, Or and Xor take integers and Booleans, Xnor takes signed integers, bytes and Booleans, SumAs takes Booleans and integers, Squares takes floats and complex numbers, ScaledProduct takes floats, Alternated<Sum> takes floats, integers and complex numbers, Alternated<ScaledProduct> takes floats, and Alternated<Product> takes complex numbers. An f16 reduction computes in f32. A sum or product of f16s gives an f32 total, for its caller to round once. A total that doesn’t fit its type fails, with its value wrapped to that type.

Provided Associated Constants§

Source

const COMMUTATIVE: bool = false

Whether disjoint subsequences may be merged in any order.

Source

const SPLIT: usize = 8

The most classes for which crate::search::ClassNumbers folds each class into four totals by position. Each step of a class’s total waits on the last through memory, so a reduction whose step takes longer gains from the split over more classes.

Required Associated Types§

Source

type Total: Copy + Send + Sync

The type of a fold: the item type, i64 for a sum or product of integers narrower than 64 bits, f32 for a sum or product of f16s, i16 for an XNOR of bytes, the C of SumAs, f64 for Squares, a Scaled product for ScaledProduct, or a total with the parity of its count for Alternated.

Required Methods§

Source

fn unit() -> Self::Total

The fold of no items: 0, 1, -∞ or ∞, the integer type’s least or greatest value, all ones, or false or true.

Source

fn step(acc: Self::Total, x: T) -> Self::Total

acc combined with one more item.

Source

fn merge(a: Self::Total, b: Self::Total) -> (Self::Total, bool)

The folds of two neighbouring runs of items combined into the fold of both, and whether it fits.

Provided Methods§

Source

fn identity() -> Self::Total

The exact identity of step, which leaves every total unchanged. For float sums it’s -0.0, because adding unit(), 0.0, turns a total of -0.0 into 0.0. For every other reduction it’s unit().

Source

fn checked(acc: Self::Total, x: T) -> (Self::Total, bool)

step, and whether its result fits. An integer sum or product wraps where it doesn’t.

Source

fn one(x: T) -> (Self::Total, bool)

The fold of the one item x, checked(identity(), x), and whether it fits.

Source

fn chain(a: Self::Total, b: Self::Total) -> (Self::Total, bool)

merge, for a chain of folds whose every step waits for the one before, as van Herk-Gil-Werman’s suffixes and prefixes do. As in merge, a folds the earlier items. Float maximums and minimums override it with Folds::step_max and step_min, which use AArch64’s one-instruction fmaxnm and fminnm. LLVM can’t vectorize those, so they stay out of merge.

Source

fn slice(x: &[T]) -> (Self::Total, bool)
where Self: Sized, T: Copy,

The fold of x, which is unit() where x is empty, and whether it fits. The default takes each item in turn by checked.

Source

fn scan(x: &[T], start: Option<Self::Total>, out: &mut Vec<Self::Total>) -> bool
where Self: Sized, T: Copy,

Appends the running folds of x to out, the first combining start where it is given, and gives whether every one fits. The default takes each item in turn by checked (checked_steps).

Source

fn rows(data: &[T], columns: usize, out: &mut [Self::Total]) -> bool
where Self: Sized, T: Copy,

Folds each row of data, a matrix with columns items in each row, into the matching item of out, and gives whether every fold fits. The default is fold_rows, which folds each row of 16 items or more by slice. Float sums and products, integer maximums and minimums, and bitwise folds fold rows of 16 to 63 items by checked instead, eight rows side by side (rows_side_by_side). Each of their steps is one instruction, so keeping several rows’ steps in flight costs less than a call of slice for each row. Other reductions keep fold_rows, where slice vectorizes within the row: an integer sum’s step checks for overflow, and a float maximum’s or minimum’s slice runs Folds’ vector kernels. Nobody has timed float maximums side by side since their step became one select on x86.

Source

fn short_lanes( _data: &[T], _len: usize, _start: Option<Self::Total>, _out: &mut Vec<Self::Total>, ) -> Option<bool>
where Self: Sized,

Appends the running folds of data’s lanes of len consecutive items to out, each lane from start, through a route that runs many short lanes at once, and gives whether every fold fits. A reduction without such a route gives None and appends nothing, as the default does. scan_along then runs the lanes itself. The Boolean reductions run lanes of 1 to 64 items through Logical, 64 items to a word.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Reduction<bool> for And

Source§

impl Reduction<bool> for Max

Source§

impl Reduction<bool> for Min

Source§

impl Reduction<bool> for Or

Source§

impl Reduction<bool> for Xnor

Source§

impl Reduction<bool> for Xor

Source§

impl Reduction<f16> for Max

Source§

impl Reduction<f16> for Min

Source§

impl Reduction<f16> for Product

Source§

impl Reduction<f16> for Sum

Source§

impl Reduction<i16> for And

Source§

impl Reduction<i16> for Max

Source§

impl Reduction<i16> for Min

Source§

impl Reduction<i16> for Or

Source§

impl Reduction<i16> for Product

Source§

impl Reduction<i16> for Sum

Source§

impl Reduction<i16> for Wrapped<Product>

Source§

impl Reduction<i16> for Wrapped<Sum>

Source§

impl Reduction<i16> for Xnor

Source§

impl Reduction<i16> for Xor

Source§

impl Reduction<i32> for And

Source§

impl Reduction<i32> for Max

Source§

impl Reduction<i32> for Min

Source§

impl Reduction<i32> for Or

Source§

impl Reduction<i32> for Product

Source§

impl Reduction<i32> for Sum

Source§

impl Reduction<i32> for Wrapped<Product>

Source§

impl Reduction<i32> for Wrapped<Sum>

Source§

impl Reduction<i32> for Xnor

Source§

impl Reduction<i32> for Xor

Source§

impl Reduction<i64> for And

Source§

impl Reduction<i64> for Max

Source§

impl Reduction<i64> for Min

Source§

impl Reduction<i64> for Or

Source§

impl Reduction<i64> for Product

Source§

impl Reduction<i64> for Sum

Source§

impl Reduction<i64> for Wrapped<Product>

Source§

impl Reduction<i64> for Wrapped<Sum>

Source§

impl Reduction<i64> for Xnor

Source§

impl Reduction<i64> for Xor

Source§

impl Reduction<u8> for And

Source§

impl Reduction<u8> for Max

Source§

impl Reduction<u8> for Min

Source§

impl Reduction<u8> for Or

Source§

impl Reduction<u8> for Product

Source§

impl Reduction<u8> for Sum

Source§

impl Reduction<u8> for Wrapped<Product>

Source§

impl Reduction<u8> for Wrapped<Sum>

Source§

impl Reduction<u8> for Xnor

Xnor of bytes, totalled at i16. Each total lies between -256 and 255, and the XNOR of two bytes is negative.

Source§

impl Reduction<u8> for Xor

Source§

impl Reduction<u16> for And

Source§

impl Reduction<u16> for Max

Source§

impl Reduction<u16> for Min

Source§

impl Reduction<u16> for Or

Source§

impl Reduction<u16> for Xor

Source§

impl Reduction<u32> for And

Source§

impl Reduction<u32> for Max

Source§

impl Reduction<u32> for Min

Source§

impl Reduction<u32> for Or

Source§

impl Reduction<u32> for Xor

Source§

impl Reduction<u64> for And

Source§

impl Reduction<u64> for Max

Source§

impl Reduction<u64> for Min

Source§

impl Reduction<u64> for Or

Source§

impl Reduction<u64> for Xor

Source§

impl<F: Native> Reduction<Complex<F>> for Product

Complex products regroup multiplication through the same running totals as real products.

Source§

impl<F: Native> Reduction<Complex<F>> for Sum

Each sum of complex numbers adds their real parts and their imaginary parts, in any order.

Source§

impl<F: Native> Reduction<F> for Max

Source§

const COMMUTATIVE: bool = true

Source§

const SPLIT: usize = 64

Source§

type Total = F

Source§

impl<F: Native> Reduction<F> for Min

Source§

const COMMUTATIVE: bool = true

Source§

const SPLIT: usize = 64

Source§

type Total = F

Source§

impl<F: Native> Reduction<F> for Product

Source§

const COMMUTATIVE: bool = true

Source§

const SPLIT: usize = 64

Source§

type Total = F

Source§

impl<F: Native> Reduction<F> for ScaledProduct

Source§

impl<F: Native> Reduction<F> for Sum

Source§

const COMMUTATIVE: bool = true

Source§

const SPLIT: usize = 64

Source§

type Total = F

Source§

impl<T: Copy, R: Inverse<T>> Reduction<T> for Alternated<R>

Source§

type Total = (<R as Reduction<T>>::Total, bool)

Source§

impl<T: Scalar> Reduction<T> for Squares

Source§

impl<T: Term, C: Tally> Reduction<T> for SumAs<C>

Source§

const COMMUTATIVE: bool = true

Source§

type Total = C