DSPLIB User Guide
DSPLIB_recip_cn.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
3 **| **** |**
4 **| **** |**
5 **| ******o*** |**
6 **| ********_///_**** |**
7 **| ***** /_//_/ **** |**
8 **| ** ** (__/ **** |**
9 **| ********* |**
10 **| **** |**
11 **| *** |**
12 **| |**
13 **| Copyright (c) 2016 Texas Instruments Incorporated |**
14 **| ALL RIGHTS RESERVED |**
15 **| |**
16 **| Permission to use, copy, modify, or distribute this software, |**
17 **| whether in part or in whole, for any purpose is forbidden without |**
18 **| a signed licensing agreement and NDA from Texas Instruments |**
19 **| Incorporated (TI). |**
20 **| |**
21 **| TI makes no representation or warranties with respect to the |**
22 **| performance of this computer program, and specifically disclaims |**
23 **| any responsibility for any damages, special or consequential, |**
24 **| connected with the use of this program. |**
25 **| |**
26 **+--------------------------------------------------------------------------+**
27 *******************************************************************************/
28 
29 #include "DSPLIB_bufParams.h"
30 #include "DSPLIB_recip.h"
31 #include "DSPLIB_recip_priv.h"
32 #include "DSPLIB_types.h"
33 
34 template <typename dataType, int32_t dataIn>
35 DSPLIB_STATUS DSPLIB_recip_exec_cn(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
36 {
38 
39  DSPLIB_recip_PrivArgs *pKerPrivArgs = (DSPLIB_recip_PrivArgs *) handle;
40  uint32_t blockSize = pKerPrivArgs->blockSize;
41 
42 #if DSPLIB_DEBUGPRINT
43  printf("Enter DSPLIB_recip_exec_cn\n");
44 #endif
45 
46  dataType *pInLocal = (dataType *) pIn;
47  dataType *pOutLocal = (dataType *) pOut;
48 
49  dataType a, out;
50 
51  for (int32_t counter = 0; counter < blockSize; counter++) {
52  a = *pInLocal++;
53 #if DSPLIB_DEBUGPRINT
54  if (counter <= 8)
55  printf("counter %d a %d\n", counter, a);
56 #endif
57 
58  out = 1 / a;
59 #if DSPLIB_DEBUGPRINT
60  if (counter <= 8)
61  printf("counter %d y %d\n", counter, out);
62 #endif
63 
64  *pOutLocal++ = out;
65  }
66  return (status);
67 }
68 
69 // explicit instantiation for the different data type versions
70 template DSPLIB_STATUS
71 DSPLIB_recip_exec_cn<float, DSPLIB_FLOAT32>(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut);
72 
73 template DSPLIB_STATUS
74 DSPLIB_recip_exec_cn<double, DSPLIB_FLOAT64>(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut);
75 
76 // Input Buffer: Q15 format
77 // Output Buffer: 2x size of input, exp and frac
78 
79 template <>
81 DSPLIB_recip_exec_cn<int16_t, DSPLIB_INT16>(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
82 {
84 
85  DSPLIB_recip_PrivArgs *pKerPrivArgs = (DSPLIB_recip_PrivArgs *) handle;
86  int32_t blockSize = pKerPrivArgs->blockSize;
87 
88 #if DSPLIB_DEBUGPRINT
89  printf("Enter DSPLIB_recip_exec_cn\n");
90 #endif
91 
92  int16_t *pInLocal = (int16_t *) pIn;
93  int16_t *pOutLocal = (int16_t *) pOut;
94 
95  int32_t a, b;
96  int16_t neg, normal;
97 
98  // Horizontal stack store pattern
99  /* +++++++++++++++++++++++++++++++
100  | exp0 | exp1 | frac0 | frac1 |
101  +++++++++++++++++++++++++++++++
102  */
103 
104  if (pKerPrivArgs->initArgs.storeStyle == DSPLIB_HSTACK_ST) {
105  for (int32_t i = 0; i < blockSize; i++) {
106  a = *pInLocal++;
107 
108  /* printf("%d,", a); */
109  // take absolute value
110  if (a < 0) {
111  a = -a;
112  neg = 1;
113  }
114 
115  else {
116  neg = 0;
117  }
118 
119  // normalize
120  normal = __norm(a);
121  a = (int32_t)((uint32_t) a << (uint32_t)normal);
122 
123  // store exponent
124  *(pOutLocal) = normal - 15;
125 
126  // print exp
127  /* printf("%d,", *pOutLocal); */
128 
129  // dividend
130  b = 0x80000000U;
131 
132  // divide
133  for (int32_t j = 0; j < 15; j++) {
134  b = __sub_cond((uint32_t) b, (uint32_t) a);
135  }
136 
137  b = (int32_t)((uint32_t) b & (uint32_t)0x7FFF);
138 
139  if (neg){
140  b = -b;
141  }
142 
143  // store fraction
144  *(pOutLocal + blockSize) = b;
145 
146  // advance out pointer
147  pOutLocal++;
148  }
149  }
150 
151  // Interleaved store pattern
152  /* +++++++++++++++++++++++++++++++
153  | exp0 | frac0 | exp1 | frac1 |
154  +++++++++++++++++++++++++++++++
155  */
156 
157  else if (pKerPrivArgs->initArgs.storeStyle == DSPLIB_INTERLEAVE_ST) {
158  for (int32_t i = 0; i < blockSize; i++) {
159  a = *pInLocal++;
160 
161  // take absolute value
162  if (a < 0) {
163  a = -a;
164  neg = 1;
165  }
166 
167  else {
168  neg = 0;
169  }
170 
171  // normalize
172  normal = __norm(a);
173  a = (int32_t)((uint32_t) a << (uint32_t)normal);
174 
175  // store exponent
176  *pOutLocal++ = normal - 15;
177 
178  // dividend
179  b = 0x80000000U;
180 
181  // divide
182  for (int32_t j = 0; j < 15; j++) {
183  b = __sub_cond((uint32_t) b, (uint32_t) a);
184  }
185 
186  b = (int32_t)((uint32_t) b & (uint32_t)0x7FFF);
187 
188  if (neg){
189  b = -b;
190  }
191  // store fraction
192  *pOutLocal++ = b;
193  }
194  }
195 
196  else {
197  // return error because fixed point cannot perform "linear store"
199  }
200  return (status);
201 }
template DSPLIB_STATUS DSPLIB_recip_exec_cn< double, DSPLIB_FLOAT64 >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
DSPLIB_STATUS DSPLIB_recip_exec_cn(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
This function is the main execution function for the natural C implementation of the kernel....
DSPLIB_STATUS DSPLIB_recip_exec_cn< int16_t, DSPLIB_INT16 >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
template DSPLIB_STATUS DSPLIB_recip_exec_cn< float, DSPLIB_FLOAT32 >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_recip.
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
@ DSPLIB_ERR_NOT_IMPLEMENTED
Definition: DSPLIB_types.h:158
@ DSPLIB_HSTACK_ST
input is fixed point, store pattern is horizontal stack.
Definition: DSPLIB_recip.h:112
@ DSPLIB_INTERLEAVE_ST
input is fixed point, store pattern is interleaved.
Definition: DSPLIB_recip.h:115
DSPLIB_recip_storeStyle storeStyle
Fixed Point Output Store Style
Definition: DSPLIB_recip.h:126
Structure that is reserved for internal use by the kernel.
int32_t blockSize
Size of input buffer for different batches DSPLIB_recip_init that will be retrieved and used by DSPLI...
DSPLIB_recip_InitArgs initArgs