MATHLIB User Guide
MATHLIB_tanh_scalar.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2024 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 #ifndef MATHLIB_TANH_SCALAR_H_
35 #define MATHLIB_TANH_SCALAR_H_ 1
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 
43 #include "../common/MATHLIB_scalarTables.h"
44 #include "../common/MATHLIB_types.h"
45 #include "c6x_migration.h"
46 #include <float.h>
47 
48 static inline float divsp_tanhsp_i(float a, float b);
49 static inline float pol_est_tanhsp_i(float x);
50 static inline float exp_tanhsp_i(float x);
51 static inline float MATHLIB_tanh_scalar_ci(float x);
52 
53 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
54 #pragma CODE_SECTION(divsp_tanhsp_i, ".text:optci");
55 #endif
56 
57 /* ======================================================================== */
58 /* This function divides a by b. The return value is a/b. */
59 /* ======================================================================== */
60 
61 static inline float divsp_tanhsp_i(float a, float b)
62 {
63  const float two = 2.0f;
64  float x1, x2, y;
65 
66  x1 = _rcpsp(b);
67  x1 = x1 * (two - (b * x1));
68  x2 = x1 * (two - (b * x1));
69  y = a * x2;
70 
71  /* Commenting conditions for coverage- Conditions will never hit */
72  // if (a == 0.0f) {
73  // y = 0.0f;
74  // }
75 
76  // if (_fabsf(b) > FLT_MAX) {
77  // y = 0.0f;
78  // }
79 
80  return (y);
81 } /* End of divsp_tanhsp_i */
82 
83 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
84 #pragma CODE_SECTION(pol_est_tanhsp_i, ".text:optci");
85 #endif
86 
87 /* ======================================================================== */
88 /* Polynomial calculation to estimate the hyperbolic tangent funtion. */
89 /* The polynomial used is as follows: */
90 /* pol = x + c2x^3 + c4x^5 + c6x^7 + c8x^9 + c10x^11 + c12x^13 + */
91 /* c14x^15 + c16x^17, */
92 /* where x is the input, c2 through c16 are the corresponding coefficients */
93 /* to the polynomial, and pol is the result of the polynomial. This */
94 /* polynomial only covers a small range of inputs. */
95 /* ======================================================================== */
96 
97 static inline float pol_est_tanhsp_i(float x)
98 {
99  /* coefficients for the polynomial */
100  const float c16 = 0.000244528812992865f;
101  const float c14 = -0.00119005741172407f;
102  const float c12 = 0.00349212803657248f;
103  const float c10 = -0.00886323552990220f;
104  const float c8 = 0.0218794885361552f;
105  const float c6 = -0.0539682539682540f;
106  const float c4 = 0.133333333333333f;
107  const float c2 = -0.333333333333333f;
108 
109  float x2, x4, x6, x8, x10, x12;
110  float pol, tmp1, tmp2;
111 
112  /* calculate powers of x */
113  x2 = x * x;
114  x4 = x2 * x2;
115  x6 = x2 * x4;
116  x8 = x4 * x4;
117  x10 = x6 * x4;
118  x12 = x8 * x4;
119 
120  /* ====================================================================== */
121  /* The polynomial calculation is done in two seperate parts: */
122  /* tmp1 = c2x^2 + c4x^4 + c6x^6 + c8x^8 */
123  /* tmp2 = c10x^10 + c12x^12 + c14x^14 + c16x^16 */
124  /* In order to reduce the number of multiplications x is factored out of */
125  /* the polynomial and multiplied by later. */
126  /* ====================================================================== */
127 
128  tmp1 = ((c8 * x8) + (c6 * x6)) + ((c4 * x4) + (c2 * x2));
129  tmp2 = (((c16 * x4) + (c14 * x2) + c12) * x12) + (c10 * x10);
130 
131  pol = tmp1 + tmp2;
132  pol = (pol * x) + x;
133 
134  return pol;
135 } /* End of pol_est_tanhsp_i */
136 
137 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
138 #pragma CODE_SECTION(exp_tanhsp_i, ".text:optci");
139 #endif
140 
141 /* ====================================================================== */
142 /* The exp_tanhsp_i function returns the exponential value of a real */
143 /* floating-point argument x. The return value is e^x. */
144 /* ====================================================================== */
145 
146 static inline float exp_tanhsp_i(float x)
147 {
148  const float log2_base_x16 = 23.0831206542234f; /* 1.442695041 * 16.0 */
149  const float Half = 0.5f;
150  const float c0 = 0.166668549286041f;
151  const float c1 = 0.500016170012920f;
152  const float c2 = 0.999999998618401f;
153  const double p = 0.0433216987816623;
154  float pol, r, r2, r3, res;
155  unsigned int Ttemp, J, K;
156  float Nf;
157  int N;
158  double dT;
159 
160  /* Get N such that |N - x*16/ln(2)| is minimized */
161  Nf = (x * log2_base_x16) + Half;
162  N = (int) Nf; /* Cast from intermediate variable to appease MISRA */
163 
164  /* Commenting condition for coverage- Condition is always TRUE */
165  /* if ((x * log2_base_x16) < -Half) */ {
166  N--;
167  }
168 
169  /* Argument reduction, r, and polynomial approximation pol(r) */
170  r = (float) ((double) x - (p * (double) N));
171  r2 = r * r;
172  r3 = r * r2;
173 
174  pol = (r * c2) + ((r3 * c0) + (r2 * c1));
175 
176  /* Get index for ktable and jtable */
177  K = _extu((unsigned int) N, 28u, 30u);
178  J = (unsigned int) N & 0x3u;
179  dT = MATHLIB_kTable[K] * MATHLIB_jTable[J];
180 
181  /* Scale exponent to adjust for 2^M */
182  Ttemp = _hi(dT) + (((unsigned int) N >> 4) << 20);
183  dT = _itod(Ttemp, _lo(dT));
184 
185  res = (float) (dT * (1.0 + (double) pol));
186 
187  return (res);
188 } /* End of exp_tanhsp_i */
189 
190 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
191 #pragma CODE_SECTION(MATHLIB_tanh_scalar_ci, ".text:optci");
192 #endif
193 
194 /* ====================================================================== */
195 /* The type of calculation for tanh(x) depends on the value of x. */
196 /* x_abs < 1.0, res = pol_est_tanhsp_i(input x) */
197 /* */
198 /* x_abs >= 1.0, res = -1 + 2 / (1 + e^(-2x)) */
199 /* */
200 /* x_abs > 9.0, res = 1.0 (maximum value for tanh) */
201 /* where x_abs is the absolute value of the input and res is the */
202 /* calculated value for tanh(x). */
203 /* ====================================================================== */
204 
205 static inline float MATHLIB_tanh_scalar_ci(float x)
206 {
207  const float maxTanh = 1.0f;
208  const float limit = 9.0f;
209  const float negTwo = -2.0f;
210  const float pol_bound = 1.0f;
211  float sign = 1.0f;
212  float res, x_abs, temp;
213 
214  x_abs = _fabsf(x);
215 
216  /*res = maxTanh;*/ /* assume tanh reaches max value */
217 
218  if (x < 0.0f) {
219  sign = -sign;
220  }
221 
222  if (x_abs < pol_bound) { /* |x| < 1.0 */
223  res = pol_est_tanhsp_i(x_abs); /* estimation for very small input */
224  }
225  else if (x_abs <= limit) { /* 1.0 < |x|<= 9 */
226  temp = exp_tanhsp_i(negTwo * x_abs); /* e^(-2x) */
227  temp++;
228  res = divsp_tanhsp_i(2.0f, temp) - 1.0f; /* -1 + 2 / (1 + e^(-2x)) */
229  }
230  else { /* |x| > 9 */
231  res = maxTanh;
232  }
233 
234  return (res * sign); /* restore sign */
235 
236 } /* End of tanhsp_i */
237 
238 #endif // MATHLIB_TANH_SCALAR_H_
const double MATHLIB_kTable[4]
const double MATHLIB_jTable[4]
static float pol_est_tanhsp_i(float x)
static float MATHLIB_tanh_scalar_ci(float x)
static float divsp_tanhsp_i(float a, float b)
static float exp_tanhsp_i(float x)