1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sin_cos_f32.c
4 * Description: Sine and Cosine calculation for floating-point values
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29 #include "dsp/controller_functions.h"
30 #include "arm_common_tables.h"
31
32 /**
33 @addtogroup SinCos
34 @{
35 */
36
37 /**
38 @brief Floating-point sin_cos function.
39 @param[in] theta input value in degrees
40 @param[out] pSinVal points to processed sine output
41 @param[out] pCosVal points to processed cosine output
42 */
43
arm_sin_cos_f32(float32_t theta,float32_t * pSinVal,float32_t * pCosVal)44 void arm_sin_cos_f32(
45 float32_t theta,
46 float32_t * pSinVal,
47 float32_t * pCosVal)
48 {
49 float32_t fract, in; /* Temporary input, output variables */
50 uint16_t indexS, indexC; /* Index variable */
51 float32_t f1, f2, d1, d2; /* Two nearest output values */
52 float32_t Dn, Df;
53 float32_t temp, findex;
54
55 /* input x is in degrees */
56 /* Scale input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */
57 in = theta * 0.00277777777778f;
58
59 if (in < 0.0f)
60 {
61 in = -in;
62 }
63
64 in = in - (int32_t)in;
65
66 /* Calculate the nearest index */
67 findex = (float32_t)FAST_MATH_TABLE_SIZE * in;
68 indexS = ((uint16_t)findex) & 0x1ff;
69 indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff;
70
71 /* Calculation of fractional value */
72 fract = findex - (float32_t) indexS;
73
74 /* Read two nearest values of input value from the cos & sin tables */
75 f1 = sinTable_f32[indexC ];
76 f2 = sinTable_f32[indexC+1];
77 d1 = -sinTable_f32[indexS ];
78 d2 = -sinTable_f32[indexS+1];
79
80 Dn = 0.0122718463030f; /* delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE */
81 Df = f2 - f1; /* delta between the values of the functions */
82
83 temp = Dn * (d1 + d2) - 2 * Df;
84 temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
85 temp = fract * temp + d1 * Dn;
86
87 /* Calculation of cosine value */
88 *pCosVal = fract * temp + f1;
89
90 /* Read two nearest values of input value from the cos & sin tables */
91 f1 = sinTable_f32[indexS ];
92 f2 = sinTable_f32[indexS+1];
93 d1 = sinTable_f32[indexC ];
94 d2 = sinTable_f32[indexC+1];
95
96
97 Df = f2 - f1; // delta between the values of the functions
98 temp = Dn * (d1 + d2) - 2 * Df;
99 temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
100 temp = fract * temp + d1 * Dn;
101
102 /* Calculation of sine value */
103 *pSinVal = fract * temp + f1;
104
105 if (theta < 0.0f)
106 {
107 *pSinVal = -*pSinVal;
108 }
109 }
110
111 /**
112 @} end of SinCos group
113 */
114