MATHLIB User Guide
MATHLIB_atan_scalar.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  * MATHLIB -- TI Floating-Point Math Function Library *
3  * *
4  * *
5  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/ *
6  * *
7  * *
8  * Redistribution and use in source and binary forms, with or without *
9  * modification, are permitted provided that the following conditions *
10  * are met: *
11  * *
12  * Redistributions of source code must retain the above copyright *
13  * notice, this list of conditions and the following disclaimer. *
14  * *
15  * Redistributions in binary form must reproduce the above copyright *
16  * notice, this list of conditions and the following disclaimer in the *
17  * documentation and/or other materials provided with the *
18  * distribution. *
19  * *
20  * Neither the name of Texas Instruments Incorporated nor the names of *
21  * its contributors may be used to endorse or promote products derived *
22  * from this software without specific prior written permission. *
23  * *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
27  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
28  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
34  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
35  * ======================================================================== */
36 
37 /* ======================================================================= */
38 /* MATHLIB_atan_scalar.h - single precision floating point arctangent */
39 /* optimized inlined C Implementation (w/ Intrinsics) */
40 /* ======================================================================= */
41 
42 #ifndef MATHLIB_ATAN_SCALAR_H_
43 #define MATHLIB_ATAN_SCALAR_H_ 1
44 
45 typedef bool t_bool;
46 #define TRUE ((t_bool) true)
47 #define FALSE ((t_bool) false)
48 
49 #include <c6x_migration.h>
50 
51 static inline float divspMod_atanspi(float a, float b);
52 static inline float atan2f_sr1i_atanspi(float g1, bool s, bool an);
53 static inline float MATHLIB_atan_scalar(float a);
54 
55 /* Inline division (SP) */
56 static inline float cmn_DIVSP(float a, float b)
57 {
58  float TWO = 2.0f;
59  float X;
60  X = _rcpsp(b);
61  X = X * (TWO - (b * X));
62  X = X * (TWO - (b * X));
63  X = a * X;
64  return X;
65 }
66 
67 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
68 #pragma CODE_SECTION(divspMod_atanspi, ".text:optci");
69 #endif
70 
71 /* Pull in inline for divsp */
72 static inline float divspMod_atanspi(float a, float b) { return cmn_DIVSP(b, a); }
73 
74 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
75 #pragma CODE_SECTION(atan2f_sr1i_atanspi, ".text:optci");
76 #endif
77 
78 static inline float atan2f_sr1i_atanspi(float g1, t_bool s, t_bool an)
79 {
80  float coef;
81  float g2;
82  float g4;
83  float g6;
84  float g8;
85  float g10;
86  float g12;
87  float pol;
88  float tmp1;
89  float tmp2;
90  float pih = 1.57079632679f;
91  float c1 = 0.00230158202f;
92  float c2 = -0.01394551000f;
93  float c3 = 0.03937087815f;
94  float c4 = -0.07235669163f;
95  float c5 = 0.10521499322f;
96  float c6 = -0.14175076797f;
97  float c7 = 0.19989300877f;
98  float c8 = -0.33332930041f;
99 
100  coef = pih;
101  /* get coef based on the flags */
102 
103  /* check 'swap' flag */
104  if (s == FALSE) {
105  coef = 0.0f;
106  }
107 
108  /* check if input to atansp_i is negative */
109  if (an != FALSE) { /* MISRA requires explicit checks, != FALSE is faster than == TRUE*/
110  coef = -coef;
111  }
112 
113  /* calculate polynomial */
114  g2 = g1 * g1;
115  g4 = g2 * g2;
116  g6 = g2 * g4;
117  g8 = g4 * g4;
118  g10 = g6 * g4;
119  g12 = g8 * g4;
120 
121  tmp1 = ((c5 * g8) + (c6 * g6)) + ((c7 * g4) + (c8 * g2));
122  tmp2 = ((((c1 * g4) + (c2 * g2)) + c3) * g12) + (c4 * g10);
123 
124  pol = tmp1 + tmp2;
125  pol = (pol * g1) + g1;
126 
127  /* MISRA requires explicit checks, != FALSE is faster than == TRUE*/
128  return ((s != FALSE) ? (coef - pol) : (coef + pol));
129 }
130 
131 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
132 #pragma CODE_SECTION(MATHLIB_atan_scalar, ".text:optci");
133 #endif
134 
135 static inline float MATHLIB_atan_scalar(float a)
136 {
137  float g;
138  float res;
139  float temp = 1.0f;
140 
141  t_bool an;
142  t_bool s = FALSE;
143 
144  an = (a < 0.0f) ? TRUE : FALSE; /* flag for a negative */
145 
146  /* swap a and b before calling division sub routine if a > b */
147  if (_fabsf(a) > 1.0f) {
148  temp = a;
149  a = 1.0f;
150  s = TRUE; /* swap flag */
151  }
152 
153  g = divspMod_atanspi(temp, a);
154 
155  /* do polynomial estimation */
156  res = atan2f_sr1i_atanspi(g, s, an);
157 
158  if (a == 0.0f) {
159  res = 0.0f;
160  }
161 
162  return (res);
163 }
164 
165 #endif /* MATHLIB_ATAN_SCALAR_H_ */
166 
167 /* ======================================================================== */
168 /* End of file: MATHLIB_atan_scalar.h */
169 /* ======================================================================== */
static float cmn_DIVSP(float a, float b)
static float atan2f_sr1i_atanspi(float g1, bool s, bool an)
#define TRUE
#define FALSE
static float divspMod_atanspi(float a, float b)
static float MATHLIB_atan_scalar(float a)
bool t_bool