MATHLIB User Guide
MATHLIB_log10_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_LOG10_SCALAR_H_
35 #define MATHLIB_LOG10_SCALAR_H_
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 
43 #include "../common/MATHLIB_scalarTables.h"
44 #include "../common/MATHLIB_types.h"
45 #include "../common/MATHLIB_utility.h"
46 #include "c6x_migration.h"
47 
48 /******************************************************************************/
49 /* */
50 /* Scalar/C6x implementation of log10 */
51 /* */
52 /******************************************************************************/
53 
54 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
55 #pragma CODE_SECTION(MATHLIB_log10_scalar_ci, ".text:optci");
56 #endif
57 
58 template <typename T> static inline T MATHLIB_log10_scalar_ci(T a);
59 
60 template <> inline float MATHLIB_log10_scalar_ci(float a)
61 {
62  double ln2 = 0.693147180559945f;
63  double base = 0.4342944819033f;
64  float c1 = -0.2302894f;
65  float c2 = 0.1908169f;
66  float c3 = -0.2505905f;
67  float c4 = 0.3333164f;
68  float c5 = -0.5000002f;
69  float MAXe = 3.402823466E+38f;
70  float pol, r1, r2, r3, r4, res;
71  double dr, frcpax, rcp, T;
72  unsigned int T_index;
73  int N;
74 
75  /* r = x * frcpa(x) -1 */
76  rcp = _rcpdp((double) a);
77  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
78  dr = (frcpax * (double) a) - 1.0;
79 
80  /* Polynomial p(r) that approximates ln(1+r) - r */
81  r1 = (float) dr;
82  r2 = r1 * r1;
83  r3 = r1 * r2;
84  r4 = r2 * r2;
85 
86  pol = (c5 * r2) + ((c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4));
87  pol *= (float) base;
88 
89  /* Reconstruction: result = T + r + p(r) */
90  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
91  T_index = _extu(_hi(frcpax), 12u, 29u);
92  T = (MATHLIB_logTable[T_index] - (ln2 * (double) N)) * base;
93  res = (float) ((dr * base) + T) + pol;
94 
95  if (a <= 0.0f) {
96  res = _itof(0xFF800000u);
97  }
98  if (a > MAXe) {
99  res = 308.2547f;
100  }
101 
102  return (res);
103 }
104 
105 inline double divdpMod_log10dpi(double a, double b) { return cmn_DIVDP(a, b); }
106 
107 template <> inline double MATHLIB_log10_scalar_ci(double a)
108 {
109  double Half = 0.5;
110  double MAXe = 1.7976931348623157e+308;
111  double srHalf = 0.70710678118654752440; /* sqrt(0.5) */
112  double MINe = 2.2250738585072014e-308;
113  double a0 = -0.64124943423745581147e+2;
114  double a1 = 0.16383943563021534222e+2;
115  double a2 = -0.78956112887491257267e+0;
116  double b0 = -0.76949932108494879777e+3;
117  double b1 = 0.31203222091924532844e+3;
118  double b2 = -0.35667977739034646171e+2; /* Note b3 = 1.0 */
119  double c1 = 0.693359375; /* 355/512 */
120  double c2 = -2.121944400546905827679e-4;
121  double c10e = 0.43429448190325182765; /* log (base 10) of e */
122  double W, X, Y, Z;
123  double zn, zd;
124  double Rz, Sa, Bd, Cn, Da;
125  int N, exp_;
126  unsigned int upper;
127 
128  /* get unbiased exponent */
129  Y = a;
130  exp_ = (int) _extu(_hi(Y), 1u, 21u);
131  N = exp_ - 1022;
132 
133  /* force DP exp = 1022 if not zero */
134  upper = _clr(_hi(Y), 20u, 31u);
135  upper = 0x3fe00000u | upper;
136  Z = _itod(upper, _lo(Y));
137 
138  if (exp_ == 0) {
139  Z = 0.0;
140  }
141 
142  if (Z > srHalf) {
143  zn = (Z - Half) - Half;
144  zd = (Z * Half) + Half;
145  }
146  else {
147  zn = Z - Half;
148  zd = (zn * Half) + Half;
149  N = N - 1;
150  }
151 
152  X = divdpMod_log10dpi(zn, zd);
153  W = X * X;
154  Bd = ((((W + b2) * W) + b1) * W) + b0;
155  Cn = (((W * a2) + a1) * W) + a0;
156  Rz = W * divdpMod_log10dpi(Cn, Bd);
157  Sa = X + (X * Rz);
158  Cn = (double) N;
159  Da = ((Cn * c2) + Sa) + (Cn * c1);
160  Da = c10e * Da;
161 
162  if (Y < MINe) {
163  Da = -MAXe;
164  }
165  if (Y > MAXe) {
166  Da = 308.254715974092;
167  }
168 
169  return (Da);
170 }
171 
172 extern "C" {
173 static inline float MATHLIB_log10_scalar_sp(float a)
174 {
175  float result = MATHLIB_log10_scalar_ci<float>(a);
176  return result;
177 }
178 
179 static inline double MATHLIB_log10_scalar_dp(double a, double b)
180 {
181  double result = MATHLIB_log10_scalar_ci<double>(a);
182  return result;
183 }
184 }
185 
186 #endif // MATHLIB_LOG2_SCALAR_H_
static T MATHLIB_log10_scalar_ci(T a)
static float MATHLIB_log10_scalar_sp(float a)
static double MATHLIB_log10_scalar_dp(double a, double b)
double divdpMod_log10dpi(double a, double b)
const double MATHLIB_logTable[8]
static double cmn_DIVDP(double a, double b)