Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 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" |
| 28 | #include "src/core/NEON/wrapper/intrinsics/intrinsics.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 | { |
| 39 | case ElementWiseUnary::RSQRT: |
| 40 | return 1 / sqrt(a); |
| 41 | case ElementWiseUnary::EXP: |
| 42 | return std::exp(a); |
| 43 | case ElementWiseUnary::NEG: |
| 44 | return -a; |
| 45 | case ElementWiseUnary::LOG: |
| 46 | return std::log(a); |
| 47 | case ElementWiseUnary::ABS: |
| 48 | return std::abs(a); |
| 49 | case ElementWiseUnary::ROUND: |
| 50 | return support::cpp11::nearbyint(a); |
| 51 | case ElementWiseUnary::SIN: |
| 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 | { |
| 63 | case ElementWiseUnary::RSQRT: |
| 64 | return wrapper::vinvsqrt(a); |
| 65 | case ElementWiseUnary::EXP: |
| 66 | return wrapper::vexpq(a); |
| 67 | case ElementWiseUnary::NEG: |
| 68 | return wrapper::vneg(a); |
| 69 | case ElementWiseUnary::LOG: |
| 70 | return wrapper::vlog(a); |
| 71 | case ElementWiseUnary::ABS: |
| 72 | return wrapper::vabs(a); |
| 73 | case ElementWiseUnary::ROUND: |
| 74 | return wrapper::vround(a); |
| 75 | case ElementWiseUnary::SIN: |
| 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 |