1 /*
2 * Copyright (c) 2017-2020 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 #include "src/core/NEON/kernels/NEFloorKernel.h"
25
26 #include "arm_compute/core/Coordinates.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/Validate.h"
30 #include "src/core/CPP/Validate.h"
31 #include "src/core/NEON/INEKernel.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34
35 #include "src/core/NEON/kernels/floor/impl/list.h"
36 #include "src/core/common/Registrars.h"
37
38 namespace arm_compute
39 {
40 namespace
41 {
42 struct FloorSelectorData
43 {
44 DataType dt;
45 };
46 using FloorSelectorPtr = std::add_pointer<bool(const FloorSelectorData &data)>::type;
47 using FloorUKernelPtr = std::add_pointer<void(const void *, void *, int)>::type;
48
49 struct FloorKernel
50 {
51 const char *name;
52 const FloorSelectorPtr is_selected;
53 FloorUKernelPtr ukernel;
54 };
55
56 static const FloorKernel available_kernels[] =
57 {
58 {
59 "fp16_neon_floor",
__anon1384abb60202() 60 [](const FloorSelectorData & data) { return data.dt == DataType::F16; },
61 REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_floor)
62 },
63 {
64 "f32_neon_floor",
__anon1384abb60302() 65 [](const FloorSelectorData & data) { return data.dt == DataType::F32; },
66 REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_floor)
67 },
68 };
69
get_implementation(const FloorSelectorData & data)70 const FloorKernel *get_implementation(const FloorSelectorData &data)
71 {
72 for(const auto &uk : available_kernels)
73 {
74 if(uk.is_selected(data))
75 {
76 return &uk;
77 }
78 }
79 return nullptr;
80 }
81
validate_arguments(const ITensorInfo * input,const ITensorInfo * output)82 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
83 {
84 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
85
86 const auto *uk = get_implementation(FloorSelectorData{ input->data_type() });
87 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
88
89 // Validate in case of configured output
90 if(output->total_size() > 0)
91 {
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
94 }
95
96 return Status{};
97 }
98 } // namespace
99
configure(const ITensor * input,ITensor * output)100 void NEFloorKernel::configure(const ITensor *input, ITensor *output)
101 {
102 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
103
104 // Auto initialize output
105 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
106
107 // Validate
108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
109
110 _input = input;
111 _output = output;
112
113 // Configure kernel window
114 Window win = calculate_max_window(*input->info(), Steps());
115
116 Coordinates coord;
117 coord.set_num_dimensions(output->info()->num_dimensions());
118 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
119
120 INEKernel::configure(win);
121 }
122
validate(const ITensorInfo * input,const ITensorInfo * output)123 Status NEFloorKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
124 {
125 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
126 return Status{};
127 }
128
run(const Window & window,const ThreadInfo & info)129 void NEFloorKernel::run(const Window &window, const ThreadInfo &info)
130 {
131 ARM_COMPUTE_UNUSED(info);
132 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
133 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
134
135 Window win{ window };
136 win.set(Window::DimX, Window::Dimension(0, 1, 1));
137
138 const auto len = static_cast<int>(window.x().end()) - static_cast<int>(window.x().start());
139 const auto *uk = get_implementation(FloorSelectorData{ _input->info()->data_type() });
140
141 Iterator input(_input, win);
142 Iterator output(_output, win);
143
144 execute_window_loop(win, [&](const Coordinates &)
145 {
146 uk->ukernel(input.ptr(), output.ptr(), len);
147 },
148 input, output);
149 }
150 } // namespace arm_compute
151