MATHLIB User Guide
MATHLIB_sin_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_sin_scalar.h - single and double precision floating point sine */
39 /* optimized inlined C implementation (w/ intrinsics) */
40 /* ======================================================================= */
41 
42 #ifndef MATHLIB_SIN_SCALAR_H_
43 #define MATHLIB_SIN_SCALAR_H_ 1
44 
45 #include <c6x_migration.h>
46 
47 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
48 #pragma CODE_SECTION(MATHLIB_sin_scalar, ".text:optci");
49 #endif
50 
51 template <typename T> static inline T MATHLIB_sin_scalar_ci(T a);
52 
53 template <> inline float MATHLIB_sin_scalar_ci<float>(float a)
54 {
55  float InvPI = 0.318309886183791f;
56  float One = 1.0f;
57  float MAX = 1048576.0f;
58  float Zero = 0.0f;
59  float s1 = -1.666665668e-1f;
60  float s2 = 8.333025139e-3f;
61  float s3 = -1.980741872e-4f;
62  float s4 = 2.601903036e-6f;
63  float C1 = 3.140625f;
64  float C2 = 9.67653589793e-4f;
65  float Sign, X, Y, Z, F, G, R;
66  int N;
67 
68  Sign = One;
69  Y = a;
70 
71  if (_fabsf(Y) > MAX) {
72  Y = Zero;
73  }
74 
75  X = Y * InvPI; /* X = Y * (1/PI) */
76  N = _spint(X); /* N = integer part of X */
77  Z = (float) N;
78 
79  if ((N % 2) != 0) {
80  Sign = -Sign; /* Quadrant 3 or 4 */
81  }
82 
83  F = (Y - (Z * C1)) - (Z * C2);
84  G = F * F;
85  R = ((((((s4 * G) + s3) * G) + s2) * G) + s1) * G;
86 
87  return ((F + (F * R)) * Sign);
88 }
89 
90 template <> inline double MATHLIB_sin_scalar_ci<double>(double a)
91 {
92  double InvPI = 0.31830988618379067154;
93  double C1 = 3.1416015625;
94  double C2 = -8.908910206761537356617e-6;
95  double r8 = 2.7204790957888846175e-15;
96  double r7 = -7.6429178068910467734e-13;
97  double r6 = 1.6058936490371589114e-10;
98  double r5 = -2.5052106798274584544e-8;
99  double r4 = 2.7557319210152756119e-6;
100  double r3 = -1.9841269841201840457e-4;
101  double r2 = 8.3333333333331650314e-3;
102  double r1 = -1.6666666666666665052e-1;
103  double MAX = 1.073741824e+09;
104  double Zero = 0.0;
105  double Sign = 1.0;
106  double X, Z, F1, F2, G, R;
107  int N;
108 
109  F1 = a;
110 
111  if (_fabs(F1) > MAX) {
112  F1 = Zero;
113  }
114 
115  X = F1 * InvPI; /* X = Y/PI */
116  N = _dpint(X);
117  Z = (double) N;
118 
119  if ((N % 2) != 0) {
120  Sign = -Sign; /* neg. quadrants */
121  }
122 
123  F1 = (F1 - (Z * C1)) - (Z * C2);
124  F2 = F1 * F1;
125  G = F2 * F2;
126 
127  R = ((((((G * r8) + r6) * G) + r4) * G) + r2) * G;
128  X = ((((((G * r7) + r5) * G) + r3) * G) + r1) * F2;
129  R = R + X;
130  G = (F1 + (F1 * R)) * Sign;
131 
132  return (G);
133 }
134 
135 extern "C" {
136 static inline float MATHLIB_sin_scalar_sp(float a)
137 {
138  float result = MATHLIB_sin_scalar_ci<float>(a);
139  return result;
140 }
141 
142 static inline double MATHLIB_sin_scalar_dp(double a)
143 {
144  double result = MATHLIB_sin_scalar_ci<double>(a);
145  return result;
146 }
147 }
148 
149 #endif
150 
151 /* ======================================================================== */
152 /* End of file: MATHLIB_sin_scalar.h */
153 /* ======================================================================== */
double MATHLIB_sin_scalar_ci< double >(double a)
float MATHLIB_sin_scalar_ci< float >(float a)
static float MATHLIB_sin_scalar_sp(float a)
static double MATHLIB_sin_scalar_dp(double a)
static T MATHLIB_sin_scalar_ci(T a)