//
// This confidential and proprietary software may be used only as
// authorised by a licensing agreement from ARM Limited
// (C) COPYRIGHT 2020-2023 ARM Limited
// ALL RIGHTS RESERVED
// The entire notice above must be reproduced on all authorised
// copies and copies may only be made to the extent permitted
// by a licensing agreement from ARM Limited.

=== Reduction Operators

==== REDUCE_ALL

Reduce a tensor along the given axis with a logical AND operation

include::{generated}/operators/REDUCE_ALL.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        in_out_t acc = true;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            in_out_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = acc && value;
        }
        out_index = flatten(left_index, [0], right_index);
        tensor_write<in_out_t>(output, shape, out_index, acc);
    }
}
----

==== REDUCE_ANY

Reduce a tensor along the given axis with a logical OR operation

include::{generated}/operators/REDUCE_ANY.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        in_out_t acc = false;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            in_out_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = acc || value;
        }
        out_index = flatten(left_index, [0], right_index);
        tensor_write<in_out_t>(output, shape, out_index, acc);
    }
}
----

==== REDUCE_MAX

Reduce a tensor along the given axis with a maximum operation

include::{generated}/operators/REDUCE_MAX.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        in_out_t acc = minimum<in_out_t>;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            in_out_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = apply_max_s<in_out_t>(acc, value);
        }
        out_index = flatten(left_index, [0], right_index);
        tensor_write<in_out_t>(output, shape, out_index, acc);
    }
}
----

==== REDUCE_MIN

Reduce a tensor along the given axis with a minimum operation

include::{generated}/operators/REDUCE_MIN.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        in_out_t acc = maximum<in_out_t>;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            in_out_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = apply_min_s<in_out_t>(acc, value);
        }
        out_index = flatten(left_index, [0], right_index);
        tensor_write<in_out_t>(output, shape, out_index, out);
    }
}
----

==== REDUCE_PRODUCT

Reduce a tensor along the given axis by computing the product of the axis.

include::{generated}/operators/REDUCE_PRODUCT.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        in_out_t acc = 1.0;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            in_out_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = apply_mul_s<in_out_t>(acc, value);
        }
        out_index = flatten(left_index, [0], right_index);
        tensor_write<in_out_t>(output, shape, out_index, acc);
    }
}
----

==== REDUCE_SUM

Reduce a tensor along the given axis by computing the sum of the axis.

include::{generated}/operators/REDUCE_SUM.adoc[]

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
left_shape  = (axis > 1) ? shape[0:axis-1] : [];
right_shape = (axis < rank(shape)-1) ? shape[axis+1:rank(shape)-1] : [];
for_each(left_index in left_shape) {
    for_each(right_index in right_shape) {
        acc_t acc = 0;
        for (i = 0; i < shape1[axis]; i++) {
            index = flatten(left_index, [i], right_index);
            acc_t value = tensor_read<in_out_t>(input, shape1, index);
            acc = apply_add_s<acc_t>(acc, value);
        }
        out_index = flatten(left_index, [0], right_index);
        in_out_t result = static_cast<in_out_t>(acc);
        tensor_write<in_out_t>(output, shape, out_index, result);
    }
}
----
