MATHLIB User Guide
MATHLIB_log_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_LOG_SCALAR_H_
35 #define MATHLIB_LOG_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 log */
50 /* */
51 /******************************************************************************/
52 
53 static inline float MATHLIB_log_scalar_ci(float a)
54 {
55  double ln2 = 0.693147180559945f;
56  float c1 = -0.2302894f;
57  float c2 = 0.1908169f;
58  float c3 = -0.2505905f;
59  float c4 = 0.3333164f;
60  float c5 = -0.5000002f;
61  float MAXe = 3.402823466E+38f;
62  float pol, r1, r2, r3, r4, res;
63  double dr, frcpax, rcp, T;
64  unsigned int T_index;
65  int N;
66 
67  /* r = x * frcpa(x) -1 */
68  rcp = _rcpdp((double) a);
69  frcpax = _itod(_clr(_hi(rcp), 0u, 16u), 0u);
70  dr = (frcpax * (double) a) - 1.0;
71 
72  /* Polynomial p(r) that approximates ln(1+r) - r */
73  r1 = (float) dr;
74  r2 = r1 * r1;
75  r3 = r1 * r2;
76  r4 = r2 * r2;
77 
78  pol = (c5 * r2) + ((c4 * r3) + ((((c2 * r1) + c3) + (c1 * r2)) * r4));
79 
80  /* Reconstruction: result = T + r + p(r) */
81  N = (int) _extu(_hi(frcpax), 1u, 21u) - 1023;
82  T_index = _extu(_hi(frcpax), 12u, 29u);
83  T = MATHLIB_logTable[T_index] - (ln2 * (double) N);
84  res = (float) (dr + T) + pol;
85 
86  if (a <= 0.0f) {
87  res = _itof(0xFF800000u);
88  }
89  if (a > MAXe) {
90  res = 709.7827f;
91  }
92 
93  return (res);
94 }
95 
96 #endif // MATHLIB_LOG_SCALAR_H_
static float MATHLIB_log_scalar_ci(float a)
const double MATHLIB_logTable[8]