Compute Library
 22.11
impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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  */
25 
26 namespace arm_compute
27 {
28 namespace cpu
29 {
30 template <typename ScalarType>
31 inline ScalarType elementwise_op_scalar_imp(ElementWiseUnary op, const ScalarType &a)
32 {
33  switch(op)
34  {
36  return 1 / sqrt(a);
38  return std::exp(a);
40  return -a;
42  return std::log(a);
44  return std::abs(a);
46  return support::cpp11::nearbyint(a);
48  return std::sin(a);
49  default:
50  ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
51  }
52 }
53 
54 template <typename ScalarType, typename VectorType>
55 inline VectorType elementwise_op_imp(ElementWiseUnary op, const VectorType &a)
56 {
57  switch(op)
58  {
60  return wrapper::vinvsqrt(a);
62  return wrapper::vexpq(a);
64  return wrapper::vneg(a);
66  return wrapper::vlog(a);
68  return wrapper::vabs(a);
70  return wrapper::vround(a);
72  return wrapper::vsin(a);
73  default:
74  ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
75  }
76 }
77 
78 template <typename ScalarType>
79 void elementwise_op(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
80 {
81  const int window_step_x = 16 / sizeof(ScalarType);
82  const auto window_start_x = static_cast<int>(window.x().start());
83  const auto window_end_x = static_cast<int>(window.x().end());
84 
85  Window win = window;
86  win.set(Window::DimX, Window::Dimension(0, 1, 1));
87 
88  Iterator input(in, win);
89  Iterator output(out, win);
90 
91  execute_window_loop(win, [&](const Coordinates &)
92  {
93  auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
94  const auto input_ptr = reinterpret_cast<const ScalarType *>(input.ptr());
95 
96  int x = window_start_x;
97  for(; x <= window_end_x - window_step_x; x += window_step_x)
98  {
99  wrapper::vstore(output_ptr + x, elementwise_op_imp<ScalarType>(op, wrapper::vloadq(input_ptr + x)));
100  }
101  for(; x < window_end_x; ++x)
102  {
103  *(output_ptr + x) = elementwise_op_scalar_imp(op, *(input_ptr + x));
104  }
105  },
106  input, output);
107 }
108 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
109 template void elementwise_op<__fp16>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
110 #endif //defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
111 template void elementwise_op<float>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
112 template void elementwise_op<int32_t>(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op);
113 
114 } // namespace cpu
115 } // namespace arm_compute
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
template void elementwise_op< float >(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
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:79
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
template void elementwise_op< int32_t >(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
ScalarType elementwise_op_scalar_imp(ElementWiseUnary op, const ScalarType &a)
Definition: impl.cpp:31
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 *))
Definition: impl.h:84
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)
Definition: impl.cpp:55
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: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
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:159