MATHLIB User Guide
MATHLIB_log2_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_LOG2_SCALAR_H_
35 #define MATHLIB_LOG2_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 log2 */
50 /* */
51 /******************************************************************************/
52 
53 static inline double divdpMod_log2dpi(double a, double b);
54 static inline float MATHLIB_log2_scalar_ci(float a);
55 
56 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
57 #pragma CODE_SECTION(divdpMod_log2dpi, ".text:optci");
58 #endif
59 
60 /* Pull in inline for divdp */
61 static inline double divdpMod_log2dpi(double a, double b)
62 {
63  float TWO = 2.0f;
64  float X;
65  X = _rcpsp(b);
66  X = X * (TWO - (b * X));
67  X = X * (TWO - (b * X));
68  X = a * X;
69  return X;
70 }
71 
72 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
73 #pragma CODE_SECTION(MATHLIB_log2_scalar_ci, ".text:optci");
74 #endif
75 
76 static inline float MATHLIB_log2_scalar_ci(float a)
77 {
78  double ln2 = 0.693147180559945;
79  double base = 1.4426950408890;
80  float c1 = -0.2302894f;
81  float c2 = 0.1908169f;
82  float c3 = -0.2505905f;
83  float c4 = 0.3333164f;
84  float c5 = -0.5000002f;
85  float MAXe = 3.402823466E+38f;
86  float pol, r1, r2, r3, r4, res;
87  double dr, frcpax, rcp, T;
88  unsigned int T_index;
89  int N;
90 
91  /* r = x * frcpa(x) -1 */
92  rcp = _rcpdp((double) a);
93  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
94  dr = (frcpax * (double) a) - 1.0;
95 
96  /* Polynomial p(r) that approximates ln(1+r) - r */
97  r1 = (float) dr;
98  r2 = r1 * r1;
99  r3 = r1 * r2;
100  r4 = r2 * r2;
101 
102  pol = (c5 * r2) + ((c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4));
103  pol *= (float) base;
104 
105  /* Reconstruction: result = T + r + p(r) */
106  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
107  T_index = _extu(_hi(frcpax), 12u, 29u);
108  T = (MATHLIB_logTable[T_index] - (ln2 * (double) N)) * base;
109  res = (float) ((dr * base) + T) + pol;
110 
111  if (a <= 0.0f) {
112  res = _itof(0xFF800000u);
113  }
114  if (a > MAXe) {
115  res = 1024.0f;
116  }
117 
118  return (res);
119 }
120 
121 #endif // MATHLIB_LOG2_SCALAR_H_
static float MATHLIB_log2_scalar_ci(float a)
static double divdpMod_log2dpi(double a, double b)
const double MATHLIB_logTable[8]