MATHLIB User Guide
MATHLIB_exp_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_EXP_SCALAR_H_
35 #define MATHLIB_EXP_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_exp_scalar_ci, ".text:optci");
54 #endif
55 
56 inline float MATHLIB_exp_scalar_ci(float x)
57 {
58  const float log2_base_x16 = 23.083120654f; /*1.442695041 * 16*/
59  const float half = 0.5f;
60  const float LnMin = -87.33654475f;
61  const float LnMax = 88.72283905f;
62  const float Max = 3.402823466E+38f;
63  const double p = 0.0433216987816623; /* 1/log2_base_x16 */
64 
65  /* coefficients to approximate the decimal part of the result */
66  const float c0 = 0.166668549286041f;
67  const float c1 = 0.500016170012920f;
68  const float c2 = 0.999999998618401f;
69 
70  float pol, r, r2, r3, res;
71  unsigned int Ttemp, J, K;
72  float Nf;
73  int N;
74  double dT;
75 
76  /* Get N such that |N - x*16/ln(2)| is minimized */
77  Nf = (x * log2_base_x16) + half;
78  N = (int) Nf; /* Cast from intermediate variable to appease MISRA */
79 
80  if ((x * log2_base_x16) < -half) {
81  N--;
82  }
83 
84  /* Argument reduction, r, and polynomial approximation pol(r) */
85  r = (float) ((double) x - (p * (double) N));
86  r2 = r * r;
87  r3 = r * r2;
88 
89  pol = (r * c2) + ((r3 * c0) + (r2 * c1));
90 
91  /* Get index for ktable and jtable */
92  K = _extu((unsigned int) N, 28u, 30u);
93  J = (unsigned int) N & 0x3u;
94  dT = MATHLIB_kTable[K] * MATHLIB_jTable[J];
95 
96  /* Scale exponent to adjust for 2^M */
97  Ttemp = _hi(dT) + (((unsigned int) N >> 4) << 20);
98  dT = _itod(Ttemp, _lo(dT));
99  res = (float) dT * (1.0f + pol);
100 
101  /* < LnMin returns 0 */
102  if (x < LnMin) {
103  res = 0.0f;
104  }
105 
106  /* > LnMax returns MAX */
107  if (x > LnMax) {
108  res = Max;
109  }
110 
111  return (res);
112 }
113 
114 #endif // MATHLIB_EXP_SCALAR_H_
float MATHLIB_exp_scalar_ci(float x)
const double MATHLIB_kTable[4]
const double MATHLIB_jTable[4]