VXLIB User Guide
VXLIB_add.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_add_priv.h"
41 
42 /**********************************************************************************************************************/
43 /* */
44 /* VXLIB_add_getHandleSize */
45 /* */
46 /**********************************************************************************************************************/
47 
48 // this method calculates and returns the size of the handle for the VXLIB_add kernel
50 {
51  int32_t privBufSize = sizeof(VXLIB_add_PrivArgs);
52  return privBufSize;
53 }
54 
55 /**********************************************************************************************************************/
56 /* */
57 /* VXLIB_add_init_checkParams */
58 /* */
59 /**********************************************************************************************************************/
60 
61 // this method checks the initialization parameters for the VXLIB_add kernel
64  const VXLIB_bufParams2D_t *bufParamsIn0,
65  const VXLIB_bufParams2D_t *bufParamsIn1,
66  const VXLIB_bufParams2D_t *bufParamsOut,
67  const VXLIB_add_InitArgs *pKerInitArgs)
68 {
69  VXLIB_STATUS status = VXLIB_SUCCESS;
70 
71  // obtain input0 buffer parameters
72  uint32_t dTypeIn0 = bufParamsIn0->data_type;
73  uint32_t widthIn0 = bufParamsIn0->dim_x;
74  uint32_t heightIn0 = bufParamsIn0->dim_y;
75 
76  // obtain input1 buffer parameters
77  uint32_t dTypeIn1 = bufParamsIn1->data_type;
78  uint32_t widthIn1 = bufParamsIn1->dim_x;
79  uint32_t heightIn1 = bufParamsIn1->dim_y;
80 
81  // obtain output buffer parameters
82  uint32_t dTypeOut = bufParamsOut->data_type;
83  uint32_t widthOut = bufParamsOut->dim_x;
84  uint32_t heightOut = bufParamsOut->dim_y;
85 
86  uint32_t strideIn0Elements = bufParamsIn0->stride_y / VXLIB_sizeof(bufParamsIn0->data_type);
87  uint32_t strideIn1Elements = bufParamsIn1->stride_y / VXLIB_sizeof(bufParamsIn1->data_type);
88  uint32_t strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
89 
90  // check for dimensions and datatype combinations
91  if (handle == NULL) {
92  status = VXLIB_ERR_NULL_POINTER;
93  }
94  else if ((widthIn0 != widthIn1) || (widthIn0 != widthOut) || (heightIn0 != heightIn1) || (heightIn0 != heightOut)) {
96  }
97  else if (strideIn0Elements < widthIn0 || strideIn1Elements < widthIn1 || strideOutElements < widthOut) {
99  }
102  status = VXLIB_ERR_INVALID_TYPE;
103  }
104  else {
105  status = VXLIB_SUCCESS;
106  }
107 
108  return status;
109 }
110 
111 /**********************************************************************************************************************/
112 /* */
113 /* VXLIB_add_exec_checkParams */
114 /* */
115 /**********************************************************************************************************************/
116 
117 // this method checks the execution parameters for the VXLIB_add kernel
119  const void *restrict pIn0,
120  const void *restrict pIn1,
121  const void *restrict pOut)
122 {
123  VXLIB_STATUS status;
124 
125 #if VXLIB_DEBUGPRINT
126  printf("Enter VXLIB_add_exec_checkParams\n");
127 #endif
128  if ((handle == NULL) || (pIn0 == NULL) || (pIn1 == NULL) || (pOut == NULL)) {
129  status = VXLIB_ERR_NULL_POINTER;
130  }
131  else {
132  status = VXLIB_SUCCESS;
133  }
134 
135  return status;
136 }
137 
138 /**********************************************************************************************************************/
139 /* */
140 /* VXLIB_add_init */
141 /* */
142 /**********************************************************************************************************************/
143 
144 // this method is the user-level initialization function for the VXLIB_add kernel
146  VXLIB_bufParams2D_t *bufParamsIn0,
147  VXLIB_bufParams2D_t *bufParamsIn1,
148  VXLIB_bufParams2D_t *bufParamsOut,
149  const VXLIB_add_InitArgs *pKerInitArgs)
150 {
151  VXLIB_STATUS status = VXLIB_SUCCESS;
152  VXLIB_add_PrivArgs *pKerPrivArgs = (VXLIB_add_PrivArgs *) handle;
153 
154  // copy pKerinitArgs into pKerPrivargs
155  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
156 
157  // set width and height of image
158  pKerPrivArgs->width = bufParamsIn0->dim_x;
159  pKerPrivArgs->height = bufParamsIn0->dim_y;
160 
161  // compute stride in elements as SE/SA params are set to work with given element type
162  pKerPrivArgs->strideIn0Elements = bufParamsIn0->stride_y / VXLIB_sizeof(bufParamsIn0->data_type);
163  pKerPrivArgs->strideIn1Elements = bufParamsIn1->stride_y / VXLIB_sizeof(bufParamsIn1->data_type);
164  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
165 
166 #if VXLIB_DEBUGPRINT
167  printf("VXLIB_DEBUGPRINT Enter VXLIB_add_init\n");
168 #endif
169 
170  // obtain buffer datatypes
171  uint32_t dTypeIn0 = bufParamsIn0->data_type;
172  uint32_t dTypeIn1 = bufParamsIn1->data_type;
173  uint32_t dTypeOut = bufParamsOut->data_type;
174 
175  // determine natural C vs optimized function call
176  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
177 
178  // set function pointer for natural C function with appropriate template parameters based on datatypes
179  if (VXLIB_ADD_I8U_I8U_O8U) {
181  }
182  else if (VXLIB_ADD_I8U_I8U_O16S) {
184  }
185  else if (VXLIB_ADD_I8U_I16S_O16S) {
187  }
188  else if (VXLIB_ADD_I16S_I16S_O16S) {
190  }
191  else {
192  status = VXLIB_ERR_INVALID_TYPE;
193  }
194  }
195  else { // Optimized function
196 
197  // set function pointer for natural C function with appropriate template parameters based on datatypes
198  if (VXLIB_ADD_I8U_I8U_O8U) {
200  status = VXLIB_add_init_ci<VXLIB_ADD_DTYPE_I8U_I8U_O8U>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut,
201  pKerInitArgs);
202  }
203  else if (VXLIB_ADD_I8U_I8U_O16S) {
205  status = VXLIB_add_init_ci<VXLIB_ADD_DTYPE_I8U_I8U_O16S>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut,
206  pKerInitArgs);
207  }
208  else if (VXLIB_ADD_I8U_I16S_O16S) {
210  status = VXLIB_add_init_ci<VXLIB_ADD_DTYPE_I8U_I16S_O16S>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut,
211  pKerInitArgs);
212  }
213  else if (VXLIB_ADD_I16S_I16S_O16S) {
215  status = VXLIB_add_init_ci<VXLIB_ADD_DTYPE_I16S_I16S_O16S>(handle, bufParamsIn0, bufParamsIn1, bufParamsOut,
216  pKerInitArgs);
217  }
218  else {
219  status = VXLIB_ERR_INVALID_TYPE;
220  }
221  }
222 
223  return status;
224 }
225 
226 /**********************************************************************************************************************/
227 /* */
228 /* VXLIB_add_exec */
229 /* */
230 /**********************************************************************************************************************/
231 
232 // this method is the user-level execution function for the VXLIB_add kernel
233 VXLIB_STATUS VXLIB_add_exec(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
234 {
235  VXLIB_STATUS status;
236 
237 #if VXLIB_DEBUGPRINT
238  printf("VXLIB_DEBUGPRINT Enter VXLIB_add_exec\n");
239 #endif
240 
241  VXLIB_add_PrivArgs *pKerPrivArgs = (VXLIB_add_PrivArgs *) handle;
242 
243  status = pKerPrivArgs->execute(handle, pIn0, pIn1, pOut);
244 
245  return status;
246 }
template VXLIB_STATUS VXLIB_add_init_ci< VXLIB_ADD_DTYPE_I16S_I16S_O16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn0, const VXLIB_bufParams2D_t *bufParamsIn1, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_add_init_ci< VXLIB_ADD_DTYPE_I8U_I8U_O8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn0, const VXLIB_bufParams2D_t *bufParamsIn1, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_add_init_ci< VXLIB_ADD_DTYPE_I8U_I8U_O16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn0, const VXLIB_bufParams2D_t *bufParamsIn1, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_add_exec_ci< VXLIB_ADD_TYPENAME_I8U_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_ci< VXLIB_ADD_TYPENAME_I8U_I16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_init_ci< VXLIB_ADD_DTYPE_I8U_I16S_O16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn0, const VXLIB_bufParams2D_t *bufParamsIn1, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_add_exec_ci< VXLIB_ADD_TYPENAME_I16S_I16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_ci< VXLIB_ADD_TYPENAME_I8U_I8U_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_cn< VXLIB_ADD_TYPENAME_I8U_I8U_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_cn< VXLIB_ADD_TYPENAME_I8U_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_cn< VXLIB_ADD_TYPENAME_I8U_I16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
template VXLIB_STATUS VXLIB_add_exec_cn< VXLIB_ADD_TYPENAME_I16S_I16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_add.
#define VXLIB_ADD_I8U_I8U_O8U
Macros that will be useful to check for datatype combinations.
#define VXLIB_ADD_I8U_I8U_O16S
#define VXLIB_ADD_I8U_I16S_O16S
#define VXLIB_ADD_I16S_I16S_O16S
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_STATUS VXLIB_add_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn0, VXLIB_bufParams2D_t *bufParamsIn1, VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_add_exec function is called. This function takes care...
Definition: VXLIB_add.cpp:145
VXLIB_STATUS VXLIB_add_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn0, const VXLIB_bufParams2D_t *bufParamsIn1, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_add_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_add_init function....
Definition: VXLIB_add.cpp:63
int32_t VXLIB_add_getHandleSize(VXLIB_add_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
Definition: VXLIB_add.cpp:49
VXLIB_STATUS VXLIB_add_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn0, const void *restrict pIn1, const void *restrict pOut)
This function checks the validity of the parameters passed to VXLIB_add_exec function....
Definition: VXLIB_add.cpp:118
VXLIB_STATUS VXLIB_add_exec(VXLIB_kernelHandle handle, void *restrict pIn0, void *restrict pIn1, void *restrict pOut)
This function is the main kernel compute function.
Definition: VXLIB_add.cpp:233
Structure containing the parameters to initialize the kernel.
Definition: VXLIB_add.h:82
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
Definition: VXLIB_add.h:84
Structure that is reserved for internal use by the kernel.
VXLIB_add_InitArgs pKerInitArgs
Initargs of the kernel.
size_t strideIn0Elements
Stride of input0 in elements.
size_t strideIn1Elements
Stride of input1 in elements.
pFxnVXLIB_add_exec execute
Function pointer to point to the right execution variant between VXLIB_add_exec_cn and VXLIB_add_exec...
size_t width
Width of image
size_t height
Height of image
size_t strideOutElements
Stride of output in elements.
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.