MATHLIB User Guide
MATHLIB_sinh_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_SINH_SCALAR_H_
35 #define MATHLIB_SINH_SCALAR_H_
36 
37 #include "../common/MATHLIB_scalarTables.h"
38 #include "../common/MATHLIB_types.h"
39 #include "c6x_migration.h"
40 #include <float.h>
41 
42 static inline float expsp_sinhsp_i(float x);
43 static inline float recipsp_sinhsp_i(float a);
44 static inline float pol_est_sinhsp_i(float x);
45 static inline float MATHLIB_sinh_scalar_ci(float x);
46 
47 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
48 #pragma CODE_SECTION(expsp_sinhsp_i, ".text:optci");
49 #endif
50 
51 /* ======================================================================= */
52 /* This function returns the exponential value of a real floating-point */
53 /* argument. The return value is (e^x)/2. */
54 /* ======================================================================= */
55 
56 static inline float expsp_sinhsp_i(float x)
57 {
58  const float log2_base_x16 = 23.0831206542234f; /*1.442695041 * 16.0*/
59  const float Half = 0.5f;
60  const float LnMax = 88.72283905f;
61  const float Ln2 = 0.693147180559945f; /* log(2) */
62  const double p = 0.0433216987816623; /* 1/log2_base_x16 */
63 
64  /* coefficients to approximate the decimal part of the result */
65  const float c0 = 0.166668549286041f;
66  const float c1 = 0.500016170012920f;
67  const float c2 = 0.999999998618401f;
68 
69  float pol, r, r2, r3, res;
70  unsigned int Ttemp, J, K;
71  float Nf;
72  int N;
73  double dT;
74 
75  /* Get N such that |N - x*16/ln(2)| is minimized */
76  Nf = (x * log2_base_x16) + Half;
77  N = (int) Nf; /* Cast from intermediate variable to appease MISRA */
78 
79  /* Commenting condition for coverage- Condition will never hit */
80  // if ((x * log2_base_x16) < -Half) {
81  // N--;
82  // }
83 
84  /* Argument reduction, r, and polynomial approximation pol(r) */
85  r = (float) ((double) x - (p * (double) N));
86  r2 = r * r;
87  r3 = r * r2;
88 
89  pol = (r * c2) + ((r3 * c0) + (r2 * c1));
90  /* substract 16 in order to get (e^x)/2 as a result */
91  N = N - 16;
92 
93  /* Get index for ktable and jtable */
94  K = _extu((unsigned int) N, 28u, 30u);
95  J = (unsigned int) N & 0x3u;
96  dT = MATHLIB_kTable[K] * MATHLIB_jTable[J];
97 
98  /* Scale exponent to adjust for 2^M */
99  Ttemp = _hi(dT) + (((unsigned int) N >> 4) << 20);
100  dT = _itod(Ttemp, _lo(dT));
101 
102  res = (float) (dT * (1.0 + (double) pol));
103 
104  /* > LnMax returns INF */
105  /* Ln2 adjusts the new range for exp(x)/2 */
106  if ((x - Ln2) > LnMax) {
107  res = _itof(0x7F800000u);
108  }
109 
110  return (res);
111 } /* expsp_sinhsp_i */
112 
113 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
114 #pragma CODE_SECTION(recipsp_sinhsp_i, ".text:optci");
115 #endif
116 
117 /* ======================================================================= */
118 /* This function returns the reciprocral of a real floating-point value a. */
119 /* The return value is 1/a. */
120 /* ======================================================================= */
121 
122 static inline float recipsp_sinhsp_i(float a)
123 {
124  const float two = 2.0f;
125  float y;
126 
127  y = _rcpsp(a); /* 1/b */
128  y = y * (two - (a * y));
129  y = y * (two - (a * y));
130 
131  /* Commenting conditions for coverage- Conditions will never hit */
132  // if (a == 0.0f) {
133  // y = 0.0f;
134  // }
135 
136  // if (_fabsf(a) > FLT_MAX) {
137  // y = 0.0f;
138  // }
139 
140  return (y);
141 } /* divsp_sinhsp_i */
142 
143 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
144 #pragma CODE_SECTION(pol_est_sinhsp_i, ".text:optci");
145 #endif
146 
147 /* ======================================================================== */
148 /* Polynomial calculation to estimate the hyperbolic sine funtion. */
149 /* The polynomial used is as follows: */
150 /* pol = x + c2 x^3 + c4 x^5 + c6 x^7 + c8 x^9 */
151 /* where x is the input, c2 through c8 are the corresponding coefficients */
152 /* to the polynomial, and pol is the result of the polynomial. This */
153 /* polynomial only covers inputs in the range [-1, 1]. */
154 /* ======================================================================== */
155 
156 static inline float pol_est_sinhsp_i(float x)
157 {
158  /* coefficients for the polynomial */
159  const float c8 = 2.75573192239859e-6f;
160  const float c6 = 0.000198412698412698f;
161  const float c4 = 0.00833333333333333f;
162  const float c2 = 0.166666666666667f;
163 
164  float x2, x4, x6, x8, pol;
165 
166  /* calculate the power of x */
167  x2 = x * x;
168  x4 = x2 * x2;
169  x6 = x2 * x4;
170  x8 = x4 * x4;
171 
172  /* ====================================================================== */
173  /* In order to reduce the number of multiplications x is factored out of */
174  /* the polynomial and multiplied by later. */
175  /* pol = c2 x^2 + c4 x^4 + c6 x^6 + c8 x^8 */
176  /* ====================================================================== */
177 
178  pol = ((c2 * x2) + (c4 * x4)) + ((c6 * x6) + (c8 * x8));
179  pol = (pol * x) + x;
180 
181  return pol;
182 } /* pol_est_sinhsp_i */
183 
184 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
185 #pragma CODE_SECTION(MATHLIB_sinh_scalar_ci, ".text:optci");
186 #endif
187 
188 /* ====================================================================== */
189 /* The type of calculation for sinh(x) depends on the value of x: */
190 /* */
191 /* for x_abs <= 1, res = pol_est_sinhsp_i (input x) */
192 /* for x_abs > 16, res = sign * expsp_sinhsp_i (input x_abs), */
193 /* e^-|x| is negligible */
194 /* for 1 < x_abs <= 16, res = (e^x - e^-x)/2, */
195 /* e^x = 2 * expsp_sinhsp_i (input x) */
196 /* where x_abs is the absolute value of the input, sign has a value of 1 */
197 /* or -1 depending on the sign of the input, and res is the value */
198 /* for sinh(x). */
199 /* ====================================================================== */
200 
201 static inline float MATHLIB_sinh_scalar_ci(float x)
202 {
203  const float half = 0.5f;
204  const float bound = 16.0f;
205  const float Max = 89.41598629f;
206  const float pol_bound = 1.0f; /* polynomial boundary */
207  float sign = 1.0f;
208  float res, temp, exp_, x_abs;
209 
210  x_abs = _fabsf(x);
211 
212  if (x < 0.0f) {
213  sign = -sign; /* sign variable */
214  }
215 
216  /* e^-|x| is negligible */
217  if (x_abs > bound) { /* |x| > 16 */
218  res = expsp_sinhsp_i(x_abs); /* res = sign * (e^x)/2 */
219  }
220  else if (x_abs <= pol_bound) { /* |x| <= 1 */
221  res = pol_est_sinhsp_i(x_abs);
222  }
223  else { /* 1 < |x| <= 16 */
224  exp_ = 2.0f * expsp_sinhsp_i(x_abs); /* e^x */
225  temp = recipsp_sinhsp_i(exp_); /* e^-x */
226  res = (exp_ - temp) * half; /* (e^x - e^-x)/2 */
227  }
228 
229  if (x_abs > Max) { /* reached max */
230  res = _itof(0x7F800000u);
231  }
232 
233  return res * sign;
234 } /* sinhsp_i */
235 
236 #endif // MATHLIB_SINH_SCALAR_H_
const double MATHLIB_kTable[4]
const double MATHLIB_jTable[4]
static float MATHLIB_sinh_scalar_ci(float x)
static float recipsp_sinhsp_i(float a)
static float pol_est_sinhsp_i(float x)
static float expsp_sinhsp_i(float x)