VXLIB User Guide
VXLIB_tableLookup.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  ******************************************************************************/
33 
34 /**********************************************************************************************************************/
35 /* */
36 /* INCLUDES */
37 /* */
38 /**********************************************************************************************************************/
39 
40 #include "VXLIB_tableLookup_priv.h"
41 #include "VXLIB_types.h"
42 #include <cstdint>
43 
44 /**********************************************************************************************************************/
45 /* */
46 /* VXLIB_tableLookup_getHandleSize */
47 /* */
48 /**********************************************************************************************************************/
49 
50 // this method calculates and returns the size of the handle for the VXLIB_tableLookup kernel
52 {
53  int32_t privBufSize = sizeof(VXLIB_tableLookup_PrivArgs);
54  return privBufSize;
55 }
56 
57 /**********************************************************************************************************************/
58 /* */
59 /* VXLIB_tableLookup_init_checkParams */
60 /* */
61 /**********************************************************************************************************************/
62 
63 // this method checks the initialization parameters for the VXLIB_tableLookup kernel
66  const VXLIB_bufParams2D_t * bufParamsIn,
67  const VXLIB_bufParams2D_t * bufParamsOut,
68  const VXLIB_bufParams1D_t * bufParamsLut,
69  const VXLIB_tableLookup_InitArgs *pKerInitArgs)
70 {
71  VXLIB_STATUS status = VXLIB_SUCCESS;
72 
73  // check for null handles
74  if (handle == NULL || bufParamsIn == NULL || bufParamsOut == NULL || bufParamsLut == NULL || pKerInitArgs == NULL) {
75  status = VXLIB_ERR_NULL_POINTER;
76  }
77  // check for valid datatype combinations
78  else {
79  uint32_t strideInElements = (uint32_t)(bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type));
80  uint32_t strideOutElements = (uint32_t)(bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type));
81  // uint32_t inDimX = bufParamsIn->dim_x;
82  // uint32_t outDimX = bufParamsOut->dim_x;
83 
84  if (((bufParamsIn->data_type != VXLIB_UINT8) || (bufParamsOut->data_type != VXLIB_UINT8) ||
85  (bufParamsLut->data_type != VXLIB_UINT8) || (pKerInitArgs->count > 256U)) &&
86  ((bufParamsIn->data_type != VXLIB_INT8) || (bufParamsOut->data_type != VXLIB_INT8) ||
87  (bufParamsLut->data_type != VXLIB_INT8) || (pKerInitArgs->count > 256U)) &&
88  ((bufParamsIn->data_type != VXLIB_UINT16) || (bufParamsOut->data_type != VXLIB_UINT16) ||
89  (bufParamsLut->data_type != VXLIB_UINT16) || (pKerInitArgs->count > 65536U)) &&
90  ((bufParamsIn->data_type != VXLIB_INT16) || (bufParamsOut->data_type != VXLIB_INT16) ||
91  (bufParamsLut->data_type != VXLIB_INT16) || (pKerInitArgs->count > 65536U)))
92  {
93  status = VXLIB_ERR_INVALID_TYPE;
94  }
95  else if ((bufParamsIn->dim_x != bufParamsOut->dim_x) || (bufParamsIn->dim_y != bufParamsOut->dim_y))
96  {
98  }
99  // check valid dimensions; input height, width must be greater, equal to filter size for non padded implementation
100  else if ((strideInElements < bufParamsIn->dim_x) || (strideOutElements < bufParamsOut->dim_x))
101  {
103  }
104  else
105  {
106  status = VXLIB_SUCCESS;
107  }
108  }
109  return status;
110 }
111 
112 /**********************************************************************************************************************/
113 /* */
114 /* VXLIB_tableLookup_exec_checkParams */
115 /* */
116 /**********************************************************************************************************************/
117 
118 // this method checks the execution parameters for the VXLIB_tableLookup kernel
119 
120 // this method checks the execution parameters for the VXLIB_tableLookup kernel
122  const void *restrict pIn,
123  const void *restrict pOut,
124  const void *restrict pLut)
125 {
126  VXLIB_STATUS status;
127 
128 #if VXLIB_DEBUGPRINT
129  printf("Enter VXLIB_tableLookup_exec_checkParams\n");
130 #endif
131  if ((handle == NULL) || (pIn == NULL) || (pOut == NULL) || (pLut == NULL)) {
132  status = VXLIB_ERR_NULL_POINTER;
133  }
134  else {
135  status = VXLIB_SUCCESS;
136  }
137 
138  return status;
139 }
140 
141 /**********************************************************************************************************************/
142 /* */
143 /* VXLIB_tableLookup_init */
144 /* */
145 /**********************************************************************************************************************/
146 
147 // this method is the user-level initialization function for the VXLIB_tableLookup kernel
149  VXLIB_bufParams2D_t * bufParamsIn,
150  VXLIB_bufParams2D_t * bufParamsOut,
151  VXLIB_bufParams1D_t * bufParamsLut,
152  const VXLIB_tableLookup_InitArgs *pKerInitArgs)
153 {
154  VXLIB_STATUS status = VXLIB_SUCCESS;
155  VXLIB_tableLookup_PrivArgs *pKerPrivArgs = (VXLIB_tableLookup_PrivArgs *) handle;
156 
157  // copy pKerinitArgs into pKerPrivargs
158  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
159 
160  // set priv args
161  pKerPrivArgs->width = bufParamsIn->dim_x;
162  pKerPrivArgs->height = bufParamsIn->dim_y;
163 
164  // compute stride in elements as SE/SA params are set to work with given element type
165  pKerPrivArgs->strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
166  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
167 
168 #if VXLIB_DEBUGPRINT
169  printf("VXLIB_DEBUGPRINT Enter VXLIB_tableLookup_init\n");
170 #endif
171 
172  // obtain buffer datatypes
173  uint32_t dTypeIn = bufParamsIn->data_type;
174  uint32_t dTypeOut = bufParamsOut->data_type;
175  uint32_t dTypeLut = bufParamsLut->data_type;
176 
177  // determine natural C vs optimized function call
178  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
179  // set function pointer for natural C function with appropriate template parameters based on datatypes
183  }
187  }
191  }
195  }
196  else {
197  status = VXLIB_ERR_INVALID_TYPE;
198  }
199  }
200  else { // Optimized function
201 
203  status = VXLIB_tableLookup_init_ci<VXLIB_TABLELOOKUP_DTYPE_I8U_O8U>(handle, bufParamsIn, bufParamsOut, bufParamsLut,
204  pKerInitArgs);
207  }
209  status = VXLIB_tableLookup_init_ci<VXLIB_TABLELOOKUP_DTYPE_I8S_O8S>(handle, bufParamsIn, bufParamsOut, bufParamsLut,
210  pKerInitArgs);
213  }
215  status = VXLIB_tableLookup_init_ci<VXLIB_TABLELOOKUP_DTYPE_I16U_O16U>(handle, bufParamsIn, bufParamsOut, bufParamsLut,
216  pKerInitArgs);
219  }
221  status = VXLIB_tableLookup_init_ci<VXLIB_TABLELOOKUP_DTYPE_I16S_O16S>(handle, bufParamsIn, bufParamsOut, bufParamsLut,
222  pKerInitArgs);
225  }
226  else {
227  status = VXLIB_ERR_INVALID_TYPE;
228  }
229  }
230 
231  return status;
232 }
233 
234 /**********************************************************************************************************************/
235 /* */
236 /* VXLIB_tableLookup_exec */
237 /* */
238 /**********************************************************************************************************************/
239 
240 // this method is the user-level execution function for the VXLIB_tableLookup kernel
242 VXLIB_tableLookup_set(VXLIB_kernelHandle handle, void *restrict pLut)
243 {
244  VXLIB_STATUS status;
245 
246  VXLIB_tableLookup_PrivArgs *pKerPrivArgs = (VXLIB_tableLookup_PrivArgs *) handle;
247 
248  status = pKerPrivArgs->lutSet(handle, pLut);
249 
250  return status;
251 }
252 
254 VXLIB_tableLookup_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
255 {
256  VXLIB_STATUS status;
257 
258 #if VXLIB_DEBUGPRINT
259  printf("VXLIB_DEBUGPRINT Enter VXLIB_tableLookup_exec\n");
260 #endif
261 
262  VXLIB_tableLookup_PrivArgs *pKerPrivArgs = (VXLIB_tableLookup_PrivArgs *) handle;
263 
264  status = pKerPrivArgs->execute(handle, pIn, pOut, pLut);
265 
266  return status;
267 }
268 /* ======================================================================== */
269 /* End of file: VXLIB_tableLookup.cpp */
270 /* ======================================================================== */
template VXLIB_STATUS VXLIB_tableLookup_set_ci< int16_t >(VXLIB_kernelHandle handle, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_set_ci< int8_t >(VXLIB_kernelHandle handle, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_exec_ci< uint16_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_exec_ci< int8_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_init_ci< VXLIB_TABLELOOKUP_DTYPE_I16U_O16U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_tableLookup_set_ci< uint8_t >(VXLIB_kernelHandle handle, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_exec_ci< int16_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_init_ci< VXLIB_TABLELOOKUP_DTYPE_I16S_O16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_tableLookup_init_ci< VXLIB_TABLELOOKUP_DTYPE_I8U_O8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_tableLookup_set_ci< uint16_t >(VXLIB_kernelHandle handle, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_init_ci< VXLIB_TABLELOOKUP_DTYPE_I8S_O8S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_tableLookup_exec_ci< uint8_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict lut)
template VXLIB_STATUS VXLIB_tableLookup_exec_cn< uint16_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_set_cn< int8_t >(VXLIB_kernelHandle handle, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_exec_cn< int16_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_set_cn< uint16_t >(VXLIB_kernelHandle handle, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_exec_cn< int8_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_exec_cn< uint8_t >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_set_cn< int16_t >(VXLIB_kernelHandle handle, void *restrict pLut)
template VXLIB_STATUS VXLIB_tableLookup_set_cn< uint8_t >(VXLIB_kernelHandle handle, void *restrict pLut)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_tableLookup.
#define VXLIB_TABLELOOKUP_I16U_I16U_O16U
#define VXLIB_TABLELOOKUP_I8S_I8S_O8S
#define VXLIB_TABLELOOKUP_I16S_I16S_O16S
#define VXLIB_TABLELOOKUP_I8U_I8U_O8U
Macros that will be useful to check for datatype combinations.
void * VXLIB_kernelHandle
Handle type for VXLIB operations.
Definition: VXLIB_types.h:247
VXLIB_STATUS_NAME
The enumeration of all status codes.
Definition: VXLIB_types.h:220
@ VXLIB_ERR_INVALID_DIMENSION
Definition: VXLIB_types.h:225
@ VXLIB_ERR_NOT_EQUAL_WIDTH_STRIDE
Definition: VXLIB_types.h:228
@ VXLIB_ERR_NULL_POINTER
Definition: VXLIB_types.h:226
@ VXLIB_SUCCESS
Definition: VXLIB_types.h:221
@ VXLIB_ERR_INVALID_TYPE
Definition: VXLIB_types.h:224
@ VXLIB_FUNCTION_NATC
Definition: VXLIB_types.h:251
static int32_t VXLIB_sizeof(uint32_t type)
Inline function returns number of bytes per element given a type of VXLIB_data_type_e.
@ VXLIB_UINT16
@ VXLIB_INT16
@ VXLIB_INT8
@ VXLIB_UINT8
VXLIB_STATUS VXLIB_tableLookup_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pOut, const void *restrict pLut)
This function checks the validity of the parameters passed to VXLIB_tableLookup_exec function....
VXLIB_STATUS VXLIB_tableLookup_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn, VXLIB_bufParams2D_t *bufParamsOut, VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_tableLookup_exec function is called....
VXLIB_STATUS VXLIB_tableLookup_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut, void *restrict pLut)
This function is the main kernel compute function.
VXLIB_STATUS VXLIB_tableLookup_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams1D_t *bufParamsLut, const VXLIB_tableLookup_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_tableLookup_init function....
int32_t VXLIB_tableLookup_getHandleSize(VXLIB_tableLookup_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
VXLIB_STATUS VXLIB_tableLookup_set(VXLIB_kernelHandle handle, void *restrict pLut)
This function place the lookup table in suitable memory location.
A structure for a 1 dimensional buffer descriptor.
uint32_t data_type
Values are of type VXLIB_data_type_e.
A structure for a 2 dimensional buffer descriptor.
uint32_t dim_y
Height of buffer in Y dimension in elements.
uint32_t dim_x
Width of buffer in X dimension in elements.
uint32_t data_type
Values are of type VXLIB_data_type_e.
int32_t stride_y
Stride in Y dimension in bytes.
Structure containing the parameters to initialize the kernel.
uint32_t count
Parameter indicating size of lookup table
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
Structure that is reserved for internal use by the kernel.
pFxnVXLIB_tableLookup_set lutSet
size_t strideOutElements
Stride of output in elements.
VXLIB_tableLookup_InitArgs pKerInitArgs
Initargs of the kernel.
size_t strideInElements
Stride of input in elements.
pFxnVXLIB_tableLookup_exec execute
Function pointer to point to the right execution variant between VXLIB_tableLookup_exec_cn and VXLIB_...