Compute Library
 21.02
NETableLookupKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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  */
25 
26 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/ILut.h"
31 #include "arm_compute/core/Utils.h"
33 
34 #include <cstddef>
35 #include <cstdint>
36 
37 using namespace arm_compute;
38 
39 namespace arm_compute
40 {
41 class Coordinates;
42 
43 constexpr unsigned int num_num_elems_processed_per_iteration = 16;
44 } // namespace arm_compute
45 
47  : _func(nullptr), _lut(nullptr)
48 {
49 }
50 
51 template <class T>
52 void NETableLookupKernel::tableLookup(const Window &window)
53 {
54  uint32_t offset = _lut->index_offset();
55  size_t count = _lut->num_elements();
56  const auto lut = reinterpret_cast<const T *>(_lut->buffer());
58 
59  ARM_COMPUTE_ERROR_ON(lut == nullptr);
60 
61  Iterator input = Iterator(_input, window);
62  Iterator output = Iterator(_output, window);
63 
64  execute_window_loop(window, [&](const Coordinates &)
65  {
66  auto input_ptr = reinterpret_cast<const T *>(input.ptr());
67  auto output_ptr = reinterpret_cast<T *>(output.ptr());
68 
69  for(unsigned int i = 0; i < step; ++i, ++input_ptr, ++output_ptr)
70  {
71  const int32_t index = offset + *input_ptr;
72 
73  if(0 <= index && index < static_cast<int32_t>(count))
74  {
75  *output_ptr = lut[index];
76  }
77  }
78  },
79  input, output);
80 }
81 
82 namespace arm_compute
83 {
84 template <>
85 void NETableLookupKernel::tableLookup<uint8_t>(const Window &window)
86 {
87  const uint8_t *const lut = _lut->buffer();
89 
90  ARM_COMPUTE_ERROR_ON(lut == nullptr);
91 
92  Iterator input = Iterator(_input, window);
93  Iterator output = Iterator(_output, window);
94 
95  execute_window_loop(window, [&](const Coordinates &)
96  {
97  const uint8_t *input_ptr = input.ptr();
98  uint8_t *output_ptr = output.ptr();
99 
100  for(unsigned int i = 0; i < step; ++i)
101  {
102  *output_ptr++ = lut[*input_ptr++];
103  }
104  },
105  input, output);
106 }
107 } // namespace arm_compute
108 
109 void NETableLookupKernel::configure(const ITensor *input, const ILut *lut, ITensor *output)
110 {
111  ARM_COMPUTE_ERROR_ON(input == nullptr);
112  ARM_COMPUTE_ERROR_ON(lut == nullptr);
113  ARM_COMPUTE_ERROR_ON(output == nullptr);
117 
118  _lut = lut;
119 
120  if(input->info()->data_type() == DataType::U8 && output->info()->data_type() == DataType::U8)
121  {
122  _func = &NETableLookupKernel::tableLookup<uint8_t>;
123  }
124  else if(input->info()->data_type() == DataType::S16 && output->info()->data_type() == DataType::S16)
125  {
126  _func = &NETableLookupKernel::tableLookup<int16_t>;
127  }
128  else
129  {
130  ARM_COMPUTE_ERROR("Unsupported combination of input and output DataType.");
131  }
132 
133  INESimpleKernel::configure(input, output, num_num_elems_processed_per_iteration);
134 }
135 
136 void NETableLookupKernel::run(const Window &window, const ThreadInfo &info)
137 {
138  ARM_COMPUTE_UNUSED(info);
141  ARM_COMPUTE_ERROR_ON(_func == nullptr);
142  (this->*_func)(window);
143 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
constexpr unsigned int num_num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Interface for Neon tensor.
Definition: ITensor.h:36
NETableLookupKernel()
Default constructor.
virtual size_t num_elements() const =0
Returns the total number of elements in the LUT.
Copyright (c) 2017-2021 Arm Limited.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void configure(const ITensor *input, const ILut *lut, ITensor *output)
Initialise the kernel&#39;s input, lut and output.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
Lookup Table object interface.
Definition: ILut.h:34
Coordinates of an item.
Definition: Coordinates.h:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
virtual uint32_t index_offset() const =0
Indicates the offset that needs to be applied to the raw index before performing a lookup in the LUT...
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
constexpr int step
Definition: fp32.cpp:35
virtual uint8_t * buffer() const =0
Returns a pointer to the start of the LUT.
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
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205