MATHLIB User Guide
MATHLIB_acosh_scalar.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2023 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_ACOSH_SCALAR_H_
35 #define MATHLIB_ACOSH_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 acosh */
51 /* */
52 /******************************************************************************/
53 
54 static inline float logsp_acoshsp_i(float x);
55 static inline float sqrtsp_acoshsp_i(float a, float x);
56 static inline float MATHLIB_acosh_scalar_ci(float x);
57 
58 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
59 #pragma CODE_SECTION(logsp_acoshsp_i, ".text:optci");
60 #endif
61 
62 /* ======================================================================== */
63 /* This function returns the logarithm value of a real floating-point */
64 /* argument x. The return value is the base e logarithmic value of x. */
65 /* ======================================================================== */
66 
67 static inline float logsp_acoshsp_i(float x)
68 {
69  /* coefficients for the polynomial p(r) */
70  const float c1 = -0.2302894f;
71  const float c2 = 0.1908169f;
72  const float c3 = -0.2505905f;
73  const float c4 = 0.3333164f;
74  const float c5 = -0.5000002f;
75 
76  const double ln2 = 0.693147180559945;
77  const float max = 88.7228390519551f;
78  float pol, r1, r2, r3, r4, res;
79  double dr, frcpax, rcp, T;
80  unsigned int T_index;
81  int N;
82 
83  /* r = x * frcpa(x) -1 */
84  rcp = _rcpdp((double) x);
85  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
86  dr = (frcpax * (double) x) - 1.0;
87 
88  /* calculate powers of r */
89  r1 = (float) dr;
90  r2 = r1 * r1;
91  r3 = r1 * r2;
92  r4 = r2 * r2;
93 
94  /* Polynomial p(r) that approximates ln(1+r) - r */
95  pol = (c5 * r2) + ((c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4));
96 
97  /* Reconstruction: result = T + r + p(r) */
98  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
99  T_index = _extu(_hi(frcpax), 12u, 29u);
100  T = MATHLIB_logTable[T_index] - (ln2 * (double) N);
101  res = (float) (dr + T) + pol;
102 
103  if (x > FLT_MAX) {
104  res = max;
105  }
106 
107  return (res);
108 } /* logsp_acoshsp_i */
109 
110 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
111 #pragma CODE_SECTION(sqrtsp_acoshsp_i, ".text:optci");
112 #endif
113 
114 /* ======================================================================== */
115 /* This function returns the square root of the argument a. This function */
116 /* has been modified to return the argument x when a = x*x. The argument a */
117 /* is equal to x * x + 1, if a = x* x then +1 is irrelevant or x * x */
118 /* overflows and the real sqrt of a is lost. */
119 /* ======================================================================== */
120 
121 static inline float sqrtsp_acoshsp_i(float a, float x)
122 {
123  const float half = 0.5f;
124  const float OneP5 = 1.5f;
125  float x0, x1, x2, x_half, res;
126 
127  x_half = a * half;
128  x0 = _rsqrsp(a);
129 
130  x1 = OneP5 - (x_half * x0 * x0);
131  x1 = x0 * x1;
132  x2 = x1 * (OneP5 - (x1 * x1 * x_half));
133  res = a * x2;
134 
135  if (a == (x * x)) {
136  res = x;
137  }
138 
139  if (a == 0.0f) {
140  res = 0.0f;
141  }
142 
143  return res;
144 } /* End of sqrtsp_acoshsp_i */
145 
146 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
147 #pragma CODE_SECTION(MATHLIB_acosh_scalar_ci, ".text:optci");
148 #endif
149 
150 /* ======================================================================== */
151 /* This function returns the value of acosh(x), which is estimated as */
152 /* follows: */
153 /* res = ln( x + sqrt (x^2 + 1)) */
154 /* where x is the input, sqrt stands for the square root function, and */
155 /* res is the calculated value for acosh(x). This equation is modified as */
156 /* follows to avoid overflow for a large x; */
157 /* ln([x + sqrt(x^2 + 1)]/2] + ln(2) */
158 /* ======================================================================== */
159 
160 static inline float MATHLIB_acosh_scalar_ci(float x)
161 {
162  const float ln2 = 0.69314718056f; /* ln(2) */
163  const float half = 0.5f;
164 
165  float res, sqrt_, temp, x2;
166 
167  x2 = x * x; /* x^2 */
168 
169  sqrt_ = sqrtsp_acoshsp_i(x2 - 1.0f, x); /* sqrt(x^2 + 1) */
170  temp = (sqrt_ * half) + (x * half); /* (x+sqrt(x^2 + 1))/2 */
171  res = logsp_acoshsp_i(temp) + ln2; /* ln((x + sqrt(x^2 + 1))/2) +ln(2) */
172 
173  if (x < 1.0f) {
174  res = _itof(0x7FFFFFFFu); /* NaN */
175  }
176 
177  return res;
178 }
179 
180 #endif /* ACOSHSP_H_ */
181 
182 /* ======================================================================== */
183 /* End of file: acoshsp_i.h */
184 /* ======================================================================== */
static float logsp_acoshsp_i(float x)
static float sqrtsp_acoshsp_i(float a, float x)
static float MATHLIB_acosh_scalar_ci(float x)
const double MATHLIB_logTable[8]