xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/elementwise_unary/generic/sve/impl.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "arm_compute/core/Helpers.h"
26 #include "arm_compute/core/utils/misc/Traits.h"
27 #include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
28 
29 namespace arm_compute
30 {
31 namespace cpu
32 {
33 template <typename ScalarType, typename VectorType>
elementwise_op_sve_imp(svbool_t pg,ElementWiseUnary op,const VectorType & a)34 inline typename std::enable_if<utils::traits::is_floating_point<ScalarType>::value, VectorType>::type elementwise_op_sve_imp(svbool_t pg, ElementWiseUnary op, const VectorType &a)
35 {
36     switch(op)
37     {
38         case ElementWiseUnary::RSQRT:
39             return svinvsqrt(pg, a);
40         case ElementWiseUnary::EXP:
41             return wrapper::svexp_z(pg, a);
42         case ElementWiseUnary::NEG:
43             return svneg_z(pg, a);
44         case ElementWiseUnary::LOG:
45             return wrapper::svlog_z(pg, a);
46         case ElementWiseUnary::ABS:
47             return svabs_z(pg, a);
48         case ElementWiseUnary::ROUND:
49             return svrintn_z(pg, a);
50         case ElementWiseUnary::SIN:
51             return wrapper::svsin_z(pg, a);
52         default:
53             ARM_COMPUTE_ERROR("NOT_SUPPORTED");
54     }
55 }
56 
57 template <typename ScalarType, typename VectorType>
elementwise_op_sve_imp(svbool_t pg,ElementWiseUnary op,const VectorType & a)58 inline typename std::enable_if<std::is_integral<ScalarType>::value, VectorType>::type elementwise_op_sve_imp(svbool_t pg, ElementWiseUnary op, const VectorType &a)
59 {
60     switch(op)
61     {
62         case ElementWiseUnary::NEG:
63             return svneg_z(pg, a);
64         case ElementWiseUnary::ABS:
65             return svabs_z(pg, a);
66         default:
67             ARM_COMPUTE_ERROR("NOT_SUPPORTED");
68     }
69 }
70 
71 template <typename ScalarType>
elementwise_sve_op(const ITensor * in,ITensor * out,const Window & window,ElementWiseUnary op)72 void elementwise_sve_op(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
73 {
74     const auto all_true_pg    = wrapper::svptrue<ScalarType>();
75     const auto window_start_x = static_cast<int>(window.x().start());
76     const auto window_end_x   = static_cast<int>(window.x().end());
77 
78     Window win = window;
79     win.set(Window::DimX, Window::Dimension(0, 1, 1));
80 
81     Iterator input(in, win);
82     Iterator output(out, win);
83 
84     execute_window_loop(win, [&](const Coordinates &)
85     {
86         auto       output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
87         const auto input_ptr  = reinterpret_cast<const ScalarType *>(input.ptr());
88         int        x          = window_start_x;
89 
90         svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
91         do
92         {
93             const auto vin = svld1(pg, input_ptr + x);
94             svst1(pg, output_ptr + x, elementwise_op_sve_imp<ScalarType, decltype(vin)>(pg, op, vin));
95             x += wrapper::svcnt<ScalarType>();
96             pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
97         }
98         while(svptest_any(all_true_pg, pg));
99     },
100     input, output);
101 }
102 
103 template void elementwise_sve_op<float16_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
104 template void elementwise_sve_op<float32_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
105 template void elementwise_sve_op<int32_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
106 
107 } // namespace cpu
108 } // namespace arm_compute
109