MATHLIB User Guide
MATHLIB_exp10_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_EXP10_SCALAR_H_
35 #define MATHLIB_EXP10_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 
47 /******************************************************************************/
48 /* */
49 /* Scalar/C6x implementation of exp */
50 /* */
51 /******************************************************************************/
52 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
53 #pragma CODE_SECTION(MATHLIB_exp10_scalar_ci, ".text:optci");
54 #endif
55 
56 inline float MATHLIB_exp10_scalar_ci(float a)
57 {
58  float log2_base_x16 = 16.0f * 3.321928095f;
59  float Halfe = 0.5f;
60  float LnMine = -87.33654475f;
61  float LnMaxe = 88.72283905f;
62  float Maxe = 3.402823466E+38f;
63  float c0 = 0.1667361910f;
64  float c1 = 0.4999999651f;
65  float c2 = 0.9999998881f;
66  float P1 = 0.04331970214844f;
67  float P2 = 1.99663646e-6f;
68  float k10e = 2.302585093f;
69  float pol, r, r2, r3, res, Ye;
70  unsigned int Ttemp, J, K;
71  float Nf;
72  int N;
73  double dT;
74 
75  Ye = k10e * a;
76 
77  /* Get N such that |N - x*16/ln(2)| is minimized */
78  Nf = (a * log2_base_x16) + Halfe;
79  N = (int) Nf; /* Cast from intermediate variable to appease MISRA */
80 
81  if ((a * log2_base_x16) < -Halfe) {
82  N--;
83  }
84 
85  /* Argument reduction, r, and polynomial approximation pol(r) */
86  r = (Ye - (P1 * (float) N)) - (P2 * (float) N);
87  r2 = r * r;
88  r3 = r * r2;
89 
90  pol = (r * c2) + ((r3 * c0) + (r2 * c1));
91 
92  /* Get index for ktable and jtable */
93  K = _extu((unsigned int) N, 28u, 30u);
94  J = (unsigned int) N & 0x3u;
95  dT = MATHLIB_kTable[K] * MATHLIB_jTable[J];
96 
97  /* Scale exponent to adjust for 2^M */
98  Ttemp = _hi(dT) + (((unsigned int) N >> 4) << 20);
99  dT = _itod(Ttemp, _lo(dT));
100 
101  res = (float) (dT * (1.0 + (double) pol));
102 
103  /* Early exit for small a */
104  if (_extu(_ftoi(Ye), 1u, 24u) < 114u) {
105  res = 1.0f + Ye;
106  }
107 
108  /* < LnMin returns 0 */
109  if (Ye < LnMine) {
110  res = 0.0f;
111  }
112 
113  /* > LnMax returns MAX */
114  if (Ye > LnMaxe) {
115  res = Maxe;
116  }
117 
118  return (res);
119 }
120 
121 #endif // MATHLIB_EXP10_SCALAR_H_
float MATHLIB_exp10_scalar_ci(float a)
const double MATHLIB_kTable[4]
const double MATHLIB_jTable[4]