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