Compute Library
 21.02
elementwise_unary_list.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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 #ifndef SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H
25 #define SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H
26 
27 #include "arm_compute/core/Types.h"
29 
30 namespace arm_compute
31 {
32 namespace cpu
33 {
34 template <typename ScalarType>
35 inline ScalarType elementwise_op_scalar_imp(ElementWiseUnary op, const ScalarType &a)
36 {
37  switch(op)
38  {
40  return 1 / sqrt(a);
42  return std::exp(a);
44  return -a;
46  return std::log(a);
48  return std::abs(a);
50  return support::cpp11::nearbyint(a);
52  return std::sin(a);
53  default:
54  ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
55  }
56 }
57 
58 template <typename ScalarType, typename VectorType>
59 inline VectorType elementwise_op_imp(ElementWiseUnary op, const VectorType &a)
60 {
61  switch(op)
62  {
64  return wrapper::vinvsqrt(a);
66  return wrapper::vexpq(a);
68  return wrapper::vneg(a);
70  return wrapper::vlog(a);
72  return wrapper::vabs(a);
74  return wrapper::vround(a);
76  return wrapper::vsin(a);
77  default:
78  ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
79  }
80 }
81 
82 template <typename ScalarType>
83 void elementwise_op(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
84 {
85  const int window_step_x = 16 / sizeof(ScalarType);
86  const auto window_start_x = static_cast<int>(window.x().start());
87  const auto window_end_x = static_cast<int>(window.x().end());
88 
89  Window win = window;
90  win.set(Window::DimX, Window::Dimension(0, 1, 1));
91 
92  Iterator input(in, win);
93  Iterator output(out, win);
94 
95  execute_window_loop(win, [&](const Coordinates &)
96  {
97  auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
98  const auto input_ptr = reinterpret_cast<const ScalarType *>(input.ptr());
99 
100  int x = window_start_x;
101  for(; x <= window_end_x - window_step_x; x += window_step_x)
102  {
103  wrapper::vstore(output_ptr + x, elementwise_op_imp<ScalarType>(op, wrapper::vloadq(input_ptr + x)));
104  }
105  for(; x < window_end_x; ++x)
106  {
107  *(output_ptr + x) = elementwise_op_scalar_imp(op, *(input_ptr + x));
108  }
109  },
110  input, output);
111 }
112 
113 } // namespace cpu
114 } // namespace arm_compute
115 
116 #endif // SRC_CORE_NEON_KERNELS_ELEMENTWISE_UNARY_LIST_H
float32x4_t vlog(const float32x4_t &a)
Definition: log.h:47
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
T nearbyint(T value)
Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
float32x2_t vinvsqrt(const float32x2_t &a)
Definition: invsqrt.h:47
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
int8x8_t vabs(const int8x8_t &a)
Definition: abs.h:46
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
ScalarType elementwise_op_scalar_imp(ElementWiseUnary op, const ScalarType &a)
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
void elementwise_op(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, OutputScalarType(*scalar_func)(const InputScalarType &, const InputScalarType &), int(*broadcast_func)(int, int, int, const InputScalarType *, const InputScalarType &, OutputScalarType *, const bool), int(*neon_func)(int, int, int, const InputScalarType *, const InputScalarType *, OutputScalarType *))
float32x4_t vsin(const float32x4_t &a)
Definition: sin.h:47
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
int8x8_t vneg(const int8x8_t &a)
Definition: neg.h:39
VectorType elementwise_op_imp(ElementWiseUnary op, const VectorType &a)
float32x4_t vround(const float32x4_t &a)
Definition: round.h:47
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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:99
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
ElementWiseUnary
Available element wise unary operations.
Definition: Types.h:547
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
float32x4_t vexpq(const float32x4_t &a)
Definition: exp.h:47
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:145