Compute Library
 22.11
impl.cpp
Go to the documentation of this file.
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 
28 
29 namespace arm_compute
30 {
31 namespace cpu
32 {
33 template <typename ScalarType, typename VectorType>
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  {
39  return svinvsqrt(pg, a);
41  return wrapper::svexp_z(pg, a);
43  return svneg_z(pg, a);
45  return wrapper::svlog_z(pg, a);
47  return svabs_z(pg, a);
49  return svrintn_z(pg, a);
51  return wrapper::svsin_z(pg, a);
52  default:
53  ARM_COMPUTE_ERROR("NOT_SUPPORTED");
54  }
55 }
56 
57 template <typename ScalarType, typename VectorType>
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  {
63  return svneg_z(pg, a);
65  return svabs_z(pg, a);
66  default:
67  ARM_COMPUTE_ERROR("NOT_SUPPORTED");
68  }
69 }
70 
71 template <typename ScalarType>
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
template void elementwise_sve_op< int32_t >(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
template void elementwise_sve_op< float32_t >(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
std::enable_if< utils::traits::is_floating_point< ScalarType >::value, VectorType >::type elementwise_op_sve_imp(svbool_t pg, ElementWiseUnary op, const VectorType &a)
Definition: impl.cpp:34
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
void elementwise_sve_op(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: impl.cpp:72
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Coordinates of an item.
Definition: Coordinates.h:37
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:102
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
ElementWiseUnary
Available element wise unary operations.
Definition: Types.h:502
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:97
Describe a multidimensional execution window.
Definition: Window.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:159
template void elementwise_sve_op< float16_t >(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)