MATHLIB User Guide
MATHLIB_exp2_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_EXP2_SCALAR_H_
35 #define MATHLIB_EXP2_SCALAR_H_
36 
37 /******************************************************************************/
38 /* */
39 /* Includes */
40 /* */
41 /******************************************************************************/
42 
43 #include "../common/MATHLIB_types.h"
44 #include "c6x_migration.h"
45 
46 /******************************************************************************/
47 /* */
48 /* Scalar/C6x implementation of exp2 */
49 /* */
50 /******************************************************************************/
51 
52 static inline float divspMod_exp2spi(float a, float b);
53 static inline float exp2sp_i(float a);
54 
55 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
56 #pragma CODE_SECTION(divspMod_exp2spi, ".text:optci");
57 #endif
58 
59 /* Pull in inline for divsp */
60 static inline float divspMod_exp2spi(float a, float b)
61 {
62  float TWO = 2.0f;
63  float X;
64  X = _rcpsp(b);
65  X = X * (TWO - (b * X));
66  X = X * (TWO - (b * X));
67  X = a * X;
68  return X;
69 }
70 
71 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
72 #pragma CODE_SECTION(MATHLIB_exp2_scalar_ci, ".text:optci");
73 #endif
74 
75 static inline float MATHLIB_exp2_scalar_ci(float a)
76 {
77  float Zeroe = 0.0f;
78  float Halfe = 0.5f;
79  float epse = 9.313225746e-10f; /* [2^-29] / 2 */
80  float LnMine = -126.0f;
81  float MAXe = 3.40282347e+38f;
82  float LnMaxe = 128.0f;
83  float a0e = 2.499999995e-1f;
84  float a1e = 4.1602886268e-3f;
85  float b0e = 0.5f;
86  float b1e = 4.9987178778e-2f;
87  float CC1E = 0.693359375f; /* 355/512 */
88  float CC2E = -2.12194440055e-4f; /* lne(2) - 355/512 */
89  float Ln2E = 1.442695040889f; /* ln(base 2) of e */
90  float k2e = 0.69314718056f; /* log (base e) of 2 */
91 
92  float Ye, Xe, We, Re, Se, Be, Ce, De;
93  int Ne;
94 
95  Ye = k2e * a;
96 
97  /* < epsilon returns unity */
98  if (_fabsf(Ye) < epse) {
99  Ye = 0.0f;
100  }
101 
102  Ce = Ye * Ln2E; /* base e --> base 2 argument */
103  Ne = _spint(Ce); /* get unbiased exponent as int */
104  Se = (float) Ne; /* float(int N) */
105 
106  Xe = (Ye - (Se * CC1E)) - (Se * CC2E); /* range reduction */
107  We = Xe * Xe;
108  Be = (b1e * We) + b0e; /* denominator */
109  De = ((a1e * We) + a0e) * Xe; /* numerator */
110 
111  Re = Halfe + divspMod_exp2spi(De, Be - De);
112  Se = _itof(_extu((unsigned int) Ne + 128u, 23u, 0u));
113  Ce = Re * Se; /* scale by power of 2 */
114 
115  /* < LnMin returns 0 */
116  if (a < LnMine) {
117  Ce = Zeroe;
118  }
119 
120  /* > LnMax returns MAX */
121  if (a > LnMaxe) {
122  Ce = MAXe;
123  }
124 
125  return Ce;
126 }
127 
128 #endif
static float MATHLIB_exp2_scalar_ci(float a)
static float divspMod_exp2spi(float a, float b)
static float exp2sp_i(float a)