MATHLIB User Guide
MATHLIB_asinh_scalar.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  * MATHLIB -- TI Floating-Point Math Function Library *
3  * *
4  * *
5  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/ *
6  * *
7  * *
8  * Redistribution and use in source and binary forms, with or without *
9  * modification, are permitted provided that the following conditions *
10  * are met: *
11  * *
12  * Redistributions of source code must retain the above copyright *
13  * notice, this list of conditions and the following disclaimer. *
14  * *
15  * Redistributions in binary form must reproduce the above copyright *
16  * notice, this list of conditions and the following disclaimer in the *
17  * documentation and/or other materials provided with the *
18  * distribution. *
19  * *
20  * Neither the name of Texas Instruments Incorporated nor the names of *
21  * its contributors may be used to endorse or promote products derived *
22  * from this software without specific prior written permission. *
23  * *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
27  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
28  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
34  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
35  * ======================================================================== */
36 
37 /* ======================================================================= */
38 /* asinhsp_i.h - single precision floating point inverse hyperbolic sine */
39 /* optimized inlined C implementation (w/ intrinsics) */
40 /* ======================================================================= */
41 
42 #include "c7x_scalable.h"
43 #ifndef ASINHSP_I_
44 #define ASINHSP_I_ 1
45 
46 #include "../common/MATHLIB_scalarTables.h"
47 #include "../common/MATHLIB_types.h"
48 #include <c6x_migration.h>
49 #include <float.h>
50 
51 static inline float logsp_asinhsp_i(float x);
52 static inline float sqrtsp_asinhsp_i(float a, float x);
53 static inline float asinhsp_i(float x);
54 
55 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
56 #pragma CODE_SECTION(logsp_asinhsp_i, ".text:optci");
57 #endif
58 
59 /* ======================================================================== */
60 /* This function returns the logarithm value of a real floating-point */
61 /* argument x. The return value is the base e logarithmic value of x. */
62 /* ======================================================================== */
63 
64 static inline float logsp_asinhsp_i(float x)
65 {
66  /* coeffincients for the polynomial p(r) */
67  const float c1 = -0.2302894f;
68  const float c2 = 0.1908169f;
69  const float c3 = -0.2505905f;
70  const float c4 = 0.3333164f;
71  const float c5 = -0.5000002f;
72 
73  const double ln2 = 0.693147180559945;
74  const float max = 88.7228390519551f;
75  float pol, r1, r2, r3, r4, res;
76  double dr, frcpax, rcp, T;
77  unsigned int T_index;
78  int N;
79 
80  /* r = x * frcpa(x) -1 */
81  // debug test: valgrind error
82  // ===============================================
83  c7x::double_vec x_vec = (c7x::double_vec) x;
84  c7x::double_vec rcp_vec = __recip(x_vec);
85  rcp = rcp_vec.s[0];
86  /* rcp = _rcpdp((double) x); */
87  // ===============================================
88  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
89  dr = (frcpax * (double) x) - 1.0;
90 
91  /* calculate powers of r */
92  r1 = (float) dr;
93  r2 = r1 * r1;
94  r3 = r1 * r2;
95  r4 = r2 * r2;
96 
97  /* Polynomial p(r) that approximates ln(1+r) - r */
98  pol = (c5 * r2) + (c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4);
99 
100  /* Reconstruction: result = T + r + p(r) */
101  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
102  T_index = _extu(_hi(frcpax), 12u, 29u);
103  T = MATHLIB_logTable[T_index] - (ln2 * (double) N);
104  res = (float) (dr + T) + pol;
105 
106  if (x > FLT_MAX) {
107  res = max;
108  }
109 
110  return (res);
111 } /* logsp_asinhsp_i */
112 
113 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
114 #pragma CODE_SECTION(sqrtsp_asinhsp_i, ".text:optci");
115 #endif
116 
117 /* ======================================================================== */
118 /* This function returns the square root of the argument a. This function */
119 /* has been modified to return the argument x when a = x*x. The argument a */
120 /* is equal to x * x + 1, if a = x* x then +1 is irrelevant or x * x */
121 /* overflows and the real sqrt of a is lost. */
122 /* ======================================================================== */
123 
124 static inline float sqrtsp_asinhsp_i(float a, float x)
125 {
126  const float half = 0.5f;
127  const float OneP5 = 1.5f;
128  float x0, x1, x2, res, a_half;
129 
130  a_half = a * half;
131  // debug test: valgrind error
132  // ==============================================
133  c7x::float_vec a_vec = (c7x::float_vec) a;
134  c7x::float_vec x0_vec = __recip_sqrt(a_vec);
135  x0 = x0_vec.s[0];
136  /* x0 = _rsqrsp(a); */
137  // ===============================================
138 
139  x1 = OneP5 - (a_half * x0 * x0);
140  x1 = x0 * x1;
141  x2 = x1 * (OneP5 - (x1 * x1 * a_half));
142  res = a * x2;
143 
144  if (a == (x * x)) {
145  res = x;
146  }
147 
148  return res;
149 } /* End of sqrtsp_asinhsp_i */
150 
151 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
152 #pragma CODE_SECTION(asinhsp_i, ".text:optci");
153 #endif
154 
155 /* ======================================================================== */
156 /* The type of calculation for asinh(x) depends on the value of x: */
157 /* x_abs > 0.5, res = ln( x + sqrt (x^2 + 1)) */
158 /* This equation is modified as follows to avoid overflow for a large x; */
159 /* ln([x + sqrt(x^2 + 1)]/2] + ln(2) */
160 /* */
161 /* x_abs <= 0.5, res = polynomial estimation (input x) */
162 /* where x_abs is the absolute value of the input and res is the calculated */
163 /* value for asinh(x) */
164 /* */
165 /* The polynomial used is as follows: */
166 /* pol = x + c2 x^3 + c4 x^5 + c6 x^7 */
167 /* where x is the input, c2 through c6 are the corresponding coefficients */
168 /* for the polynomial, and pol is the result of the polynomial. */
169 /* ======================================================================== */
170 
171 static inline float asinhsp_i(float x)
172 {
173  const float ln2 = 0.69314718056f; /* ln(2) */
174  const float pol_bound = 0.5f;
175  const float half = 0.5f;
176  float sign = 1.0f;
177 
178  /* Coefficients for the polynomial */
179  const float c2 = -0.166605362341955f;
180  const float c4 = 0.0734464812833510f;
181  const float c6 = -0.0330279320352987f;
182 
183  float res, sqrt_, pol;
184  float x2, x4, x6, x_abs;
185  float temp;
186 
187  // debug test: valgrind error
188  //======================================================
189  /* x_abs = _fabsf(x); /\* |x| *\/ */
190  c7x::float_vec x_vec = (c7x::float_vec) x;
191  c7x::float_vec x_abs_vec = __abs(x_vec);
192  x_abs = x_abs_vec.s[0];
193  x2 = x * x; /* x^2 */
194  //======================================================
195 
196  if (x < 0.0f) {
197  sign = -sign;
198  }
199 
200  /* powers of x */
201  x4 = x2 * x2;
202  x6 = x4 * x2;
203 
204  /* use polynomial estimation for |x| <= 0.5 */
205  /* polynomial to estimate asinh(x) for small inputs*/
206  pol = (x2 * c2) + (x4 * c4) + (x6 * c6);
207  pol = (pol * x) + x;
208 
209  /* assume |x| > 0.5 and calculate asinh(x) */
210  sqrt_ = sqrtsp_asinhsp_i(x2 + 1.0f, x_abs); /* sqrt(x^2 + 1) */
211 
212  /* prevent overflow for large x, log(2x) where x > max/2 */
213  temp = (sqrt_ * half) + (x_abs * half); /* (x+sqrt(x^2 + 1))/2 */
214  res = logsp_asinhsp_i(temp) + ln2; /* ln((x + sqrt(x^2 + 1))/2) +ln(2) */
215  res = res * sign; /* restore sign */
216 
217  /* set the result to the right value depending on the input value */
218  if (x_abs <= pol_bound) {
219  res = pol;
220  }
221 
222  return res;
223 }
224 
225 #endif /* _ASINHSP_H_ */
226 
227 /* ======================================================================== */
228 /* End of file: asinhsp_i .h */
229 /* ======================================================================== */
static float asinhsp_i(float x)
static float sqrtsp_asinhsp_i(float a, float x)
static float logsp_asinhsp_i(float x)
const double MATHLIB_logTable[8]