aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/kernels/elementwise_binary/generic/sve/impl.h
blob: 4c61b9f3157cad96fffd9e11f31cd61d1c645476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) 2021-2022 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef SRC_CORE_SVE_KERNELS_ELEMENTWISE_LIST_H
#define SRC_CORE_SVE_KERNELS_ELEMENTWISE_LIST_H

#include "arm_compute/core/Helpers.h"

#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
#include "src/core/NEON/wrapper/svtraits.h"

namespace arm_compute
{
namespace cpu
{
using namespace arm_compute::wrapper;

template <typename VectorType>
VectorType elementwise_pow(svbool_t &pg, const VectorType &a, const VectorType &b)
{
    return svpow_z(pg, a, b);
}

template <typename VectorType>
VectorType elementwise_div(svbool_t &pg, const VectorType &a, const VectorType &b)
{
    return svdiv_z(pg, a, b);
}

template <uint32_t bytewidth>
svbool_t narrow_to_byte_predicate(svbool_t pg)
{
    const auto all_false = svpfalse();

    switch (bytewidth)
    {
        case 8:
            pg = svuzp1_b32(pg, all_false);
        /* fall through */
        case 4:
            pg = svuzp1_b16(pg, all_false);
        /* fall through */
        case 2:
            pg = svuzp1_b8(pg, all_false);
        /* fall through */
        default:
            break;
    }
    return pg;
}

template <typename VectorType>
VectorType elementwise_arithmetic_op(svbool_t &pg, const VectorType &a, const VectorType &b, ArithmeticOperation op)
{
    using ScalarType = typename wrapper::sve_scalar<VectorType>::type;
    VectorType res{};

    switch (op)
    {
        case ArithmeticOperation::MAX:
            res = svmax_z(pg, a, b);
            break;
        case ArithmeticOperation::MIN:
            res = svmin_z(pg, a, b);
            break;
        case ArithmeticOperation::SQUARED_DIFF:
        {
            const auto tmp = svsub_z(pg, a, b);
            res            = svmul_z(pg, tmp, tmp);
            break;
        }
        case ArithmeticOperation::PRELU:
        {
            const auto zero = svdup_n(ScalarType(0));
            const auto tmp  = svmul_z(pg, a, b);
            const auto gt   = svcmpgt(pg, a, zero);
            res             = svsel(gt, a, tmp);
            break;
        }
        case ArithmeticOperation::DIV:
        {
            res = elementwise_div(pg, a, b);
            break;
        }
        case ArithmeticOperation::POWER:
        {
            res = elementwise_pow(pg, a, b);
            break;
        }
        default:
            ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
    }

    return res;
}

template <typename InputVectorType, typename OutputVectorType>
OutputVectorType
elementwise_comparison_op(svbool_t &pg, const InputVectorType &a, const InputVectorType &b, ComparisonOperation op)
{
    svbool_t selection_vector{};

    switch (op)
    {
        case ComparisonOperation::Equal:
            selection_vector = svcmpeq(pg, a, b);
            break;
        case ComparisonOperation::NotEqual:
            selection_vector = svcmpne(pg, a, b);
            break;
        case ComparisonOperation::Greater:
            selection_vector = svcmpgt(pg, a, b);
            break;
        case ComparisonOperation::GreaterEqual:
            selection_vector = svcmpge(pg, a, b);
            break;
        case ComparisonOperation::Less:
            selection_vector = svcmplt(pg, a, b);
            break;
        case ComparisonOperation::LessEqual:
            selection_vector = svcmple(pg, a, b);
            break;
        default:
            ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
    }

    using InputScalarType = typename wrapper::sve_scalar<InputVectorType>::type;
    selection_vector      = narrow_to_byte_predicate<sizeof(InputScalarType)>(selection_vector);

    using OutputScalarType  = typename wrapper::sve_scalar<OutputVectorType>::type;
    const auto false_vector = svdup_n(static_cast<OutputScalarType>((uint32_t)0));
    const auto true_vector  = svdup_n(static_cast<OutputScalarType>(~(uint32_t)0));
    auto       ret          = svsel(selection_vector, true_vector, false_vector);

    return ret;
}

template <typename ScalarType>
void elementwise_arithmetic_op(
    const ITensor *in1, const ITensor *in2, ITensor *out, ArithmeticOperation op, const Window &window);

template <typename ScalarType, typename OutputScalarType = uint8_t>
void elementwise_comparison_op(
    const ITensor *in1, const ITensor *in2, ITensor *out, ComparisonOperation op, const Window &window);
} // namespace cpu
} // namespace arm_compute
#endif /* SRC_CORE_SVE_KERNELS_ELEMENTWISE_LIST_H */