DSPLIB User Guide
DSPLIB_fltoq15_ci.cpp
Go to the documentation of this file.
1 /******************************************************************************/
5 /* Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution.
18  *
19  * Neither the name of Texas Instruments Incorporated nor the names of
20  * its contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
36 /******************************************************************************
37  * Version 1.0 Date Aug 2023 Author: Asheesh Bhardwaj
38  *****************************************************************************/
39 /*******************************************************************************
40  *
41  * INCLUDES
42  *
43  ******************************************************************************/
44 
45 #include "../common/c71/DSPLIB_inlines.h"
46 #include "DSPLIB_fltoq15_priv.h"
47 
48 /**********************************************************************/
49 /* INITIALIZATION */
50 /**********************************************************************/
51 template <typename dataTypeIn> inline void fltoq15_stream_init(uint8_t *pBlock, int32_t blockSize);
52 
53 template <> inline void fltoq15_stream_init<float>(uint8_t *pBlock, int32_t blockSize)
54 {
55  typedef typename c7x::make_full_vector<float>::type vecIn;
56  int32_t eleCount = c7x::element_count_of<vecIn>::value;
57  typedef typename c7x::short_vec vecOut;
58  int32_t loopCount = blockSize / (eleCount * 2);
59 
60  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
61  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
62 
63  __SE_ELETYPE SE_ELETYPE = c7x::se_eletype<vecIn>::value;
64  __SE_VECLEN SE_VECLEN = c7x::se_veclen<vecIn>::value;
65  __SA_VECLEN SA_VECLEN = c7x::sa_veclen<vecOut>::value;
66 
67  /* Prepare streaming engine 0 to fetch the input */
68  se0Params.ICNT0 = eleCount;
69  se0Params.ICNT1 = loopCount;
70  se0Params.DIM1 = eleCount * 2;
71  se0Params.DIMFMT = __SE_DIMFMT_2D;
72  se0Params.ELETYPE = SE_ELETYPE;
73  se0Params.VECLEN = SE_VECLEN;
74 
75  /* Prepare SA template to store output */
76  sa0Params.ICNT0 = blockSize;
77  sa0Params.VECLEN = SA_VECLEN;
78  sa0Params.DIMFMT = __SA_DIMFMT_1D;
79 
80  *(__SE_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SE0_PARAM_OFFSET) = se0Params;
81  *(__SA_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SA0_PARAM_OFFSET) = sa0Params;
82 }
83 
84 template <> inline void fltoq15_stream_init<double>(uint8_t *pBlock, int32_t blockSize)
85 {
86 
87  typedef typename c7x::make_full_vector<double>::type vecIn;
88  int32_t eleCount = c7x::element_count_of<vecIn>::value;
89  typedef typename c7x::short_hvec vecOut;
90  int32_t loopCount = blockSize / (eleCount * 2);
91 
92  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
93  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
94 
95  __SE_ELETYPE SE_ELETYPE = c7x::se_eletype<vecIn>::value;
96  __SE_VECLEN SE_VECLEN = c7x::se_veclen<vecIn>::value;
97  __SA_VECLEN SA_VECLEN = c7x::sa_veclen<vecOut>::value;
98 
99  /* Prepare streaming engine 0 to fetch the input */
100  se0Params.ICNT0 = eleCount;
101  se0Params.ICNT1 = loopCount;
102  se0Params.DIM1 = eleCount * 2;
103  se0Params.DIMFMT = __SE_DIMFMT_2D;
104  se0Params.ELETYPE = SE_ELETYPE;
105  se0Params.VECLEN = SE_VECLEN;
106 
107  /* Prepare SA template to store output */
108  sa0Params.ICNT0 = blockSize;
109  sa0Params.VECLEN = SA_VECLEN;
110  sa0Params.DIMFMT = __SA_DIMFMT_1D;
111 
112  *(__SE_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SE0_PARAM_OFFSET) = se0Params;
113  *(__SA_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SA0_PARAM_OFFSET) = sa0Params;
114 }
115 
116 template <typename dataTypeIn>
118  const DSPLIB_bufParams1D_t *bufParamsIn,
119  const DSPLIB_bufParams1D_t *bufParamsOut,
120  const DSPLIB_fltoq15_InitArgs *pKerInitArgs)
121 {
122  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
123 
124  DSPLIB_STATUS status = DSPLIB_SUCCESS;
125  DSPLIB_fltoq15_PrivArgs *pKerPrivArgs = (DSPLIB_fltoq15_PrivArgs *) handle;
126  uint32_t blockSize = pKerPrivArgs->blockSize;
127  uint8_t *pBlock = pKerPrivArgs->bufPblock;
128 
129  typedef typename c7x::make_full_vector<dataTypeIn>::type vecIn;
130  int32_t eleCount = c7x::element_count_of<vecIn>::value;
131  int32_t loopCount = blockSize / (eleCount * 2);
132 
133  if (loopCount == 0) {
134  pKerPrivArgs->isSEReadEnabled = false;
135  }
136  else {
137  pKerPrivArgs->isSEReadEnabled = true;
138  fltoq15_stream_init<dataTypeIn>(pBlock, blockSize);
139  }
140 
141  DSPLIB_DEBUGPRINTFN(0, "isSEReadEnabled: %d\n", pKerPrivArgs->isSEReadEnabled);
142  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", status);
143 
144  return status;
145 }
147  const DSPLIB_bufParams1D_t *bufParamsIn,
148  const DSPLIB_bufParams1D_t *bufParamsOut,
149  const DSPLIB_fltoq15_InitArgs *pKerInitArgs);
151  const DSPLIB_bufParams1D_t *bufParamsIn,
152  const DSPLIB_bufParams1D_t *bufParamsOut,
153  const DSPLIB_fltoq15_InitArgs *pKerInitArgs);
154 
155 /**********************************************************************/
156 /* IMPLEMENTATION */
157 /**********************************************************************/
158 template <typename dataTypeIn>
159 inline void fltoq15_convert(void *restrict pIn,
160  void *restrict pOut,
161  __SE_TEMPLATE_v1 se0Params,
162  __SA_TEMPLATE_v1 sa0Params,
163  uint32_t blockSize,
164  bool isSEReadEnabled);
165 
166 template <>
167 inline void fltoq15_convert<float>(void *restrict pIn,
168  void *restrict pOut,
169  __SE_TEMPLATE_v1 se0Params,
170  __SA_TEMPLATE_v1 sa0Params,
171  uint32_t blockSize,
172  bool isSEReadEnabled)
173 {
174  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering template function for FLOAT32 to Q15 conversion");
175 
176  float *restrict pInLocal = (float *) pIn;
177  int16_t *restrict pOutLocal = (int16_t *) pOut;
178 
179  typedef typename c7x::make_full_vector<float>::type vecIn;
180  typedef typename c7x::short_vec vecOut;
181 
182  int32_t eleCount = c7x::element_count_of<vecIn>::value;
183  int32_t loopCount = blockSize / (eleCount * 2);
184  int32_t remainingElements = blockSize - (loopCount * eleCount * 2);
185  int32_t i;
186 
187  DSPLIB_DEBUGPRINTFN(0, "pInLocal: %p pOutLocal: %p blockSize: %d loopCount: %d remainingElements: %d\n", pInLocal,
188  pOutLocal, blockSize, loopCount, remainingElements);
189 
190  if (isSEReadEnabled) {
191  vecIn mulFactor = (vecIn) 32768.0;
192 
193  __SE0_OPEN(pInLocal, se0Params);
194  __SE1_OPEN(pInLocal + eleCount, se0Params);
195  __SA0_OPEN(sa0Params);
196 
197  for (i = 0; i < loopCount; i++) {
198  vecIn loadVec1 = c7x::strm_eng<0, vecIn>::get_adv();
199  vecIn loadVec2 = c7x::strm_eng<1, vecIn>::get_adv();
200 
201  vecIn mulLoadVec1 = loadVec1 * mulFactor;
202  vecIn mulLoadVec2 = loadVec2 * mulFactor;
203 
204  vecOut intermediate1 = __float_to_short(mulLoadVec1);
205  vecOut intermediate2 = __float_to_short(mulLoadVec2);
206 
207  c7x::short_vec *addr = c7x::strm_agen<0, c7x::short_vec>::get_adv(pOutLocal);
208  __vstore_packl_2src(addr, c7x::as_int_vec(intermediate1), c7x::as_int_vec(intermediate2));
209  }
210  __SE0_CLOSE();
211  __SE1_CLOSE();
212  __SA0_CLOSE();
213  }
214 
215  pInLocal += loopCount * eleCount * 2;
216  pOutLocal += loopCount * eleCount * 2;
217 
218 #pragma PROB_ITERATE(, 31)
219  for (i = 0; i < remainingElements; i++) {
220  float inData = *pInLocal++;
221  float mult = inData * 32768.00f;
222  short2 convertData = __float_to_short(mult);
223  *pOutLocal++ = convertData.s[0];
224  }
225  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Exiting template function for FLOAT32 to Q15 conversion");
226 }
227 
228 template <>
229 inline void fltoq15_convert<double>(void *restrict pIn,
230  void *restrict pOut,
231  __SE_TEMPLATE_v1 se0Params,
232  __SA_TEMPLATE_v1 sa0Params,
233  uint32_t blockSize,
234  bool isSEReadEnabled)
235 {
236  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering template function for FLOAT64 to Q15 conversion");
237 
238  double *restrict pInLocal = (double *) pIn;
239  int16_t *restrict pOutLocal = (int16_t *) pOut;
240 
241  typedef typename c7x::make_full_vector<double>::type vecIn;
242 
243  int32_t eleCount = c7x::element_count_of<vecIn>::value;
244  int32_t loopCount = blockSize / (eleCount * 2);
245  int32_t remainingElements = blockSize - (loopCount * eleCount * 2);
246  int32_t i;
247 
248  DSPLIB_DEBUGPRINTFN(0, "pInLocal: %p pOutLocal: %p blockSize: %d loopCount: %d remainingElements: %d\n", pInLocal,
249  pOutLocal, blockSize, loopCount, remainingElements);
250 
251  if (isSEReadEnabled) {
252  vecIn mulFactor = (vecIn) 32768.0;
253 
254  __SE0_OPEN(pInLocal, se0Params);
255  __SE1_OPEN(pInLocal + eleCount, se0Params);
256  __SA0_OPEN(sa0Params);
257 
258  for (i = 0; i < loopCount; i++) {
259 
260  vecIn loadVec1 = c7x::strm_eng<0, vecIn>::get_adv();
261  vecIn loadVec2 = c7x::strm_eng<1, vecIn>::get_adv();
262 
263  loadVec1 = loadVec1 * mulFactor;
264  loadVec2 = loadVec2 * mulFactor;
265 
266  c7x::int_vec dpToInt1 = __double_to_int(loadVec1);
267  c7x::int_vec dpToInt2 = __double_to_int(loadVec2);
268 
269  c7x::int_vec pack1 = __pack_consec_low(dpToInt2, dpToInt1);
270 
271  pack1 = __int_to_short_sat(pack1);
272 
273  c7x::short_hvec *addr = c7x::strm_agen<0, c7x::short_hvec>::get_adv(pOutLocal);
274  __vstore_packl(addr, c7x::as_int_vec(pack1));
275  }
276  __SE0_CLOSE();
277  __SE1_CLOSE();
278  __SA0_CLOSE();
279  }
280 
281  pInLocal += loopCount * eleCount * 2;
282  pOutLocal += loopCount * eleCount * 2;
283 #pragma PROB_ITERATE(, 15)
284  for (i = 0; i < remainingElements; i++) {
285  double inData = *pInLocal++;
286  double mult = inData * 32768.00;
287  int2 convTemp1 = __double_to_int(mult);
288  int2 convertData = __int_to_short_sat(convTemp1);
289  *pOutLocal++ = convertData.s[0];
290  }
291  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Exiting tempate function for FLOAT64 to Q15 conversion");
292 }
293 
294 template <typename dataTypeIn>
295 DSPLIB_STATUS DSPLIB_fltoq15_exec_ci(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
296 {
297  DSPLIB_DEBUGPRINTFN(0, "%s\n", "Entering function");
298 
299  DSPLIB_fltoq15_PrivArgs *pKerPrivArgs = (DSPLIB_fltoq15_PrivArgs *) handle;
300  uint32_t blockSize = pKerPrivArgs->blockSize;
301  bool isSEReadEnabled = pKerPrivArgs->isSEReadEnabled;
302 
303  __SE_TEMPLATE_v1 se0Params;
304  __SA_TEMPLATE_v1 sa0Params;
305 
306  uint8_t *pBlock = pKerPrivArgs->bufPblock;
307  se0Params = *(__SE_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SE0_PARAM_OFFSET);
308  sa0Params = *(__SA_TEMPLATE_v1 *) ((uint8_t *) pBlock + SE_SA0_PARAM_OFFSET);
309 
310  fltoq15_convert<dataTypeIn>(pIn, pOut, se0Params, sa0Params, blockSize, isSEReadEnabled);
311 
312  DSPLIB_DEBUGPRINTFN(0, "Exiting function with return status: %d\n", DSPLIB_SUCCESS);
313  return DSPLIB_SUCCESS;
314 }
316 DSPLIB_fltoq15_exec_ci<float>(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut);
317 
319 DSPLIB_fltoq15_exec_ci<double>(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut);
#define SE_SE0_PARAM_OFFSET
#define SE_SA0_PARAM_OFFSET
void fltoq15_stream_init(uint8_t *pBlock, int32_t blockSize)
void fltoq15_convert(void *restrict pIn, void *restrict pOut, __SE_TEMPLATE_v1 se0Params, __SA_TEMPLATE_v1 sa0Params, uint32_t blockSize, bool isSEReadEnabled)
void fltoq15_convert< float >(void *restrict pIn, void *restrict pOut, __SE_TEMPLATE_v1 se0Params, __SA_TEMPLATE_v1 sa0Params, uint32_t blockSize, bool isSEReadEnabled)
DSPLIB_STATUS DSPLIB_fltoq15_init_ci(DSPLIB_kernelHandle handle, const DSPLIB_bufParams1D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsOut, const DSPLIB_fltoq15_InitArgs *pKerInitArgs)
This function is the initialization function for the C7x implementation of the kernel....
void fltoq15_convert< double >(void *restrict pIn, void *restrict pOut, __SE_TEMPLATE_v1 se0Params, __SA_TEMPLATE_v1 sa0Params, uint32_t blockSize, bool isSEReadEnabled)
template DSPLIB_STATUS DSPLIB_fltoq15_exec_ci< double >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
DSPLIB_STATUS DSPLIB_fltoq15_exec_ci(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
This function is the main execution function for the C7x implementation of the kernel....
template DSPLIB_STATUS DSPLIB_fltoq15_exec_ci< float >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
template DSPLIB_STATUS DSPLIB_fltoq15_init_ci< float >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams1D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsOut, const DSPLIB_fltoq15_InitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_fltoq15_init_ci< double >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams1D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsOut, const DSPLIB_fltoq15_InitArgs *pKerInitArgs)
void fltoq15_stream_init< float >(uint8_t *pBlock, int32_t blockSize)
void fltoq15_stream_init< double >(uint8_t *pBlock, int32_t blockSize)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_fltoq15.
#define DSPLIB_DEBUGPRINTFN(N, fmt,...)
Definition: DSPLIB_types.h:83
DSPLIB_STATUS_NAME
The enumeration of all status codes.
Definition: DSPLIB_types.h:151
void * DSPLIB_kernelHandle
Handle type for DSPLIB operations.
Definition: DSPLIB_types.h:172
@ DSPLIB_SUCCESS
Definition: DSPLIB_types.h:152
A structure for a 1 dimensional buffer descriptor.
Structure containing the parameters to initialize the kernel.
Structure that is reserved for internal use by the kernel.
int32_t blockSize
Size of input buffer for different batches DSPLIB_fltoq15_init that will be retrieved and used by DSP...
bool isSEReadEnabled
Flag indicating if SE reads and stores are enabled, set by DSPLIB_fltoq15_init_ci that will be retrie...
uint8_t bufPblock[DSPLIB_FLTOQ15_IXX_IXX_OXX_PBLOCK_SIZE]