//
// This confidential and proprietary software may be used only as
// authorised by a licensing agreement from ARM Limited
// (C) COPYRIGHT 2020-2021 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

*Arguments:*

|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, true);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = acc && value;
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|Any|Boolean|bool_t
|===

==== REDUCE_ANY

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

*Arguments:*

|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, false);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = acc || value;
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|Any|Boolean|bool_t
|===

==== REDUCE_MAX

Reduce a tensor along the given axis with a maximum operation

*Arguments:*

|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, minimum<in_t>);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = apply_max<in_t>(acc, value);
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|Any|signed 8|int8_t
|Any|signed 16|int16_t
|Any|signed 32|int32_t
|MI, MT|floating-point|float_t
|===

==== REDUCE_MIN

Reduce a tensor along the given axis with a minimum operation

*Arguments:*
|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, maximum<in_t>);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = apply_min<in_t>(acc, value);
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|Any|signed 8|int8_t
|Any|signed 16|int16_t
|Any|signed 32|int32_t
|MI, MT|floating-point|float_t
|===

==== REDUCE_PRODUCT

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

*Arguments:*

|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, 1.0);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = acc * value;
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|MI, MT|floating-point|float_t
|===

==== REDUCE_SUM

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

*Arguments:*

|===
|Argument|Type|Name|Shape|Description

|Input|in_t*|input|shape1|Input tensor with rank from 1 to 4
|Attribute|int32_t|axis|-|Axis to reduce, in range from 0 to rank(shape1)-1
|Output|in_t*|output|shape|Output tensor. Same rank as the input tensor.
|===

*Operation Function:*

[source,c]
----
ERROR_IF(axis < 0  || axis >= rank(shape1));
ERROR_IF(shape[axis] != 1);
for_each(index in shape) {
    tensor_write<in_t>(output, shape, index, 0);
}
for_each(index in shape1) {
    tmp_index = index;
    tmp_index[axis]=0;
    value = tensor_read<in_t>(input, shape1, index);
    acc   = tensor_read<in_t>(output, shape, tmp_index);
    acc   = apply_add<in_t>(acc, value);
    tensor_write<in_t>(output, shape, tmp_index, acc);
}
----

*Supported Data Types:*

|===
|Profile|Mode|in_t

|Any|signed 32|int32_t
|MI, MT|floating-point|float_t
|===

