MATHLIB User Guide
MATHLIB_atanh_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_ATANH_SCALAR_H_
35 #define MATHLIB_ATANH_SCALAR_H_
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 #include "../common/MATHLIB_scalarTables.h"
43 #include "../common/MATHLIB_types.h"
44 #include "c6x_migration.h"
45 #include <float.h>
46 
47 /******************************************************************************/
48 /* */
49 /* Scalar/C6x implementation of atanh */
50 /* */
51 /******************************************************************************/
52 
53 static inline float logsp_atanhsp_i(float x);
54 static inline float divsp_atanhsp_i(float a, float b);
55 static inline float MATHLIB_atanh_scalar_ci(float x);
56 
57 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
58 #pragma CODE_SECTION(logsp_atanhsp_i, ".text:optci");
59 #endif
60 
61 /* ======================================================================== */
62 /* This function returns the logarithm value of a real floating-point */
63 /* argument x. The return value is the base e logarithmic value of x. */
64 /* ======================================================================== */
65 
66 static inline float logsp_atanhsp_i(float x)
67 {
68  /* coefficients for the polynomial p(r) */
69  const float c1 = -0.2302894f;
70  const float c2 = 0.1908169f;
71  const float c3 = -0.2505905f;
72  const float c4 = 0.3333164f;
73  const float c5 = -0.5000002f;
74 
75  const double ln2 = 0.693147180559945;
76  float pol, r1, r2, r3, r4, res;
77  double dr, frcpax, rcp, T;
78  unsigned int T_index;
79  int N;
80 
81  /* r = x * frcpa(x) -1 */
82  rcp = _rcpdp((double) x);
83  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
84  dr = (frcpax * (double) x) - 1.0;
85 
86  /* calculate power of r */
87  r1 = (float) dr;
88  r2 = r1 * r1;
89  r3 = r1 * r2;
90  r4 = r2 * r2;
91 
92  /* Polynomial p(r) that approximates ln(1+r) - r */
93  pol = (c5 * r2) + ((c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4));
94 
95  /* Reconstruction: result = T + r + p(r) */
96  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
97  T_index = _extu(_hi(frcpax), 12u, 29u);
98  T = MATHLIB_logTable[T_index] - (ln2 * (double) N);
99  res = (float) (dr + T) + pol;
100 
101  return (res);
102 } /* End of logsp_atanhsp_i */
103 
104 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
105 #pragma CODE_SECTION(divsp_atanhsp_i, ".text:optci");
106 #endif
107 
108 /* ======================================================================= */
109 /* This function returns the division result of two real floating-point */
110 /* values, a and b. The return value is a/b. */
111 /* ======================================================================= */
112 
113 static inline float divsp_atanhsp_i(float a, float b)
114 {
115  const float two = 2.0f;
116  float x1, x2, res;
117 
118  x1 = _rcpsp(b);
119  x1 = x1 * (two - (b * x1));
120  x2 = x1 * (two - (b * x1));
121  res = a * x2;
122 
123  return (res);
124 } /* End of divsp_atanhsp_i */
125 
126 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
127 #pragma CODE_SECTION(MATHLIB_atanh_scalar_ci, ".text:optci");
128 #endif
129 
130 /* ======================================================================== */
131 /* The type of calculation for atanh(x) depends on the value of x: */
132 /* x_abs > 0.1, res = 0.5 * ln((1 + x)/(1 - x)) */
133 /* */
134 /* x_abs <= 0.1, res = polynomial estimation (input x) */
135 /* where x_abs is the absolute value of the input and res is the calculated */
136 /* value for atanh(x) */
137 /* */
138 /* The polynomial used is as follows: */
139 /* pol = x + c2 x^3 + c4 x^5 */
140 /* where x is the input, c2 through c4 are the corresponding coefficients */
141 /* for the polynomial, and pol is the result of the polynomial. */
142 /* ======================================================================== */
143 
144 static inline float MATHLIB_atanh_scalar_ci(float x)
145 {
146  const float pol_bound = 0.1f;
147  const float half = 0.5f;
148  const float limit = 1.0f;
149  float sign = 1.0f;
150 
151  /* Coefficients for the polynomial */
152  const float c2 = 0.333327051f;
153  const float c4 = 0.202017226f;
154 
155  float res, temp1, temp2, x_abs;
156  float x2, x4, pol, div_;
157 
158  x_abs = _fabsf(x);
159 
160  if (x < 0.0f) {
161  sign = -sign;
162  }
163 
164  /* set the result to the right value depending on the input value */
165  if (x_abs <= pol_bound) {
166  /* powers of x */
167  x2 = x * x;
168  x4 = x2 * x2;
169 
170  /* use polynomial estimation for |x| <= 0.1 */
171  /* polynomial to estimate atanh(x) for small inputs */
172  pol = (c2 * x2) + (c4 * x4);
173  pol = (pol * x_abs) + x_abs;
174  res = pol;
175  }
176  else {
177  /* calculate atanh(x) based on atanh(x) = 0.5*ln((1+ x) / (1- x)) */
178  temp1 = 1.0f + x_abs;
179  temp2 = 1.0f - x_abs;
180 
181  div_ = divsp_atanhsp_i(temp1, temp2); /* (1+ x) / (1- x) */
182  res = logsp_atanhsp_i(div_); /* ln((1 + x)/(1 - x)) */
183  res = res * half; /* divide by 2*/
184  }
185 
186  if (x_abs == limit) {
187  res = _itof(0x7F800000u); /* INF */
188  }
189 
190  res = res * sign; /* restore sign */
191  return res;
192 }
193 
194 #endif /* MATHLIB_ATANH_SCALAR_H_ */
195 
196 /* ======================================================================== */
197 /* End of file: MATHLIB_atanh_scalar.h */
198 /* ======================================================================== */
static float divsp_atanhsp_i(float a, float b)
static float logsp_atanhsp_i(float x)
static float MATHLIB_atanh_scalar_ci(float x)
const double MATHLIB_logTable[8]