MATHLIB User Guide
MATHLIB_exp2.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 #define ELEMENT_COUNT(x) c7x::element_count_of<x>::value
35 
36 /******************************************************************************/
37 /* */
38 /* Includes */
39 /* */
40 /******************************************************************************/
41 
42 #include "MATHLIB_types.h"
43 #include "MATHLIB_utility.h"
44 
45 /******************************************************************************/
46 /* */
47 /* MATHLIB_exp2 */
48 /* */
49 /******************************************************************************/
50 template <typename vecType> static inline vecType MATHLIB_exp2_div(vecType a, vecType b)
51 {
52  vecType TWO = 2.0f;
53  vecType X;
54  X = __recip(b);
55  X = X * (TWO - (b * X));
56  X = X * (TWO - (b * X));
57  X = a * X;
58  return X;
59 }
60 
61 // this method performs exp2 compution of input vector
62 template <typename T> MATHLIB_STATUS MATHLIB_exp2(size_t length, T *pSrc, T *pDst)
63 {
64 
65  // variables
66  MATHLIB_STATUS status = MATHLIB_SUCCESS; // return function status
67  size_t numBlocks = 0; // compute loop's iteration count
68  size_t remNumBlocks = 0; // when numBlocks is not a multiple of SIMD width
69 
70  // derive c7x vector type from template typename
71  typedef typename c7x::make_full_vector<T>::type vec;
72 
73  __SE_TEMPLATE_v1 se0Params = __gen_SE_TEMPLATE_v1();
74  __SA_TEMPLATE_v1 sa0Params = __gen_SA_TEMPLATE_v1();
75 
76  // check for null pointers and non-zero length
77  status = MATHLIB_checkParams(length, pSrc, pDst);
78 
79  if (status == MATHLIB_SUCCESS) {
80 
81  MATHLIB_SE0SA01DSequentialInit(&se0Params, &sa0Params, length, pSrc, pDst);
82 
83  // calculate compute loop's iteration counter
84  numBlocks = length / c7x::element_count_of<vec>::value;
85  remNumBlocks = length % c7x::element_count_of<vec>::value;
86  if (remNumBlocks) {
87  numBlocks++;
88  }
89 
90  // open SE0, SE1, and SA0 for reading and writing operands
91  MATHLIB_SE0SA0Open(&se0Params, &sa0Params, pSrc);
92 
93  /***********************************************************************/
94  /* Create and assign values for constants employed on exp2 computation */
95  /***********************************************************************/
96 
97  vec zero, half, epsilon, minInput, maxVal, maxInput, A0, A1, B0, B1, CC1, CC2, ln2E, lnE2;
98 
99  zero = (vec) 0.0f;
100  half = (vec) 0.5f;
101  epsilon = (vec) 9.313225746e-10f; // [2^-29] / 2
102  minInput = (vec) -126.0f;
103  maxVal = (vec) 3.40282347e+38f;
104  maxInput = (vec) 128.0f;
105  A0 = (vec) 2.499999995e-1f;
106  A1 = (vec) 4.1602886268e-3f;
107  B0 = (vec) 0.5f;
108  B1 = (vec) 4.9987178778e-2f;
109  CC1 = (vec) 0.693359375f; // 355/512
110  CC2 = (vec) -2.12194440055e-4f; // lne(2) - 355/512
111  ln2E = (vec) 1.442695040889f; // ln(base 2) of e
112  lnE2 = (vec) 0.69314718056f; // log (base e) of 2
113 
114  // compute loop to perform vector exp2
115  for (size_t i = 0; i < numBlocks; i++) {
116  vec inVec = c7x::strm_eng<0, vec>::get_adv();
117 
118  /***********************************************************************/
119  /* Create and assign values for variables employed on exp2 computation */
120  /***********************************************************************/
121 
122  vec Y, X, W, R, S, B, C, D, absY;
123  c7x::int_vec N;
124  c7x::uint_vec uN, uNShift;
125 
126  Y = inVec * lnE2;
127 
128  absY = __abs(Y);
129 
130  // if (abs(Y) < epsilon) {
131  // Y = 0;
132  // }
133  __vpred cmp_Y = __cmp_lt_pred(absY, epsilon);
134  Y = __select(cmp_Y, zero, Y);
135 
136  // Convert base e --> base 2 argument
137  C = Y * ln2E;
138 
139  // Get unbiased exponent as int
140  N = __float_to_int(C);
141  S = __int_to_float(N);
142 
143  // Range reduction
144  X = (Y - (S * CC1)) - (S * CC2);
145  W = X * X;
146 
147  /**********************************************************************/
148  /* Calculate numerator and denominator */
149  /**********************************************************************/
150  B = (B1 * W) + B0;
151  D = ((A1 * W) + A0) * X;
152 
153  R = half + MATHLIB_exp2_div(D, (B - D));
154  uN = c7x::convert<c7x::uint_vec>(N);
155  uN = uN + 128;
156  uNShift = uN << 23u;
157  S = c7x::reinterpret<c7x::float_vec>(uNShift);
158 
159  // Scale final answer by power of 2
160  C = R * S;
161 
162  /**********************************************************************/
163  /* Bounds checking */
164  /**********************************************************************/
165 
166  // if (inVec < minInput) {
167  // C = 0.0f;
168  // }
169  __vpred cmp_min = __cmp_lt_pred(inVec, minInput);
170  C = __select(cmp_min, zero, C);
171 
172  // if (inVec > maxInput) {
173  // C = maxVal;
174  // }
175  __vpred cmp_max = __cmp_lt_pred(maxInput, inVec);
176  C = __select(cmp_max, maxVal, C);
177 
178  __vpred tmp = c7x::strm_agen<0, vec>::get_vpred();
179  vec *addr = c7x::strm_agen<0, vec>::get_adv(pDst);
180  __vstore_pred(tmp, addr, C);
181  }
182 
184  }
185 
186  return status;
187 }
188 
189 /******************************************************************************/
190 /* */
191 /* Explicit templatization for datatypes supported by MATHLIB_exp2 */
192 /* */
193 /******************************************************************************/
194 
195 // single precision
196 template MATHLIB_STATUS MATHLIB_exp2<float>(size_t length, float *pSrc, float *pDst);
197 
198 /******************************************************************************/
199 /* */
200 /* C-interface wrapper functions */
201 /* */
202 /******************************************************************************/
203 
204 extern "C" {
205 
206 // single-precision wrapper
207 MATHLIB_STATUS MATHLIB_exp2_sp(size_t length, float *pSrc, float *pDst)
208 {
209  MATHLIB_STATUS status = MATHLIB_exp2(length, pSrc, pDst);
210  return status;
211 }
212 
213 } // extern "C"
template MATHLIB_STATUS MATHLIB_exp2< float >(size_t length, float *pSrc, float *pDst)
static vecType MATHLIB_exp2_div(vecType a, vecType b)
MATHLIB_STATUS MATHLIB_exp2(size_t length, T *pSrc, T *pDst)
Performs the elementwise 2^x for all x in the input vector. Function can be overloaded with float poi...
MATHLIB_STATUS MATHLIB_exp2_sp(size_t length, float *pSrc, float *pDst)
This function is the C interface for MATHLIB_exp2. Function accepts float pointers.
static void MATHLIB_SE0SA0Close()
This method performs SE0 and SA0 close.
static void MATHLIB_SE0SA01DSequentialInit(__SE_TEMPLATE_v1 *se0Params, __SA_TEMPLATE_v1 *sa0Params, size_t length, T *pSrc, T *pDst)
static MATHLIB_STATUS MATHLIB_checkParams(size_t length, T *pSrc, T *pDst)
This method performs parameter checks for MATHLIB function.
static void MATHLIB_SE0SA0Open(__SE_TEMPLATE_v1 *se0Params, __SA_TEMPLATE_v1 *sa0Params, T *pSrc)
This method performs SE0 and SA0 open.
MATHLIB_STATUS_NAME
The enumeration of all status codes.
@ MATHLIB_SUCCESS